Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_event_router.h
blob2e6a76a660cca31e0338007da3f5f733699df349
1 // Copyright (c) 2012 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 EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
8 #include <map>
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "extensions/browser/extension_event_histogram_value.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "extensions/common/api/bluetooth.h"
22 #include "extensions/common/api/bluetooth_private.h"
24 namespace content {
25 class BrowserContext;
28 namespace device {
30 class BluetoothDevice;
31 class BluetoothDiscoverySession;
33 } // namespace device
35 namespace extensions {
36 class BluetoothApiPairingDelegate;
37 class ExtensionRegistry;
39 class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
40 public content::NotificationObserver,
41 public ExtensionRegistryObserver {
42 public:
43 explicit BluetoothEventRouter(content::BrowserContext* context);
44 ~BluetoothEventRouter() override;
46 // Returns true if adapter_ has been initialized for testing or bluetooth
47 // adapter is available for the current platform.
48 bool IsBluetoothSupported() const;
50 void GetAdapter(
51 const device::BluetoothAdapterFactory::AdapterCallback& callback);
53 // Requests that a new device discovery session be initiated for extension
54 // with id |extension_id|. |callback| is called, if a session has been
55 // initiated. |error_callback| is called, if the adapter failed to initiate
56 // the session or if an active session already exists for the extension.
57 void StartDiscoverySession(device::BluetoothAdapter* adapter,
58 const std::string& extension_id,
59 const base::Closure& callback,
60 const base::Closure& error_callback);
62 // Requests that the active discovery session that belongs to the extension
63 // with id |extension_id| be terminated. |callback| is called, if the session
64 // successfully ended. |error_callback| is called, if the adapter failed to
65 // terminate the session or if no active discovery session exists for the
66 // extension.
67 void StopDiscoverySession(device::BluetoothAdapter* adapter,
68 const std::string& extension_id,
69 const base::Closure& callback,
70 const base::Closure& error_callback);
72 // Requests that the filter associated with discovery session that belongs
73 // to the extension with id |extension_id| be set to |discovery_filter|.
74 // Callback is called, if the filter was successfully updated.
75 // |error_callback| is called, if filter update failed.
76 void SetDiscoveryFilter(
77 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
78 device::BluetoothAdapter* adapter,
79 const std::string& extension_id,
80 const base::Closure& callback,
81 const base::Closure& error_callback);
83 // Called when a bluetooth event listener is added.
84 void OnListenerAdded();
86 // Called when a bluetooth event listener is removed.
87 void OnListenerRemoved();
89 // Adds a pairing delegate for an extension.
90 void AddPairingDelegate(const std::string& extension_id);
92 // Removes the pairing delegate for an extension.
93 void RemovePairingDelegate(const std::string& extension_id);
95 // Returns the pairing delegate for an extension or NULL if it doesn't have a
96 // pairing delegate.
97 BluetoothApiPairingDelegate* GetPairingDelegate(
98 const std::string& extension_id);
100 // Exposed for testing.
101 void SetAdapterForTest(device::BluetoothAdapter* adapter) {
102 adapter_ = adapter;
105 // Override from device::BluetoothAdapter::Observer.
106 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
107 bool present) override;
108 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
109 bool has_power) override;
110 void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
111 bool discovering) override;
112 void DeviceAdded(device::BluetoothAdapter* adapter,
113 device::BluetoothDevice* device) override;
114 void DeviceChanged(device::BluetoothAdapter* adapter,
115 device::BluetoothDevice* device) override;
116 void DeviceRemoved(device::BluetoothAdapter* adapter,
117 device::BluetoothDevice* device) override;
119 // Overridden from content::NotificationObserver.
120 void Observe(int type,
121 const content::NotificationSource& source,
122 const content::NotificationDetails& details) override;
124 // Overridden from ExtensionRegistryObserver.
125 void OnExtensionUnloaded(content::BrowserContext* browser_context,
126 const Extension* extension,
127 UnloadedExtensionInfo::Reason reason) override;
129 // BrowserContextKeyedAPI implementation.
130 static const char* service_name() { return "BluetoothEventRouter"; }
131 static const bool kServiceRedirectedInIncognito = true;
132 static const bool kServiceIsNULLWhileTesting = true;
134 private:
135 void OnAdapterInitialized(const base::Closure& callback,
136 scoped_refptr<device::BluetoothAdapter> adapter);
137 void MaybeReleaseAdapter();
138 void DispatchAdapterStateEvent();
139 void DispatchDeviceEvent(events::HistogramValue histogram_value,
140 const std::string& event_name,
141 device::BluetoothDevice* device);
142 void CleanUpForExtension(const std::string& extension_id);
143 void CleanUpAllExtensions();
144 void OnStartDiscoverySession(
145 const std::string& extension_id,
146 const base::Closure& callback,
147 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
149 void OnSetDiscoveryFilter(const std::string& extension_id,
150 const base::Closure& callback);
152 content::BrowserContext* browser_context_;
153 scoped_refptr<device::BluetoothAdapter> adapter_;
155 int num_event_listeners_;
157 // A map that maps extension ids to BluetoothDiscoverySession pointers.
158 typedef std::map<std::string, device::BluetoothDiscoverySession*>
159 DiscoverySessionMap;
160 DiscoverySessionMap discovery_session_map_;
162 typedef std::map<std::string, device::BluetoothDiscoveryFilter*>
163 PreSetFilterMap;
165 // Maps an extension id to it's pre-set discovery filter.
166 PreSetFilterMap pre_set_filter_map_;
168 // Maps an extension id to its pairing delegate.
169 typedef std::map<std::string, BluetoothApiPairingDelegate*>
170 PairingDelegateMap;
171 PairingDelegateMap pairing_delegate_map_;
173 content::NotificationRegistrar registrar_;
175 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
176 extension_registry_observer_;
178 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
180 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
183 } // namespace extensions
185 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_