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

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

Issue 2680783002: bluetooth: show better error messages for services, characteristics and descriptors (Closed)
Patch Set: Added UUID to DESCRIPTOR_NOT_FOUND, INVALID_DESCRIPTOR and INVALID_CHARACTERISTIC. Created 3 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/BluetoothRemoteGATTDescriptor.h" 5 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.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 "modules/bluetooth/BluetoothError.h" 10 #include "modules/bluetooth/BluetoothError.h"
(...skipping 20 matching lines...) Expand all
31 void BluetoothRemoteGATTDescriptor::ReadValueCallback( 31 void BluetoothRemoteGATTDescriptor::ReadValueCallback(
32 ScriptPromiseResolver* resolver, 32 ScriptPromiseResolver* resolver,
33 mojom::blink::WebBluetoothResult result, 33 mojom::blink::WebBluetoothResult result,
34 const Optional<Vector<uint8_t>>& value) { 34 const Optional<Vector<uint8_t>>& value) {
35 if (!resolver->getExecutionContext() || 35 if (!resolver->getExecutionContext() ||
36 resolver->getExecutionContext()->isContextDestroyed()) 36 resolver->getExecutionContext()->isContextDestroyed())
37 return; 37 return;
38 38
39 // If the device is disconnected, reject. 39 // If the device is disconnected, reject.
40 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 40 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
41 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 41 resolver->reject(BluetoothError::createDOMException(
42 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 42 mojom::blink::WebBluetoothResult::GATT_SERVER_DISCONNECTED));
43 return; 43 return;
44 } 44 }
45 45
46 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 46 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
47 DCHECK(value); 47 DCHECK(value);
48 DOMDataView* domDataView = 48 DOMDataView* domDataView =
49 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); 49 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value());
50 m_value = domDataView; 50 m_value = domDataView;
51 resolver->resolve(domDataView); 51 resolver->resolve(domDataView);
52 } else { 52 } else {
53 resolver->reject(BluetoothError::take(resolver, result)); 53 resolver->reject(BluetoothError::createDOMException(result));
54 } 54 }
55 } 55 }
56 56
57 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( 57 ScriptPromise BluetoothRemoteGATTDescriptor::readValue(
58 ScriptState* scriptState) { 58 ScriptState* scriptState) {
59 if (!getGatt()->connected()) { 59 if (!getGatt()->connected()) {
60 return ScriptPromise::rejectWithDOMException( 60 return ScriptPromise::rejectWithDOMException(
61 scriptState, 61 scriptState,
62 BluetoothRemoteGATTUtils::CreateDOMException( 62 BluetoothError::createDOMException(
63 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 63 mojom::blink::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED));
64 } 64 }
65 65
66 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { 66 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) {
67 return ScriptPromise::rejectWithDOMException( 67 return ScriptPromise::rejectWithDOMException(
68 scriptState, 68 scriptState, createInvalidDescriptorError());
69 BluetoothRemoteGATTUtils::CreateDOMException(
70 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor));
71 } 69 }
72 70
73 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
74 ScriptPromise promise = resolver->promise(); 72 ScriptPromise promise = resolver->promise();
75 getGatt()->AddToActiveAlgorithms(resolver); 73 getGatt()->AddToActiveAlgorithms(resolver);
76 getService()->RemoteDescriptorReadValue( 74 getService()->RemoteDescriptorReadValue(
77 m_descriptor->instance_id, 75 m_descriptor->instance_id,
78 convertToBaseCallback( 76 convertToBaseCallback(
79 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, 77 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback,
80 wrapPersistent(this), wrapPersistent(resolver)))); 78 wrapPersistent(this), wrapPersistent(resolver))));
81 79
82 return promise; 80 return promise;
83 } 81 }
84 82
85 void BluetoothRemoteGATTDescriptor::WriteValueCallback( 83 void BluetoothRemoteGATTDescriptor::WriteValueCallback(
86 ScriptPromiseResolver* resolver, 84 ScriptPromiseResolver* resolver,
87 const Vector<uint8_t>& value, 85 const Vector<uint8_t>& value,
88 mojom::blink::WebBluetoothResult result) { 86 mojom::blink::WebBluetoothResult result) {
89 if (!resolver->getExecutionContext() || 87 if (!resolver->getExecutionContext() ||
90 resolver->getExecutionContext()->isContextDestroyed()) 88 resolver->getExecutionContext()->isContextDestroyed())
91 return; 89 return;
92 90
93 // If the resolver is not in the set of ActiveAlgorithms then the frame 91 // If the resolver is not in the set of ActiveAlgorithms then the frame
94 // disconnected so we reject. 92 // disconnected so we reject.
95 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 93 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
96 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 94 resolver->reject(BluetoothError::createDOMException(
97 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 95 mojom::blink::WebBluetoothResult::GATT_SERVER_DISCONNECTED));
98 return; 96 return;
99 } 97 }
100 98
101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 99 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
102 m_value = BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value); 100 m_value = BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value);
103 resolver->resolve(); 101 resolver->resolve();
104 } else { 102 } else {
105 resolver->reject(BluetoothError::take(resolver, result)); 103 resolver->reject(BluetoothError::createDOMException(result));
106 } 104 }
107 } 105 }
108 106
109 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( 107 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue(
110 ScriptState* scriptState, 108 ScriptState* scriptState,
111 const DOMArrayPiece& value) { 109 const DOMArrayPiece& value) {
112 if (!getGatt()->connected()) { 110 if (!getGatt()->connected()) {
113 return ScriptPromise::rejectWithDOMException( 111 return ScriptPromise::rejectWithDOMException(
114 scriptState, 112 scriptState,
115 BluetoothRemoteGATTUtils::CreateDOMException( 113 BluetoothError::createDOMException(
116 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 114 mojom::blink::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED));
117 } 115 }
118 116
119 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { 117 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) {
120 return ScriptPromise::rejectWithDOMException( 118 return ScriptPromise::rejectWithDOMException(
121 scriptState, 119 scriptState, createInvalidDescriptorError());
122 BluetoothRemoteGATTUtils::CreateDOMException(
123 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor));
124 } 120 }
125 121
126 // Partial implementation of writeValue algorithm: 122 // Partial implementation of writeValue algorithm:
127 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdesc riptor-writevalue 123 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdesc riptor-writevalue
128 124
129 // If bytes is more than 512 bytes long (the maximum length of an attribute 125 // If bytes is more than 512 bytes long (the maximum length of an attribute
130 // value, per Long Attribute Values) return a promise rejected with an 126 // value, per Long Attribute Values) return a promise rejected with an
131 // InvalidModificationError and abort. 127 // InvalidModificationError and abort.
132 if (value.byteLength() > 512) { 128 if (value.byteLength() > 512) {
133 return ScriptPromise::rejectWithDOMException( 129 return ScriptPromise::rejectWithDOMException(
(...skipping 10 matching lines...) Expand all
144 getGatt()->AddToActiveAlgorithms(resolver); 140 getGatt()->AddToActiveAlgorithms(resolver);
145 getService()->RemoteDescriptorWriteValue( 141 getService()->RemoteDescriptorWriteValue(
146 m_descriptor->instance_id, valueVector, 142 m_descriptor->instance_id, valueVector,
147 convertToBaseCallback(WTF::bind( 143 convertToBaseCallback(WTF::bind(
148 &BluetoothRemoteGATTDescriptor::WriteValueCallback, 144 &BluetoothRemoteGATTDescriptor::WriteValueCallback,
149 wrapPersistent(this), wrapPersistent(resolver), valueVector))); 145 wrapPersistent(this), wrapPersistent(resolver), valueVector)));
150 146
151 return promise; 147 return promise;
152 } 148 }
153 149
150 DOMException* BluetoothRemoteGATTDescriptor::createInvalidDescriptorError() {
151 return BluetoothError::createDOMException(
152 mojom::blink::WebBluetoothResult::INVALID_DESCRIPTOR,
153 "Descriptor with UUID " + uuid() +
154 " is no longer valid. Remember to retrieve the Descriptor again "
155 "after reconnecting.");
156 }
157
154 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { 158 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) {
155 visitor->trace(m_characteristic); 159 visitor->trace(m_characteristic);
156 visitor->trace(m_value); 160 visitor->trace(m_value);
157 } 161 }
158 162
159 } // namespace blink 163 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698