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_
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_registry_observer.h"
20 #include "extensions/common/api/bluetooth.h"
21 #include "extensions/common/api/bluetooth_private.h"
29 class BluetoothDevice
;
30 class BluetoothDiscoverySession
;
34 namespace extensions
{
35 class BluetoothApiPairingDelegate
;
36 class ExtensionRegistry
;
38 class BluetoothEventRouter
: public device::BluetoothAdapter::Observer
,
39 public content::NotificationObserver
,
40 public ExtensionRegistryObserver
{
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;
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
66 void StopDiscoverySession(device::BluetoothAdapter
* adapter
,
67 const std::string
& extension_id
,
68 const base::Closure
& callback
,
69 const base::Closure
& error_callback
);
71 // Called when a bluetooth event listener is added.
72 void OnListenerAdded();
74 // Called when a bluetooth event listener is removed.
75 void OnListenerRemoved();
77 // Adds a pairing delegate for an extension.
78 void AddPairingDelegate(const std::string
& extension_id
);
80 // Removes the pairing delegate for an extension.
81 void RemovePairingDelegate(const std::string
& extension_id
);
83 // Returns the pairing delegate for an extension or NULL if it doesn't have a
85 BluetoothApiPairingDelegate
* GetPairingDelegate(
86 const std::string
& extension_id
);
88 // Exposed for testing.
89 void SetAdapterForTest(device::BluetoothAdapter
* adapter
) {
93 // Override from device::BluetoothAdapter::Observer.
94 void AdapterPresentChanged(device::BluetoothAdapter
* adapter
,
95 bool present
) override
;
96 void AdapterPoweredChanged(device::BluetoothAdapter
* adapter
,
97 bool has_power
) override
;
98 void AdapterDiscoveringChanged(device::BluetoothAdapter
* adapter
,
99 bool discovering
) override
;
100 void DeviceAdded(device::BluetoothAdapter
* adapter
,
101 device::BluetoothDevice
* device
) override
;
102 void DeviceChanged(device::BluetoothAdapter
* adapter
,
103 device::BluetoothDevice
* device
) override
;
104 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
105 device::BluetoothDevice
* device
) override
;
107 // Overridden from content::NotificationObserver.
108 void Observe(int type
,
109 const content::NotificationSource
& source
,
110 const content::NotificationDetails
& details
) override
;
112 // Overridden from ExtensionRegistryObserver.
113 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
114 const Extension
* extension
,
115 UnloadedExtensionInfo::Reason reason
) override
;
117 // BrowserContextKeyedAPI implementation.
118 static const char* service_name() { return "BluetoothEventRouter"; }
119 static const bool kServiceRedirectedInIncognito
= true;
120 static const bool kServiceIsNULLWhileTesting
= true;
123 void OnAdapterInitialized(const base::Closure
& callback
,
124 scoped_refptr
<device::BluetoothAdapter
> adapter
);
125 void MaybeReleaseAdapter();
126 void DispatchAdapterStateEvent();
127 void DispatchDeviceEvent(const std::string
& event_name
,
128 device::BluetoothDevice
* device
);
129 void CleanUpForExtension(const std::string
& extension_id
);
130 void CleanUpAllExtensions();
131 void OnStartDiscoverySession(
132 const std::string
& extension_id
,
133 const base::Closure
& callback
,
134 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
);
136 content::BrowserContext
* browser_context_
;
137 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
139 int num_event_listeners_
;
141 // A map that maps extension ids to BluetoothDiscoverySession pointers.
142 typedef std::map
<std::string
, device::BluetoothDiscoverySession
*>
144 DiscoverySessionMap discovery_session_map_
;
146 // Maps an extension id to its pairing delegate.
147 typedef std::map
<std::string
, BluetoothApiPairingDelegate
*>
149 PairingDelegateMap pairing_delegate_map_
;
151 content::NotificationRegistrar registrar_
;
153 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
154 extension_registry_observer_
;
156 base::WeakPtrFactory
<BluetoothEventRouter
> weak_ptr_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter
);
161 } // namespace extensions
163 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_