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

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

Issue 2387293008: bluetooth: Require context is connected for getCharacteristic(s) (Closed)
Patch Set: Fix line length Created 4 years, 2 months 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
« no previous file with comments | « third_party/WebKit/LayoutTests/bluetooth/getCharacteristics/reconnect-during-with-uuid.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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 m_service->device()->gatt()->RemoveFromActiveAlgorithms(m_resolver.get());
Jeffrey Yasskin 2016/10/06 17:50:32 https://webbluetoothcg.github.io/web-bluetooth/#co
ortuno 2016/10/07 04:45:59 Done.
83 m_resolver->reject(BluetoothError::take(m_resolver, error)); 107 m_resolver->reject(BluetoothError::take(m_resolver, error));
84 } 108 }
85 109
86 private: 110 private:
87 Persistent<BluetoothRemoteGATTService> m_service; 111 Persistent<BluetoothRemoteGATTService> m_service;
88 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; 112 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity;
89 Persistent<ScriptPromiseResolver> m_resolver; 113 Persistent<ScriptPromiseResolver> m_resolver;
Jeffrey Yasskin 2016/10/06 17:50:32 Can you make this const, to be clear that we alway
ortuno 2016/10/07 04:45:59 Done.
90 }; 114 };
91 115
92 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( 116 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(
93 ScriptState* scriptState, 117 ScriptState* scriptState,
94 const StringOrUnsignedLong& characteristic, 118 const StringOrUnsignedLong& characteristic,
95 ExceptionState& exceptionState) { 119 ExceptionState& exceptionState) {
96 String characteristicUUID = 120 String characteristicUUID =
97 BluetoothUUID::getCharacteristic(characteristic, exceptionState); 121 BluetoothUUID::getCharacteristic(characteristic, exceptionState);
98 if (exceptionState.hadException()) 122 if (exceptionState.hadException())
99 return exceptionState.reject(scriptState); 123 return exceptionState.reject(scriptState);
(...skipping 21 matching lines...) Expand all
121 ScriptState* scriptState, 145 ScriptState* scriptState,
122 ExceptionState&) { 146 ExceptionState&) {
123 return getCharacteristicsImpl( 147 return getCharacteristicsImpl(
124 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); 148 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE);
125 } 149 }
126 150
127 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( 151 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(
128 ScriptState* scriptState, 152 ScriptState* scriptState,
129 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 153 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
130 String characteristicsUUID) { 154 String characteristicsUUID) {
155 if (!device()->gatt()->connected()) {
156 return ScriptPromise::rejectWithDOMException(
157 scriptState,
158 DOMException::create(NetworkError, kGATTServerNotConnected));
159 }
160
131 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 161 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
132 ScriptPromise promise = resolver->promise(); 162 ScriptPromise promise = resolver->promise();
133 163
134 WebBluetooth* webbluetooth = 164 WebBluetooth* webbluetooth =
135 BluetoothSupplement::fromScriptState(scriptState); 165 BluetoothSupplement::fromScriptState(scriptState);
136 webbluetooth->getCharacteristics( 166 webbluetooth->getCharacteristics(
137 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), 167 m_webService->serviceInstanceID, static_cast<int32_t>(quantity),
138 characteristicsUUID, 168 characteristicsUUID,
139 new GetCharacteristicsCallback(this, quantity, resolver)); 169 new GetCharacteristicsCallback(this, quantity, resolver));
140 170
141 return promise; 171 return promise;
142 } 172 }
143 173
144 } // namespace blink 174 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/bluetooth/getCharacteristics/reconnect-during-with-uuid.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698