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

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

Issue 2474653003: PreferencesManager (Closed)
Patch Set: Updates Created 4 years 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/auto_reset.h"
8 #include "base/bind.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "components/prefs/pref_change_registrar.h"
12 #include "components/prefs/pref_service.h"
13
14 PreferencesManager::PreferencesManager(Profile* profile)
15 : preferences_change_registrar_(new PrefChangeRegistrar),
16 setting_preferences_(false) {
17 DCHECK(profile);
18 service_ = profile->GetPrefs();
19 preferences_change_registrar_->Init(service_);
20 }
21
22 PreferencesManager::~PreferencesManager() {}
23
24 void PreferencesManager::PreferenceChanged(const std::string& preference_name) {
25 if (setting_preferences_)
26 return;
27 const PrefService::Preference* pref =
28 service_->FindPreference(preference_name);
29 base::DictionaryValue dictionary;
30 dictionary.Set(preference_name, pref->GetValue()->CreateDeepCopy());
31 client_->OnPreferencesChanged(dictionary);
32 }
33
34 void PreferencesManager::AddObserver(
35 prefs::mojom::PreferencesObserverPtr client) {
36 // TODO(jonross): once service_manager::Connector supports enforcing two-way
37 // binding at connection time, update PreferencesManager to use that approach.
38 // After which enforcing bind checks will not be needed (crbug.com/674140)
39 client_ = std::move(client);
40 }
41
42 void PreferencesManager::SetPreferences(
43 const base::DictionaryValue& preferences) {
44 DCHECK(client_.is_bound());
dcheng 2016/12/15 16:17:08 This should be an if (...) with an early return.
jonross 2016/12/19 15:58:13 Done.
45 DCHECK(!setting_preferences_);
46 // We ignore preference changes caused by us.
47 base::AutoReset<bool> setting_preferences(&setting_preferences_, true);
48 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
49 it.Advance()) {
50 if (!preferences_change_registrar_->IsObserved(it.key()))
51 continue;
52 const PrefService::Preference* pref = service_->FindPreference(it.key());
53 if (!pref) {
54 DLOG(ERROR) << "Preference " << it.key() << " not found.\n";
55 continue;
56 }
57 if (it.value().Equals(pref->GetValue()))
58 continue;
59 service_->Set(it.key(), it.value());
60 }
61 }
62
63 void PreferencesManager::Subscribe(
64 const std::vector<std::string>& preferences) {
65 DCHECK(client_.is_bound());
66 base::DictionaryValue dictionary;
67 for (auto& it : preferences) {
68 const PrefService::Preference* pref = service_->FindPreference(it);
69 if (!pref) {
70 DLOG(ERROR) << "Preference " << it << " not found.\n";
71 continue;
72 }
73 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned
74 // by PreferenceConnectionManager. It will outlive
75 // |preferences_change_registrar_| which it owns.
76 preferences_change_registrar_->Add(
77 it, base::Bind(&PreferencesManager::PreferenceChanged,
78 base::Unretained(this)));
79 dictionary.Set(it, pref->GetValue()->CreateDeepCopy());
80 }
81
82 if (dictionary.empty())
83 return;
84 client_->OnPreferencesChanged(dictionary);
85 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698