Chromium Code Reviews| 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 #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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "chrome/browser/prefs/synced_pref_observer.h" | 17 #include "chrome/browser/prefs/synced_pref_observer.h" |
| 18 #include "sync/api/sync_data.h" | 18 #include "sync/api/sync_data.h" |
| 19 #include "sync/api/syncable_service.h" | 19 #include "sync/api/syncable_service.h" |
| 20 | 20 |
| 21 class PrefModelAssociatorClient; | |
| 21 class PrefRegistrySyncable; | 22 class PrefRegistrySyncable; |
| 22 class PrefServiceSyncable; | 23 class PrefServiceSyncable; |
| 23 | 24 |
| 24 namespace sync_pb { | 25 namespace sync_pb { |
| 25 class PreferenceSpecifics; | 26 class PreferenceSpecifics; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class Value; | 30 class Value; |
| 30 } | 31 } |
| 31 | 32 |
| 32 // Contains all preference sync related logic. | 33 // Contains all preference sync related logic. |
| 33 // TODO(sync): Merge this into PrefService once we separate the profile | 34 // TODO(sync): Merge this into PrefService once we separate the profile |
| 34 // PrefService from the local state PrefService. | 35 // PrefService from the local state PrefService. |
| 35 class PrefModelAssociator | 36 class PrefModelAssociator : public syncer::SyncableService, |
| 36 : public syncer::SyncableService, | 37 public base::NonThreadSafe { |
| 37 public base::NonThreadSafe { | |
| 38 public: | 38 public: |
| 39 explicit PrefModelAssociator(syncer::ModelType type); | 39 explicit PrefModelAssociator(syncer::ModelType type); |
| 40 ~PrefModelAssociator() override; | 40 ~PrefModelAssociator() override; |
| 41 | 41 |
| 42 // See description above field for details. | 42 // See description above field for details. |
| 43 bool models_associated() const { return models_associated_; } | 43 bool models_associated() const { return models_associated_; } |
| 44 | 44 |
| 45 // syncer::SyncableService implementation. | 45 // syncer::SyncableService implementation. |
| 46 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | 46 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; |
| 47 syncer::SyncError ProcessSyncChanges( | 47 syncer::SyncError ProcessSyncChanges( |
| 48 const tracked_objects::Location& from_here, | 48 const tracked_objects::Location& from_here, |
| 49 const syncer::SyncChangeList& change_list) override; | 49 const syncer::SyncChangeList& change_list) override; |
| 50 syncer::SyncMergeResult MergeDataAndStartSyncing( | 50 syncer::SyncMergeResult MergeDataAndStartSyncing( |
| 51 syncer::ModelType type, | 51 syncer::ModelType type, |
| 52 const syncer::SyncDataList& initial_sync_data, | 52 const syncer::SyncDataList& initial_sync_data, |
| 53 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 53 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 54 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) override; | 54 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) override; |
| 55 void StopSyncing(syncer::ModelType type) override; | 55 void StopSyncing(syncer::ModelType type) override; |
| 56 | 56 |
| 57 // Returns the list of preference names that are registered as syncable, and | 57 // Returns the list of preference names that are registered as syncable, and |
| 58 // hence should be monitored for changes. | 58 // hence should be monitored for changes. |
| 59 std::set<std::string> registered_preferences() const; | 59 std::set<std::string> registered_preferences() const; |
| 60 | 60 |
| 61 // Sets the PrefModelAssociatorClient that control special treatment of | |
| 62 // some preferences during synchronisation. Must be called before starting | |
| 63 // the synchronisation. | |
| 64 void set_pref_model_associator_client( | |
| 65 PrefModelAssociatorClient* pref_model_associator_client) { | |
| 66 pref_model_associator_client_ = pref_model_associator_client; | |
| 67 } | |
| 68 | |
| 61 // Register a preference with the specified name for syncing. We do not care | 69 // Register a preference with the specified name for syncing. We do not care |
| 62 // about the type at registration time, but when changes arrive from the | 70 // about the type at registration time, but when changes arrive from the |
| 63 // syncer, we check if they can be applied and if not drop them. | 71 // syncer, we check if they can be applied and if not drop them. |
| 64 // Note: This should only be called at profile startup time (before sync | 72 // Note: This should only be called at profile startup time (before sync |
| 65 // begins). | 73 // begins). |
| 66 virtual void RegisterPref(const char* name); | 74 virtual void RegisterPref(const char* name); |
| 67 | 75 |
| 68 // Returns true if the specified preference is registered for syncing. | 76 // Returns true if the specified preference is registered for syncing. |
| 69 virtual bool IsPrefRegistered(const char* name); | 77 virtual bool IsPrefRegistered(const char* name); |
| 70 | 78 |
| 71 // Process a local preference change. This can trigger new SyncChanges being | 79 // Process a local preference change. This can trigger new SyncChanges being |
| 72 // sent to the syncer. | 80 // sent to the syncer. |
| 73 virtual void ProcessPrefChange(const std::string& name); | 81 virtual void ProcessPrefChange(const std::string& name); |
| 74 | 82 |
| 75 void SetPrefService(PrefServiceSyncable* pref_service); | 83 void SetPrefService(PrefServiceSyncable* pref_service); |
| 76 | 84 |
| 77 // Merges the local_value into the supplied server_value and returns | 85 // Merges the local_value into the supplied server_value and returns |
| 78 // the result (caller takes ownership). If there is a conflict, the server | 86 // the result (caller takes ownership). If there is a conflict, the server |
| 79 // value always takes precedence. Note that only certain preferences will | 87 // value always takes precedence. Note that only certain preferences will |
| 80 // actually be merged, all others will return a copy of the server value. See | 88 // actually be merged, all others will return a copy of the server value. See |
| 81 // the method's implementation for details. | 89 // the method's implementation for details. |
| 82 static scoped_ptr<base::Value> MergePreference( | 90 static scoped_ptr<base::Value> MergePreference( |
| 83 const std::string& name, | 91 const std::string& name, |
| 84 const base::Value& local_value, | 92 const base::Value& local_value, |
| 85 const base::Value& server_value); | 93 const base::Value& server_value, |
| 94 PrefModelAssociatorClient* pref_model_associator_registry); | |
| 86 | 95 |
| 87 // Fills |sync_data| with a sync representation of the preference data | 96 // Fills |sync_data| with a sync representation of the preference data |
| 88 // provided. | 97 // provided. |
| 89 bool CreatePrefSyncData(const std::string& name, | 98 bool CreatePrefSyncData(const std::string& name, |
| 90 const base::Value& value, | 99 const base::Value& value, |
| 91 syncer::SyncData* sync_data) const; | 100 syncer::SyncData* sync_data) const; |
| 92 | 101 |
| 93 // Extract preference value from sync specifics. | 102 // Extract preference value from sync specifics. |
| 94 base::Value* ReadPreferenceSpecifics( | 103 base::Value* ReadPreferenceSpecifics( |
| 95 const sync_pb::PreferenceSpecifics& specifics); | 104 const sync_pb::PreferenceSpecifics& specifics); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 116 // with ours and append a new UPDATE SyncChange to |sync_changes|. If | 125 // with ours and append a new UPDATE SyncChange to |sync_changes|. If |
| 117 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with | 126 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with |
| 118 // the current preference data. | 127 // the current preference data. |
| 119 // Note: We do not modify the sync data for preferences that are either | 128 // Note: We do not modify the sync data for preferences that are either |
| 120 // controlled by policy (are not user modifiable) or have their default value | 129 // controlled by policy (are not user modifiable) or have their default value |
| 121 // (are not user controlled). | 130 // (are not user controlled). |
| 122 void InitPrefAndAssociate(const syncer::SyncData& sync_pref, | 131 void InitPrefAndAssociate(const syncer::SyncData& sync_pref, |
| 123 const std::string& pref_name, | 132 const std::string& pref_name, |
| 124 syncer::SyncChangeList* sync_changes); | 133 syncer::SyncChangeList* sync_changes); |
| 125 | 134 |
| 126 static base::Value* MergeListValues( | 135 static base::Value* MergeListValues(const base::Value& from_value, |
| 127 const base::Value& from_value, const base::Value& to_value); | 136 const base::Value& to_value); |
| 128 static base::Value* MergeDictionaryValues(const base::Value& from_value, | 137 static base::Value* MergeDictionaryValues(const base::Value& from_value, |
| 129 const base::Value& to_value); | 138 const base::Value& to_value); |
| 130 | 139 |
| 131 // Do we have an active association between the preferences and sync models? | 140 // Do we have an active association between the preferences and sync models? |
| 132 // Set when start syncing, reset in StopSyncing. While this is not set, we | 141 // Set when start syncing, reset in StopSyncing. While this is not set, we |
| 133 // ignore any local preference changes (when we start syncing we will look | 142 // ignore any local preference changes (when we start syncing we will look |
| 134 // up the most recent values anyways). | 143 // up the most recent values anyways). |
| 135 bool models_associated_; | 144 bool models_associated_; |
| 136 | 145 |
| 137 // Whether we're currently processing changes from the syncer. While this is | 146 // Whether we're currently processing changes from the syncer. While this is |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 // Map prefs to lists of observers. Observers will receive notification when | 179 // Map prefs to lists of observers. Observers will receive notification when |
| 171 // a pref changes, including the detail of whether or not the change came | 180 // a pref changes, including the detail of whether or not the change came |
| 172 // from sync. | 181 // from sync. |
| 173 typedef base::ObserverList<SyncedPrefObserver> SyncedPrefObserverList; | 182 typedef base::ObserverList<SyncedPrefObserver> SyncedPrefObserverList; |
| 174 typedef base::hash_map<std::string, SyncedPrefObserverList*> | 183 typedef base::hash_map<std::string, SyncedPrefObserverList*> |
| 175 SyncedPrefObserverMap; | 184 SyncedPrefObserverMap; |
| 176 | 185 |
| 177 void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const; | 186 void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const; |
| 178 | 187 |
| 179 SyncedPrefObserverMap synced_pref_observers_; | 188 SyncedPrefObserverMap synced_pref_observers_; |
| 189 PrefModelAssociatorClient* pref_model_associator_client_; | |
|
gab
2015/09/10 14:15:09
Should this be a const*?
| |
| 180 | 190 |
| 181 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | 191 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); |
| 182 }; | 192 }; |
| 183 | 193 |
| 184 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 194 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |