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
|
// Copyright 2014 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 "extensions/browser/api/storage/storage_api.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/storage/storage_frontend.h"
#include "extensions/browser/quota_service.h"
#include "extensions/common/api/storage.h"
namespace extensions {
using content::BrowserThread;
// SettingsFunction
SettingsFunction::SettingsFunction()
: settings_namespace_(settings_namespace::INVALID),
tried_restoring_storage_(false) {}
SettingsFunction::~SettingsFunction() {}
bool SettingsFunction::ShouldSkipQuotaLimiting() const {
// Only apply quota if this is for sync storage.
std::string settings_namespace_string;
if (!args_->GetString(0, &settings_namespace_string)) {
// This should be EXTENSION_FUNCTION_VALIDATE(false) but there is no way
// to signify that from this function. It will be caught in
// RunImplTypesafe().
return false;
}
return settings_namespace_string != "sync";
}
ExtensionFunction::ResponseAction SettingsFunction::RunImplTypesafe() {
std::string settings_namespace_string;
EXTENSION_FUNCTION_VALIDATE_TYPESAFE(
args_->GetString(0, &settings_namespace_string));
args_->Remove(0, NULL);
settings_namespace_ =
settings_namespace::FromString(settings_namespace_string);
EXTENSION_FUNCTION_VALIDATE_TYPESAFE(settings_namespace_ !=
settings_namespace::INVALID);
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
if (!frontend->IsStorageEnabled(settings_namespace_)) {
return RespondNow(Error(
base::StringPrintf("\"%s\" is not available in this instance of Chrome",
settings_namespace_string.c_str())));
}
observers_ = frontend->GetObservers();
frontend->RunWithStorage(
GetExtension(),
settings_namespace_,
base::Bind(&SettingsFunction::AsyncRunWithStorage, this));
return RespondLater();
}
void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) {
ResponseValue response = RunWithStorage(storage);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&SettingsFunction::SendResponseTypesafe,
this,
base::Passed(&response)));
}
ExtensionFunction::ResponseValue SettingsFunction::UseReadResult(
ValueStore::ReadResult result,
ValueStore* storage) {
if (result->HasError())
return HandleError(result->error(), storage);
base::DictionaryValue* dict = new base::DictionaryValue();
dict->Swap(&result->settings());
return SingleArgument(dict);
}
ExtensionFunction::ResponseValue SettingsFunction::UseWriteResult(
ValueStore::WriteResult result,
ValueStore* storage) {
if (result->HasError())
return HandleError(result->error(), storage);
if (!result->changes().empty()) {
observers_->Notify(
&SettingsObserver::OnSettingsChanged,
extension_id(),
settings_namespace_,
ValueStoreChange::ToJson(result->changes()));
}
return NoArguments();
}
ExtensionFunction::ResponseValue SettingsFunction::HandleError(
const ValueStore::Error& error,
ValueStore* storage) {
// If the method failed due to corruption, and we haven't tried to fix it, we
// can try to restore the storage and re-run it. Otherwise, the method has
// failed.
if (error.code == ValueStore::CORRUPTION && !tried_restoring_storage_) {
tried_restoring_storage_ = true;
// If the corruption is on a particular key, try to restore that key and
// re-run.
if (error.key.get() && storage->RestoreKey(*error.key))
return RunWithStorage(storage);
// If the full database is corrupted, try to restore the whole thing and
// re-run.
if (storage->Restore())
return RunWithStorage(storage);
}
return Error(error.message);
}
// Concrete settings functions
namespace {
// Adds all StringValues from a ListValue to a vector of strings.
void AddAllStringValues(const base::ListValue& from,
std::vector<std::string>* to) {
DCHECK(to->empty());
std::string as_string;
for (base::ListValue::const_iterator it = from.begin();
it != from.end(); ++it) {
if ((*it)->GetAsString(&as_string)) {
to->push_back(as_string);
}
}
}
// Gets the keys of a DictionaryValue.
std::vector<std::string> GetKeys(const base::DictionaryValue& dict) {
std::vector<std::string> keys;
for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
keys.push_back(it.key());
}
return keys;
}
// Creates quota heuristics for settings modification.
void GetModificationQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) {
QuotaLimitHeuristic::Config longLimitConfig = {
// See storage.json for current value.
core_api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR,
base::TimeDelta::FromHours(1)
};
heuristics->push_back(new QuotaService::TimedLimit(
longLimitConfig,
new QuotaLimitHeuristic::SingletonBucketMapper(),
"MAX_WRITE_OPERATIONS_PER_HOUR"));
// A max of 10 operations per minute, sustained over 10 minutes.
QuotaLimitHeuristic::Config shortLimitConfig = {
// See storage.json for current value.
core_api::storage::sync::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE,
base::TimeDelta::FromMinutes(1)
};
heuristics->push_back(new QuotaService::SustainedLimit(
base::TimeDelta::FromMinutes(10),
shortLimitConfig,
new QuotaLimitHeuristic::SingletonBucketMapper(),
"MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE"));
};
} // namespace
ExtensionFunction::ResponseValue StorageStorageAreaGetFunction::RunWithStorage(
ValueStore* storage) {
base::Value* input = NULL;
if (!args_->Get(0, &input))
return BadMessage();
switch (input->GetType()) {
case base::Value::TYPE_NULL:
return UseReadResult(storage->Get(), storage);
case base::Value::TYPE_STRING: {
std::string as_string;
input->GetAsString(&as_string);
return UseReadResult(storage->Get(as_string), storage);
}
case base::Value::TYPE_LIST: {
std::vector<std::string> as_string_list;
AddAllStringValues(*static_cast<base::ListValue*>(input),
&as_string_list);
return UseReadResult(storage->Get(as_string_list), storage);
}
case base::Value::TYPE_DICTIONARY: {
base::DictionaryValue* as_dict =
static_cast<base::DictionaryValue*>(input);
ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict));
if (result->HasError()) {
return UseReadResult(result.Pass(), storage);
}
base::DictionaryValue* with_default_values = as_dict->DeepCopy();
with_default_values->MergeDictionary(&result->settings());
return UseReadResult(
ValueStore::MakeReadResult(make_scoped_ptr(with_default_values)),
storage);
}
default:
return BadMessage();
}
}
ExtensionFunction::ResponseValue
StorageStorageAreaGetBytesInUseFunction::RunWithStorage(ValueStore* storage) {
base::Value* input = NULL;
if (!args_->Get(0, &input))
return BadMessage();
size_t bytes_in_use = 0;
switch (input->GetType()) {
case base::Value::TYPE_NULL:
bytes_in_use = storage->GetBytesInUse();
break;
case base::Value::TYPE_STRING: {
std::string as_string;
input->GetAsString(&as_string);
bytes_in_use = storage->GetBytesInUse(as_string);
break;
}
case base::Value::TYPE_LIST: {
std::vector<std::string> as_string_list;
AddAllStringValues(*static_cast<base::ListValue*>(input),
&as_string_list);
bytes_in_use = storage->GetBytesInUse(as_string_list);
break;
}
default:
return BadMessage();
}
return SingleArgument(
new base::FundamentalValue(static_cast<int>(bytes_in_use)));
}
ExtensionFunction::ResponseValue StorageStorageAreaSetFunction::RunWithStorage(
ValueStore* storage) {
base::DictionaryValue* input = NULL;
if (!args_->GetDictionary(0, &input))
return BadMessage();
return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input), storage);
}
void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
ExtensionFunction::ResponseValue
StorageStorageAreaRemoveFunction::RunWithStorage(ValueStore* storage) {
base::Value* input = NULL;
if (!args_->Get(0, &input))
return BadMessage();
switch (input->GetType()) {
case base::Value::TYPE_STRING: {
std::string as_string;
input->GetAsString(&as_string);
return UseWriteResult(storage->Remove(as_string), storage);
}
case base::Value::TYPE_LIST: {
std::vector<std::string> as_string_list;
AddAllStringValues(*static_cast<base::ListValue*>(input),
&as_string_list);
return UseWriteResult(storage->Remove(as_string_list), storage);
}
default:
return BadMessage();
};
}
void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
ExtensionFunction::ResponseValue
StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) {
return UseWriteResult(storage->Clear(), storage);
}
void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
} // namespace extensions
|