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