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" | 18 #include "wtf/PtrUtil.h" |
19 #include <memory> | 19 #include <memory> |
20 | 20 |
21 namespace blink { | 21 namespace blink { |
22 | 22 |
| 23 namespace { |
| 24 |
| 25 const char kGATTServerDisconnected[] = |
| 26 "GATT Server disconnected while retrieving characteristics."; |
| 27 const char kGATTServerNotConnected[] = |
| 28 "GATT Server is disconnected. Cannot retrieve characteristics."; |
| 29 |
| 30 } // namespace |
| 31 |
23 BluetoothRemoteGATTService::BluetoothRemoteGATTService( | 32 BluetoothRemoteGATTService::BluetoothRemoteGATTService( |
24 std::unique_ptr<WebBluetoothRemoteGATTService> webService, | 33 std::unique_ptr<WebBluetoothRemoteGATTService> webService, |
25 BluetoothDevice* device) | 34 BluetoothDevice* device) |
26 : m_webService(std::move(webService)), m_device(device) {} | 35 : m_webService(std::move(webService)), m_device(device) {} |
27 | 36 |
28 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take( | 37 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take( |
29 ScriptPromiseResolver*, | 38 ScriptPromiseResolver*, |
30 std::unique_ptr<WebBluetoothRemoteGATTService> webService, | 39 std::unique_ptr<WebBluetoothRemoteGATTService> webService, |
31 BluetoothDevice* device) { | 40 BluetoothDevice* device) { |
32 if (!webService) { | 41 if (!webService) { |
33 return nullptr; | 42 return nullptr; |
34 } | 43 } |
35 return new BluetoothRemoteGATTService(std::move(webService), device); | 44 return new BluetoothRemoteGATTService(std::move(webService), device); |
36 } | 45 } |
37 | 46 |
38 DEFINE_TRACE(BluetoothRemoteGATTService) { | 47 DEFINE_TRACE(BluetoothRemoteGATTService) { |
39 visitor->trace(m_device); | 48 visitor->trace(m_device); |
40 } | 49 } |
41 | 50 |
42 // Class that allows us to resolve the promise with a single Characteristic or | 51 // Class that allows us to resolve the promise with a single Characteristic or |
43 // with a vector owning the characteristics. | 52 // with a vector owning the characteristics. |
44 class GetCharacteristicsCallback | 53 class GetCharacteristicsCallback |
45 : public WebBluetoothGetCharacteristicsCallbacks { | 54 : public WebBluetoothGetCharacteristicsCallbacks { |
46 public: | 55 public: |
47 GetCharacteristicsCallback( | 56 GetCharacteristicsCallback( |
48 BluetoothRemoteGATTService* service, | 57 BluetoothRemoteGATTService* service, |
49 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 58 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
50 ScriptPromiseResolver* resolver) | 59 ScriptPromiseResolver* resolver) |
51 : m_service(service), m_quantity(quantity), m_resolver(resolver) {} | 60 : m_service(service), m_quantity(quantity), m_resolver(resolver) { |
| 61 // We always check that the device is connected before constructing this |
| 62 // object. |
| 63 CHECK(m_service->device()->gatt()->connected()); |
| 64 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 65 } |
52 | 66 |
53 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& | 67 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& |
54 webCharacteristics) override { | 68 webCharacteristics) override { |
55 if (!m_resolver->getExecutionContext() || | 69 if (!m_resolver->getExecutionContext() || |
56 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 70 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
57 return; | 71 return; |
58 | 72 |
| 73 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 74 // disconnected so we reject. |
| 75 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( |
| 76 m_resolver.get())) { |
| 77 m_resolver->reject( |
| 78 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 79 return; |
| 80 } |
| 81 |
59 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 82 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
60 DCHECK_EQ(1u, webCharacteristics.size()); | 83 DCHECK_EQ(1u, webCharacteristics.size()); |
61 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take( | 84 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take( |
62 m_resolver, wrapUnique(webCharacteristics[0]), m_service)); | 85 m_resolver, wrapUnique(webCharacteristics[0]), m_service)); |
63 return; | 86 return; |
64 } | 87 } |
65 | 88 |
66 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; | 89 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
67 characteristics.reserveInitialCapacity(webCharacteristics.size()); | 90 characteristics.reserveInitialCapacity(webCharacteristics.size()); |
68 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : | 91 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : |
69 webCharacteristics) { | 92 webCharacteristics) { |
70 characteristics.append(BluetoothRemoteGATTCharacteristic::take( | 93 characteristics.append(BluetoothRemoteGATTCharacteristic::take( |
71 m_resolver, wrapUnique(webCharacteristic), m_service)); | 94 m_resolver, wrapUnique(webCharacteristic), m_service)); |
72 } | 95 } |
73 m_resolver->resolve(characteristics); | 96 m_resolver->resolve(characteristics); |
74 } | 97 } |
75 | 98 |
76 void onError( | 99 void onError( |
77 int32_t | 100 int32_t |
78 error /* Corresponds to WebBluetoothError in web_bluetooth.mojom */) | 101 error /* Corresponds to WebBluetoothError in web_bluetooth.mojom */) |
79 override { | 102 override { |
80 if (!m_resolver->getExecutionContext() || | 103 if (!m_resolver->getExecutionContext() || |
81 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 104 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
82 return; | 105 return; |
| 106 |
| 107 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 108 // disconnected so we reject. |
| 109 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( |
| 110 m_resolver.get())) { |
| 111 m_resolver->reject( |
| 112 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 113 return; |
| 114 } |
| 115 |
83 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 116 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
84 } | 117 } |
85 | 118 |
86 private: | 119 private: |
87 Persistent<BluetoothRemoteGATTService> m_service; | 120 Persistent<BluetoothRemoteGATTService> m_service; |
88 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; | 121 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; |
89 Persistent<ScriptPromiseResolver> m_resolver; | 122 const Persistent<ScriptPromiseResolver> m_resolver; |
90 }; | 123 }; |
91 | 124 |
92 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( | 125 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
93 ScriptState* scriptState, | 126 ScriptState* scriptState, |
94 const StringOrUnsignedLong& characteristic, | 127 const StringOrUnsignedLong& characteristic, |
95 ExceptionState& exceptionState) { | 128 ExceptionState& exceptionState) { |
96 String characteristicUUID = | 129 String characteristicUUID = |
97 BluetoothUUID::getCharacteristic(characteristic, exceptionState); | 130 BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
98 if (exceptionState.hadException()) | 131 if (exceptionState.hadException()) |
99 return exceptionState.reject(scriptState); | 132 return exceptionState.reject(scriptState); |
(...skipping 21 matching lines...) Expand all Loading... |
121 ScriptState* scriptState, | 154 ScriptState* scriptState, |
122 ExceptionState&) { | 155 ExceptionState&) { |
123 return getCharacteristicsImpl( | 156 return getCharacteristicsImpl( |
124 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 157 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
125 } | 158 } |
126 | 159 |
127 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( | 160 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
128 ScriptState* scriptState, | 161 ScriptState* scriptState, |
129 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 162 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
130 String characteristicsUUID) { | 163 String characteristicsUUID) { |
| 164 if (!device()->gatt()->connected()) { |
| 165 return ScriptPromise::rejectWithDOMException( |
| 166 scriptState, |
| 167 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 168 } |
| 169 |
131 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 170 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
132 ScriptPromise promise = resolver->promise(); | 171 ScriptPromise promise = resolver->promise(); |
133 | 172 |
134 WebBluetooth* webbluetooth = | 173 WebBluetooth* webbluetooth = |
135 BluetoothSupplement::fromScriptState(scriptState); | 174 BluetoothSupplement::fromScriptState(scriptState); |
136 webbluetooth->getCharacteristics( | 175 webbluetooth->getCharacteristics( |
137 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), | 176 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), |
138 characteristicsUUID, | 177 characteristicsUUID, |
139 new GetCharacteristicsCallback(this, quantity, resolver)); | 178 new GetCharacteristicsCallback(this, quantity, resolver)); |
140 | 179 |
141 return promise; | 180 return promise; |
142 } | 181 } |
143 | 182 |
144 } // namespace blink | 183 } // namespace blink |
OLD | NEW |