| 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/prefs/pref_model_associator.h" | 5 #include "chrome/browser/prefs/pref_model_associator.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/sync/api/sync_change.h" | 14 #include "chrome/browser/sync/api/sync_change.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "sync/protocol/preference_specifics.pb.h" | 17 #include "sync/protocol/preference_specifics.pb.h" |
| 18 #include "sync/protocol/sync.pb.h" | 18 #include "sync/protocol/sync.pb.h" |
| 19 | 19 |
| 20 using syncable::PREFERENCES; | 20 using syncable::PREFERENCES; |
| 21 | 21 |
| 22 PrefModelAssociator::PrefModelAssociator() | 22 PrefModelAssociator::PrefModelAssociator() |
| 23 : models_associated_(false), | 23 : models_associated_(false), |
| 24 processing_syncer_changes_(false), | 24 processing_syncer_changes_(false), |
| 25 pref_service_(NULL), | 25 pref_service_(NULL) { |
| 26 sync_processor_(NULL) { | |
| 27 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
| 28 } | 27 } |
| 29 | 28 |
| 30 PrefModelAssociator::~PrefModelAssociator() { | 29 PrefModelAssociator::~PrefModelAssociator() { |
| 31 DCHECK(CalledOnValidThread()); | 30 DCHECK(CalledOnValidThread()); |
| 32 sync_processor_ = NULL; | |
| 33 pref_service_ = NULL; | 31 pref_service_ = NULL; |
| 34 } | 32 } |
| 35 | 33 |
| 36 void PrefModelAssociator::InitPrefAndAssociate( | 34 void PrefModelAssociator::InitPrefAndAssociate( |
| 37 const SyncData& sync_pref, | 35 const SyncData& sync_pref, |
| 38 const std::string& pref_name, | 36 const std::string& pref_name, |
| 39 SyncChangeList* sync_changes) { | 37 SyncChangeList* sync_changes) { |
| 40 const PrefService::Preference* pref = | 38 const PrefService::Preference* pref = |
| 41 pref_service_->FindPreference(pref_name.c_str()); | 39 pref_service_->FindPreference(pref_name.c_str()); |
| 42 DCHECK(pref); | 40 DCHECK(pref); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 100 |
| 103 // Make sure we add it to our list of synced preferences so we know what | 101 // Make sure we add it to our list of synced preferences so we know what |
| 104 // the server is aware of. | 102 // the server is aware of. |
| 105 synced_preferences_.insert(pref_name); | 103 synced_preferences_.insert(pref_name); |
| 106 return; | 104 return; |
| 107 } | 105 } |
| 108 | 106 |
| 109 SyncError PrefModelAssociator::MergeDataAndStartSyncing( | 107 SyncError PrefModelAssociator::MergeDataAndStartSyncing( |
| 110 syncable::ModelType type, | 108 syncable::ModelType type, |
| 111 const SyncDataList& initial_sync_data, | 109 const SyncDataList& initial_sync_data, |
| 112 SyncChangeProcessor* sync_processor) { | 110 scoped_ptr<SyncChangeProcessor> sync_processor) { |
| 113 DCHECK_EQ(type, PREFERENCES); | 111 DCHECK_EQ(type, PREFERENCES); |
| 114 DCHECK(CalledOnValidThread()); | 112 DCHECK(CalledOnValidThread()); |
| 115 DCHECK(pref_service_); | 113 DCHECK(pref_service_); |
| 116 DCHECK(!sync_processor_); | 114 DCHECK(!sync_processor_.get()); |
| 117 sync_processor_ = sync_processor; | 115 DCHECK(sync_processor.get()); |
| 116 sync_processor_ = sync_processor.Pass(); |
| 118 | 117 |
| 119 SyncChangeList new_changes; | 118 SyncChangeList new_changes; |
| 120 std::set<std::string> remaining_preferences = registered_preferences_; | 119 std::set<std::string> remaining_preferences = registered_preferences_; |
| 121 | 120 |
| 122 // Go through and check for all preferences we care about that sync already | 121 // Go through and check for all preferences we care about that sync already |
| 123 // knows about. | 122 // knows about. |
| 124 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); | 123 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); |
| 125 sync_iter != initial_sync_data.end(); | 124 sync_iter != initial_sync_data.end(); |
| 126 ++sync_iter) { | 125 ++sync_iter) { |
| 127 DCHECK_EQ(PREFERENCES, sync_iter->GetDataType()); | 126 DCHECK_EQ(PREFERENCES, sync_iter->GetDataType()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 154 return error; | 153 return error; |
| 155 } | 154 } |
| 156 | 155 |
| 157 models_associated_ = true; | 156 models_associated_ = true; |
| 158 return SyncError(); | 157 return SyncError(); |
| 159 } | 158 } |
| 160 | 159 |
| 161 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { | 160 void PrefModelAssociator::StopSyncing(syncable::ModelType type) { |
| 162 DCHECK_EQ(type, PREFERENCES); | 161 DCHECK_EQ(type, PREFERENCES); |
| 163 models_associated_ = false; | 162 models_associated_ = false; |
| 164 sync_processor_ = NULL; | 163 sync_processor_.reset(); |
| 165 } | 164 } |
| 166 | 165 |
| 167 Value* PrefModelAssociator::MergePreference( | 166 Value* PrefModelAssociator::MergePreference( |
| 168 const PrefService::Preference& local_pref, | 167 const PrefService::Preference& local_pref, |
| 169 const Value& server_value) { | 168 const Value& server_value) { |
| 170 const std::string& name(local_pref.name()); | 169 const std::string& name(local_pref.name()); |
| 171 if (name == prefs::kURLsToRestoreOnStartup || | 170 if (name == prefs::kURLsToRestoreOnStartup || |
| 172 name == prefs::kDesktopNotificationAllowedOrigins || | 171 name == prefs::kDesktopNotificationAllowedOrigins || |
| 173 name == prefs::kDesktopNotificationDeniedOrigins) { | 172 name == prefs::kDesktopNotificationDeniedOrigins) { |
| 174 return MergeListValues(*local_pref.GetValue(), server_value); | 173 return MergeListValues(*local_pref.GetValue(), server_value); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 SyncData sync_data; | 417 SyncData sync_data; |
| 419 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 418 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 420 LOG(ERROR) << "Failed to update preference."; | 419 LOG(ERROR) << "Failed to update preference."; |
| 421 return; | 420 return; |
| 422 } | 421 } |
| 423 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 422 changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 424 } | 423 } |
| 425 | 424 |
| 426 SyncError error = | 425 SyncError error = |
| 427 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 426 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 428 if (error.IsSet()) | |
| 429 StopSyncing(PREFERENCES); | |
| 430 } | 427 } |
| 431 | 428 |
| 432 void PrefModelAssociator::SetPrefService(PrefService* pref_service) { | 429 void PrefModelAssociator::SetPrefService(PrefService* pref_service) { |
| 433 DCHECK(pref_service_ == NULL); | 430 DCHECK(pref_service_ == NULL); |
| 434 pref_service_ = pref_service; | 431 pref_service_ = pref_service; |
| 435 } | 432 } |
| OLD | NEW |