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 BluetoothDeviceProvider; |
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. |
(...skipping 17 matching lines...) Expand all Loading... | |
59 private: | 61 private: |
60 typedef base::Callback<void(device::BluetoothDevice*)> | 62 typedef base::Callback<void(device::BluetoothDevice*)> |
61 PrimaryServicesRequestCallback; | 63 PrimaryServicesRequestCallback; |
62 | 64 |
63 // WebContentsObserver: | 65 // WebContentsObserver: |
64 // These functions should always check that the affected RenderFrameHost | 66 // These functions should always check that the affected RenderFrameHost |
65 // is this->render_frame_host_ and not some other frame in the same tab. | 67 // is this->render_frame_host_ and not some other frame in the same tab. |
66 void DidFinishNavigation(NavigationHandle* navigation_handle) override; | 68 void DidFinishNavigation(NavigationHandle* navigation_handle) override; |
67 | 69 |
68 // BluetoothAdapter::Observer: | 70 // BluetoothAdapter::Observer: |
69 void AdapterPresentChanged(device::BluetoothAdapter* adapter, | 71 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
70 bool present) override; | 72 bool powered) override; |
73 void DeviceAdded(device::BluetoothAdapter* adapter, | |
74 device::BluetoothDevice* device) override; | |
71 void DeviceChanged(device::BluetoothAdapter* adapter, | 75 void DeviceChanged(device::BluetoothAdapter* adapter, |
72 device::BluetoothDevice* device) override; | 76 device::BluetoothDevice* device) override; |
73 void GattServicesDiscovered(device::BluetoothAdapter* adapter, | 77 void GattServicesDiscovered(device::BluetoothAdapter* adapter, |
74 device::BluetoothDevice* device) override; | 78 device::BluetoothDevice* device) override; |
75 void GattCharacteristicValueChanged( | 79 void GattCharacteristicValueChanged( |
76 device::BluetoothAdapter* adapter, | 80 device::BluetoothAdapter* adapter, |
77 device::BluetoothRemoteGattCharacteristic* characteristic, | 81 device::BluetoothRemoteGattCharacteristic* characteristic, |
78 const std::vector<uint8_t>& value) override; | 82 const std::vector<uint8_t>& value) override; |
79 | 83 |
80 // Notifies the WebBluetoothServiceClient that characteristic | 84 // Notifies the WebBluetoothServiceClient that characteristic |
81 // |characteristic_instance_id| changed it's value. We only do this for | 85 // |characteristic_instance_id| changed it's value. We only do this for |
82 // characteristics that have been returned to the client in the past. | 86 // characteristics that have been returned to the client in the past. |
83 void NotifyCharacteristicValueChanged( | 87 void NotifyCharacteristicValueChanged( |
84 const std::string& characteristic_instance_id, | 88 const std::string& characteristic_instance_id, |
85 std::vector<uint8_t> value); | 89 std::vector<uint8_t> value); |
86 | 90 |
87 // WebBluetoothService methods: | 91 // WebBluetoothService methods: |
88 void SetClient( | 92 void SetClient( |
89 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) override; | 93 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) override; |
94 void RequestDevice(blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
95 const RequestDeviceCallback& callback) override; | |
90 void RemoteServerConnect( | 96 void RemoteServerConnect( |
91 const mojo::String& device_id, | 97 const mojo::String& device_id, |
92 const RemoteServerConnectCallback& callback) override; | 98 const RemoteServerConnectCallback& callback) override; |
93 void RemoteServerDisconnect(const mojo::String& device_id) override; | 99 void RemoteServerDisconnect(const mojo::String& device_id) override; |
94 void RemoteServerGetPrimaryService( | 100 void RemoteServerGetPrimaryService( |
95 const mojo::String& device_id, | 101 const mojo::String& device_id, |
96 const mojo::String& service_uuid, | 102 const mojo::String& service_uuid, |
97 const RemoteServerGetPrimaryServiceCallback& callback) override; | 103 const RemoteServerGetPrimaryServiceCallback& callback) override; |
98 void RemoteServiceGetCharacteristics( | 104 void RemoteServiceGetCharacteristics( |
99 const mojo::String& service_instance_id, | 105 const mojo::String& service_instance_id, |
100 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | 106 blink::mojom::WebBluetoothGATTQueryQuantity quantity, |
101 const mojo::String& characteristics_uuid, | 107 const mojo::String& characteristics_uuid, |
102 const RemoteServiceGetCharacteristicsCallback& callback) override; | 108 const RemoteServiceGetCharacteristicsCallback& callback) override; |
103 void RemoteCharacteristicReadValue( | 109 void RemoteCharacteristicReadValue( |
104 const mojo::String& characteristic_instance_id, | 110 const mojo::String& characteristic_instance_id, |
105 const RemoteCharacteristicReadValueCallback& callback) override; | 111 const RemoteCharacteristicReadValueCallback& callback) override; |
106 void RemoteCharacteristicWriteValue( | 112 void RemoteCharacteristicWriteValue( |
107 const mojo::String& characteristic_instance_id, | 113 const mojo::String& characteristic_instance_id, |
108 mojo::Array<uint8_t> value, | 114 mojo::Array<uint8_t> value, |
109 const RemoteCharacteristicWriteValueCallback& callback) override; | 115 const RemoteCharacteristicWriteValueCallback& callback) override; |
110 void RemoteCharacteristicStartNotifications( | 116 void RemoteCharacteristicStartNotifications( |
111 const mojo::String& characteristic_instance_id, | 117 const mojo::String& characteristic_instance_id, |
112 const RemoteCharacteristicStartNotificationsCallback& callback) override; | 118 const RemoteCharacteristicStartNotificationsCallback& callback) override; |
113 void RemoteCharacteristicStopNotifications( | 119 void RemoteCharacteristicStopNotifications( |
114 const mojo::String& characteristic_instance_id, | 120 const mojo::String& characteristic_instance_id, |
115 const RemoteCharacteristicStopNotificationsCallback& callback) override; | 121 const RemoteCharacteristicStopNotificationsCallback& callback) override; |
116 | 122 |
123 // Should only be run after we've acquired an adapter from | |
124 // BluetoothAdapterFactoryWrapper. | |
125 void RequestDeviceImpl( | |
126 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
127 const RequestDeviceCallback& callback); | |
128 | |
117 // Should only be run after the services have been discovered for | 129 // Should only be run after the services have been discovered for |
118 // |device_address|. | 130 // |device_address|. |
119 void RemoteServerGetPrimaryServiceImpl( | 131 void RemoteServerGetPrimaryServiceImpl( |
120 const std::string& service_uuid, | 132 const std::string& service_uuid, |
121 const RemoteServerGetPrimaryServiceCallback& callback, | 133 const RemoteServerGetPrimaryServiceCallback& callback, |
122 device::BluetoothDevice* device); | 134 device::BluetoothDevice* device); |
123 | 135 |
136 // Callbacks for BluetoothDeviceProvider::GetDevice. | |
137 void OnGetDeviceSuccess( | |
138 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, | |
139 const RequestDeviceCallback& callback, | |
140 const std::string& device_id); | |
141 void OnGetDeviceFailed(const RequestDeviceCallback& callback, | |
142 blink::mojom::WebBluetoothError error); | |
143 | |
124 // Callbacks for BluetoothDevice::CreateGattConnection. | 144 // Callbacks for BluetoothDevice::CreateGattConnection. |
125 void OnCreateGATTConnectionSuccess( | 145 void OnCreateGATTConnectionSuccess( |
126 const std::string& device_id, | 146 const std::string& device_id, |
127 base::TimeTicks start_time, | 147 base::TimeTicks start_time, |
128 const RemoteServerConnectCallback& callback, | 148 const RemoteServerConnectCallback& callback, |
129 std::unique_ptr<device::BluetoothGattConnection> connection); | 149 std::unique_ptr<device::BluetoothGattConnection> connection); |
130 void OnCreateGATTConnectionFailed( | 150 void OnCreateGATTConnectionFailed( |
131 const std::string& device_id, | 151 const std::string& device_id, |
132 base::TimeTicks start_time, | 152 base::TimeTicks start_time, |
133 const RemoteServerConnectCallback& callback, | 153 const RemoteServerConnectCallback& callback, |
(...skipping 27 matching lines...) Expand all Loading... | |
161 const RemoteCharacteristicStopNotificationsCallback& callback); | 181 const RemoteCharacteristicStopNotificationsCallback& callback); |
162 | 182 |
163 // Functions to query the platform cache for the bluetooth object. | 183 // Functions to query the platform cache for the bluetooth object. |
164 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the | 184 // result.outcome == CacheQueryOutcome::SUCCESS if the object was found in the |
165 // cache. Otherwise result.outcome that can used to record the outcome and | 185 // 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. | 186 // 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 | 187 // 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 | 188 // renderer, record the reason and close the pipe, so it's safe to drop |
169 // any callbacks. | 189 // any callbacks. |
170 | 190 |
191 CacheQueryResult QueryCacheForDevice(const std::string& device_id); | |
Jeffrey Yasskin
2016/05/13 04:41:59
Comment this function like the other two.
ortuno
2016/05/13 20:11:18
Done.
| |
192 | |
171 // Queries the platform cache for a Service with |service_instance_id|. Fills | 193 // Queries the platform cache for a Service with |service_instance_id|. Fills |
172 // in the |outcome| field, and |device| and |service| fields if successful. | 194 // in the |outcome| field, and |device| and |service| fields if successful. |
173 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); | 195 CacheQueryResult QueryCacheForService(const std::string& service_instance_id); |
174 | 196 |
175 // Queries the platform cache for a characteristic with | 197 // Queries the platform cache for a characteristic with |
176 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, | 198 // |characteristic_instance_id|. Fills in the |outcome| field, and |device|, |
177 // |service| and |characteristic| fields if successful. | 199 // |service| and |characteristic| fields if successful. |
178 CacheQueryResult QueryCacheForCharacteristic( | 200 CacheQueryResult QueryCacheForCharacteristic( |
179 const std::string& characteristic_instance_id); | 201 const std::string& characteristic_instance_id); |
180 | 202 |
181 RenderProcessHost* GetRenderProcessHost(); | 203 RenderProcessHost* GetRenderProcessHost(); |
182 BluetoothDispatcherHost* GetBluetoothDispatcherHost(); | 204 BluetoothAdapterFactoryWrapper* GetBluetoothAdapterFactoryWrapper(); |
183 void CrashRendererAndClosePipe(bad_message::BadMessageReason reason); | 205 void CrashRendererAndClosePipe(bad_message::BadMessageReason reason); |
184 url::Origin GetOrigin(); | 206 url::Origin GetOrigin(); |
185 | 207 |
186 // Clears all state (maps, sets, etc). | 208 // Clears all state (maps, sets, etc). |
187 void ClearState(); | 209 void ClearState(); |
188 | 210 |
211 // Used to open a BluetoothChooser and start a device discovery session. | |
212 std::unique_ptr<BluetoothDeviceProvider> device_provider_; | |
213 | |
214 // Keeps track of which devices the frame's origin is allowed to access. | |
215 BluetoothAllowedDevicesMap allowed_devices_map_; | |
216 | |
189 // Maps to get the object's parent based on its instanceID. | 217 // Maps to get the object's parent based on its instanceID. |
190 std::unordered_map<std::string, std::string> service_id_to_device_address_; | 218 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_; | 219 std::unordered_map<std::string, std::string> characteristic_id_to_service_id_; |
192 | 220 |
193 // Map to keep track of the connected Bluetooth devices. | 221 // Map to keep track of the connected Bluetooth devices. |
194 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; | 222 std::unique_ptr<FrameConnectedBluetoothDevices> connected_devices_; |
195 | 223 |
196 // Maps a device address to callbacks that are waiting for services to | 224 // Maps a device address to callbacks that are waiting for services to |
197 // be discovered for that device. | 225 // be discovered for that device. |
198 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> | 226 std::unordered_map<std::string, std::vector<PrimaryServicesRequestCallback>> |
(...skipping 16 matching lines...) Expand all Loading... | |
215 mojo::Binding<blink::mojom::WebBluetoothService> binding_; | 243 mojo::Binding<blink::mojom::WebBluetoothService> binding_; |
216 | 244 |
217 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; | 245 base::WeakPtrFactory<WebBluetoothServiceImpl> weak_ptr_factory_; |
218 | 246 |
219 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); | 247 DISALLOW_COPY_AND_ASSIGN(WebBluetoothServiceImpl); |
220 }; | 248 }; |
221 | 249 |
222 } // namespace content | 250 } // namespace content |
223 | 251 |
224 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ | 252 #endif // CONTENT_BROWSER_BLUETOOTH_WEB_BLUETOOTH_SERVICE_IMPL_H_ |
OLD | NEW |