Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(757)

Side by Side Diff: chrome/browser/extensions/settings/settings_leveldb_storage.cc

Issue 8587025: Extension settings API: force through changes that come from sync (ignoring (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/settings/settings_leveldb_storage.h" 5 #include "chrome/browser/extensions/settings/settings_leveldb_storage.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 157
158 if (!it->status().ok()) { 158 if (!it->status().ok()) {
159 LOG(ERROR) << "DB iteration failed: " << it->status().ToString(); 159 LOG(ERROR) << "DB iteration failed: " << it->status().ToString();
160 return ReadResult(kGenericOnFailureMessage); 160 return ReadResult(kGenericOnFailureMessage);
161 } 161 }
162 162
163 return ReadResult(settings.release()); 163 return ReadResult(settings.release());
164 } 164 }
165 165
166 SettingsStorage::WriteResult SettingsLeveldbStorage::Set( 166 SettingsStorage::WriteResult SettingsLeveldbStorage::Set(
167 const std::string& key, const Value& value) { 167 WriteOptions options, const std::string& key, const Value& value) {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
169 DictionaryValue settings; 169 DictionaryValue settings;
170 settings.SetWithoutPathExpansion(key, value.DeepCopy()); 170 settings.SetWithoutPathExpansion(key, value.DeepCopy());
171 return Set(settings); 171 return Set(options, settings);
172 } 172 }
173 173
174 SettingsStorage::WriteResult SettingsLeveldbStorage::Set( 174 SettingsStorage::WriteResult SettingsLeveldbStorage::Set(
175 const DictionaryValue& settings) { 175 WriteOptions options, const DictionaryValue& settings) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
177 177
178 std::string value_as_json; 178 std::string value_as_json;
179 leveldb::WriteBatch batch; 179 leveldb::WriteBatch batch;
180 scoped_ptr<SettingChangeList> changes( 180 scoped_ptr<SettingChangeList> changes(new SettingChangeList());
181 new SettingChangeList());
182 181
183 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { 182 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) {
184 scoped_ptr<Value> old_value; 183 scoped_ptr<Value> old_value;
185 if (!ReadFromDb(leveldb::ReadOptions(), it.key(), &old_value)) { 184 if (!ReadFromDb(leveldb::ReadOptions(), it.key(), &old_value)) {
186 return WriteResult(kGenericOnFailureMessage); 185 return WriteResult(kGenericOnFailureMessage);
187 } 186 }
188 187
189 if (!old_value.get() || !old_value->Equals(&it.value())) { 188 if (!old_value.get() || !old_value->Equals(&it.value())) {
190 changes->push_back( 189 changes->push_back(
191 SettingChange( 190 SettingChange(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 LOG(WARNING) << "DB batch delete failed: " << status.ToString(); 238 LOG(WARNING) << "DB batch delete failed: " << status.ToString();
240 return WriteResult(kGenericOnFailureMessage); 239 return WriteResult(kGenericOnFailureMessage);
241 } 240 }
242 241
243 return WriteResult(changes.release()); 242 return WriteResult(changes.release());
244 } 243 }
245 244
246 SettingsStorage::WriteResult SettingsLeveldbStorage::Clear() { 245 SettingsStorage::WriteResult SettingsLeveldbStorage::Clear() {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
248 247
249 leveldb::ReadOptions options; 248 leveldb::ReadOptions read_options;
250 // All interaction with the db is done on the same thread, so snapshotting 249 // All interaction with the db is done on the same thread, so snapshotting
251 // isn't strictly necessary. This is just defensive. 250 // isn't strictly necessary. This is just defensive.
252 leveldb::WriteBatch batch; 251 leveldb::WriteBatch batch;
253 scoped_ptr<SettingChangeList> changes( 252 scoped_ptr<SettingChangeList> changes(new SettingChangeList());
254 new SettingChangeList());
255 253
256 ScopedSnapshot snapshot(db_.get()); 254 ScopedSnapshot snapshot(db_.get());
257 options.snapshot = snapshot.get(); 255 read_options.snapshot = snapshot.get();
258 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); 256 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(read_options));
259 for (it->SeekToFirst(); it->Valid(); it->Next()) { 257 for (it->SeekToFirst(); it->Valid(); it->Next()) {
260 const std::string key = it->key().ToString(); 258 const std::string key = it->key().ToString();
261 const std::string old_value_json = it->value().ToString(); 259 const std::string old_value_json = it->value().ToString();
262 Value* old_value = 260 Value* old_value =
263 base::JSONReader().JsonToValue(old_value_json, false, false); 261 base::JSONReader().JsonToValue(old_value_json, false, false);
264 if (old_value) { 262 if (old_value) {
265 changes->push_back(SettingChange(key, old_value, NULL)); 263 changes->push_back(SettingChange(key, old_value, NULL));
266 } else { 264 } else {
267 LOG(ERROR) << "Invalid JSON in database: " << old_value_json; 265 LOG(ERROR) << "Invalid JSON in database: " << old_value_json;
268 } 266 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 it->SeekToFirst(); 318 it->SeekToFirst();
321 bool is_empty = !it->Valid(); 319 bool is_empty = !it->Valid();
322 if (!it->status().ok()) { 320 if (!it->status().ok()) {
323 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); 321 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString();
324 return false; 322 return false;
325 } 323 }
326 return is_empty; 324 return is_empty;
327 } 325 }
328 326
329 } // namespace extensions 327 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698