OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | |
7 | |
8 #include <map> | |
9 | |
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 "chrome/common/extensions/api/bluetooth.h" | |
16 #include "chrome/common/extensions/api/bluetooth_private.h" | |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 #include "device/bluetooth/bluetooth_adapter.h" | |
20 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
21 #include "extensions/browser/extension_registry_observer.h" | |
22 | |
23 namespace content { | |
24 class BrowserContext; | |
25 } | |
26 | |
27 namespace device { | |
28 | |
29 class BluetoothDevice; | |
30 class BluetoothDiscoverySession; | |
31 | |
32 } // namespace device | |
33 | |
34 namespace extensions { | |
35 class BluetoothApiPairingDelegate; | |
36 class ExtensionRegistry; | |
37 | |
38 class BluetoothEventRouter : public device::BluetoothAdapter::Observer, | |
39 public content::NotificationObserver, | |
40 public ExtensionRegistryObserver { | |
41 public: | |
42 explicit BluetoothEventRouter(content::BrowserContext* context); | |
43 virtual ~BluetoothEventRouter(); | |
44 | |
45 // Returns true if adapter_ has been initialized for testing or bluetooth | |
46 // adapter is available for the current platform. | |
47 bool IsBluetoothSupported() const; | |
48 | |
49 void GetAdapter( | |
50 const device::BluetoothAdapterFactory::AdapterCallback& callback); | |
51 | |
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); | |
60 | |
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); | |
70 | |
71 // Called when a bluetooth event listener is added. | |
72 void OnListenerAdded(); | |
73 | |
74 // Called when a bluetooth event listener is removed. | |
75 void OnListenerRemoved(); | |
76 | |
77 // Adds a pairing delegate for an extension. | |
78 void AddPairingDelegate(const std::string& extension_id); | |
79 | |
80 // Removes the pairing delegate for an extension. | |
81 void RemovePairingDelegate(const std::string& extension_id); | |
82 | |
83 // Returns the pairing delegate for an extension or NULL if it doesn't have a | |
84 // pairing delegate. | |
85 BluetoothApiPairingDelegate* GetPairingDelegate( | |
86 const std::string& extension_id); | |
87 | |
88 // Exposed for testing. | |
89 void SetAdapterForTest(device::BluetoothAdapter* adapter) { | |
90 adapter_ = adapter; | |
91 } | |
92 | |
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; | |
106 | |
107 // Overridden from content::NotificationObserver. | |
108 virtual void Observe(int type, | |
109 const content::NotificationSource& source, | |
110 const content::NotificationDetails& details) OVERRIDE; | |
111 | |
112 // Overridden from ExtensionRegistryObserver. | |
113 virtual void OnExtensionUnloaded( | |
114 content::BrowserContext* browser_context, | |
115 const Extension* extension, | |
116 UnloadedExtensionInfo::Reason reason) OVERRIDE; | |
117 | |
118 // BrowserContextKeyedAPI implementation. | |
119 static const char* service_name() { return "BluetoothEventRouter"; } | |
120 static const bool kServiceRedirectedInIncognito = true; | |
121 static const bool kServiceIsNULLWhileTesting = true; | |
122 | |
123 private: | |
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); | |
136 | |
137 content::BrowserContext* browser_context_; | |
138 scoped_refptr<device::BluetoothAdapter> adapter_; | |
139 | |
140 int num_event_listeners_; | |
141 | |
142 // A map that maps extension ids to BluetoothDiscoverySession pointers. | |
143 typedef std::map<std::string, device::BluetoothDiscoverySession*> | |
144 DiscoverySessionMap; | |
145 DiscoverySessionMap discovery_session_map_; | |
146 | |
147 // Maps an extension id to its pairing delegate. | |
148 typedef std::map<std::string, BluetoothApiPairingDelegate*> | |
149 PairingDelegateMap; | |
150 PairingDelegateMap pairing_delegate_map_; | |
151 | |
152 content::NotificationRegistrar registrar_; | |
153 | |
154 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
155 extension_registry_observer_; | |
156 | |
157 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter); | |
160 }; | |
161 | |
162 } // namespace extensions | |
163 | |
164 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | |
OLD | NEW |