Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "content/browser/browser_thread.h" | |
| 13 #include "chrome/browser/extensions/extension_settings_leveldb_storage.h" | 12 #include "chrome/browser/extensions/extension_settings_leveldb_storage.h" |
| 14 #include "chrome/browser/extensions/extension_settings_noop_storage.h" | 13 #include "chrome/browser/extensions/extension_settings_noop_storage.h" |
| 15 #include "chrome/browser/extensions/extension_settings_storage_cache.h" | 14 #include "chrome/browser/extensions/extension_settings_storage_cache.h" |
| 15 #include "chrome/browser/extensions/extension_settings_sync_util.h" | |
| 16 #include "chrome/common/extensions/extension.h" | |
| 17 #include "content/browser/browser_thread.h" | |
| 16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 18 | 20 |
| 19 ExtensionSettings::ExtensionSettings(const FilePath& base_path) | 21 ExtensionSettings::ExtensionSettings(const FilePath& base_path) |
| 20 : base_path_(base_path) {} | 22 : base_path_(base_path), |
| 23 extension_service_(NULL), | |
| 24 sync_processor_(NULL) {} | |
| 21 | 25 |
| 22 ExtensionSettings::~ExtensionSettings() { | 26 ExtensionSettings::~ExtensionSettings() { |
| 23 std::map<std::string, ExtensionSettingsStorage*>::iterator it; | 27 std::map<std::string, SyncableExtensionSettingsStorage*>::iterator it; |
| 24 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) { | 28 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) { |
| 25 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, it->second); | 29 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, it->second); |
| 26 } | 30 } |
| 27 } | 31 } |
| 28 | 32 |
| 33 void ExtensionSettings::SetExtensionService( | |
| 34 ExtensionServiceInterface* extension_service) { | |
| 35 extension_service_ = extension_service; | |
| 36 } | |
| 37 | |
| 29 ExtensionSettingsStorage* ExtensionSettings::GetStorage( | 38 ExtensionSettingsStorage* ExtensionSettings::GetStorage( |
| 30 const std::string& extension_id) { | 39 const std::string& extension_id) const { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 41 DictionaryValue empty; | |
| 42 return GetOrCreateStorageWithSyncData(extension_id, empty); | |
| 43 } | |
| 32 | 44 |
| 33 std::map<std::string, ExtensionSettingsStorage*>::iterator existing = | 45 SyncableExtensionSettingsStorage* |
| 34 storage_objs_.find(extension_id); | 46 ExtensionSettings::GetOrCreateStorageWithSyncData( |
| 35 if (existing != storage_objs_.end()) { | 47 const std::string& extension_id, const DictionaryValue& sync_data) const { |
| 36 return existing->second; | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 49 SyncableExtensionSettingsStorage* storage = GetOrCreateAndInitStorage( | |
| 50 ExtensionSettingsStorage::LEVELDB, | |
| 51 true, | |
| 52 extension_id, | |
| 53 sync_data); | |
| 54 if (storage == NULL) { | |
| 55 // Fall back to an in-memory storage area (cached NOOP). | |
| 56 storage = GetOrCreateAndInitStorage( | |
| 57 ExtensionSettingsStorage::NOOP, | |
| 58 true, | |
| 59 extension_id, | |
| 60 sync_data); | |
| 61 DCHECK(storage != NULL); | |
| 37 } | 62 } |
| 38 | 63 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 } | 64 } |
| 52 | 65 |
| 53 ExtensionSettingsStorage* ExtensionSettings::GetStorageForTesting( | 66 ExtensionSettingsStorage* ExtensionSettings::GetStorageForTesting( |
| 54 ExtensionSettingsStorage::Type type, | 67 ExtensionSettingsStorage::Type type, |
| 55 bool cached, | 68 bool cached, |
| 56 const std::string& extension_id) { | 69 const std::string& extension_id) const { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 71 DictionaryValue empty; | |
| 72 ExtensionSettingsStorage* storage = | |
| 73 GetOrCreateAndInitStorage(type, cached, extension_id, empty); | |
| 74 DCHECK(storage != NULL); | |
| 75 return storage; | |
| 76 } | |
| 58 | 77 |
| 59 std::map<std::string, ExtensionSettingsStorage*>::iterator existing = | 78 SyncableExtensionSettingsStorage* ExtensionSettings::GetOrCreateAndInitStorage( |
| 79 ExtensionSettingsStorage::Type type, | |
| 80 bool cached, | |
| 81 const std::string& extension_id, | |
| 82 const DictionaryValue& initial_sync_data) const { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 84 std::map<std::string, SyncableExtensionSettingsStorage*>::iterator existing = | |
| 60 storage_objs_.find(extension_id); | 85 storage_objs_.find(extension_id); |
| 61 if (existing != storage_objs_.end()) { | 86 if (existing != storage_objs_.end()) { |
| 62 return existing->second; | 87 return existing->second; |
| 63 } | 88 } |
| 64 | 89 return CreateAndInitStorage(type, cached, extension_id, initial_sync_data); |
| 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 } | 90 } |
| 71 | 91 |
| 72 ExtensionSettingsStorage* ExtensionSettings::CreateStorage( | 92 SyncableExtensionSettingsStorage* ExtensionSettings::CreateAndInitStorage( |
| 93 ExtensionSettingsStorage::Type type, | |
| 94 bool cached, | |
| 73 const std::string& extension_id, | 95 const std::string& extension_id, |
| 74 ExtensionSettingsStorage::Type type, | 96 const DictionaryValue& initial_sync_data) const { |
| 75 bool cached) { | |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 98 DCHECK(storage_objs_.count(extension_id) == 0); | |
| 77 ExtensionSettingsStorage* storage = NULL; | 99 ExtensionSettingsStorage* storage = NULL; |
| 78 switch (type) { | 100 switch (type) { |
| 79 case ExtensionSettingsStorage::NOOP: | 101 case ExtensionSettingsStorage::NOOP: |
| 80 storage = new ExtensionSettingsNoopStorage(); | 102 storage = new ExtensionSettingsNoopStorage(); |
| 81 break; | 103 break; |
| 82 case ExtensionSettingsStorage::LEVELDB: | 104 case ExtensionSettingsStorage::LEVELDB: |
| 83 storage = ExtensionSettingsLeveldbStorage::Create( | 105 storage = ExtensionSettingsLeveldbStorage::Create( |
| 84 base_path_, extension_id); | 106 base_path_, extension_id); |
| 85 break; | 107 break; |
| 86 default: | 108 default: |
| 87 NOTREACHED(); | 109 NOTREACHED(); |
| 88 } | 110 } |
| 89 if (storage != NULL && cached) { | 111 if (storage == NULL) { |
|
akalin
2011/09/14 21:08:53
i believe chrome style is:
!storage
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 112 return NULL; | |
| 113 } | |
| 114 if (cached) { | |
| 90 storage = new ExtensionSettingsStorageCache(storage); | 115 storage = new ExtensionSettingsStorageCache(storage); |
| 91 } | 116 } |
| 92 return storage; | 117 |
| 118 SyncableExtensionSettingsStorage* synced_storage = | |
| 119 new SyncableExtensionSettingsStorage(extension_id, storage); | |
| 120 if (sync_processor_ != NULL) { | |
|
akalin
2011/09/14 21:08:53
chrome style: if (sync_processor_)
(here and othe
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 121 synced_storage->StartSyncing(initial_sync_data, sync_processor_); | |
| 122 } | |
| 123 storage_objs_[extension_id] = synced_storage; | |
| 124 return synced_storage; | |
| 93 } | 125 } |
| 126 | |
| 127 SyncDataList ExtensionSettings::GetAllSyncData( | |
| 128 syncable::ModelType type) const { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 130 DCHECK(type == syncable::EXTENSION_SETTINGS); | |
|
akalin
2011/09/14 21:08:53
DCHECK_EQ
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 131 DCHECK(extension_service_ != NULL); | |
| 132 | |
| 133 // For all extensions, get all their settings. | |
| 134 // This has the effect of bringing in the entire state of extension settings | |
| 135 // in memory; sad. | |
| 136 SyncDataList all_sync_data; | |
| 137 const ExtensionList* extensions = extension_service_->extensions(); | |
|
Ben Olmstead
2011/09/14 20:33:24
You will also need to go through the disabled_exte
akalin
2011/09/14 21:08:53
Maybe we should expose a method in ExtensionsServi
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 138 for (ExtensionList::const_iterator it = extensions->begin(); | |
| 139 it != extensions->end(); ++it) { | |
| 140 std::string extension_id = (*it)->id(); | |
| 141 ExtensionSettingsStorage::Result maybe_settings = | |
| 142 GetStorage(extension_id)->Get(); | |
| 143 if (maybe_settings.HasError()) { | |
| 144 LOG(WARNING) << "Failed to get settings for " << extension_id << ": " << | |
| 145 maybe_settings.GetError(); | |
| 146 continue; | |
| 147 } | |
| 148 | |
| 149 DictionaryValue* settings = maybe_settings.GetSettings(); | |
| 150 for (DictionaryValue::key_iterator key_it = settings->begin_keys(); | |
| 151 key_it != settings->end_keys(); ++key_it) { | |
| 152 Value *value; | |
| 153 settings->GetWithoutPathExpansion(*key_it, &value); | |
| 154 all_sync_data.push_back( | |
| 155 extension_settings_sync_util::CreateData( | |
| 156 extension_id, *key_it, *value)); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 return all_sync_data; | |
| 161 } | |
| 162 | |
| 163 SyncError ExtensionSettings::MergeDataAndStartSyncing( | |
| 164 syncable::ModelType type, | |
| 165 const SyncDataList& initial_sync_data, | |
| 166 SyncChangeProcessor* sync_processor) { | |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 168 DCHECK(type == syncable::EXTENSION_SETTINGS); | |
| 169 DCHECK(sync_processor_ == NULL); | |
| 170 sync_processor_ = sync_processor; | |
| 171 | |
| 172 // Group the initial sync data by extension id. | |
| 173 std::map<std::string, DictionaryValue*> grouped_sync_data; | |
|
akalin
2011/09/14 21:08:53
i'd rather you use linked_ptr<DictionaryValue> her
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 174 for (SyncDataList::const_iterator it = initial_sync_data.begin(); | |
| 175 it != initial_sync_data.end(); ++it) { | |
| 176 ExtensionSettingSyncData data(*it); | |
| 177 if (data.value() == NULL) { | |
| 178 LOG(WARNING) << "NULL value in sync data"; | |
| 179 continue; | |
| 180 } | |
| 181 DictionaryValue* sync_data = grouped_sync_data[data.extension_id()]; | |
| 182 if (sync_data == NULL) { | |
| 183 sync_data = new DictionaryValue(); | |
| 184 grouped_sync_data[data.extension_id()] = sync_data; | |
| 185 } | |
| 186 DCHECK(!sync_data->HasKey(data.key())) << | |
| 187 "Duplicate settings for " << data.extension_id() << "/" << data.key(); | |
| 188 sync_data->Set(data.key(), data.value()->DeepCopy()); | |
| 189 } | |
| 190 | |
| 191 // Start syncing all existing storage areas. Any storage areas created in | |
| 192 // the future will start being synced as part of the creation process. | |
| 193 std::map<std::string, SyncableExtensionSettingsStorage*>::iterator storage_it; | |
|
akalin
2011/09/14 21:08:53
prefer scoping the iterator in the for loop, even
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 194 for (storage_it = storage_objs_.begin(); | |
| 195 storage_it != storage_objs_.end(); ++storage_it) { | |
| 196 scoped_ptr<DictionaryValue> sync_data(grouped_sync_data[storage_it->first]); | |
| 197 if (sync_data.get() == NULL) { | |
|
akalin
2011/09/14 21:08:53
prefer the "positive" case first, i.e.
if (sync_d
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 198 DictionaryValue empty; | |
| 199 storage_it->second->StartSyncing(empty, sync_processor); | |
| 200 } else { | |
| 201 storage_it->second->StartSyncing(*sync_data, sync_processor); | |
| 202 } | |
| 203 grouped_sync_data.erase(storage_it->first); | |
| 204 } | |
| 205 | |
| 206 // Eagerly create and init the rest of the storage areas that have sync data. | |
| 207 // Under normal circumstances this will probably be all of them. | |
|
akalin
2011/09/14 21:08:53
"normal circumstances" -> "normal circumstances (i
not at google - send to devlin
2011/09/14 22:45:05
Done.
| |
| 208 std::map<std::string, DictionaryValue*>::iterator data_it; | |
| 209 for (data_it = grouped_sync_data.begin(); | |
| 210 data_it != grouped_sync_data.end(); ++data_it) { | |
| 211 scoped_ptr<DictionaryValue> sync_data(data_it->second); | |
| 212 GetOrCreateStorageWithSyncData(data_it->first, *sync_data); | |
| 213 } | |
| 214 | |
| 215 return SyncError(); | |
| 216 } | |
| 217 | |
| 218 SyncError ExtensionSettings::ProcessSyncChanges( | |
| 219 const tracked_objects::Location& from_here, | |
| 220 const SyncChangeList& sync_changes) { | |
| 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 222 DCHECK(sync_processor_ != NULL); | |
| 223 | |
| 224 // Group changes by extension, to pass all changes in a single method call. | |
| 225 std::map<std::string, ExtensionSettingSyncDataList> grouped_sync_data; | |
| 226 for (SyncChangeList::const_iterator it = sync_changes.begin(); | |
| 227 it != sync_changes.end(); ++it) { | |
| 228 ExtensionSettingSyncData data(*it); | |
| 229 if (data.value() == NULL) { | |
| 230 LOG(WARNING) << "NULL value in sync data"; | |
| 231 continue; | |
| 232 } | |
| 233 grouped_sync_data[data.extension_id()].push_back(data); | |
| 234 } | |
| 235 | |
| 236 std::map<std::string, ExtensionSettingSyncDataList>::iterator it; | |
| 237 DictionaryValue empty; | |
| 238 for (it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { | |
| 239 GetOrCreateStorageWithSyncData(it->first, empty)-> | |
| 240 ProcessSyncChanges(it->second); | |
| 241 } | |
| 242 | |
| 243 return SyncError(); | |
| 244 } | |
| 245 | |
| 246 void ExtensionSettings::StopSyncing(syncable::ModelType type) { | |
| 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 248 DCHECK(sync_processor_ != NULL); | |
| 249 sync_processor_ = NULL; | |
| 250 | |
| 251 std::map<std::string, SyncableExtensionSettingsStorage*>::iterator it; | |
| 252 for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) { | |
| 253 it->second->StopSyncing(); | |
| 254 } | |
| 255 } | |
| OLD | NEW |