Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_event_router.h
blob411e9977fcf6aff9e7794829dbf6a1c7b1cf101a
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/weak_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_factory.h"
18 #include "extensions/browser/extension_event_histogram_value.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/api/bluetooth.h"
21 #include "extensions/common/api/bluetooth_private.h"
23 namespace content {
24 class BrowserContext;
27 namespace device {
29 class BluetoothDevice;
30 class BluetoothDiscoverySession;
32 } // namespace device
34 namespace extensions {
35 class BluetoothApiPairingDelegate;
36 class ExtensionRegistry;
38 class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
39 public content::NotificationObserver,
40 public ExtensionRegistryObserver {
41 public:
42 explicit BluetoothEventRouter(content::BrowserContext* context);
43 ~BluetoothEventRouter() override;
45 // Returns true if adapter_ has been initialized for testing or bluetooth
46 // adapter is available for the current platform.
47 bool IsBluetoothSupported() const;
49 void GetAdapter(
50 const device::BluetoothAdapterFactory::AdapterCallback& callback);
52 // Requests that a new device discovery session be initiated for extension
53 // with id |extension_id|. |callback| is called, if a session has been
54 // initiated. |error_callback| is called, if the adapter failed to initiate
55 // the session or if an active session already exists for the extension.
56 void StartDiscoverySession(device::BluetoothAdapter* adapter,
57 const std::string& extension_id,
58 const base::Closure& callback,
59 const base::Closure& error_callback);
61 // Requests that the active discovery session that belongs to the extension
62 // with id |extension_id| be terminated. |callback| is called, if the session
63 // successfully ended. |error_callback| is called, if the adapter failed to
64 // terminate the session or if no active discovery session exists for the
65 // extension.
66 void StopDiscoverySession(device::BluetoothAdapter* adapter,
67 const std::string& extension_id,
68 const base::Closure& callback,
69 const base::Closure& error_callback);
71 // Requests that the filter associated with discovery session that belongs
72 // to the extension with id |extension_id| be set to |discovery_filter|.
73 // Callback is called, if the filter was successfully updated.
74 // |error_callback| is called, if filter update failed.
75 void SetDiscoveryFilter(
76 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
77 device::BluetoothAdapter* adapter,
78 const std::string& extension_id,
79 const base::Closure& callback,
80 const base::Closure& error_callback);
82 // Called when a bluetooth event listener is added.
83 void OnListenerAdded();
85 // Called when a bluetooth event listener is removed.
86 void OnListenerRemoved();
88 // Adds a pairing delegate for an extension.
89 void AddPairingDelegate(const std::string& extension_id);
91 // Removes the pairing delegate for an extension.
92 void RemovePairingDelegate(const std::string& extension_id);
94 // Returns the pairing delegate for an extension or NULL if it doesn't have a
95 // pairing delegate.
96 BluetoothApiPairingDelegate* GetPairingDelegate(
97 const std::string& extension_id);
99 // Exposed for testing.
100 void SetAdapterForTest(device::BluetoothAdapter* adapter) {
101 adapter_ = adapter;
104 // Override from device::BluetoothAdapter::Observer.
105 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
106 bool present) override;
107 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
108 bool has_power) override;
109 void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
110 bool discovering) override;
111 void DeviceAdded(device::BluetoothAdapter* adapter,
112 device::BluetoothDevice* device) override;
113 void DeviceChanged(device::BluetoothAdapter* adapter,
114 device::BluetoothDevice* device) override;
115 void DeviceRemoved(device::BluetoothAdapter* adapter,
116 device::BluetoothDevice* device) override;
118 // Overridden from content::NotificationObserver.
119 void Observe(int type,
120 const content::NotificationSource& source,
121 const content::NotificationDetails& details) override;
123 // Overridden from ExtensionRegistryObserver.
124 void OnExtensionUnloaded(content::BrowserContext* browser_context,
125 const Extension* extension,
126 UnloadedExtensionInfo::Reason reason) override;
128 // BrowserContextKeyedAPI implementation.
129 static const char* service_name() { return "BluetoothEventRouter"; }
130 static const bool kServiceRedirectedInIncognito = true;
131 static const bool kServiceIsNULLWhileTesting = true;
133 private:
134 void OnAdapterInitialized(const base::Closure& callback,
135 scoped_refptr<device::BluetoothAdapter> adapter);
136 void MaybeReleaseAdapter();
137 void DispatchAdapterStateEvent();
138 void DispatchDeviceEvent(events::HistogramValue histogram_value,
139 const std::string& event_name,
140 device::BluetoothDevice* device);
141 void CleanUpForExtension(const std::string& extension_id);
142 void CleanUpAllExtensions();
143 void OnStartDiscoverySession(
144 const std::string& extension_id,
145 const base::Closure& callback,
146 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
148 void OnSetDiscoveryFilter(const std::string& extension_id,
149 const base::Closure& callback);
151 content::BrowserContext* browser_context_;
152 scoped_refptr<device::BluetoothAdapter> adapter_;
154 int num_event_listeners_;
156 // A map that maps extension ids to BluetoothDiscoverySession pointers.
157 typedef std::map<std::string, device::BluetoothDiscoverySession*>
158 DiscoverySessionMap;
159 DiscoverySessionMap discovery_session_map_;
161 typedef std::map<std::string, device::BluetoothDiscoveryFilter*>
162 PreSetFilterMap;
164 // Maps an extension id to it's pre-set discovery filter.
165 PreSetFilterMap pre_set_filter_map_;
167 // Maps an extension id to its pairing delegate.
168 typedef std::map<std::string, BluetoothApiPairingDelegate*>
169 PairingDelegateMap;
170 PairingDelegateMap pairing_delegate_map_;
172 content::NotificationRegistrar registrar_;
174 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
175 extension_registry_observer_;
177 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
179 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
182 } // namespace extensions
184 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_