OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_WEB_BLUETOOTH_SERVICE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
6 #define CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ | 6 #define CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "content/browser/bad_message.h" | 13 #include "content/browser/bad_message.h" |
14 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
14 #include "content/browser/bluetooth/cache_query_result.h" | 15 #include "content/browser/bluetooth/cache_query_result.h" |
15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
16 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
17 #include "device/bluetooth/bluetooth_adapter.h" | 18 #include "device/bluetooth/bluetooth_adapter.h" |
18 #include "device/bluetooth/bluetooth_gatt_connection.h" | 19 #include "device/bluetooth/bluetooth_gatt_connection.h" |
19 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 20 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
20 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
21 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 22 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
22 #include "mojo/public/cpp/bindings/binding.h" | 23 #include "mojo/public/cpp/bindings/binding.h" |
23 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj om.h" | 24 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj om.h" |
24 | 25 |
25 namespace url { | 26 namespace url { |
26 class Origin; | 27 class Origin; |
27 } // namespace url | 28 } // namespace url |
28 | 29 |
29 namespace content { | 30 namespace content { |
30 | 31 |
31 class BluetoothDispatcherHost; | 32 class BluetoothDeviceChooserController; |
33 class BluetoothAdapterFactoryWrapper; | |
32 class FrameConnectedBluetoothDevices; | 34 class FrameConnectedBluetoothDevices; |
33 class RenderFrameHost; | 35 class RenderFrameHost; |
34 class RenderProcessHost; | 36 class RenderProcessHost; |
35 | 37 |
36 // Implementation of Mojo WebBluetoothService located in | 38 // Implementation of Mojo WebBluetoothService located in |
37 // third_party/WebKit/public/platform/modules/bluetooth. | 39 // third_party/WebKit/public/platform/modules/bluetooth. |
38 // It handles Web Bluetooth API requests coming from Blink / renderer | 40 // It handles Web Bluetooth API requests coming from Blink / renderer |
39 // process and uses the platform abstraction of device/bluetooth. | 41 // process and uses the platform abstraction of device/bluetooth. |
40 // WebBluetoothServiceImpl is not thread-safe and should be created on the | 42 // WebBluetoothServiceImpl is not thread-safe and should be created on the |
41 // UI thread as required by device/bluetooth. | 43 // UI thread as required by device/bluetooth. |
42 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | 44 // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
43 // from the renderer when the first Web Bluetooth API request is handled. | 45 // from the renderer when the first Web Bluetooth API request is handled. |
44 // RenderFrameHostImpl will create an instance of this class and keep | 46 // RenderFrameHostImpl will create an instance of this class and keep |
45 // ownership of it. | 47 // ownership of it. |
46 class WebBluetoothServiceImpl : public blink::mojom::WebBluetoothService, | 48 class WebBluetoothServiceImpl : public blink::mojom::WebBluetoothService, |
47 public WebContentsObserver, | 49 public WebContentsObserver, |
48 public device::BluetoothAdapter::Observer { | 50 public device::BluetoothAdapter::Observer { |
49 public: | 51 public: |
50 // |render_frame_host|: The RFH that owns this instance. | 52 // |render_frame_host|: The RFH that owns this instance. |
51 // |request|: The instance will be bound to this request's pipe. | 53 // |request|: The instance will be bound to this request's pipe. |
52 WebBluetoothServiceImpl(RenderFrameHost* render_frame_host, | 54 WebBluetoothServiceImpl(RenderFrameHost* render_frame_host, |
53 blink::mojom::WebBluetoothServiceRequest request); | 55 blink::mojom::WebBluetoothServiceRequest request); |
54 ~WebBluetoothServiceImpl() override; | 56 ~WebBluetoothServiceImpl() override; |
55 | 57 |
58 void CrashRendererAndClosePipe(bad_message::BadMessageReason reason); | |
59 | |
56 // Sets the connection error handler for WebBluetoothServiceImpl's Binding. | 60 // Sets the connection error handler for WebBluetoothServiceImpl's Binding. |
57 void SetClientConnectionErrorHandler(base::Closure closure); | 61 void SetClientConnectionErrorHandler(base::Closure closure); |
58 | 62 |
59 private: | 63 private: |
60 typedef base::Callback<void(device::BluetoothDevice*)> | 64 typedef base::Callback<void(device::BluetoothDevice*)> |
61 PrimaryServicesRequestCallback; | 65 PrimaryServicesRequestCallback; |
62 | 66 |
63 // WebContentsObserver: | 67 // WebContentsObserver: |
64 // These functions should always check that the affected RenderFrameHost | 68 // These functions should always check that the affected RenderFrameHost |
65 // is this->render_frame_host_ and not some other frame in the same tab. | 69 // is this->render_frame_host_ and not some other frame in the same tab. |
66 void DidFinishNavigation(NavigationHandle* navigation_handle) override; | 70 void DidFinishNavigation(NavigationHandle* navigation_handle) override; |
67 | 71 |
68 // BluetoothAdapter::Observer: | 72 // BluetoothAdapter::Observer: |
69 void AdapterPresentChanged(device::BluetoothAdapter* adapter, | 73 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
70 bool present) override; | 74 bool powered) override; |
75 void DeviceAdded(device::BluetoothAdapter* adapter, | |
76 device::BluetoothDevice* device) override; | |
71 void DeviceChanged(device::BluetoothAdapter* adapter, | 77 void DeviceChanged(device::BluetoothAdapter* adapter, |
72 device::BluetoothDevice* device) override; | 78 device::BluetoothDevice* device) override; |
73 void GattServicesDiscovered(device::BluetoothAdapter* adapter, | 79 void GattServicesDiscovered(device::BluetoothAdapter* adapter, |
74 device::BluetoothDevice* device) override; | 80 device::BluetoothDevice* device) override; |
75 void GattCharacteristicValueChanged( | 81 void GattCharacteristicValueChanged( |
76 device::BluetoothAdapter* adapter, | 82 device::BluetoothAdapter* adapter, |
77 device::BluetoothRemoteGattCharacteristic* characteristic, | 83 device::BluetoothRemoteGattCharacteristic* characteristic, |
78 const std::vector<uint8_t>& value) override; | 84 const std::vector<uint8_t>& value) override; |
79 | 85 |
80 // Notifies the WebBluetoothServiceClient that characteristic | 86 // Notifies the WebBluetoothServiceClient that characteristic |
81 // |characteristic_instance_id| changed it's value. We only do this for | 87 // |characteristic_instance_id| changed it's value. We only do this for |
82 // characteristics that have been returned to the client in the past. | 88 // characteristics that have been returned to the client in the past. |
83 void NotifyCharacteristicValueChanged( | 89 void NotifyCharacteristicValueChanged( |
84 const std::string& characteristic_instance_id, | 90 const std::string& characteristic_instance_id, |
85 std::vector<uint8_t> value); | 91 std::vector<uint8_t> value); |
86 | 92 |
87 // WebBluetoothService methods: | 93 // WebBluetoothService methods: |
88 void SetClient( | 94 void SetClient( |
89 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) override; | 95 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) override; |
96 void RequestDevice(blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
97 const RequestDeviceCallback& callback) override; | |
90 void RemoteServerConnect( | 98 void RemoteServerConnect( |
91 const mojo::String& device_id, | 99 const mojo::String& device_id, |
92 const RemoteServerConnectCallback& callback) override; | 100 const RemoteServerConnectCallback& callback) override; |
93 void RemoteServerDisconnect(const mojo::String& device_id) override; | 101 void RemoteServerDisconnect(const mojo::String& device_id) override; |
94 void RemoteServerGetPrimaryService( | 102 void RemoteServerGetPrimaryService( |
95 const mojo::String& device_id, | 103 const mojo::String& device_id, |
96 const mojo::String& service_uuid, | 104 const mojo::String& service_uuid, |
97 const RemoteServerGetPrimaryServiceCallback& callback) override; | 105 const RemoteServerGetPrimaryServiceCallback& callback) override; |
98 void RemoteServiceGetCharacteristics( | 106 void RemoteServiceGetCharacteristics( |
99 const mojo::String& service_instance_id, | 107 const mojo::String& service_instance_id, |
100 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | 108 blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
101 const mojo::String& characteristics_uuid, | 109 const mojo::String& characteristics_uuid, |
102 const RemoteServiceGetCharacteristicsCallback& callback) override; | 110 const RemoteServiceGetCharacteristicsCallback& callback) override; |
103 void RemoteCharacteristicReadValue( | 111 void RemoteCharacteristicReadValue( |
104 const mojo::String& characteristic_instance_id, | 112 const mojo::String& characteristic_instance_id, |
105 const RemoteCharacteristicReadValueCallback& callback) override; | 113 const RemoteCharacteristicReadValueCallback& callback) override; |
106 void RemoteCharacteristicWriteValue( | 114 void RemoteCharacteristicWriteValue( |
107 const mojo::String& characteristic_instance_id, | 115 const mojo::String& characteristic_instance_id, |
108 mojo::Array<uint8_t> value, | 116 mojo::Array<uint8_t> value, |
109 const RemoteCharacteristicWriteValueCallback& callback) override; | 117 const RemoteCharacteristicWriteValueCallback& callback) override; |
110 void RemoteCharacteristicStartNotifications( | 118 void RemoteCharacteristicStartNotifications( |
111 const mojo::String& characteristic_instance_id, | 119 const mojo::String& characteristic_instance_id, |
112 const RemoteCharacteristicStartNotificationsCallback& callback) override; | 120 const RemoteCharacteristicStartNotificationsCallback& callback) override; |
113 void RemoteCharacteristicStopNotifications( | 121 void RemoteCharacteristicStopNotifications( |
114 const mojo::String& characteristic_instance_id, | 122 const mojo::String& characteristic_instance_id, |
115 const RemoteCharacteristicStopNotificationsCallback& callback) override; | 123 const RemoteCharacteristicStopNotificationsCallback& callback) override; |
116 | 124 |
125 // Should only be run after we've acquired an adapter from | |
Jeffrey Yasskin
2016/05/13 21:36:55
This comment is now redundant with the parameter.
ortuno
2016/05/13 22:14:38
Done.
| |
126 // BluetoothAdapterFactoryWrapper. | |
127 void RequestDeviceImpl( | |
128 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
129 const RequestDeviceCallback& callback, | |
130 device::BluetoothAdapter* adapter); | |
131 | |
117 // Should only be run after the services have been discovered for | 132 // Should only be run after the services have been discovered for |
118 // |device_address|. | 133 // |device_address|. |
119 void RemoteServerGetPrimaryServiceImpl( | 134 void RemoteServerGetPrimaryServiceImpl( |
120 const std::string& service_uuid, | 135 const std::string& service_uuid, |
121 const RemoteServerGetPrimaryServiceCallback& callback, | 136 const RemoteServerGetPrimaryServiceCallback& callback, |
122 device::BluetoothDevice* device); | 137 device::BluetoothDevice* device); |
123 | 138 |
139 // Callbacks for BluetoothDeviceChooserController::GetDevice. | |
140 void OnGetDeviceSuccess( | |
141 const RequestDeviceCallback& callback, | |
142 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
143 const std::string& device_id); | |
144 void OnGetDeviceFailed(const RequestDeviceCallback& callback, | |
145 blink::mojom::WebBluetoothError error); | |
146 | |
124 // Callbacks for BluetoothDevice::CreateGattConnection. | 147 // Callbacks for BluetoothDevice::CreateGattConnection. |
125 void OnCreateGATTConnectionSuccess( | 148 void OnCreateGATTConnectionSuccess( |
126 const std::string& device_id, | 149 const std::string& device_id, |
127 base::TimeTicks start_time, | 150 base::TimeTicks start_time, |
128 const RemoteServerConnectCallback& callback, | 151 const RemoteServerConnectCallback& callback, |
129 std::unique_ptr<device::BluetoothGattConnection> connection); | 152 std::unique_ptr<device::BluetoothGattConnection> connection); |
130 void OnCreateGATTConnectionFailed( | 153 void OnCreateGATTConnectionFailed( |
131 const std::string& device_id, | 154 const std::string& device_id, |
132 base::TimeTicks start_time, | 155 base::TimeTicks start_time, |
133 const RemoteServerConnectCallback& callback, | 156 const RemoteServerConnectCallback& callback, |
(...skipping 27 matching lines...) Expand all Loading... | |
161 const RemoteCharacteristicStopNotificationsCallback& callback); | 184 const RemoteCharacteristicStopNotificationsCallback& callback); |
162 | 185 |
163 // Functions to query the platform cache for the bluetooth object. | 186 // Functions to query the platform cache for the bluetooth object. |
164 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the | 187 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the |
165 // cache. Otherwise result.outcome that can used to record the outcome and | 188 // cache. Otherwise result.outcome that can used to record the outcome and |
166 // result.error will contain the error that should be sent to the renderer. | 189 // result.error will contain the error that should be sent to the renderer. |
167 // One of the possible outcomes is BAD_RENDERER. In this case we crash the | 190 // One of the possible outcomes is BAD_RENDERER. In this case we crash the |
168 // renderer, record the reason and close the pipe, so it's safe to drop | 191 // renderer, record the reason and close the pipe, so it's safe to drop |
169 // any callbacks. | 192 // any callbacks. |
170 | 193 |
194 // Queries the platform cache for a Device with |device_id| for |origin|. | |
195 // Fills in the |outcome| field and the |device| field if successful. | |
196 CacheQueryResult QueryCacheForDevice(const std::string& device_id); | |
197 | |
171 // Queries the platform cache for a Service with |service_instance_id|. Fills | 198 // Queries the platform cache for a Service with |service_instance_id|. Fills |
172 // in the |outcome| field, and |device| and |service| fields if successful. | 199 // in the |outcome| field, and |device| and |service| fields if successful. |
173 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); | 200 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); |
174 | 201 |
175 // Queries the platform cache for a characteristic with | 202 // Queries the platform cache for a characteristic with |
176 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, | 203 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, |
177 // |service| and |characteristic| fields if successful. | 204 // |service| and |characteristic| fields if successful. |
178 CacheQueryResult QueryCacheForCharacteristic( | 205 CacheQueryResult QueryCacheForCharacteristic( |
179 const std::string& characteristic_instance_id); | 206 const std::string& characteristic_instance_id); |
180 | 207 |
181 RenderProcessHost* GetRenderProcessHost(); | 208 RenderProcessHost* GetRenderProcessHost(); |
182 BluetoothDispatcherHost* GetBluetoothDispatcherHost(); | 209 BluetoothAdapterFactoryWrapper& GetBluetoothAdapterFactoryWrapper(); |
183 void CrashRendererAndClosePipe(bad_message::BadMessageReason reason); | 210 device::BluetoothAdapter* GetAdapter(); |
184 url::Origin GetOrigin(); | 211 url::Origin GetOrigin(); |
185 | 212 |
186 // Clears all state (maps, sets, etc). | 213 // Clears all state (maps, sets, etc). |
187 void ClearState(); | 214 void ClearState(); |
188 | 215 |
216 // Used to open a BluetoothChooser and start a device discovery session. | |
217 std::unique_ptr<BluetoothDeviceChooserController> device_chooser_controller_; | |
218 | |
219 // Keeps track of which devices the frame's origin is allowed to access. | |
220 BluetoothAllowedDevicesMap allowed_devices_map_; | |
221 | |
189 // Maps to get the object's parent based on its instanceID. | 222 // Maps to get the object's parent based on its instanceID. |
190 std::unordered_map<std::string, std::string> service_id_to_device_address_; | 223 std::unordered_map<std::string, std::string> service_id_to_device_address_; |
191 std::unordered_map<std::string, std::string> characteristic_id_to_service_id_; | 224 std::unordered_map<std::string, std::string> characteristic_id_to_service_id_; |
192 | 225 |
193 // Map to keep track of the connected Bluetooth devices. | 226 // Map to keep track of the connected Bluetooth devices. |
194 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; | 227 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; |
195 | 228 |
196 // Maps a device address to callbacks that are waiting for services to | 229 // Maps a device address to callbacks that are waiting for services to |
197 // be discovered for that device. | 230 // be discovered for that device. |
198 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> | 231 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> |
(...skipping 16 matching lines...) Expand all Loading... | |
215 mojo::Binding<blink::mojom::WebBluetoothService> binding_; | 248 mojo::Binding<blink::mojom::WebBluetoothService> binding_; |
216 | 249 |
217 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; | 250 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; |
218 | 251 |
219 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); | 252 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); |
220 }; | 253 }; |
221 | 254 |
222 } // namespace content | 255 } // namespace content |
223 | 256 |
224 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ | 257 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
OLD | NEW |