OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "modules/bluetooth/Bluetooth.h" | 5 #include "modules/bluetooth/Bluetooth.h" |
6 | 6 |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/Document.h" |
11 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "core/frame/LocalFrame.h" |
12 #include "modules/bluetooth/BluetoothDevice.h" | 15 #include "modules/bluetooth/BluetoothDevice.h" |
13 #include "modules/bluetooth/BluetoothError.h" | 16 #include "modules/bluetooth/BluetoothError.h" |
14 #include "modules/bluetooth/BluetoothSupplement.h" | 17 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
15 #include "modules/bluetooth/BluetoothUUID.h" | 18 #include "modules/bluetooth/BluetoothUUID.h" |
16 #include "modules/bluetooth/RequestDeviceOptions.h" | 19 #include "modules/bluetooth/RequestDeviceOptions.h" |
17 #include "platform/UserGestureIndicator.h" | 20 #include "platform/UserGestureIndicator.h" |
18 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 21 #include "public/platform/InterfaceProvider.h" |
19 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" | |
20 #include <memory> | 22 #include <memory> |
21 #include <utility> | 23 #include <utility> |
22 | 24 |
23 namespace blink { | 25 namespace blink { |
24 | 26 |
25 namespace { | 27 namespace { |
26 // A device name can never be longer than 29 bytes. A adv packet is at most | 28 // A device name can never be longer than 29 bytes. A adv packet is at most |
27 // 31 bytes long. The length and identifier of the length field take 2 bytes. | 29 // 31 bytes long. The length and identifier of the length field take 2 bytes. |
28 // That least 29 bytes for the name. | 30 // That least 29 bytes for the name. |
29 const size_t kMaxFilterNameLength = 29; | 31 const size_t kMaxFilterNameLength = 29; |
30 const char kFilterNameTooLong[] = | 32 const char kFilterNameTooLong[] = |
31 "A 'name' or 'namePrefix' longer than 29 bytes results in no devices being " | 33 "A 'name' or 'namePrefix' longer than 29 bytes results in no devices being " |
32 "found, because a device can't advertise a name longer than 29 bytes."; | 34 "found, because a device can't advertise a name longer than 29 bytes."; |
33 // Per the Bluetooth Spec: The name is a user-friendly name associated with the | 35 // Per the Bluetooth Spec: The name is a user-friendly name associated with the |
34 // device and consists of a maximum of 248 bytes coded according to the UTF-8 | 36 // device and consists of a maximum of 248 bytes coded according to the UTF-8 |
35 // standard. | 37 // standard. |
36 const size_t kMaxDeviceNameLength = 248; | 38 const size_t kMaxDeviceNameLength = 248; |
37 const char kDeviceNameTooLong[] = | 39 const char kDeviceNameTooLong[] = |
38 "A device name can't be longer than 248 bytes."; | 40 "A device name can't be longer than 248 bytes."; |
39 } // namespace | 41 } // namespace |
40 | 42 |
41 static void canonicalizeFilter(const BluetoothScanFilterInit& filter, | 43 static void canonicalizeFilter( |
42 WebBluetoothScanFilter& canonicalizedFilter, | 44 const BluetoothScanFilterInit& filter, |
43 ExceptionState& exceptionState) { | 45 mojom::blink::WebBluetoothScanFilterPtr& canonicalizedFilter, |
| 46 ExceptionState& exceptionState) { |
44 if (!(filter.hasServices() || filter.hasName() || filter.hasNamePrefix())) { | 47 if (!(filter.hasServices() || filter.hasName() || filter.hasNamePrefix())) { |
45 exceptionState.throwTypeError( | 48 exceptionState.throwTypeError( |
46 "A filter must restrict the devices in some way."); | 49 "A filter must restrict the devices in some way."); |
47 return; | 50 return; |
48 } | 51 } |
49 | 52 |
50 if (filter.hasServices()) { | 53 if (filter.hasServices()) { |
51 if (filter.services().size() == 0) { | 54 if (filter.services().size() == 0) { |
52 exceptionState.throwTypeError( | 55 exceptionState.throwTypeError( |
53 "'services', if present, must contain at least one service."); | 56 "'services', if present, must contain at least one service."); |
54 return; | 57 return; |
55 } | 58 } |
56 Vector<WebString> services; | 59 canonicalizedFilter->services.emplace(); |
57 for (const StringOrUnsignedLong& service : filter.services()) { | 60 for (const StringOrUnsignedLong& service : filter.services()) { |
58 const String& validatedService = | 61 const String& validatedService = |
59 BluetoothUUID::getService(service, exceptionState); | 62 BluetoothUUID::getService(service, exceptionState); |
60 if (exceptionState.hadException()) | 63 if (exceptionState.hadException()) |
61 return; | 64 return; |
62 services.append(validatedService); | 65 bluetooth::mojom::blink::UUIDPtr uuid = |
| 66 BluetoothUUID::createMojoUuid(validatedService); |
| 67 canonicalizedFilter->services->append(std::move(uuid)); |
63 } | 68 } |
64 canonicalizedFilter.services.assign(services); | |
65 } | 69 } |
66 | 70 |
67 canonicalizedFilter.hasName = filter.hasName(); | |
68 if (filter.hasName()) { | 71 if (filter.hasName()) { |
69 size_t nameLength = filter.name().utf8().length(); | 72 size_t nameLength = filter.name().utf8().length(); |
70 if (nameLength > kMaxDeviceNameLength) { | 73 if (nameLength > kMaxDeviceNameLength) { |
71 exceptionState.throwTypeError(kDeviceNameTooLong); | 74 exceptionState.throwTypeError(kDeviceNameTooLong); |
72 return; | 75 return; |
73 } | 76 } |
74 if (nameLength > kMaxFilterNameLength) { | 77 if (nameLength > kMaxFilterNameLength) { |
75 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong); | 78 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong); |
76 return; | 79 return; |
77 } | 80 } |
78 canonicalizedFilter.name = filter.name(); | 81 canonicalizedFilter->name = filter.name(); |
79 } | 82 } |
80 | 83 |
81 if (filter.hasNamePrefix()) { | 84 if (filter.hasNamePrefix()) { |
82 size_t namePrefixLength = filter.namePrefix().utf8().length(); | 85 size_t namePrefixLength = filter.namePrefix().utf8().length(); |
83 if (namePrefixLength > kMaxDeviceNameLength) { | 86 if (namePrefixLength > kMaxDeviceNameLength) { |
84 exceptionState.throwTypeError(kDeviceNameTooLong); | 87 exceptionState.throwTypeError(kDeviceNameTooLong); |
85 return; | 88 return; |
86 } | 89 } |
87 if (namePrefixLength > kMaxFilterNameLength) { | 90 if (namePrefixLength > kMaxFilterNameLength) { |
88 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong); | 91 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong); |
89 return; | 92 return; |
90 } | 93 } |
91 if (filter.namePrefix().length() == 0) { | 94 if (filter.namePrefix().length() == 0) { |
92 exceptionState.throwTypeError( | 95 exceptionState.throwTypeError( |
93 "'namePrefix', if present, must me non-empty."); | 96 "'namePrefix', if present, must me non-empty."); |
94 return; | 97 return; |
95 } | 98 } |
96 canonicalizedFilter.namePrefix = filter.namePrefix(); | 99 canonicalizedFilter->name_prefix = filter.namePrefix(); |
97 } | 100 } |
98 } | 101 } |
99 | 102 |
100 static void convertRequestDeviceOptions(const RequestDeviceOptions& options, | 103 static void convertRequestDeviceOptions( |
101 WebRequestDeviceOptions& result, | 104 const RequestDeviceOptions& options, |
102 ExceptionState& exceptionState) { | 105 mojom::blink::WebBluetoothRequestDeviceOptionsPtr& result, |
| 106 ExceptionState& exceptionState) { |
103 if (!(options.hasFilters() ^ options.acceptAllDevices())) { | 107 if (!(options.hasFilters() ^ options.acceptAllDevices())) { |
104 exceptionState.throwTypeError( | 108 exceptionState.throwTypeError( |
105 "Either 'filters' should be present or 'acceptAllDevices' should be " | 109 "Either 'filters' should be present or 'acceptAllDevices' should be " |
106 "true, but not both."); | 110 "true, but not both."); |
107 return; | 111 return; |
108 } | 112 } |
109 | 113 |
110 result.acceptAllDevices = options.acceptAllDevices(); | 114 result->accept_all_devices = options.acceptAllDevices(); |
111 | 115 |
112 result.hasFilters = options.hasFilters(); | 116 if (options.hasFilters()) { |
113 if (result.hasFilters) { | |
114 if (options.filters().isEmpty()) { | 117 if (options.filters().isEmpty()) { |
115 exceptionState.throwTypeError( | 118 exceptionState.throwTypeError( |
116 "'filters' member must be non-empty to find any devices."); | 119 "'filters' member must be non-empty to find any devices."); |
117 return; | 120 return; |
118 } | 121 } |
119 | 122 |
120 Vector<WebBluetoothScanFilter> filters; | 123 result->filters.emplace(); |
| 124 |
121 for (const BluetoothScanFilterInit& filter : options.filters()) { | 125 for (const BluetoothScanFilterInit& filter : options.filters()) { |
122 WebBluetoothScanFilter canonicalizedFilter = WebBluetoothScanFilter(); | 126 auto canonicalizedFilter = mojom::blink::WebBluetoothScanFilter::New(); |
123 | 127 |
124 canonicalizeFilter(filter, canonicalizedFilter, exceptionState); | 128 canonicalizeFilter(filter, canonicalizedFilter, exceptionState); |
125 | 129 |
126 if (exceptionState.hadException()) | 130 if (exceptionState.hadException()) |
127 return; | 131 return; |
128 | 132 |
129 filters.append(canonicalizedFilter); | 133 result->filters.value().append(std::move(canonicalizedFilter)); |
130 } | 134 } |
131 | |
132 result.filters.assign(filters); | |
133 } | 135 } |
134 | 136 |
135 if (options.hasOptionalServices()) { | 137 if (options.hasOptionalServices()) { |
136 Vector<WebString> optionalServices; | |
137 for (const StringOrUnsignedLong& optionalService : | 138 for (const StringOrUnsignedLong& optionalService : |
138 options.optionalServices()) { | 139 options.optionalServices()) { |
139 const String& validatedOptionalService = | 140 const String& validatedOptionalService = |
140 BluetoothUUID::getService(optionalService, exceptionState); | 141 BluetoothUUID::getService(optionalService, exceptionState); |
141 if (exceptionState.hadException()) | 142 if (exceptionState.hadException()) |
142 return; | 143 return; |
143 optionalServices.append(validatedOptionalService); | 144 bluetooth::mojom::blink::UUIDPtr uuid = |
| 145 BluetoothUUID::createMojoUuid(validatedOptionalService); |
| 146 result->optional_services.append(std::move(uuid)); |
144 } | 147 } |
145 result.optionalServices.assign(optionalServices); | |
146 } | 148 } |
147 } | 149 } |
148 | 150 |
149 class RequestDeviceCallback : public WebBluetoothRequestDeviceCallbacks { | 151 void Bluetooth::dispose() { |
150 public: | 152 // The pipe to this object must be closed when is marked unreachable to |
151 RequestDeviceCallback(Bluetooth* bluetooth, ScriptPromiseResolver* resolver) | 153 // prevent messages from being dispatched before lazy sweeping. |
152 : m_bluetooth(bluetooth), m_resolver(resolver) {} | 154 m_clientBinding.Close(); |
| 155 } |
153 | 156 |
154 void onSuccess(std::unique_ptr<WebBluetoothDeviceInit> deviceInit) override { | 157 void Bluetooth::RequestDeviceCallback( |
155 if (!m_resolver->getExecutionContext() || | 158 ScriptPromiseResolver* resolver, |
156 m_resolver->getExecutionContext()->isContextDestroyed()) | 159 mojom::blink::WebBluetoothResult result, |
157 return; | 160 mojom::blink::WebBluetoothDevicePtr device) { |
| 161 if (!resolver->getExecutionContext() || |
| 162 resolver->getExecutionContext()->isContextDestroyed()) |
| 163 return; |
158 | 164 |
159 BluetoothDevice* device = m_bluetooth->getBluetoothDeviceRepresentingDevice( | 165 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
160 std::move(deviceInit), m_resolver); | 166 BluetoothDevice* bluetoothDevice = getBluetoothDeviceRepresentingDevice( |
161 | 167 device->id->device_id, device->name, resolver); |
162 m_resolver->resolve(device); | 168 resolver->resolve(bluetoothDevice); |
| 169 } else { |
| 170 resolver->reject(BluetoothError::take(resolver, result)); |
163 } | 171 } |
164 | 172 } |
165 void onError( | |
166 int32_t | |
167 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | |
168 override { | |
169 if (!m_resolver->getExecutionContext() || | |
170 m_resolver->getExecutionContext()->isContextDestroyed()) | |
171 return; | |
172 m_resolver->reject(BluetoothError::take(m_resolver, error)); | |
173 } | |
174 | |
175 private: | |
176 Persistent<Bluetooth> m_bluetooth; | |
177 Persistent<ScriptPromiseResolver> m_resolver; | |
178 }; | |
179 | 173 |
180 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice | 174 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice |
181 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, | 175 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, |
182 const RequestDeviceOptions& options, | 176 const RequestDeviceOptions& options, |
183 ExceptionState& exceptionState) { | 177 ExceptionState& exceptionState) { |
184 ExecutionContext* context = scriptState->getExecutionContext(); | 178 ExecutionContext* context = scriptState->getExecutionContext(); |
185 | 179 |
186 // If the incumbent settings object is not a secure context, reject promise | 180 // If the incumbent settings object is not a secure context, reject promise |
187 // with a SecurityError and abort these steps. | 181 // with a SecurityError and abort these steps. |
188 String errorMessage; | 182 String errorMessage; |
189 if (!context->isSecureContext(errorMessage)) { | 183 if (!context->isSecureContext(errorMessage)) { |
190 return ScriptPromise::rejectWithDOMException( | 184 return ScriptPromise::rejectWithDOMException( |
191 scriptState, DOMException::create(SecurityError, errorMessage)); | 185 scriptState, DOMException::create(SecurityError, errorMessage)); |
192 } | 186 } |
193 | 187 |
194 // If the algorithm is not allowed to show a popup, reject promise with a | 188 // If the algorithm is not allowed to show a popup, reject promise with a |
195 // SecurityError and abort these steps. | 189 // SecurityError and abort these steps. |
196 if (!UserGestureIndicator::consumeUserGesture()) { | 190 if (!UserGestureIndicator::consumeUserGesture()) { |
197 return ScriptPromise::rejectWithDOMException( | 191 return ScriptPromise::rejectWithDOMException( |
198 scriptState, | 192 scriptState, |
199 DOMException::create( | 193 DOMException::create( |
200 SecurityError, | 194 SecurityError, |
201 "Must be handling a user gesture to show a permission request.")); | 195 "Must be handling a user gesture to show a permission request.")); |
202 } | 196 } |
203 | 197 |
204 WebBluetooth* webbluetooth = | 198 if (!m_service) { |
205 BluetoothSupplement::fromScriptState(scriptState); | 199 InterfaceProvider* interfaceProvider = nullptr; |
206 if (!webbluetooth) | 200 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 201 if (executionContext->isDocument()) { |
| 202 Document* document = toDocument(executionContext); |
| 203 if (document->frame()) |
| 204 interfaceProvider = document->frame()->interfaceProvider(); |
| 205 } |
| 206 |
| 207 if (interfaceProvider) |
| 208 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); |
| 209 |
| 210 if (m_service) { |
| 211 // Create an associated interface ptr and pass it to the |
| 212 // WebBluetoothService so that it can send us events without us |
| 213 // prompting. |
| 214 mojom::blink::WebBluetoothServiceClientAssociatedPtrInfo ptrInfo; |
| 215 m_clientBinding.Bind(&ptrInfo, m_service.associated_group()); |
| 216 m_service->SetClient(std::move(ptrInfo)); |
| 217 } |
| 218 } |
| 219 |
| 220 if (!m_service) { |
207 return ScriptPromise::rejectWithDOMException( | 221 return ScriptPromise::rejectWithDOMException( |
208 scriptState, DOMException::create(NotSupportedError)); | 222 scriptState, DOMException::create(NotSupportedError)); |
| 223 } |
209 | 224 |
210 // In order to convert the arguments from service names and aliases to just | 225 // In order to convert the arguments from service names and aliases to just |
211 // UUIDs, do the following substeps: | 226 // UUIDs, do the following substeps: |
212 WebRequestDeviceOptions webOptions; | 227 auto deviceOptions = mojom::blink::WebBluetoothRequestDeviceOptions::New(); |
213 convertRequestDeviceOptions(options, webOptions, exceptionState); | 228 convertRequestDeviceOptions(options, deviceOptions, exceptionState); |
| 229 |
214 if (exceptionState.hadException()) | 230 if (exceptionState.hadException()) |
215 return exceptionState.reject(scriptState); | 231 return exceptionState.reject(scriptState); |
216 | 232 |
217 // Subsequent steps are handled in the browser process. | 233 // Subsequent steps are handled in the browser process. |
218 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 234 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
219 ScriptPromise promise = resolver->promise(); | 235 ScriptPromise promise = resolver->promise(); |
220 webbluetooth->requestDevice(webOptions, | 236 |
221 new RequestDeviceCallback(this, resolver)); | 237 service()->RequestDevice( |
| 238 std::move(deviceOptions), |
| 239 convertToBaseCallback(WTF::bind(&Bluetooth::RequestDeviceCallback, |
| 240 wrapPersistent(this), |
| 241 wrapPersistent(resolver)))); |
222 return promise; | 242 return promise; |
223 } | 243 } |
224 | 244 |
| 245 void Bluetooth::addDevice(const String& deviceId, BluetoothDevice* device) { |
| 246 m_connectedDevices.add(deviceId, device); |
| 247 } |
| 248 |
| 249 void Bluetooth::removeDevice(const String& deviceId) { |
| 250 m_connectedDevices.remove(deviceId); |
| 251 } |
| 252 |
| 253 void Bluetooth::registerCharacteristicObject( |
| 254 const String& characteristicInstanceId, |
| 255 BluetoothRemoteGATTCharacteristic* characteristic) { |
| 256 m_activeCharacteristics.add(characteristicInstanceId, characteristic); |
| 257 } |
| 258 |
| 259 void Bluetooth::characteristicObjectRemoved( |
| 260 const String& characteristicInstanceId) { |
| 261 m_activeCharacteristics.remove(characteristicInstanceId); |
| 262 } |
| 263 |
225 DEFINE_TRACE(Bluetooth) { | 264 DEFINE_TRACE(Bluetooth) { |
226 visitor->trace(m_deviceInstanceMap); | 265 visitor->trace(m_deviceInstanceMap); |
| 266 visitor->trace(m_activeCharacteristics); |
| 267 visitor->trace(m_connectedDevices); |
| 268 } |
| 269 |
| 270 Bluetooth::Bluetooth() : m_clientBinding(this) {} |
| 271 |
| 272 void Bluetooth::RemoteCharacteristicValueChanged( |
| 273 const WTF::String& characteristicInstanceId, |
| 274 const WTF::Vector<uint8_t>& value) { |
| 275 BluetoothRemoteGATTCharacteristic* characteristic = |
| 276 m_activeCharacteristics.get(characteristicInstanceId); |
| 277 if (characteristic) |
| 278 characteristic->dispatchCharacteristicValueChanged(value); |
| 279 } |
| 280 |
| 281 void Bluetooth::GattServerDisconnected( |
| 282 mojom::blink::WebBluetoothDeviceIdPtr deviceId) { |
| 283 BluetoothDevice* device = m_connectedDevices.get(deviceId->device_id); |
| 284 if (device) { |
| 285 // Remove device from the map before calling dispatchGattServerDisconnected |
| 286 // to avoid removing a device the gattserverdisconnected event handler might |
| 287 // have re-connected. |
| 288 m_connectedDevices.remove(deviceId->device_id); |
| 289 device->dispatchGattServerDisconnected(); |
| 290 } |
227 } | 291 } |
228 | 292 |
229 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( | 293 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( |
230 std::unique_ptr<WebBluetoothDeviceInit> deviceInit, | 294 const String& id, |
| 295 const String& name, |
231 ScriptPromiseResolver* resolver) { | 296 ScriptPromiseResolver* resolver) { |
232 BluetoothDevice* device = m_deviceInstanceMap.get(deviceInit->id); | 297 BluetoothDevice* device = m_deviceInstanceMap.get(id); |
233 if (!device) { | 298 if (!device) { |
234 String deviceId = deviceInit->id; | 299 device = BluetoothDevice::take(resolver, id, name, this); |
235 device = BluetoothDevice::take(resolver, std::move(deviceInit)); | 300 auto result = m_deviceInstanceMap.add(id, device); |
236 | |
237 auto result = m_deviceInstanceMap.add(deviceId, device); | |
238 DCHECK(result.isNewEntry); | 301 DCHECK(result.isNewEntry); |
239 } | 302 } |
240 return device; | 303 return device; |
241 } | 304 } |
242 | 305 |
243 } // namespace blink | 306 } // namespace blink |
OLD | NEW |