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/settings/settings_backend.h" | 5 #include "chrome/browser/extensions/settings/settings_backend.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 } | 67 } |
68 | 68 |
69 linked_ptr<SyncableSettingsStorage> syncable_storage( | 69 linked_ptr<SyncableSettingsStorage> syncable_storage( |
70 new SyncableSettingsStorage( | 70 new SyncableSettingsStorage( |
71 observers_, | 71 observers_, |
72 extension_id, | 72 extension_id, |
73 storage)); | 73 storage)); |
74 storage_objs_[extension_id] = syncable_storage; | 74 storage_objs_[extension_id] = syncable_storage; |
75 | 75 |
76 if (sync_processor_.get()) { | 76 if (sync_processor_.get()) { |
77 SyncError error = | 77 csync::SyncError error = |
78 syncable_storage->StartSyncing( | 78 syncable_storage->StartSyncing( |
79 sync_data, | 79 sync_data, |
80 CreateSettingsSyncProcessor(extension_id).Pass()); | 80 CreateSettingsSyncProcessor(extension_id).Pass()); |
81 if (error.IsSet()) | 81 if (error.IsSet()) |
82 syncable_storage.get()->StopSyncing(); | 82 syncable_storage.get()->StopSyncing(); |
83 } | 83 } |
84 | 84 |
85 return syncable_storage.get(); | 85 return syncable_storage.get(); |
86 } | 86 } |
87 | 87 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 return result; | 132 return result; |
133 } | 133 } |
134 | 134 |
135 static void AddAllSyncData( | 135 static void AddAllSyncData( |
136 const std::string& extension_id, | 136 const std::string& extension_id, |
137 const DictionaryValue& src, | 137 const DictionaryValue& src, |
138 syncable::ModelType type, | 138 syncable::ModelType type, |
139 SyncDataList* dst) { | 139 csync::SyncDataList* dst) { |
140 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { | 140 for (DictionaryValue::Iterator it(src); it.HasNext(); it.Advance()) { |
141 dst->push_back(settings_sync_util::CreateData( | 141 dst->push_back(settings_sync_util::CreateData( |
142 extension_id, it.key(), it.value(), type)); | 142 extension_id, it.key(), it.value(), type)); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 SyncDataList SettingsBackend::GetAllSyncData( | 146 csync::SyncDataList SettingsBackend::GetAllSyncData( |
147 syncable::ModelType type) const { | 147 syncable::ModelType type) const { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
149 // Ignore the type, it's just for sanity checking; assume that whatever base | 149 // Ignore the type, it's just for sanity checking; assume that whatever base |
150 // path we're constructed with is correct for the sync type. | 150 // path we're constructed with is correct for the sync type. |
151 DCHECK(type == syncable::EXTENSION_SETTINGS || | 151 DCHECK(type == syncable::EXTENSION_SETTINGS || |
152 type == syncable::APP_SETTINGS); | 152 type == syncable::APP_SETTINGS); |
153 | 153 |
154 // For all extensions, get all their settings. This has the effect | 154 // For all extensions, get all their settings. This has the effect |
155 // of bringing in the entire state of extension settings in memory; sad. | 155 // of bringing in the entire state of extension settings in memory; sad. |
156 SyncDataList all_sync_data; | 156 csync::SyncDataList all_sync_data; |
157 std::set<std::string> known_extension_ids(GetKnownExtensionIDs()); | 157 std::set<std::string> known_extension_ids(GetKnownExtensionIDs()); |
158 | 158 |
159 for (std::set<std::string>::const_iterator it = known_extension_ids.begin(); | 159 for (std::set<std::string>::const_iterator it = known_extension_ids.begin(); |
160 it != known_extension_ids.end(); ++it) { | 160 it != known_extension_ids.end(); ++it) { |
161 ValueStore::ReadResult maybe_settings = GetStorage(*it)->Get(); | 161 ValueStore::ReadResult maybe_settings = GetStorage(*it)->Get(); |
162 if (maybe_settings->HasError()) { | 162 if (maybe_settings->HasError()) { |
163 LOG(WARNING) << "Failed to get settings for " << *it << ": " << | 163 LOG(WARNING) << "Failed to get settings for " << *it << ": " << |
164 maybe_settings->error(); | 164 maybe_settings->error(); |
165 continue; | 165 continue; |
166 } | 166 } |
167 AddAllSyncData(*it, *maybe_settings->settings().get(), | 167 AddAllSyncData(*it, *maybe_settings->settings().get(), |
168 type, &all_sync_data); | 168 type, &all_sync_data); |
169 } | 169 } |
170 | 170 |
171 return all_sync_data; | 171 return all_sync_data; |
172 } | 172 } |
173 | 173 |
174 SyncError SettingsBackend::MergeDataAndStartSyncing( | 174 csync::SyncError SettingsBackend::MergeDataAndStartSyncing( |
175 syncable::ModelType type, | 175 syncable::ModelType type, |
176 const SyncDataList& initial_sync_data, | 176 const csync::SyncDataList& initial_sync_data, |
177 scoped_ptr<SyncChangeProcessor> sync_processor, | 177 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
178 scoped_ptr<SyncErrorFactory> sync_error_factory) { | 178 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) { |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
180 DCHECK(type == syncable::EXTENSION_SETTINGS || | 180 DCHECK(type == syncable::EXTENSION_SETTINGS || |
181 type == syncable::APP_SETTINGS); | 181 type == syncable::APP_SETTINGS); |
182 DCHECK_EQ(sync_type_, syncable::UNSPECIFIED); | 182 DCHECK_EQ(sync_type_, syncable::UNSPECIFIED); |
183 DCHECK(!sync_processor_.get()); | 183 DCHECK(!sync_processor_.get()); |
184 DCHECK(sync_processor.get()); | 184 DCHECK(sync_processor.get()); |
185 DCHECK(sync_error_factory.get()); | 185 DCHECK(sync_error_factory.get()); |
186 | 186 |
187 sync_type_ = type; | 187 sync_type_ = type; |
188 sync_processor_ = sync_processor.Pass(); | 188 sync_processor_ = sync_processor.Pass(); |
189 sync_error_factory_ = sync_error_factory.Pass(); | 189 sync_error_factory_ = sync_error_factory.Pass(); |
190 | 190 |
191 // Group the initial sync data by extension id. | 191 // Group the initial sync data by extension id. |
192 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data; | 192 std::map<std::string, linked_ptr<DictionaryValue> > grouped_sync_data; |
193 for (SyncDataList::const_iterator it = initial_sync_data.begin(); | 193 for (csync::SyncDataList::const_iterator it = initial_sync_data.begin(); |
194 it != initial_sync_data.end(); ++it) { | 194 it != initial_sync_data.end(); ++it) { |
195 SettingSyncData data(*it); | 195 SettingSyncData data(*it); |
asargent_no_longer_on_chrome
2012/06/26 18:08:17
nit: double space
akalin
2012/06/26 20:28:28
Done.
| |
196 linked_ptr<DictionaryValue> sync_data = | 196 linked_ptr<DictionaryValue> sync_data = |
197 grouped_sync_data[data.extension_id()]; | 197 grouped_sync_data[data.extension_id()]; |
198 if (!sync_data.get()) { | 198 if (!sync_data.get()) { |
199 sync_data = linked_ptr<DictionaryValue>(new DictionaryValue()); | 199 sync_data = linked_ptr<DictionaryValue>(new DictionaryValue()); |
200 grouped_sync_data[data.extension_id()] = sync_data; | 200 grouped_sync_data[data.extension_id()] = sync_data; |
201 } | 201 } |
202 DCHECK(!sync_data->HasKey(data.key())) << | 202 DCHECK(!sync_data->HasKey(data.key())) << |
203 "Duplicate settings for " << data.extension_id() << "/" << data.key(); | 203 "Duplicate settings for " << data.extension_id() << "/" << data.key(); |
204 sync_data->Set(data.key(), data.value().DeepCopy()); | 204 sync_data->Set(data.key(), data.value().DeepCopy()); |
205 } | 205 } |
206 | 206 |
207 // Start syncing all existing storage areas. Any storage areas created in | 207 // Start syncing all existing storage areas. Any storage areas created in |
208 // the future will start being synced as part of the creation process. | 208 // the future will start being synced as part of the creation process. |
209 for (StorageObjMap::iterator it = storage_objs_.begin(); | 209 for (StorageObjMap::iterator it = storage_objs_.begin(); |
210 it != storage_objs_.end(); ++it) { | 210 it != storage_objs_.end(); ++it) { |
211 std::map<std::string, linked_ptr<DictionaryValue> >::iterator | 211 std::map<std::string, linked_ptr<DictionaryValue> >::iterator |
212 maybe_sync_data = grouped_sync_data.find(it->first); | 212 maybe_sync_data = grouped_sync_data.find(it->first); |
213 SyncError error; | 213 csync::SyncError error; |
214 if (maybe_sync_data != grouped_sync_data.end()) { | 214 if (maybe_sync_data != grouped_sync_data.end()) { |
215 error = it->second->StartSyncing( | 215 error = it->second->StartSyncing( |
216 *maybe_sync_data->second, | 216 *maybe_sync_data->second, |
217 CreateSettingsSyncProcessor(it->first).Pass()); | 217 CreateSettingsSyncProcessor(it->first).Pass()); |
218 grouped_sync_data.erase(it->first); | 218 grouped_sync_data.erase(it->first); |
219 } else { | 219 } else { |
220 DictionaryValue empty; | 220 DictionaryValue empty; |
221 error = it->second->StartSyncing( | 221 error = it->second->StartSyncing( |
222 empty, | 222 empty, |
223 CreateSettingsSyncProcessor(it->first).Pass()); | 223 CreateSettingsSyncProcessor(it->first).Pass()); |
224 } | 224 } |
225 if (error.IsSet()) | 225 if (error.IsSet()) |
226 it->second->StopSyncing(); | 226 it->second->StopSyncing(); |
227 } | 227 } |
228 | 228 |
229 // Eagerly create and init the rest of the storage areas that have sync data. | 229 // Eagerly create and init the rest of the storage areas that have sync data. |
230 // Under normal circumstances (i.e. not first-time sync) this will be all of | 230 // Under normal circumstances (i.e. not first-time sync) this will be all of |
231 // them. | 231 // them. |
232 for (std::map<std::string, linked_ptr<DictionaryValue> >::iterator it = | 232 for (std::map<std::string, linked_ptr<DictionaryValue> >::iterator it = |
233 grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { | 233 grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { |
234 GetOrCreateStorageWithSyncData(it->first, *it->second); | 234 GetOrCreateStorageWithSyncData(it->first, *it->second); |
235 } | 235 } |
236 | 236 |
237 return SyncError(); | 237 return csync::SyncError(); |
238 } | 238 } |
239 | 239 |
240 SyncError SettingsBackend::ProcessSyncChanges( | 240 csync::SyncError SettingsBackend::ProcessSyncChanges( |
241 const tracked_objects::Location& from_here, | 241 const tracked_objects::Location& from_here, |
242 const SyncChangeList& sync_changes) { | 242 const csync::SyncChangeList& sync_changes) { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
244 DCHECK(sync_processor_.get()); | 244 DCHECK(sync_processor_.get()); |
245 | 245 |
246 // Group changes by extension, to pass all changes in a single method call. | 246 // Group changes by extension, to pass all changes in a single method call. |
247 std::map<std::string, SettingSyncDataList> grouped_sync_data; | 247 std::map<std::string, SettingSyncDataList> grouped_sync_data; |
248 for (SyncChangeList::const_iterator it = sync_changes.begin(); | 248 for (csync::SyncChangeList::const_iterator it = sync_changes.begin(); |
249 it != sync_changes.end(); ++it) { | 249 it != sync_changes.end(); ++it) { |
250 SettingSyncData data(*it); | 250 SettingSyncData data(*it); |
asargent_no_longer_on_chrome
2012/06/26 18:08:17
nit: double space
akalin
2012/06/26 20:28:28
Done.
| |
251 grouped_sync_data[data.extension_id()].push_back(data); | 251 grouped_sync_data[data.extension_id()].push_back(data); |
252 } | 252 } |
253 | 253 |
254 // Create any storage areas that don't exist yet but have sync data. | 254 // Create any storage areas that don't exist yet but have sync data. |
255 DictionaryValue empty; | 255 DictionaryValue empty; |
256 for (std::map<std::string, SettingSyncDataList>::iterator | 256 for (std::map<std::string, SettingSyncDataList>::iterator |
257 it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { | 257 it = grouped_sync_data.begin(); it != grouped_sync_data.end(); ++it) { |
258 SyncableSettingsStorage* storage = | 258 SyncableSettingsStorage* storage = |
259 GetOrCreateStorageWithSyncData(it->first, empty); | 259 GetOrCreateStorageWithSyncData(it->first, empty); |
260 SyncError error = storage->ProcessSyncChanges(it->second); | 260 csync::SyncError error = storage->ProcessSyncChanges(it->second); |
261 if (error.IsSet()) | 261 if (error.IsSet()) |
262 storage->StopSyncing(); | 262 storage->StopSyncing(); |
263 } | 263 } |
264 | 264 |
265 return SyncError(); | 265 return csync::SyncError(); |
266 } | 266 } |
267 | 267 |
268 void SettingsBackend::StopSyncing(syncable::ModelType type) { | 268 void SettingsBackend::StopSyncing(syncable::ModelType type) { |
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
270 DCHECK(type == syncable::EXTENSION_SETTINGS || | 270 DCHECK(type == syncable::EXTENSION_SETTINGS || |
271 type == syncable::APP_SETTINGS); | 271 type == syncable::APP_SETTINGS); |
272 DCHECK(sync_type_ == type || sync_type_ == syncable::UNSPECIFIED); | 272 DCHECK(sync_type_ == type || sync_type_ == syncable::UNSPECIFIED); |
273 | 273 |
274 for (StorageObjMap::iterator it = storage_objs_.begin(); | 274 for (StorageObjMap::iterator it = storage_objs_.begin(); |
275 it != storage_objs_.end(); ++it) { | 275 it != storage_objs_.end(); ++it) { |
(...skipping 10 matching lines...) Expand all Loading... | |
286 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( | 286 scoped_ptr<SettingsSyncProcessor> SettingsBackend::CreateSettingsSyncProcessor( |
287 const std::string& extension_id) const { | 287 const std::string& extension_id) const { |
288 CHECK(sync_processor_.get()); | 288 CHECK(sync_processor_.get()); |
289 return scoped_ptr<SettingsSyncProcessor>( | 289 return scoped_ptr<SettingsSyncProcessor>( |
290 new SettingsSyncProcessor(extension_id, | 290 new SettingsSyncProcessor(extension_id, |
291 sync_type_, | 291 sync_type_, |
292 sync_processor_.get())); | 292 sync_processor_.get())); |
293 } | 293 } |
294 | 294 |
295 } // namespace extensions | 295 } // namespace extensions |
OLD | NEW |