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 m_device->dispatchGattServerDisconnected(); | |
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::dispose() { | |
ortuno
2017/02/24 03:28:52
Similar to BluetoothRemoteGATTCharacteristic, we w
juncai
2017/03/01 02:04:12
Done.
| |
58 // The pipe to this object must be closed when is marked unreachable to | |
59 // prevent messages from being dispatched before lazy sweeping. | |
60 if (m_clientBinding.is_bound()) | |
61 m_clientBinding.Close(); | |
62 } | |
63 | |
53 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 64 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
54 visitor->trace(m_activeAlgorithms); | 65 visitor->trace(m_activeAlgorithms); |
55 visitor->trace(m_device); | 66 visitor->trace(m_device); |
56 } | 67 } |
57 | 68 |
58 void BluetoothRemoteGATTServer::ConnectCallback( | 69 void BluetoothRemoteGATTServer::ConnectCallback( |
59 ScriptPromiseResolver* resolver, | 70 ScriptPromiseResolver* resolver, |
60 mojom::blink::WebBluetoothResult result) { | 71 mojom::blink::WebBluetoothResult result, |
72 mojom::blink::WebBluetoothServerClientAssociatedRequest request) { | |
61 if (!resolver->getExecutionContext() || | 73 if (!resolver->getExecutionContext() || |
62 resolver->getExecutionContext()->isContextDestroyed()) | 74 resolver->getExecutionContext()->isContextDestroyed()) |
63 return; | 75 return; |
64 | 76 |
77 m_clientBinding.Bind(std::move(request)); | |
ortuno
2017/02/24 03:28:52
Hmm seems we are missing tests. We need a test to
juncai
2017/03/01 02:04:12
Done.
| |
78 | |
65 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 79 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
66 m_device->bluetooth()->addToConnectedDevicesMap(device()->id(), device()); | |
67 setConnected(true); | 80 setConnected(true); |
68 resolver->resolve(this); | 81 resolver->resolve(this); |
69 } else { | 82 } else { |
70 resolver->reject(BluetoothError::take(resolver, result)); | 83 resolver->reject(BluetoothError::take(resolver, result)); |
71 } | 84 } |
72 } | 85 } |
73 | 86 |
74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 87 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 88 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
76 ScriptPromise promise = resolver->promise(); | 89 ScriptPromise promise = resolver->promise(); |
77 | 90 |
78 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 91 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
79 service->RemoteServerConnect( | 92 service->RemoteServerConnect( |
80 device()->id(), convertToBaseCallback(WTF::bind( | 93 device()->id(), convertToBaseCallback(WTF::bind( |
81 &BluetoothRemoteGATTServer::ConnectCallback, | 94 &BluetoothRemoteGATTServer::ConnectCallback, |
82 wrapPersistent(this), wrapPersistent(resolver)))); | 95 wrapPersistent(this), wrapPersistent(resolver)))); |
83 | 96 |
84 return promise; | 97 return promise; |
85 } | 98 } |
86 | 99 |
87 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 100 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
88 if (!m_connected) | 101 if (!m_connected) |
89 return; | 102 return; |
90 device()->cleanupDisconnectedDeviceAndFireEvent(); | 103 device()->cleanupDisconnectedDeviceAndFireEvent(); |
91 m_device->bluetooth()->removeFromConnectedDevicesMap(device()->id()); | |
92 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 104 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
93 service->RemoteServerDisconnect(device()->id()); | 105 service->RemoteServerDisconnect(device()->id()); |
94 } | 106 } |
95 | 107 |
96 // Callback that allows us to resolve the promise with a single service or | 108 // Callback that allows us to resolve the promise with a single service or |
97 // with a vector owning the services. | 109 // with a vector owning the services. |
98 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 110 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
99 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 111 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
100 ScriptPromiseResolver* resolver, | 112 ScriptPromiseResolver* resolver, |
101 mojom::blink::WebBluetoothResult result, | 113 mojom::blink::WebBluetoothResult result, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 197 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
186 service->RemoteServerGetPrimaryServices( | 198 service->RemoteServerGetPrimaryServices( |
187 device()->id(), quantity, servicesUUID, | 199 device()->id(), quantity, servicesUUID, |
188 convertToBaseCallback( | 200 convertToBaseCallback( |
189 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 201 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
190 wrapPersistent(this), quantity, wrapPersistent(resolver)))); | 202 wrapPersistent(this), quantity, wrapPersistent(resolver)))); |
191 return promise; | 203 return promise; |
192 } | 204 } |
193 | 205 |
194 } // namespace blink | 206 } // namespace blink |
OLD | NEW |