summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/modules/bluetooth/BluetoothDevice.cpp
blob: b763a3b6eb47faf0eebd959fe0f926914008d8ee (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/events/Event.h"
#include "core/page/PageVisibilityState.h"
#include "modules/bluetooth/BluetoothError.h"
#include "modules/bluetooth/BluetoothRemoteGATTServer.h"
#include "modules/bluetooth/BluetoothSupplement.h"
#include "public/platform/modules/bluetooth/WebBluetooth.h"

namespace blink {

BluetoothDevice::BluetoothDevice(ExecutionContext* context, PassOwnPtr<WebBluetoothDevice> webDevice)
    : ActiveDOMObject(context)
    , PageLifecycleObserver(toDocument(context)->page())
    , m_webDevice(webDevice)
    , m_adData(BluetoothAdvertisingData::create(m_webDevice->txPower,
        m_webDevice->rssi))
    , m_gatt(BluetoothRemoteGATTServer::create(this))
{
    // See example in Source/platform/heap/ThreadState.h
    ThreadState::current()->registerPreFinalizer(this);
}

BluetoothDevice* BluetoothDevice::take(ScriptPromiseResolver* resolver, PassOwnPtr<WebBluetoothDevice> webDevice)
{
    ASSERT(webDevice);
    BluetoothDevice* device = new BluetoothDevice(resolver->executionContext(), webDevice);
    device->suspendIfNeeded();
    return device;
}

void BluetoothDevice::dispose()
{
    disconnectGATTIfConnected();
}

void BluetoothDevice::stop()
{
    disconnectGATTIfConnected();
}

void BluetoothDevice::pageVisibilityChanged()
{
    if (!page()->isPageVisible() && disconnectGATTIfConnected()) {
        dispatchEvent(Event::create(EventTypeNames::gattserverdisconnected));
    }
}

bool BluetoothDevice::disconnectGATTIfConnected()
{
    if (m_gatt->connected()) {
        m_gatt->setConnected(false);
        BluetoothSupplement::fromExecutionContext(executionContext())->disconnect(id());
        return true;
    }
    return false;
}

const WTF::AtomicString& BluetoothDevice::interfaceName() const
{
    return EventTargetNames::BluetoothDevice;
}

ExecutionContext* BluetoothDevice::executionContext() const
{
    return ActiveDOMObject::executionContext();
}

DEFINE_TRACE(BluetoothDevice)
{
    RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothDevice>::trace(visitor);
    ActiveDOMObject::trace(visitor);
    PageLifecycleObserver::trace(visitor);
    visitor->trace(m_adData);
    visitor->trace(m_gatt);
}

unsigned BluetoothDevice::deviceClass(bool& isNull)
{
    isNull = false;
    return m_webDevice->deviceClass;
}

String BluetoothDevice::vendorIDSource()
{
    switch (m_webDevice->vendorIDSource) {
    case WebBluetoothDevice::VendorIDSource::Unknown: return String();
    case WebBluetoothDevice::VendorIDSource::Bluetooth: return "bluetooth";
    case WebBluetoothDevice::VendorIDSource::USB: return "usb";
    }
    ASSERT_NOT_REACHED();
    return String();
}

unsigned BluetoothDevice::vendorID(bool& isNull)
{
    isNull = false;
    return m_webDevice->vendorID;
}

unsigned BluetoothDevice::productID(bool& isNull)
{
    isNull = false;
    return m_webDevice->productID;
}

unsigned BluetoothDevice::productVersion(bool& isNull)
{
    isNull = false;
    return m_webDevice->productVersion;
}

Vector<String> BluetoothDevice::uuids()
{
    Vector<String> uuids(m_webDevice->uuids.size());
    for (size_t i = 0; i < m_webDevice->uuids.size(); ++i)
        uuids[i] = m_webDevice->uuids[i];
    return uuids;
}

ScriptPromise BluetoothDevice::connectGATT(ScriptState* scriptState)
{
    return m_gatt->connect(scriptState);
}

} // namespace blink