| 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 #ifndef CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/sync/syncable_service.h" | 17 #include "chrome/browser/sync/api/syncable_service.h" |
| 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" | 18 #include "chrome/browser/sync/api/sync_data.h" |
| 19 | 19 |
| 20 class Profile; | 20 namespace sync_pb { |
| 21 class ProfileSyncService; | 21 class PreferenceSpecifics; |
| 22 } |
| 23 |
| 22 class Value; | 24 class Value; |
| 23 | 25 |
| 24 namespace sync_api { | 26 // Contains all preference sync related logic. |
| 25 class WriteNode; | 27 // TODO(sync): Merge this into PrefService once we separate the profile |
| 26 class WriteTransaction; | 28 // PrefService from the local state PrefService. |
| 27 } | |
| 28 | |
| 29 namespace browser_sync { | |
| 30 class ChangeProcessor; | |
| 31 class GenericChangeProcessor; | |
| 32 } | |
| 33 | |
| 34 // Contains all model association related logic: | |
| 35 // * Algorithm to associate preferences model and sync model. | |
| 36 // TODO(sync): Rewrite to use change processor instead of transactions. | |
| 37 // TODO(sync): Merge this into PrefService. We don't actually need the id_map | |
| 38 // mapping since the sync node tags are the same as the pref names. | |
| 39 class PrefModelAssociator | 29 class PrefModelAssociator |
| 40 : public SyncableService, | 30 : public SyncableService, |
| 41 public base::NonThreadSafe { | 31 public base::NonThreadSafe { |
| 42 public: | 32 public: |
| 43 explicit PrefModelAssociator(PrefService* pref_service); | 33 explicit PrefModelAssociator(PrefService* pref_service); |
| 44 virtual ~PrefModelAssociator(); | 34 virtual ~PrefModelAssociator(); |
| 45 | 35 |
| 46 // SyncableService implementation. | 36 // SyncableService implementation. |
| 47 virtual bool AssociateModels() OVERRIDE; | 37 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
| 48 virtual bool DisassociateModels() OVERRIDE; | 38 virtual void ProcessSyncChanges(const SyncChangeList& change_list) OVERRIDE; |
| 49 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes) OVERRIDE; | 39 virtual bool MergeDataAndStartSyncing( |
| 50 virtual void AbortAssociation() OVERRIDE; // Not implemented. | 40 syncable::ModelType type, |
| 51 virtual bool CryptoReadyIfNecessary() OVERRIDE; | 41 const SyncDataList& initial_sync_data, |
| 52 virtual void ApplyChangesFromSync( | 42 SyncChangeProcessor* sync_processor) OVERRIDE; |
| 53 const sync_api::BaseTransaction* trans, | 43 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 54 const sync_api::SyncManager::ChangeRecord* changes, | |
| 55 int change_count) OVERRIDE; | |
| 56 virtual void SetupSync( | |
| 57 ProfileSyncService* sync_service, | |
| 58 browser_sync::GenericChangeProcessor* change_processor) OVERRIDE; | |
| 59 | 44 |
| 60 // Returns the list of preference names that should be monitored for changes. | 45 // Returns the list of preference names that are registered as syncable, and |
| 61 // Only preferences that are registered will be in this list. | 46 // hence should be monitored for changes. |
| 47 std::set<std::string> registered_preferences() const; |
| 48 |
| 49 // Returns the list of preferences actually being synced (which is a subset |
| 50 // of those registered as syncable). |
| 62 std::set<std::string> synced_preferences() const; | 51 std::set<std::string> synced_preferences() const; |
| 63 | 52 |
| 64 // Register a preference with the specified name for syncing. We do not care | 53 // Register a preference with the specified name for syncing. We do not care |
| 65 // about the type at registration time, but when changes arrive from the | 54 // about the type at registration time, but when changes arrive from the |
| 66 // syncer, we check if they can be applied and if not drop them. | 55 // syncer, we check if they can be applied and if not drop them. |
| 56 // Note: This should only be called at profile startup time (before sync |
| 57 // begins). |
| 67 virtual void RegisterPref(const char* name); | 58 virtual void RegisterPref(const char* name); |
| 68 | 59 |
| 69 // Returns true if the specified preference is registered for syncing. | 60 // Returns true if the specified preference is registered for syncing. |
| 70 virtual bool IsPrefRegistered(const char* name); | 61 virtual bool IsPrefRegistered(const char* name); |
| 71 | 62 |
| 72 // Process a local preference change. | 63 // Process a local preference change. This can trigger new SyncChanges being |
| 64 // sent to the syncer. |
| 73 virtual void ProcessPrefChange(const std::string& name); | 65 virtual void ProcessPrefChange(const std::string& name); |
| 74 | 66 |
| 75 // Merges the value of local_pref into the supplied server_value and returns | 67 // Merges the value of local_pref into the supplied server_value and returns |
| 76 // the result (caller takes ownership). If there is a conflict, the server | 68 // the result (caller takes ownership). If there is a conflict, the server |
| 77 // value always takes precedence. Note that only certain preferences will | 69 // value always takes precedence. Note that only certain preferences will |
| 78 // actually be merged, all others will return a copy of the server value. See | 70 // actually be merged, all others will return a copy of the server value. See |
| 79 // the method's implementation for details. | 71 // the method's implementation for details. |
| 80 static Value* MergePreference(const PrefService::Preference& local_pref, | 72 static Value* MergePreference(const PrefService::Preference& local_pref, |
| 81 const Value& server_value); | 73 const Value& server_value); |
| 82 | 74 |
| 83 // Writes the value of pref into the specified node. Returns true | 75 // Fills |sync_data| with a sync representation of the preference data |
| 84 // upon success. | 76 // provided. |
| 85 static bool WritePreferenceToNode(const std::string& name, | 77 static bool CreatePrefSyncData(const std::string& name, |
| 86 const Value& value, | 78 const Value& value, |
| 87 sync_api::WriteNode* node); | 79 SyncData* sync_data); |
| 88 | 80 |
| 89 // Extract preference value and name from sync specifics. | 81 // Extract preference value and name from sync specifics. |
| 90 Value* ReadPreferenceSpecifics( | 82 Value* ReadPreferenceSpecifics( |
| 91 const sync_pb::PreferenceSpecifics& specifics, | 83 const sync_pb::PreferenceSpecifics& specifics, |
| 92 std::string* name); | 84 std::string* name); |
| 93 | 85 |
| 94 // Returns the sync id for the given preference name, or sync_api::kInvalidId | |
| 95 // if the preference name is not associated to any sync id. | |
| 96 int64 GetSyncIdFromChromeId(const std::string& node_id); | |
| 97 protected: | 86 protected: |
| 98 friend class ProfileSyncServicePreferenceTest; | 87 friend class ProfileSyncServicePreferenceTest; |
| 99 | 88 |
| 89 typedef std::map<std::string, SyncData> SyncDataMap; |
| 90 |
| 100 // For testing. | 91 // For testing. |
| 101 PrefModelAssociator(); | 92 PrefModelAssociator(); |
| 102 | 93 |
| 103 // Create an association for a given preference. A sync node is created if | 94 // Create an association for a given preference. If |sync_pref| is valid, |
| 104 // necessary and the value is read from or written to the node as appropriate. | 95 // signifying that sync has data for this preference, we reconcile their data |
| 105 bool InitPrefNodeAndAssociate(sync_api::WriteTransaction* trans, | 96 // with ours and append a new UPDATE SyncChange to |sync_changes|. If |
| 106 const sync_api::BaseNode& root, | 97 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with |
| 107 const PrefService::Preference* pref); | 98 // the current preference data. |
| 99 // Note: We do not modify the sync data for preferences that are either |
| 100 // controlled by policy (are not user modifiable) or have their default value |
| 101 // (are not user controlled). |
| 102 void InitPrefAndAssociate(const SyncData& sync_pref, |
| 103 const std::string& pref_name, |
| 104 SyncChangeList* sync_changes); |
| 108 | 105 |
| 109 // Associates the given preference name with the given sync id. | 106 // Perform any additional local operations that need to happen after a |
| 110 void Associate(const PrefService::Preference* node, int64 sync_id); | 107 // preference has been updated. |
| 111 | |
| 112 // Remove the association that corresponds to the given sync id. | |
| 113 void Disassociate(int64 sync_id); | |
| 114 | |
| 115 // Returns whether a node with the given permanent tag was found and update | |
| 116 // |sync_id| with that node's id. | |
| 117 bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); | |
| 118 | |
| 119 // Perform any additional operations that need to happen after a preference | |
| 120 // has been updated. | |
| 121 void SendUpdateNotificationsIfNecessary(const std::string& pref_name); | 108 void SendUpdateNotificationsIfNecessary(const std::string& pref_name); |
| 122 | 109 |
| 123 typedef std::map<std::string, int64> PreferenceNameToSyncIdMap; | |
| 124 typedef std::map<int64, std::string> SyncIdToPreferenceNameMap; | |
| 125 | |
| 126 static Value* MergeListValues(const Value& from_value, const Value& to_value); | 110 static Value* MergeListValues(const Value& from_value, const Value& to_value); |
| 127 static Value* MergeDictionaryValues(const Value& from_value, | 111 static Value* MergeDictionaryValues(const Value& from_value, |
| 128 const Value& to_value); | 112 const Value& to_value); |
| 129 | 113 |
| 130 PrefService* pref_service_; | |
| 131 ProfileSyncService* sync_service_; | |
| 132 | |
| 133 // Do we have an active association between the preferences and sync models? | 114 // Do we have an active association between the preferences and sync models? |
| 134 // Set by AssociateModels, reset by DisassociateModels. | 115 // Set when start syncing, reset in StopSyncing. While this is not set, we |
| 116 // ignore any local preference changes (when we start syncing we will look |
| 117 // up the most recent values anyways). |
| 135 bool models_associated_; | 118 bool models_associated_; |
| 136 | 119 |
| 137 // Whether we're currently processing changes from the syncer. While this is | 120 // Whether we're currently processing changes from the syncer. While this is |
| 138 // true, we ignore any pref changes, since we triggered them. | 121 // true, we ignore any local preference changes, since we triggered them. |
| 139 bool processing_syncer_changes_; | 122 bool processing_syncer_changes_; |
| 140 | 123 |
| 141 PreferenceNameToSyncIdMap id_map_; | 124 // A set of preference names. |
| 142 SyncIdToPreferenceNameMap id_map_inverse_; | 125 typedef std::set<std::string> PreferenceSet; |
| 143 std::set<std::string> synced_preferences_; | |
| 144 | 126 |
| 145 // TODO(zea): Get rid of this and use one owned by the PSS. | 127 // All preferences that have registered as being syncable with this profile. |
| 146 // We create, but don't own this (will be destroyed by data type controller). | 128 PreferenceSet registered_preferences_; |
| 147 browser_sync::GenericChangeProcessor* change_processor_; | 129 |
| 130 // The preferences we are currently actually syncing (i.e. those the server |
| 131 // is aware of). This is a subset of |registered_preferences_|, but excludes |
| 132 // those with default values or not modifiable by the user (for example due |
| 133 // to being controlled by policy) |
| 134 PreferenceSet synced_preferences_; |
| 135 |
| 136 // We keep track of the most recent sync data we've received those |
| 137 // preferences registered as syncable but not in our synced_preferences_ list. |
| 138 // These are used if at a later time the preference in question should be |
| 139 // synced (for example the pref policy changes), and we need to get the |
| 140 // most recent sync data. |
| 141 // TODO(zea): See if we can get rid of the difference between |
| 142 // synced_preferences_ and registered_preferences_ by always updating the |
| 143 // local user pref store with pref data and letting the PrefStoreKeeper |
| 144 // handle ensuring the appropriate policy value is used. |
| 145 SyncDataMap untracked_pref_sync_data_; |
| 146 |
| 147 // The PrefService we are syncing to. |
| 148 PrefService* pref_service_; |
| 149 |
| 150 // Sync's SyncChange handler. We push all our changes through this. |
| 151 SyncChangeProcessor* sync_processor_; |
| 148 | 152 |
| 149 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | 153 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); |
| 150 }; | 154 }; |
| 151 | 155 |
| 152 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 156 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |