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 virtual ~BluetoothEventRouter();
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 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
95 bool present) OVERRIDE;
96 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
97 bool has_power) OVERRIDE;
98 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
99 bool discovering) OVERRIDE;
100 virtual void DeviceAdded(device::BluetoothAdapter* adapter,
101 device::BluetoothDevice* device) OVERRIDE;
102 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
103 device::BluetoothDevice* device) OVERRIDE;
104 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
105 device::BluetoothDevice* device) OVERRIDE;
107 // Overridden from content::NotificationObserver.
108 virtual void Observe(int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) OVERRIDE;
112 // Overridden from ExtensionRegistryObserver.
113 virtual void OnExtensionUnloaded(
114 content::BrowserContext* browser_context,
115 const Extension* extension,
116 UnloadedExtensionInfo::Reason reason) OVERRIDE;
118 // BrowserContextKeyedAPI implementation.
119 static const char* service_name() { return "BluetoothEventRouter"; }
120 static const bool kServiceRedirectedInIncognito = true;
121 static const bool kServiceIsNULLWhileTesting = true;
124 void OnAdapterInitialized(const base::Closure& callback,
125 scoped_refptr<device::BluetoothAdapter> adapter);
126 void MaybeReleaseAdapter();
127 void DispatchAdapterStateEvent();
128 void DispatchDeviceEvent(const std::string& event_name,
129 device::BluetoothDevice* device);
130 void CleanUpForExtension(const std::string& extension_id);
131 void CleanUpAllExtensions();
132 void OnStartDiscoverySession(
133 const std::string& extension_id,
134 const base::Closure& callback,
135 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
137 content::BrowserContext* browser_context_;
138 scoped_refptr<device::BluetoothAdapter> adapter_;
140 int num_event_listeners_;
142 // A map that maps extension ids to BluetoothDiscoverySession pointers.
143 typedef std::map<std::string, device::BluetoothDiscoverySession*>
145 DiscoverySessionMap discovery_session_map_;
147 // Maps an extension id to its pairing delegate.
148 typedef std::map<std::string, BluetoothApiPairingDelegate*>
150 PairingDelegateMap pairing_delegate_map_;
152 content::NotificationRegistrar registrar_;
154 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
155 extension_registry_observer_;
157 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
159 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
162 } // namespace extensions
164 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_