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/BluetoothRemoteGATTServer.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTServer.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/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
12 #include "core/events/Event.h" | 12 #include "core/events/Event.h" |
13 #include "modules/bluetooth/Bluetooth.h" | 13 #include "modules/bluetooth/Bluetooth.h" |
14 #include "modules/bluetooth/BluetoothDevice.h" | 14 #include "modules/bluetooth/BluetoothDevice.h" |
15 #include "modules/bluetooth/BluetoothError.h" | 15 #include "modules/bluetooth/BluetoothError.h" |
16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
17 #include "modules/bluetooth/BluetoothUUID.h" | 17 #include "modules/bluetooth/BluetoothUUID.h" |
18 #include <utility> | 18 #include <utility> |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(ExecutionContext* context, |
23 : m_device(device), m_connected(false) {} | 23 BluetoothDevice* device) |
| 24 : ContextLifecycleObserver(context), m_device(device), m_connected(false) {} |
24 | 25 |
25 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( | 26 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( |
| 27 ExecutionContext* context, |
26 BluetoothDevice* device) { | 28 BluetoothDevice* device) { |
27 return new BluetoothRemoteGATTServer(device); | 29 return new BluetoothRemoteGATTServer(context, device); |
| 30 } |
| 31 |
| 32 void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { |
| 33 Dispose(); |
| 34 } |
| 35 |
| 36 void BluetoothRemoteGATTServer::GATTServerDisconnected() { |
| 37 DispatchDisconnected(); |
28 } | 38 } |
29 | 39 |
30 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( | 40 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
31 ScriptPromiseResolver* resolver) { | 41 ScriptPromiseResolver* resolver) { |
32 auto result = m_activeAlgorithms.insert(resolver); | 42 auto result = m_activeAlgorithms.insert(resolver); |
33 CHECK(result.isNewEntry); | 43 CHECK(result.isNewEntry); |
34 } | 44 } |
35 | 45 |
36 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( | 46 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( |
37 ScriptPromiseResolver* resolver) { | 47 ScriptPromiseResolver* resolver) { |
38 if (!m_activeAlgorithms.contains(resolver)) { | 48 if (!m_activeAlgorithms.contains(resolver)) { |
39 return false; | 49 return false; |
40 } | 50 } |
41 m_activeAlgorithms.erase(resolver); | 51 m_activeAlgorithms.erase(resolver); |
42 return true; | 52 return true; |
43 } | 53 } |
44 | 54 |
| 55 void BluetoothRemoteGATTServer::DisconnectIfConnected() { |
| 56 if (m_connected) { |
| 57 SetConnected(false); |
| 58 ClearActiveAlgorithms(); |
| 59 mojom::blink::WebBluetoothService* service = |
| 60 m_device->bluetooth()->Service(); |
| 61 service->RemoteServerDisconnect(m_device->id()); |
| 62 } |
| 63 } |
| 64 |
| 65 void BluetoothRemoteGATTServer::CleanupDisconnectedDeviceAndFireEvent() { |
| 66 DCHECK(m_connected); |
| 67 SetConnected(false); |
| 68 ClearActiveAlgorithms(); |
| 69 m_device->ClearAttributeInstanceMapAndFireEvent(); |
| 70 } |
| 71 |
| 72 void BluetoothRemoteGATTServer::DispatchDisconnected() { |
| 73 if (!m_connected) { |
| 74 return; |
| 75 } |
| 76 CleanupDisconnectedDeviceAndFireEvent(); |
| 77 } |
| 78 |
| 79 void BluetoothRemoteGATTServer::Dispose() { |
| 80 DisconnectIfConnected(); |
| 81 // The pipe to this object must be closed when is marked unreachable to |
| 82 // prevent messages from being dispatched before lazy sweeping. |
| 83 CloseClientBindings(); |
| 84 } |
| 85 |
45 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 86 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
46 visitor->trace(m_activeAlgorithms); | 87 visitor->trace(m_activeAlgorithms); |
47 visitor->trace(m_device); | 88 visitor->trace(m_device); |
| 89 ContextLifecycleObserver::trace(visitor); |
48 } | 90 } |
49 | 91 |
50 void BluetoothRemoteGATTServer::ConnectCallback( | 92 void BluetoothRemoteGATTServer::ConnectCallback( |
51 ScriptPromiseResolver* resolver, | 93 ScriptPromiseResolver* resolver, |
52 mojom::blink::WebBluetoothResult result) { | 94 mojom::blink::WebBluetoothResult result) { |
53 if (!resolver->getExecutionContext() || | 95 if (!resolver->getExecutionContext() || |
54 resolver->getExecutionContext()->isContextDestroyed()) | 96 resolver->getExecutionContext()->isContextDestroyed()) |
55 return; | 97 return; |
56 | 98 |
57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 99 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
58 m_device->bluetooth()->AddToConnectedDevicesMap(m_device->id(), m_device); | |
59 SetConnected(true); | 100 SetConnected(true); |
60 resolver->resolve(this); | 101 resolver->resolve(this); |
61 } else { | 102 } else { |
62 resolver->reject(BluetoothError::CreateDOMException(result)); | 103 resolver->reject(BluetoothError::CreateDOMException(result)); |
63 } | 104 } |
64 } | 105 } |
65 | 106 |
66 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 107 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 108 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
68 ScriptPromise promise = resolver->promise(); | 109 ScriptPromise promise = resolver->promise(); |
69 | 110 |
70 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 111 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 112 mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; |
| 113 std::unique_ptr< |
| 114 mojo::AssociatedBinding<mojom::blink::WebBluetoothServerClient>> |
| 115 clientBinding( |
| 116 new mojo::AssociatedBinding<mojom::blink::WebBluetoothServerClient>( |
| 117 this)); |
| 118 clientBinding->Bind(&ptrInfo); |
| 119 m_clientBindings.push_back(std::move(clientBinding)); |
71 service->RemoteServerConnect( | 120 service->RemoteServerConnect( |
72 m_device->id(), convertToBaseCallback(WTF::bind( | 121 m_device->id(), std::move(ptrInfo), |
73 &BluetoothRemoteGATTServer::ConnectCallback, | 122 convertToBaseCallback( |
74 wrapPersistent(this), wrapPersistent(resolver)))); | 123 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, |
| 124 wrapPersistent(this), wrapPersistent(resolver)))); |
75 | 125 |
76 return promise; | 126 return promise; |
77 } | 127 } |
78 | 128 |
79 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 129 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
80 if (!m_connected) | 130 if (!m_connected) |
81 return; | 131 return; |
82 m_device->CleanupDisconnectedDeviceAndFireEvent(); | 132 CleanupDisconnectedDeviceAndFireEvent(); |
83 m_device->bluetooth()->RemoveFromConnectedDevicesMap(m_device->id()); | 133 CloseClientBindings(); |
84 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 134 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
85 service->RemoteServerDisconnect(m_device->id()); | 135 service->RemoteServerDisconnect(m_device->id()); |
86 } | 136 } |
87 | 137 |
88 // Callback that allows us to resolve the promise with a single service or | 138 // Callback that allows us to resolve the promise with a single service or |
89 // with a vector owning the services. | 139 // with a vector owning the services. |
90 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 140 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
91 const String& requestedServiceUUID, | 141 const String& requestedServiceUUID, |
92 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 142 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
93 ScriptPromiseResolver* resolver, | 143 ScriptPromiseResolver* resolver, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 235 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
186 service->RemoteServerGetPrimaryServices( | 236 service->RemoteServerGetPrimaryServices( |
187 m_device->id(), quantity, servicesUUID, | 237 m_device->id(), quantity, servicesUUID, |
188 convertToBaseCallback( | 238 convertToBaseCallback( |
189 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 239 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
190 wrapPersistent(this), servicesUUID, quantity, | 240 wrapPersistent(this), servicesUUID, quantity, |
191 wrapPersistent(resolver)))); | 241 wrapPersistent(resolver)))); |
192 return promise; | 242 return promise; |
193 } | 243 } |
194 | 244 |
| 245 void BluetoothRemoteGATTServer::CloseClientBindings() { |
| 246 for (auto& clientBinding : m_clientBindings) { |
| 247 clientBinding->Close(); |
| 248 } |
| 249 } |
| 250 |
195 } // namespace blink | 251 } // namespace blink |
OLD | NEW |