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