Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/extensions/api/storage/storage_api.cc

Issue 24021002: Propagate more information about ValueStore errors to callers, notably an (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add Pass*() Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/api/storage/storage_api.h" 5 #include "chrome/browser/extensions/api/storage/storage_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/extensions/api/storage/settings_frontend.h" 13 #include "chrome/browser/extensions/api/storage/settings_frontend.h"
14 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extensions_quota_service.h" 15 #include "chrome/browser/extensions/extensions_quota_service.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/extensions/api/storage.h" 17 #include "chrome/common/extensions/api/storage.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 19
20 namespace extensions { 20 namespace extensions {
21 21
22 using content::BrowserThread; 22 using content::BrowserThread;
23 23
24 namespace {
25 const char kUnsupportedArgumentType[] = "Unsupported argument type";
26 const char kInvalidNamespaceErrorMessage[] =
27 "\"%s\" is not available in this instance of Chrome";
28 const char kManagedNamespaceDisabledErrorMessage[] =
29 "\"managed\" is disabled. Use \"--%s\" to enable it.";
30 const char kStorageErrorMessage[] = "Storage error";
31 } // namespace
32
33 // SettingsFunction 24 // SettingsFunction
34 25
35 SettingsFunction::SettingsFunction() 26 SettingsFunction::SettingsFunction()
36 : settings_namespace_(settings_namespace::INVALID) {} 27 : settings_namespace_(settings_namespace::INVALID) {}
37 28
38 SettingsFunction::~SettingsFunction() {} 29 SettingsFunction::~SettingsFunction() {}
39 30
40 bool SettingsFunction::ShouldSkipQuotaLimiting() const { 31 bool SettingsFunction::ShouldSkipQuotaLimiting() const {
41 // Only apply quota if this is for sync storage. 32 // Only apply quota if this is for sync storage.
42 std::string settings_namespace_string; 33 std::string settings_namespace_string;
43 if (!args_->GetString(0, &settings_namespace_string)) { 34 if (!args_->GetString(0, &settings_namespace_string)) {
44 // This is an error but it will be caught in RunImpl(), there is no 35 // This should be EXTENSION_FUNCTION_VALIDATE(false) but there is no way
45 // mechanism to signify an error from this function. 36 // to signify that from this function. It will be caught in RunImpl().
46 return false; 37 return false;
47 } 38 }
48 return settings_namespace_string != "sync"; 39 return settings_namespace_string != "sync";
49 } 40 }
50 41
51 bool SettingsFunction::RunImpl() { 42 bool SettingsFunction::RunImpl() {
52 std::string settings_namespace_string; 43 std::string settings_namespace_string;
53 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &settings_namespace_string)); 44 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &settings_namespace_string));
54 args_->Remove(0, NULL); 45 args_->Remove(0, NULL);
55 settings_namespace_ = 46 settings_namespace_ =
56 settings_namespace::FromString(settings_namespace_string); 47 settings_namespace::FromString(settings_namespace_string);
57 EXTENSION_FUNCTION_VALIDATE( 48 EXTENSION_FUNCTION_VALIDATE(
58 settings_namespace_ != settings_namespace::INVALID); 49 settings_namespace_ != settings_namespace::INVALID);
59 50
60 SettingsFrontend* frontend = 51 SettingsFrontend* frontend =
61 profile()->GetExtensionService()->settings_frontend(); 52 profile()->GetExtensionService()->settings_frontend();
62 if (!frontend->IsStorageEnabled(settings_namespace_)) { 53 if (!frontend->IsStorageEnabled(settings_namespace_)) {
63 error_ = base::StringPrintf(kInvalidNamespaceErrorMessage, 54 error_ = base::StringPrintf(
64 settings_namespace_string.c_str()); 55 "\"%s\" is not available in this instance of Chrome",
56 settings_namespace_string.c_str());
65 return false; 57 return false;
66 } 58 }
67 59
68 observers_ = frontend->GetObservers(); 60 observers_ = frontend->GetObservers();
69 frontend->RunWithStorage( 61 frontend->RunWithStorage(
70 extension_id(), 62 extension_id(),
71 settings_namespace_, 63 settings_namespace_,
72 base::Bind(&SettingsFunction::AsyncRunWithStorage, this)); 64 base::Bind(&SettingsFunction::AsyncRunWithStorage, this));
73 return true; 65 return true;
74 } 66 }
75 67
76 void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) { 68 void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) {
77 bool success = RunWithStorage(storage); 69 bool success = RunWithStorage(storage);
78 BrowserThread::PostTask( 70 BrowserThread::PostTask(
79 BrowserThread::UI, 71 BrowserThread::UI,
80 FROM_HERE, 72 FROM_HERE,
81 base::Bind(&SettingsFunction::SendResponse, this, success)); 73 base::Bind(&SettingsFunction::SendResponse, this, success));
82 } 74 }
83 75
84 bool SettingsFunction::UseReadResult(ValueStore::ReadResult result) { 76 bool SettingsFunction::UseReadResult(ValueStore::ReadResult read_result) {
85 if (result->HasError()) { 77 if (read_result->HasError()) {
86 error_ = result->error(); 78 error_ = read_result->error().message;
87 return false; 79 return false;
88 } 80 }
89 81
90 SetResult(result->settings().release()); 82 base::DictionaryValue* result = new base::DictionaryValue();
83 result->Swap(&read_result->settings());
84 SetResult(result);
91 return true; 85 return true;
92 } 86 }
93 87
94 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result) { 88 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result) {
95 if (result->HasError()) { 89 if (result->HasError()) {
96 error_ = result->error(); 90 error_ = result->error().message;
97 return false; 91 return false;
98 } 92 }
99 93
100 if (!result->changes().empty()) { 94 if (!result->changes().empty()) {
101 observers_->Notify( 95 observers_->Notify(
102 &SettingsObserver::OnSettingsChanged, 96 &SettingsObserver::OnSettingsChanged,
103 extension_id(), 97 extension_id(),
104 settings_namespace_, 98 settings_namespace_,
105 ValueStoreChange::ToJson(result->changes())); 99 ValueStoreChange::ToJson(result->changes()));
106 } 100 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 } 179 }
186 180
187 case base::Value::TYPE_DICTIONARY: { 181 case base::Value::TYPE_DICTIONARY: {
188 base::DictionaryValue* as_dict = static_cast<base::DictionaryValue*>(input ); 182 base::DictionaryValue* as_dict = static_cast<base::DictionaryValue*>(input );
189 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict)); 183 ValueStore::ReadResult result = storage->Get(GetKeys(*as_dict));
190 if (result->HasError()) { 184 if (result->HasError()) {
191 return UseReadResult(result.Pass()); 185 return UseReadResult(result.Pass());
192 } 186 }
193 187
194 base::DictionaryValue* with_default_values = as_dict->DeepCopy(); 188 base::DictionaryValue* with_default_values = as_dict->DeepCopy();
195 with_default_values->MergeDictionary(result->settings().get()); 189 with_default_values->MergeDictionary(&result->settings());
196 return UseReadResult( 190 return UseReadResult(
197 ValueStore::MakeReadResult(with_default_values)); 191 ValueStore::MakeReadResult(make_scoped_ptr(with_default_values)));
198 } 192 }
199 193
200 default: 194 default:
201 return UseReadResult( 195 EXTENSION_FUNCTION_VALIDATE(false);
202 ValueStore::MakeReadResult(kUnsupportedArgumentType)); 196 return false;
203 } 197 }
204 } 198 }
205 199
206 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage( 200 bool StorageStorageAreaGetBytesInUseFunction::RunWithStorage(
207 ValueStore* storage) { 201 ValueStore* storage) {
208 base::Value* input = NULL; 202 base::Value* input = NULL;
209 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); 203 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
210 204
211 size_t bytes_in_use = 0; 205 size_t bytes_in_use = 0;
212 206
(...skipping 11 matching lines...) Expand all
224 218
225 case base::Value::TYPE_LIST: { 219 case base::Value::TYPE_LIST: {
226 std::vector<std::string> as_string_list; 220 std::vector<std::string> as_string_list;
227 AddAllStringValues(*static_cast<base::ListValue*>(input), 221 AddAllStringValues(*static_cast<base::ListValue*>(input),
228 &as_string_list); 222 &as_string_list);
229 bytes_in_use = storage->GetBytesInUse(as_string_list); 223 bytes_in_use = storage->GetBytesInUse(as_string_list);
230 break; 224 break;
231 } 225 }
232 226
233 default: 227 default:
234 error_ = kUnsupportedArgumentType; 228 EXTENSION_FUNCTION_VALIDATE(false);
235 return false; 229 return false;
236 } 230 }
237 231
238 SetResult(new base::FundamentalValue(static_cast<int>(bytes_in_use))); 232 SetResult(new base::FundamentalValue(static_cast<int>(bytes_in_use)));
239 return true; 233 return true;
240 } 234 }
241 235
242 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) { 236 bool StorageStorageAreaSetFunction::RunWithStorage(ValueStore* storage) {
243 base::DictionaryValue* input = NULL; 237 base::DictionaryValue* input = NULL;
244 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); 238 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input));
(...skipping 17 matching lines...) Expand all
262 } 256 }
263 257
264 case base::Value::TYPE_LIST: { 258 case base::Value::TYPE_LIST: {
265 std::vector<std::string> as_string_list; 259 std::vector<std::string> as_string_list;
266 AddAllStringValues(*static_cast<base::ListValue*>(input), 260 AddAllStringValues(*static_cast<base::ListValue*>(input),
267 &as_string_list); 261 &as_string_list);
268 return UseWriteResult(storage->Remove(as_string_list)); 262 return UseWriteResult(storage->Remove(as_string_list));
269 } 263 }
270 264
271 default: 265 default:
272 return UseWriteResult( 266 EXTENSION_FUNCTION_VALIDATE(false);
273 ValueStore::MakeWriteResult(kUnsupportedArgumentType)); 267 return false;
274 }; 268 };
275 } 269 }
276 270
277 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics( 271 void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics(
278 QuotaLimitHeuristics* heuristics) const { 272 QuotaLimitHeuristics* heuristics) const {
279 GetModificationQuotaLimitHeuristics(heuristics); 273 GetModificationQuotaLimitHeuristics(heuristics);
280 } 274 }
281 275
282 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) { 276 bool StorageStorageAreaClearFunction::RunWithStorage(ValueStore* storage) {
283 return UseWriteResult(storage->Clear()); 277 return UseWriteResult(storage->Clear());
284 } 278 }
285 279
286 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics( 280 void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics(
287 QuotaLimitHeuristics* heuristics) const { 281 QuotaLimitHeuristics* heuristics) const {
288 GetModificationQuotaLimitHeuristics(heuristics); 282 GetModificationQuotaLimitHeuristics(heuristics);
289 } 283 }
290 284
291 } // namespace extensions 285 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698