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/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 | 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( |
192 it.key(), old_value.release(), it.value().DeepCopy())); | 191 it.key(), old_value.release(), it.value().DeepCopy())); |
193 base::JSONWriter::Write(&it.value(), false, &value_as_json); | 192 base::JSONWriter::Write(&it.value(), false, &value_as_json); |
194 batch.Put(it.key(), value_as_json); | 193 batch.Put(it.key(), value_as_json); |
195 } | 194 } |
196 } | 195 } |
197 | 196 |
198 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 197 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
199 if (!status.ok()) { | 198 if (!status.ok()) { |
200 LOG(WARNING) << "DB batch write failed: " << status.ToString(); | 199 LOG(WARNING) << "DB batch write failed: " << status.ToString(); |
201 return WriteResult(kGenericOnFailureMessage); | 200 return WriteResult(kGenericOnFailureMessage); |
202 } | 201 } |
203 | 202 |
204 return WriteResult(changes.release()); | 203 return WriteResult(changes.release()); |
205 } | 204 } |
206 | 205 |
207 SettingsStorage::WriteResult SettingsLeveldbStorage::Remove( | 206 SettingsStorage::WriteResult SettingsLeveldbStorage::Remove( |
208 const std::string& key) { | 207 WriteOptions options, const std::string& key) { |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
210 std::vector<std::string> keys; | 209 std::vector<std::string> keys; |
211 keys.push_back(key); | 210 keys.push_back(key); |
212 return Remove(keys); | 211 return Remove(options, keys); |
213 } | 212 } |
214 | 213 |
215 SettingsStorage::WriteResult SettingsLeveldbStorage::Remove( | 214 SettingsStorage::WriteResult SettingsLeveldbStorage::Remove( |
216 const std::vector<std::string>& keys) { | 215 WriteOptions options, const std::vector<std::string>& keys) { |
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
218 | 217 |
219 leveldb::WriteBatch batch; | 218 leveldb::WriteBatch batch; |
220 scoped_ptr<SettingChangeList> changes( | 219 scoped_ptr<SettingChangeList> changes( |
221 new SettingChangeList()); | 220 new SettingChangeList()); |
222 | 221 |
223 for (std::vector<std::string>::const_iterator it = keys.begin(); | 222 for (std::vector<std::string>::const_iterator it = keys.begin(); |
224 it != keys.end(); ++it) { | 223 it != keys.end(); ++it) { |
225 scoped_ptr<Value> old_value; | 224 scoped_ptr<Value> old_value; |
226 if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) { | 225 if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) { |
227 return WriteResult(kGenericOnFailureMessage); | 226 return WriteResult(kGenericOnFailureMessage); |
228 } | 227 } |
229 | 228 |
230 if (old_value.get()) { | 229 if (old_value.get()) { |
231 changes->push_back( | 230 changes->push_back( |
232 SettingChange(*it, old_value.release(), NULL)); | 231 SettingChange(*it, old_value.release(), NULL)); |
233 batch.Delete(*it); | 232 batch.Delete(*it); |
234 } | 233 } |
235 } | 234 } |
236 | 235 |
237 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 236 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
238 if (!status.ok() && !status.IsNotFound()) { | 237 if (!status.ok() && !status.IsNotFound()) { |
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( |
| 246 WriteOptions options) { |
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
248 | 248 |
249 leveldb::ReadOptions options; | 249 leveldb::ReadOptions read_options; |
250 // All interaction with the db is done on the same thread, so snapshotting | 250 // All interaction with the db is done on the same thread, so snapshotting |
251 // isn't strictly necessary. This is just defensive. | 251 // isn't strictly necessary. This is just defensive. |
252 leveldb::WriteBatch batch; | 252 leveldb::WriteBatch batch; |
253 scoped_ptr<SettingChangeList> changes( | 253 scoped_ptr<SettingChangeList> changes( |
254 new SettingChangeList()); | 254 new SettingChangeList()); |
255 | 255 |
256 ScopedSnapshot snapshot(db_.get()); | 256 ScopedSnapshot snapshot(db_.get()); |
257 options.snapshot = snapshot.get(); | 257 read_options.snapshot = snapshot.get(); |
258 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 258 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(read_options)); |
259 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 259 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
260 const std::string key = it->key().ToString(); | 260 const std::string key = it->key().ToString(); |
261 const std::string old_value_json = it->value().ToString(); | 261 const std::string old_value_json = it->value().ToString(); |
262 Value* old_value = | 262 Value* old_value = |
263 base::JSONReader().JsonToValue(old_value_json, false, false); | 263 base::JSONReader().JsonToValue(old_value_json, false, false); |
264 if (old_value) { | 264 if (old_value) { |
265 changes->push_back(SettingChange(key, old_value, NULL)); | 265 changes->push_back(SettingChange(key, old_value, NULL)); |
266 } else { | 266 } else { |
267 LOG(ERROR) << "Invalid JSON in database: " << old_value_json; | 267 LOG(ERROR) << "Invalid JSON in database: " << old_value_json; |
268 } | 268 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 it->SeekToFirst(); | 320 it->SeekToFirst(); |
321 bool is_empty = !it->Valid(); | 321 bool is_empty = !it->Valid(); |
322 if (!it->status().ok()) { | 322 if (!it->status().ok()) { |
323 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); | 323 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); |
324 return false; | 324 return false; |
325 } | 325 } |
326 return is_empty; | 326 return is_empty; |
327 } | 327 } |
328 | 328 |
329 } // namespace extensions | 329 } // namespace extensions |
OLD | NEW |