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

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

Issue 2474653003: PreferencesManager (Closed)
Patch Set: PreferencesConnectionManager Created 4 years, 1 month 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 2016 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/preferences_manager.h"
6
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "components/prefs/pref_change_registrar.h"
12 #include "components/prefs/pref_service.h"
13
14 namespace chrome {
15
16 PreferencesManager::PreferencesManager()
17 : preferences_change_registrar_(new PrefChangeRegistrar),
18 setting_preferences_(false) {
19 Profile* profile = ProfileManager::GetActiveUserProfile();
20 service_ = profile->GetPrefs();
21 preferences_change_registrar_->Init(service_);
22 }
23
24 PreferencesManager::~PreferencesManager() {}
25
26 void PreferencesManager::OnProfileDestroyed() {
27 preferences_change_registrar_.reset();
28 }
29
30 void PreferencesManager::AddObserver(
31 const std::vector<std::string>& preferences,
32 prefs::mojom::PreferencesObserverPtr client) {
33 client_ = std::move(client);
34 base::DictionaryValue dictionary;
35 for (auto it : preferences) {
36 const PrefService::Preference* pref = service_->FindPreference(it);
37 if (!pref)
38 continue;
39 // PreferenceManager lifetime is managed by PreferenceConnectionManager it
40 // will outlive |preferences_change_registrar_| which it owns.
41 preferences_change_registrar_->Add(
42 it, base::Bind(&PreferencesManager::PreferenceChanged,
43 base::Unretained(this)));
44 dictionary.Set(it, pref->GetValue()->CreateDeepCopy());
45 }
46
47 if (dictionary.empty())
48 return;
49 client_->OnPreferencesChanged(dictionary);
50 }
51
52 void PreferencesManager::SetPreferences(
53 const base::DictionaryValue& preferences) {
54 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
55 it.Advance()) {
56 if (!preferences_change_registrar_->IsObserved(it.key()))
57 continue;
58 const PrefService::Preference* pref = service_->FindPreference(it.key());
59 if (!pref)
60 continue;
61 if (it.value().Equals(pref->GetValue()))
62 continue;
63 // We ignore preference changes caused by us.
64 setting_preferences_ = true;
65 service_->Set(it.key(), it.value());
66 setting_preferences_ = false;
67 }
68 }
69
70 void PreferencesManager::PreferenceChanged(const std::string& preference_name) {
71 if (setting_preferences_)
72 return;
73 const PrefService::Preference* pref =
74 service_->FindPreference(preference_name);
75 if (!pref)
76 return;
77 base::DictionaryValue dictionary;
78 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy());
79 client_->OnPreferencesChanged(dictionary);
80 }
81
82 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698