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 "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. | |
47 static CacheModifyingCallback* Create( | |
48 ExtensionSettingsStorage::Callback* delegate, | |
49 base::WeakPtrFactory<DictionaryValue>& cache_ptr_factory) { | |
50 return new CacheModifyingCallback( | |
51 delegate, | |
52 cache_ptr_factory.GetWeakPtr(), | |
53 NULL, | |
54 NULL); | |
55 } | |
56 | |
57 static CacheModifyingCallback* Create( | |
58 ExtensionSettingsStorage::Callback* delegate, | |
59 base::WeakPtrFactory<DictionaryValue>& cache_ptr_factory, | |
60 DictionaryValue* existing) { | |
61 return new CacheModifyingCallback( | |
62 delegate, | |
63 cache_ptr_factory.GetWeakPtr(), | |
64 existing, | |
65 NULL); | |
66 } | |
67 | |
68 static CacheModifyingCallback* Create( | |
69 ExtensionSettingsStorage::Callback* delegate, | |
70 base::WeakPtrFactory<DictionaryValue>& cache_ptr_factory, | |
71 ListValue* remove) { | |
72 return new CacheModifyingCallback( | |
73 delegate, | |
74 cache_ptr_factory.GetWeakPtr(), | |
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. | |
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(callback, cache_ptr_factory_)); | |
Matt Perry
2011/06/29 18:08:11
you can just pass in cache_ptr_factory_.GetWeakPtr
not at google - send to devlin
2011/08/03 06:36:51
Done.
| |
156 } | |
157 } | |
158 | |
159 void ExtensionSettingsStorageCache::Get(const ListValue& keys, | |
160 ExtensionSettingsStorageCache::Callback* callback) { | |
161 std::string key; | |
162 DictionaryValue* settings = new DictionaryValue(); | |
163 ListValue missing_keys; | |
164 | |
165 for (ListValue::const_iterator it = keys.begin(); it != keys.end(); ++it) { | |
166 if ((*it)->GetAsString(&key)) { | |
167 Value *value; | |
168 if (GetFromCache(key, &value)) { | |
169 settings->Set(key, value); | |
170 } else { | |
171 missing_keys.Append(Value::CreateStringValue(key)); | |
172 } | |
173 } | |
174 } | |
175 | |
176 if (missing_keys.empty()) { | |
177 (new SuccessClosure(callback, settings))->Run(); | |
178 } else { | |
179 delegate_->Get( | |
180 missing_keys, | |
181 CacheModifyingCallback::Create(callback, cache_ptr_factory_, settings)); | |
182 } | |
183 } | |
184 | |
185 void ExtensionSettingsStorageCache::Get( | |
186 ExtensionSettingsStorageCache::Callback* callback) { | |
187 // Copy the cache when passing in, as a semi-hack so that caching a no-op | |
188 // storage object works (which always returns empty from Get()). | |
189 // TODO(kalman): Consider doing this conditionally on type == NOOP. | |
190 delegate_->Get( | |
191 CacheModifyingCallback::Create( | |
192 callback, | |
193 cache_ptr_factory_, | |
194 cache_.DeepCopy())); | |
195 } | |
196 | |
197 void ExtensionSettingsStorageCache::Set( | |
198 const std::string& key, | |
199 const Value& value, | |
200 ExtensionSettingsStorageCache::Callback* callback) { | |
201 // Invalidate the cached entry first, in case the set fails. | |
202 cache_.Remove(key, NULL); | |
203 delegate_->Set( | |
204 key, | |
205 value, | |
206 CacheModifyingCallback::Create(callback, cache_ptr_factory_)); | |
207 } | |
208 | |
209 void ExtensionSettingsStorageCache::Set( | |
210 const DictionaryValue& values, | |
211 ExtensionSettingsStorageCache::Callback* callback) { | |
212 // Invalidate the cached entries first, in case the set fails. | |
213 for (DictionaryValue::key_iterator it = values.begin_keys(); | |
214 it != values.end_keys(); ++it) { | |
215 cache_.RemoveWithoutPathExpansion(*it, NULL); | |
216 } | |
217 delegate_->Set( | |
218 values, | |
219 CacheModifyingCallback::Create(callback, cache_ptr_factory_)); | |
220 } | |
221 | |
222 void ExtensionSettingsStorageCache::Remove( | |
223 const std::string& key, | |
224 ExtensionSettingsStorageCache::Callback *callback) { | |
225 // Invalidate the cached entry first, in case the remove fails. | |
226 // We will also need to do if after the callback, to avoid race conditions | |
227 // whether other API calls fill the cache on the UI thread. | |
228 // This would be a good time to use structured cloning... | |
229 cache_.Remove(key, NULL); | |
230 ListValue* key_list = new ListValue(); | |
231 key_list->Append(Value::CreateStringValue(key)); | |
232 delegate_->Remove( | |
233 key, | |
234 CacheModifyingCallback::Create(callback, cache_ptr_factory_, key_list)); | |
235 } | |
236 | |
237 void ExtensionSettingsStorageCache::Remove( | |
238 const ListValue& keys, | |
239 ExtensionSettingsStorageCache::Callback *callback) { | |
240 std::string key; | |
241 // Invalidate each cached entry first, in case the remove fails. | |
242 // We will also need to do if after the callback, to avoid race conditions | |
243 // whether other API calls fill the cache on the UI thread. | |
244 RemoveAll(&cache_, &keys); | |
245 delegate_->Remove( | |
246 keys, | |
247 CacheModifyingCallback::Create( | |
248 callback, | |
249 cache_ptr_factory_, | |
250 keys.DeepCopy())); | |
251 } | |
252 | |
253 void ExtensionSettingsStorageCache::Clear( | |
254 ExtensionSettingsStorageCache::Callback* callback) { | |
255 cache_.Clear(); | |
256 delegate_->Clear( | |
257 CacheModifyingCallback::Create(callback, cache_ptr_factory_)); | |
258 } | |
259 | |
260 bool ExtensionSettingsStorageCache::GetFromCache( | |
261 const std::string& key, Value** value) { | |
262 Value* cached_value; | |
263 if (!cache_.Get(key, &cached_value)) { | |
264 return false; | |
265 } | |
266 *value = cached_value->DeepCopy(); | |
267 return true; | |
268 } | |
OLD | NEW |