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

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: Use CreateDOMException instead of take(). 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, BluetoothError::CreateDOMException(
69 BluetoothRemoteGATTUtils::CreateDOMException( 69 mojom::blink::WebBluetoothResult::INVALID_DESCRIPTOR));
70 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor));
71 } 70 }
72 71
73 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 72 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
74 ScriptPromise promise = resolver->promise(); 73 ScriptPromise promise = resolver->promise();
75 getGatt()->AddToActiveAlgorithms(resolver); 74 getGatt()->AddToActiveAlgorithms(resolver);
76 getService()->RemoteDescriptorReadValue( 75 getService()->RemoteDescriptorReadValue(
77 m_descriptor->instance_id, 76 m_descriptor->instance_id,
78 convertToBaseCallback( 77 convertToBaseCallback(
79 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, 78 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback,
80 wrapPersistent(this), wrapPersistent(resolver)))); 79 wrapPersistent(this), wrapPersistent(resolver))));
81 80
82 return promise; 81 return promise;
83 } 82 }
84 83
85 void BluetoothRemoteGATTDescriptor::WriteValueCallback( 84 void BluetoothRemoteGATTDescriptor::WriteValueCallback(
86 ScriptPromiseResolver* resolver, 85 ScriptPromiseResolver* resolver,
87 const Vector<uint8_t>& value, 86 const Vector<uint8_t>& value,
88 mojom::blink::WebBluetoothResult result) { 87 mojom::blink::WebBluetoothResult result) {
89 if (!resolver->getExecutionContext() || 88 if (!resolver->getExecutionContext() ||
90 resolver->getExecutionContext()->isContextDestroyed()) 89 resolver->getExecutionContext()->isContextDestroyed())
91 return; 90 return;
92 91
93 // If the resolver is not in the set of ActiveAlgorithms then the frame 92 // If the resolver is not in the set of ActiveAlgorithms then the frame
94 // disconnected so we reject. 93 // disconnected so we reject.
95 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 94 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
96 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 95 resolver->reject(BluetoothError::CreateDOMException(
97 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 96 mojom::blink::WebBluetoothResult::GATT_SERVER_DISCONNECTED));
98 return; 97 return;
99 } 98 }
100 99
101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 100 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
102 m_value = BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value); 101 m_value = BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value);
103 resolver->resolve(); 102 resolver->resolve();
104 } else { 103 } else {
105 resolver->reject(BluetoothError::take(resolver, result)); 104 resolver->reject(BluetoothError::CreateDOMException(result));
106 } 105 }
107 } 106 }
108 107
109 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( 108 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue(
110 ScriptState* scriptState, 109 ScriptState* scriptState,
111 const DOMArrayPiece& value) { 110 const DOMArrayPiece& value) {
112 if (!getGatt()->connected()) { 111 if (!getGatt()->connected()) {
113 return ScriptPromise::rejectWithDOMException( 112 return ScriptPromise::rejectWithDOMException(
114 scriptState, 113 scriptState,
115 BluetoothRemoteGATTUtils::CreateDOMException( 114 BluetoothError::CreateDOMException(
116 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 115 mojom::blink::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED));
117 } 116 }
118 117
119 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { 118 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) {
120 return ScriptPromise::rejectWithDOMException( 119 return ScriptPromise::rejectWithDOMException(
121 scriptState, 120 scriptState, BluetoothError::CreateDOMException(
122 BluetoothRemoteGATTUtils::CreateDOMException( 121 mojom::blink::WebBluetoothResult::INVALID_DESCRIPTOR));
123 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor));
124 } 122 }
125 123
126 // Partial implementation of writeValue algorithm: 124 // Partial implementation of writeValue algorithm:
127 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdesc riptor-writevalue 125 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdesc riptor-writevalue
128 126
129 // If bytes is more than 512 bytes long (the maximum length of an attribute 127 // 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 128 // value, per Long Attribute Values) return a promise rejected with an
131 // InvalidModificationError and abort. 129 // InvalidModificationError and abort.
132 if (value.byteLength() > 512) { 130 if (value.byteLength() > 512) {
133 return ScriptPromise::rejectWithDOMException( 131 return ScriptPromise::rejectWithDOMException(
(...skipping 16 matching lines...) Expand all
150 148
151 return promise; 149 return promise;
152 } 150 }
153 151
154 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { 152 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) {
155 visitor->trace(m_characteristic); 153 visitor->trace(m_characteristic);
156 visitor->trace(m_value); 154 visitor->trace(m_value);
157 } 155 }
158 156
159 } // namespace blink 157 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698