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" |
| 15 #include "base/observer_list.h" |
14 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "chrome/browser/prefs/synced_pref_observer.h" |
15 #include "sync/api/sync_data.h" | 18 #include "sync/api/sync_data.h" |
16 #include "sync/api/syncable_service.h" | 19 #include "sync/api/syncable_service.h" |
17 | 20 |
18 class PrefRegistrySyncable; | 21 class PrefRegistrySyncable; |
19 class PrefServiceSyncable; | 22 class PrefServiceSyncable; |
20 | 23 |
21 namespace sync_pb { | 24 namespace sync_pb { |
22 class PreferenceSpecifics; | 25 class PreferenceSpecifics; |
23 } | 26 } |
24 | 27 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 93 |
91 // Extract preference value and name from sync specifics. | 94 // Extract preference value and name from sync specifics. |
92 base::Value* ReadPreferenceSpecifics( | 95 base::Value* ReadPreferenceSpecifics( |
93 const sync_pb::PreferenceSpecifics& specifics, | 96 const sync_pb::PreferenceSpecifics& specifics, |
94 std::string* name); | 97 std::string* name); |
95 | 98 |
96 // Returns true if the pref under the given name is pulled down from sync. | 99 // Returns true if the pref under the given name is pulled down from sync. |
97 // Note this does not refer to SYNCABLE_PREF. | 100 // Note this does not refer to SYNCABLE_PREF. |
98 bool IsPrefSynced(const std::string& name) const; | 101 bool IsPrefSynced(const std::string& name) const; |
99 | 102 |
| 103 // Adds a SyncedPrefObserver to watch for changes to a specific pref. |
| 104 void AddSyncedPrefObserver(const std::string& name, |
| 105 SyncedPrefObserver* observer); |
| 106 |
| 107 // Removes a SyncedPrefObserver from a pref's list of observers. |
| 108 void RemoveSyncedPrefObserver(const std::string& name, |
| 109 SyncedPrefObserver* observer); |
| 110 |
100 protected: | 111 protected: |
101 friend class ProfileSyncServicePreferenceTest; | 112 friend class ProfileSyncServicePreferenceTest; |
102 | 113 |
103 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | 114 typedef std::map<std::string, syncer::SyncData> SyncDataMap; |
104 | 115 |
105 // Create an association for a given preference. If |sync_pref| is valid, | 116 // Create an association for a given preference. If |sync_pref| is valid, |
106 // signifying that sync has data for this preference, we reconcile their data | 117 // signifying that sync has data for this preference, we reconcile their data |
107 // with ours and append a new UPDATE SyncChange to |sync_changes|. If | 118 // with ours and append a new UPDATE SyncChange to |sync_changes|. If |
108 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with | 119 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with |
109 // the current preference data. | 120 // the current preference data. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // Sync's syncer::SyncChange handler. We push all our changes through this. | 161 // Sync's syncer::SyncChange handler. We push all our changes through this. |
151 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 162 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
152 | 163 |
153 // Sync's error handler. We use this to create sync errors. | 164 // Sync's error handler. We use this to create sync errors. |
154 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; | 165 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; |
155 | 166 |
156 // The datatype that this associator is responible for, either PREFERENCES or | 167 // The datatype that this associator is responible for, either PREFERENCES or |
157 // PRIORITY_PREFERENCES. | 168 // PRIORITY_PREFERENCES. |
158 syncer::ModelType type_; | 169 syncer::ModelType type_; |
159 | 170 |
| 171 private: |
| 172 // Map prefs to lists of observers. Observers will receive notification when |
| 173 // a pref changes, including the detail of whether or not the change came |
| 174 // from sync. |
| 175 typedef ObserverList<SyncedPrefObserver> SyncedPrefObserverList; |
| 176 typedef base::hash_map<std::string, SyncedPrefObserverList*> |
| 177 SyncedPrefObserverMap; |
| 178 |
| 179 void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const; |
| 180 |
| 181 SyncedPrefObserverMap synced_pref_observers_; |
| 182 |
160 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | 183 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); |
161 }; | 184 }; |
162 | 185 |
163 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | 186 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
OLD | NEW |