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/settings/syncable_settings_storage.h" | 5 #include "chrome/browser/extensions/settings/syncable_settings_storage.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 8 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
9 #include "chrome/browser/sync/api/sync_data.h" | 9 #include "chrome/browser/sync/api/sync_data.h" |
10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" | 10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 139 |
140 if (sync_state.empty()) { | 140 if (sync_state.empty()) { |
141 return SendLocalSettingsToSync(maybe_settings.settings()); | 141 return SendLocalSettingsToSync(maybe_settings.settings()); |
142 } | 142 } |
143 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); | 143 return OverwriteLocalSettingsWithSync(sync_state, maybe_settings.settings()); |
144 } | 144 } |
145 | 145 |
146 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( | 146 SyncError SyncableSettingsStorage::SendLocalSettingsToSync( |
147 const DictionaryValue& settings) { | 147 const DictionaryValue& settings) { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 DCHECK(sync_processor_); |
| 150 |
149 SyncChangeList changes; | 151 SyncChangeList changes; |
150 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 152 for (DictionaryValue::key_iterator it = settings.begin_keys(); |
151 it != settings.end_keys(); ++it) { | 153 it != settings.end_keys(); ++it) { |
152 Value* value; | 154 Value* value; |
153 settings.GetWithoutPathExpansion(*it, &value); | 155 settings.GetWithoutPathExpansion(*it, &value); |
154 changes.push_back( | 156 changes.push_back( |
155 settings_sync_util::CreateAdd(extension_id_, *it, *value)); | 157 settings_sync_util::CreateAdd(extension_id_, *it, *value)); |
156 } | 158 } |
157 | 159 |
158 if (changes.empty()) { | 160 if (changes.empty()) { |
159 return SyncError(); | 161 return SyncError(); |
160 } | 162 } |
161 | 163 |
162 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 164 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
163 if (error.IsSet()) { | 165 if (error.IsSet()) { |
| 166 StopSyncing(); |
164 return error; | 167 return error; |
165 } | 168 } |
166 | 169 |
167 for (DictionaryValue::key_iterator it = settings.begin_keys(); | 170 for (DictionaryValue::key_iterator it = settings.begin_keys(); |
168 it != settings.end_keys(); ++it) { | 171 it != settings.end_keys(); ++it) { |
169 synced_keys_.insert(*it); | 172 synced_keys_.insert(*it); |
170 } | 173 } |
171 return SyncError(); | 174 return SyncError(); |
172 } | 175 } |
173 | 176 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 Value* value = NULL; | 218 Value* value = NULL; |
216 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); | 219 CHECK(new_sync_state->RemoveWithoutPathExpansion(key, &value)); |
217 changes.push_back( | 220 changes.push_back( |
218 SettingSyncData( | 221 SettingSyncData( |
219 SyncChange::ACTION_ADD, extension_id_, key, value)); | 222 SyncChange::ACTION_ADD, extension_id_, key, value)); |
220 } | 223 } |
221 | 224 |
222 if (changes.empty()) { | 225 if (changes.empty()) { |
223 return SyncError(); | 226 return SyncError(); |
224 } | 227 } |
225 | 228 return ProcessSyncChanges(changes); |
226 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); | |
227 if (sync_errors.empty()) { | |
228 return SyncError(); | |
229 } | |
230 // TODO(kalman): something sensible with multiple errors. | |
231 return sync_errors[0]; | |
232 } | 229 } |
233 | 230 |
234 void SyncableSettingsStorage::StopSyncing() { | 231 void SyncableSettingsStorage::StopSyncing() { |
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
236 DCHECK(sync_type_ == syncable::EXTENSION_SETTINGS || | |
237 sync_type_ == syncable::APP_SETTINGS); | |
238 DCHECK(sync_processor_); | |
239 | 233 |
| 234 // Stop syncing is allowed to be called multiple times without StartSyncing, |
| 235 // so don't DCHECK that these values aren't already disabled. |
240 sync_type_ = syncable::UNSPECIFIED; | 236 sync_type_ = syncable::UNSPECIFIED; |
241 sync_processor_ = NULL; | 237 sync_processor_ = NULL; |
242 synced_keys_.clear(); | 238 synced_keys_.clear(); |
243 } | 239 } |
244 | 240 |
245 std::vector<SyncError> SyncableSettingsStorage::ProcessSyncChanges( | 241 SyncError SyncableSettingsStorage::ProcessSyncChanges( |
246 const SettingSyncDataList& sync_changes) { | 242 const SettingSyncDataList& sync_changes) { |
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
248 DCHECK(sync_processor_); | |
249 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; | 244 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; |
250 | 245 |
| 246 if (!sync_processor_) { |
| 247 return SyncError( |
| 248 FROM_HERE, |
| 249 std::string("Sync is inactive for ") + extension_id_, |
| 250 sync_type_); |
| 251 } |
| 252 |
251 std::vector<SyncError> errors; | 253 std::vector<SyncError> errors; |
252 SettingChangeList changes; | 254 SettingChangeList changes; |
253 | 255 |
254 for (SettingSyncDataList::const_iterator it = sync_changes.begin(); | 256 for (SettingSyncDataList::const_iterator it = sync_changes.begin(); |
255 it != sync_changes.end(); ++it) { | 257 it != sync_changes.end(); ++it) { |
256 DCHECK_EQ(extension_id_, it->extension_id()); | 258 DCHECK_EQ(extension_id_, it->extension_id()); |
257 | 259 |
258 const std::string& key = it->key(); | 260 const std::string& key = it->key(); |
259 const Value& value = it->value(); | 261 const Value& value = it->value(); |
260 | 262 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 if (error.IsSet()) { | 322 if (error.IsSet()) { |
321 errors.push_back(error); | 323 errors.push_back(error); |
322 } | 324 } |
323 } | 325 } |
324 | 326 |
325 observers_->Notify( | 327 observers_->Notify( |
326 &SettingsObserver::OnSettingsChanged, | 328 &SettingsObserver::OnSettingsChanged, |
327 extension_id_, | 329 extension_id_, |
328 SettingChange::GetEventJson(changes)); | 330 SettingChange::GetEventJson(changes)); |
329 | 331 |
330 return errors; | 332 // TODO(kalman): Something sensible with multiple errors. |
| 333 return errors.empty() ? SyncError() : errors[0]; |
331 } | 334 } |
332 | 335 |
333 void SyncableSettingsStorage::SendChangesToSync( | 336 void SyncableSettingsStorage::SendChangesToSync( |
334 const SettingChangeList& changes) { | 337 const SettingChangeList& changes) { |
335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
336 DCHECK(sync_processor_); | 339 DCHECK(sync_processor_); |
337 DCHECK(!changes.empty()); | 340 DCHECK(!changes.empty()); |
338 | 341 |
339 SyncChangeList sync_changes; | 342 SyncChangeList sync_changes; |
340 std::set<std::string> added_keys; | 343 std::set<std::string> added_keys; |
(...skipping 28 matching lines...) Expand all Loading... |
369 } | 372 } |
370 } | 373 } |
371 | 374 |
372 if (sync_changes.empty()) { | 375 if (sync_changes.empty()) { |
373 return; | 376 return; |
374 } | 377 } |
375 | 378 |
376 SyncError error = | 379 SyncError error = |
377 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_changes); | 380 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_changes); |
378 if (error.IsSet()) { | 381 if (error.IsSet()) { |
379 LOG(WARNING) << "Failed to send changes to sync: " << error.message(); | 382 StopSyncing(); |
380 return; | 383 return; |
381 } | 384 } |
382 | 385 |
383 synced_keys_.insert(added_keys.begin(), added_keys.end()); | 386 synced_keys_.insert(added_keys.begin(), added_keys.end()); |
384 for (std::set<std::string>::iterator it = deleted_keys.begin(); | 387 for (std::set<std::string>::iterator it = deleted_keys.begin(); |
385 it != deleted_keys.end(); ++it) { | 388 it != deleted_keys.end(); ++it) { |
386 synced_keys_.erase(*it); | 389 synced_keys_.erase(*it); |
387 } | 390 } |
388 } | 391 } |
389 | 392 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 FROM_HERE, | 439 FROM_HERE, |
437 std::string("Error pushing sync remove to local settings: ") + | 440 std::string("Error pushing sync remove to local settings: ") + |
438 result.error(), | 441 result.error(), |
439 sync_type_); | 442 sync_type_); |
440 } | 443 } |
441 changes->push_back(SettingChange(key, old_value, NULL)); | 444 changes->push_back(SettingChange(key, old_value, NULL)); |
442 return SyncError(); | 445 return SyncError(); |
443 } | 446 } |
444 | 447 |
445 } // namespace extensions | 448 } // namespace extensions |
OLD | NEW |