| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sync_storage_backend.h" | 5 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 11 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
| 11 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 12 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
| 12 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" | 13 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "sync/api/sync_error_factory.h" | 15 #include "sync/api/sync_error_factory.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 void AddAllSyncData(const std::string& extension_id, | 23 void AddAllSyncData(const std::string& extension_id, |
| 23 const base::DictionaryValue& src, | 24 const base::DictionaryValue& src, |
| 24 syncer::ModelType type, | 25 syncer::ModelType type, |
| 25 syncer::SyncDataList* dst) { | 26 syncer::SyncDataList* dst) { |
| 26 for (base::DictionaryValue::Iterator it(src); !it.IsAtEnd(); it.Advance()) { | 27 for (base::DictionaryValue::Iterator it(src); !it.IsAtEnd(); it.Advance()) { |
| 27 dst->push_back(settings_sync_util::CreateData( | 28 dst->push_back(settings_sync_util::CreateData( |
| 28 extension_id, it.key(), it.value(), type)); | 29 extension_id, it.key(), it.value(), type)); |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 | 32 |
| 32 scoped_ptr<base::DictionaryValue> EmptyDictionaryValue() { | 33 std::unique_ptr<base::DictionaryValue> EmptyDictionaryValue() { |
| 33 return make_scoped_ptr(new base::DictionaryValue()); | 34 return base::WrapUnique(new base::DictionaryValue()); |
| 34 } | 35 } |
| 35 | 36 |
| 36 ValueStoreFactory::ModelType ToFactoryModelType(syncer::ModelType sync_type) { | 37 ValueStoreFactory::ModelType ToFactoryModelType(syncer::ModelType sync_type) { |
| 37 switch (sync_type) { | 38 switch (sync_type) { |
| 38 case syncer::APP_SETTINGS: | 39 case syncer::APP_SETTINGS: |
| 39 return ValueStoreFactory::ModelType::APP; | 40 return ValueStoreFactory::ModelType::APP; |
| 40 case syncer::EXTENSION_SETTINGS: | 41 case syncer::EXTENSION_SETTINGS: |
| 41 return ValueStoreFactory::ModelType::EXTENSION; | 42 return ValueStoreFactory::ModelType::EXTENSION; |
| 42 default: | 43 default: |
| 43 NOTREACHED(); | 44 NOTREACHED(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 65 | 66 |
| 66 SyncStorageBackend::~SyncStorageBackend() {} | 67 SyncStorageBackend::~SyncStorageBackend() {} |
| 67 | 68 |
| 68 ValueStore* SyncStorageBackend::GetStorage(const std::string& extension_id) { | 69 ValueStore* SyncStorageBackend::GetStorage(const std::string& extension_id) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 70 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 70 return GetOrCreateStorageWithSyncData(extension_id, EmptyDictionaryValue()); | 71 return GetOrCreateStorageWithSyncData(extension_id, EmptyDictionaryValue()); |
| 71 } | 72 } |
| 72 | 73 |
| 73 SyncableSettingsStorage* SyncStorageBackend::GetOrCreateStorageWithSyncData( | 74 SyncableSettingsStorage* SyncStorageBackend::GetOrCreateStorageWithSyncData( |
| 74 const std::string& extension_id, | 75 const std::string& extension_id, |
| 75 scoped_ptr<base::DictionaryValue> sync_data) const { | 76 std::unique_ptr<base::DictionaryValue> sync_data) const { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 77 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 77 | 78 |
| 78 StorageObjMap::iterator maybe_storage = storage_objs_.find(extension_id); | 79 StorageObjMap::iterator maybe_storage = storage_objs_.find(extension_id); |
| 79 if (maybe_storage != storage_objs_.end()) { | 80 if (maybe_storage != storage_objs_.end()) { |
| 80 return maybe_storage->second.get(); | 81 return maybe_storage->second.get(); |
| 81 } | 82 } |
| 82 | 83 |
| 83 scoped_ptr<SettingsStorageQuotaEnforcer> storage( | 84 std::unique_ptr<SettingsStorageQuotaEnforcer> storage( |
| 84 new SettingsStorageQuotaEnforcer( | 85 new SettingsStorageQuotaEnforcer( |
| 85 quota_, storage_factory_->CreateSettingsStore( | 86 quota_, storage_factory_->CreateSettingsStore( |
| 86 settings_namespace::SYNC, ToFactoryModelType(sync_type_), | 87 settings_namespace::SYNC, ToFactoryModelType(sync_type_), |
| 87 extension_id))); | 88 extension_id))); |
| 88 | 89 |
| 89 // It's fine to create the quota enforcer underneath the sync layer, since | 90 // It's fine to create the quota enforcer underneath the sync layer, since |
| 90 // sync will only go ahead if each underlying storage operation succeeds. | 91 // sync will only go ahead if each underlying storage operation succeeds. |
| 91 linked_ptr<SyncableSettingsStorage> syncable_storage( | 92 linked_ptr<SyncableSettingsStorage> syncable_storage( |
| 92 new SyncableSettingsStorage( | 93 new SyncableSettingsStorage( |
| 93 observers_, extension_id, storage.release(), sync_type_, flare_)); | 94 observers_, extension_id, storage.release(), sync_type_, flare_)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 160 } |
| 160 AddAllSyncData(*it, maybe_settings->settings(), type, &all_sync_data); | 161 AddAllSyncData(*it, maybe_settings->settings(), type, &all_sync_data); |
| 161 } | 162 } |
| 162 | 163 |
| 163 return all_sync_data; | 164 return all_sync_data; |
| 164 } | 165 } |
| 165 | 166 |
| 166 syncer::SyncMergeResult SyncStorageBackend::MergeDataAndStartSyncing( | 167 syncer::SyncMergeResult SyncStorageBackend::MergeDataAndStartSyncing( |
| 167 syncer::ModelType type, | 168 syncer::ModelType type, |
| 168 const syncer::SyncDataList& initial_sync_data, | 169 const syncer::SyncDataList& initial_sync_data, |
| 169 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 170 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 170 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 171 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 172 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 172 DCHECK_EQ(sync_type_, type); | 173 DCHECK_EQ(sync_type_, type); |
| 173 DCHECK(!sync_processor_.get()); | 174 DCHECK(!sync_processor_.get()); |
| 174 DCHECK(sync_processor.get()); | 175 DCHECK(sync_processor.get()); |
| 175 DCHECK(sync_error_factory.get()); | 176 DCHECK(sync_error_factory.get()); |
| 176 | 177 |
| 177 sync_processor_ = std::move(sync_processor); | 178 sync_processor_ = std::move(sync_processor); |
| 178 sync_error_factory_ = std::move(sync_error_factory); | 179 sync_error_factory_ = std::move(sync_error_factory); |
| 179 | 180 |
| 180 // Group the initial sync data by extension id. | 181 // Group the initial sync data by extension id. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 196 | 197 |
| 197 // Start syncing all existing storage areas. Any storage areas created in | 198 // Start syncing all existing storage areas. Any storage areas created in |
| 198 // the future will start being synced as part of the creation process. | 199 // the future will start being synced as part of the creation process. |
| 199 for (const auto& storage_obj : storage_objs_) { | 200 for (const auto& storage_obj : storage_objs_) { |
| 200 const std::string& extension_id = storage_obj.first; | 201 const std::string& extension_id = storage_obj.first; |
| 201 SyncableSettingsStorage* storage = storage_obj.second.get(); | 202 SyncableSettingsStorage* storage = storage_obj.second.get(); |
| 202 | 203 |
| 203 auto group = grouped_sync_data.find(extension_id); | 204 auto group = grouped_sync_data.find(extension_id); |
| 204 syncer::SyncError error; | 205 syncer::SyncError error; |
| 205 if (group != grouped_sync_data.end()) { | 206 if (group != grouped_sync_data.end()) { |
| 206 error = storage->StartSyncing(make_scoped_ptr(group->second), | 207 error = storage->StartSyncing(base::WrapUnique(group->second), |
| 207 CreateSettingsSyncProcessor(extension_id)); | 208 CreateSettingsSyncProcessor(extension_id)); |
| 208 grouped_sync_data.erase(group); | 209 grouped_sync_data.erase(group); |
| 209 } else { | 210 } else { |
| 210 error = storage->StartSyncing(EmptyDictionaryValue(), | 211 error = storage->StartSyncing(EmptyDictionaryValue(), |
| 211 CreateSettingsSyncProcessor(extension_id)); | 212 CreateSettingsSyncProcessor(extension_id)); |
| 212 } | 213 } |
| 213 | 214 |
| 214 if (error.IsSet()) | 215 if (error.IsSet()) |
| 215 storage->StopSyncing(); | 216 storage->StopSyncing(); |
| 216 } | 217 } |
| 217 | 218 |
| 218 // Eagerly create and init the rest of the storage areas that have sync data. | 219 // Eagerly create and init the rest of the storage areas that have sync data. |
| 219 // Under normal circumstances (i.e. not first-time sync) this will be all of | 220 // Under normal circumstances (i.e. not first-time sync) this will be all of |
| 220 // them. | 221 // them. |
| 221 for (const auto& group : grouped_sync_data) { | 222 for (const auto& group : grouped_sync_data) { |
| 222 GetOrCreateStorageWithSyncData(group.first, make_scoped_ptr(group.second)); | 223 GetOrCreateStorageWithSyncData(group.first, base::WrapUnique(group.second)); |
| 223 } | 224 } |
| 224 | 225 |
| 225 return syncer::SyncMergeResult(type); | 226 return syncer::SyncMergeResult(type); |
| 226 } | 227 } |
| 227 | 228 |
| 228 syncer::SyncError SyncStorageBackend::ProcessSyncChanges( | 229 syncer::SyncError SyncStorageBackend::ProcessSyncChanges( |
| 229 const tracked_objects::Location& from_here, | 230 const tracked_objects::Location& from_here, |
| 230 const syncer::SyncChangeList& sync_changes) { | 231 const syncer::SyncChangeList& sync_changes) { |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 232 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 232 DCHECK(sync_processor_.get()); | 233 DCHECK(sync_processor_.get()); |
| 233 | 234 |
| 234 // Group changes by extension, to pass all changes in a single method call. | 235 // Group changes by extension, to pass all changes in a single method call. |
| 235 // The raw pointers are safe because ownership of each item is passed to | 236 // The raw pointers are safe because ownership of each item is passed to |
| 236 // storage->ProcessSyncChanges. | 237 // storage->ProcessSyncChanges. |
| 237 std::map<std::string, SettingSyncDataList*> grouped_sync_data; | 238 std::map<std::string, SettingSyncDataList*> grouped_sync_data; |
| 238 | 239 |
| 239 for (const syncer::SyncChange& change : sync_changes) { | 240 for (const syncer::SyncChange& change : sync_changes) { |
| 240 scoped_ptr<SettingSyncData> data(new SettingSyncData(change)); | 241 std::unique_ptr<SettingSyncData> data(new SettingSyncData(change)); |
| 241 SettingSyncDataList*& group = grouped_sync_data[data->extension_id()]; | 242 SettingSyncDataList*& group = grouped_sync_data[data->extension_id()]; |
| 242 if (!group) | 243 if (!group) |
| 243 group = new SettingSyncDataList(); | 244 group = new SettingSyncDataList(); |
| 244 group->push_back(std::move(data)); | 245 group->push_back(std::move(data)); |
| 245 } | 246 } |
| 246 | 247 |
| 247 // Create any storage areas that don't exist yet but have sync data. | 248 // Create any storage areas that don't exist yet but have sync data. |
| 248 for (const auto& group : grouped_sync_data) { | 249 for (const auto& group : grouped_sync_data) { |
| 249 SyncableSettingsStorage* storage = | 250 SyncableSettingsStorage* storage = |
| 250 GetOrCreateStorageWithSyncData(group.first, EmptyDictionaryValue()); | 251 GetOrCreateStorageWithSyncData(group.first, EmptyDictionaryValue()); |
| 251 syncer::SyncError error = | 252 syncer::SyncError error = |
| 252 storage->ProcessSyncChanges(make_scoped_ptr(group.second)); | 253 storage->ProcessSyncChanges(base::WrapUnique(group.second)); |
| 253 if (error.IsSet()) | 254 if (error.IsSet()) |
| 254 storage->StopSyncing(); | 255 storage->StopSyncing(); |
| 255 } | 256 } |
| 256 | 257 |
| 257 return syncer::SyncError(); | 258 return syncer::SyncError(); |
| 258 } | 259 } |
| 259 | 260 |
| 260 void SyncStorageBackend::StopSyncing(syncer::ModelType type) { | 261 void SyncStorageBackend::StopSyncing(syncer::ModelType type) { |
| 261 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 262 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 262 DCHECK(type == syncer::EXTENSION_SETTINGS || type == syncer::APP_SETTINGS); | 263 DCHECK(type == syncer::EXTENSION_SETTINGS || type == syncer::APP_SETTINGS); |
| 263 DCHECK_EQ(sync_type_, type); | 264 DCHECK_EQ(sync_type_, type); |
| 264 | 265 |
| 265 for (const auto& storage_obj : storage_objs_) { | 266 for (const auto& storage_obj : storage_objs_) { |
| 266 // Some storage areas may have already stopped syncing if they had areas | 267 // Some storage areas may have already stopped syncing if they had areas |
| 267 // and syncing was disabled, but StopSyncing is safe to call multiple times. | 268 // and syncing was disabled, but StopSyncing is safe to call multiple times. |
| 268 storage_obj.second->StopSyncing(); | 269 storage_obj.second->StopSyncing(); |
| 269 } | 270 } |
| 270 | 271 |
| 271 sync_processor_.reset(); | 272 sync_processor_.reset(); |
| 272 sync_error_factory_.reset(); | 273 sync_error_factory_.reset(); |
| 273 } | 274 } |
| 274 | 275 |
| 275 scoped_ptr<SettingsSyncProcessor> | 276 std::unique_ptr<SettingsSyncProcessor> |
| 276 SyncStorageBackend::CreateSettingsSyncProcessor(const std::string& extension_id) | 277 SyncStorageBackend::CreateSettingsSyncProcessor( |
| 277 const { | 278 const std::string& extension_id) const { |
| 278 CHECK(sync_processor_.get()); | 279 CHECK(sync_processor_.get()); |
| 279 return scoped_ptr<SettingsSyncProcessor>(new SettingsSyncProcessor( | 280 return std::unique_ptr<SettingsSyncProcessor>(new SettingsSyncProcessor( |
| 280 extension_id, sync_type_, sync_processor_.get())); | 281 extension_id, sync_type_, sync_processor_.get())); |
| 281 } | 282 } |
| 282 | 283 |
| 283 } // namespace extensions | 284 } // namespace extensions |
| OLD | NEW |