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/browser/browser_thread.h" | 11 #include "content/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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); | 219 std::vector<SyncError> sync_errors(ProcessSyncChanges(changes)); |
212 if (sync_errors.empty()) { | 220 if (sync_errors.empty()) { |
213 return SyncError(); | 221 return SyncError(); |
214 } | 222 } |
215 // TODO(kalman): something sensible with multiple errors. | 223 // TODO(kalman): something sensible with multiple errors. |
216 return sync_errors[0]; | 224 return sync_errors[0]; |
217 } | 225 } |
218 | 226 |
219 void SyncableExtensionSettingsStorage::StopSyncing() { | 227 void SyncableExtensionSettingsStorage::StopSyncing() { |
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 229 DCHECK(sync_type_ == syncable::EXTENSION_SETTINGS || |
| 230 sync_type_ == syncable::APP_SETTINGS); |
221 DCHECK(sync_processor_); | 231 DCHECK(sync_processor_); |
| 232 |
| 233 sync_type_ = syncable::UNSPECIFIED; |
222 sync_processor_ = NULL; | 234 sync_processor_ = NULL; |
223 synced_keys_.clear(); | 235 synced_keys_.clear(); |
224 } | 236 } |
225 | 237 |
226 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( | 238 std::vector<SyncError> SyncableExtensionSettingsStorage::ProcessSyncChanges( |
227 const ExtensionSettingSyncDataList& sync_changes) { | 239 const ExtensionSettingSyncDataList& sync_changes) { |
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
229 DCHECK(sync_processor_); | 241 DCHECK(sync_processor_); |
230 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; | 242 DCHECK(!sync_changes.empty()) << "No sync changes for " << extension_id_; |
231 | 243 |
232 std::vector<SyncError> errors; | 244 std::vector<SyncError> errors; |
233 ExtensionSettingChanges::Builder changes; | 245 ExtensionSettingChanges::Builder changes; |
234 | 246 |
235 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); | 247 for (ExtensionSettingSyncDataList::const_iterator it = sync_changes.begin(); |
236 it != sync_changes.end(); ++it) { | 248 it != sync_changes.end(); ++it) { |
237 DCHECK_EQ(extension_id_, it->extension_id()); | 249 DCHECK_EQ(extension_id_, it->extension_id()); |
238 | 250 |
239 const std::string& key = it->key(); | 251 const std::string& key = it->key(); |
240 const Value& value = it->value(); | 252 const Value& value = it->value(); |
241 | 253 |
242 scoped_ptr<Value> current_value; | 254 scoped_ptr<Value> current_value; |
243 { | 255 { |
244 Result maybe_settings = Get(it->key()); | 256 Result maybe_settings = Get(it->key()); |
245 if (maybe_settings.HasError()) { | 257 if (maybe_settings.HasError()) { |
246 errors.push_back(SyncError( | 258 errors.push_back(SyncError( |
247 FROM_HERE, | 259 FROM_HERE, |
248 std::string("Error getting current sync state for ") + | 260 std::string("Error getting current sync state for ") + |
249 extension_id_ + "/" + key + ": " + maybe_settings.GetError(), | 261 extension_id_ + "/" + key + ": " + maybe_settings.GetError(), |
250 syncable::EXTENSION_SETTINGS)); | 262 sync_type_)); |
251 continue; | 263 continue; |
252 } | 264 } |
253 const DictionaryValue* settings = maybe_settings.GetSettings(); | 265 const DictionaryValue* settings = maybe_settings.GetSettings(); |
254 if (settings) { | 266 if (settings) { |
255 Value* value = NULL; | 267 Value* value = NULL; |
256 if (settings->GetWithoutPathExpansion(key, &value)) { | 268 if (settings->GetWithoutPathExpansion(key, &value)) { |
257 current_value.reset(value->DeepCopy()); | 269 current_value.reset(value->DeepCopy()); |
258 } | 270 } |
259 } | 271 } |
260 } | 272 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 Value* new_value, | 399 Value* new_value, |
388 ExtensionSettingChanges::Builder* changes) { | 400 ExtensionSettingChanges::Builder* changes) { |
389 DCHECK(new_value); | 401 DCHECK(new_value); |
390 synced_keys_.insert(key); | 402 synced_keys_.insert(key); |
391 Result result = delegate_->Set(key, *new_value); | 403 Result result = delegate_->Set(key, *new_value); |
392 if (result.HasError()) { | 404 if (result.HasError()) { |
393 return SyncError( | 405 return SyncError( |
394 FROM_HERE, | 406 FROM_HERE, |
395 std::string("Error pushing sync add to local settings: ") + | 407 std::string("Error pushing sync add to local settings: ") + |
396 result.GetError(), | 408 result.GetError(), |
397 syncable::EXTENSION_SETTINGS); | 409 sync_type_); |
398 } | 410 } |
399 changes->AppendChange(key, NULL, new_value); | 411 changes->AppendChange(key, NULL, new_value); |
400 return SyncError(); | 412 return SyncError(); |
401 } | 413 } |
402 | 414 |
403 SyncError SyncableExtensionSettingsStorage::OnSyncUpdate( | 415 SyncError SyncableExtensionSettingsStorage::OnSyncUpdate( |
404 const std::string& key, | 416 const std::string& key, |
405 Value* old_value, | 417 Value* old_value, |
406 Value* new_value, | 418 Value* new_value, |
407 ExtensionSettingChanges::Builder* changes) { | 419 ExtensionSettingChanges::Builder* changes) { |
408 DCHECK(old_value); | 420 DCHECK(old_value); |
409 DCHECK(new_value); | 421 DCHECK(new_value); |
410 Result result = delegate_->Set(key, *new_value); | 422 Result result = delegate_->Set(key, *new_value); |
411 if (result.HasError()) { | 423 if (result.HasError()) { |
412 return SyncError( | 424 return SyncError( |
413 FROM_HERE, | 425 FROM_HERE, |
414 std::string("Error pushing sync update to local settings: ") + | 426 std::string("Error pushing sync update to local settings: ") + |
415 result.GetError(), | 427 result.GetError(), |
416 syncable::EXTENSION_SETTINGS); | 428 sync_type_); |
417 } | 429 } |
418 changes->AppendChange(key, old_value, new_value); | 430 changes->AppendChange(key, old_value, new_value); |
419 return SyncError(); | 431 return SyncError(); |
420 } | 432 } |
421 | 433 |
422 SyncError SyncableExtensionSettingsStorage::OnSyncDelete( | 434 SyncError SyncableExtensionSettingsStorage::OnSyncDelete( |
423 const std::string& key, | 435 const std::string& key, |
424 Value* old_value, | 436 Value* old_value, |
425 ExtensionSettingChanges::Builder* changes) { | 437 ExtensionSettingChanges::Builder* changes) { |
426 DCHECK(old_value); | 438 DCHECK(old_value); |
427 synced_keys_.erase(key); | 439 synced_keys_.erase(key); |
428 Result result = delegate_->Remove(key); | 440 Result result = delegate_->Remove(key); |
429 if (result.HasError()) { | 441 if (result.HasError()) { |
430 return SyncError( | 442 return SyncError( |
431 FROM_HERE, | 443 FROM_HERE, |
432 std::string("Error pushing sync remove to local settings: ") + | 444 std::string("Error pushing sync remove to local settings: ") + |
433 result.GetError(), | 445 result.GetError(), |
434 syncable::EXTENSION_SETTINGS); | 446 sync_type_); |
435 } | 447 } |
436 changes->AppendChange(key, old_value, NULL); | 448 changes->AppendChange(key, old_value, NULL); |
437 return SyncError(); | 449 return SyncError(); |
438 } | 450 } |
OLD | NEW |