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