File: BluetoothDevice.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (123 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download
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