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/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/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 40 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
41 ScriptPromiseResolver* resolver, | 41 ScriptPromiseResolver* resolver, |
42 mojom::blink::WebBluetoothResult result, | 42 mojom::blink::WebBluetoothResult result, |
43 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> | 43 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> |
44 characteristics) { | 44 characteristics) { |
45 if (!resolver->getExecutionContext() || | 45 if (!resolver->getExecutionContext() || |
46 resolver->getExecutionContext()->isContextDestroyed()) | 46 resolver->getExecutionContext()->isContextDestroyed()) |
47 return; | 47 return; |
48 | 48 |
49 // If the device is disconnected, reject. | 49 // If the device is disconnected, reject. |
50 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { | 50 if (!m_device->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
51 resolver->reject(BluetoothError::createDOMException( | 51 resolver->reject(BluetoothError::createDOMException( |
52 mojom::blink::WebBluetoothResult:: | 52 mojom::blink::WebBluetoothResult:: |
53 GATT_SERVER_DISCONNECTED_WHILE_RETRIEVING_CHARACTERISTICS)); | 53 GATT_SERVER_DISCONNECTED_WHILE_RETRIEVING_CHARACTERISTICS)); |
54 return; | 54 return; |
55 } | 55 } |
56 | 56 |
57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
58 DCHECK(characteristics); | 58 DCHECK(characteristics); |
59 | 59 |
60 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 60 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
61 DCHECK_EQ(1u, characteristics->size()); | 61 DCHECK_EQ(1u, characteristics->size()); |
62 resolver->resolve(device()->getOrCreateRemoteGATTCharacteristic( | 62 resolver->resolve(m_device->getOrCreateRemoteGATTCharacteristic( |
63 resolver->getExecutionContext(), | 63 resolver->getExecutionContext(), |
64 std::move(characteristics.value()[0]), this)); | 64 std::move(characteristics.value()[0]), this)); |
65 return; | 65 return; |
66 } | 66 } |
67 | 67 |
68 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; | 68 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; |
69 gattCharacteristics.reserveInitialCapacity(characteristics->size()); | 69 gattCharacteristics.reserveInitialCapacity(characteristics->size()); |
70 for (auto& characteristic : characteristics.value()) { | 70 for (auto& characteristic : characteristics.value()) { |
71 gattCharacteristics.push_back( | 71 gattCharacteristics.push_back( |
72 device()->getOrCreateRemoteGATTCharacteristic( | 72 m_device->getOrCreateRemoteGATTCharacteristic( |
73 resolver->getExecutionContext(), std::move(characteristic), | 73 resolver->getExecutionContext(), std::move(characteristic), |
74 this)); | 74 this)); |
75 } | 75 } |
76 resolver->resolve(gattCharacteristics); | 76 resolver->resolve(gattCharacteristics); |
77 } else { | 77 } else { |
78 if (result == mojom::blink::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND) { | 78 if (result == mojom::blink::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND) { |
79 resolver->reject(BluetoothError::createDOMException( | 79 resolver->reject(BluetoothError::createDOMException( |
80 BluetoothErrorCode::CharacteristicNotFound, | 80 BluetoothErrorCode::CharacteristicNotFound, |
81 "No Characteristics matching UUID " + requestedCharacteristicUUID + | 81 "No Characteristics matching UUID " + requestedCharacteristicUUID + |
82 " found in Service with UUID " + uuid() + ".")); | 82 " found in Service with UUID " + uuid() + ".")); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 ScriptState* scriptState, | 118 ScriptState* scriptState, |
119 ExceptionState&) { | 119 ExceptionState&) { |
120 return getCharacteristicsImpl( | 120 return getCharacteristicsImpl( |
121 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 121 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
122 } | 122 } |
123 | 123 |
124 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( | 124 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
125 ScriptState* scriptState, | 125 ScriptState* scriptState, |
126 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 126 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
127 const String& characteristicsUUID) { | 127 const String& characteristicsUUID) { |
128 if (!device()->gatt()->connected()) { | 128 if (!m_device->gatt()->connected()) { |
129 return ScriptPromise::rejectWithDOMException( | 129 return ScriptPromise::rejectWithDOMException( |
130 scriptState, | 130 scriptState, |
131 BluetoothError::createDOMException( | 131 BluetoothError::createDOMException( |
132 mojom::blink::WebBluetoothResult:: | 132 mojom::blink::WebBluetoothResult:: |
133 GATT_SERVER_NOT_CONNECTED_CANNOT_RETRIEVE_CHARACTERISTICS)); | 133 GATT_SERVER_NOT_CONNECTED_CANNOT_RETRIEVE_CHARACTERISTICS)); |
134 } | 134 } |
135 | 135 |
136 if (!device()->isValidService(m_service->instance_id)) { | 136 if (!m_device->isValidService(m_service->instance_id)) { |
137 return ScriptPromise::rejectWithDOMException( | 137 return ScriptPromise::rejectWithDOMException( |
138 scriptState, BluetoothError::createDOMException( | 138 scriptState, BluetoothError::createDOMException( |
139 BluetoothErrorCode::InvalidService, | 139 BluetoothErrorCode::InvalidService, |
140 "Service with UUID " + m_service->uuid + | 140 "Service with UUID " + m_service->uuid + |
141 " is no longer valid. Remember to retrieve " | 141 " is no longer valid. Remember to retrieve " |
142 "the service again after reconnecting.")); | 142 "the service again after reconnecting.")); |
143 } | 143 } |
144 | 144 |
145 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 145 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
146 ScriptPromise promise = resolver->promise(); | 146 ScriptPromise promise = resolver->promise(); |
147 device()->gatt()->AddToActiveAlgorithms(resolver); | 147 m_device->gatt()->AddToActiveAlgorithms(resolver); |
148 | 148 |
149 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 149 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
150 service->RemoteServiceGetCharacteristics( | 150 service->RemoteServiceGetCharacteristics( |
151 m_service->instance_id, quantity, characteristicsUUID, | 151 m_service->instance_id, quantity, characteristicsUUID, |
152 convertToBaseCallback( | 152 convertToBaseCallback( |
153 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, | 153 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, |
154 wrapPersistent(this), m_service->instance_id, | 154 wrapPersistent(this), m_service->instance_id, |
155 characteristicsUUID, quantity, wrapPersistent(resolver)))); | 155 characteristicsUUID, quantity, wrapPersistent(resolver)))); |
156 | 156 |
157 return promise; | 157 return promise; |
158 } | 158 } |
159 | 159 |
160 } // namespace blink | 160 } // namespace blink |
OLD | NEW |