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/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()); | |
129 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
130 } | |
131 | 135 |
132 void onSuccess(const WebVector<uint8_t>& value) override { | 136 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
133 if (!m_resolver->getExecutionContext() || | 137 DCHECK(value); |
134 m_resolver->getExecutionContext()->isContextDestroyed()) | 138 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
135 return; | 139 resolver->reject( |
136 | |
137 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
138 m_resolver.get())) { | |
139 m_resolver->reject( | |
140 DOMException::create(NetworkError, kGATTServerDisconnected)); | 140 DOMException::create(NetworkError, kGATTServerDisconnected)); |
141 return; | 141 return; |
142 } | 142 } |
143 | 143 |
144 DOMDataView* domDataView = ConvertWebVectorToDataView(value); | 144 DOMDataView* domDataView = ConvertWTFVectorToDataView(value.value()); |
145 if (m_characteristic) | 145 setValue(domDataView); |
146 m_characteristic->setValue(domDataView); | 146 resolver->resolve(domDataView); |
147 | 147 } else { |
148 m_resolver->resolve(domDataView); | 148 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
Reilly Grant (use Gerrit)
2016/12/20 02:08:17
This check can be moved out to before you check re
juncai
2016/12/20 22:33:43
Done.
| |
149 } | 149 resolver->reject( |
150 | |
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)); | 150 DOMException::create(NetworkError, kGATTServerDisconnected)); |
163 return; | 151 return; |
164 } | 152 } |
165 | 153 |
166 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 154 resolver->reject(BluetoothError::take(resolver, result)); |
167 } | 155 } |
168 | 156 } |
169 private: | |
170 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
171 Persistent<ScriptPromiseResolver> m_resolver; | |
172 }; | |
173 | 157 |
174 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 158 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
175 ScriptState* scriptState) { | 159 ScriptState* scriptState) { |
160 // We always check that the device is connected. | |
176 if (!gatt()->connected()) { | 161 if (!gatt()->connected()) { |
177 return ScriptPromise::rejectWithDOMException( | 162 return ScriptPromise::rejectWithDOMException( |
178 scriptState, | 163 scriptState, |
179 DOMException::create(NetworkError, kGATTServerNotConnected)); | 164 DOMException::create(NetworkError, kGATTServerNotConnected)); |
180 } | 165 } |
181 | 166 |
182 if (!gatt()->device()->isValidCharacteristic( | 167 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
183 m_webCharacteristic->characteristicInstanceID)) { | |
184 return ScriptPromise::rejectWithDOMException( | 168 return ScriptPromise::rejectWithDOMException( |
185 scriptState, | 169 scriptState, |
186 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 170 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
187 } | 171 } |
188 | 172 |
189 WebBluetooth* webbluetooth = | |
190 BluetoothSupplement::fromScriptState(scriptState); | |
191 | |
192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 173 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
193 ScriptPromise promise = resolver->promise(); | 174 ScriptPromise promise = resolver->promise(); |
194 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 175 gatt()->AddToActiveAlgorithms(resolver); |
195 new ReadValueCallback(this, resolver)); | 176 |
177 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
178 service->RemoteCharacteristicReadValue( | |
179 m_characteristicInstanceId, | |
180 convertToBaseCallback( | |
181 WTF::bind(&BluetoothRemoteGATTCharacteristic::ReadValueCallback, | |
182 wrapPersistent(this), wrapPersistent(resolver)))); | |
196 | 183 |
197 return promise; | 184 return promise; |
198 } | 185 } |
199 | 186 |
200 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { | 187 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( |
201 public: | 188 ScriptPromiseResolver* resolver, |
202 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 189 const Vector<uint8_t>& value, |
203 ScriptPromiseResolver* resolver) | 190 mojom::blink::WebBluetoothResult result) { |
204 : m_characteristic(characteristic), m_resolver(resolver) { | 191 if (!resolver->getExecutionContext() || |
205 // We always check that the device is connected before constructing this | 192 resolver->getExecutionContext()->isContextDestroyed()) |
206 // object. | 193 return; |
207 CHECK(m_characteristic->gatt()->connected()); | |
208 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
209 } | |
210 | 194 |
211 void onSuccess(const WebVector<uint8_t>& value) override { | 195 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
212 if (!m_resolver->getExecutionContext() || | 196 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
213 m_resolver->getExecutionContext()->isContextDestroyed()) | 197 resolver->reject( |
214 return; | |
215 | |
216 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
217 m_resolver.get())) { | |
218 m_resolver->reject( | |
219 DOMException::create(NetworkError, kGATTServerDisconnected)); | 198 DOMException::create(NetworkError, kGATTServerDisconnected)); |
220 return; | 199 return; |
221 } | 200 } |
222 | 201 |
223 if (m_characteristic) { | 202 setValue(ConvertWTFVectorToDataView(value)); |
224 m_characteristic->setValue(ConvertWebVectorToDataView(value)); | 203 resolver->resolve(); |
225 } | 204 } else { |
226 m_resolver->resolve(); | 205 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
Reilly Grant (use Gerrit)
2016/12/20 02:08:17
Same here.
juncai
2016/12/20 22:33:43
Done.
| |
227 } | 206 resolver->reject( |
228 | |
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)); | 207 DOMException::create(NetworkError, kGATTServerDisconnected)); |
241 return; | 208 return; |
242 } | 209 } |
243 | 210 |
244 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 211 resolver->reject(BluetoothError::take(resolver, result)); |
245 } | 212 } |
246 | 213 } |
247 private: | |
248 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
249 Persistent<ScriptPromiseResolver> m_resolver; | |
250 }; | |
251 | 214 |
252 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 215 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
253 ScriptState* scriptState, | 216 ScriptState* scriptState, |
254 const DOMArrayPiece& value) { | 217 const DOMArrayPiece& value) { |
218 // We always check that the device is connected. | |
255 if (!gatt()->connected()) { | 219 if (!gatt()->connected()) { |
256 return ScriptPromise::rejectWithDOMException( | 220 return ScriptPromise::rejectWithDOMException( |
257 scriptState, | 221 scriptState, |
258 DOMException::create(NetworkError, kGATTServerNotConnected)); | 222 DOMException::create(NetworkError, kGATTServerNotConnected)); |
259 } | 223 } |
260 | 224 |
261 if (!gatt()->device()->isValidCharacteristic( | 225 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
262 m_webCharacteristic->characteristicInstanceID)) { | |
263 return ScriptPromise::rejectWithDOMException( | 226 return ScriptPromise::rejectWithDOMException( |
264 scriptState, | 227 scriptState, |
265 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 228 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
266 } | 229 } |
267 | 230 |
268 WebBluetooth* webbluetooth = | |
269 BluetoothSupplement::fromScriptState(scriptState); | |
270 // Partial implementation of writeValue algorithm: | 231 // Partial implementation of writeValue algorithm: |
271 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue | 232 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue |
272 | 233 |
273 // If bytes is more than 512 bytes long (the maximum length of an attribute | 234 // 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 | 235 // value, per Long Attribute Values) return a promise rejected with an |
275 // InvalidModificationError and abort. | 236 // InvalidModificationError and abort. |
276 if (value.byteLength() > 512) | 237 if (value.byteLength() > 512) |
277 return ScriptPromise::rejectWithDOMException( | 238 return ScriptPromise::rejectWithDOMException( |
278 scriptState, DOMException::create(InvalidModificationError, | 239 scriptState, DOMException::create(InvalidModificationError, |
279 "Value can't exceed 512 bytes.")); | 240 "Value can't exceed 512 bytes.")); |
280 | 241 |
281 // Let valueVector be a copy of the bytes held by value. | 242 // Let valueVector be a copy of the bytes held by value. |
282 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); | 243 Vector<uint8_t> valueVector; |
244 valueVector.append(value.bytes(), value.byteLength()); | |
283 | 245 |
284 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 246 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
247 ScriptPromise promise = resolver->promise(); | |
248 gatt()->AddToActiveAlgorithms(resolver); | |
285 | 249 |
286 ScriptPromise promise = resolver->promise(); | 250 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
287 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, | 251 service->RemoteCharacteristicWriteValue( |
288 valueVector, new WriteValueCallback(this, resolver)); | 252 m_characteristicInstanceId, valueVector, |
253 convertToBaseCallback(WTF::bind( | |
254 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, | |
255 wrapPersistent(this), wrapPersistent(resolver), valueVector))); | |
289 | 256 |
290 return promise; | 257 return promise; |
291 } | 258 } |
292 | 259 |
293 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { | 260 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
294 public: | 261 ScriptPromiseResolver* resolver, |
295 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 262 mojom::blink::WebBluetoothResult result) { |
296 ScriptPromiseResolver* resolver) | 263 if (!resolver->getExecutionContext() || |
297 : m_characteristic(characteristic), m_resolver(resolver) { | 264 resolver->getExecutionContext()->isContextDestroyed()) |
298 // We always check that the device is connected before constructing this | 265 return; |
299 // object. | |
300 CHECK(m_characteristic->gatt()->connected()); | |
301 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
302 } | |
303 | 266 |
304 void onSuccess() override { | 267 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
305 if (!m_resolver->getExecutionContext() || | 268 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
306 m_resolver->getExecutionContext()->isContextDestroyed()) | 269 resolver->reject( |
307 return; | |
308 | |
309 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
310 m_resolver.get())) { | |
311 m_resolver->reject( | |
312 DOMException::create(NetworkError, kGATTServerDisconnected)); | 270 DOMException::create(NetworkError, kGATTServerDisconnected)); |
313 return; | 271 return; |
314 } | 272 } |
315 | 273 |
316 m_resolver->resolve(m_characteristic); | 274 resolver->resolve(this); |
317 } | 275 } else { |
318 | 276 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
Reilly Grant (use Gerrit)
2016/12/20 02:08:17
...and here.
juncai
2016/12/20 22:33:43
Done.
| |
319 void onError( | 277 resolver->reject( |
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)); | 278 DOMException::create(NetworkError, kGATTServerDisconnected)); |
331 return; | 279 return; |
332 } | 280 } |
333 | 281 |
334 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 282 resolver->reject(BluetoothError::take(resolver, result)); |
335 } | 283 } |
336 | 284 } |
337 private: | |
338 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
339 Persistent<ScriptPromiseResolver> m_resolver; | |
340 }; | |
341 | 285 |
342 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 286 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
343 ScriptState* scriptState) { | 287 ScriptState* scriptState) { |
288 // We always check that the device is connected. | |
344 if (!gatt()->connected()) { | 289 if (!gatt()->connected()) { |
345 return ScriptPromise::rejectWithDOMException( | 290 return ScriptPromise::rejectWithDOMException( |
346 scriptState, | 291 scriptState, |
347 DOMException::create(NetworkError, kGATTServerNotConnected)); | 292 DOMException::create(NetworkError, kGATTServerNotConnected)); |
348 } | 293 } |
349 | 294 |
350 if (!gatt()->device()->isValidCharacteristic( | 295 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
351 m_webCharacteristic->characteristicInstanceID)) { | |
352 return ScriptPromise::rejectWithDOMException( | 296 return ScriptPromise::rejectWithDOMException( |
353 scriptState, | 297 scriptState, |
354 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 298 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
355 } | 299 } |
356 | 300 |
357 WebBluetooth* webbluetooth = | |
358 BluetoothSupplement::fromScriptState(scriptState); | |
359 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 301 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
360 ScriptPromise promise = resolver->promise(); | 302 ScriptPromise promise = resolver->promise(); |
361 webbluetooth->startNotifications( | 303 gatt()->AddToActiveAlgorithms(resolver); |
362 m_webCharacteristic->characteristicInstanceID, | 304 |
363 new NotificationsCallback(this, resolver)); | 305 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
306 service->RemoteCharacteristicStartNotifications( | |
307 m_characteristicInstanceId, | |
308 convertToBaseCallback( | |
309 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | |
310 wrapPersistent(this), wrapPersistent(resolver)))); | |
311 | |
364 return promise; | 312 return promise; |
365 } | 313 } |
366 | 314 |
367 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 315 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
368 ScriptState* scriptState) { | 316 ScriptState* scriptState) { |
317 // We always check that the device is connected. | |
369 if (!gatt()->connected()) { | 318 if (!gatt()->connected()) { |
370 return ScriptPromise::rejectWithDOMException( | 319 return ScriptPromise::rejectWithDOMException( |
371 scriptState, | 320 scriptState, |
372 DOMException::create(NetworkError, kGATTServerNotConnected)); | 321 DOMException::create(NetworkError, kGATTServerNotConnected)); |
373 } | 322 } |
374 | 323 |
375 if (!gatt()->device()->isValidCharacteristic( | 324 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
376 m_webCharacteristic->characteristicInstanceID)) { | |
377 return ScriptPromise::rejectWithDOMException( | 325 return ScriptPromise::rejectWithDOMException( |
378 scriptState, | 326 scriptState, |
379 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 327 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
380 } | 328 } |
381 | 329 |
382 WebBluetooth* webbluetooth = | |
383 BluetoothSupplement::fromScriptState(scriptState); | |
384 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 330 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
385 ScriptPromise promise = resolver->promise(); | 331 ScriptPromise promise = resolver->promise(); |
386 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceID, | 332 gatt()->AddToActiveAlgorithms(resolver); |
387 new NotificationsCallback(this, resolver)); | 333 |
334 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
335 service->RemoteCharacteristicStopNotifications( | |
336 m_characteristicInstanceId, | |
337 convertToBaseCallback( | |
338 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | |
339 wrapPersistent(this), wrapPersistent(resolver), | |
340 mojom::blink::WebBluetoothResult::SUCCESS))); | |
388 return promise; | 341 return promise; |
389 } | 342 } |
390 | 343 |
391 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 344 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
392 visitor->trace(m_service); | 345 visitor->trace(m_service); |
393 visitor->trace(m_properties); | 346 visitor->trace(m_properties); |
394 visitor->trace(m_value); | 347 visitor->trace(m_value); |
348 visitor->trace(m_device); | |
395 EventTargetWithInlineData::trace(visitor); | 349 EventTargetWithInlineData::trace(visitor); |
396 SuspendableObject::trace(visitor); | 350 SuspendableObject::trace(visitor); |
397 } | 351 } |
398 | 352 |
399 } // namespace blink | 353 } // namespace blink |
OLD | NEW |