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/BluetoothRemoteGATTService.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
6 | 6 |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.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/inspector/ConsoleMessage.h" | 12 #include "core/inspector/ConsoleMessage.h" |
13 #include "modules/bluetooth/BluetoothError.h" | 13 #include "modules/bluetooth/BluetoothError.h" |
14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.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 "public/platform/modules/bluetooth/WebBluetooth.h" | 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 18 #include "wtf/PtrUtil.h" |
| 19 #include <memory> |
18 | 20 |
19 namespace blink { | 21 namespace blink { |
20 | 22 |
21 BluetoothRemoteGATTService::BluetoothRemoteGATTService(PassOwnPtr<WebBluetoothRe
moteGATTService> webService) | 23 BluetoothRemoteGATTService::BluetoothRemoteGATTService(std::unique_ptr<WebBlueto
othRemoteGATTService> webService) |
22 : m_webService(std::move(webService)) | 24 : m_webService(std::move(webService)) |
23 { | 25 { |
24 } | 26 } |
25 | 27 |
26 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take(ScriptPromiseResolv
er*, PassOwnPtr<WebBluetoothRemoteGATTService> webService) | 28 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take(ScriptPromiseResolv
er*, std::unique_ptr<WebBluetoothRemoteGATTService> webService) |
27 { | 29 { |
28 if (!webService) { | 30 if (!webService) { |
29 return nullptr; | 31 return nullptr; |
30 } | 32 } |
31 return new BluetoothRemoteGATTService(std::move(webService)); | 33 return new BluetoothRemoteGATTService(std::move(webService)); |
32 } | 34 } |
33 | 35 |
34 // Class that allows us to resolve the promise with a single Characteristic or | 36 // Class that allows us to resolve the promise with a single Characteristic or |
35 // with a vector owning the characteristics. | 37 // with a vector owning the characteristics. |
36 class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallback
s { | 38 class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallback
s { |
37 public: | 39 public: |
38 GetCharacteristicsCallback(mojom::WebBluetoothGATTQueryQuantity quantity, Sc
riptPromiseResolver* resolver) | 40 GetCharacteristicsCallback(mojom::WebBluetoothGATTQueryQuantity quantity, Sc
riptPromiseResolver* resolver) |
39 : m_resolver(resolver) | 41 : m_resolver(resolver) |
40 , m_quantity(quantity) {} | 42 , m_quantity(quantity) {} |
41 | 43 |
42 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& w
ebCharacteristics) override | 44 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& w
ebCharacteristics) override |
43 { | 45 { |
44 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 46 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
45 return; | 47 return; |
46 | 48 |
47 if (m_quantity == mojom::WebBluetoothGATTQueryQuantity::SINGLE) { | 49 if (m_quantity == mojom::WebBluetoothGATTQueryQuantity::SINGLE) { |
48 DCHECK_EQ(1u, webCharacteristics.size()); | 50 DCHECK_EQ(1u, webCharacteristics.size()); |
49 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolv
er, adoptPtr(webCharacteristics[0]))); | 51 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolv
er, wrapUnique(webCharacteristics[0]))); |
50 return; | 52 return; |
51 } | 53 } |
52 | 54 |
53 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; | 55 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
54 characteristics.reserveInitialCapacity(webCharacteristics.size()); | 56 characteristics.reserveInitialCapacity(webCharacteristics.size()); |
55 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCh
aracteristics) { | 57 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCh
aracteristics) { |
56 characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_res
olver, adoptPtr(webCharacteristic))); | 58 characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_res
olver, wrapUnique(webCharacteristic))); |
57 } | 59 } |
58 m_resolver->resolve(characteristics); | 60 m_resolver->resolve(characteristics); |
59 } | 61 } |
60 | 62 |
61 void onError(const WebBluetoothError& e) override | 63 void onError(const WebBluetoothError& e) override |
62 { | 64 { |
63 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 65 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
64 return; | 66 return; |
65 m_resolver->reject(BluetoothError::take(m_resolver, e)); | 67 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
66 } | 68 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
112 ScriptPromise promise = resolver->promise(); | 114 ScriptPromise promise = resolver->promise(); |
113 | 115 |
114 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 116 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
115 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, quantity,
characteristicsUUID, new GetCharacteristicsCallback(quantity, resolver)); | 117 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, quantity,
characteristicsUUID, new GetCharacteristicsCallback(quantity, resolver)); |
116 | 118 |
117 return promise; | 119 return promise; |
118 } | 120 } |
119 | 121 |
120 } // namespace blink | 122 } // namespace blink |
OLD | NEW |