OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 Loading... |
157 base::JSONReader json_reader; | 157 base::JSONReader json_reader; |
158 leveldb::ReadOptions options = leveldb::ReadOptions(); | 158 leveldb::ReadOptions options = leveldb::ReadOptions(); |
159 // All interaction with the db is done on the same thread, so snapshotting | 159 // All interaction with the db is done on the same thread, so snapshotting |
160 // isn't strictly necessary. This is just defensive. | 160 // isn't strictly necessary. This is just defensive. |
161 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); | 161 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); |
162 | 162 |
163 ScopedSnapshot snapshot(db_.get()); | 163 ScopedSnapshot snapshot(db_.get()); |
164 options.snapshot = snapshot.get(); | 164 options.snapshot = snapshot.get(); |
165 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 165 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); |
166 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 166 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
167 Value* value = | 167 Value* value = json_reader.ReadToValue(it->value().ToString()); |
168 json_reader.JsonToValue(it->value().ToString(), false, false); | |
169 if (value != NULL) { | 168 if (value != NULL) { |
170 settings->SetWithoutPathExpansion(it->key().ToString(), value); | 169 settings->SetWithoutPathExpansion(it->key().ToString(), value); |
171 } else { | 170 } else { |
172 // TODO(kalman): clear the offending non-JSON value from the database. | 171 // TODO(kalman): clear the offending non-JSON value from the database. |
173 LOG(ERROR) << "Invalid JSON: " << it->value().ToString(); | 172 LOG(ERROR) << "Invalid JSON: " << it->value().ToString(); |
174 } | 173 } |
175 } | 174 } |
176 | 175 |
177 if (!it->status().ok()) { | 176 if (!it->status().ok()) { |
178 LOG(ERROR) << "DB iteration failed: " << it->status().ToString(); | 177 LOG(ERROR) << "DB iteration failed: " << it->status().ToString(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 // isn't strictly necessary. This is just defensive. | 268 // isn't strictly necessary. This is just defensive. |
270 leveldb::WriteBatch batch; | 269 leveldb::WriteBatch batch; |
271 scoped_ptr<SettingChangeList> changes(new SettingChangeList()); | 270 scoped_ptr<SettingChangeList> changes(new SettingChangeList()); |
272 | 271 |
273 ScopedSnapshot snapshot(db_.get()); | 272 ScopedSnapshot snapshot(db_.get()); |
274 read_options.snapshot = snapshot.get(); | 273 read_options.snapshot = snapshot.get(); |
275 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(read_options)); | 274 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(read_options)); |
276 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 275 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
277 const std::string key = it->key().ToString(); | 276 const std::string key = it->key().ToString(); |
278 const std::string old_value_json = it->value().ToString(); | 277 const std::string old_value_json = it->value().ToString(); |
279 Value* old_value = | 278 Value* old_value = base::JSONReader().ReadToValue(old_value_json); |
280 base::JSONReader().JsonToValue(old_value_json, false, false); | |
281 if (old_value) { | 279 if (old_value) { |
282 changes->push_back(SettingChange(key, old_value, NULL)); | 280 changes->push_back(SettingChange(key, old_value, NULL)); |
283 } else { | 281 } else { |
284 LOG(ERROR) << "Invalid JSON in database: " << old_value_json; | 282 LOG(ERROR) << "Invalid JSON in database: " << old_value_json; |
285 } | 283 } |
286 batch.Delete(key); | 284 batch.Delete(key); |
287 } | 285 } |
288 | 286 |
289 if (!it->status().ok()) { | 287 if (!it->status().ok()) { |
290 LOG(WARNING) << "Clear iteration failed: " << it->status().ToString(); | 288 LOG(WARNING) << "Clear iteration failed: " << it->status().ToString(); |
(...skipping 21 matching lines...) Expand all Loading... |
312 if (s.IsNotFound()) { | 310 if (s.IsNotFound()) { |
313 // Despite there being no value, it was still a success. | 311 // Despite there being no value, it was still a success. |
314 return true; | 312 return true; |
315 } | 313 } |
316 | 314 |
317 if (!s.ok()) { | 315 if (!s.ok()) { |
318 LOG(ERROR) << "Error reading from database: " << s.ToString(); | 316 LOG(ERROR) << "Error reading from database: " << s.ToString(); |
319 return false; | 317 return false; |
320 } | 318 } |
321 | 319 |
322 Value* value = base::JSONReader().JsonToValue(value_as_json, false, false); | 320 Value* value = base::JSONReader().ReadToValue(value_as_json); |
323 if (value == NULL) { | 321 if (value == NULL) { |
324 // TODO(kalman): clear the offending non-JSON value from the database. | 322 // TODO(kalman): clear the offending non-JSON value from the database. |
325 LOG(ERROR) << "Invalid JSON in database: " << value_as_json; | 323 LOG(ERROR) << "Invalid JSON in database: " << value_as_json; |
326 return false; | 324 return false; |
327 } | 325 } |
328 | 326 |
329 setting->reset(value); | 327 setting->reset(value); |
330 return true; | 328 return true; |
331 } | 329 } |
332 | 330 |
333 bool SettingsLeveldbStorage::IsEmpty() { | 331 bool SettingsLeveldbStorage::IsEmpty() { |
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
335 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); | 333 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
336 | 334 |
337 it->SeekToFirst(); | 335 it->SeekToFirst(); |
338 bool is_empty = !it->Valid(); | 336 bool is_empty = !it->Valid(); |
339 if (!it->status().ok()) { | 337 if (!it->status().ok()) { |
340 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); | 338 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); |
341 return false; | 339 return false; |
342 } | 340 } |
343 return is_empty; | 341 return is_empty; |
344 } | 342 } |
345 | 343 |
346 } // namespace extensions | 344 } // namespace extensions |
OLD | NEW |