Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: address more comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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();
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 (connected()) {
ortuno 2017/03/09 23:16:38 nit: We changed to m_connected so you might want t
juncai 2017/03/10 03:57:02 Done.
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(connected());
70 setConnected(false);
71 ClearActiveAlgorithms();
72 m_device->clearAttributeInstanceMapAndFireEvent();
73 }
74
75 void BluetoothRemoteGATTServer::dispatchDisconnected() {
76 if (!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,
99 mojom::blink::WebBluetoothServerClientAssociatedRequest request) {
53 if (!resolver->getExecutionContext() || 100 if (!resolver->getExecutionContext() ||
54 resolver->getExecutionContext()->isContextDestroyed()) 101 resolver->getExecutionContext()->isContextDestroyed())
55 return; 102 return;
56 103
57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 104 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
58 m_device->bluetooth()->addToConnectedDevicesMap(m_device->id(), m_device); 105 if (request.is_pending())
106 m_clientBinding.Bind(std::move(request));
59 setConnected(true); 107 setConnected(true);
60 resolver->resolve(this); 108 resolver->resolve(this);
61 } else { 109 } else {
62 resolver->reject(BluetoothError::createDOMException(result)); 110 resolver->reject(BluetoothError::createDOMException(result));
63 } 111 }
64 } 112 }
65 113
66 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { 114 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) {
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 115 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
68 ScriptPromise promise = resolver->promise(); 116 ScriptPromise promise = resolver->promise();
69 117
70 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 118 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
71 service->RemoteServerConnect( 119 service->RemoteServerConnect(
72 m_device->id(), convertToBaseCallback(WTF::bind( 120 m_device->id(), convertToBaseCallback(WTF::bind(
73 &BluetoothRemoteGATTServer::ConnectCallback, 121 &BluetoothRemoteGATTServer::ConnectCallback,
74 wrapPersistent(this), wrapPersistent(resolver)))); 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();
ortuno 2017/03/09 23:16:38 Should we unbind the client as well? Otherwise we
juncai 2017/03/10 03:57:02 Done.
ortuno 2017/03/10 04:37:09 Would it make sense to have a test for this?
juncai 2017/03/10 22:31:34 Done.
83 m_device->bluetooth()->removeFromConnectedDevicesMap(m_device->id());
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698