| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/prefs/pref_model_associator.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sync/engine/syncapi.h" |
| 14 #include "chrome/browser/sync/glue/generic_change_processor.h" |
| 15 #include "chrome/browser/sync/profile_sync_service.h" |
| 16 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
| 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/browser/browser_thread.h" |
| 19 #include "content/common/json_value_serializer.h" |
| 20 #include "content/common/notification_service.h" |
| 21 |
| 22 using syncable::PREFERENCES; |
| 23 |
| 24 PrefModelAssociator::PrefModelAssociator() |
| 25 : pref_service_(NULL), |
| 26 sync_service_(NULL), |
| 27 models_associated_(false), |
| 28 processing_syncer_changes_(false), |
| 29 change_processor_(NULL) { |
| 30 } |
| 31 |
| 32 PrefModelAssociator::PrefModelAssociator( |
| 33 PrefService* pref_service) |
| 34 : pref_service_(pref_service), |
| 35 sync_service_(NULL), |
| 36 models_associated_(false), |
| 37 processing_syncer_changes_(false), |
| 38 change_processor_(NULL) { |
| 39 DCHECK(CalledOnValidThread()); |
| 40 } |
| 41 |
| 42 PrefModelAssociator::~PrefModelAssociator() { |
| 43 DCHECK(CalledOnValidThread()); |
| 44 change_processor_ = NULL; |
| 45 sync_service_ = NULL; |
| 46 pref_service_ = NULL; |
| 47 } |
| 48 |
| 49 bool PrefModelAssociator::InitPrefNodeAndAssociate( |
| 50 sync_api::WriteTransaction* trans, |
| 51 const sync_api::BaseNode& root, |
| 52 const PrefService::Preference* pref) { |
| 53 DCHECK(pref); |
| 54 |
| 55 base::JSONReader reader; |
| 56 std::string tag = pref->name(); |
| 57 sync_api::WriteNode node(trans); |
| 58 if (node.InitByClientTagLookup(PREFERENCES, tag)) { |
| 59 // The server has a value for the preference. |
| 60 const sync_pb::PreferenceSpecifics& preference( |
| 61 node.GetPreferenceSpecifics()); |
| 62 DCHECK_EQ(tag, preference.name()); |
| 63 |
| 64 if (!pref->IsUserModifiable()) { |
| 65 Associate(pref, node.GetId()); |
| 66 return true; |
| 67 } |
| 68 |
| 69 scoped_ptr<Value> value( |
| 70 reader.JsonToValue(preference.value(), false, false)); |
| 71 std::string pref_name = preference.name(); |
| 72 if (!value.get()) { |
| 73 LOG(ERROR) << "Failed to deserialize preference value: " |
| 74 << reader.GetErrorMessage(); |
| 75 return false; |
| 76 } |
| 77 |
| 78 // Merge the server value of this preference with the local value. |
| 79 scoped_ptr<Value> new_value(MergePreference(*pref, *value)); |
| 80 |
| 81 // Update the local preference based on what we got from the |
| 82 // sync server. |
| 83 if (new_value->IsType(Value::TYPE_NULL)) { |
| 84 pref_service_->ClearPref(pref_name.c_str()); |
| 85 } else if (!new_value->IsType(pref->GetType())) { |
| 86 LOG(WARNING) << "Synced value for " << preference.name() |
| 87 << " is of type " << new_value->GetType() |
| 88 << " which doesn't match pref type " << pref->GetType(); |
| 89 } else if (!pref->GetValue()->Equals(new_value.get())) { |
| 90 pref_service_->Set(pref_name.c_str(), *new_value); |
| 91 } |
| 92 |
| 93 SendUpdateNotificationsIfNecessary(pref_name); |
| 94 |
| 95 // If the merge resulted in an updated value, write it back to |
| 96 // the sync node. |
| 97 if (!value->Equals(new_value.get()) && |
| 98 !WritePreferenceToNode(pref->name(), *new_value, &node)) { |
| 99 return false; |
| 100 } |
| 101 Associate(pref, node.GetId()); |
| 102 } else if (pref->IsUserControlled()) { |
| 103 // The server doesn't have a value, but we have a user-controlled value, |
| 104 // so we push it to the server. |
| 105 sync_api::WriteNode write_node(trans); |
| 106 if (!write_node.InitUniqueByCreation(PREFERENCES, root, tag)) { |
| 107 LOG(ERROR) << "Failed to create preference sync node."; |
| 108 return false; |
| 109 } |
| 110 |
| 111 // Update the sync node with the local value for this preference. |
| 112 if (!WritePreferenceToNode(pref->name(), *pref->GetValue(), &write_node)) |
| 113 return false; |
| 114 |
| 115 Associate(pref, write_node.GetId()); |
| 116 } else { |
| 117 // This preference is handled by policy, not the user, and therefore |
| 118 // we do not associate it. |
| 119 } |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 bool PrefModelAssociator::AssociateModels() { |
| 125 DCHECK(CalledOnValidThread()); |
| 126 |
| 127 int64 root_id; |
| 128 if (!GetSyncIdForTaggedNode(syncable::ModelTypeToRootTag(PREFERENCES), |
| 129 &root_id)) { |
| 130 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 131 << "might be running against an out-of-date server."; |
| 132 return false; |
| 133 } |
| 134 |
| 135 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
| 136 sync_api::ReadNode root(&trans); |
| 137 if (!root.InitByIdLookup(root_id)) { |
| 138 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 139 << "might be running against an out-of-date server."; |
| 140 return false; |
| 141 } |
| 142 |
| 143 for (std::set<std::string>::iterator it = synced_preferences_.begin(); |
| 144 it != synced_preferences_.end(); ++it) { |
| 145 std::string name = *it; |
| 146 const PrefService::Preference* pref = |
| 147 pref_service_->FindPreference(name.c_str()); |
| 148 VLOG(1) << "Associating preference " << name; |
| 149 DCHECK(pref); |
| 150 if (!pref->IsUserModifiable()) |
| 151 continue; // We don't sync preferences the user cannot change. |
| 152 InitPrefNodeAndAssociate(&trans, root, pref); |
| 153 } |
| 154 models_associated_ = true; |
| 155 return true; |
| 156 } |
| 157 |
| 158 bool PrefModelAssociator::DisassociateModels() { |
| 159 id_map_.clear(); |
| 160 id_map_inverse_.clear(); |
| 161 models_associated_ = false; |
| 162 return true; |
| 163 } |
| 164 |
| 165 bool PrefModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
| 166 DCHECK(has_nodes); |
| 167 *has_nodes = false; |
| 168 int64 preferences_sync_id; |
| 169 if (!GetSyncIdForTaggedNode(syncable::ModelTypeToRootTag(PREFERENCES), |
| 170 &preferences_sync_id)) { |
| 171 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 172 << "might be running against an out-of-date server."; |
| 173 return false; |
| 174 } |
| 175 sync_api::ReadTransaction trans(sync_service_->GetUserShare()); |
| 176 |
| 177 sync_api::ReadNode preferences_node(&trans); |
| 178 if (!preferences_node.InitByIdLookup(preferences_sync_id)) { |
| 179 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 180 << "might be running against an out-of-date server."; |
| 181 return false; |
| 182 } |
| 183 |
| 184 // The sync model has user created nodes if the preferences folder has any |
| 185 // children. |
| 186 *has_nodes = sync_api::kInvalidId != preferences_node.GetFirstChildId(); |
| 187 return true; |
| 188 } |
| 189 |
| 190 int64 PrefModelAssociator::GetSyncIdFromChromeId( |
| 191 const std::string& preference_name) { |
| 192 PreferenceNameToSyncIdMap::const_iterator iter = |
| 193 id_map_.find(preference_name); |
| 194 return iter == id_map_.end() ? sync_api::kInvalidId : iter->second; |
| 195 } |
| 196 |
| 197 void PrefModelAssociator::Associate( |
| 198 const PrefService::Preference* preference, int64 sync_id) { |
| 199 DCHECK(CalledOnValidThread()); |
| 200 |
| 201 std::string name = preference->name(); |
| 202 DCHECK_NE(sync_api::kInvalidId, sync_id); |
| 203 DCHECK_EQ(0U, id_map_.count(name)); |
| 204 DCHECK_EQ(0U, id_map_inverse_.count(sync_id)); |
| 205 id_map_[name] = sync_id; |
| 206 id_map_inverse_[sync_id] = name; |
| 207 } |
| 208 |
| 209 void PrefModelAssociator::Disassociate(int64 sync_id) { |
| 210 DCHECK(CalledOnValidThread()); |
| 211 SyncIdToPreferenceNameMap::iterator iter = id_map_inverse_.find(sync_id); |
| 212 if (iter == id_map_inverse_.end()) |
| 213 return; |
| 214 id_map_.erase(iter->second); |
| 215 id_map_inverse_.erase(iter); |
| 216 } |
| 217 |
| 218 bool PrefModelAssociator::GetSyncIdForTaggedNode(const std::string& tag, |
| 219 int64* sync_id) { |
| 220 sync_api::ReadTransaction trans(sync_service_->GetUserShare()); |
| 221 sync_api::ReadNode sync_node(&trans); |
| 222 if (!sync_node.InitByTagLookup(tag.c_str())) |
| 223 return false; |
| 224 *sync_id = sync_node.GetId(); |
| 225 return true; |
| 226 } |
| 227 |
| 228 Value* PrefModelAssociator::MergePreference( |
| 229 const PrefService::Preference& local_pref, |
| 230 const Value& server_value) { |
| 231 const std::string& name(local_pref.name()); |
| 232 if (name == prefs::kURLsToRestoreOnStartup || |
| 233 name == prefs::kDesktopNotificationAllowedOrigins || |
| 234 name == prefs::kDesktopNotificationDeniedOrigins) { |
| 235 return MergeListValues(*local_pref.GetValue(), server_value); |
| 236 } |
| 237 |
| 238 if (name == prefs::kContentSettingsPatterns || |
| 239 name == prefs::kGeolocationContentSettings) { |
| 240 return MergeDictionaryValues(*local_pref.GetValue(), server_value); |
| 241 } |
| 242 |
| 243 // If this is not a specially handled preference, server wins. |
| 244 return server_value.DeepCopy(); |
| 245 } |
| 246 |
| 247 bool PrefModelAssociator::WritePreferenceToNode( |
| 248 const std::string& name, |
| 249 const Value& value, |
| 250 sync_api::WriteNode* node) { |
| 251 std::string serialized; |
| 252 JSONStringValueSerializer json(&serialized); |
| 253 if (!json.Serialize(value)) { |
| 254 LOG(ERROR) << "Failed to serialize preference value."; |
| 255 return false; |
| 256 } |
| 257 |
| 258 sync_pb::PreferenceSpecifics preference; |
| 259 preference.set_name(name); |
| 260 preference.set_value(serialized); |
| 261 node->SetPreferenceSpecifics(preference); |
| 262 // TODO(viettrungluu): eliminate conversion (it's temporary) |
| 263 node->SetTitle(UTF8ToWide(name)); |
| 264 return true; |
| 265 } |
| 266 |
| 267 Value* PrefModelAssociator::MergeListValues(const Value& from_value, |
| 268 const Value& to_value) { |
| 269 if (from_value.GetType() == Value::TYPE_NULL) |
| 270 return to_value.DeepCopy(); |
| 271 if (to_value.GetType() == Value::TYPE_NULL) |
| 272 return from_value.DeepCopy(); |
| 273 |
| 274 DCHECK(from_value.GetType() == Value::TYPE_LIST); |
| 275 DCHECK(to_value.GetType() == Value::TYPE_LIST); |
| 276 const ListValue& from_list_value = static_cast<const ListValue&>(from_value); |
| 277 const ListValue& to_list_value = static_cast<const ListValue&>(to_value); |
| 278 ListValue* result = to_list_value.DeepCopy(); |
| 279 |
| 280 for (ListValue::const_iterator i = from_list_value.begin(); |
| 281 i != from_list_value.end(); ++i) { |
| 282 Value* value = (*i)->DeepCopy(); |
| 283 result->AppendIfNotPresent(value); |
| 284 } |
| 285 return result; |
| 286 } |
| 287 |
| 288 Value* PrefModelAssociator::MergeDictionaryValues( |
| 289 const Value& from_value, |
| 290 const Value& to_value) { |
| 291 if (from_value.GetType() == Value::TYPE_NULL) |
| 292 return to_value.DeepCopy(); |
| 293 if (to_value.GetType() == Value::TYPE_NULL) |
| 294 return from_value.DeepCopy(); |
| 295 |
| 296 DCHECK_EQ(from_value.GetType(), Value::TYPE_DICTIONARY); |
| 297 DCHECK_EQ(to_value.GetType(), Value::TYPE_DICTIONARY); |
| 298 const DictionaryValue& from_dict_value = |
| 299 static_cast<const DictionaryValue&>(from_value); |
| 300 const DictionaryValue& to_dict_value = |
| 301 static_cast<const DictionaryValue&>(to_value); |
| 302 DictionaryValue* result = to_dict_value.DeepCopy(); |
| 303 |
| 304 for (DictionaryValue::key_iterator key = from_dict_value.begin_keys(); |
| 305 key != from_dict_value.end_keys(); ++key) { |
| 306 Value* from_value; |
| 307 bool success = from_dict_value.GetWithoutPathExpansion(*key, &from_value); |
| 308 DCHECK(success); |
| 309 |
| 310 Value* to_key_value; |
| 311 if (result->GetWithoutPathExpansion(*key, &to_key_value)) { |
| 312 if (to_key_value->GetType() == Value::TYPE_DICTIONARY) { |
| 313 Value* merged_value = MergeDictionaryValues(*from_value, *to_key_value); |
| 314 result->SetWithoutPathExpansion(*key, merged_value); |
| 315 } |
| 316 // Note that for all other types we want to preserve the "to" |
| 317 // values so we do nothing here. |
| 318 } else { |
| 319 result->SetWithoutPathExpansion(*key, from_value->DeepCopy()); |
| 320 } |
| 321 } |
| 322 return result; |
| 323 } |
| 324 |
| 325 void PrefModelAssociator::SendUpdateNotificationsIfNecessary( |
| 326 const std::string& pref_name) { |
| 327 // The bookmark bar visibility preference requires a special |
| 328 // notification to update the UI. |
| 329 if (0 == pref_name.compare(prefs::kShowBookmarkBar)) { |
| 330 NotificationService::current()->Notify( |
| 331 NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, |
| 332 Source<PrefModelAssociator>(this), |
| 333 NotificationService::NoDetails()); |
| 334 } |
| 335 } |
| 336 |
| 337 // Not implemented. |
| 338 void PrefModelAssociator::AbortAssociation() {} |
| 339 |
| 340 bool PrefModelAssociator::CryptoReadyIfNecessary() { |
| 341 // We only access the cryptographer while holding a transaction. |
| 342 sync_api::ReadTransaction trans(sync_service_->GetUserShare()); |
| 343 syncable::ModelTypeSet encrypted_types; |
| 344 sync_service_->GetEncryptedDataTypes(&encrypted_types); |
| 345 return encrypted_types.count(PREFERENCES) == 0 || |
| 346 sync_service_->IsCryptographerReady(&trans); |
| 347 } |
| 348 |
| 349 void PrefModelAssociator::SetupSync( |
| 350 ProfileSyncService* sync_service, |
| 351 browser_sync::GenericChangeProcessor* change_processor) { |
| 352 sync_service_ = sync_service; |
| 353 change_processor_ = change_processor; |
| 354 } |
| 355 |
| 356 void PrefModelAssociator::ApplyChangesFromSync( |
| 357 const sync_api::BaseTransaction* trans, |
| 358 const sync_api::SyncManager::ChangeRecord* changes, |
| 359 int change_count) { |
| 360 if (!models_associated_) |
| 361 return; |
| 362 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 363 for (int i = 0; i < change_count; ++i) { |
| 364 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == |
| 365 changes[i].action) { |
| 366 // We never delete preferences. |
| 367 NOTREACHED(); |
| 368 } |
| 369 |
| 370 sync_api::ReadNode node(trans); |
| 371 if (!node.InitByIdLookup(changes[i].id)) { |
| 372 LOG(ERROR) << "Preference node lookup failed."; |
| 373 return; |
| 374 } |
| 375 DCHECK(PREFERENCES == node.GetModelType()); |
| 376 std::string name; |
| 377 sync_pb::PreferenceSpecifics pref_specifics = |
| 378 node.GetPreferenceSpecifics(); |
| 379 scoped_ptr<Value> value(ReadPreferenceSpecifics(pref_specifics, |
| 380 &name)); |
| 381 // Skip values we can't deserialize. |
| 382 if (!value.get()) |
| 383 continue; |
| 384 |
| 385 // It is possible that we may receive a change to a preference we do not |
| 386 // want to sync. For example if the user is syncing a Mac client and a |
| 387 // Windows client, the Windows client does not support |
| 388 // kConfirmToQuitEnabled. Ignore updates from these preferences. |
| 389 const char* pref_name = name.c_str(); |
| 390 if (!IsPrefRegistered(pref_name)) |
| 391 continue; |
| 392 |
| 393 const PrefService::Preference* pref = |
| 394 pref_service_->FindPreference(pref_name); |
| 395 DCHECK(pref); |
| 396 if (!pref->IsUserModifiable()) { |
| 397 continue; |
| 398 } |
| 399 |
| 400 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == |
| 401 changes[i].action) { |
| 402 pref_service_->ClearPref(pref_name); |
| 403 } else { |
| 404 pref_service_->Set(pref_name, *value); |
| 405 |
| 406 // If this is a newly added node, associate. |
| 407 if (sync_api::SyncManager::ChangeRecord::ACTION_ADD == |
| 408 changes[i].action) { |
| 409 Associate(pref, changes[i].id); |
| 410 } |
| 411 |
| 412 SendUpdateNotificationsIfNecessary(name); |
| 413 } |
| 414 } |
| 415 } |
| 416 |
| 417 Value* PrefModelAssociator::ReadPreferenceSpecifics( |
| 418 const sync_pb::PreferenceSpecifics& preference, |
| 419 std::string* name) { |
| 420 base::JSONReader reader; |
| 421 scoped_ptr<Value> value(reader.JsonToValue(preference.value(), false, false)); |
| 422 if (!value.get()) { |
| 423 std::string err = "Failed to deserialize preference value: " + |
| 424 reader.GetErrorMessage(); |
| 425 LOG(ERROR) << err; |
| 426 return NULL; |
| 427 } |
| 428 *name = preference.name(); |
| 429 return value.release(); |
| 430 } |
| 431 |
| 432 |
| 433 std::set<std::string> PrefModelAssociator::synced_preferences() const { |
| 434 return synced_preferences_; |
| 435 } |
| 436 |
| 437 void PrefModelAssociator::RegisterPref(const char* name) { |
| 438 DCHECK(!models_associated_ && synced_preferences_.count(name) == 0); |
| 439 synced_preferences_.insert(name); |
| 440 } |
| 441 |
| 442 bool PrefModelAssociator::IsPrefRegistered(const char* name) { |
| 443 return synced_preferences_.count(name) > 0; |
| 444 } |
| 445 |
| 446 void PrefModelAssociator::ProcessPrefChange(const std::string& name) { |
| 447 if (processing_syncer_changes_) |
| 448 return; // These are changes originating from us, ignore. |
| 449 |
| 450 // We only process changes if we've already associated models. |
| 451 if (!sync_service_ || !models_associated_) |
| 452 return; |
| 453 |
| 454 const PrefService::Preference* preference = |
| 455 pref_service_->FindPreference(name.c_str()); |
| 456 if (!IsPrefRegistered(name.c_str())) |
| 457 return; // We are not syncing this preference. |
| 458 |
| 459 // The preference does not have a node. This can happen if the preference |
| 460 // held the default value at association time. Create one and associate. |
| 461 int64 root_id; |
| 462 if (!GetSyncIdForTaggedNode(syncable::ModelTypeToRootTag(PREFERENCES), |
| 463 &root_id)) { |
| 464 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 465 << "might be running against an out-of-date server."; |
| 466 return; |
| 467 } |
| 468 |
| 469 int64 sync_id = GetSyncIdFromChromeId(name); |
| 470 if (!preference->IsUserModifiable()) { |
| 471 // If the preference is not currently user modifiable, disassociate, so that |
| 472 // if it becomes user modifiable me pick up the server value. |
| 473 Disassociate(sync_id); |
| 474 return; |
| 475 } |
| 476 |
| 477 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
| 478 sync_api::WriteTransaction trans(sync_service_->GetUserShare()); |
| 479 |
| 480 // Since we don't create sync nodes for preferences that are not under control |
| 481 // of the user or still have their default value, this changed preference may |
| 482 // not have a sync node yet. If so, we create a node. Similarly, a preference |
| 483 // may become user-modifiable (e.g. due to laxer policy configuration), in |
| 484 // which case we also need to create a sync node and associate it. |
| 485 if (sync_id == sync_api::kInvalidId) { |
| 486 sync_api::ReadNode root(&trans); |
| 487 if (!root.InitByIdLookup(root_id)) { |
| 488 LOG(ERROR) << "Server did not create the top-level preferences node. We " |
| 489 << "might be running against an out-of-date server."; |
| 490 return; |
| 491 } |
| 492 InitPrefNodeAndAssociate(&trans, root, preference); |
| 493 } else { |
| 494 sync_api::WriteNode node(&trans); |
| 495 if (!node.InitByIdLookup(sync_id)) { |
| 496 LOG(ERROR) << "Preference node lookup failed."; |
| 497 return; |
| 498 } |
| 499 |
| 500 if (!WritePreferenceToNode(name, *preference->GetValue(), &node)) { |
| 501 LOG(ERROR) << "Failed to update preference node."; |
| 502 return; |
| 503 } |
| 504 } |
| 505 } |
| OLD | NEW |