1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_settings_leveldb_storage.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/sys_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/leveldatabase/src/include/leveldb/iterator.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
namespace {
// Generic error message sent to extensions on failure.
const char* kGenericOnFailureMessage = "Extension settings failed";
// Scoped leveldb snapshot which releases the snapshot on destruction.
class ScopedSnapshot {
public:
explicit ScopedSnapshot(leveldb::DB* db)
: db_(db), snapshot_(db->GetSnapshot()) {}
~ScopedSnapshot() {
db_->ReleaseSnapshot(snapshot_);
}
const leveldb::Snapshot* get() {
return snapshot_;
}
private:
leveldb::DB* db_;
const leveldb::Snapshot* snapshot_;
DISALLOW_COPY_AND_ASSIGN(ScopedSnapshot);
};
} // namespace
/* static */
ExtensionSettingsLeveldbStorage* ExtensionSettingsLeveldbStorage::Create(
const FilePath& base_path,
const std::string& extension_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
FilePath path = base_path.AppendASCII(extension_id);
#if defined(OS_POSIX)
std::string os_path(path.value());
#elif defined(OS_WIN)
std::string os_path = base::SysWideToUTF8(path.value());
#endif
leveldb::Options options;
options.create_if_missing = true;
leveldb::DB* db;
leveldb::Status status = leveldb::DB::Open(options, os_path, &db);
if (!status.ok()) {
LOG(WARNING) << "Failed to create leveldb at " << path.value() <<
": " << status.ToString();
return NULL;
}
return new ExtensionSettingsLeveldbStorage(path, db);
}
ExtensionSettingsLeveldbStorage::ExtensionSettingsLeveldbStorage(
const FilePath& db_path, leveldb::DB* db)
: db_path_(db_path), db_(db) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
ExtensionSettingsLeveldbStorage::~ExtensionSettingsLeveldbStorage() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
// Delete the database from disk if it's empty. This is safe on destruction,
// assuming that we have exclusive access to the database.
if (IsEmpty()) {
// Close |db_| now to release any lock on the directory.
db_.reset();
if (!file_util::Delete(db_path_, true)) {
LOG(WARNING) << "Failed to delete extension settings directory " <<
db_path_.value();
}
}
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get(
const std::string& key) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_ptr<Value> setting;
if (!ReadFromDb(leveldb::ReadOptions(), key, &setting)) {
return Result(kGenericOnFailureMessage);
}
DictionaryValue* settings = new DictionaryValue();
if (setting.get()) {
settings->SetWithoutPathExpansion(key, setting.release());
}
return Result(settings, NULL, NULL);
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get(
const std::vector<std::string>& keys) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
leveldb::ReadOptions options;
scoped_ptr<DictionaryValue> settings(new DictionaryValue());
// All interaction with the db is done on the same thread, so snapshotting
// isn't strictly necessary. This is just defensive.
ScopedSnapshot snapshot(db_.get());
options.snapshot = snapshot.get();
for (std::vector<std::string>::const_iterator it = keys.begin();
it != keys.end(); ++it) {
scoped_ptr<Value> setting;
if (!ReadFromDb(options, *it, &setting)) {
return Result(kGenericOnFailureMessage);
}
if (setting.get()) {
settings->SetWithoutPathExpansion(*it, setting.release());
}
}
return Result(settings.release(), NULL, NULL);
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Get() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::JSONReader json_reader;
leveldb::ReadOptions options = leveldb::ReadOptions();
// All interaction with the db is done on the same thread, so snapshotting
// isn't strictly necessary. This is just defensive.
scoped_ptr<DictionaryValue> settings(new DictionaryValue());
ScopedSnapshot snapshot(db_.get());
options.snapshot = snapshot.get();
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
Value* value =
json_reader.JsonToValue(it->value().ToString(), false, false);
if (value != NULL) {
settings->SetWithoutPathExpansion(it->key().ToString(), value);
} else {
// TODO(kalman): clear the offending non-JSON value from the database.
LOG(ERROR) << "Invalid JSON: " << it->value().ToString();
}
}
if (!it->status().ok()) {
LOG(ERROR) << "DB iteration failed: " << it->status().ToString();
return Result(kGenericOnFailureMessage);
}
return Result(settings.release(), NULL, NULL);
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set(
const std::string& key, const Value& value) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DictionaryValue settings;
settings.SetWithoutPathExpansion(key, value.DeepCopy());
return Set(settings);
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Set(
const DictionaryValue& settings) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_ptr<DictionaryValue> old_settings(new DictionaryValue());
scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>());
std::string value_as_json;
leveldb::WriteBatch batch;
for (DictionaryValue::key_iterator it = settings.begin_keys();
it != settings.end_keys(); ++it) {
scoped_ptr<Value> old_value;
if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) {
return Result(kGenericOnFailureMessage);
}
Value* new_value = NULL;
settings.GetWithoutPathExpansion(*it, &new_value);
if (!old_value.get() || !old_value->Equals(new_value)) {
changed_keys->insert(*it);
base::JSONWriter::Write(new_value, false, &value_as_json);
batch.Put(*it, value_as_json);
}
if (old_value.get()) {
old_settings->SetWithoutPathExpansion(*it, old_value.release());
}
}
leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
if (!status.ok()) {
LOG(WARNING) << "DB batch write failed: " << status.ToString();
return Result(kGenericOnFailureMessage);
}
return Result(
settings.DeepCopy(), old_settings.release(), changed_keys.release());
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove(
const std::string& key) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
std::vector<std::string> keys;
keys.push_back(key);
return Remove(keys);
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Remove(
const std::vector<std::string>& keys) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_ptr<DictionaryValue> old_settings(new DictionaryValue());
scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>());
leveldb::WriteBatch batch;
for (std::vector<std::string>::const_iterator it = keys.begin();
it != keys.end(); ++it) {
scoped_ptr<Value> old_value;
if (!ReadFromDb(leveldb::ReadOptions(), *it, &old_value)) {
return Result(kGenericOnFailureMessage);
}
if (old_value.get()) {
changed_keys->insert(*it);
old_settings->SetWithoutPathExpansion(*it, old_value.release());
batch.Delete(*it);
}
}
leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
if (!status.ok() && !status.IsNotFound()) {
LOG(WARNING) << "DB batch delete failed: " << status.ToString();
return Result(kGenericOnFailureMessage);
}
return Result(NULL, old_settings.release(), changed_keys.release());
}
ExtensionSettingsStorage::Result ExtensionSettingsLeveldbStorage::Clear() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_ptr<DictionaryValue> old_settings(new DictionaryValue());
scoped_ptr<std::set<std::string> > changed_keys(new std::set<std::string>());
leveldb::ReadOptions options;
// All interaction with the db is done on the same thread, so snapshotting
// isn't strictly necessary. This is just defensive.
leveldb::WriteBatch batch;
ScopedSnapshot snapshot(db_.get());
options.snapshot = snapshot.get();
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
const std::string key = it->key().ToString();
const std::string old_value_as_json = it->value().ToString();
changed_keys->insert(key);
Value* old_value =
base::JSONReader().JsonToValue(old_value_as_json, false, false);
if (old_value) {
old_settings->SetWithoutPathExpansion(key, old_value);
} else {
LOG(ERROR) << "Invalid JSON in database: " << old_value_as_json;
}
batch.Delete(key);
}
if (!it->status().ok()) {
LOG(WARNING) << "Clear iteration failed: " << it->status().ToString();
return Result(kGenericOnFailureMessage);
}
leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
if (!status.ok() && !status.IsNotFound()) {
LOG(WARNING) << "Clear failed: " << status.ToString();
return Result(kGenericOnFailureMessage);
}
return Result(NULL, old_settings.release(), changed_keys.release());
}
bool ExtensionSettingsLeveldbStorage::ReadFromDb(
leveldb::ReadOptions options,
const std::string& key,
scoped_ptr<Value>* setting) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DCHECK(setting != NULL);
std::string value_as_json;
leveldb::Status s = db_->Get(options, key, &value_as_json);
if (s.IsNotFound()) {
// Despite there being no value, it was still a success.
return true;
}
if (!s.ok()) {
LOG(ERROR) << "Error reading from database: " << s.ToString();
return false;
}
Value* value = base::JSONReader().JsonToValue(value_as_json, false, false);
if (value == NULL) {
// TODO(kalman): clear the offending non-JSON value from the database.
LOG(ERROR) << "Invalid JSON in database: " << value_as_json;
return false;
}
setting->reset(value);
return true;
}
bool ExtensionSettingsLeveldbStorage::IsEmpty() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions()));
it->SeekToFirst();
bool is_empty = !it->Valid();
if (!it->status().ok()) {
LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString();
return false;
}
return is_empty;
}
|