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

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

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: rebase 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/BluetoothDevice.h" 5 #include "modules/bluetooth/BluetoothDevice.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/events/Event.h" 11 #include "core/events/Event.h"
12 #include "modules/bluetooth/Bluetooth.h" 12 #include "modules/bluetooth/Bluetooth.h"
13 #include "modules/bluetooth/BluetoothAttributeInstanceMap.h" 13 #include "modules/bluetooth/BluetoothAttributeInstanceMap.h"
14 #include "modules/bluetooth/BluetoothError.h" 14 #include "modules/bluetooth/BluetoothError.h"
15 #include "modules/bluetooth/BluetoothRemoteGATTServer.h" 15 #include "modules/bluetooth/BluetoothRemoteGATTServer.h"
16 #include <memory> 16 #include <memory>
17 #include <utility> 17 #include <utility>
18 18
19 namespace blink { 19 namespace blink {
20 20
21 BluetoothDevice::BluetoothDevice(ExecutionContext* context, 21 BluetoothDevice::BluetoothDevice(ExecutionContext* context,
22 mojom::blink::WebBluetoothDevicePtr device, 22 mojom::blink::WebBluetoothDevicePtr device,
23 Bluetooth* bluetooth) 23 Bluetooth* bluetooth)
24 : ContextLifecycleObserver(context), 24 : ContextLifecycleObserver(context),
25 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)), 25 m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)),
26 m_device(std::move(device)), 26 m_device(std::move(device)),
27 m_gatt(BluetoothRemoteGATTServer::Create(this)), 27 m_gatt(BluetoothRemoteGATTServer::Create(context, this)),
28 m_bluetooth(bluetooth) {} 28 m_bluetooth(bluetooth) {}
29 29
30 // static 30 // static
31 BluetoothDevice* BluetoothDevice::take( 31 BluetoothDevice* BluetoothDevice::take(
32 ScriptPromiseResolver* resolver, 32 ScriptPromiseResolver* resolver,
33 mojom::blink::WebBluetoothDevicePtr device, 33 mojom::blink::WebBluetoothDevicePtr device,
34 Bluetooth* bluetooth) { 34 Bluetooth* bluetooth) {
35 return new BluetoothDevice(resolver->getExecutionContext(), std::move(device), 35 return new BluetoothDevice(resolver->getExecutionContext(), std::move(device),
36 bluetooth); 36 bluetooth);
37 } 37 }
(...skipping 30 matching lines...) Expand all
68 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, 68 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor,
69 BluetoothRemoteGATTCharacteristic* characteristic) { 69 BluetoothRemoteGATTCharacteristic* characteristic) {
70 return m_attributeInstanceMap->GetOrCreateBluetoothRemoteGATTDescriptor( 70 return m_attributeInstanceMap->GetOrCreateBluetoothRemoteGATTDescriptor(
71 std::move(descriptor), characteristic); 71 std::move(descriptor), characteristic);
72 } 72 }
73 73
74 bool BluetoothDevice::IsValidDescriptor(const String& descriptorInstanceId) { 74 bool BluetoothDevice::IsValidDescriptor(const String& descriptorInstanceId) {
75 return m_attributeInstanceMap->ContainsDescriptor(descriptorInstanceId); 75 return m_attributeInstanceMap->ContainsDescriptor(descriptorInstanceId);
76 } 76 }
77 77
78 void BluetoothDevice::Dispose() { 78 void BluetoothDevice::ClearAttributeInstanceMapAndFireEvent() {
79 DisconnectGATTIfConnected();
80 }
81
82 void BluetoothDevice::contextDestroyed(ExecutionContext*) {
83 DisconnectGATTIfConnected();
84 }
85
86 void BluetoothDevice::DisconnectGATTIfConnected() {
87 if (m_gatt->connected()) {
88 m_gatt->SetConnected(false);
89 m_gatt->ClearActiveAlgorithms();
90 m_bluetooth->RemoveFromConnectedDevicesMap(id());
91 mojom::blink::WebBluetoothService* service = m_bluetooth->Service();
92 service->RemoteServerDisconnect(id());
93 }
94 }
95
96 void BluetoothDevice::CleanupDisconnectedDeviceAndFireEvent() {
97 DCHECK(m_gatt->connected());
98 m_gatt->SetConnected(false);
99 m_gatt->ClearActiveAlgorithms();
100 m_attributeInstanceMap->Clear(); 79 m_attributeInstanceMap->Clear();
101 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected)); 80 dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected));
102 } 81 }
103 82
104 const WTF::AtomicString& BluetoothDevice::interfaceName() const { 83 const WTF::AtomicString& BluetoothDevice::interfaceName() const {
105 return EventTargetNames::BluetoothDevice; 84 return EventTargetNames::BluetoothDevice;
106 } 85 }
107 86
108 ExecutionContext* BluetoothDevice::getExecutionContext() const { 87 ExecutionContext* BluetoothDevice::getExecutionContext() const {
109 return ContextLifecycleObserver::getExecutionContext(); 88 return ContextLifecycleObserver::getExecutionContext();
110 } 89 }
111 90
112 void BluetoothDevice::DispatchGattServerDisconnected() {
113 if (!m_gatt->connected()) {
114 return;
115 }
116 CleanupDisconnectedDeviceAndFireEvent();
117 }
118
119 DEFINE_TRACE(BluetoothDevice) { 91 DEFINE_TRACE(BluetoothDevice) {
120 visitor->trace(m_attributeInstanceMap); 92 visitor->trace(m_attributeInstanceMap);
121 visitor->trace(m_gatt); 93 visitor->trace(m_gatt);
122 visitor->trace(m_bluetooth); 94 visitor->trace(m_bluetooth);
123 EventTargetWithInlineData::trace(visitor); 95 EventTargetWithInlineData::trace(visitor);
124 ContextLifecycleObserver::trace(visitor); 96 ContextLifecycleObserver::trace(visitor);
125 } 97 }
126 98
127 } // namespace blink 99 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698