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

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

Issue 2742663002: MD Settings: defer sending pref updates until all synchronous operations finish (Closed)
Patch Set: damn 's' Created 3 years, 9 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
« no previous file with comments | « chrome/browser/extensions/api/settings_private/settings_private_event_router.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/threading/thread_task_runner_handle.h"
12 #include "build/build_config.h" 13 #include "build/build_config.h"
13 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/extensions/api/settings_private.h" 16 #include "chrome/common/extensions/api/settings_private.h"
16 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
17 #include "content/public/browser/browser_context.h" 18 #include "content/public/browser/browser_context.h"
18 19
19 namespace extensions { 20 namespace extensions {
20 21
21 SettingsPrivateEventRouter::SettingsPrivateEventRouter( 22 SettingsPrivateEventRouter::SettingsPrivateEventRouter(
22 content::BrowserContext* context) 23 content::BrowserContext* context)
23 : context_(context), listening_(false) { 24 : context_(context), listening_(false), weak_ptr_factory_(this) {
24 // Register with the event router so we know when renderers are listening to 25 // Register with the event router so we know when renderers are listening to
25 // our events. We first check and see if there *is* an event router, because 26 // our events. We first check and see if there *is* an event router, because
26 // some unit tests try to create all context services, but don't initialize 27 // some unit tests try to create all context services, but don't initialize
27 // the event router first. 28 // the event router first.
28 EventRouter* event_router = EventRouter::Get(context_); 29 EventRouter* event_router = EventRouter::Get(context_);
29 if (event_router) { 30 if (event_router) {
30 event_router->RegisterObserver( 31 event_router->RegisterObserver(
31 this, api::settings_private::OnPrefsChanged::kEventName); 32 this, api::settings_private::OnPrefsChanged::kEventName);
32 StartOrStopListeningForPrefsChanges(); 33 StartOrStopListeningForPrefsChanges();
33 } 34 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 cros_settings_subscription_map_.erase(it.first); 117 cros_settings_subscription_map_.erase(it.first);
117 else 118 else
118 FindRegistrarForPref(it.first)->Remove(it.first); 119 FindRegistrarForPref(it.first)->Remove(it.first);
119 } 120 }
120 } 121 }
121 listening_ = should_listen; 122 listening_ = should_listen;
122 } 123 }
123 124
124 void SettingsPrivateEventRouter::OnPreferenceChanged( 125 void SettingsPrivateEventRouter::OnPreferenceChanged(
125 const std::string& pref_name) { 126 const std::string& pref_name) {
127 // This posts an asynchronous task to ensure that all pref stores are updated,
128 // as |prefs_util_->GetPref()| relies on this information to determine if a
129 // preference is controlled by e.g. extensions.
130 base::ThreadTaskRunnerHandle::Get()->PostTask(
131 FROM_HERE, base::Bind(&SettingsPrivateEventRouter::SendPrefChange,
132 weak_ptr_factory_.GetWeakPtr(), pref_name));
133 }
134
135 void SettingsPrivateEventRouter::SendPrefChange(const std::string& pref_name) {
126 EventRouter* event_router = EventRouter::Get(context_); 136 EventRouter* event_router = EventRouter::Get(context_);
127 if (!event_router->HasEventListener( 137 if (!event_router->HasEventListener(
128 api::settings_private::OnPrefsChanged::kEventName)) { 138 api::settings_private::OnPrefsChanged::kEventName)) {
129 return; 139 return;
130 } 140 }
131 141
132 std::unique_ptr<api::settings_private::PrefObject> pref_object = 142 std::unique_ptr<api::settings_private::PrefObject> pref_object =
133 prefs_util_->GetPref(pref_name); 143 prefs_util_->GetPref(pref_name);
134 144
135 std::vector<api::settings_private::PrefObject> prefs; 145 std::vector<api::settings_private::PrefObject> prefs;
136 if (pref_object) 146 if (pref_object)
137 prefs.push_back(std::move(*pref_object)); 147 prefs.push_back(std::move(*pref_object));
138 148
139 std::unique_ptr<base::ListValue> args( 149 std::unique_ptr<base::ListValue> args(
140 api::settings_private::OnPrefsChanged::Create(prefs)); 150 api::settings_private::OnPrefsChanged::Create(prefs));
141 151
142 std::unique_ptr<Event> extension_event(new Event( 152 std::unique_ptr<Event> extension_event(new Event(
143 events::SETTINGS_PRIVATE_ON_PREFS_CHANGED, 153 events::SETTINGS_PRIVATE_ON_PREFS_CHANGED,
144 api::settings_private::OnPrefsChanged::kEventName, std::move(args))); 154 api::settings_private::OnPrefsChanged::kEventName, std::move(args)));
145 event_router->BroadcastEvent(std::move(extension_event)); 155 event_router->BroadcastEvent(std::move(extension_event));
146 } 156 }
147 157
148 SettingsPrivateEventRouter* SettingsPrivateEventRouter::Create( 158 SettingsPrivateEventRouter* SettingsPrivateEventRouter::Create(
149 content::BrowserContext* context) { 159 content::BrowserContext* context) {
150 return new SettingsPrivateEventRouter(context); 160 return new SettingsPrivateEventRouter(context);
151 } 161 }
152 162
153 } // namespace extensions 163 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/settings_private/settings_private_event_router.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698