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