| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/storage/settings_backend.h" | 5 #include "chrome/browser/extensions/api/storage/settings_backend.h" |
| 6 | 6 |
| 7 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 9 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
| 10 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 10 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 sync_type_ == syncer::APP_SETTINGS); | 34 sync_type_ == syncer::APP_SETTINGS); |
| 35 } | 35 } |
| 36 | 36 |
| 37 SettingsBackend::~SettingsBackend() { | 37 SettingsBackend::~SettingsBackend() { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 39 } | 39 } |
| 40 | 40 |
| 41 ValueStore* SettingsBackend::GetStorage( | 41 ValueStore* SettingsBackend::GetStorage( |
| 42 const std::string& extension_id) const { | 42 const std::string& extension_id) const { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 44 DictionaryValue empty; | 44 base::DictionaryValue empty; |
| 45 return GetOrCreateStorageWithSyncData(extension_id, empty); | 45 return GetOrCreateStorageWithSyncData(extension_id, empty); |
| 46 } | 46 } |
| 47 | 47 |
| 48 SyncableSettingsStorage* SettingsBackend::GetOrCreateStorageWithSyncData( | 48 SyncableSettingsStorage* SettingsBackend::GetOrCreateStorageWithSyncData( |
| 49 const std::string& extension_id, const DictionaryValue& sync_data) const { | 49 const std::string& extension_id, |
| 50 const base::DictionaryValue& sync_data) const { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 51 | 52 |
| 52 StorageObjMap::iterator maybe_storage = storage_objs_.find(extension_id); | 53 StorageObjMap::iterator maybe_storage = storage_objs_.find(extension_id); |
| 53 if (maybe_storage != storage_objs_.end()) { | 54 if (maybe_storage != storage_objs_.end()) { |
| 54 return maybe_storage->second.get(); | 55 return maybe_storage->second.get(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 ValueStore* storage = storage_factory_->Create(base_path_, extension_id); | 58 ValueStore* storage = storage_factory_->Create(base_path_, extension_id); |
| 58 CHECK(storage); | 59 CHECK(storage); |
| 59 | 60 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 if (!maybe_as_ascii.empty()) { | 126 if (!maybe_as_ascii.empty()) { |
| 126 result.insert(maybe_as_ascii); | 127 result.insert(maybe_as_ascii); |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 | 130 |
| 130 return result; | 131 return result; |
| 131 } | 132 } |
| 132 | 133 |
| 133 static void AddAllSyncData( | 134 static void AddAllSyncData( |
| 134 const std::string& extension_id, | 135 const std::string& extension_id, |
| 135 const DictionaryValue& src, | 136 const base::DictionaryValue& src, |
| 136 syncer::ModelType type, | 137 syncer::ModelType type, |
| 137 syncer::SyncDataList* dst) { | 138 syncer::SyncDataList* dst) { |
| 138 for (DictionaryValue::Iterator it(src); !it.IsAtEnd(); it.Advance()) { | 139 for (base::DictionaryValue::Iterator it(src); !it.IsAtEnd(); it.Advance()) { |
| 139 dst->push_back(settings_sync_util::CreateData( | 140 dst->push_back(settings_sync_util::CreateData( |
| 140 extension_id, it.key(), it.value(), type)); | 141 extension_id, it.key(), it.value(), type)); |
| 141 } | 142 } |
| 142 } | 143 } |
| 143 | 144 |
| 144 syncer::SyncDataList SettingsBackend::GetAllSyncData( | 145 syncer::SyncDataList SettingsBackend::GetAllSyncData( |
| 145 syncer::ModelType type) const { | 146 syncer::ModelType type) const { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 147 // Ignore the type, it's just for sanity checking; assume that whatever base | 148 // Ignore the type, it's just for sanity checking; assume that whatever base |
| 148 // path we're constructed with is correct for the sync type. | 149 // path we're constructed with is correct for the sync type. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 178 DCHECK_EQ(sync_type_, type); | 179 DCHECK_EQ(sync_type_, type); |
| 179 DCHECK(!sync_processor_.get()); | 180 DCHECK(!sync_processor_.get()); |
| 180 DCHECK(sync_processor.get()); | 181 DCHECK(sync_processor.get()); |
| 181 DCHECK(sync_error_factory.get()); | 182 DCHECK(sync_error_factory.get()); |
| 182 | 183 |
| 183 sync_processor_ = sync_processor.Pass(); | 184 sync_processor_ = sync_processor.Pass(); |
| 184 sync_error_factory_ = sync_error_factory.Pass(); | 185 sync_error_factory_ = sync_error_factory.Pass(); |
| 185 | 186 |
| 186 // Group the initial sync data by extension id. | 187 // Group the initial sync data by extension id. |
| 187 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data; | 188 std::map<std::string, linked_ptr<base::DictionaryValue> > grouped_sync_data; |
| 188 for (syncer::SyncDataList::const_iterator it = initial_sync_data.begin(); | 189 for (syncer::SyncDataList::const_iterator it = initial_sync_data.begin(); |
| 189 it != initial_sync_data.end(); ++it) { | 190 it != initial_sync_data.end(); ++it) { |
| 190 SettingSyncData data(*it); | 191 SettingSyncData data(*it); |
| 191 linked_ptr<DictionaryValue> sync_data = | 192 linked_ptr<base::DictionaryValue> sync_data = |
| 192 grouped_sync_data[data.extension_id()]; | 193 grouped_sync_data[data.extension_id()]; |
| 193 if (!sync_data.get()) { | 194 if (!sync_data.get()) { |
| 194 sync_data = linked_ptr<DictionaryValue>(new DictionaryValue()); | 195 sync_data = linked_ptr<base::DictionaryValue>( |
| 196 new base::DictionaryValue()); |
| 195 grouped_sync_data[data.extension_id()] = sync_data; | 197 grouped_sync_data[data.extension_id()] = sync_data; |
| 196 } | 198 } |
| 197 DCHECK(!sync_data->HasKey(data.key())) << | 199 DCHECK(!sync_data->HasKey(data.key())) << |
| 198 "Duplicate settings for " << data.extension_id() << "/" << data.key(); | 200 "Duplicate settings for " << data.extension_id() << "/" << data.key(); |
| 199 sync_data->SetWithoutPathExpansion(data.key(), data.value().DeepCopy()); | 201 sync_data->SetWithoutPathExpansion(data.key(), data.value().DeepCopy()); |
| 200 } | 202 } |
| 201 | 203 |
| 202 // Start syncing all existing storage areas. Any storage areas created in | 204 // Start syncing all existing storage areas. Any storage areas created in |
| 203 // the future will start being synced as part of the creation process. | 205 // the future will start being synced as part of the creation process. |
| 204 for (StorageObjMap::iterator it = storage_objs_.begin(); | 206 for (StorageObjMap::iterator it = storage_objs_.begin(); |
| 205 it != storage_objs_.end(); ++it) { | 207 it != storage_objs_.end(); ++it) { |
| 206 std::map<std::string, linked_ptr<DictionaryValue> >::iterator | 208 std::map<std::string, linked_ptr<base::DictionaryValue> >::iterator |
| 207 maybe_sync_data = grouped_sync_data.find(it->first); | 209 maybe_sync_data = grouped_sync_data.find(it->first); |
| 208 syncer::SyncError error; | 210 syncer::SyncError error; |
| 209 if (maybe_sync_data != grouped_sync_data.end()) { | 211 if (maybe_sync_data != grouped_sync_data.end()) { |
| 210 error = it->second->StartSyncing( | 212 error = it->second->StartSyncing( |
| 211 *maybe_sync_data->second, | 213 *maybe_sync_data->second, |
| 212 CreateSettingsSyncProcessor(it->first).Pass()); | 214 CreateSettingsSyncProcessor(it->first).Pass()); |
| 213 grouped_sync_data.erase(it->first); | 215 grouped_sync_data.erase(it->first); |
| 214 } else { | 216 } else { |
| 215 DictionaryValue empty; | 217 base::DictionaryValue empty; |
| 216 error = it->second->StartSyncing( | 218 error = it->second->StartSyncing( |
| 217 empty, | 219 empty, |
| 218 CreateSettingsSyncProcessor(it->first).Pass()); | 220 CreateSettingsSyncProcessor(it->first).Pass()); |
| 219 } | 221 } |
| 220 if (error.IsSet()) | 222 if (error.IsSet()) |
| 221 it->second->StopSyncing(); | 223 it->second->StopSyncing(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 // Eagerly create and init the rest of the storage areas that have sync data. | 226 // Eagerly create and init the rest of the storage areas that have sync data. |
| 225 // Under normal circumstances (i.e. not first-time sync) this will be all of | 227 // Under normal circumstances (i.e. not first-time sync) this will be all of |
| 226 // them. | 228 // them. |
| 227 for (std::map<std::string, linked_ptr<DictionaryValue> >::iterator it = | 229 for (std::map<std::string, linked_ptr<base::DictionaryValue> >::iterator it = |
| 228 grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { | 230 grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { |
| 229 GetOrCreateStorageWithSyncData(it->first, *it->second); | 231 GetOrCreateStorageWithSyncData(it->first, *it->second); |
| 230 } | 232 } |
| 231 | 233 |
| 232 return syncer::SyncMergeResult(type); | 234 return syncer::SyncMergeResult(type); |
| 233 } | 235 } |
| 234 | 236 |
| 235 syncer::SyncError SettingsBackend::ProcessSyncChanges( | 237 syncer::SyncError SettingsBackend::ProcessSyncChanges( |
| 236 const tracked_objects::Location& from_here, | 238 const tracked_objects::Location& from_here, |
| 237 const syncer::SyncChangeList& sync_changes) { | 239 const syncer::SyncChangeList& sync_changes) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 239 DCHECK(sync_processor_.get()); | 241 DCHECK(sync_processor_.get()); |
| 240 | 242 |
| 241 // Group changes by extension, to pass all changes in a single method call. | 243 // Group changes by extension, to pass all changes in a single method call. |
| 242 std::map<std::string, SettingSyncDataList> grouped_sync_data; | 244 std::map<std::string, SettingSyncDataList> grouped_sync_data; |
| 243 for (syncer::SyncChangeList::const_iterator it = sync_changes.begin(); | 245 for (syncer::SyncChangeList::const_iterator it = sync_changes.begin(); |
| 244 it != sync_changes.end(); ++it) { | 246 it != sync_changes.end(); ++it) { |
| 245 SettingSyncData data(*it); | 247 SettingSyncData data(*it); |
| 246 grouped_sync_data[data.extension_id()].push_back(data); | 248 grouped_sync_data[data.extension_id()].push_back(data); |
| 247 } | 249 } |
| 248 | 250 |
| 249 // Create any storage areas that don't exist yet but have sync data. | 251 // Create any storage areas that don't exist yet but have sync data. |
| 250 DictionaryValue empty; | 252 base::DictionaryValue empty; |
| 251 for (std::map<std::string, SettingSyncDataList>::iterator | 253 for (std::map<std::string, SettingSyncDataList>::iterator |
| 252 it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { | 254 it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { |
| 253 SyncableSettingsStorage* storage = | 255 SyncableSettingsStorage* storage = |
| 254 GetOrCreateStorageWithSyncData(it->first, empty); | 256 GetOrCreateStorageWithSyncData(it->first, empty); |
| 255 syncer::SyncError error = storage->ProcessSyncChanges(it->second); | 257 syncer::SyncError error = storage->ProcessSyncChanges(it->second); |
| 256 if (error.IsSet()) | 258 if (error.IsSet()) |
| 257 storage->StopSyncing(); | 259 storage->StopSyncing(); |
| 258 } | 260 } |
| 259 | 261 |
| 260 return syncer::SyncError(); | 262 return syncer::SyncError(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 280 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( | 282 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( |
| 281 const std::string& extension_id) const { | 283 const std::string& extension_id) const { |
| 282 CHECK(sync_processor_.get()); | 284 CHECK(sync_processor_.get()); |
| 283 return scoped_ptr<SettingsSyncProcessor>( | 285 return scoped_ptr<SettingsSyncProcessor>( |
| 284 new SettingsSyncProcessor(extension_id, | 286 new SettingsSyncProcessor(extension_id, |
| 285 sync_type_, | 287 sync_type_, |
| 286 sync_processor_.get())); | 288 sync_processor_.get())); |
| 287 } | 289 } |
| 288 | 290 |
| 289 } // namespace extensions | 291 } // namespace extensions |
| OLD | NEW |