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

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

Issue 7775008: Enable sync for the settings from the Extension Settings API. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Comments, GCC compile fix Created 9 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) 2011 The Chromium Authors. All rights reserved. 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 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/extension_settings.h" 5 #include "chrome/browser/extensions/extension_settings.h"
6 6
7 #include "base/bind.h" 7 #include "base/compiler_specific.h"
8 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/linked_ptr.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
12 #include "content/browser/browser_thread.h"
13 #include "chrome/browser/extensions/extension_settings_leveldb_storage.h" 14 #include "chrome/browser/extensions/extension_settings_leveldb_storage.h"
14 #include "chrome/browser/extensions/extension_settings_noop_storage.h" 15 #include "chrome/browser/extensions/extension_settings_noop_storage.h"
15 #include "chrome/browser/extensions/extension_settings_storage_cache.h" 16 #include "chrome/browser/extensions/extension_settings_storage_cache.h"
17 #include "chrome/browser/extensions/extension_settings_sync_util.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "content/browser/browser_thread.h"
16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" 20 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 21 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
18 22
19 ExtensionSettings::ExtensionSettings(const FilePath& base_path) 23 ExtensionSettings::ExtensionSettings(const FilePath& base_path)
20 : base_path_(base_path) {} 24 : base_path_(base_path),
25 sync_processor_(NULL) {}
akalin 2011/09/20 14:53:11 DCHECK file thread
not at google - send to devlin 2011/09/21 00:20:14 Done.
21 26
22 ExtensionSettings::~ExtensionSettings() { 27 ExtensionSettings::~ExtensionSettings() {
23 std::map<std::string, ExtensionSettingsStorage*>::iterator it; 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
24 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) {
25 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, it->second);
26 }
27 } 29 }
28 30
29 ExtensionSettingsStorage* ExtensionSettings::GetStorage( 31 ExtensionSettingsStorage* ExtensionSettings::GetStorage(
30 const std::string& extension_id) { 32 const std::string& extension_id) const {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
34 DictionaryValue empty;
35 return GetOrCreateStorageWithSyncData(extension_id, empty);
36 }
32 37
33 std::map<std::string, ExtensionSettingsStorage*>::iterator existing = 38 SyncableExtensionSettingsStorage*
34 storage_objs_.find(extension_id); 39 ExtensionSettings::GetOrCreateStorageWithSyncData(
35 if (existing != storage_objs_.end()) { 40 const std::string& extension_id, const DictionaryValue& sync_data) const {
36 return existing->second; 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
42 SyncableExtensionSettingsStorage* storage = GetOrCreateAndInitStorage(
43 ExtensionSettingsStorage::LEVELDB,
44 true,
45 extension_id,
46 sync_data);
47 if (!storage) {
48 // Fall back to an in-memory storage area (cached NOOP).
49 // It's ok for these to be synced, it just means that on next starting up
50 // extensions will see the "old" settings, then overwritten (and notified)
51 // when the sync changes come through.
52 storage = GetOrCreateAndInitStorage(
53 ExtensionSettingsStorage::NOOP,
54 true,
55 extension_id,
56 sync_data);
57 DCHECK(storage);
37 } 58 }
38 59 return storage;
39 ExtensionSettingsStorage* new_storage =
40 CreateStorage(extension_id, ExtensionSettingsStorage::LEVELDB, true);
41 if (new_storage == NULL) {
42 // Failed to create a leveldb storage for some reason. Use an in memory
43 // storage area (no-op wrapped in a cache) instead.
44 new_storage =
45 CreateStorage(extension_id, ExtensionSettingsStorage::NOOP, true);
46 DCHECK(new_storage != NULL);
47 }
48
49 storage_objs_[extension_id] = new_storage;
50 return new_storage;
51 } 60 }
52 61
53 ExtensionSettingsStorage* ExtensionSettings::GetStorageForTesting( 62 ExtensionSettingsStorage* ExtensionSettings::GetStorageForTesting(
54 ExtensionSettingsStorage::Type type, 63 ExtensionSettingsStorage::Type type,
55 bool cached, 64 bool cached,
56 const std::string& extension_id) { 65 const std::string& extension_id) const {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
58 67 DictionaryValue empty;
59 std::map<std::string, ExtensionSettingsStorage*>::iterator existing = 68 ExtensionSettingsStorage* storage =
60 storage_objs_.find(extension_id); 69 GetOrCreateAndInitStorage(type, cached, extension_id, empty);
61 if (existing != storage_objs_.end()) { 70 DCHECK(storage);
62 return existing->second; 71 return storage;
63 }
64
65 ExtensionSettingsStorage* new_storage =
66 CreateStorage(extension_id, type, cached);
67 DCHECK(new_storage != NULL);
68 storage_objs_[extension_id] = new_storage;
69 return new_storage;
70 } 72 }
71 73
72 ExtensionSettingsStorage* ExtensionSettings::CreateStorage( 74 SyncableExtensionSettingsStorage* ExtensionSettings::GetOrCreateAndInitStorage(
75 ExtensionSettingsStorage::Type type,
76 bool cached,
73 const std::string& extension_id, 77 const std::string& extension_id,
78 const DictionaryValue& initial_sync_data) const {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
80 StorageObjMap::iterator existing = storage_objs_.find(extension_id);
81 if (existing != storage_objs_.end()) {
82 return existing->second.get();
83 }
84 return CreateAndInitStorage(type, cached, extension_id, initial_sync_data);
85 }
86
87 SyncableExtensionSettingsStorage* ExtensionSettings::CreateAndInitStorage(
74 ExtensionSettingsStorage::Type type, 88 ExtensionSettingsStorage::Type type,
75 bool cached) { 89 bool cached,
90 const std::string& extension_id,
91 const DictionaryValue& initial_sync_data) const {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
93 DCHECK_EQ(0u, storage_objs_.count(extension_id));
77 ExtensionSettingsStorage* storage = NULL; 94 ExtensionSettingsStorage* storage = NULL;
78 switch (type) { 95 switch (type) {
79 case ExtensionSettingsStorage::NOOP: 96 case ExtensionSettingsStorage::NOOP:
80 storage = new ExtensionSettingsNoopStorage(); 97 storage = new ExtensionSettingsNoopStorage();
81 break; 98 break;
82 case ExtensionSettingsStorage::LEVELDB: 99 case ExtensionSettingsStorage::LEVELDB:
83 storage = ExtensionSettingsLeveldbStorage::Create( 100 storage = ExtensionSettingsLeveldbStorage::Create(
84 base_path_, extension_id); 101 base_path_, extension_id);
85 break; 102 break;
86 default: 103 default:
87 NOTREACHED(); 104 NOTREACHED();
88 } 105 }
89 if (storage != NULL && cached) { 106 if (!storage) {
107 return NULL;
108 }
109 if (cached) {
90 storage = new ExtensionSettingsStorageCache(storage); 110 storage = new ExtensionSettingsStorageCache(storage);
91 } 111 }
92 return storage; 112
113 SyncableExtensionSettingsStorage* synced_storage =
114 new SyncableExtensionSettingsStorage(extension_id, storage);
115 if (sync_processor_) {
116 // TODO(kalman): do something if StartSyncing fails.
117 ignore_result(
118 synced_storage->StartSyncing(initial_sync_data, sync_processor_));
119 }
120 storage_objs_[extension_id] =
121 linked_ptr<SyncableExtensionSettingsStorage>(synced_storage);
122 return synced_storage;
93 } 123 }
124
125 std::set<std::string> ExtensionSettings::GetKnownExtensionIDs() const {
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
127 std::set<std::string> result;
128
129 // TODO(kalman): we will need to do something to disambiguate between app
130 // settings and extension settings, since settings for apps should be synced
131 // iff app sync is turned on, ditto for extensions.
132
133 // Extension IDs live in-memory and/or on disk. The cache will contain all
134 // that are in-memory.
135 for (StorageObjMap::iterator it = storage_objs_.begin();
136 it != storage_objs_.end(); ++it) {
137 result.insert(it->first);
138 }
139
140 // Leveldb databases are directories inside base_path_.
141 file_util::FileEnumerator::FindInfo find_info;
142 file_util::FileEnumerator extension_dirs(
143 base_path_, false, file_util::FileEnumerator::DIRECTORIES);
144 while (!extension_dirs.Next().empty()) {
145 extension_dirs.GetFindInfo(&find_info);
146 FilePath extension_dir(file_util::FileEnumerator::GetFilename(find_info));
147 DCHECK(!extension_dir.IsAbsolute());
148 // Extension IDs are created as std::strings so they *should* be ASCII.
149 std::string maybe_as_ascii(extension_dir.MaybeAsASCII());
150 if (!maybe_as_ascii.empty()) {
151 result.insert(maybe_as_ascii);
152 }
153 }
154
155 return result;
156 }
157
158 SyncDataList ExtensionSettings::GetAllSyncData(
159 syncable::ModelType type) const {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
161 DCHECK_EQ(type, syncable::EXTENSION_SETTINGS);
162
163 // For all extensions, get all their settings.
164 // This has the effect of bringing in the entire state of extension settings
165 // in memory; sad.
166 SyncDataList all_sync_data;
167 std::set<std::string> known_extension_ids(GetKnownExtensionIDs());
168
169 for (std::set<std::string>::const_iterator it = known_extension_ids.begin();
170 it != known_extension_ids.end(); ++it) {
171 ExtensionSettingsStorage::Result maybe_settings = GetStorage(*it)->Get();
172 if (maybe_settings.HasError()) {
173 LOG(WARNING) << "Failed to get settings for " << *it << ": " <<
174 maybe_settings.GetError();
175 continue;
176 }
177
178 DictionaryValue* settings = maybe_settings.GetSettings();
179 for (DictionaryValue::key_iterator key_it = settings->begin_keys();
180 key_it != settings->end_keys(); ++key_it) {
181 Value *value = NULL;
182 settings->GetWithoutPathExpansion(*key_it, &value);
183 all_sync_data.push_back(
184 extension_settings_sync_util::CreateData(*it, *key_it, *value));
185 }
186 }
187
188 return all_sync_data;
189 }
190
191 SyncError ExtensionSettings::MergeDataAndStartSyncing(
192 syncable::ModelType type,
193 const SyncDataList& initial_sync_data,
194 SyncChangeProcessor* sync_processor) {
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
196 DCHECK_EQ(type, syncable::EXTENSION_SETTINGS);
197 DCHECK(!sync_processor_);
198 sync_processor_ = sync_processor;
199
200 // Group the initial sync data by extension id.
201 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data;
202 for (SyncDataList::const_iterator it = initial_sync_data.begin();
203 it != initial_sync_data.end(); ++it) {
204 ExtensionSettingSyncData data(*it);
205 linked_ptr<DictionaryValue> sync_data =
206 grouped_sync_data[data.extension_id()];
207 if (!sync_data.get()) {
208 sync_data = linked_ptr<DictionaryValue>(new DictionaryValue());
209 grouped_sync_data[data.extension_id()] = sync_data;
210 }
211 DCHECK(!sync_data->HasKey(data.key())) <<
212 "Duplicate settings for " << data.extension_id() << "/" << data.key();
213 sync_data->Set(data.key(), data.value().DeepCopy());
214 }
215
216 // Start syncing all existing storage areas. Any storage areas created in
217 // the future will start being synced as part of the creation process.
218 for (StorageObjMap::iterator it = storage_objs_.begin();
219 it != storage_objs_.end(); ++it) {
220 std::map<std::string, linked_ptr<DictionaryValue> >::iterator
221 maybe_sync_data = grouped_sync_data.find(it->first);
222 if (maybe_sync_data != grouped_sync_data.end()) {
223 // TODO(kalman): do something if StartSyncing fails.
224 ignore_result(
225 it->second->StartSyncing(*maybe_sync_data->second, sync_processor));
226 grouped_sync_data.erase(it->first);
227 } else {
228 DictionaryValue empty;
229 // TODO(kalman): do something if StartSyncing fails.
230 ignore_result(it->second->StartSyncing(empty, sync_processor));
231 }
232 }
233
234 // Eagerly create and init the rest of the storage areas that have sync data.
235 // Under normal circumstances (i.e. not first-time sync) this will be all of
236 // them.
237 for (std::map<std::string, linked_ptr<DictionaryValue> >::iterator it =
238 grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) {
239 GetOrCreateStorageWithSyncData(it->first, *it->second);
240 }
241
242 return SyncError();
243 }
244
245 SyncError ExtensionSettings::ProcessSyncChanges(
246 const tracked_objects::Location& from_here,
247 const SyncChangeList& sync_changes) {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
249 DCHECK(sync_processor_);
250
251 // Group changes by extension, to pass all changes in a single method call.
252 std::map<std::string, ExtensionSettingSyncDataList> grouped_sync_data;
253 for (SyncChangeList::const_iterator it = sync_changes.begin();
254 it != sync_changes.end(); ++it) {
255 ExtensionSettingSyncData data(*it);
256 grouped_sync_data[data.extension_id()].push_back(data);
257 }
258
259 DictionaryValue empty;
260 for (std::map<std::string, ExtensionSettingSyncDataList>::iterator
261 it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) {
262 // TODO(kalman): do something if ProcessSyncChanges fails.
263 ignore_result(
264 GetOrCreateStorageWithSyncData(it->first, empty)->
265 ProcessSyncChanges(it->second));
266 }
267
268 return SyncError();
269 }
270
271 void ExtensionSettings::StopSyncing(syncable::ModelType type) {
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
273 DCHECK(sync_processor_);
274 sync_processor_ = NULL;
275
276 for (StorageObjMap::iterator it = storage_objs_.begin();
277 it != storage_objs_.end(); ++it) {
278 it->second->StopSyncing();
279 }
280 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698