| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/bluetooth/Bluetooth.h" | 6 #include "modules/bluetooth/Bluetooth.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "modules/bluetooth/BluetoothDevice.h" | 13 #include "modules/bluetooth/BluetoothDevice.h" |
| 14 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
| 15 #include "modules/bluetooth/BluetoothSupplement.h" | 15 #include "modules/bluetooth/BluetoothSupplement.h" |
| 16 #include "modules/bluetooth/BluetoothUUID.h" | 16 #include "modules/bluetooth/BluetoothUUID.h" |
| 17 #include "modules/bluetooth/RequestDeviceOptions.h" | 17 #include "modules/bluetooth/RequestDeviceOptions.h" |
| 18 #include "platform/UserGestureIndicator.h" | 18 #include "platform/UserGestureIndicator.h" |
| 19 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 19 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 20 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" | 20 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 | 23 |
| 24 // Returns a DOMException if the conversion fails, or null if it succeeds. | 24 static void canonicalizeFilter(const BluetoothScanFilter& filter, WebBluetoothSc
anFilter& canonicalizedFilter, ExceptionState& exceptionState) |
| 25 { |
| 26 if (!(filter.hasServices() || filter.hasName() || filter.hasNamePrefix())) { |
| 27 exceptionState.throwTypeError( |
| 28 "A filter must restrict the devices in some way."); |
| 29 return; |
| 30 } |
| 31 |
| 32 if (filter.hasServices()) { |
| 33 if (filter.services().size() == 0) { |
| 34 exceptionState.throwTypeError( |
| 35 "'services', if present, must contain at least one service."); |
| 36 return; |
| 37 } |
| 38 Vector<WebString> services; |
| 39 for (const StringOrUnsignedLong& service : filter.services()) { |
| 40 const String& validatedService = BluetoothUUID::getService(service,
exceptionState); |
| 41 if (exceptionState.hadException()) |
| 42 return; |
| 43 services.append(validatedService); |
| 44 } |
| 45 canonicalizedFilter.services.assign(services); |
| 46 } |
| 47 |
| 48 if (filter.hasName()) { |
| 49 canonicalizedFilter.name = filter.name(); |
| 50 } |
| 51 |
| 52 if (filter.hasNamePrefix()) { |
| 53 if (filter.namePrefix().length() == 0) { |
| 54 exceptionState.throwTypeError( |
| 55 "'namePrefix', if present, must me non-empty."); |
| 56 return; |
| 57 } |
| 58 canonicalizedFilter.namePrefix = filter.namePrefix(); |
| 59 } |
| 60 } |
| 61 |
| 25 static void convertRequestDeviceOptions(const RequestDeviceOptions& options, Web
RequestDeviceOptions& result, ExceptionState& exceptionState) | 62 static void convertRequestDeviceOptions(const RequestDeviceOptions& options, Web
RequestDeviceOptions& result, ExceptionState& exceptionState) |
| 26 { | 63 { |
| 27 if (options.hasFilters()) { | 64 ASSERT(options.hasFilters()); |
| 28 Vector<WebBluetoothScanFilter> filters; | 65 |
| 29 for (const BluetoothScanFilter& filter : options.filters()) { | 66 Vector<WebBluetoothScanFilter> filters; |
| 30 Vector<WebString> services; | 67 for (const BluetoothScanFilter& filter : options.filters()) { |
| 31 for (const StringOrUnsignedLong& service : filter.services()) { | 68 WebBluetoothScanFilter canonicalizedFilter = WebBluetoothScanFilter(); |
| 32 const String& validatedService = BluetoothUUID::getService(servi
ce, exceptionState); | 69 |
| 33 if (exceptionState.hadException()) | 70 canonicalizeFilter(filter, canonicalizedFilter, exceptionState); |
| 34 return; | 71 |
| 35 services.append(validatedService); | 72 if (exceptionState.hadException()) |
| 36 } | 73 return; |
| 37 filters.append(WebBluetoothScanFilter(services)); | 74 |
| 38 } | 75 filters.append(canonicalizedFilter); |
| 39 result.filters.assign(filters); | |
| 40 } | 76 } |
| 77 |
| 78 result.filters.assign(filters); |
| 79 |
| 41 if (options.hasOptionalServices()) { | 80 if (options.hasOptionalServices()) { |
| 42 Vector<WebString> optionalServices; | 81 Vector<WebString> optionalServices; |
| 43 for (const StringOrUnsignedLong& optionalService : options.optionalServi
ces()) { | 82 for (const StringOrUnsignedLong& optionalService : options.optionalServi
ces()) { |
| 44 const String& validatedOptionalService = BluetoothUUID::getService(o
ptionalService, exceptionState); | 83 const String& validatedOptionalService = BluetoothUUID::getService(o
ptionalService, exceptionState); |
| 45 if (exceptionState.hadException()) | 84 if (exceptionState.hadException()) |
| 46 return; | 85 return; |
| 47 optionalServices.append(validatedOptionalService); | 86 optionalServices.append(validatedOptionalService); |
| 48 } | 87 } |
| 49 result.optionalServices.assign(optionalServices); | 88 result.optionalServices.assign(optionalServices); |
| 50 } | 89 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 76 | 115 |
| 77 // Subsequent steps are handled in the browser process. | 116 // Subsequent steps are handled in the browser process. |
| 78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 117 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 79 ScriptPromise promise = resolver->promise(); | 118 ScriptPromise promise = resolver->promise(); |
| 80 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth
Device, BluetoothError>(resolver)); | 119 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth
Device, BluetoothError>(resolver)); |
| 81 return promise; | 120 return promise; |
| 82 | 121 |
| 83 } | 122 } |
| 84 | 123 |
| 85 } // namespace blink | 124 } // namespace blink |
| OLD | NEW |