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

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

Issue 2565913002: [Onion Soup] Move WebBluetoothImpl from //content/renderer/bluetooth to Blink's bluetooth module (Closed)
Patch Set: address comments Created 4 years 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 "modules/bluetooth/BluetoothRemoteGATTService.h" 5 #include "modules/bluetooth/BluetoothRemoteGATTService.h"
6 6
7 #include "bindings/core/v8/ScriptPromise.h" 7 #include "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/DOMException.h" 9 #include "core/dom/DOMException.h"
10 #include "core/dom/ExceptionCode.h" 10 #include "core/dom/ExceptionCode.h"
11 #include "core/inspector/ConsoleMessage.h" 11 #include "core/inspector/ConsoleMessage.h"
12 #include "modules/bluetooth/Bluetooth.h"
12 #include "modules/bluetooth/BluetoothError.h" 13 #include "modules/bluetooth/BluetoothError.h"
13 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h"
14 #include "modules/bluetooth/BluetoothSupplement.h"
15 #include "modules/bluetooth/BluetoothUUID.h" 15 #include "modules/bluetooth/BluetoothUUID.h"
16 #include "public/platform/modules/bluetooth/WebBluetooth.h"
17 #include "wtf/PtrUtil.h" 16 #include "wtf/PtrUtil.h"
18 #include <memory> 17 #include <memory>
19 #include <utility> 18 #include <utility>
20 19
21 namespace blink { 20 namespace blink {
22 21
23 namespace { 22 namespace {
24 23
25 const char kGATTServerDisconnected[] = 24 const char kGATTServerDisconnected[] =
26 "GATT Server disconnected while retrieving characteristics."; 25 "GATT Server disconnected while retrieving characteristics.";
27 const char kGATTServerNotConnected[] = 26 const char kGATTServerNotConnected[] =
28 "GATT Server is disconnected. Cannot retrieve characteristics."; 27 "GATT Server is disconnected. Cannot retrieve characteristics.";
29 const char kInvalidService[] = 28 const char kInvalidService[] =
30 "Service is no longer valid. Remember to retrieve the service again after " 29 "Service is no longer valid. Remember to retrieve the service again after "
31 "reconnecting."; 30 "reconnecting.";
32 31
33 } // namespace 32 } // namespace
34 33
35 BluetoothRemoteGATTService::BluetoothRemoteGATTService( 34 BluetoothRemoteGATTService::BluetoothRemoteGATTService(
36 std::unique_ptr<WebBluetoothRemoteGATTService> webService, 35 const String& serviceInstanceId,
36 const String& uuid,
37 bool isPrimary,
38 const String& deviceInstanceId,
37 BluetoothDevice* device) 39 BluetoothDevice* device)
38 : m_webService(std::move(webService)), m_device(device) { 40 : m_serviceInstanceId(serviceInstanceId),
39 DCHECK(m_webService); 41 m_uuid(uuid),
40 } 42 m_isPrimary(isPrimary),
43 m_deviceInstanceId(deviceInstanceId),
44 m_device(device) {}
41 45
42 DEFINE_TRACE(BluetoothRemoteGATTService) { 46 DEFINE_TRACE(BluetoothRemoteGATTService) {
43 visitor->trace(m_device); 47 visitor->trace(m_device);
44 } 48 }
45 49
46 // Class that allows us to resolve the promise with a single Characteristic or 50 // Callback that allows us to resolve the promise with a single Characteristic
47 // with a vector owning the characteristics. 51 // or with a vector owning the characteristics.
48 class GetCharacteristicsCallback 52 void BluetoothRemoteGATTService::GetCharacteristicsCallback(
49 : public WebBluetoothGetCharacteristicsCallbacks { 53 const String& serviceInstanceId,
50 public: 54 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
51 GetCharacteristicsCallback( 55 ScriptPromiseResolver* resolver,
52 BluetoothRemoteGATTService* service, 56 mojom::blink::WebBluetoothResult result,
53 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 57 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>>
54 ScriptPromiseResolver* resolver) 58 characteristics) {
55 : m_service(service), m_quantity(quantity), m_resolver(resolver) { 59 if (!resolver->getExecutionContext() ||
56 // We always check that the device is connected before constructing this 60 resolver->getExecutionContext()->isContextDestroyed())
57 // object. 61 return;
58 CHECK(m_service->device()->gatt()->connected());
59 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get());
60 }
61 62
62 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& 63 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
63 webCharacteristics) override {
64 if (!m_resolver->getExecutionContext() ||
65 m_resolver->getExecutionContext()->isContextDestroyed())
66 return;
67
68 // If the resolver is not in the set of ActiveAlgorithms then the frame 64 // If the resolver is not in the set of ActiveAlgorithms then the frame
69 // disconnected so we reject. 65 // disconnected so we reject.
70 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( 66 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) {
71 m_resolver.get())) { 67 resolver->reject(
72 m_resolver->reject(
73 DOMException::create(NetworkError, kGATTServerDisconnected)); 68 DOMException::create(NetworkError, kGATTServerDisconnected));
74 return; 69 return;
75 } 70 }
76 71
77 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { 72 DCHECK(characteristics);
78 DCHECK_EQ(1u, webCharacteristics.size()); 73
79 m_resolver->resolve( 74 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) {
80 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( 75 DCHECK_EQ(1u, characteristics->size());
81 m_resolver->getExecutionContext(), 76 resolver->resolve(device()->getOrCreateBluetoothRemoteGATTCharacteristic(
82 WTF::wrapUnique(webCharacteristics[0]), m_service)); 77 resolver->getExecutionContext(),
78 characteristics.value()[0]->instance_id, serviceInstanceId,
79 characteristics.value()[0]->uuid,
80 characteristics.value()[0]->properties, this));
83 return; 81 return;
84 } 82 }
85 83
86 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; 84 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics;
87 characteristics.reserveInitialCapacity(webCharacteristics.size()); 85 gattCharacteristics.reserveInitialCapacity(characteristics->size());
88 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : 86 for (const auto& characteristic : characteristics.value()) {
89 webCharacteristics) { 87 gattCharacteristics.append(
90 characteristics.append( 88 device()->getOrCreateBluetoothRemoteGATTCharacteristic(
91 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( 89 resolver->getExecutionContext(), characteristic->instance_id,
92 m_resolver->getExecutionContext(), 90 serviceInstanceId, characteristic->uuid,
93 WTF::wrapUnique(webCharacteristic), m_service)); 91 characteristic->properties, this));
94 } 92 }
95 m_resolver->resolve(characteristics); 93 resolver->resolve(gattCharacteristics);
96 } 94 } else {
97
98 void onError(
99 int32_t
100 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
101 override {
102 if (!m_resolver->getExecutionContext() ||
103 m_resolver->getExecutionContext()->isContextDestroyed())
104 return;
105
106 // If the resolver is not in the set of ActiveAlgorithms then the frame 95 // If the resolver is not in the set of ActiveAlgorithms then the frame
107 // disconnected so we reject. 96 // disconnected so we reject.
108 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( 97 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) {
Reilly Grant (use Gerrit) 2016/12/20 02:08:17 This check can be hoisted.
juncai 2016/12/20 22:33:43 Done.
109 m_resolver.get())) { 98 resolver->reject(
110 m_resolver->reject(
111 DOMException::create(NetworkError, kGATTServerDisconnected)); 99 DOMException::create(NetworkError, kGATTServerDisconnected));
112 return; 100 return;
113 } 101 }
114 102
115 m_resolver->reject(BluetoothError::take(m_resolver, error)); 103 resolver->reject(BluetoothError::take(resolver, result));
116 } 104 }
117 105 }
118 private:
119 Persistent<BluetoothRemoteGATTService> m_service;
120 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity;
121 const Persistent<ScriptPromiseResolver> m_resolver;
122 };
123 106
124 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( 107 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(
125 ScriptState* scriptState, 108 ScriptState* scriptState,
126 const StringOrUnsignedLong& characteristic, 109 const StringOrUnsignedLong& characteristic,
127 ExceptionState& exceptionState) { 110 ExceptionState& exceptionState) {
128 String characteristicUUID = 111 String characteristicUUID =
129 BluetoothUUID::getCharacteristic(characteristic, exceptionState); 112 BluetoothUUID::getCharacteristic(characteristic, exceptionState);
130 if (exceptionState.hadException()) 113 if (exceptionState.hadException())
131 return exceptionState.reject(scriptState); 114 return exceptionState.reject(scriptState);
132 115
(...skipping 20 matching lines...) Expand all
153 ScriptState* scriptState, 136 ScriptState* scriptState,
154 ExceptionState&) { 137 ExceptionState&) {
155 return getCharacteristicsImpl( 138 return getCharacteristicsImpl(
156 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); 139 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE);
157 } 140 }
158 141
159 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( 142 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(
160 ScriptState* scriptState, 143 ScriptState* scriptState,
161 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 144 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
162 String characteristicsUUID) { 145 String characteristicsUUID) {
146 // We always check that the device is connected.
163 if (!device()->gatt()->connected()) { 147 if (!device()->gatt()->connected()) {
164 return ScriptPromise::rejectWithDOMException( 148 return ScriptPromise::rejectWithDOMException(
165 scriptState, 149 scriptState,
166 DOMException::create(NetworkError, kGATTServerNotConnected)); 150 DOMException::create(NetworkError, kGATTServerNotConnected));
167 } 151 }
168 152
169 if (!device()->isValidService(m_webService->serviceInstanceID)) { 153 if (!device()->isValidService(m_serviceInstanceId)) {
170 return ScriptPromise::rejectWithDOMException( 154 return ScriptPromise::rejectWithDOMException(
171 scriptState, DOMException::create(InvalidStateError, kInvalidService)); 155 scriptState, DOMException::create(InvalidStateError, kInvalidService));
172 } 156 }
173 157
174 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 158 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
175 ScriptPromise promise = resolver->promise(); 159 ScriptPromise promise = resolver->promise();
160 device()->gatt()->AddToActiveAlgorithms(resolver);
176 161
177 WebBluetooth* webbluetooth = 162 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
178 BluetoothSupplement::fromScriptState(scriptState); 163 bluetooth::mojom::blink::UUIDPtr uuid = nullptr;
179 webbluetooth->getCharacteristics( 164 if (!characteristicsUUID.isEmpty())
180 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), 165 uuid = BluetoothUUID::createMojoUuid(characteristicsUUID);
181 characteristicsUUID, 166 service->RemoteServiceGetCharacteristics(
182 new GetCharacteristicsCallback(this, quantity, resolver)); 167 m_serviceInstanceId, quantity, std::move(uuid),
168 convertToBaseCallback(
169 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback,
170 wrapPersistent(this), m_serviceInstanceId, quantity,
171 wrapPersistent(resolver))));
183 172
184 return promise; 173 return promise;
185 } 174 }
186 175
187 } // namespace blink 176 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698