Chromium Code Reviews| 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() { | |
|
ortuno
2017/03/01 04:52:06
What do you think of the following:
void disconne
juncai
2017/03/02 03:23:49
I think this change can be combined in a follow-up
| |
| 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 // The pipe to this object must be closed when is marked unreachable to | |
| 83 // prevent messages from being dispatched before lazy sweeping. | |
| 84 if (m_clientBinding.is_bound()) | |
| 85 m_clientBinding.Close(); | |
| 86 } | |
| 87 | |
| 53 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 88 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
| 54 visitor->trace(m_activeAlgorithms); | 89 visitor->trace(m_activeAlgorithms); |
| 55 visitor->trace(m_device); | 90 visitor->trace(m_device); |
| 56 } | 91 } |
| 57 | 92 |
| 58 void BluetoothRemoteGATTServer::ConnectCallback( | 93 void BluetoothRemoteGATTServer::ConnectCallback( |
| 59 ScriptPromiseResolver* resolver, | 94 ScriptPromiseResolver* resolver, |
| 60 mojom::blink::WebBluetoothResult result) { | 95 mojom::blink::WebBluetoothResult result, |
| 96 mojom::blink::WebBluetoothServerClientAssociatedRequest request) { | |
| 61 if (!resolver->getExecutionContext() || | 97 if (!resolver->getExecutionContext() || |
| 62 resolver->getExecutionContext()->isContextDestroyed()) | 98 resolver->getExecutionContext()->isContextDestroyed()) |
| 63 return; | 99 return; |
| 64 | 100 |
| 101 if (!m_connected) | |
|
ortuno
2017/03/01 04:52:06
Why not just send a nullptr for already connected
juncai
2017/03/02 03:23:49
Done.
| |
| 102 m_clientBinding.Bind(std::move(request)); | |
| 103 | |
| 65 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 104 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 66 m_device->bluetooth()->addToConnectedDevicesMap(device()->id(), device()); | |
| 67 setConnected(true); | 105 setConnected(true); |
| 68 resolver->resolve(this); | 106 resolver->resolve(this); |
| 69 } else { | 107 } else { |
| 70 resolver->reject(BluetoothError::createDOMException(result)); | 108 resolver->reject(BluetoothError::createDOMException(result)); |
| 71 } | 109 } |
| 72 } | 110 } |
| 73 | 111 |
| 74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 112 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| 75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 76 ScriptPromise promise = resolver->promise(); | 114 ScriptPromise promise = resolver->promise(); |
| 77 | 115 |
| 78 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 116 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 79 service->RemoteServerConnect( | 117 service->RemoteServerConnect( |
| 80 device()->id(), convertToBaseCallback(WTF::bind( | 118 device()->id(), convertToBaseCallback(WTF::bind( |
| 81 &BluetoothRemoteGATTServer::ConnectCallback, | 119 &BluetoothRemoteGATTServer::ConnectCallback, |
| 82 wrapPersistent(this), wrapPersistent(resolver)))); | 120 wrapPersistent(this), wrapPersistent(resolver)))); |
| 83 | 121 |
| 84 return promise; | 122 return promise; |
| 85 } | 123 } |
| 86 | 124 |
| 87 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 125 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| 88 if (!m_connected) | 126 if (!m_connected) |
| 89 return; | 127 return; |
| 90 device()->cleanupDisconnectedDeviceAndFireEvent(); | 128 cleanupDisconnectedDeviceAndFireEvent(); |
| 91 m_device->bluetooth()->removeFromConnectedDevicesMap(device()->id()); | |
| 92 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 129 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 93 service->RemoteServerDisconnect(device()->id()); | 130 service->RemoteServerDisconnect(device()->id()); |
| 94 } | 131 } |
| 95 | 132 |
| 96 // Callback that allows us to resolve the promise with a single service or | 133 // Callback that allows us to resolve the promise with a single service or |
| 97 // with a vector owning the services. | 134 // with a vector owning the services. |
| 98 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 135 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
| 99 const String& requestedServiceUUID, | 136 const String& requestedServiceUUID, |
| 100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 137 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 101 ScriptPromiseResolver* resolver, | 138 ScriptPromiseResolver* resolver, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 service->RemoteServerGetPrimaryServices( | 231 service->RemoteServerGetPrimaryServices( |
| 195 device()->id(), quantity, servicesUUID, | 232 device()->id(), quantity, servicesUUID, |
| 196 convertToBaseCallback( | 233 convertToBaseCallback( |
| 197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 234 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 198 wrapPersistent(this), servicesUUID, quantity, | 235 wrapPersistent(this), servicesUUID, quantity, |
| 199 wrapPersistent(resolver)))); | 236 wrapPersistent(resolver)))); |
| 200 return promise; | 237 return promise; |
| 201 } | 238 } |
| 202 | 239 |
| 203 } // namespace blink | 240 } // namespace blink |
| OLD | NEW |