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

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

Issue 2831783002: bluetooth: Clean up better 'not connected' errors (Closed)
Patch Set: Fix enum Created 3 years, 8 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
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/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/DOMException.h" 9 #include "core/dom/DOMException.h"
10 #include "core/events/Event.h" 10 #include "core/events/Event.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 ScriptPromiseResolver* resolver, 87 ScriptPromiseResolver* resolver,
88 mojom::blink::WebBluetoothResult result, 88 mojom::blink::WebBluetoothResult result,
89 const Optional<Vector<uint8_t>>& value) { 89 const Optional<Vector<uint8_t>>& value) {
90 if (!resolver->GetExecutionContext() || 90 if (!resolver->GetExecutionContext() ||
91 resolver->GetExecutionContext()->IsContextDestroyed()) 91 resolver->GetExecutionContext()->IsContextDestroyed())
92 return; 92 return;
93 93
94 // If the device is disconnected, reject. 94 // If the device is disconnected, reject.
95 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { 95 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) {
96 resolver->Reject( 96 resolver->Reject(
97 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 97 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
98 return; 98 return;
99 } 99 }
100 100
101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
102 DCHECK(value); 102 DCHECK(value);
103 DOMDataView* dom_data_view = 103 DOMDataView* dom_data_view =
104 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); 104 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value());
105 SetValue(dom_data_view); 105 SetValue(dom_data_view);
106 DispatchEvent(Event::Create(EventTypeNames::characteristicvaluechanged)); 106 DispatchEvent(Event::Create(EventTypeNames::characteristicvaluechanged));
107 resolver->Resolve(dom_data_view); 107 resolver->Resolve(dom_data_view);
108 } else { 108 } else {
109 resolver->Reject(BluetoothError::CreateDOMException(result)); 109 resolver->Reject(BluetoothError::CreateDOMException(result));
110 } 110 }
111 } 111 }
112 112
113 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( 113 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue(
114 ScriptState* script_state) { 114 ScriptState* script_state) {
115 if (!GetGatt()->connected()) { 115 if (!GetGatt()->connected()) {
116 return ScriptPromise::RejectWithDOMException( 116 return ScriptPromise::RejectWithDOMException(
117 script_state, 117 script_state,
118 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 118 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
119 } 119 }
120 120
121 if (!GetGatt()->device()->IsValidCharacteristic( 121 if (!GetGatt()->device()->IsValidCharacteristic(
122 characteristic_->instance_id)) { 122 characteristic_->instance_id)) {
123 return ScriptPromise::RejectWithDOMException( 123 return ScriptPromise::RejectWithDOMException(
124 script_state, CreateInvalidCharacteristicError()); 124 script_state, CreateInvalidCharacteristicError());
125 } 125 }
126 126
127 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 127 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
128 ScriptPromise promise = resolver->Promise(); 128 ScriptPromise promise = resolver->Promise();
(...skipping 14 matching lines...) Expand all
143 ScriptPromiseResolver* resolver, 143 ScriptPromiseResolver* resolver,
144 const Vector<uint8_t>& value, 144 const Vector<uint8_t>& value,
145 mojom::blink::WebBluetoothResult result) { 145 mojom::blink::WebBluetoothResult result) {
146 if (!resolver->GetExecutionContext() || 146 if (!resolver->GetExecutionContext() ||
147 resolver->GetExecutionContext()->IsContextDestroyed()) 147 resolver->GetExecutionContext()->IsContextDestroyed())
148 return; 148 return;
149 149
150 // If the device is disconnected, reject. 150 // If the device is disconnected, reject.
151 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { 151 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) {
152 resolver->Reject( 152 resolver->Reject(
153 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 153 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
154 return; 154 return;
155 } 155 }
156 156
157 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 157 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
158 SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); 158 SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value));
159 resolver->Resolve(); 159 resolver->Resolve();
160 } else { 160 } else {
161 resolver->Reject(BluetoothError::CreateDOMException(result)); 161 resolver->Reject(BluetoothError::CreateDOMException(result));
162 } 162 }
163 } 163 }
164 164
165 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( 165 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue(
166 ScriptState* script_state, 166 ScriptState* script_state,
167 const DOMArrayPiece& value) { 167 const DOMArrayPiece& value) {
168 if (!GetGatt()->connected()) { 168 if (!GetGatt()->connected()) {
169 return ScriptPromise::RejectWithDOMException( 169 return ScriptPromise::RejectWithDOMException(
170 script_state, 170 script_state,
171 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 171 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
172 } 172 }
173 173
174 if (!GetGatt()->device()->IsValidCharacteristic( 174 if (!GetGatt()->device()->IsValidCharacteristic(
175 characteristic_->instance_id)) { 175 characteristic_->instance_id)) {
176 return ScriptPromise::RejectWithDOMException( 176 return ScriptPromise::RejectWithDOMException(
177 script_state, CreateInvalidCharacteristicError()); 177 script_state, CreateInvalidCharacteristicError());
178 } 178 }
179 179
180 // Partial implementation of writeValue algorithm: 180 // Partial implementation of writeValue algorithm:
181 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue 181 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue
(...skipping 28 matching lines...) Expand all
210 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( 210 void BluetoothRemoteGATTCharacteristic::NotificationsCallback(
211 ScriptPromiseResolver* resolver, 211 ScriptPromiseResolver* resolver,
212 mojom::blink::WebBluetoothResult result) { 212 mojom::blink::WebBluetoothResult result) {
213 if (!resolver->GetExecutionContext() || 213 if (!resolver->GetExecutionContext() ||
214 resolver->GetExecutionContext()->IsContextDestroyed()) 214 resolver->GetExecutionContext()->IsContextDestroyed())
215 return; 215 return;
216 216
217 // If the device is disconnected, reject. 217 // If the device is disconnected, reject.
218 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { 218 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) {
219 resolver->Reject( 219 resolver->Reject(
220 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 220 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
221 return; 221 return;
222 } 222 }
223 223
224 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 224 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
225 resolver->Resolve(this); 225 resolver->Resolve(this);
226 } else { 226 } else {
227 resolver->Reject(BluetoothError::CreateDOMException(result)); 227 resolver->Reject(BluetoothError::CreateDOMException(result));
228 } 228 }
229 } 229 }
230 230
231 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( 231 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications(
232 ScriptState* script_state) { 232 ScriptState* script_state) {
233 if (!GetGatt()->connected()) { 233 if (!GetGatt()->connected()) {
234 return ScriptPromise::RejectWithDOMException( 234 return ScriptPromise::RejectWithDOMException(
235 script_state, 235 script_state,
236 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 236 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
237 } 237 }
238 238
239 if (!GetGatt()->device()->IsValidCharacteristic( 239 if (!GetGatt()->device()->IsValidCharacteristic(
240 characteristic_->instance_id)) { 240 characteristic_->instance_id)) {
241 return ScriptPromise::RejectWithDOMException( 241 return ScriptPromise::RejectWithDOMException(
242 script_state, CreateInvalidCharacteristicError()); 242 script_state, CreateInvalidCharacteristicError());
243 } 243 }
244 244
245 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 245 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
246 ScriptPromise promise = resolver->Promise(); 246 ScriptPromise promise = resolver->Promise();
(...skipping 12 matching lines...) Expand all
259 WrapPersistent(this), WrapPersistent(resolver)))); 259 WrapPersistent(this), WrapPersistent(resolver))));
260 260
261 return promise; 261 return promise;
262 } 262 }
263 263
264 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( 264 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications(
265 ScriptState* script_state) { 265 ScriptState* script_state) {
266 if (!GetGatt()->connected()) { 266 if (!GetGatt()->connected()) {
267 return ScriptPromise::RejectWithDOMException( 267 return ScriptPromise::RejectWithDOMException(
268 script_state, 268 script_state,
269 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); 269 BluetoothError::CreateNotConnectedException(BluetoothOperation::kGATT));
270 } 270 }
271 271
272 if (!GetGatt()->device()->IsValidCharacteristic( 272 if (!GetGatt()->device()->IsValidCharacteristic(
273 characteristic_->instance_id)) { 273 characteristic_->instance_id)) {
274 return ScriptPromise::RejectWithDOMException( 274 return ScriptPromise::RejectWithDOMException(
275 script_state, CreateInvalidCharacteristicError()); 275 script_state, CreateInvalidCharacteristicError());
276 } 276 }
277 277
278 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 278 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
279 ScriptPromise promise = resolver->Promise(); 279 ScriptPromise promise = resolver->Promise();
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { 421 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) {
422 visitor->Trace(service_); 422 visitor->Trace(service_);
423 visitor->Trace(properties_); 423 visitor->Trace(properties_);
424 visitor->Trace(value_); 424 visitor->Trace(value_);
425 visitor->Trace(device_); 425 visitor->Trace(device_);
426 EventTargetWithInlineData::Trace(visitor); 426 EventTargetWithInlineData::Trace(visitor);
427 ContextLifecycleObserver::Trace(visitor); 427 ContextLifecycleObserver::Trace(visitor);
428 } 428 }
429 429
430 } // namespace blink 430 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698