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> |
| 8 |
7 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" | 11 #include "chrome/browser/extensions/api/storage/settings_sync_processor.h" |
10 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 12 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
11 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" | 13 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" |
12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
13 #include "sync/api/sync_error_factory.h" | 15 #include "sync/api/sync_error_factory.h" |
14 | 16 |
15 using content::BrowserThread; | 17 using content::BrowserThread; |
16 | 18 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 77 |
76 // It's fine to create the quota enforcer underneath the sync layer, since | 78 // It's fine to create the quota enforcer underneath the sync layer, since |
77 // sync will only go ahead if each underlying storage operation succeeds. | 79 // sync will only go ahead if each underlying storage operation succeeds. |
78 linked_ptr<SyncableSettingsStorage> syncable_storage( | 80 linked_ptr<SyncableSettingsStorage> syncable_storage( |
79 new SyncableSettingsStorage( | 81 new SyncableSettingsStorage( |
80 observers_, extension_id, storage.release(), sync_type_, flare_)); | 82 observers_, extension_id, storage.release(), sync_type_, flare_)); |
81 storage_objs_[extension_id] = syncable_storage; | 83 storage_objs_[extension_id] = syncable_storage; |
82 | 84 |
83 if (sync_processor_.get()) { | 85 if (sync_processor_.get()) { |
84 syncer::SyncError error = syncable_storage->StartSyncing( | 86 syncer::SyncError error = syncable_storage->StartSyncing( |
85 sync_data.Pass(), CreateSettingsSyncProcessor(extension_id).Pass()); | 87 std::move(sync_data), CreateSettingsSyncProcessor(extension_id)); |
86 if (error.IsSet()) | 88 if (error.IsSet()) |
87 syncable_storage->StopSyncing(); | 89 syncable_storage->StopSyncing(); |
88 } | 90 } |
89 return syncable_storage.get(); | 91 return syncable_storage.get(); |
90 } | 92 } |
91 | 93 |
92 void SyncStorageBackend::DeleteStorage(const std::string& extension_id) { | 94 void SyncStorageBackend::DeleteStorage(const std::string& extension_id) { |
93 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 95 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
94 | 96 |
95 // Clear settings when the extension is uninstalled. Leveldb implementations | 97 // Clear settings when the extension is uninstalled. Leveldb implementations |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 syncer::ModelType type, | 167 syncer::ModelType type, |
166 const syncer::SyncDataList& initial_sync_data, | 168 const syncer::SyncDataList& initial_sync_data, |
167 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 169 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
168 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { | 170 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
169 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 171 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
170 DCHECK_EQ(sync_type_, type); | 172 DCHECK_EQ(sync_type_, type); |
171 DCHECK(!sync_processor_.get()); | 173 DCHECK(!sync_processor_.get()); |
172 DCHECK(sync_processor.get()); | 174 DCHECK(sync_processor.get()); |
173 DCHECK(sync_error_factory.get()); | 175 DCHECK(sync_error_factory.get()); |
174 | 176 |
175 sync_processor_ = sync_processor.Pass(); | 177 sync_processor_ = std::move(sync_processor); |
176 sync_error_factory_ = sync_error_factory.Pass(); | 178 sync_error_factory_ = std::move(sync_error_factory); |
177 | 179 |
178 // Group the initial sync data by extension id. | 180 // Group the initial sync data by extension id. |
179 // The raw pointers are safe because ownership of each item is passed to | 181 // The raw pointers are safe because ownership of each item is passed to |
180 // storage->StartSyncing or GetOrCreateStorageWithSyncData. | 182 // storage->StartSyncing or GetOrCreateStorageWithSyncData. |
181 std::map<std::string, base::DictionaryValue*> grouped_sync_data; | 183 std::map<std::string, base::DictionaryValue*> grouped_sync_data; |
182 | 184 |
183 for (const syncer::SyncData& sync_data : initial_sync_data) { | 185 for (const syncer::SyncData& sync_data : initial_sync_data) { |
184 SettingSyncData data(sync_data); | 186 SettingSyncData data(sync_data); |
185 // Yes this really is a reference to a pointer. | 187 // Yes this really is a reference to a pointer. |
186 base::DictionaryValue*& settings = grouped_sync_data[data.extension_id()]; | 188 base::DictionaryValue*& settings = grouped_sync_data[data.extension_id()]; |
187 if (!settings) | 189 if (!settings) |
188 settings = new base::DictionaryValue(); | 190 settings = new base::DictionaryValue(); |
189 DCHECK(!settings->HasKey(data.key())) << "Duplicate settings for " | 191 DCHECK(!settings->HasKey(data.key())) << "Duplicate settings for " |
190 << data.extension_id() << "/" | 192 << data.extension_id() << "/" |
191 << data.key(); | 193 << data.key(); |
192 settings->SetWithoutPathExpansion(data.key(), data.PassValue()); | 194 settings->SetWithoutPathExpansion(data.key(), data.PassValue()); |
193 } | 195 } |
194 | 196 |
195 // Start syncing all existing storage areas. Any storage areas created in | 197 // Start syncing all existing storage areas. Any storage areas created in |
196 // the future will start being synced as part of the creation process. | 198 // the future will start being synced as part of the creation process. |
197 for (const auto& storage_obj : storage_objs_) { | 199 for (const auto& storage_obj : storage_objs_) { |
198 const std::string& extension_id = storage_obj.first; | 200 const std::string& extension_id = storage_obj.first; |
199 SyncableSettingsStorage* storage = storage_obj.second.get(); | 201 SyncableSettingsStorage* storage = storage_obj.second.get(); |
200 | 202 |
201 auto group = grouped_sync_data.find(extension_id); | 203 auto group = grouped_sync_data.find(extension_id); |
202 syncer::SyncError error; | 204 syncer::SyncError error; |
203 if (group != grouped_sync_data.end()) { | 205 if (group != grouped_sync_data.end()) { |
204 error = storage->StartSyncing( | 206 error = storage->StartSyncing(make_scoped_ptr(group->second), |
205 make_scoped_ptr(group->second), | 207 CreateSettingsSyncProcessor(extension_id)); |
206 CreateSettingsSyncProcessor(extension_id).Pass()); | |
207 grouped_sync_data.erase(group); | 208 grouped_sync_data.erase(group); |
208 } else { | 209 } else { |
209 error = storage->StartSyncing( | 210 error = storage->StartSyncing(EmptyDictionaryValue(), |
210 EmptyDictionaryValue(), | 211 CreateSettingsSyncProcessor(extension_id)); |
211 CreateSettingsSyncProcessor(extension_id).Pass()); | |
212 } | 212 } |
213 | 213 |
214 if (error.IsSet()) | 214 if (error.IsSet()) |
215 storage->StopSyncing(); | 215 storage->StopSyncing(); |
216 } | 216 } |
217 | 217 |
218 // Eagerly create and init the rest of the storage areas that have sync data. | 218 // 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 | 219 // Under normal circumstances (i.e. not first-time sync) this will be all of |
220 // them. | 220 // them. |
221 for (const auto& group : grouped_sync_data) { | 221 for (const auto& group : grouped_sync_data) { |
(...skipping 12 matching lines...) Expand all Loading... |
234 // Group changes by extension, to pass all changes in a single method call. | 234 // 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 | 235 // The raw pointers are safe because ownership of each item is passed to |
236 // storage->ProcessSyncChanges. | 236 // storage->ProcessSyncChanges. |
237 std::map<std::string, SettingSyncDataList*> grouped_sync_data; | 237 std::map<std::string, SettingSyncDataList*> grouped_sync_data; |
238 | 238 |
239 for (const syncer::SyncChange& change : sync_changes) { | 239 for (const syncer::SyncChange& change : sync_changes) { |
240 scoped_ptr<SettingSyncData> data(new SettingSyncData(change)); | 240 scoped_ptr<SettingSyncData> data(new SettingSyncData(change)); |
241 SettingSyncDataList*& group = grouped_sync_data[data->extension_id()]; | 241 SettingSyncDataList*& group = grouped_sync_data[data->extension_id()]; |
242 if (!group) | 242 if (!group) |
243 group = new SettingSyncDataList(); | 243 group = new SettingSyncDataList(); |
244 group->push_back(data.Pass()); | 244 group->push_back(std::move(data)); |
245 } | 245 } |
246 | 246 |
247 // Create any storage areas that don't exist yet but have sync data. | 247 // Create any storage areas that don't exist yet but have sync data. |
248 for (const auto& group : grouped_sync_data) { | 248 for (const auto& group : grouped_sync_data) { |
249 SyncableSettingsStorage* storage = | 249 SyncableSettingsStorage* storage = |
250 GetOrCreateStorageWithSyncData(group.first, EmptyDictionaryValue()); | 250 GetOrCreateStorageWithSyncData(group.first, EmptyDictionaryValue()); |
251 syncer::SyncError error = | 251 syncer::SyncError error = |
252 storage->ProcessSyncChanges(make_scoped_ptr(group.second)); | 252 storage->ProcessSyncChanges(make_scoped_ptr(group.second)); |
253 if (error.IsSet()) | 253 if (error.IsSet()) |
254 storage->StopSyncing(); | 254 storage->StopSyncing(); |
(...skipping 19 matching lines...) Expand all Loading... |
274 | 274 |
275 scoped_ptr<SettingsSyncProcessor> | 275 scoped_ptr<SettingsSyncProcessor> |
276 SyncStorageBackend::CreateSettingsSyncProcessor(const std::string& extension_id) | 276 SyncStorageBackend::CreateSettingsSyncProcessor(const std::string& extension_id) |
277 const { | 277 const { |
278 CHECK(sync_processor_.get()); | 278 CHECK(sync_processor_.get()); |
279 return scoped_ptr<SettingsSyncProcessor>(new SettingsSyncProcessor( | 279 return scoped_ptr<SettingsSyncProcessor>(new SettingsSyncProcessor( |
280 extension_id, sync_type_, sync_processor_.get())); | 280 extension_id, sync_type_, sync_processor_.get())); |
281 } | 281 } |
282 | 282 |
283 } // namespace extensions | 283 } // namespace extensions |
OLD | NEW |