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

Side by Side Diff: chrome/browser/extensions/api/settings_private/settings_private_event_router.cc

Issue 1163593005: Update chrome.settingsPrivate to support CrOS-only settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix issues preventing compilation on non-CrOS builds. Created 5 years, 6 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/extensions/api/settings_private/settings_private_event_ router.h" 5 #include "chrome/browser/extensions/api/settings_private/settings_private_event_ router.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/api/settings_private/prefs_util.h"
14 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/extensions/api/settings_private.h" 14 #include "chrome/common/extensions/api/settings_private.h"
16 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
17 16
18 namespace extensions { 17 namespace extensions {
19 18
20 SettingsPrivateEventRouter::SettingsPrivateEventRouter( 19 SettingsPrivateEventRouter::SettingsPrivateEventRouter(
21 content::BrowserContext* context) 20 content::BrowserContext* context)
22 : context_(context), listening_(false) { 21 : context_(context), listening_(false) {
23 // Register with the event router so we know when renderers are listening to 22 // Register with the event router so we know when renderers are listening to
24 // our events. We first check and see if there *is* an event router, because 23 // our events. We first check and see if there *is* an event router, because
25 // some unit tests try to create all context services, but don't initialize 24 // some unit tests try to create all context services, but don't initialize
26 // the event router first. 25 // the event router first.
27 EventRouter* event_router = EventRouter::Get(context_); 26 EventRouter* event_router = EventRouter::Get(context_);
28 if (event_router) { 27 if (event_router) {
29 event_router->RegisterObserver( 28 event_router->RegisterObserver(
30 this, api::settings_private::OnPrefsChanged::kEventName); 29 this, api::settings_private::OnPrefsChanged::kEventName);
31 StartOrStopListeningForPrefsChanges(); 30 StartOrStopListeningForPrefsChanges();
32 } 31 }
33 32
34 Profile* profile = Profile::FromBrowserContext(context_); 33 Profile* profile = Profile::FromBrowserContext(context_);
34 prefs_util_.reset(new PrefsUtil(profile));
35 user_prefs_registrar_.Init(profile->GetPrefs()); 35 user_prefs_registrar_.Init(profile->GetPrefs());
36 local_state_registrar_.Init(g_browser_process->local_state()); 36 local_state_registrar_.Init(g_browser_process->local_state());
37 } 37 }
38 38
39 SettingsPrivateEventRouter::~SettingsPrivateEventRouter() { 39 SettingsPrivateEventRouter::~SettingsPrivateEventRouter() {
40 DCHECK(!listening_); 40 DCHECK(!listening_);
41 } 41 }
42 42
43 void SettingsPrivateEventRouter::Shutdown() { 43 void SettingsPrivateEventRouter::Shutdown() {
44 // Unregister with the event router. We first check and see if there *is* an 44 // Unregister with the event router. We first check and see if there *is* an
45 // event router, because some unit tests try to shutdown all context services, 45 // event router, because some unit tests try to shutdown all context services,
46 // but didn't initialize the event router first. 46 // but didn't initialize the event router first.
47 EventRouter* event_router = EventRouter::Get(context_); 47 EventRouter* event_router = EventRouter::Get(context_);
48 if (event_router) 48 if (event_router)
49 event_router->UnregisterObserver(this); 49 event_router->UnregisterObserver(this);
50 50
51 if (listening_) { 51 if (listening_) {
52 const prefs_util::TypedPrefMap& keys = prefs_util::GetWhitelistedKeys(); 52 cros_settings_subscription_map_.clear();
53 const PrefsUtil::TypedPrefMap& keys = prefs_util_->GetWhitelistedKeys();
53 for (const auto& it : keys) { 54 for (const auto& it : keys) {
54 FindRegistrarForPref(it.first)->Remove(it.first); 55 if (!prefs_util_->IsCrosSetting(it.first))
56 FindRegistrarForPref(it.first)->Remove(it.first);
55 } 57 }
56 } 58 }
57 listening_ = false; 59 listening_ = false;
58 } 60 }
59 61
60 void SettingsPrivateEventRouter::OnListenerAdded( 62 void SettingsPrivateEventRouter::OnListenerAdded(
61 const EventListenerInfo& details) { 63 const EventListenerInfo& details) {
62 // Start listening to events from the PrefChangeRegistrars. 64 // Start listening to events from the PrefChangeRegistrars.
63 StartOrStopListeningForPrefsChanges(); 65 StartOrStopListeningForPrefsChanges();
64 } 66 }
65 67
66 void SettingsPrivateEventRouter::OnListenerRemoved( 68 void SettingsPrivateEventRouter::OnListenerRemoved(
67 const EventListenerInfo& details) { 69 const EventListenerInfo& details) {
68 // Stop listening to events from the PrefChangeRegistrars if there are no 70 // Stop listening to events from the PrefChangeRegistrars if there are no
69 // more listeners. 71 // more listeners.
70 StartOrStopListeningForPrefsChanges(); 72 StartOrStopListeningForPrefsChanges();
71 } 73 }
72 74
73 PrefChangeRegistrar* SettingsPrivateEventRouter::FindRegistrarForPref( 75 PrefChangeRegistrar* SettingsPrivateEventRouter::FindRegistrarForPref(
74 const std::string& pref_name) { 76 const std::string& pref_name) {
75 Profile* profile = Profile::FromBrowserContext(context_); 77 Profile* profile = Profile::FromBrowserContext(context_);
76 if (prefs_util::FindServiceForPref(profile, pref_name) == 78 if (prefs_util_->FindServiceForPref(pref_name) == profile->GetPrefs()) {
77 profile->GetPrefs()) {
78 return &user_prefs_registrar_; 79 return &user_prefs_registrar_;
79 } 80 }
80 return &local_state_registrar_; 81 return &local_state_registrar_;
81 } 82 }
82 83
83 void SettingsPrivateEventRouter::StartOrStopListeningForPrefsChanges() { 84 void SettingsPrivateEventRouter::StartOrStopListeningForPrefsChanges() {
84 EventRouter* event_router = EventRouter::Get(context_); 85 EventRouter* event_router = EventRouter::Get(context_);
85 bool should_listen = event_router->HasEventListener( 86 bool should_listen = event_router->HasEventListener(
86 api::settings_private::OnPrefsChanged::kEventName); 87 api::settings_private::OnPrefsChanged::kEventName);
87 88
88 if (should_listen && !listening_) { 89 if (should_listen && !listening_) {
89 const prefs_util::TypedPrefMap& keys = prefs_util::GetWhitelistedKeys(); 90 const PrefsUtil::TypedPrefMap& keys = prefs_util_->GetWhitelistedKeys();
90 for (const auto& it : keys) { 91 for (const auto& it : keys) {
91 FindRegistrarForPref(it.first)->Add( 92 std::string pref_name = it.first;
92 it.first, 93 if (prefs_util_->IsCrosSetting(pref_name)) {
93 base::Bind(&SettingsPrivateEventRouter::OnPreferenceChanged, 94 #if defined(OS_CHROMEOS)
94 base::Unretained(this), user_prefs_registrar_.prefs())); 95 scoped_ptr<chromeos::CrosSettings::ObserverSubscription> observer =
96 chromeos::CrosSettings::Get()->AddSettingsObserver(
97 pref_name.c_str(),
98 base::Bind(&SettingsPrivateEventRouter::OnPreferenceChanged,
99 base::Unretained(this), pref_name));
100 linked_ptr<chromeos::CrosSettings::ObserverSubscription> subscription(
101 observer.release());
102 cros_settings_subscription_map_.insert(
103 make_pair(pref_name, subscription));
104 #endif
stevenjb 2015/06/05 22:59:15 nit: This could be written as follows to avoid an
105 } else {
106 FindRegistrarForPref(it.first)
107 ->Add(pref_name,
108 base::Bind(&SettingsPrivateEventRouter::OnPreferenceChanged,
109 base::Unretained(this)));
110 }
95 } 111 }
96 } else if (!should_listen && listening_) { 112 } else if (!should_listen && listening_) {
97 const prefs_util::TypedPrefMap& keys = prefs_util::GetWhitelistedKeys(); 113 const PrefsUtil::TypedPrefMap& keys = prefs_util_->GetWhitelistedKeys();
98 for (const auto& it : keys) { 114 for (const auto& it : keys) {
99 FindRegistrarForPref(it.first)->Remove(it.first); 115 if (prefs_util_->IsCrosSetting(it.first))
116 cros_settings_subscription_map_.erase(it.first);
117 else
118 FindRegistrarForPref(it.first)->Remove(it.first);
100 } 119 }
101 } 120 }
102 listening_ = should_listen; 121 listening_ = should_listen;
103 } 122 }
104 123
105 void SettingsPrivateEventRouter::OnPreferenceChanged( 124 void SettingsPrivateEventRouter::OnPreferenceChanged(
106 PrefService* service,
107 const std::string& pref_name) { 125 const std::string& pref_name) {
108 EventRouter* event_router = EventRouter::Get(context_); 126 EventRouter* event_router = EventRouter::Get(context_);
109 if (!event_router->HasEventListener( 127 if (!event_router->HasEventListener(
110 api::settings_private::OnPrefsChanged::kEventName)) { 128 api::settings_private::OnPrefsChanged::kEventName)) {
111 return; 129 return;
112 } 130 }
113 131
114 Profile* profile = Profile::FromBrowserContext(context_);
115 api::settings_private::PrefObject* pref_object = 132 api::settings_private::PrefObject* pref_object =
116 prefs_util::GetPref(profile, pref_name).release(); 133 prefs_util_->GetPref(pref_name).release();
117 134
118 std::vector<linked_ptr<api::settings_private::PrefObject>> prefs; 135 std::vector<linked_ptr<api::settings_private::PrefObject>> prefs;
119 prefs.push_back(linked_ptr<api::settings_private::PrefObject>(pref_object)); 136 prefs.push_back(linked_ptr<api::settings_private::PrefObject>(pref_object));
120 137
121 scoped_ptr<base::ListValue> args( 138 scoped_ptr<base::ListValue> args(
122 api::settings_private::OnPrefsChanged::Create(prefs)); 139 api::settings_private::OnPrefsChanged::Create(prefs));
123 140
124 scoped_ptr<Event> extension_event(new Event( 141 scoped_ptr<Event> extension_event(new Event(
125 api::settings_private::OnPrefsChanged::kEventName, args.Pass())); 142 api::settings_private::OnPrefsChanged::kEventName, args.Pass()));
126 event_router->BroadcastEvent(extension_event.Pass()); 143 event_router->BroadcastEvent(extension_event.Pass());
127 } 144 }
128 145
129 SettingsPrivateEventRouter* SettingsPrivateEventRouter::Create( 146 SettingsPrivateEventRouter* SettingsPrivateEventRouter::Create(
130 content::BrowserContext* context) { 147 content::BrowserContext* context) {
131 return new SettingsPrivateEventRouter(context); 148 return new SettingsPrivateEventRouter(context);
132 } 149 }
133 150
134 } // namespace extensions 151 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698