1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/bluetooth/BluetoothDevice.h"
#include "bindings/core/v8/CallbackPromiseAdapter.h"
#include "bindings/core/v8/ScriptPromise.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "core/dom/DOMException.h"
#include "core/events/Event.h"
#include "modules/bluetooth/Bluetooth.h"
#include "modules/bluetooth/BluetoothAttributeInstanceMap.h"
#include "modules/bluetooth/BluetoothError.h"
#include "modules/bluetooth/BluetoothRemoteGATTServer.h"
#include <memory>
#include <utility>
namespace blink {
BluetoothDevice::BluetoothDevice(ExecutionContext* context,
mojom::blink::WebBluetoothDevicePtr device,
Bluetooth* bluetooth)
: ContextLifecycleObserver(context),
m_attributeInstanceMap(new BluetoothAttributeInstanceMap(this)),
m_device(std::move(device)),
m_gatt(BluetoothRemoteGATTServer::create(this)),
m_bluetooth(bluetooth) {}
// static
BluetoothDevice* BluetoothDevice::take(
ScriptPromiseResolver* resolver,
mojom::blink::WebBluetoothDevicePtr device,
Bluetooth* bluetooth) {
return new BluetoothDevice(resolver->getExecutionContext(), std::move(device),
bluetooth);
}
BluetoothRemoteGATTService* BluetoothDevice::getOrCreateRemoteGATTService(
mojom::blink::WebBluetoothRemoteGATTServicePtr service,
bool isPrimary,
const String& deviceInstanceId) {
return m_attributeInstanceMap->getOrCreateRemoteGATTService(
std::move(service), isPrimary, deviceInstanceId);
}
bool BluetoothDevice::isValidService(const String& serviceInstanceId) {
return m_attributeInstanceMap->containsService(serviceInstanceId);
}
BluetoothRemoteGATTCharacteristic*
BluetoothDevice::getOrCreateRemoteGATTCharacteristic(
ExecutionContext* context,
mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic,
BluetoothRemoteGATTService* service) {
return m_attributeInstanceMap->getOrCreateRemoteGATTCharacteristic(
context, std::move(characteristic), service);
}
bool BluetoothDevice::isValidCharacteristic(
const String& characteristicInstanceId) {
return m_attributeInstanceMap->containsCharacteristic(
characteristicInstanceId);
}
BluetoothRemoteGATTDescriptor*
BluetoothDevice::getOrCreateBluetoothRemoteGATTDescriptor(
mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor,
BluetoothRemoteGATTCharacteristic* characteristic) {
return m_attributeInstanceMap->getOrCreateBluetoothRemoteGATTDescriptor(
std::move(descriptor), characteristic);
}
void BluetoothDevice::dispose() {
disconnectGATTIfConnected();
}
void BluetoothDevice::contextDestroyed(ExecutionContext*) {
disconnectGATTIfConnected();
}
void BluetoothDevice::disconnectGATTIfConnected() {
if (m_gatt->connected()) {
m_gatt->setConnected(false);
m_gatt->ClearActiveAlgorithms();
m_bluetooth->removeDevice(id());
mojom::blink::WebBluetoothService* service = m_bluetooth->service();
service->RemoteServerDisconnect(id());
}
}
void BluetoothDevice::cleanupDisconnectedDeviceAndFireEvent() {
DCHECK(m_gatt->connected());
m_gatt->setConnected(false);
m_gatt->ClearActiveAlgorithms();
m_attributeInstanceMap->Clear();
dispatchEvent(Event::createBubble(EventTypeNames::gattserverdisconnected));
}
const WTF::AtomicString& BluetoothDevice::interfaceName() const {
return EventTargetNames::BluetoothDevice;
}
ExecutionContext* BluetoothDevice::getExecutionContext() const {
return ContextLifecycleObserver::getExecutionContext();
}
void BluetoothDevice::dispatchGattServerDisconnected() {
if (!m_gatt->connected()) {
return;
}
cleanupDisconnectedDeviceAndFireEvent();
}
DEFINE_TRACE(BluetoothDevice) {
visitor->trace(m_attributeInstanceMap);
visitor->trace(m_gatt);
visitor->trace(m_bluetooth);
EventTargetWithInlineData::trace(visitor);
ContextLifecycleObserver::trace(visitor);
}
} // namespace blink
|