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" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kGATTServerNotConnected[] = | 24 const char kGATTServerNotConnected[] = |
25 "GATT Server is disconnected. Cannot retrieve services. (Re)connect first " | 25 "GATT Server is disconnected. Cannot retrieve services. (Re)connect first " |
26 "with `device.gatt.connect`."; | 26 "with `device.gatt.connect`."; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 30 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
31 : m_device(device), m_connected(false) {} | 31 : m_clientBinding(this), m_device(device), m_connected(false) {} |
32 | 32 |
33 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create( | 33 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create( |
34 BluetoothDevice* device) { | 34 BluetoothDevice* device) { |
35 return new BluetoothRemoteGATTServer(device); | 35 return new BluetoothRemoteGATTServer(device); |
36 } | 36 } |
37 | 37 |
38 void BluetoothRemoteGATTServer::GATTServerDisconnected() { | |
39 dispatchDisconnected(); | |
40 } | |
41 | |
38 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( | 42 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
39 ScriptPromiseResolver* resolver) { | 43 ScriptPromiseResolver* resolver) { |
40 auto result = m_activeAlgorithms.insert(resolver); | 44 auto result = m_activeAlgorithms.insert(resolver); |
41 CHECK(result.isNewEntry); | 45 CHECK(result.isNewEntry); |
42 } | 46 } |
43 | 47 |
44 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( | 48 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( |
45 ScriptPromiseResolver* resolver) { | 49 ScriptPromiseResolver* resolver) { |
46 if (!m_activeAlgorithms.contains(resolver)) { | 50 if (!m_activeAlgorithms.contains(resolver)) { |
47 return false; | 51 return false; |
48 } | 52 } |
49 m_activeAlgorithms.erase(resolver); | 53 m_activeAlgorithms.erase(resolver); |
50 return true; | 54 return true; |
51 } | 55 } |
52 | 56 |
57 void BluetoothRemoteGATTServer::disconnectIfConnected() { | |
58 if (connected()) { | |
59 setConnected(false); | |
60 ClearActiveAlgorithms(); | |
61 mojom::blink::WebBluetoothService* service = | |
62 m_device->bluetooth()->service(); | |
63 service->RemoteServerDisconnect(m_device->id()); | |
64 } | |
65 } | |
66 | |
67 void BluetoothRemoteGATTServer::cleanupDisconnectedDeviceAndFireEvent() { | |
68 DCHECK(connected()); | |
69 setConnected(false); | |
70 ClearActiveAlgorithms(); | |
71 m_device->clearAttributeInstanceMapAndFireEvent(); | |
72 } | |
73 | |
74 void BluetoothRemoteGATTServer::dispatchDisconnected() { | |
75 if (!connected()) { | |
76 return; | |
77 } | |
78 cleanupDisconnectedDeviceAndFireEvent(); | |
79 } | |
80 | |
81 void BluetoothRemoteGATTServer::dispose() { | |
82 disconnectIfConnected(); | |
83 // The pipe to this object must be closed when is marked unreachable to | |
84 // prevent messages from being dispatched before lazy sweeping. | |
85 if (m_clientBinding.is_bound()) | |
86 m_clientBinding.Close(); | |
87 } | |
88 | |
53 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 89 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
54 visitor->trace(m_activeAlgorithms); | 90 visitor->trace(m_activeAlgorithms); |
55 visitor->trace(m_device); | 91 visitor->trace(m_device); |
56 } | 92 } |
57 | 93 |
58 void BluetoothRemoteGATTServer::ConnectCallback( | 94 void BluetoothRemoteGATTServer::ConnectCallback( |
59 ScriptPromiseResolver* resolver, | 95 ScriptPromiseResolver* resolver, |
60 mojom::blink::WebBluetoothResult result) { | 96 mojom::blink::WebBluetoothResult result, |
97 mojom::blink::WebBluetoothServerClientAssociatedRequest request) { | |
61 if (!resolver->getExecutionContext() || | 98 if (!resolver->getExecutionContext() || |
62 resolver->getExecutionContext()->isContextDestroyed()) | 99 resolver->getExecutionContext()->isContextDestroyed()) |
63 return; | 100 return; |
64 | 101 |
102 if (!m_connected) | |
ortuno
2017/03/06 11:31:20
Binding the request based on tha value of m_connec
juncai
2017/03/09 07:30:57
Done.
| |
103 m_clientBinding.Bind(std::move(request)); | |
104 | |
65 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 105 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
66 m_device->bluetooth()->addToConnectedDevicesMap(device()->id(), device()); | |
67 setConnected(true); | 106 setConnected(true); |
68 resolver->resolve(this); | 107 resolver->resolve(this); |
69 } else { | 108 } else { |
70 resolver->reject(BluetoothError::createDOMException(result)); | 109 resolver->reject(BluetoothError::createDOMException(result)); |
71 } | 110 } |
72 } | 111 } |
73 | 112 |
74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 113 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 114 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
76 ScriptPromise promise = resolver->promise(); | 115 ScriptPromise promise = resolver->promise(); |
77 | 116 |
78 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 117 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
79 service->RemoteServerConnect( | 118 service->RemoteServerConnect( |
80 device()->id(), convertToBaseCallback(WTF::bind( | 119 device()->id(), convertToBaseCallback(WTF::bind( |
81 &BluetoothRemoteGATTServer::ConnectCallback, | 120 &BluetoothRemoteGATTServer::ConnectCallback, |
82 wrapPersistent(this), wrapPersistent(resolver)))); | 121 wrapPersistent(this), wrapPersistent(resolver)))); |
83 | 122 |
84 return promise; | 123 return promise; |
85 } | 124 } |
86 | 125 |
87 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 126 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
88 if (!m_connected) | 127 if (!m_connected) |
89 return; | 128 return; |
90 device()->cleanupDisconnectedDeviceAndFireEvent(); | 129 cleanupDisconnectedDeviceAndFireEvent(); |
91 m_device->bluetooth()->removeFromConnectedDevicesMap(device()->id()); | |
92 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 130 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
93 service->RemoteServerDisconnect(device()->id()); | 131 service->RemoteServerDisconnect(device()->id()); |
94 } | 132 } |
95 | 133 |
96 // Callback that allows us to resolve the promise with a single service or | 134 // Callback that allows us to resolve the promise with a single service or |
97 // with a vector owning the services. | 135 // with a vector owning the services. |
98 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 136 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
99 const String& requestedServiceUUID, | 137 const String& requestedServiceUUID, |
100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 138 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
101 ScriptPromiseResolver* resolver, | 139 ScriptPromiseResolver* resolver, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 service->RemoteServerGetPrimaryServices( | 232 service->RemoteServerGetPrimaryServices( |
195 device()->id(), quantity, servicesUUID, | 233 device()->id(), quantity, servicesUUID, |
196 convertToBaseCallback( | 234 convertToBaseCallback( |
197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 235 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
198 wrapPersistent(this), servicesUUID, quantity, | 236 wrapPersistent(this), servicesUUID, quantity, |
199 wrapPersistent(resolver)))); | 237 wrapPersistent(resolver)))); |
200 return promise; | 238 return promise; |
201 } | 239 } |
202 | 240 |
203 } // namespace blink | 241 } // namespace blink |
OLD | NEW |