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

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

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

Powered by Google App Engine
This is Rietveld 408576698