| OLD | NEW |
| 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/extension_settings_leveldb_storage.h" | 5 #include "chrome/browser/extensions/extension_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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 const std::string& key) { | 93 const std::string& key) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 95 scoped_ptr<Value> setting; | 95 scoped_ptr<Value> setting; |
| 96 if (!ReadFromDb(leveldb::ReadOptions(), key, &setting)) { | 96 if (!ReadFromDb(leveldb::ReadOptions(), key, &setting)) { |
| 97 return Result(kGenericOnFailureMessage); | 97 return Result(kGenericOnFailureMessage); |
| 98 } | 98 } |
| 99 DictionaryValue* settings = new DictionaryValue(); | 99 DictionaryValue* settings = new DictionaryValue(); |
| 100 if (setting.get()) { | 100 if (setting.get()) { |
| 101 settings->SetWithoutPathExpansion(key, setting.release()); | 101 settings->SetWithoutPathExpansion(key, setting.release()); |
| 102 } | 102 } |
| 103 return Result(settings, NULL); | 103 return Result(settings, NULL, NULL); |
| 104 } | 104 } |
| 105 | 105 |
| 106 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get( | 106 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get( |
| 107 const std::vector<std::string>& keys) { | 107 const std::vector<std::string>& keys) { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 109 leveldb::ReadOptions options; | 109 leveldb::ReadOptions options; |
| 110 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); | 110 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); |
| 111 | 111 |
| 112 // All interaction with the db is done on the same thread, so snapshotting | 112 // All interaction with the db is done on the same thread, so snapshotting |
| 113 // isn't strictly necessary. This is just defensive. | 113 // isn't strictly necessary. This is just defensive. |
| 114 ScopedSnapshot snapshot(db_.get()); | 114 ScopedSnapshot snapshot(db_.get()); |
| 115 options.snapshot = snapshot.get(); | 115 options.snapshot = snapshot.get(); |
| 116 for (std::vector<std::string>::const_iterator it = keys.begin(); | 116 for (std::vector<std::string>::const_iterator it = keys.begin(); |
| 117 it != keys.end(); ++it) { | 117 it != keys.end(); ++it) { |
| 118 scoped_ptr<Value> setting; | 118 scoped_ptr<Value> setting; |
| 119 if (!ReadFromDb(options, *it, &setting)) { | 119 if (!ReadFromDb(options, *it, &setting)) { |
| 120 return Result(kGenericOnFailureMessage); | 120 return Result(kGenericOnFailureMessage); |
| 121 } | 121 } |
| 122 if (setting.get()) { | 122 if (setting.get()) { |
| 123 settings->SetWithoutPathExpansion(*it, setting.release()); | 123 settings->SetWithoutPathExpansion(*it, setting.release()); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 return Result(settings.release(), NULL); | 127 return Result(settings.release(), NULL, NULL); |
| 128 } | 128 } |
| 129 | 129 |
| 130 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get() { | 130 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get() { |
| 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 132 base::JSONReader json_reader; | 132 base::JSONReader json_reader; |
| 133 leveldb::ReadOptions options = leveldb::ReadOptions(); | 133 leveldb::ReadOptions options = leveldb::ReadOptions(); |
| 134 // All interaction with the db is done on the same thread, so snapshotting | 134 // All interaction with the db is done on the same thread, so snapshotting |
| 135 // isn't strictly necessary. This is just defensive. | 135 // isn't strictly necessary. This is just defensive. |
| 136 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); | 136 scoped_ptr<DictionaryValue> settings(new DictionaryValue()); |
| 137 | 137 |
| 138 ScopedSnapshot snapshot(db_.get()); | 138 ScopedSnapshot snapshot(db_.get()); |
| 139 options.snapshot = snapshot.get(); | 139 options.snapshot = snapshot.get(); |
| 140 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 140 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); |
| 141 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 141 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 142 Value* value = | 142 Value* value = |
| 143 json_reader.JsonToValue(it->value().ToString(), false, false); | 143 json_reader.JsonToValue(it->value().ToString(), false, false); |
| 144 if (value != NULL) { | 144 if (value != NULL) { |
| 145 settings->SetWithoutPathExpansion(it->key().ToString(), value); | 145 settings->SetWithoutPathExpansion(it->key().ToString(), value); |
| 146 } else { | 146 } else { |
| 147 // TODO(kalman): clear the offending non-JSON value from the database. | 147 // TODO(kalman): clear the offending non-JSON value from the database. |
| 148 LOG(ERROR) << "Invalid JSON: " << it->value().ToString(); | 148 LOG(ERROR) << "Invalid JSON: " << it->value().ToString(); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 if (!it->status().ok()) { | 152 if (!it->status().ok()) { |
| 153 LOG(ERROR) << "DB iteration failed: " << it->status().ToString(); | 153 LOG(ERROR) << "DB iteration failed: " << it->status().ToString(); |
| 154 return Result(kGenericOnFailureMessage); | 154 return Result(kGenericOnFailureMessage); |
| 155 } | 155 } |
| 156 | 156 |
| 157 return Result(settings.release(), NULL); | 157 return Result(settings.release(), NULL, NULL); |
| 158 } | 158 } |
| 159 | 159 |
| 160 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set( | 160 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set( |
| 161 const std::string& key, const Value& value) { | 161 const std::string& key, const Value& value) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 163 DictionaryValue settings; | 163 DictionaryValue settings; |
| 164 settings.SetWithoutPathExpansion(key, value.DeepCopy()); | 164 settings.SetWithoutPathExpansion(key, value.DeepCopy()); |
| 165 return Set(settings); | 165 return Set(settings); |
| 166 } | 166 } |
| 167 | 167 |
| 168 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set( | 168 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set( |
| 169 const DictionaryValue& settings) { | 169 const DictionaryValue& settings) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 171 scoped_ptr<DictionaryValue> old_settings(new DictionaryValue()); |
| 171 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); | 172 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); |
| 172 std::string value_as_json; | 173 std::string value_as_json; |
| 173 leveldb::WriteBatch batch; | 174 leveldb::WriteBatch batch; |
| 174 | 175 |
| 175 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 176 for (DictionaryValue::key_iterator it = settings.begin_keys(); |
| 176 it != settings.end_keys(); ++it) { | 177 it != settings.end_keys(); ++it) { |
| 177 scoped_ptr<Value> original_value; | 178 scoped_ptr<Value> old_value; |
| 178 if (!ReadFromDb(leveldb::ReadOptions(), *it, &original_value)) { | 179 if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) { |
| 179 return Result(kGenericOnFailureMessage); | 180 return Result(kGenericOnFailureMessage); |
| 180 } | 181 } |
| 181 | 182 |
| 182 Value* new_value = NULL; | 183 Value* new_value = NULL; |
| 183 settings.GetWithoutPathExpansion(*it, &new_value); | 184 settings.GetWithoutPathExpansion(*it, &new_value); |
| 184 if (!original_value.get() || !original_value->Equals(new_value)) { | 185 if (!old_value.get() || !old_value->Equals(new_value)) { |
| 185 changed_keys->insert(*it); | 186 changed_keys->insert(*it); |
| 186 base::JSONWriter::Write(new_value, false, &value_as_json); | 187 base::JSONWriter::Write(new_value, false, &value_as_json); |
| 187 batch.Put(*it, value_as_json); | 188 batch.Put(*it, value_as_json); |
| 188 } | 189 } |
| 190 |
| 191 if (old_value.get()) { |
| 192 old_settings->SetWithoutPathExpansion(*it, old_value.release()); |
| 193 } |
| 189 } | 194 } |
| 190 | 195 |
| 191 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 196 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
| 192 if (!status.ok()) { | 197 if (!status.ok()) { |
| 193 LOG(WARNING) << "DB batch write failed: " << status.ToString(); | 198 LOG(WARNING) << "DB batch write failed: " << status.ToString(); |
| 194 return Result(kGenericOnFailureMessage); | 199 return Result(kGenericOnFailureMessage); |
| 195 } | 200 } |
| 196 | 201 |
| 197 return Result(settings.DeepCopy(), changed_keys.release()); | 202 return Result( |
| 203 settings.DeepCopy(), old_settings.release(), changed_keys.release()); |
| 198 } | 204 } |
| 199 | 205 |
| 200 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove( | 206 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove( |
| 201 const std::string& key) { | 207 const std::string& key) { |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 203 std::vector<std::string> keys; | 209 std::vector<std::string> keys; |
| 204 keys.push_back(key); | 210 keys.push_back(key); |
| 205 return Remove(keys); | 211 return Remove(keys); |
| 206 } | 212 } |
| 207 | 213 |
| 208 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove( | 214 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove( |
| 209 const std::vector<std::string>& keys) { | 215 const std::vector<std::string>& keys) { |
| 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 217 scoped_ptr<DictionaryValue> old_settings(new DictionaryValue()); |
| 211 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); | 218 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); |
| 212 | 219 |
| 213 leveldb::WriteBatch batch; | 220 leveldb::WriteBatch batch; |
| 214 for (std::vector<std::string>::const_iterator it = keys.begin(); | 221 for (std::vector<std::string>::const_iterator it = keys.begin(); |
| 215 it != keys.end(); ++it) { | 222 it != keys.end(); ++it) { |
| 216 scoped_ptr<Value> original_value; | 223 scoped_ptr<Value> old_value; |
| 217 if (!ReadFromDb(leveldb::ReadOptions(), *it, &original_value)) { | 224 if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) { |
| 218 return Result(kGenericOnFailureMessage); | 225 return Result(kGenericOnFailureMessage); |
| 219 } | 226 } |
| 220 | 227 |
| 221 if (original_value.get()) { | 228 if (old_value.get()) { |
| 222 changed_keys->insert(*it); | 229 changed_keys->insert(*it); |
| 230 old_settings->SetWithoutPathExpansion(*it, old_value.release()); |
| 223 batch.Delete(*it); | 231 batch.Delete(*it); |
| 224 } | 232 } |
| 225 } | 233 } |
| 226 | 234 |
| 227 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 235 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
| 228 if (!status.ok() && !status.IsNotFound()) { | 236 if (!status.ok() && !status.IsNotFound()) { |
| 229 LOG(WARNING) << "DB batch delete failed: " << status.ToString(); | 237 LOG(WARNING) << "DB batch delete failed: " << status.ToString(); |
| 230 return Result(kGenericOnFailureMessage); | 238 return Result(kGenericOnFailureMessage); |
| 231 } | 239 } |
| 232 | 240 |
| 233 return Result(NULL, changed_keys.release()); | 241 return Result(NULL, old_settings.release(), changed_keys.release()); |
| 234 } | 242 } |
| 235 | 243 |
| 236 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Clear() { | 244 ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Clear() { |
| 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 246 scoped_ptr<DictionaryValue> old_settings(new DictionaryValue()); |
| 238 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); | 247 scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>()); |
| 239 leveldb::ReadOptions options; | 248 leveldb::ReadOptions options; |
| 240 // 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 |
| 241 // isn't strictly necessary. This is just defensive. | 250 // isn't strictly necessary. This is just defensive. |
| 242 leveldb::WriteBatch batch; | 251 leveldb::WriteBatch batch; |
| 243 | 252 |
| 244 ScopedSnapshot snapshot(db_.get()); | 253 ScopedSnapshot snapshot(db_.get()); |
| 245 options.snapshot = snapshot.get(); | 254 options.snapshot = snapshot.get(); |
| 246 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 255 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); |
| 247 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 256 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 248 changed_keys->insert(it->key().ToString()); | 257 const std::string key = it->key().ToString(); |
| 249 batch.Delete(it->key()); | 258 const std::string old_value_as_json = it->value().ToString(); |
| 259 changed_keys->insert(key); |
| 260 Value* old_value = |
| 261 base::JSONReader().JsonToValue(old_value_as_json, false, false); |
| 262 if (old_value) { |
| 263 old_settings->SetWithoutPathExpansion(key, old_value); |
| 264 } else { |
| 265 LOG(ERROR) << "Invalid JSON in database: " << old_value_as_json; |
| 266 } |
| 267 batch.Delete(key); |
| 250 } | 268 } |
| 251 | 269 |
| 252 if (!it->status().ok()) { | 270 if (!it->status().ok()) { |
| 253 LOG(WARNING) << "Clear iteration failed: " << it->status().ToString(); | 271 LOG(WARNING) << "Clear iteration failed: " << it->status().ToString(); |
| 254 return Result(kGenericOnFailureMessage); | 272 return Result(kGenericOnFailureMessage); |
| 255 } | 273 } |
| 256 | 274 |
| 257 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 275 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
| 258 if (!status.ok() && !status.IsNotFound()) { | 276 if (!status.ok() && !status.IsNotFound()) { |
| 259 LOG(WARNING) << "Clear failed: " << status.ToString(); | 277 LOG(WARNING) << "Clear failed: " << status.ToString(); |
| 260 return Result(kGenericOnFailureMessage); | 278 return Result(kGenericOnFailureMessage); |
| 261 } | 279 } |
| 262 | 280 |
| 263 return Result(NULL, changed_keys.release()); | 281 return Result(NULL, old_settings.release(), changed_keys.release()); |
| 264 } | 282 } |
| 265 | 283 |
| 266 bool ExtensionSettingsLeveldbStorage::ReadFromDb( | 284 bool ExtensionSettingsLeveldbStorage::ReadFromDb( |
| 267 leveldb::ReadOptions options, | 285 leveldb::ReadOptions options, |
| 268 const std::string& key, | 286 const std::string& key, |
| 269 scoped_ptr<Value>* setting) { | 287 scoped_ptr<Value>* setting) { |
| 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 271 DCHECK(setting != NULL); | 289 DCHECK(setting != NULL); |
| 272 std::string value_as_json; | 290 std::string value_as_json; |
| 273 leveldb::Status s = db_->Get(options, key, &value_as_json); | 291 leveldb::Status s = db_->Get(options, key, &value_as_json); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 298 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); | 316 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
| 299 | 317 |
| 300 it->SeekToFirst(); | 318 it->SeekToFirst(); |
| 301 bool is_empty = !it->Valid(); | 319 bool is_empty = !it->Valid(); |
| 302 if (!it->status().ok()) { | 320 if (!it->status().ok()) { |
| 303 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); | 321 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); |
| 304 return false; | 322 return false; |
| 305 } | 323 } |
| 306 return is_empty; | 324 return is_empty; |
| 307 } | 325 } |
| OLD | NEW |