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/protocol/preference_specifics.pb.h" |
akalin
2011/05/20 00:19:39
forward-declare
Nicolas Zea
2011/05/20 02:00:28
Done.
| |
19 | 19 |
20 class Profile; | 20 class SyncData; |
21 class ProfileSyncService; | |
22 class Value; | 21 class Value; |
23 | 22 |
24 namespace sync_api { | 23 typedef std::map<std::string, SyncData> SyncDataMap; |
akalin
2011/05/20 00:19:39
is this only used by the MA? If so, make it a pri
Nicolas Zea
2011/05/20 02:00:28
Done.
| |
25 class WriteNode; | |
26 class WriteTransaction; | |
27 } | |
28 | 24 |
29 namespace browser_sync { | 25 // Contains all preference sync related logic. |
30 class ChangeProcessor; | 26 // TODO(sync): Merge this into PrefService once we separate the profile |
31 class GenericChangeProcessor; | 27 // PrefService from the local state PrefService. |
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 | 28 class PrefModelAssociator |
40 : public SyncableService, | 29 : public SyncableService, |
41 public base::NonThreadSafe { | 30 public base::NonThreadSafe { |
42 public: | 31 public: |
43 explicit PrefModelAssociator(PrefService* pref_service); | 32 explicit PrefModelAssociator(PrefService* pref_service); |
44 virtual ~PrefModelAssociator(); | 33 virtual ~PrefModelAssociator(); |
45 | 34 |
46 // SyncableService implementation. | 35 // SyncableService implementation. |
47 virtual bool AssociateModels() OVERRIDE; | 36 virtual bool GetAllSyncData(syncable::ModelType type, |
48 virtual bool DisassociateModels() OVERRIDE; | 37 SyncDataList* current_data) OVERRIDE; |
49 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes) OVERRIDE; | 38 virtual void ProcessSyncChanges(const SyncChangeList& change_list) OVERRIDE; |
50 virtual void AbortAssociation() OVERRIDE; // Not implemented. | 39 virtual bool MergeDataAndStartSyncing( |
51 virtual bool CryptoReadyIfNecessary() OVERRIDE; | 40 syncable::ModelType type, |
52 virtual void ApplyChangesFromSync( | 41 const SyncDataList& initial_sync_data, |
53 const sync_api::BaseTransaction* trans, | 42 SyncChangeProcessor* sync_processor) OVERRIDE; |
54 const sync_api::SyncManager::ChangeRecord* changes, | 43 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
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 |
100 // For testing. | 89 // For testing. |
101 PrefModelAssociator(); | 90 PrefModelAssociator(); |
102 | 91 |
103 // Create an association for a given preference. A sync node is created if | 92 // 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. | 93 // signifying that sync has data for this preference, we reconcile their data |
105 bool InitPrefNodeAndAssociate(sync_api::WriteTransaction* trans, | 94 // with ours and append a new UPDATE SyncChange to |sync_changes|. If |
106 const sync_api::BaseNode& root, | 95 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with |
107 const PrefService::Preference* pref); | 96 // the current preference data. |
97 // Note: We do not modify the sync data for preferences that are either | |
98 // controlled by policy (are not user modifiable) or have their default value | |
99 // (are not user controlled). | |
100 void InitPrefAndAssociate(const SyncData& sync_pref, | |
101 const std::string& pref_name, | |
102 SyncChangeList* sync_changes); | |
108 | 103 |
109 // Associates the given preference name with the given sync id. | 104 // Perform any additional local operations that need to happen after a |
110 void Associate(const PrefService::Preference* node, int64 sync_id); | 105 // 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); | 106 void SendUpdateNotificationsIfNecessary(const std::string& pref_name); |
122 | 107 |
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); | 108 static Value* MergeListValues(const Value& from_value, const Value& to_value); |
127 static Value* MergeDictionaryValues(const Value& from_value, | 109 static Value* MergeDictionaryValues(const Value& from_value, |
128 const Value& to_value); | 110 const Value& to_value); |
129 | 111 |
130 PrefService* pref_service_; | |
131 ProfileSyncService* sync_service_; | |
132 | |
133 // Do we have an active association between the preferences and sync models? | 112 // Do we have an active association between the preferences and sync models? |
134 // Set by AssociateModels, reset by DisassociateModels. | 113 // Set when start syncing, reset in StopSyncing. While this is not set, we |
114 // ignore any local preference changes (when we start syncing we will look | |
115 // up the most recent values anyways). | |
135 bool models_associated_; | 116 bool models_associated_; |
136 | 117 |
137 // Whether we're currently processing changes from the syncer. While this is | 118 // Whether we're currently processing changes from the syncer. While this is |
138 // true, we ignore any pref changes, since we triggered them. | 119 // true, we ignore any local preference changes, since we triggered them. |
139 bool processing_syncer_changes_; | 120 bool processing_syncer_changes_; |
140 | 121 |
141 PreferenceNameToSyncIdMap id_map_; | 122 // A set of preference names. |
142 SyncIdToPreferenceNameMap id_map_inverse_; | 123 typedef std::set<std::string> PreferenceSet; |
143 std::set<std::string> synced_preferences_; | |
144 | 124 |
145 // TODO(zea): Get rid of this and use one owned by the PSS. | 125 // 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). | 126 PreferenceSet registered_preferences_; |
147 browser_sync::GenericChangeProcessor* change_processor_; | 127 |
128 // The preferences we are currently actually syncing (i.e. those the server | |
129 // is aware of). This is a subset of |registered_preferences_|, but excludes | |
130 // those with default values or not modifiable by the user (for example due | |
131 // to being controlled by policy) | |
132 PreferenceSet synced_preferences_; | |
133 | |
134 // We keep track of the most recent sync data we've received those | |
135 // preferences registered as syncable but not in our synced_preferences_ list. | |
136 // These are used if at a later time the preference in question should be | |
137 // synced (for example the pref policy changes), and we need to get the | |
138 // most recent sync data. | |
139 // TODO(zea): See if we can get rid of the difference between | |
140 // synced_preferences_ and registered_preferences_ by always updating the | |
141 // local user pref store with pref data and letting the PrefStoreKeeper | |
142 // handle ensuring the appropriate policy value is used. | |
143 SyncDataMap untracked_pref_sync_data_; | |
144 | |
145 // The PrefService we are syncing to. | |
146 PrefService* pref_service_; | |
147 | |
148 // Sync's SyncChange handler. We push all our changes through this. | |
149 SyncChangeProcessor* sync_processor_; | |
148 | 150 |
149 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | 151 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); |
150 }; | 152 }; |
151 | 153 |
152 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 154 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
OLD | NEW |