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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_EVENT_ROUTER_H_
8 #include "base/prefs/pref_change_registrar.h"
9 #include "chrome/browser/chromeos/settings/cros_settings.h"
10 #include "chrome/browser/extensions/api/settings_private/prefs_util.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "extensions/browser/event_router.h"
18 namespace extensions
{
20 class SettingsPrivateDelegate
;
22 // This is an event router that will observe listeners to pref changes on the
23 // appropriate pref service(s) and notify listeners on the JavaScript
24 // settingsPrivate API.
25 class SettingsPrivateEventRouter
: public KeyedService
,
26 public EventRouter::Observer
{
28 static SettingsPrivateEventRouter
* Create(
29 content::BrowserContext
* browser_context
);
30 ~SettingsPrivateEventRouter() override
;
33 explicit SettingsPrivateEventRouter(content::BrowserContext
* context
);
35 // KeyedService overrides:
36 void Shutdown() override
;
38 // EventRouter::Observer overrides:
39 void OnListenerAdded(const EventListenerInfo
& details
) override
;
40 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
42 // This registrar monitors for user prefs changes.
43 PrefChangeRegistrar user_prefs_registrar_
;
45 // This registrar monitors for local state changes.
46 PrefChangeRegistrar local_state_registrar_
;
49 // Decide if we should listen for pref changes or not. If there are any
50 // JavaScript listeners registered for the onPrefsChanged event, then we
51 // want to register for change notification from the PrefChangeRegistrar.
52 // Otherwise, we want to unregister and not be listening for pref changes.
53 void StartOrStopListeningForPrefsChanges();
55 void OnPreferenceChanged(const std::string
& pref_name
);
57 PrefChangeRegistrar
* FindRegistrarForPref(const std::string
& pref_name
);
59 typedef std::map
<std::string
,
60 linked_ptr
<chromeos::CrosSettings::ObserverSubscription
>>
62 SubscriptionMap cros_settings_subscription_map_
;
64 content::BrowserContext
* context_
;
67 scoped_ptr
<PrefsUtil
> prefs_util_
;
69 DISALLOW_COPY_AND_ASSIGN(SettingsPrivateEventRouter
);
72 } // namespace extensions
74 #endif // CHROME_BROWSER_EXTENSIONS_API_SETTINGS_PRIVATE_SETTINGS_PRIVATE_EVENT_ROUTER_H_