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 mojom::blink::WebBluetoothDevicePtr device, | 22 mojom::blink::WebBluetoothDevicePtr device, |
23 Bluetooth* bluetooth) | 23 Bluetooth* bluetooth) |
24 : ContextLifecycleObserver(context), | 24 : ContextLifecycleObserver(context), |
25 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)), | 25 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)), |
26 m_device(std::move(device)), | 26 m_device(std::move(device)), |
27 m_gatt(BluetoothRemoteGATTServer::create(this)), | 27 m_gatt(BluetoothRemoteGATTServer::create(context, this)), |
28 m_bluetooth(bluetooth) {} | 28 m_bluetooth(bluetooth) {} |
29 | 29 |
30 // static | 30 // static |
31 BluetoothDevice* BluetoothDevice::take( | 31 BluetoothDevice* BluetoothDevice::take( |
32 ScriptPromiseResolver* resolver, | 32 ScriptPromiseResolver* resolver, |
33 mojom::blink::WebBluetoothDevicePtr device, | 33 mojom::blink::WebBluetoothDevicePtr device, |
34 Bluetooth* bluetooth) { | 34 Bluetooth* bluetooth) { |
35 return new BluetoothDevice(resolver->getExecutionContext(), std::move(device), | 35 return new BluetoothDevice(resolver->getExecutionContext(), std::move(device), |
36 bluetooth); | 36 bluetooth); |
37 } | 37 } |
(...skipping 30 matching lines...) Expand all Loading... |
68 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 68 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, |
69 BluetoothRemoteGATTCharacteristic* characteristic) { | 69 BluetoothRemoteGATTCharacteristic* characteristic) { |
70 return m_attributeInstanceMap->getOrCreateBluetoothRemoteGATTDescriptor( | 70 return m_attributeInstanceMap->getOrCreateBluetoothRemoteGATTDescriptor( |
71 std::move(descriptor), characteristic); | 71 std::move(descriptor), characteristic); |
72 } | 72 } |
73 | 73 |
74 bool BluetoothDevice::isValidDescriptor(const String& descriptorInstanceId) { | 74 bool BluetoothDevice::isValidDescriptor(const String& descriptorInstanceId) { |
75 return m_attributeInstanceMap->containsDescriptor(descriptorInstanceId); | 75 return m_attributeInstanceMap->containsDescriptor(descriptorInstanceId); |
76 } | 76 } |
77 | 77 |
78 void BluetoothDevice::dispose() { | 78 void BluetoothDevice::clearAttributeInstanceMapAndFireEvent() { |
79 disconnectGATTIfConnected(); | |
80 } | |
81 | |
82 void BluetoothDevice::contextDestroyed(ExecutionContext*) { | |
83 disconnectGATTIfConnected(); | |
84 } | |
85 | |
86 void BluetoothDevice::disconnectGATTIfConnected() { | |
87 if (m_gatt->connected()) { | |
88 m_gatt->setConnected(false); | |
89 m_gatt->ClearActiveAlgorithms(); | |
90 m_bluetooth->removeFromConnectedDevicesMap(id()); | |
91 mojom::blink::WebBluetoothService* service = m_bluetooth->service(); | |
92 service->RemoteServerDisconnect(id()); | |
93 } | |
94 } | |
95 | |
96 void BluetoothDevice::cleanupDisconnectedDeviceAndFireEvent() { | |
97 DCHECK(m_gatt->connected()); | |
98 m_gatt->setConnected(false); | |
99 m_gatt->ClearActiveAlgorithms(); | |
100 m_attributeInstanceMap->Clear(); | 79 m_attributeInstanceMap->Clear(); |
101 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected)); | 80 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected)); |
102 } | 81 } |
103 | 82 |
104 const WTF::AtomicString& BluetoothDevice::interfaceName() const { | 83 const WTF::AtomicString& BluetoothDevice::interfaceName() const { |
105 return EventTargetNames::BluetoothDevice; | 84 return EventTargetNames::BluetoothDevice; |
106 } | 85 } |
107 | 86 |
108 ExecutionContext* BluetoothDevice::getExecutionContext() const { | 87 ExecutionContext* BluetoothDevice::getExecutionContext() const { |
109 return ContextLifecycleObserver::getExecutionContext(); | 88 return ContextLifecycleObserver::getExecutionContext(); |
110 } | 89 } |
111 | 90 |
112 void BluetoothDevice::dispatchGattServerDisconnected() { | |
113 if (!m_gatt->connected()) { | |
114 return; | |
115 } | |
116 cleanupDisconnectedDeviceAndFireEvent(); | |
117 } | |
118 | |
119 DEFINE_TRACE(BluetoothDevice) { | 91 DEFINE_TRACE(BluetoothDevice) { |
120 visitor->trace(m_attributeInstanceMap); | 92 visitor->trace(m_attributeInstanceMap); |
121 visitor->trace(m_gatt); | 93 visitor->trace(m_gatt); |
122 visitor->trace(m_bluetooth); | 94 visitor->trace(m_bluetooth); |
123 EventTargetWithInlineData::trace(visitor); | 95 EventTargetWithInlineData::trace(visitor); |
124 ContextLifecycleObserver::trace(visitor); | 96 ContextLifecycleObserver::trace(visitor); |
125 } | 97 } |
126 | 98 |
127 } // namespace blink | 99 } // namespace blink |
OLD | NEW |