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

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

Issue 2565913002: [Onion Soup] Move WebBluetoothImpl from //content/renderer/bluetooth to Blink's bluetooth module (Closed)
Patch Set: fixed layout tests Created 4 years 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/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"
11 #include "core/inspector/ConsoleMessage.h" 11 #include "core/inspector/ConsoleMessage.h"
12 #include "modules/bluetooth/Bluetooth.h"
12 #include "modules/bluetooth/BluetoothError.h" 13 #include "modules/bluetooth/BluetoothError.h"
13 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h"
14 #include "modules/bluetooth/BluetoothSupplement.h"
15 #include "modules/bluetooth/BluetoothUUID.h" 15 #include "modules/bluetooth/BluetoothUUID.h"
16 #include "public/platform/modules/bluetooth/WebBluetooth.h" 16 #include "modules/bluetooth/BluetoothUtils.h"
17 #include "wtf/PtrUtil.h" 17 #include "wtf/PtrUtil.h"
18 #include <memory> 18 #include <memory>
19 #include <utility> 19 #include <utility>
20 20
21 namespace blink { 21 namespace blink {
22 22
23 namespace { 23 namespace {
24 24
25 const char kGATTServerDisconnected[] = 25 const char kGATTServerDisconnected[] =
26 "GATT Server disconnected while retrieving characteristics."; 26 "GATT Server disconnected while retrieving characteristics.";
27 const char kGATTServerNotConnected[] = 27 const char kGATTServerNotConnected[] =
28 "GATT Server is disconnected. Cannot retrieve characteristics."; 28 "GATT Server is disconnected. Cannot retrieve characteristics.";
29 const char kInvalidService[] = 29 const char kInvalidService[] =
30 "Service is no longer valid. Remember to retrieve the service again after " 30 "Service is no longer valid. Remember to retrieve the service again after "
31 "reconnecting."; 31 "reconnecting.";
32 32
33 } // namespace 33 } // namespace
34 34
35 BluetoothRemoteGATTService::BluetoothRemoteGATTService( 35 BluetoothRemoteGATTService::BluetoothRemoteGATTService(
36 std::unique_ptr<WebBluetoothRemoteGATTService> webService, 36 const String& serviceInstanceId,
37 const String& uuid,
38 bool isPrimary,
39 const String& deviceInstanceId,
37 BluetoothDevice* device) 40 BluetoothDevice* device)
38 : m_webService(std::move(webService)), m_device(device) { 41 : m_serviceInstanceId(serviceInstanceId),
39 DCHECK(m_webService); 42 m_uuid(uuid),
40 } 43 m_isPrimary(isPrimary),
44 m_deviceInstanceId(deviceInstanceId),
45 m_device(device) {}
41 46
42 DEFINE_TRACE(BluetoothRemoteGATTService) { 47 DEFINE_TRACE(BluetoothRemoteGATTService) {
43 visitor->trace(m_device); 48 visitor->trace(m_device);
44 } 49 }
45 50
46 // Class that allows us to resolve the promise with a single Characteristic or 51 // Callback that allows us to resolve the promise with a single Characteristic
47 // with a vector owning the characteristics. 52 // or with a vector owning the characteristics.
48 class GetCharacteristicsCallback 53 void BluetoothRemoteGATTService::GetCharacteristicsCallback(
49 : public WebBluetoothGetCharacteristicsCallbacks { 54 const String& serviceInstanceId,
50 public: 55 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
51 GetCharacteristicsCallback( 56 ScriptPromiseResolver* resolver,
52 BluetoothRemoteGATTService* service, 57 mojom::blink::WebBluetoothResult result,
53 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 58 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>>
54 ScriptPromiseResolver* resolver) 59 characteristics) {
55 : m_service(service), m_quantity(quantity), m_resolver(resolver) { 60 if (!resolver->getExecutionContext() ||
56 // We always check that the device is connected before constructing this 61 resolver->getExecutionContext()->isContextDestroyed())
57 // object. 62 return;
Reilly Grant (use Gerrit) 2016/12/16 01:41:29 This check is unnecessary. It is performed by Scri
juncai 2016/12/17 01:18:52 Keep this check if the callbacks do more than Scri
58 CHECK(m_service->device()->gatt()->connected());
59 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get());
60 }
61 63
62 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& 64 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
63 webCharacteristics) override {
64 if (!m_resolver->getExecutionContext() ||
65 m_resolver->getExecutionContext()->isContextDestroyed())
66 return;
67
68 // If the resolver is not in the set of ActiveAlgorithms then the frame 65 // If the resolver is not in the set of ActiveAlgorithms then the frame
69 // disconnected so we reject. 66 // disconnected so we reject.
70 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( 67 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) {
71 m_resolver.get())) { 68 resolver->reject(
72 m_resolver->reject(
73 DOMException::create(NetworkError, kGATTServerDisconnected)); 69 DOMException::create(NetworkError, kGATTServerDisconnected));
74 return; 70 return;
75 } 71 }
76 72
77 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { 73 DCHECK(characteristics);
dougt 2016/12/16 00:11:45 I don't understand this check. characteristics is
juncai 2016/12/17 01:18:52 This DCHECK() is done inside the above if statemen
78 DCHECK_EQ(1u, webCharacteristics.size()); 74
79 m_resolver->resolve( 75 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) {
80 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( 76 DCHECK_EQ(1u, characteristics->size());
81 m_resolver->getExecutionContext(), 77 resolver->resolve(device()->getOrCreateBluetoothRemoteGATTCharacteristic(
82 WTF::wrapUnique(webCharacteristics[0]), m_service)); 78 resolver->getExecutionContext(),
79 characteristics.value()[0]->instance_id, serviceInstanceId,
80 characteristics.value()[0]->uuid,
81 characteristics.value()[0]->properties, this));
83 return; 82 return;
84 } 83 }
85 84
86 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; 85 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics;
87 characteristics.reserveInitialCapacity(webCharacteristics.size()); 86 gattCharacteristics.reserveInitialCapacity(characteristics->size());
88 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : 87 for (const auto& characteristic : characteristics.value()) {
89 webCharacteristics) { 88 gattCharacteristics.append(
90 characteristics.append( 89 device()->getOrCreateBluetoothRemoteGATTCharacteristic(
91 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( 90 resolver->getExecutionContext(), characteristic->instance_id,
92 m_resolver->getExecutionContext(), 91 serviceInstanceId, characteristic->uuid,
93 WTF::wrapUnique(webCharacteristic), m_service)); 92 characteristic->properties, this));
94 } 93 }
95 m_resolver->resolve(characteristics); 94 resolver->resolve(gattCharacteristics);
96 } 95 } else {
97
98 void onError(
99 int32_t
100 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
101 override {
102 if (!m_resolver->getExecutionContext() ||
103 m_resolver->getExecutionContext()->isContextDestroyed())
104 return;
105
106 // If the resolver is not in the set of ActiveAlgorithms then the frame 96 // If the resolver is not in the set of ActiveAlgorithms then the frame
107 // disconnected so we reject. 97 // disconnected so we reject.
108 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( 98 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) {
109 m_resolver.get())) { 99 resolver->reject(
110 m_resolver->reject(
111 DOMException::create(NetworkError, kGATTServerDisconnected)); 100 DOMException::create(NetworkError, kGATTServerDisconnected));
112 return; 101 return;
113 } 102 }
114 103
115 m_resolver->reject(BluetoothError::take(m_resolver, error)); 104 resolver->reject(BluetoothError::take(resolver, static_cast<int>(result)));
116 } 105 }
117 106 }
118 private:
119 Persistent<BluetoothRemoteGATTService> m_service;
120 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity;
121 const Persistent<ScriptPromiseResolver> m_resolver;
122 };
123 107
124 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( 108 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(
125 ScriptState* scriptState, 109 ScriptState* scriptState,
126 const StringOrUnsignedLong& characteristic, 110 const StringOrUnsignedLong& characteristic,
127 ExceptionState& exceptionState) { 111 ExceptionState& exceptionState) {
128 String characteristicUUID = 112 String characteristicUUID =
129 BluetoothUUID::getCharacteristic(characteristic, exceptionState); 113 BluetoothUUID::getCharacteristic(characteristic, exceptionState);
130 if (exceptionState.hadException()) 114 if (exceptionState.hadException())
131 return exceptionState.reject(scriptState); 115 return exceptionState.reject(scriptState);
132 116
(...skipping 26 matching lines...) Expand all
159 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( 143 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(
160 ScriptState* scriptState, 144 ScriptState* scriptState,
161 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 145 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
162 String characteristicsUUID) { 146 String characteristicsUUID) {
163 if (!device()->gatt()->connected()) { 147 if (!device()->gatt()->connected()) {
164 return ScriptPromise::rejectWithDOMException( 148 return ScriptPromise::rejectWithDOMException(
165 scriptState, 149 scriptState,
166 DOMException::create(NetworkError, kGATTServerNotConnected)); 150 DOMException::create(NetworkError, kGATTServerNotConnected));
167 } 151 }
168 152
169 if (!device()->isValidService(m_webService->serviceInstanceID)) { 153 if (!device()->isValidService(m_serviceInstanceId)) {
170 return ScriptPromise::rejectWithDOMException( 154 return ScriptPromise::rejectWithDOMException(
171 scriptState, DOMException::create(InvalidStateError, kInvalidService)); 155 scriptState, DOMException::create(InvalidStateError, kInvalidService));
172 } 156 }
173 157
174 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 158 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
175 ScriptPromise promise = resolver->promise(); 159 ScriptPromise promise = resolver->promise();
176 160
177 WebBluetooth* webbluetooth = 161 // We always check that the device is connected.
178 BluetoothSupplement::fromScriptState(scriptState); 162 CHECK(device()->gatt()->connected());
179 webbluetooth->getCharacteristics( 163 device()->gatt()->AddToActiveAlgorithms(resolver);
180 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), 164
181 characteristicsUUID, 165 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
182 new GetCharacteristicsCallback(this, quantity, resolver)); 166 bluetooth::mojom::blink::UUIDPtr characteristicsUUIDPtr = nullptr;
167 if (!characteristicsUUID.isEmpty())
168 characteristicsUUIDPtr = createUUIDPtr(characteristicsUUID);
169 service->RemoteServiceGetCharacteristics(
170 m_serviceInstanceId, quantity, std::move(characteristicsUUIDPtr),
171 convertToBaseCallback(
172 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback,
173 wrapPersistent(this), m_serviceInstanceId, quantity,
174 wrapPersistent(resolver))));
183 175
184 return promise; 176 return promise;
185 } 177 }
186 178
187 } // namespace blink 179 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698