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

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

Issue 2565913002: [Onion Soup] Move WebBluetoothImpl from //content/renderer/bluetooth to Blink's bluetooth module (Closed)
Patch Set: check if binding is bound before close it 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/BluetoothRemoteGATTCharacteristic.h" 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.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/DOMDataView.h" 9 #include "core/dom/DOMDataView.h"
10 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/events/Event.h" 12 #include "core/events/Event.h"
13 #include "core/inspector/ConsoleMessage.h" 13 #include "core/inspector/ConsoleMessage.h"
14 #include "modules/bluetooth/Bluetooth.h"
14 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h"
16 #include "modules/bluetooth/BluetoothDevice.h"
15 #include "modules/bluetooth/BluetoothError.h" 17 #include "modules/bluetooth/BluetoothError.h"
16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" 18 #include "modules/bluetooth/BluetoothRemoteGATTService.h"
17 #include "modules/bluetooth/BluetoothSupplement.h"
18 #include "public/platform/modules/bluetooth/WebBluetooth.h"
19 #include <memory> 19 #include <memory>
20 20
21 namespace blink { 21 namespace blink {
22 22
23 namespace { 23 namespace {
24 24
25 const char kGATTServerDisconnected[] = 25 const char kGATTServerDisconnected[] =
26 "GATT Server disconnected while performing a GATT operation."; 26 "GATT Server disconnected while performing a GATT operation.";
27 const char kGATTServerNotConnected[] = 27 const char kGATTServerNotConnected[] =
28 "GATT Server is disconnected. Cannot perform GATT operations."; 28 "GATT Server is disconnected. Cannot perform GATT operations.";
29 const char kInvalidCharacteristic[] = 29 const char kInvalidCharacteristic[] =
30 "Characteristic is no longer valid. Remember to retrieve the " 30 "Characteristic is no longer valid. Remember to retrieve the "
31 "characteristic again after reconnecting."; 31 "characteristic again after reconnecting.";
32 32
33 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { 33 DOMDataView* ConvertWTFVectorToDataView(const Vector<uint8_t>& wtfVector) {
34 static_assert(sizeof(*webVector.data()) == 1, 34 static_assert(sizeof(*wtfVector.data()) == 1,
35 "uint8_t should be a single byte"); 35 "uint8_t should be a single byte");
36 DOMArrayBuffer* domBuffer = 36 DOMArrayBuffer* domBuffer =
37 DOMArrayBuffer::create(webVector.data(), webVector.size()); 37 DOMArrayBuffer::create(wtfVector.data(), wtfVector.size());
38 return DOMDataView::create(domBuffer, 0, webVector.size()); 38 return DOMDataView::create(domBuffer, 0, wtfVector.size());
39 } 39 }
40 40
41 } // anonymous namespace 41 } // anonymous namespace
42 42
43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( 43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic(
44 ExecutionContext* context, 44 ExecutionContext* context,
45 std::unique_ptr<WebBluetoothRemoteGATTCharacteristicInit> webCharacteristic, 45 const String& characteristicInstanceId,
46 BluetoothRemoteGATTService* service) 46 const String& serviceInstanceId,
47 const String& uuid,
48 uint32_t characteristicProperties,
49 BluetoothRemoteGATTService* service,
50 BluetoothDevice* device)
47 : SuspendableObject(context), 51 : SuspendableObject(context),
48 m_webCharacteristic(std::move(webCharacteristic)), 52 m_characteristicInstanceId(characteristicInstanceId),
53 m_serviceInstanceId(serviceInstanceId),
54 m_uuid(uuid),
55 m_characteristicProperties(characteristicProperties),
49 m_service(service), 56 m_service(service),
50 m_stopped(false) { 57 m_stopped(false),
51 m_properties = BluetoothCharacteristicProperties::create( 58 m_device(device) {
52 m_webCharacteristic->characteristicProperties); 59 m_properties =
60 BluetoothCharacteristicProperties::create(m_characteristicProperties);
53 } 61 }
54 62
55 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( 63 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create(
56 ExecutionContext* context, 64 ExecutionContext* context,
57 std::unique_ptr<WebBluetoothRemoteGATTCharacteristicInit> webCharacteristic, 65 const String& characteristicInstanceId,
58 BluetoothRemoteGATTService* service) { 66 const String& serviceInstanceId,
59 DCHECK(webCharacteristic); 67 const String& uuid,
60 68 uint32_t characteristicProperties,
69 BluetoothRemoteGATTService* service,
70 BluetoothDevice* device) {
61 BluetoothRemoteGATTCharacteristic* characteristic = 71 BluetoothRemoteGATTCharacteristic* characteristic =
62 new BluetoothRemoteGATTCharacteristic( 72 new BluetoothRemoteGATTCharacteristic(
63 context, std::move(webCharacteristic), service); 73 context, characteristicInstanceId, serviceInstanceId, uuid,
74 characteristicProperties, service, device);
64 // See note in SuspendableObject about suspendIfNeeded. 75 // See note in SuspendableObject about suspendIfNeeded.
65 characteristic->suspendIfNeeded(); 76 characteristic->suspendIfNeeded();
66 return characteristic; 77 return characteristic;
67 } 78 }
68 79
69 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { 80 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) {
70 m_value = domDataView; 81 m_value = domDataView;
71 } 82 }
72 83
73 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged( 84 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged(
74 const WebVector<uint8_t>& value) { 85 const Vector<uint8_t>& value) {
75 this->setValue(ConvertWebVectorToDataView(value)); 86 this->setValue(ConvertWTFVectorToDataView(value));
76 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); 87 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged));
77 } 88 }
78 89
79 void BluetoothRemoteGATTCharacteristic::contextDestroyed() { 90 void BluetoothRemoteGATTCharacteristic::contextDestroyed() {
80 notifyCharacteristicObjectRemoved(); 91 notifyCharacteristicObjectRemoved();
81 } 92 }
82 93
83 void BluetoothRemoteGATTCharacteristic::dispose() { 94 void BluetoothRemoteGATTCharacteristic::dispose() {
84 notifyCharacteristicObjectRemoved(); 95 notifyCharacteristicObjectRemoved();
85 } 96 }
86 97
87 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { 98 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() {
88 if (!m_stopped) { 99 if (!m_stopped) {
89 m_stopped = true; 100 m_stopped = true;
90 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext( 101 m_device->bluetooth()->characteristicObjectRemoved(
91 SuspendableObject::getExecutionContext()); 102 m_characteristicInstanceId);
92 webbluetooth->characteristicObjectRemoved(
93 m_webCharacteristic->characteristicInstanceID, this);
94 } 103 }
95 } 104 }
96 105
97 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() 106 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName()
98 const { 107 const {
99 return EventTargetNames::BluetoothRemoteGATTCharacteristic; 108 return EventTargetNames::BluetoothRemoteGATTCharacteristic;
100 } 109 }
101 110
102 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() 111 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext()
103 const { 112 const {
104 return SuspendableObject::getExecutionContext(); 113 return SuspendableObject::getExecutionContext();
105 } 114 }
106 115
107 void BluetoothRemoteGATTCharacteristic::addedEventListener( 116 void BluetoothRemoteGATTCharacteristic::addedEventListener(
108 const AtomicString& eventType, 117 const AtomicString& eventType,
109 RegisteredEventListener& registeredListener) { 118 RegisteredEventListener& registeredListener) {
110 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); 119 EventTargetWithInlineData::addedEventListener(eventType, registeredListener);
111 // We will also need to unregister a characteristic once all the event 120 // We will also need to unregister a characteristic once all the event
112 // listeners have been removed. See http://crbug.com/541390 121 // listeners have been removed. See http://crbug.com/541390
113 if (eventType == EventTypeNames::characteristicvaluechanged) { 122 if (eventType == EventTypeNames::characteristicvaluechanged) {
114 WebBluetooth* webbluetooth = 123 m_device->bluetooth()->registerCharacteristicObject(
115 BluetoothSupplement::fromExecutionContext(getExecutionContext()); 124 m_characteristicInstanceId, this);
116 webbluetooth->registerCharacteristicObject(
117 m_webCharacteristic->characteristicInstanceID, this);
118 } 125 }
119 } 126 }
120 127
121 class ReadValueCallback : public WebBluetoothReadValueCallbacks { 128 void BluetoothRemoteGATTCharacteristic::ReadValueCallback(
122 public: 129 ScriptPromiseResolver* resolver,
123 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, 130 mojom::blink::WebBluetoothResult result,
124 ScriptPromiseResolver* resolver) 131 const Optional<Vector<uint8_t>>& value) {
125 : m_characteristic(characteristic), m_resolver(resolver) { 132 if (!resolver->getExecutionContext() ||
126 // We always check that the device is connected before constructing this 133 resolver->getExecutionContext()->isContextDestroyed())
127 // object. 134 return;
128 CHECK(m_characteristic->gatt()->connected()); 135
129 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); 136 // If the resolver is not in the set of ActiveAlgorithms then the frame
137 // disconnected so we reject.
138 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) {
139 resolver->reject(
140 DOMException::create(NetworkError, kGATTServerDisconnected));
141 return;
130 } 142 }
131 143
132 void onSuccess(const WebVector<uint8_t>& value) override { 144 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
133 if (!m_resolver->getExecutionContext() || 145 DCHECK(value);
134 m_resolver->getExecutionContext()->isContextDestroyed()) 146 DOMDataView* domDataView = ConvertWTFVectorToDataView(value.value());
135 return; 147 setValue(domDataView);
136 148 resolver->resolve(domDataView);
137 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( 149 } else {
138 m_resolver.get())) { 150 resolver->reject(BluetoothError::take(resolver, result));
139 m_resolver->reject(
140 DOMException::create(NetworkError, kGATTServerDisconnected));
141 return;
142 }
143
144 DOMDataView* domDataView = ConvertWebVectorToDataView(value);
145 if (m_characteristic)
146 m_characteristic->setValue(domDataView);
147
148 m_resolver->resolve(domDataView);
149 } 151 }
150 152 }
151 void onError(
152 int32_t
153 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
154 override {
155 if (!m_resolver->getExecutionContext() ||
156 m_resolver->getExecutionContext()->isContextDestroyed())
157 return;
158
159 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
160 m_resolver.get())) {
161 m_resolver->reject(
162 DOMException::create(NetworkError, kGATTServerDisconnected));
163 return;
164 }
165
166 m_resolver->reject(BluetoothError::take(m_resolver, error));
167 }
168
169 private:
170 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic;
171 Persistent<ScriptPromiseResolver> m_resolver;
172 };
173 153
174 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( 154 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue(
175 ScriptState* scriptState) { 155 ScriptState* scriptState) {
156 // We always check that the device is connected.
176 if (!gatt()->connected()) { 157 if (!gatt()->connected()) {
177 return ScriptPromise::rejectWithDOMException( 158 return ScriptPromise::rejectWithDOMException(
178 scriptState, 159 scriptState,
179 DOMException::create(NetworkError, kGATTServerNotConnected)); 160 DOMException::create(NetworkError, kGATTServerNotConnected));
180 } 161 }
181 162
182 if (!gatt()->device()->isValidCharacteristic( 163 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) {
183 m_webCharacteristic->characteristicInstanceID)) {
184 return ScriptPromise::rejectWithDOMException( 164 return ScriptPromise::rejectWithDOMException(
185 scriptState, 165 scriptState,
186 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 166 DOMException::create(InvalidStateError, kInvalidCharacteristic));
187 } 167 }
188 168
189 WebBluetooth* webbluetooth =
190 BluetoothSupplement::fromScriptState(scriptState);
191
192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 169 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
193 ScriptPromise promise = resolver->promise(); 170 ScriptPromise promise = resolver->promise();
194 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, 171 gatt()->AddToActiveAlgorithms(resolver);
195 new ReadValueCallback(this, resolver)); 172
173 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
174 service->RemoteCharacteristicReadValue(
175 m_characteristicInstanceId,
176 convertToBaseCallback(
177 WTF::bind(&BluetoothRemoteGATTCharacteristic::ReadValueCallback,
178 wrapPersistent(this), wrapPersistent(resolver))));
196 179
197 return promise; 180 return promise;
198 } 181 }
199 182
200 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { 183 void BluetoothRemoteGATTCharacteristic::WriteValueCallback(
201 public: 184 ScriptPromiseResolver* resolver,
202 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, 185 const Vector<uint8_t>& value,
203 ScriptPromiseResolver* resolver) 186 mojom::blink::WebBluetoothResult result) {
204 : m_characteristic(characteristic), m_resolver(resolver) { 187 if (!resolver->getExecutionContext() ||
205 // We always check that the device is connected before constructing this 188 resolver->getExecutionContext()->isContextDestroyed())
206 // object. 189 return;
207 CHECK(m_characteristic->gatt()->connected()); 190
208 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); 191 // If the resolver is not in the set of ActiveAlgorithms then the frame
192 // disconnected so we reject.
193 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) {
194 resolver->reject(
195 DOMException::create(NetworkError, kGATTServerDisconnected));
196 return;
209 } 197 }
210 198
211 void onSuccess(const WebVector<uint8_t>& value) override { 199 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
212 if (!m_resolver->getExecutionContext() || 200 setValue(ConvertWTFVectorToDataView(value));
213 m_resolver->getExecutionContext()->isContextDestroyed()) 201 resolver->resolve();
214 return; 202 } else {
215 203 resolver->reject(BluetoothError::take(resolver, result));
216 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
217 m_resolver.get())) {
218 m_resolver->reject(
219 DOMException::create(NetworkError, kGATTServerDisconnected));
220 return;
221 }
222
223 if (m_characteristic) {
224 m_characteristic->setValue(ConvertWebVectorToDataView(value));
225 }
226 m_resolver->resolve();
227 } 204 }
228 205 }
229 void onError(
230 int32_t
231 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
232 override {
233 if (!m_resolver->getExecutionContext() ||
234 m_resolver->getExecutionContext()->isContextDestroyed())
235 return;
236
237 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
238 m_resolver.get())) {
239 m_resolver->reject(
240 DOMException::create(NetworkError, kGATTServerDisconnected));
241 return;
242 }
243
244 m_resolver->reject(BluetoothError::take(m_resolver, error));
245 }
246
247 private:
248 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic;
249 Persistent<ScriptPromiseResolver> m_resolver;
250 };
251 206
252 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( 207 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue(
253 ScriptState* scriptState, 208 ScriptState* scriptState,
254 const DOMArrayPiece& value) { 209 const DOMArrayPiece& value) {
210 // We always check that the device is connected.
255 if (!gatt()->connected()) { 211 if (!gatt()->connected()) {
256 return ScriptPromise::rejectWithDOMException( 212 return ScriptPromise::rejectWithDOMException(
257 scriptState, 213 scriptState,
258 DOMException::create(NetworkError, kGATTServerNotConnected)); 214 DOMException::create(NetworkError, kGATTServerNotConnected));
259 } 215 }
260 216
261 if (!gatt()->device()->isValidCharacteristic( 217 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) {
262 m_webCharacteristic->characteristicInstanceID)) {
263 return ScriptPromise::rejectWithDOMException( 218 return ScriptPromise::rejectWithDOMException(
264 scriptState, 219 scriptState,
265 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 220 DOMException::create(InvalidStateError, kInvalidCharacteristic));
266 } 221 }
267 222
268 WebBluetooth* webbluetooth =
269 BluetoothSupplement::fromScriptState(scriptState);
270 // Partial implementation of writeValue algorithm: 223 // Partial implementation of writeValue algorithm:
271 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue 224 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue
272 225
273 // If bytes is more than 512 bytes long (the maximum length of an attribute 226 // If bytes is more than 512 bytes long (the maximum length of an attribute
274 // value, per Long Attribute Values) return a promise rejected with an 227 // value, per Long Attribute Values) return a promise rejected with an
275 // InvalidModificationError and abort. 228 // InvalidModificationError and abort.
276 if (value.byteLength() > 512) 229 if (value.byteLength() > 512)
277 return ScriptPromise::rejectWithDOMException( 230 return ScriptPromise::rejectWithDOMException(
278 scriptState, DOMException::create(InvalidModificationError, 231 scriptState, DOMException::create(InvalidModificationError,
279 "Value can't exceed 512 bytes.")); 232 "Value can't exceed 512 bytes."));
280 233
281 // Let valueVector be a copy of the bytes held by value. 234 // Let valueVector be a copy of the bytes held by value.
282 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); 235 Vector<uint8_t> valueVector;
236 valueVector.append(value.bytes(), value.byteLength());
283 237
284 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 238 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
239 ScriptPromise promise = resolver->promise();
240 gatt()->AddToActiveAlgorithms(resolver);
285 241
286 ScriptPromise promise = resolver->promise(); 242 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
287 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, 243 service->RemoteCharacteristicWriteValue(
288 valueVector, new WriteValueCallback(this, resolver)); 244 m_characteristicInstanceId, valueVector,
245 convertToBaseCallback(WTF::bind(
246 &BluetoothRemoteGATTCharacteristic::WriteValueCallback,
247 wrapPersistent(this), wrapPersistent(resolver), valueVector)));
289 248
290 return promise; 249 return promise;
291 } 250 }
292 251
293 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { 252 void BluetoothRemoteGATTCharacteristic::NotificationsCallback(
294 public: 253 ScriptPromiseResolver* resolver,
295 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, 254 mojom::blink::WebBluetoothResult result) {
296 ScriptPromiseResolver* resolver) 255 if (!resolver->getExecutionContext() ||
297 : m_characteristic(characteristic), m_resolver(resolver) { 256 resolver->getExecutionContext()->isContextDestroyed())
298 // We always check that the device is connected before constructing this 257 return;
299 // object. 258
300 CHECK(m_characteristic->gatt()->connected()); 259 // If the resolver is not in the set of ActiveAlgorithms then the frame
301 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); 260 // disconnected so we reject.
261 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) {
262 resolver->reject(
263 DOMException::create(NetworkError, kGATTServerDisconnected));
264 return;
302 } 265 }
303 266
304 void onSuccess() override { 267 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
305 if (!m_resolver->getExecutionContext() || 268 resolver->resolve(this);
306 m_resolver->getExecutionContext()->isContextDestroyed()) 269 } else {
307 return; 270 resolver->reject(BluetoothError::take(resolver, result));
308
309 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
310 m_resolver.get())) {
311 m_resolver->reject(
312 DOMException::create(NetworkError, kGATTServerDisconnected));
313 return;
314 }
315
316 m_resolver->resolve(m_characteristic);
317 } 271 }
318 272 }
319 void onError(
320 int32_t
321 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
322 override {
323 if (!m_resolver->getExecutionContext() ||
324 m_resolver->getExecutionContext()->isContextDestroyed())
325 return;
326
327 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
328 m_resolver.get())) {
329 m_resolver->reject(
330 DOMException::create(NetworkError, kGATTServerDisconnected));
331 return;
332 }
333
334 m_resolver->reject(BluetoothError::take(m_resolver, error));
335 }
336
337 private:
338 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic;
339 Persistent<ScriptPromiseResolver> m_resolver;
340 };
341 273
342 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( 274 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications(
343 ScriptState* scriptState) { 275 ScriptState* scriptState) {
276 // We always check that the device is connected.
344 if (!gatt()->connected()) { 277 if (!gatt()->connected()) {
345 return ScriptPromise::rejectWithDOMException( 278 return ScriptPromise::rejectWithDOMException(
346 scriptState, 279 scriptState,
347 DOMException::create(NetworkError, kGATTServerNotConnected)); 280 DOMException::create(NetworkError, kGATTServerNotConnected));
348 } 281 }
349 282
350 if (!gatt()->device()->isValidCharacteristic( 283 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) {
351 m_webCharacteristic->characteristicInstanceID)) {
352 return ScriptPromise::rejectWithDOMException( 284 return ScriptPromise::rejectWithDOMException(
353 scriptState, 285 scriptState,
354 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 286 DOMException::create(InvalidStateError, kInvalidCharacteristic));
355 } 287 }
356 288
357 WebBluetooth* webbluetooth =
358 BluetoothSupplement::fromScriptState(scriptState);
359 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 289 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
360 ScriptPromise promise = resolver->promise(); 290 ScriptPromise promise = resolver->promise();
361 webbluetooth->startNotifications( 291 gatt()->AddToActiveAlgorithms(resolver);
362 m_webCharacteristic->characteristicInstanceID, 292
363 new NotificationsCallback(this, resolver)); 293 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
294 service->RemoteCharacteristicStartNotifications(
295 m_characteristicInstanceId,
296 convertToBaseCallback(
297 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
298 wrapPersistent(this), wrapPersistent(resolver))));
299
364 return promise; 300 return promise;
365 } 301 }
366 302
367 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( 303 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications(
368 ScriptState* scriptState) { 304 ScriptState* scriptState) {
305 // We always check that the device is connected.
369 if (!gatt()->connected()) { 306 if (!gatt()->connected()) {
370 return ScriptPromise::rejectWithDOMException( 307 return ScriptPromise::rejectWithDOMException(
371 scriptState, 308 scriptState,
372 DOMException::create(NetworkError, kGATTServerNotConnected)); 309 DOMException::create(NetworkError, kGATTServerNotConnected));
373 } 310 }
374 311
375 if (!gatt()->device()->isValidCharacteristic( 312 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) {
376 m_webCharacteristic->characteristicInstanceID)) {
377 return ScriptPromise::rejectWithDOMException( 313 return ScriptPromise::rejectWithDOMException(
378 scriptState, 314 scriptState,
379 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 315 DOMException::create(InvalidStateError, kInvalidCharacteristic));
380 } 316 }
381 317
382 WebBluetooth* webbluetooth =
383 BluetoothSupplement::fromScriptState(scriptState);
384 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 318 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
385 ScriptPromise promise = resolver->promise(); 319 ScriptPromise promise = resolver->promise();
386 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceID, 320 gatt()->AddToActiveAlgorithms(resolver);
387 new NotificationsCallback(this, resolver)); 321
322 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
323 service->RemoteCharacteristicStopNotifications(
324 m_characteristicInstanceId,
325 convertToBaseCallback(
326 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
327 wrapPersistent(this), wrapPersistent(resolver),
328 mojom::blink::WebBluetoothResult::SUCCESS)));
388 return promise; 329 return promise;
389 } 330 }
390 331
391 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { 332 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) {
392 visitor->trace(m_service); 333 visitor->trace(m_service);
393 visitor->trace(m_properties); 334 visitor->trace(m_properties);
394 visitor->trace(m_value); 335 visitor->trace(m_value);
336 visitor->trace(m_device);
395 EventTargetWithInlineData::trace(visitor); 337 EventTargetWithInlineData::trace(visitor);
396 SuspendableObject::trace(visitor); 338 SuspendableObject::trace(visitor);
397 } 339 }
398 340
399 } // namespace blink 341 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698