Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: chrome/browser/prefs/pref_service_syncable.cc

Issue 1305213009: Componentize PrefSyncableService and support classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pref_service_syncable
Patch Set: Add missing dependency on //sync:test_support_sync_api for unit tests with gn Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/prefs/pref_service_syncable.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/prefs/default_pref_store.h"
11 #include "base/prefs/overlay_user_pref_store.h"
12 #include "base/prefs/pref_notifier_impl.h"
13 #include "base/prefs/pref_registry.h"
14 #include "base/prefs/pref_value_store.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/value_conversions.h"
17 #include "chrome/browser/prefs/pref_model_associator.h"
18 #include "chrome/browser/prefs/pref_service_syncable_observer.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20
21 PrefServiceSyncable::PrefServiceSyncable(
22 PrefNotifierImpl* pref_notifier,
23 PrefValueStore* pref_value_store,
24 PersistentPrefStore* user_prefs,
25 user_prefs::PrefRegistrySyncable* pref_registry,
26 const PrefModelAssociatorClient* pref_model_associator_client,
27 base::Callback<void(PersistentPrefStore::PrefReadError)>
28 read_error_callback,
29 bool async)
30 : PrefService(pref_notifier,
31 pref_value_store,
32 user_prefs,
33 pref_registry,
34 read_error_callback,
35 async),
36 pref_sync_associator_(pref_model_associator_client, syncer::PREFERENCES),
37 priority_pref_sync_associator_(pref_model_associator_client,
38 syncer::PRIORITY_PREFERENCES) {
39 pref_sync_associator_.SetPrefService(this);
40 priority_pref_sync_associator_.SetPrefService(this);
41
42 // Let PrefModelAssociators know about changes to preference values.
43 pref_value_store->set_callback(base::Bind(
44 &PrefServiceSyncable::ProcessPrefChange, base::Unretained(this)));
45
46 // Add already-registered syncable preferences to PrefModelAssociator.
47 for (PrefRegistry::const_iterator it = pref_registry->begin();
48 it != pref_registry->end(); ++it) {
49 const std::string& path = it->first;
50 AddRegisteredSyncablePreference(path,
51 pref_registry_->GetRegistrationFlags(path));
52 }
53
54 // Watch for syncable preferences registered after this point.
55 pref_registry->SetSyncableRegistrationCallback(
56 base::Bind(&PrefServiceSyncable::AddRegisteredSyncablePreference,
57 base::Unretained(this)));
58 }
59
60 PrefServiceSyncable::~PrefServiceSyncable() {
61 // Remove our callback from the registry, since it may outlive us.
62 user_prefs::PrefRegistrySyncable* registry =
63 static_cast<user_prefs::PrefRegistrySyncable*>(pref_registry_.get());
64 registry->SetSyncableRegistrationCallback(
65 user_prefs::PrefRegistrySyncable::SyncableRegistrationCallback());
66 }
67
68 PrefServiceSyncable* PrefServiceSyncable::CreateIncognitoPrefService(
69 PrefStore* incognito_extension_pref_store,
70 const std::vector<const char*>& overlay_pref_names) {
71 pref_service_forked_ = true;
72 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
73 OverlayUserPrefStore* incognito_pref_store =
74 new OverlayUserPrefStore(user_pref_store_.get());
75 for (const char* overlay_pref_name : overlay_pref_names)
76 incognito_pref_store->RegisterOverlayPref(overlay_pref_name);
77
78 scoped_refptr<user_prefs::PrefRegistrySyncable> forked_registry =
79 static_cast<user_prefs::PrefRegistrySyncable*>(
80 pref_registry_.get())->ForkForIncognito();
81 PrefServiceSyncable* incognito_service = new PrefServiceSyncable(
82 pref_notifier,
83 pref_value_store_->CloneAndSpecialize(NULL, // managed
84 NULL, // supervised_user
85 incognito_extension_pref_store,
86 NULL, // command_line_prefs
87 incognito_pref_store,
88 NULL, // recommended
89 forked_registry->defaults().get(),
90 pref_notifier),
91 incognito_pref_store,
92 forked_registry.get(),
93 pref_sync_associator_.client(),
94 read_error_callback_,
95 false);
96 return incognito_service;
97 }
98
99 bool PrefServiceSyncable::IsSyncing() {
100 return pref_sync_associator_.models_associated();
101 }
102
103 bool PrefServiceSyncable::IsPrioritySyncing() {
104 return priority_pref_sync_associator_.models_associated();
105 }
106
107 bool PrefServiceSyncable::IsPrefSynced(const std::string& name) const {
108 return pref_sync_associator_.IsPrefSynced(name) ||
109 priority_pref_sync_associator_.IsPrefSynced(name);
110 }
111
112 void PrefServiceSyncable::AddObserver(PrefServiceSyncableObserver* observer) {
113 observer_list_.AddObserver(observer);
114 }
115
116 void PrefServiceSyncable::RemoveObserver(
117 PrefServiceSyncableObserver* observer) {
118 observer_list_.RemoveObserver(observer);
119 }
120
121 syncer::SyncableService* PrefServiceSyncable::GetSyncableService(
122 const syncer::ModelType& type) {
123 if (type == syncer::PREFERENCES) {
124 return &pref_sync_associator_;
125 } else if (type == syncer::PRIORITY_PREFERENCES) {
126 return &priority_pref_sync_associator_;
127 } else {
128 NOTREACHED() << "invalid model type: " << type;
129 return NULL;
130 }
131 }
132
133 void PrefServiceSyncable::UpdateCommandLinePrefStore(
134 PrefStore* cmd_line_store) {
135 // If |pref_service_forked_| is true, then this PrefService and the forked
136 // copies will be out of sync.
137 DCHECK(!pref_service_forked_);
138 PrefService::UpdateCommandLinePrefStore(cmd_line_store);
139 }
140
141 void PrefServiceSyncable::AddSyncedPrefObserver(const std::string& name,
142 SyncedPrefObserver* observer) {
143 pref_sync_associator_.AddSyncedPrefObserver(name, observer);
144 priority_pref_sync_associator_.AddSyncedPrefObserver(name, observer);
145 }
146
147 void PrefServiceSyncable::RemoveSyncedPrefObserver(
148 const std::string& name,
149 SyncedPrefObserver* observer) {
150 pref_sync_associator_.RemoveSyncedPrefObserver(name, observer);
151 priority_pref_sync_associator_.RemoveSyncedPrefObserver(name, observer);
152 }
153
154 // Set the PrefModelAssociatorClient to use for that object during tests.
155 void PrefServiceSyncable::SetPrefModelAssociatorClientForTesting(
156 const PrefModelAssociatorClient* pref_model_associator_client) {
157 pref_sync_associator_.SetPrefModelAssociatorClientForTesting(
158 pref_model_associator_client);
159 priority_pref_sync_associator_.SetPrefModelAssociatorClientForTesting(
160 pref_model_associator_client);
161 }
162
163 void PrefServiceSyncable::AddRegisteredSyncablePreference(
164 const std::string& path,
165 uint32 flags) {
166 DCHECK(FindPreference(path));
167 if (flags & user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) {
168 pref_sync_associator_.RegisterPref(path.c_str());
169 } else if (flags & user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) {
170 priority_pref_sync_associator_.RegisterPref(path.c_str());
171 }
172 }
173
174 void PrefServiceSyncable::OnIsSyncingChanged() {
175 FOR_EACH_OBSERVER(PrefServiceSyncableObserver, observer_list_,
176 OnIsSyncingChanged());
177 }
178
179 void PrefServiceSyncable::ProcessPrefChange(const std::string& name) {
180 pref_sync_associator_.ProcessPrefChange(name);
181 priority_pref_sync_associator_.ProcessPrefChange(name);
182 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_service_syncable.h ('k') | chrome/browser/prefs/pref_service_syncable_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698