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

Side by Side Diff: chrome/browser/extensions/extension_settings_storage_cache.cc

Issue 7189029: Implement an initial extension settings API. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Change to RefCountedThreadSafe Created 9 years, 4 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
(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 "chrome/browser/extensions/extension_settings_storage_cache.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/task.h"
12 #include "base/json/json_writer.h"
13
14 namespace {
15
16 void RemoveAll(DictionaryValue* from, const ListValue* keys_to_remove) {
17 std::string key;
18 for (ListValue::const_iterator it = keys_to_remove->begin();
19 it != keys_to_remove->end(); ++it) {
20 if ((*it)->GetAsString(&key)) {
21 from->Remove(key, NULL);
22 }
23 }
24 }
25
26 // Callback which delegates to a given callback but caches any result if
27 // successful. Takes an optional parameter of existing settings to merge with
28 // before returning to the callback (may be NULL) and an optional parameter of
29 // keys to remove from the cache post-success (may be NULL).
30 class CacheModifyingCallback : public ExtensionSettingsStorage::Callback {
31 public:
32 // Takes ownership of callback, existing, and keys_to_remove.
33 CacheModifyingCallback(
34 ExtensionSettingsStorage::Callback* delegate,
35 base::WeakPtr<DictionaryValue> cache,
36 DictionaryValue* existing,
37 ListValue* keys_to_remove)
38 : delegate_(delegate),
39 cache_(cache),
40 existing_(existing),
41 keys_to_remove_(keys_to_remove) {
42 }
43
44 ~CacheModifyingCallback() {}
45
46 // TODO(kalman): shouldn't expose the factory here. just the weakptr.
Matt Perry 2011/08/03 22:57:48 TODO can go away
not at google - send to devlin 2011/08/04 00:59:07 Done.
47 static CacheModifyingCallback* Create(
48 ExtensionSettingsStorage::Callback* delegate,
49 base::WeakPtr<DictionaryValue> cache_ptr_) {
50 return new CacheModifyingCallback(
51 delegate,
52 cache_ptr_,
53 NULL,
54 NULL);
55 }
56
57 static CacheModifyingCallback* Create(
58 ExtensionSettingsStorage::Callback* delegate,
59 base::WeakPtr<DictionaryValue> cache_ptr_,
60 DictionaryValue* existing) {
61 return new CacheModifyingCallback(
62 delegate,
63 cache_ptr_,
64 existing,
65 NULL);
66 }
67
68 static CacheModifyingCallback* Create(
69 ExtensionSettingsStorage::Callback* delegate,
70 base::WeakPtr<DictionaryValue> cache_ptr_,
71 ListValue* remove) {
72 return new CacheModifyingCallback(
73 delegate,
74 cache_ptr_,
75 NULL,
76 remove);
77 }
78
79 virtual void OnSuccess(DictionaryValue* settings) OVERRIDE {
80 // Note checking that the weak reference to the cache is still valid.
81 // It might be NULL if the owning ExtensionSettingsStorageCache has been
82 // deleted.
83 if (cache_.get() != NULL) {
84 cache_->MergeDictionary(settings);
85 }
86 if (existing_ != NULL) {
87 settings->MergeDictionary(existing_.get());
88 }
89 if (cache_.get() != NULL && keys_to_remove_ != NULL) {
90 RemoveAll(cache_, keys_to_remove_.get());
91 }
92 delegate_->OnSuccess(settings);
93 }
94
95 virtual void OnFailure(const std::string& message) OVERRIDE {
96 delegate_->OnFailure(message);
97 }
98
99 private:
100 scoped_ptr<ExtensionSettingsStorage::Callback> delegate_;
101 base::WeakPtr<DictionaryValue> cache_;
102 scoped_ptr<DictionaryValue> existing_;
103 scoped_ptr<ListValue> keys_to_remove_;
104 };
105
106 // Runs OnSuccess() on the message loop of the calling thread.
107 // TODO(kalman): don't make this a whole class. unnecessary.
not at google - send to devlin 2011/08/04 00:59:07 (also deleted this)
108 class SuccessClosure {
109 public:
110 SuccessClosure(
111 ExtensionSettingsStorage::Callback* callback, DictionaryValue* settings)
112 : callback_(callback), settings_(settings) {
113 }
114
115 ~SuccessClosure() {}
116
117 void Run() {
118 MessageLoop::current()->PostTask(
119 FROM_HERE,
120 base::Bind(&SuccessClosure::Run2, base::Unretained(this)));
121 }
122
123 private:
124 void Run2() {
125 callback_->OnSuccess(settings_);
126 delete this;
127 }
128
129 scoped_ptr<ExtensionSettingsStorage::Callback> callback_;
130 DictionaryValue* settings_;
131 };
132
133 } // namespace
134
135 ExtensionSettingsStorageCache::ExtensionSettingsStorageCache(
136 ExtensionSettingsStorage* delegate)
137 : delegate_(delegate), cache_ptr_factory_(&cache_) {
138 }
139
140 void ExtensionSettingsStorageCache::DeleteSoon() {
141 delegate_->DeleteSoon();
142 delete this;
143 }
144
145 void ExtensionSettingsStorageCache::Get(const std::string& key,
146 ExtensionSettingsStorageCache::Callback* callback) {
147 Value *value;
148 if (GetFromCache(key, &value)) {
149 DictionaryValue* settings = new DictionaryValue();
150 settings->Set(key, value);
151 (new SuccessClosure(callback, settings))->Run();
152 } else {
153 delegate_->Get(
154 key,
155 CacheModifyingCallback::Create(
156 callback,
157 cache_ptr_factory_.GetWeakPtr()));
158 }
159 }
160
161 void ExtensionSettingsStorageCache::Get(const ListValue& keys,
162 ExtensionSettingsStorageCache::Callback* callback) {
163 std::string key;
164 DictionaryValue* settings = new DictionaryValue();
165 ListValue missing_keys;
166
167 for (ListValue::const_iterator it = keys.begin(); it != keys.end(); ++it) {
168 if ((*it)->GetAsString(&key)) {
169 Value *value;
170 if (GetFromCache(key, &value)) {
171 settings->Set(key, value);
172 } else {
173 missing_keys.Append(Value::CreateStringValue(key));
174 }
175 }
176 }
177
178 if (missing_keys.empty()) {
179 (new SuccessClosure(callback, settings))->Run();
180 } else {
181 delegate_->Get(
182 missing_keys,
183 CacheModifyingCallback::Create(
184 callback,
185 cache_ptr_factory_.GetWeakPtr(),
186 settings));
187 }
188 }
189
190 void ExtensionSettingsStorageCache::Get(
191 ExtensionSettingsStorageCache::Callback* callback) {
192 // Copy the cache when passing in, as a semi-hack so that caching a no-op
193 // storage object works (which always returns empty from Get()).
194 // TODO(kalman): Consider doing this conditionally on type == NOOP.
195 delegate_->Get(
196 CacheModifyingCallback::Create(
197 callback,
198 cache_ptr_factory_.GetWeakPtr(),
199 cache_.DeepCopy()));
200 }
201
202 void ExtensionSettingsStorageCache::Set(
203 const std::string& key,
204 const Value& value,
205 ExtensionSettingsStorageCache::Callback* callback) {
206 // Invalidate the cached entry first, in case the set fails.
207 cache_.Remove(key, NULL);
208 delegate_->Set(
209 key,
210 value,
211 CacheModifyingCallback::Create(
212 callback,
213 cache_ptr_factory_.GetWeakPtr()));
214 }
215
216 void ExtensionSettingsStorageCache::Set(
217 const DictionaryValue& values,
218 ExtensionSettingsStorageCache::Callback* callback) {
219 // Invalidate the cached entries first, in case the set fails.
220 for (DictionaryValue::key_iterator it = values.begin_keys();
221 it != values.end_keys(); ++it) {
222 cache_.RemoveWithoutPathExpansion(*it, NULL);
223 }
224 delegate_->Set(
225 values,
226 CacheModifyingCallback::Create(
227 callback,
228 cache_ptr_factory_.GetWeakPtr()));
229 }
230
231 void ExtensionSettingsStorageCache::Remove(
232 const std::string& key,
233 ExtensionSettingsStorageCache::Callback *callback) {
234 // Invalidate the cached entry first, in case the remove fails.
235 // We will also need to do if after the callback, to avoid race conditions
236 // whether other API calls fill the cache on the UI thread.
237 // This would be a good time to use structured cloning...
238 cache_.Remove(key, NULL);
239 ListValue* key_list = new ListValue();
240 key_list->Append(Value::CreateStringValue(key));
241 delegate_->Remove(
242 key,
243 CacheModifyingCallback::Create(
244 callback,
245 cache_ptr_factory_.GetWeakPtr(),
246 key_list));
247 }
248
249 void ExtensionSettingsStorageCache::Remove(
250 const ListValue& keys,
251 ExtensionSettingsStorageCache::Callback *callback) {
252 std::string key;
253 // Invalidate each cached entry first, in case the remove fails.
254 // We will also need to do if after the callback, to avoid race conditions
255 // whether other API calls fill the cache on the UI thread.
256 RemoveAll(&cache_, &keys);
257 delegate_->Remove(
258 keys,
259 CacheModifyingCallback::Create(
260 callback,
261 cache_ptr_factory_.GetWeakPtr(),
262 keys.DeepCopy()));
263 }
264
265 void ExtensionSettingsStorageCache::Clear(
266 ExtensionSettingsStorageCache::Callback* callback) {
267 cache_.Clear();
268 delegate_->Clear(
269 CacheModifyingCallback::Create(
270 callback,
271 cache_ptr_factory_.GetWeakPtr()));
272 }
273
274 bool ExtensionSettingsStorageCache::GetFromCache(
275 const std::string& key, Value** value) {
276 Value* cached_value;
277 if (!cache_.Get(key, &cached_value)) {
278 return false;
279 }
280 *value = cached_value->DeepCopy();
281 return true;
282 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698