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/Bluetooth.h" | 5 #include "modules/bluetooth/Bluetooth.h" |
6 | 6 |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 options.optionalServices()) { | 123 options.optionalServices()) { |
124 const String& validatedOptionalService = | 124 const String& validatedOptionalService = |
125 BluetoothUUID::getService(optionalService, exceptionState); | 125 BluetoothUUID::getService(optionalService, exceptionState); |
126 if (exceptionState.hadException()) | 126 if (exceptionState.hadException()) |
127 return; | 127 return; |
128 result->optional_services.push_back(validatedOptionalService); | 128 result->optional_services.push_back(validatedOptionalService); |
129 } | 129 } |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 void Bluetooth::Dispose() { | |
134 // The pipe to this object must be closed when is marked unreachable to | |
135 // prevent messages from being dispatched before lazy sweeping. | |
136 if (m_clientBinding.is_bound()) | |
137 m_clientBinding.Close(); | |
138 } | |
139 | |
140 void Bluetooth::RequestDeviceCallback( | 133 void Bluetooth::RequestDeviceCallback( |
141 ScriptPromiseResolver* resolver, | 134 ScriptPromiseResolver* resolver, |
142 mojom::blink::WebBluetoothResult result, | 135 mojom::blink::WebBluetoothResult result, |
143 mojom::blink::WebBluetoothDevicePtr device) { | 136 mojom::blink::WebBluetoothDevicePtr device) { |
144 if (!resolver->getExecutionContext() || | 137 if (!resolver->getExecutionContext() || |
145 resolver->getExecutionContext()->isContextDestroyed()) | 138 resolver->getExecutionContext()->isContextDestroyed()) |
146 return; | 139 return; |
147 | 140 |
148 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 141 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
149 BluetoothDevice* bluetoothDevice = | 142 BluetoothDevice* bluetoothDevice = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (!m_service) { | 174 if (!m_service) { |
182 InterfaceProvider* interfaceProvider = nullptr; | 175 InterfaceProvider* interfaceProvider = nullptr; |
183 if (context->isDocument()) { | 176 if (context->isDocument()) { |
184 Document* document = toDocument(context); | 177 Document* document = toDocument(context); |
185 if (document->frame()) | 178 if (document->frame()) |
186 interfaceProvider = document->frame()->interfaceProvider(); | 179 interfaceProvider = document->frame()->interfaceProvider(); |
187 } | 180 } |
188 | 181 |
189 if (interfaceProvider) | 182 if (interfaceProvider) |
190 interfaceProvider->getInterface(mojo::MakeRequest(&m_service)); | 183 interfaceProvider->getInterface(mojo::MakeRequest(&m_service)); |
191 | |
192 if (m_service) { | |
193 // Create an associated interface ptr and pass it to the | |
194 // WebBluetoothService so that it can send us events without us | |
195 // prompting. | |
196 mojom::blink::WebBluetoothServiceClientAssociatedPtrInfo ptrInfo; | |
197 m_clientBinding.Bind(&ptrInfo); | |
198 m_service->SetClient(std::move(ptrInfo)); | |
199 } | |
200 } | 184 } |
201 | 185 |
202 if (!m_service) { | 186 if (!m_service) { |
203 return ScriptPromise::rejectWithDOMException( | 187 return ScriptPromise::rejectWithDOMException( |
204 scriptState, DOMException::create(NotSupportedError)); | 188 scriptState, DOMException::create(NotSupportedError)); |
205 } | 189 } |
206 | 190 |
207 // In order to convert the arguments from service names and aliases to just | 191 // In order to convert the arguments from service names and aliases to just |
208 // UUIDs, do the following substeps: | 192 // UUIDs, do the following substeps: |
209 auto deviceOptions = mojom::blink::WebBluetoothRequestDeviceOptions::New(); | 193 auto deviceOptions = mojom::blink::WebBluetoothRequestDeviceOptions::New(); |
(...skipping 12 matching lines...) Expand all Loading... |
222 ScriptPromise promise = resolver->promise(); | 206 ScriptPromise promise = resolver->promise(); |
223 | 207 |
224 m_service->RequestDevice( | 208 m_service->RequestDevice( |
225 std::move(deviceOptions), | 209 std::move(deviceOptions), |
226 convertToBaseCallback(WTF::bind(&Bluetooth::RequestDeviceCallback, | 210 convertToBaseCallback(WTF::bind(&Bluetooth::RequestDeviceCallback, |
227 wrapPersistent(this), | 211 wrapPersistent(this), |
228 wrapPersistent(resolver)))); | 212 wrapPersistent(resolver)))); |
229 return promise; | 213 return promise; |
230 } | 214 } |
231 | 215 |
232 void Bluetooth::AddToConnectedDevicesMap(const String& deviceId, | |
233 BluetoothDevice* device) { | |
234 m_connectedDevices.insert(deviceId, device); | |
235 } | |
236 | |
237 void Bluetooth::RemoveFromConnectedDevicesMap(const String& deviceId) { | |
238 m_connectedDevices.remove(deviceId); | |
239 } | |
240 | |
241 void Bluetooth::RegisterCharacteristicObject( | |
242 const String& characteristicInstanceId, | |
243 BluetoothRemoteGATTCharacteristic* characteristic) { | |
244 m_activeCharacteristics.insert(characteristicInstanceId, characteristic); | |
245 } | |
246 | |
247 void Bluetooth::CharacteristicObjectRemoved( | |
248 const String& characteristicInstanceId) { | |
249 m_activeCharacteristics.remove(characteristicInstanceId); | |
250 } | |
251 | |
252 DEFINE_TRACE(Bluetooth) { | 216 DEFINE_TRACE(Bluetooth) { |
253 visitor->trace(m_deviceInstanceMap); | 217 visitor->trace(m_deviceInstanceMap); |
254 visitor->trace(m_activeCharacteristics); | |
255 visitor->trace(m_connectedDevices); | |
256 } | 218 } |
257 | 219 |
258 Bluetooth::Bluetooth() : m_clientBinding(this) {} | 220 Bluetooth::Bluetooth() {} |
259 | |
260 void Bluetooth::RemoteCharacteristicValueChanged( | |
261 const WTF::String& characteristicInstanceId, | |
262 const WTF::Vector<uint8_t>& value) { | |
263 BluetoothRemoteGATTCharacteristic* characteristic = | |
264 m_activeCharacteristics.at(characteristicInstanceId); | |
265 if (characteristic) | |
266 characteristic->DispatchCharacteristicValueChanged(value); | |
267 } | |
268 | |
269 void Bluetooth::GattServerDisconnected(const WTF::String& deviceId) { | |
270 BluetoothDevice* device = m_connectedDevices.at(deviceId); | |
271 if (device) { | |
272 // Remove device from the map before calling dispatchGattServerDisconnected | |
273 // to avoid removing a device the gattserverdisconnected event handler might | |
274 // have re-connected. | |
275 m_connectedDevices.remove(deviceId); | |
276 device->DispatchGattServerDisconnected(); | |
277 } | |
278 } | |
279 | 221 |
280 BluetoothDevice* Bluetooth::GetBluetoothDeviceRepresentingDevice( | 222 BluetoothDevice* Bluetooth::GetBluetoothDeviceRepresentingDevice( |
281 mojom::blink::WebBluetoothDevicePtr devicePtr, | 223 mojom::blink::WebBluetoothDevicePtr devicePtr, |
282 ScriptPromiseResolver* resolver) { | 224 ScriptPromiseResolver* resolver) { |
283 WTF::String id = devicePtr->id; | 225 WTF::String id = devicePtr->id; |
284 BluetoothDevice* device = m_deviceInstanceMap.at(id); | 226 BluetoothDevice* device = m_deviceInstanceMap.at(id); |
285 if (!device) { | 227 if (!device) { |
286 device = BluetoothDevice::take(resolver, std::move(devicePtr), this); | 228 device = BluetoothDevice::take(resolver, std::move(devicePtr), this); |
287 auto result = m_deviceInstanceMap.insert(id, device); | 229 auto result = m_deviceInstanceMap.insert(id, device); |
288 DCHECK(result.isNewEntry); | 230 DCHECK(result.isNewEntry); |
289 } | 231 } |
290 return device; | 232 return device; |
291 } | 233 } |
292 | 234 |
293 } // namespace blink | 235 } // namespace blink |
OLD | NEW |