OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/values.h" | |
6 #include "chrome/browser/extensions/extension_service.h" | |
7 #include "chrome/browser/extensions/extension_settings_api.h" | |
8 #include "chrome/browser/profiles/profile.h" | |
9 | |
10 namespace { | |
11 | |
12 const char* kUnsupportedArgumentType = "Unsupported argument type"; | |
13 | |
14 } // namespace | |
15 | |
16 // GetStorageCallback | |
17 | |
18 SettingsFunction::GetStorageCallback::GetStorageCallback( | |
19 SettingsFunction* settings_function) | |
20 : settings_function_(settings_function) { | |
21 } | |
22 | |
23 SettingsFunction::GetStorageCallback::~GetStorageCallback() { | |
24 } | |
25 | |
26 void SettingsFunction::GetStorageCallback::Run( | |
27 ExtensionSettingsStorage* storage) { | |
28 // Mimic how RunImpl() is handled in extensions code. | |
29 if (!settings_function_->RunWithStorage(storage)) { | |
30 settings_function_->SendResponse(false); | |
31 } | |
32 } | |
33 | |
34 // StorageResultCallback | |
35 | |
36 SettingsFunction::StorageResultCallback::StorageResultCallback( | |
37 SettingsFunction* settings_function) | |
38 : settings_function_(settings_function) { | |
39 } | |
40 | |
41 SettingsFunction::StorageResultCallback::~StorageResultCallback() { | |
42 } | |
43 | |
44 void SettingsFunction::StorageResultCallback::OnSuccess( | |
45 DictionaryValue* settings) { | |
46 settings_function_->result_.reset(settings); | |
47 settings_function_->SendResponse(true); | |
48 } | |
49 | |
50 void SettingsFunction::StorageResultCallback::OnFailure( | |
51 const std::string& message) { | |
52 settings_function_->error_ = message; | |
53 settings_function_->SendResponse(false); | |
54 } | |
55 | |
56 // SettingsFunction | |
57 | |
58 bool SettingsFunction::RunImpl() { | |
59 profile()->GetExtensionService()->extension_settings()->GetStorage( | |
60 extension_id(), new GetStorageCallback(this)); | |
61 return true; | |
Matt Perry
2011/06/23 18:11:45
You need to AddRef here, and Release when you send
not at google - send to devlin
2011/06/27 08:51:02
Done.
| |
62 } | |
63 | |
64 // Concrete settings functions | |
65 | |
66 bool GetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
67 Value *input; | |
68 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | |
69 | |
70 std::string as_string; | |
71 ListValue* as_list; | |
72 if (input->IsType(Value::TYPE_NULL)) { | |
73 storage->Get(new StorageResultCallback(this)); | |
74 } else if (input->GetAsString(&as_string)) { | |
75 storage->Get(as_string, new StorageResultCallback(this)); | |
76 } else if (input->GetAsList(&as_list)) { | |
77 storage->Get(*as_list, new StorageResultCallback(this)); | |
78 } else { | |
79 error_ = kUnsupportedArgumentType; | |
80 return false; | |
81 } | |
82 | |
83 return true; | |
84 } | |
85 | |
86 bool SetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
87 DictionaryValue *input; | |
88 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | |
89 storage->Set(*input, new StorageResultCallback(this)); | |
90 return true; | |
91 } | |
92 | |
93 bool RemoveSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
94 Value *input; | |
95 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | |
96 | |
97 std::string as_string; | |
98 ListValue* as_list; | |
99 if (input->GetAsString(&as_string)) { | |
100 storage->Remove(as_string, new StorageResultCallback(this)); | |
101 } else if (input->GetAsList(&as_list)) { | |
102 storage->Remove(*as_list, new StorageResultCallback(this)); | |
103 } else { | |
104 error_ = kUnsupportedArgumentType; | |
105 return false; | |
106 } | |
107 | |
108 return true; | |
109 } | |
110 | |
111 bool ClearSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
112 storage->Clear(new StorageResultCallback(this)); | |
113 return true; | |
114 } | |
OLD | NEW |