Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(759)

Side by Side Diff: third_party/WebKit/Source/modules/bluetooth/Bluetooth.cpp

Issue 1415533006: bluetooth: Implement requestDevice by name or name prefix (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-characteristic-properties
Patch Set: Merge with ToT and fix histograms conflict Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 namespace {
25 // A device name can never be longer than 29 bytes. A adv packet is at most
26 // 31 bytes long. The length and identifier of the length field take 2 bytes.
27 // That least 29 bytes for the name.
28 const size_t kMaxFilterNameLength = 29;
29 const char kFilterNameTooLong[] =
30 "A 'name' or 'namePrefix' longer than 29 bytes results in no devices being f ound, because a device can't advertise a name longer than 29 bytes.";
31 // Per the Bluetooth Spec: The name is a user-friendly name associated with the
32 // device and consists of a maximum of 248 bytes coded according to the UTF-8
33 // standard.
34 const size_t kMaxDeviceNameLength = 248;
35 const char kDeviceNameTooLong[] = "A device name can't be longer than 248 bytes. ";
36 } // namespace
37
38 static void canonicalizeFilter(const BluetoothScanFilter& filter, WebBluetoothSc anFilter& canonicalizedFilter, ExceptionState& exceptionState)
39 {
40 if (!(filter.hasServices() || filter.hasName() || filter.hasNamePrefix())) {
41 exceptionState.throwTypeError(
42 "A filter must restrict the devices in some way.");
43 return;
44 }
45
46 if (filter.hasServices()) {
47 if (filter.services().size() == 0) {
48 exceptionState.throwTypeError(
49 "'services', if present, must contain at least one service.");
50 return;
51 }
52 Vector<WebString> services;
53 for (const StringOrUnsignedLong& service : filter.services()) {
54 const String& validatedService = BluetoothUUID::getService(service, exceptionState);
55 if (exceptionState.hadException())
56 return;
57 services.append(validatedService);
58 }
59 canonicalizedFilter.services.assign(services);
60 }
61
62 if (filter.hasName()) {
63 size_t nameLength = filter.name().utf8().length();
64 if (nameLength > kMaxDeviceNameLength) {
65 exceptionState.throwTypeError(kDeviceNameTooLong);
66 return;
67 }
68 if (nameLength > kMaxFilterNameLength) {
69 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong);
70 return;
71 }
72 canonicalizedFilter.name = filter.name();
73 }
74
75 if (filter.hasNamePrefix()) {
76 size_t namePrefixLength = filter.namePrefix().utf8().length();
77 if (namePrefixLength > kMaxDeviceNameLength) {
78 exceptionState.throwTypeError(kDeviceNameTooLong);
79 return;
80 }
81 if (namePrefixLength > kMaxFilterNameLength) {
82 exceptionState.throwDOMException(NotFoundError, kFilterNameTooLong);
83 return;
84 }
85 if (filter.namePrefix().length() == 0) {
86 exceptionState.throwTypeError(
87 "'namePrefix', if present, must me non-empty.");
88 return;
89 }
90 canonicalizedFilter.namePrefix = filter.namePrefix();
91 }
92 }
93
25 static void convertRequestDeviceOptions(const RequestDeviceOptions& options, Web RequestDeviceOptions& result, ExceptionState& exceptionState) 94 static void convertRequestDeviceOptions(const RequestDeviceOptions& options, Web RequestDeviceOptions& result, ExceptionState& exceptionState)
26 { 95 {
27 if (options.hasFilters()) { 96 ASSERT(options.hasFilters());
28 Vector<WebBluetoothScanFilter> filters; 97
29 for (const BluetoothScanFilter& filter : options.filters()) { 98 Vector<WebBluetoothScanFilter> filters;
30 Vector<WebString> services; 99 for (const BluetoothScanFilter& filter : options.filters()) {
31 for (const StringOrUnsignedLong& service : filter.services()) { 100 WebBluetoothScanFilter canonicalizedFilter = WebBluetoothScanFilter();
32 const String& validatedService = BluetoothUUID::getService(servi ce, exceptionState); 101
33 if (exceptionState.hadException()) 102 canonicalizeFilter(filter, canonicalizedFilter, exceptionState);
34 return; 103
35 services.append(validatedService); 104 if (exceptionState.hadException())
36 } 105 return;
37 filters.append(WebBluetoothScanFilter(services)); 106
38 } 107 filters.append(canonicalizedFilter);
39 result.filters.assign(filters);
40 } 108 }
109
110 result.filters.assign(filters);
111
41 if (options.hasOptionalServices()) { 112 if (options.hasOptionalServices()) {
42 Vector<WebString> optionalServices; 113 Vector<WebString> optionalServices;
43 for (const StringOrUnsignedLong& optionalService : options.optionalServi ces()) { 114 for (const StringOrUnsignedLong& optionalService : options.optionalServi ces()) {
44 const String& validatedOptionalService = BluetoothUUID::getService(o ptionalService, exceptionState); 115 const String& validatedOptionalService = BluetoothUUID::getService(o ptionalService, exceptionState);
45 if (exceptionState.hadException()) 116 if (exceptionState.hadException())
46 return; 117 return;
47 optionalServices.append(validatedOptionalService); 118 optionalServices.append(validatedOptionalService);
48 } 119 }
49 result.optionalServices.assign(optionalServices); 120 result.optionalServices.assign(optionalServices);
50 } 121 }
(...skipping 25 matching lines...) Expand all
76 147
77 // Subsequent steps are handled in the browser process. 148 // Subsequent steps are handled in the browser process.
78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 149 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
79 ScriptPromise promise = resolver->promise(); 150 ScriptPromise promise = resolver->promise();
80 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth Device, BluetoothError>(resolver)); 151 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth Device, BluetoothError>(resolver));
81 return promise; 152 return promise;
82 153
83 } 154 }
84 155
85 } // namespace blink 156 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698