OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "chrome/common/extensions/api/bluetooth.h" | 13 #include "chrome/common/extensions/api/bluetooth.h" |
| 14 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" |
14 #include "device/bluetooth/bluetooth_adapter.h" | 16 #include "device/bluetooth/bluetooth_adapter.h" |
15 #include "device/bluetooth/bluetooth_adapter_factory.h" | 17 #include "device/bluetooth/bluetooth_adapter_factory.h" |
16 #include "device/bluetooth/bluetooth_socket.h" | 18 #include "device/bluetooth/bluetooth_socket.h" |
17 | 19 |
18 class Profile; | 20 class Profile; |
19 | 21 |
20 namespace device { | 22 namespace device { |
21 | 23 |
22 class BluetoothDevice; | 24 class BluetoothDevice; |
23 class BluetoothProfile; | 25 class BluetoothProfile; |
24 | 26 |
25 } // namespace device | 27 } // namespace device |
26 | 28 |
27 namespace extensions { | 29 namespace extensions { |
28 | 30 |
| 31 // Foward declarations of internal structs. |
| 32 struct ExtensionBluetoothSocketRecord; |
| 33 struct ExtensionBluetoothProfileRecord; |
| 34 |
29 class ExtensionBluetoothEventRouter | 35 class ExtensionBluetoothEventRouter |
30 : public device::BluetoothAdapter::Observer { | 36 : public device::BluetoothAdapter::Observer, |
| 37 public content::NotificationObserver { |
31 public: | 38 public: |
32 explicit ExtensionBluetoothEventRouter(Profile* profile); | 39 explicit ExtensionBluetoothEventRouter(Profile* profile); |
33 virtual ~ExtensionBluetoothEventRouter(); | 40 virtual ~ExtensionBluetoothEventRouter(); |
34 | 41 |
35 // Returns true if adapter_ has been initialized for testing or bluetooth | 42 // Returns true if adapter_ has been initialized for testing or bluetooth |
36 // adapter is available for the current platform. | 43 // adapter is available for the current platform. |
37 bool IsBluetoothSupported() const; | 44 bool IsBluetoothSupported() const; |
38 | 45 |
39 void GetAdapter( | 46 void GetAdapter( |
40 const device::BluetoothAdapterFactory::AdapterCallback& callback); | 47 const device::BluetoothAdapterFactory::AdapterCallback& callback); |
41 | 48 |
42 // Called when a bluetooth event listener is added. | 49 // Called when a bluetooth event listener is added. |
43 void OnListenerAdded(); | 50 void OnListenerAdded(); |
44 | 51 |
45 // Called when a bluetooth event listener is removed. | 52 // Called when a bluetooth event listener is removed. |
46 void OnListenerRemoved(); | 53 void OnListenerRemoved(); |
47 | 54 |
48 // Register the BluetoothSocket |socket| for use by the extensions system. | 55 // Register the BluetoothSocket |socket| for use by the extensions system. |
49 // This class will hold onto the socket for its lifetime, or until | 56 // This class will hold onto the socket for its lifetime until |
50 // ReleaseSocket is called for the socket. Returns an id for the socket. | 57 // ReleaseSocket is called for the socket, or until the extension associated |
51 int RegisterSocket(scoped_refptr<device::BluetoothSocket> socket); | 58 // with the socket is disabled/ reloaded. Returns an id for the socket. |
| 59 int RegisterSocket(const std::string& extension_id, |
| 60 scoped_refptr<device::BluetoothSocket> socket); |
52 | 61 |
53 // Release the BluetoothSocket corresponding to |id|. Returns true if | 62 // Release the BluetoothSocket corresponding to |id|. Returns true if |
54 // the socket was found and released, false otherwise. | 63 // the socket was found and released, false otherwise. |
55 bool ReleaseSocket(int id); | 64 bool ReleaseSocket(int id); |
56 | 65 |
57 // Add the BluetoothProfile |bluetooth_profile| for use by the extension | 66 // Add the BluetoothProfile |bluetooth_profile| for use by the extension |
58 // system. This class will hold onto the profile for its lifetime, or until | 67 // system. This class will hold onto the profile until RemoveProfile is |
59 // RemoveProfile is called for the profile. | 68 // called for the profile, or until the extension that added the profile |
| 69 // is disabled/reloaded. |
60 void AddProfile(const std::string& uuid, | 70 void AddProfile(const std::string& uuid, |
| 71 const std::string& extension_id, |
61 device::BluetoothProfile* bluetooth_profile); | 72 device::BluetoothProfile* bluetooth_profile); |
62 | 73 |
63 // Unregister the BluetoothProfile corersponding to |uuid| and release the | 74 // Unregister the BluetoothProfile corersponding to |uuid| and release the |
64 // object from this class. | 75 // object from this class. |
65 void RemoveProfile(const std::string& uuid); | 76 void RemoveProfile(const std::string& uuid); |
66 | 77 |
67 // Returns true if the BluetoothProfile corresponding to |uuid| is already | 78 // Returns true if the BluetoothProfile corresponding to |uuid| is already |
68 // registered. | 79 // registered. |
69 bool HasProfile(const std::string& uuid) const; | 80 bool HasProfile(const std::string& uuid) const; |
70 | 81 |
(...skipping 27 matching lines...) Expand all Loading... |
98 // Override from device::BluetoothAdapter::Observer | 109 // Override from device::BluetoothAdapter::Observer |
99 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, | 110 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, |
100 bool present) OVERRIDE; | 111 bool present) OVERRIDE; |
101 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | 112 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
102 bool has_power) OVERRIDE; | 113 bool has_power) OVERRIDE; |
103 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter, | 114 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter, |
104 bool discovering) OVERRIDE; | 115 bool discovering) OVERRIDE; |
105 virtual void DeviceAdded(device::BluetoothAdapter* adapter, | 116 virtual void DeviceAdded(device::BluetoothAdapter* adapter, |
106 device::BluetoothDevice* device) OVERRIDE; | 117 device::BluetoothDevice* device) OVERRIDE; |
107 | 118 |
| 119 // Overridden from content::NotificationObserver |
| 120 virtual void Observe(int type, |
| 121 const content::NotificationSource& source, |
| 122 const content::NotificationDetails& details) OVERRIDE; |
| 123 |
108 // Exposed for testing. | 124 // Exposed for testing. |
109 void SetAdapterForTest(device::BluetoothAdapter* adapter) { | 125 void SetAdapterForTest(device::BluetoothAdapter* adapter) { |
110 adapter_ = adapter; | 126 adapter_ = adapter; |
111 } | 127 } |
112 private: | 128 private: |
113 void InitializeAdapterIfNeeded(); | 129 void InitializeAdapterIfNeeded(); |
114 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter); | 130 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter); |
115 void MaybeReleaseAdapter(); | 131 void MaybeReleaseAdapter(); |
116 void DispatchAdapterStateEvent(); | 132 void DispatchAdapterStateEvent(); |
| 133 void CleanUpForExtension(const std::string& extension_id); |
117 | 134 |
118 bool send_discovery_events_; | 135 bool send_discovery_events_; |
119 bool responsible_for_discovery_; | 136 bool responsible_for_discovery_; |
120 | 137 |
121 Profile* profile_; | 138 Profile* profile_; |
122 scoped_refptr<device::BluetoothAdapter> adapter_; | 139 scoped_refptr<device::BluetoothAdapter> adapter_; |
123 | 140 |
124 int num_event_listeners_; | 141 int num_event_listeners_; |
125 | 142 |
126 // The next id to use for referring to a BluetoothSocket. We avoid using | 143 // The next id to use for referring to a BluetoothSocket. We avoid using |
127 // the fd of the socket because we don't want to leak that information to | 144 // the fd of the socket because we don't want to leak that information to |
128 // the extension javascript. | 145 // the extension javascript. |
129 int next_socket_id_; | 146 int next_socket_id_; |
130 | 147 |
131 typedef std::map<int, scoped_refptr<device::BluetoothSocket> > SocketMap; | 148 typedef std::map<int, ExtensionBluetoothSocketRecord> SocketMap; |
132 SocketMap socket_map_; | 149 SocketMap socket_map_; |
133 | 150 |
134 typedef ScopedVector<extensions::api::bluetooth::Device> | 151 typedef ScopedVector<extensions::api::bluetooth::Device> |
135 DeviceList; | 152 DeviceList; |
136 DeviceList discovered_devices_; | 153 DeviceList discovered_devices_; |
137 | 154 |
138 // A map that maps uuids to the BluetoothProfile objects. | 155 // A map that maps uuids to ExtensionBluetoothProfileRecord. |
139 typedef std::map<std::string, device::BluetoothProfile*> BluetoothProfileMap; | 156 typedef std::map<std::string, ExtensionBluetoothProfileRecord> |
| 157 BluetoothProfileMap; |
140 BluetoothProfileMap bluetooth_profile_map_; | 158 BluetoothProfileMap bluetooth_profile_map_; |
141 | 159 |
| 160 content::NotificationRegistrar registrar_; |
| 161 |
142 base::WeakPtrFactory<ExtensionBluetoothEventRouter> weak_ptr_factory_; | 162 base::WeakPtrFactory<ExtensionBluetoothEventRouter> weak_ptr_factory_; |
143 | 163 |
144 DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter); | 164 DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter); |
145 }; | 165 }; |
146 | 166 |
147 } // namespace extensions | 167 } // namespace extensions |
148 | 168 |
149 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 169 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
OLD | NEW |