OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/contacts/contact_database.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "leveldb/db.h" |
| 15 #include "leveldb/iterator.h" |
| 16 #include "leveldb/options.h" |
| 17 #include "leveldb/slice.h" |
| 18 #include "leveldb/status.h" |
| 19 #include "leveldb/write_batch.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace contacts { |
| 24 |
| 25 ContactDatabase::ContactDatabase() : weak_ptr_factory_(this) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 27 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 28 task_runner_ = pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
| 29 } |
| 30 |
| 31 void ContactDatabase::DestroyOnUIThread() { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 34 task_runner_->PostNonNestableTask( |
| 35 FROM_HERE, |
| 36 base::Bind(&ContactDatabase::DestroyFromTaskRunner, |
| 37 base::Unretained(this))); |
| 38 } |
| 39 |
| 40 void ContactDatabase::Init(const FilePath& database_dir, |
| 41 InitCallback callback) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 bool* success = new bool(false); |
| 44 task_runner_->PostTaskAndReply( |
| 45 FROM_HERE, |
| 46 base::Bind(&ContactDatabase::InitFromTaskRunner, |
| 47 base::Unretained(this), |
| 48 database_dir, |
| 49 success), |
| 50 base::Bind(&ContactDatabase::RunInitCallback, |
| 51 weak_ptr_factory_.GetWeakPtr(), |
| 52 callback, |
| 53 base::Owned(success))); |
| 54 } |
| 55 |
| 56 void ContactDatabase::SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 57 bool is_full_update, |
| 58 SaveCallback callback) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 bool* success = new bool(false); |
| 61 task_runner_->PostTaskAndReply( |
| 62 FROM_HERE, |
| 63 base::Bind(&ContactDatabase::SaveContactsFromTaskRunner, |
| 64 base::Unretained(this), |
| 65 base::Passed(contacts.Pass()), |
| 66 is_full_update, |
| 67 success), |
| 68 base::Bind(&ContactDatabase::RunSaveCallback, |
| 69 weak_ptr_factory_.GetWeakPtr(), |
| 70 callback, |
| 71 base::Owned(success))); |
| 72 } |
| 73 |
| 74 void ContactDatabase::LoadContacts(LoadCallback callback) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 76 bool* success = new bool(false); |
| 77 scoped_ptr<ScopedVector<Contact> > contacts(new ScopedVector<Contact>); |
| 78 ScopedVector<Contact>* contacts_ptr = contacts.get(); |
| 79 task_runner_->PostTaskAndReply( |
| 80 FROM_HERE, |
| 81 base::Bind(&ContactDatabase::LoadContactsFromTaskRunner, |
| 82 base::Unretained(this), |
| 83 success, |
| 84 contacts_ptr), |
| 85 base::Bind(&ContactDatabase::RunLoadCallback, |
| 86 weak_ptr_factory_.GetWeakPtr(), |
| 87 callback, |
| 88 base::Owned(success), |
| 89 base::Passed(contacts.Pass()))); |
| 90 } |
| 91 |
| 92 ContactDatabase::~ContactDatabase() { |
| 93 DCHECK(IsRunByTaskRunner()); |
| 94 } |
| 95 |
| 96 bool ContactDatabase::IsRunByTaskRunner() const { |
| 97 return BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread(); |
| 98 } |
| 99 |
| 100 void ContactDatabase::DestroyFromTaskRunner() { |
| 101 DCHECK(IsRunByTaskRunner()); |
| 102 delete this; |
| 103 } |
| 104 |
| 105 void ContactDatabase::RunInitCallback(InitCallback callback, |
| 106 const bool* success) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 108 callback.Run(*success); |
| 109 } |
| 110 |
| 111 void ContactDatabase::RunSaveCallback(SaveCallback callback, |
| 112 const bool* success) { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 114 callback.Run(*success); |
| 115 } |
| 116 |
| 117 void ContactDatabase::RunLoadCallback( |
| 118 LoadCallback callback, |
| 119 const bool* success, |
| 120 scoped_ptr<ScopedVector<Contact> > contacts) { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 122 callback.Run(*success, contacts.Pass()); |
| 123 } |
| 124 |
| 125 void ContactDatabase::InitFromTaskRunner(const FilePath& database_dir, |
| 126 bool* success) { |
| 127 DCHECK(IsRunByTaskRunner()); |
| 128 DCHECK(success); |
| 129 VLOG(1) << "Opening " << database_dir.value(); |
| 130 |
| 131 *success = false; |
| 132 |
| 133 leveldb::Options options; |
| 134 options.create_if_missing = true; |
| 135 bool delete_and_retry_on_corruption = true; |
| 136 |
| 137 while (true) { |
| 138 leveldb::DB* db = NULL; |
| 139 leveldb::Status status = |
| 140 leveldb::DB::Open(options, database_dir.value(), &db); |
| 141 if (status.ok()) { |
| 142 CHECK(db); |
| 143 db_.reset(db); |
| 144 *success = true; |
| 145 return; |
| 146 } |
| 147 |
| 148 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " |
| 149 << status.ToString(); |
| 150 |
| 151 // Delete the existing database and try again (just once, though). |
| 152 if (status.IsCorruption() && delete_and_retry_on_corruption) { |
| 153 LOG(WARNING) << "Deleting possibly-corrupt database"; |
| 154 file_util::Delete(database_dir, true); |
| 155 delete_and_retry_on_corruption = false; |
| 156 } else { |
| 157 break; |
| 158 } |
| 159 } |
| 160 } |
| 161 |
| 162 void ContactDatabase::SaveContactsFromTaskRunner( |
| 163 scoped_ptr<ContactPointers> contacts, |
| 164 bool is_full_update, |
| 165 bool* success) { |
| 166 DCHECK(IsRunByTaskRunner()); |
| 167 DCHECK(success); |
| 168 VLOG(1) << "Saving " << contacts->size() << " contact(s) to database as " |
| 169 << (is_full_update ? "full" : "partial") << " update"; |
| 170 |
| 171 *success = false; |
| 172 |
| 173 // If we're doing a full update, find all of the existing keys first so we can |
| 174 // delete ones that aren't present in the new set of contacts. |
| 175 std::set<std::string> keys_to_delete; |
| 176 if (is_full_update) { |
| 177 leveldb::ReadOptions options; |
| 178 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); |
| 179 db_iterator->SeekToFirst(); |
| 180 while (db_iterator->Valid()) { |
| 181 keys_to_delete.insert(db_iterator->key().ToString()); |
| 182 db_iterator->Next(); |
| 183 } |
| 184 } |
| 185 |
| 186 // TODO(derat): Serializing all of the contacts and so we can write them in a |
| 187 // single batch may be expensive, memory-wise. Consider writing them in |
| 188 // several batches instead. (To avoid using partial writes in the event of a |
| 189 // crash, maybe add a dummy "write completed" contact that's removed in the |
| 190 // first batch and added in the last.) |
| 191 leveldb::WriteBatch updates; |
| 192 for (ContactPointers::const_iterator it = contacts->begin(); |
| 193 it != contacts->end(); ++it) { |
| 194 const contacts::Contact& contact = **it; |
| 195 updates.Put(leveldb::Slice(contact.provider_id()), |
| 196 leveldb::Slice(contact.SerializeAsString())); |
| 197 keys_to_delete.erase(contact.provider_id()); |
| 198 } |
| 199 |
| 200 for (std::set<std::string>::const_iterator it = keys_to_delete.begin(); |
| 201 it != keys_to_delete.end(); ++it) { |
| 202 updates.Delete(leveldb::Slice(*it)); |
| 203 } |
| 204 |
| 205 leveldb::WriteOptions options; |
| 206 options.sync = true; |
| 207 leveldb::Status status = db_->Write(options, &updates); |
| 208 if (status.ok()) |
| 209 *success = true; |
| 210 else |
| 211 LOG(WARNING) << "Failed writing contacts: " << status.ToString(); |
| 212 } |
| 213 |
| 214 void ContactDatabase::LoadContactsFromTaskRunner( |
| 215 bool* success, |
| 216 ScopedVector<Contact>* contacts_out) { |
| 217 DCHECK(IsRunByTaskRunner()); |
| 218 DCHECK(success); |
| 219 DCHECK(contacts_out); |
| 220 |
| 221 *success = false; |
| 222 contacts_out->clear(); |
| 223 |
| 224 leveldb::ReadOptions options; |
| 225 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); |
| 226 db_iterator->SeekToFirst(); |
| 227 while (db_iterator->Valid()) { |
| 228 scoped_ptr<Contact> contact(new Contact); |
| 229 leveldb::Slice value_slice = db_iterator->value(); |
| 230 if (!contact->ParseFromArray(value_slice.data(), value_slice.size())) { |
| 231 LOG(WARNING) << "Unable to parse contact " |
| 232 << db_iterator->key().ToString(); |
| 233 return; |
| 234 } |
| 235 contacts_out->push_back(contact.release()); |
| 236 db_iterator->Next(); |
| 237 } |
| 238 |
| 239 *success = true; |
| 240 } |
| 241 |
| 242 } // namespace contacts |
OLD | NEW |