| 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 void RequestDeviceImpl( |
| 126 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 127 const RequestDeviceCallback& callback, |
| 128 device::BluetoothAdapter* adapter); |
| 129 |
| 117 // Should only be run after the services have been discovered for | 130 // Should only be run after the services have been discovered for |
| 118 // |device_address|. | 131 // |device_address|. |
| 119 void RemoteServerGetPrimaryServiceImpl( | 132 void RemoteServerGetPrimaryServiceImpl( |
| 120 const std::string& service_uuid, | 133 const std::string& service_uuid, |
| 121 const RemoteServerGetPrimaryServiceCallback& callback, | 134 const RemoteServerGetPrimaryServiceCallback& callback, |
| 122 device::BluetoothDevice* device); | 135 device::BluetoothDevice* device); |
| 123 | 136 |
| 137 // Callbacks for BluetoothDeviceChooserController::GetDevice. |
| 138 void OnGetDeviceSuccess( |
| 139 const RequestDeviceCallback& callback, |
| 140 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, |
| 141 const std::string& device_id); |
| 142 void OnGetDeviceFailed(const RequestDeviceCallback& callback, |
| 143 blink::mojom::WebBluetoothError error); |
| 144 |
| 124 // Callbacks for BluetoothDevice::CreateGattConnection. | 145 // Callbacks for BluetoothDevice::CreateGattConnection. |
| 125 void OnCreateGATTConnectionSuccess( | 146 void OnCreateGATTConnectionSuccess( |
| 126 const std::string& device_id, | 147 const std::string& device_id, |
| 127 base::TimeTicks start_time, | 148 base::TimeTicks start_time, |
| 128 const RemoteServerConnectCallback& callback, | 149 const RemoteServerConnectCallback& callback, |
| 129 std::unique_ptr<device::BluetoothGattConnection> connection); | 150 std::unique_ptr<device::BluetoothGattConnection> connection); |
| 130 void OnCreateGATTConnectionFailed( | 151 void OnCreateGATTConnectionFailed( |
| 131 const std::string& device_id, | 152 const std::string& device_id, |
| 132 base::TimeTicks start_time, | 153 base::TimeTicks start_time, |
| 133 const RemoteServerConnectCallback& callback, | 154 const RemoteServerConnectCallback& callback, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 161 const RemoteCharacteristicStopNotificationsCallback& callback); | 182 const RemoteCharacteristicStopNotificationsCallback& callback); |
| 162 | 183 |
| 163 // Functions to query the platform cache for the bluetooth object. | 184 // Functions to query the platform cache for the bluetooth object. |
| 164 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the | 185 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the |
| 165 // cache. Otherwise result.outcome that can used to record the outcome and | 186 // 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. | 187 // 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 | 188 // 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 | 189 // renderer, record the reason and close the pipe, so it's safe to drop |
| 169 // any callbacks. | 190 // any callbacks. |
| 170 | 191 |
| 192 // Queries the platform cache for a Device with |device_id| for |origin|. |
| 193 // Fills in the |outcome| field and the |device| field if successful. |
| 194 CacheQueryResult QueryCacheForDevice(const std::string& device_id); |
| 195 |
| 171 // Queries the platform cache for a Service with |service_instance_id|. Fills | 196 // Queries the platform cache for a Service with |service_instance_id|. Fills |
| 172 // in the |outcome| field, and |device| and |service| fields if successful. | 197 // in the |outcome| field, and |device| and |service| fields if successful. |
| 173 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); | 198 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); |
| 174 | 199 |
| 175 // Queries the platform cache for a characteristic with | 200 // Queries the platform cache for a characteristic with |
| 176 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, | 201 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, |
| 177 // |service| and |characteristic| fields if successful. | 202 // |service| and |characteristic| fields if successful. |
| 178 CacheQueryResult QueryCacheForCharacteristic( | 203 CacheQueryResult QueryCacheForCharacteristic( |
| 179 const std::string& characteristic_instance_id); | 204 const std::string& characteristic_instance_id); |
| 180 | 205 |
| 181 RenderProcessHost* GetRenderProcessHost(); | 206 RenderProcessHost* GetRenderProcessHost(); |
| 182 BluetoothDispatcherHost* GetBluetoothDispatcherHost(); | 207 BluetoothAdapterFactoryWrapper& GetBluetoothAdapterFactoryWrapper(); |
| 183 void CrashRendererAndClosePipe(bad_message::BadMessageReason reason); | 208 device::BluetoothAdapter* GetAdapter(); |
| 184 url::Origin GetOrigin(); | 209 url::Origin GetOrigin(); |
| 185 | 210 |
| 186 // Clears all state (maps, sets, etc). | 211 // Clears all state (maps, sets, etc). |
| 187 void ClearState(); | 212 void ClearState(); |
| 188 | 213 |
| 214 // Used to open a BluetoothChooser and start a device discovery session. |
| 215 std::unique_ptr<BluetoothDeviceChooserController> device_chooser_controller_; |
| 216 |
| 217 // Keeps track of which devices the frame's origin is allowed to access. |
| 218 BluetoothAllowedDevicesMap allowed_devices_map_; |
| 219 |
| 189 // Maps to get the object's parent based on its instanceID. | 220 // Maps to get the object's parent based on its instanceID. |
| 190 std::unordered_map<std::string, std::string> service_id_to_device_address_; | 221 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_; | 222 std::unordered_map<std::string, std::string> characteristic_id_to_service_id_; |
| 192 | 223 |
| 193 // Map to keep track of the connected Bluetooth devices. | 224 // Map to keep track of the connected Bluetooth devices. |
| 194 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; | 225 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; |
| 195 | 226 |
| 196 // Maps a device address to callbacks that are waiting for services to | 227 // Maps a device address to callbacks that are waiting for services to |
| 197 // be discovered for that device. | 228 // be discovered for that device. |
| 198 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> | 229 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 215 mojo::Binding<blink::mojom::WebBluetoothService> binding_; | 246 mojo::Binding<blink::mojom::WebBluetoothService> binding_; |
| 216 | 247 |
| 217 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; | 248 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; |
| 218 | 249 |
| 219 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); | 250 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); |
| 220 }; | 251 }; |
| 221 | 252 |
| 222 } // namespace content | 253 } // namespace content |
| 223 | 254 |
| 224 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ | 255 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
| OLD | NEW |