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

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

Issue 2474653003: PreferencesManager (Closed)
Patch Set: Update Binding Check and ConnectionError Handling 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 if (!client_.is_bound())
45 return;
46 DCHECK(!setting_preferences_);
47 // We ignore preference changes caused by us.
48 base::AutoReset<bool> setting_preferences(&setting_preferences_, true);
49 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
50 it.Advance()) {
51 if (!preferences_change_registrar_->IsObserved(it.key()))
52 continue;
53 const PrefService::Preference* pref = service_->FindPreference(it.key());
54 if (!pref) {
55 DLOG(ERROR) << "Preference " << it.key() << " not found.\n";
56 continue;
57 }
58 if (it.value().Equals(pref->GetValue()))
59 continue;
60 service_->Set(it.key(), it.value());
61 }
62 }
63
64 void PreferencesManager::Subscribe(
65 const std::vector<std::string>& preferences) {
66 if (!client_.is_bound())
67 return;
68 base::DictionaryValue dictionary;
69 for (auto& it : preferences) {
70 const PrefService::Preference* pref = service_->FindPreference(it);
71 if (!pref) {
72 DLOG(ERROR) << "Preference " << it << " not found.\n";
73 continue;
74 }
75 // PreferenceManager lifetime is managed by a mojo::StrongBindingPtr owned
76 // by PreferenceConnectionManager. It will outlive
77 // |preferences_change_registrar_| which it owns.
78 preferences_change_registrar_->Add(
79 it, base::Bind(&PreferencesManager::PreferenceChanged,
80 base::Unretained(this)));
81 dictionary.Set(it, pref->GetValue()->CreateDeepCopy());
82 }
83
84 if (dictionary.empty())
85 return;
86 client_->OnPreferencesChanged(dictionary);
87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698