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/syncable_extension_settings_storage.h" | 5 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/extensions/extension_settings_sync_util.h" | 8 #include "chrome/browser/extensions/extension_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" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 | 12 |
13 SyncableExtensionSettingsStorage::SyncableExtensionSettingsStorage( | 13 SyncableExtensionSettingsStorage::SyncableExtensionSettingsStorage( |
14 const scoped_refptr<ObserverListThreadSafe<ExtensionSettingsObserver> >& | 14 const scoped_refptr<ObserverListThreadSafe<ExtensionSettingsObserver> >& |
15 observers, | 15 observers, |
16 const std::string& extension_id, | 16 const std::string& extension_id, |
17 ExtensionSettingsStorage* delegate) | 17 ExtensionSettingsStorage* delegate) |
18 : observers_(observers), | 18 : observers_(observers), |
19 extension_id_(extension_id), | 19 extension_id_(extension_id), |
20 delegate_(delegate), | 20 delegate_(delegate), |
| 21 sync_type_(syncable::UNSPECIFIED), |
21 sync_processor_(NULL) { | 22 sync_processor_(NULL) { |
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
23 } | 24 } |
24 | 25 |
25 SyncableExtensionSettingsStorage::~SyncableExtensionSettingsStorage() { | 26 SyncableExtensionSettingsStorage::~SyncableExtensionSettingsStorage() { |
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
27 } | 28 } |
28 | 29 |
29 ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get( | 30 ExtensionSettingsStorage::Result SyncableExtensionSettingsStorage::Get( |
30 const std::string& key) { | 31 const std::string& key) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 104 } |
104 if (sync_processor_ && !result.GetChangedKeys()->empty()) { | 105 if (sync_processor_ && !result.GetChangedKeys()->empty()) { |
105 SendDeletesToSync(*result.GetChangedKeys()); | 106 SendDeletesToSync(*result.GetChangedKeys()); |
106 } | 107 } |
107 return result; | 108 return result; |
108 } | 109 } |
109 | 110 |
110 // Sync-related methods. | 111 // Sync-related methods. |
111 | 112 |
112 SyncError SyncableExtensionSettingsStorage::StartSyncing( | 113 SyncError SyncableExtensionSettingsStorage::StartSyncing( |
113 const DictionaryValue& sync_state, SyncChangeProcessor* sync_processor) { | 114 syncable::ModelType type, |
| 115 const DictionaryValue& sync_state, |
| 116 SyncChangeProcessor* sync_processor) { |
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 118 DCHECK(type == syncable::EXTENSION_SETTINGS || |
| 119 type == syncable::APP_SETTINGS); |
| 120 DCHECK_EQ(sync_type_, syncable::UNSPECIFIED); |
115 DCHECK(!sync_processor_); | 121 DCHECK(!sync_processor_); |
116 DCHECK(synced_keys_.empty()); | 122 DCHECK(synced_keys_.empty()); |
| 123 |
| 124 sync_type_ = type; |
117 sync_processor_ = sync_processor; | 125 sync_processor_ = sync_processor; |
118 | 126 |
119 Result maybe_settings = delegate_->Get(); | 127 Result maybe_settings = delegate_->Get(); |
120 if (maybe_settings.HasError()) { | 128 if (maybe_settings.HasError()) { |
121 return SyncError( | 129 return SyncError( |
122 FROM_HERE, | 130 FROM_HERE, |
123 std::string("Failed to get settings: ") + maybe_settings.GetError(), | 131 std::string("Failed to get settings: ") + maybe_settings.GetError(), |
124 syncable::EXTENSION_SETTINGS); | 132 type); |
125 } | 133 } |
126 | 134 |
127 const DictionaryValue* settings = maybe_settings.GetSettings(); | 135 const DictionaryValue* settings = maybe_settings.GetSettings(); |
128 if (sync_state.empty()) { | 136 if (sync_state.empty()) { |
129 return SendLocalSettingsToSync(*settings); | 137 return SendLocalSettingsToSync(*settings); |
130 } | 138 } |
131 return OverwriteLocalSettingsWithSync(sync_state, *settings); | 139 return OverwriteLocalSettingsWithSync(sync_state, *settings); |
132 } | 140 } |
133 | 141 |
134 SyncError SyncableExtensionSettingsStorage::SendLocalSettingsToSync( | 142 SyncError SyncableExtensionSettingsStorage::SendLocalSettingsToSync( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); | 222 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); |
215 if (sync_errors.empty()) { | 223 if (sync_errors.empty()) { |
216 return SyncError(); | 224 return SyncError(); |
217 } | 225 } |
218 // TODO(kalman): something sensible with multiple errors. | 226 // TODO(kalman): something sensible with multiple errors. |
219 return sync_errors[0]; | 227 return sync_errors[0]; |
220 } | 228 } |
221 | 229 |
222 void SyncableExtensionSettingsStorage::StopSyncing() { | 230 void SyncableExtensionSettingsStorage::StopSyncing() { |
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 232 DCHECK(sync_type_ == syncable::EXTENSION_SETTINGS || |
| 233 sync_type_ == syncable::APP_SETTINGS); |
224 DCHECK(sync_processor_); | 234 DCHECK(sync_processor_); |
| 235 |
| 236 sync_type_ = syncable::UNSPECIFIED; |
225 sync_processor_ = NULL; | 237 sync_processor_ = NULL; |
226 synced_keys_.clear(); | 238 synced_keys_.clear(); |
227 } | 239 } |
228 | 240 |
229 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( | 241 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( |
230 const ExtensionSettingSyncDataList& sync_changes) { | 242 const ExtensionSettingSyncDataList& sync_changes) { |
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
232 DCHECK(sync_processor_); | 244 DCHECK(sync_processor_); |
233 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; | 245 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; |
234 | 246 |
235 std::vector<SyncError> errors; | 247 std::vector<SyncError> errors; |
236 ExtensionSettingChanges::Builder changes; | 248 ExtensionSettingChanges::Builder changes; |
237 | 249 |
238 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); | 250 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); |
239 it != sync_changes.end(); ++it) { | 251 it != sync_changes.end(); ++it) { |
240 DCHECK_EQ(extension_id_, it->extension_id()); | 252 DCHECK_EQ(extension_id_, it->extension_id()); |
241 | 253 |
242 const std::string& key = it->key(); | 254 const std::string& key = it->key(); |
243 const Value& value = it->value(); | 255 const Value& value = it->value(); |
244 | 256 |
245 scoped_ptr<Value> current_value; | 257 scoped_ptr<Value> current_value; |
246 { | 258 { |
247 Result maybe_settings = Get(it->key()); | 259 Result maybe_settings = Get(it->key()); |
248 if (maybe_settings.HasError()) { | 260 if (maybe_settings.HasError()) { |
249 errors.push_back(SyncError( | 261 errors.push_back(SyncError( |
250 FROM_HERE, | 262 FROM_HERE, |
251 std::string("Error getting current sync state for ") + | 263 std::string("Error getting current sync state for ") + |
252 extension_id_ + "/" + key + ": " + maybe_settings.GetError(), | 264 extension_id_ + "/" + key + ": " + maybe_settings.GetError(), |
253 syncable::EXTENSION_SETTINGS)); | 265 sync_type_)); |
254 continue; | 266 continue; |
255 } | 267 } |
256 const DictionaryValue* settings = maybe_settings.GetSettings(); | 268 const DictionaryValue* settings = maybe_settings.GetSettings(); |
257 if (settings) { | 269 if (settings) { |
258 Value* value = NULL; | 270 Value* value = NULL; |
259 if (settings->GetWithoutPathExpansion(key, &value)) { | 271 if (settings->GetWithoutPathExpansion(key, &value)) { |
260 current_value.reset(value->DeepCopy()); | 272 current_value.reset(value->DeepCopy()); |
261 } | 273 } |
262 } | 274 } |
263 } | 275 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 Value* new_value, | 402 Value* new_value, |
391 ExtensionSettingChanges::Builder* changes) { | 403 ExtensionSettingChanges::Builder* changes) { |
392 DCHECK(new_value); | 404 DCHECK(new_value); |
393 synced_keys_.insert(key); | 405 synced_keys_.insert(key); |
394 Result result = delegate_->Set(key, *new_value); | 406 Result result = delegate_->Set(key, *new_value); |
395 if (result.HasError()) { | 407 if (result.HasError()) { |
396 return SyncError( | 408 return SyncError( |
397 FROM_HERE, | 409 FROM_HERE, |
398 std::string("Error pushing sync add to local settings: ") + | 410 std::string("Error pushing sync add to local settings: ") + |
399 result.GetError(), | 411 result.GetError(), |
400 syncable::EXTENSION_SETTINGS); | 412 sync_type_); |
401 } | 413 } |
402 changes->AppendChange(key, NULL, new_value); | 414 changes->AppendChange(key, NULL, new_value); |
403 return SyncError(); | 415 return SyncError(); |
404 } | 416 } |
405 | 417 |
406 SyncError SyncableExtensionSettingsStorage::OnSyncUpdate( | 418 SyncError SyncableExtensionSettingsStorage::OnSyncUpdate( |
407 const std::string& key, | 419 const std::string& key, |
408 Value* old_value, | 420 Value* old_value, |
409 Value* new_value, | 421 Value* new_value, |
410 ExtensionSettingChanges::Builder* changes) { | 422 ExtensionSettingChanges::Builder* changes) { |
411 DCHECK(old_value); | 423 DCHECK(old_value); |
412 DCHECK(new_value); | 424 DCHECK(new_value); |
413 Result result = delegate_->Set(key, *new_value); | 425 Result result = delegate_->Set(key, *new_value); |
414 if (result.HasError()) { | 426 if (result.HasError()) { |
415 return SyncError( | 427 return SyncError( |
416 FROM_HERE, | 428 FROM_HERE, |
417 std::string("Error pushing sync update to local settings: ") + | 429 std::string("Error pushing sync update to local settings: ") + |
418 result.GetError(), | 430 result.GetError(), |
419 syncable::EXTENSION_SETTINGS); | 431 sync_type_); |
420 } | 432 } |
421 changes->AppendChange(key, old_value, new_value); | 433 changes->AppendChange(key, old_value, new_value); |
422 return SyncError(); | 434 return SyncError(); |
423 } | 435 } |
424 | 436 |
425 SyncError SyncableExtensionSettingsStorage::OnSyncDelete( | 437 SyncError SyncableExtensionSettingsStorage::OnSyncDelete( |
426 const std::string& key, | 438 const std::string& key, |
427 Value* old_value, | 439 Value* old_value, |
428 ExtensionSettingChanges::Builder* changes) { | 440 ExtensionSettingChanges::Builder* changes) { |
429 DCHECK(old_value); | 441 DCHECK(old_value); |
430 synced_keys_.erase(key); | 442 synced_keys_.erase(key); |
431 Result result = delegate_->Remove(key); | 443 Result result = delegate_->Remove(key); |
432 if (result.HasError()) { | 444 if (result.HasError()) { |
433 return SyncError( | 445 return SyncError( |
434 FROM_HERE, | 446 FROM_HERE, |
435 std::string("Error pushing sync remove to local settings: ") + | 447 std::string("Error pushing sync remove to local settings: ") + |
436 result.GetError(), | 448 result.GetError(), |
437 syncable::EXTENSION_SETTINGS); | 449 sync_type_); |
438 } | 450 } |
439 changes->AppendChange(key, old_value, NULL); | 451 changes->AppendChange(key, old_value, NULL); |
440 return SyncError(); | 452 return SyncError(); |
441 } | 453 } |
OLD | NEW |