| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/BluetoothDevice.h" | 5 #include "modules/bluetooth/BluetoothDevice.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" |
| 11 #include "core/events/Event.h" | 11 #include "core/events/Event.h" |
| 12 #include "modules/bluetooth/Bluetooth.h" | 12 #include "modules/bluetooth/Bluetooth.h" |
| 13 #include "modules/bluetooth/BluetoothAttributeInstanceMap.h" | 13 #include "modules/bluetooth/BluetoothAttributeInstanceMap.h" |
| 14 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
| 15 #include "modules/bluetooth/BluetoothRemoteGATTServer.h" | 15 #include "modules/bluetooth/BluetoothRemoteGATTServer.h" |
| 16 #include <memory> | 16 #include <memory> |
| 17 #include <utility> | 17 #include <utility> |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 BluetoothDevice::BluetoothDevice(ExecutionContext* context, | 21 BluetoothDevice::BluetoothDevice(ExecutionContext* context, |
| 22 const String& id, | 22 mojom::blink::WebBluetoothDevicePtr device, |
| 23 const String& name, | |
| 24 Bluetooth* bluetooth) | 23 Bluetooth* bluetooth) |
| 25 : ContextLifecycleObserver(context), | 24 : ContextLifecycleObserver(context), |
| 26 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)), | 25 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)), |
| 27 m_id(id), | 26 m_device(std::move(device)), |
| 28 m_name(name), | |
| 29 m_gatt(BluetoothRemoteGATTServer::create(this)), | 27 m_gatt(BluetoothRemoteGATTServer::create(this)), |
| 30 m_bluetooth(bluetooth) {} | 28 m_bluetooth(bluetooth) {} |
| 31 | 29 |
| 32 // static | 30 // static |
| 33 BluetoothDevice* BluetoothDevice::take(ScriptPromiseResolver* resolver, | 31 BluetoothDevice* BluetoothDevice::take( |
| 34 const String& id, | 32 ScriptPromiseResolver* resolver, |
| 35 const String& name, | 33 mojom::blink::WebBluetoothDevicePtr device, |
| 36 Bluetooth* bluetooth) { | 34 Bluetooth* bluetooth) { |
| 37 return new BluetoothDevice(resolver->getExecutionContext(), id, name, | 35 return new BluetoothDevice(resolver->getExecutionContext(), std::move(device), |
| 38 bluetooth); | 36 bluetooth); |
| 39 } | 37 } |
| 40 | 38 |
| 41 // static | |
| 42 mojom::blink::WebBluetoothDeviceIdPtr BluetoothDevice::createMojoDeviceId( | |
| 43 const String& deviceId) { | |
| 44 auto result = mojom::blink::WebBluetoothDeviceId::New(); | |
| 45 result->device_id = deviceId; | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 49 BluetoothRemoteGATTService* BluetoothDevice::getOrCreateRemoteGATTService( | 39 BluetoothRemoteGATTService* BluetoothDevice::getOrCreateRemoteGATTService( |
| 50 const String& serviceInstanceId, | 40 mojom::blink::WebBluetoothRemoteGATTServicePtr service, |
| 51 const String& uuid, | |
| 52 bool isPrimary, | 41 bool isPrimary, |
| 53 const String& deviceInstanceId) { | 42 const String& deviceInstanceId) { |
| 54 return m_attributeInstanceMap->getOrCreateRemoteGATTService( | 43 return m_attributeInstanceMap->getOrCreateRemoteGATTService( |
| 55 serviceInstanceId, uuid, isPrimary, deviceInstanceId); | 44 std::move(service), isPrimary, deviceInstanceId); |
| 56 } | 45 } |
| 57 | 46 |
| 58 bool BluetoothDevice::isValidService(const String& serviceInstanceId) { | 47 bool BluetoothDevice::isValidService(const String& serviceInstanceId) { |
| 59 return m_attributeInstanceMap->containsService(serviceInstanceId); | 48 return m_attributeInstanceMap->containsService(serviceInstanceId); |
| 60 } | 49 } |
| 61 | 50 |
| 62 BluetoothRemoteGATTCharacteristic* | 51 BluetoothRemoteGATTCharacteristic* |
| 63 BluetoothDevice::getOrCreateRemoteGATTCharacteristic( | 52 BluetoothDevice::getOrCreateRemoteGATTCharacteristic( |
| 64 ExecutionContext* context, | 53 ExecutionContext* context, |
| 65 const String& characteristicInstanceId, | |
| 66 const String& serviceInstanceId, | 54 const String& serviceInstanceId, |
| 67 const String& uuid, | 55 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 68 uint32_t characteristicProperties, | |
| 69 BluetoothRemoteGATTService* service) { | 56 BluetoothRemoteGATTService* service) { |
| 70 return m_attributeInstanceMap->getOrCreateRemoteGATTCharacteristic( | 57 return m_attributeInstanceMap->getOrCreateRemoteGATTCharacteristic( |
| 71 context, characteristicInstanceId, serviceInstanceId, uuid, | 58 context, serviceInstanceId, std::move(characteristic), service); |
| 72 characteristicProperties, service); | |
| 73 } | 59 } |
| 74 | 60 |
| 75 bool BluetoothDevice::isValidCharacteristic( | 61 bool BluetoothDevice::isValidCharacteristic( |
| 76 const String& characteristicInstanceId) { | 62 const String& characteristicInstanceId) { |
| 77 return m_attributeInstanceMap->containsCharacteristic( | 63 return m_attributeInstanceMap->containsCharacteristic( |
| 78 characteristicInstanceId); | 64 characteristicInstanceId); |
| 79 } | 65 } |
| 80 | 66 |
| 81 void BluetoothDevice::dispose() { | 67 void BluetoothDevice::dispose() { |
| 82 disconnectGATTIfConnected(); | 68 disconnectGATTIfConnected(); |
| 83 } | 69 } |
| 84 | 70 |
| 85 void BluetoothDevice::contextDestroyed() { | 71 void BluetoothDevice::contextDestroyed() { |
| 86 disconnectGATTIfConnected(); | 72 disconnectGATTIfConnected(); |
| 87 } | 73 } |
| 88 | 74 |
| 89 void BluetoothDevice::disconnectGATTIfConnected() { | 75 void BluetoothDevice::disconnectGATTIfConnected() { |
| 90 if (m_gatt->connected()) { | 76 if (m_gatt->connected()) { |
| 91 m_gatt->setConnected(false); | 77 m_gatt->setConnected(false); |
| 92 m_gatt->ClearActiveAlgorithms(); | 78 m_gatt->ClearActiveAlgorithms(); |
| 93 m_bluetooth->removeDevice(id()); | 79 m_bluetooth->removeDevice(id()); |
| 94 mojom::blink::WebBluetoothService* service = m_bluetooth->service(); | 80 mojom::blink::WebBluetoothService* service = m_bluetooth->service(); |
| 95 auto deviceId = mojom::blink::WebBluetoothDeviceId::New(); | 81 service->RemoteServerDisconnect(id()); |
| 96 deviceId->device_id = id(); | |
| 97 service->RemoteServerDisconnect(std::move(deviceId)); | |
| 98 } | 82 } |
| 99 } | 83 } |
| 100 | 84 |
| 101 void BluetoothDevice::cleanupDisconnectedDeviceAndFireEvent() { | 85 void BluetoothDevice::cleanupDisconnectedDeviceAndFireEvent() { |
| 102 DCHECK(m_gatt->connected()); | 86 DCHECK(m_gatt->connected()); |
| 103 m_gatt->setConnected(false); | 87 m_gatt->setConnected(false); |
| 104 m_gatt->ClearActiveAlgorithms(); | 88 m_gatt->ClearActiveAlgorithms(); |
| 105 m_attributeInstanceMap->Clear(); | 89 m_attributeInstanceMap->Clear(); |
| 106 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected)); | 90 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected)); |
| 107 } | 91 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 123 | 107 |
| 124 DEFINE_TRACE(BluetoothDevice) { | 108 DEFINE_TRACE(BluetoothDevice) { |
| 125 EventTargetWithInlineData::trace(visitor); | 109 EventTargetWithInlineData::trace(visitor); |
| 126 ContextLifecycleObserver::trace(visitor); | 110 ContextLifecycleObserver::trace(visitor); |
| 127 visitor->trace(m_attributeInstanceMap); | 111 visitor->trace(m_attributeInstanceMap); |
| 128 visitor->trace(m_gatt); | 112 visitor->trace(m_gatt); |
| 129 visitor->trace(m_bluetooth); | 113 visitor->trace(m_bluetooth); |
| 130 } | 114 } |
| 131 | 115 |
| 132 } // namespace blink | 116 } // namespace blink |
| OLD | NEW |