OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/id_map.h" | 12 #include "base/id_map.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 16 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
17 #include "content/browser/bluetooth/bluetooth_metrics.h" | |
18 #include "content/common/bluetooth/bluetooth_messages.h" | |
19 #include "content/public/browser/bluetooth_chooser.h" | 17 #include "content/public/browser/bluetooth_chooser.h" |
20 #include "content/public/browser/browser_message_filter.h" | 18 #include "content/public/browser/browser_message_filter.h" |
21 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
22 #include "device/bluetooth/bluetooth_gatt_connection.h" | 20 #include "device/bluetooth/bluetooth_gatt_connection.h" |
23 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 21 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
24 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 22 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
25 | 23 |
26 namespace device { | 24 namespace device { |
27 class BluetoothUUID; | 25 class BluetoothUUID; |
28 } | 26 } |
29 | 27 |
30 namespace content { | 28 namespace content { |
31 | 29 |
| 30 class WebBluetoothServiceImpl; |
| 31 |
32 struct BluetoothScanFilter; | 32 struct BluetoothScanFilter; |
| 33 struct CacheQueryResult; |
33 | 34 |
34 // Dispatches and sends bluetooth related messages sent to/from a child | 35 // Dispatches and sends bluetooth related messages sent to/from a child |
35 // process BluetoothDispatcher from/to the main browser process. | 36 // process BluetoothDispatcher from/to the main browser process. |
36 // | 37 // |
37 // Intended to be instantiated by the RenderProcessHost and installed as | 38 // Intended to be instantiated by the RenderProcessHost and installed as |
38 // a filter on the channel. BrowserMessageFilter is refcounted and typically | 39 // a filter on the channel. BrowserMessageFilter is refcounted and typically |
39 // lives as long as it is installed on a channel. | 40 // lives as long as it is installed on a channel. |
40 // | 41 // |
41 // UI Thread Note: | 42 // UI Thread Note: |
42 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI | 43 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI |
43 // thread because BluetoothAdapter and related objects live there. | 44 // thread because BluetoothAdapter and related objects live there. |
44 class CONTENT_EXPORT BluetoothDispatcherHost final | 45 class CONTENT_EXPORT BluetoothDispatcherHost final |
45 : public BrowserMessageFilter, | 46 : public BrowserMessageFilter, |
46 public device::BluetoothAdapter::Observer { | 47 public device::BluetoothAdapter::Observer { |
47 public: | 48 public: |
48 BluetoothDispatcherHost(int render_process_id); | 49 BluetoothDispatcherHost(int render_process_id); |
49 // BrowserMessageFilter: | 50 // BrowserMessageFilter: |
50 void OnDestruct() const override; | 51 void OnDestruct() const override; |
51 void OverrideThreadForMessage(const IPC::Message& message, | 52 void OverrideThreadForMessage(const IPC::Message& message, |
52 BrowserThread::ID* thread) override; | 53 BrowserThread::ID* thread) override; |
53 bool OnMessageReceived(const IPC::Message& message) override; | 54 bool OnMessageReceived(const IPC::Message& message) override; |
54 | 55 |
55 void SetBluetoothAdapterForTesting( | 56 void SetBluetoothAdapterForTesting( |
56 scoped_refptr<device::BluetoothAdapter> mock_adapter); | 57 scoped_refptr<device::BluetoothAdapter> mock_adapter); |
57 | 58 |
58 // TODO(ortuno): We temporarily make this a public struct so that | |
59 // both BluetoothDispatcherHost and WebBluetoothServiceImpl can use it, | |
60 // while we move functions from BluetoothDispatcherHost to | |
61 // WebBluetoothServiceImpl. | |
62 // https://crbug.com/508771 | |
63 struct CacheQueryResult { | |
64 CacheQueryResult(); | |
65 CacheQueryResult(CacheQueryOutcome outcome); | |
66 ~CacheQueryResult(); | |
67 blink::WebBluetoothError GetWebError() const; | |
68 device::BluetoothDevice* device = nullptr; | |
69 device::BluetoothRemoteGattService* service = nullptr; | |
70 device::BluetoothRemoteGattCharacteristic* characteristic = nullptr; | |
71 CacheQueryOutcome outcome = CacheQueryOutcome::SUCCESS; | |
72 }; | |
73 | |
74 // Queries the platform cache for a characteristic with | |
75 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, | |
76 // |service| and |characteristic| fields if successful. | |
77 CacheQueryResult QueryCacheForCharacteristic( | |
78 const url::Origin& origin, | |
79 const std::string& characteristic_instance_id); | |
80 | |
81 // Temporary functions so that WebBluetoothServices can add themselves as | 59 // Temporary functions so that WebBluetoothServices can add themselves as |
82 // observers of the Bluetooth Adapter without having to get an adapter for | 60 // observers of the Bluetooth Adapter without having to get an adapter for |
83 // themselves. | 61 // themselves. |
84 // TODO(ortuno): Remove once WebBluetoothServiceImpl gets its own adapter. | 62 // TODO(ortuno): Remove once WebBluetoothServiceImpl gets its own adapter. |
85 // https://crbug.com/508771 | 63 // https://crbug.com/508771 |
86 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); | 64 void AddAdapterObserver(device::BluetoothAdapter::Observer* observer); |
87 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); | 65 void RemoveAdapterObserver(device::BluetoothAdapter::Observer* observer); |
88 | 66 |
89 protected: | 67 protected: |
90 ~BluetoothDispatcherHost() override; | 68 ~BluetoothDispatcherHost() override; |
91 | 69 |
92 private: | 70 private: |
| 71 friend class WebBluetoothServiceImpl; |
93 friend class base::DeleteHelper<BluetoothDispatcherHost>; | 72 friend class base::DeleteHelper<BluetoothDispatcherHost>; |
94 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 73 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
95 | 74 |
96 struct RequestDeviceSession; | 75 struct RequestDeviceSession; |
97 struct PrimaryServicesRequest; | 76 struct PrimaryServicesRequest; |
98 | 77 |
99 // Map to keep track of connections. Inserting and removing connections | 78 // Map to keep track of connections. Inserting and removing connections |
100 // will update the Web Contents for the frame. Upon destruction | 79 // will update the Web Contents for the frame. Upon destruction |
101 // the map will clear Web Contents of Bluetooth connections. | 80 // the map will clear Web Contents of Bluetooth connections. |
102 struct ConnectedDevicesMap { | 81 struct ConnectedDevicesMap { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 int frame_routing_id, | 139 int frame_routing_id, |
161 const std::string& device_id); | 140 const std::string& device_id); |
162 void OnGATTServerDisconnect(int thread_id, | 141 void OnGATTServerDisconnect(int thread_id, |
163 int frame_routing_id, | 142 int frame_routing_id, |
164 const std::string& device_id); | 143 const std::string& device_id); |
165 void OnGetPrimaryService(int thread_id, | 144 void OnGetPrimaryService(int thread_id, |
166 int request_id, | 145 int request_id, |
167 int frame_routing_id, | 146 int frame_routing_id, |
168 const std::string& device_id, | 147 const std::string& device_id, |
169 const std::string& service_uuid); | 148 const std::string& service_uuid); |
170 void OnGetCharacteristic(int thread_id, | |
171 int request_id, | |
172 int frame_routing_id, | |
173 const std::string& service_instance_id, | |
174 const std::string& characteristic_uuid); | |
175 void OnGetCharacteristics(int thread_id, | |
176 int request_id, | |
177 int frame_routing_id, | |
178 const std::string& service_instance_id, | |
179 const std::string& characteristics_uuid); | |
180 | 149 |
181 // Callbacks for BluetoothDevice::OnRequestDevice. | 150 // Callbacks for BluetoothDevice::OnRequestDevice. |
182 // If necessary, the adapter must be obtained before continuing to Impl. | 151 // If necessary, the adapter must be obtained before continuing to Impl. |
183 void OnGetAdapter(base::Closure continuation, | 152 void OnGetAdapter(base::Closure continuation, |
184 scoped_refptr<device::BluetoothAdapter> adapter); | 153 scoped_refptr<device::BluetoothAdapter> adapter); |
185 void OnRequestDeviceImpl( | 154 void OnRequestDeviceImpl( |
186 int thread_id, | 155 int thread_id, |
187 int request_id, | 156 int request_id, |
188 int frame_routing_id, | 157 int frame_routing_id, |
189 const std::vector<content::BluetoothScanFilter>& filters, | 158 const std::vector<content::BluetoothScanFilter>& filters, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // cache. Otherwise result.outcome that can used to record the outcome and | 203 // cache. Otherwise result.outcome that can used to record the outcome and |
235 // result.error will contain the error that should be send to the renderer. | 204 // result.error will contain the error that should be send to the renderer. |
236 // One of the possible outcomes is BAD_RENDERER. In this case the outcome | 205 // One of the possible outcomes is BAD_RENDERER. In this case the outcome |
237 // was already recorded and since there renderer crashed there is no need to | 206 // was already recorded and since there renderer crashed there is no need to |
238 // send a response. | 207 // send a response. |
239 | 208 |
240 // Queries the platform cache for a Device with |device_id| for |origin|. | 209 // Queries the platform cache for a Device with |device_id| for |origin|. |
241 // Fills in the |outcome| field and the |device| field if successful. | 210 // Fills in the |outcome| field and the |device| field if successful. |
242 CacheQueryResult QueryCacheForDevice(const url::Origin& origin, | 211 CacheQueryResult QueryCacheForDevice(const url::Origin& origin, |
243 const std::string& device_id); | 212 const std::string& device_id); |
| 213 |
244 // Queries the platform cache for a Service with |service_instance_id|. Fills | 214 // Queries the platform cache for a Service with |service_instance_id|. Fills |
245 // in the |outcome| field, and |device| and |service| fields if successful. | 215 // in the |outcome| field, and |device| and |service| fields if successful. |
246 CacheQueryResult QueryCacheForService(const url::Origin& origin, | 216 CacheQueryResult QueryCacheForService(const url::Origin& origin, |
247 const std::string& service_instance_id); | 217 const std::string& service_instance_id); |
248 | 218 |
249 // Adds the PrimaryServicesRequest to the vector of pending services requests | 219 // Adds the PrimaryServicesRequest to the vector of pending services requests |
250 // for that device. | 220 // for that device. |
251 void AddToPendingPrimaryServicesRequest( | 221 void AddToPendingPrimaryServicesRequest( |
252 const std::string& device_address, | 222 const std::string& device_address, |
253 const PrimaryServicesRequest& request); | 223 const PrimaryServicesRequest& request); |
254 | 224 |
255 // Returns the origin for the frame with "frame_routing_id" in | 225 // Returns the origin for the frame with "frame_routing_id" in |
256 // render_process_id_. | 226 // render_process_id_. |
257 url::Origin GetOrigin(int frame_routing_id); | 227 url::Origin GetOrigin(int frame_routing_id); |
258 | 228 |
259 int render_process_id_; | 229 int render_process_id_; |
260 | 230 |
261 // Maps a (thread_id,request_id) to information about its requestDevice call, | 231 // Maps a (thread_id,request_id) to information about its requestDevice call, |
262 // including the chooser dialog. | 232 // including the chooser dialog. |
263 // An entry is added to this map in OnRequestDevice, and should be removed | 233 // An entry is added to this map in OnRequestDevice, and should be removed |
264 // again everywhere a requestDevice() reply is sent. | 234 // again everywhere a requestDevice() reply is sent. |
265 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; | 235 IDMap<RequestDeviceSession, IDMapOwnPointer> request_device_sessions_; |
266 | 236 |
267 BluetoothAllowedDevicesMap allowed_devices_map_; | 237 BluetoothAllowedDevicesMap allowed_devices_map_; |
268 | 238 |
269 // Maps to get the object's parent based on it's instanceID | 239 // Maps to get the object's parent based on it's instanceID |
270 // Map of service_instance_id to device_address. | 240 // Map of service_instance_id to device_address. |
271 std::map<std::string, std::string> service_to_device_; | 241 std::map<std::string, std::string> service_to_device_; |
272 // Map of characteristic_instance_id to service_instance_id. | |
273 std::map<std::string, std::string> characteristic_to_service_; | |
274 | 242 |
275 // Defines how long to scan for and how long to discover services for. | 243 // Defines how long to scan for and how long to discover services for. |
276 int current_delay_time_; | 244 int current_delay_time_; |
277 | 245 |
278 // A BluetoothAdapter instance representing an adapter of the system. | 246 // A BluetoothAdapter instance representing an adapter of the system. |
279 scoped_refptr<device::BluetoothAdapter> adapter_; | 247 scoped_refptr<device::BluetoothAdapter> adapter_; |
280 | 248 |
281 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; | 249 std::unordered_set<device::BluetoothAdapter::Observer*> adapter_observers_; |
282 | 250 |
283 // Automatically stops Bluetooth discovery a set amount of time after it was | 251 // Automatically stops Bluetooth discovery a set amount of time after it was |
(...skipping 16 matching lines...) Expand all Loading... |
300 // copies verify they are also used on the UI thread. | 268 // copies verify they are also used on the UI thread. |
301 base::WeakPtr<BluetoothDispatcherHost> weak_ptr_on_ui_thread_; | 269 base::WeakPtr<BluetoothDispatcherHost> weak_ptr_on_ui_thread_; |
302 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; | 270 base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_; |
303 | 271 |
304 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); | 272 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost); |
305 }; | 273 }; |
306 | 274 |
307 } // namespace content | 275 } // namespace content |
308 | 276 |
309 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ | 277 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_ |
OLD | NEW |