OLD | NEW |
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/dom/Document.h" |
11 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/page/PageVisibilityState.h" |
12 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
13 #include "modules/bluetooth/BluetoothGATTRemoteServer.h" | 15 #include "modules/bluetooth/BluetoothGATTRemoteServer.h" |
14 #include "modules/bluetooth/BluetoothSupplement.h" | 16 #include "modules/bluetooth/BluetoothSupplement.h" |
15 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
16 | 18 |
17 namespace blink { | 19 namespace blink { |
| 20 namespace { |
| 21 |
| 22 PageVisibilityState getPageVisibilityState(ScriptState* scriptState) |
| 23 { |
| 24 return toDocument(scriptState->executionContext())->page()->visibilityState(
); |
| 25 } |
| 26 |
| 27 } // namespace |
18 | 28 |
19 BluetoothDevice::BluetoothDevice(PassOwnPtr<WebBluetoothDevice> webDevice) | 29 BluetoothDevice::BluetoothDevice(PassOwnPtr<WebBluetoothDevice> webDevice) |
20 : m_webDevice(webDevice) | 30 : m_webDevice(webDevice) |
21 , m_adData(BluetoothAdvertisingData::create(m_webDevice->txPower, | 31 , m_adData(BluetoothAdvertisingData::create(m_webDevice->txPower, |
22 m_webDevice->rssi)) | 32 m_webDevice->rssi)) |
23 { | 33 { |
24 } | 34 } |
25 | 35 |
26 BluetoothDevice* BluetoothDevice::take(ScriptPromiseResolver*, PassOwnPtr<WebBlu
etoothDevice> webDevice) | 36 BluetoothDevice* BluetoothDevice::take(ScriptPromiseResolver*, PassOwnPtr<WebBlu
etoothDevice> webDevice) |
27 { | 37 { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 Vector<String> BluetoothDevice::uuids() | 82 Vector<String> BluetoothDevice::uuids() |
73 { | 83 { |
74 Vector<String> uuids(m_webDevice->uuids.size()); | 84 Vector<String> uuids(m_webDevice->uuids.size()); |
75 for (size_t i = 0; i < m_webDevice->uuids.size(); ++i) | 85 for (size_t i = 0; i < m_webDevice->uuids.size(); ++i) |
76 uuids[i] = m_webDevice->uuids[i]; | 86 uuids[i] = m_webDevice->uuids[i]; |
77 return uuids; | 87 return uuids; |
78 } | 88 } |
79 | 89 |
80 ScriptPromise BluetoothDevice::connectGATT(ScriptState* scriptState) | 90 ScriptPromise BluetoothDevice::connectGATT(ScriptState* scriptState) |
81 { | 91 { |
| 92 // TODO(ortuno): Allow connections when the tab is in the background. |
| 93 // This is a short term solution instead of implementing a tab indicator |
| 94 // for bluetooth connections. |
| 95 // https://crbug.com/579746 |
| 96 if (getPageVisibilityState(scriptState) != PageVisibilityStateVisible) { |
| 97 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, "Connection is only allowed while the page is visible. Thi
s is a temporary measure until we are able to effectively communicate to the use
r that a page is connected to a device.")); |
| 98 } |
82 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 99 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
83 if (!webbluetooth) | 100 if (!webbluetooth) |
84 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 101 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
85 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 102 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
86 ScriptPromise promise = resolver->promise(); | 103 ScriptPromise promise = resolver->promise(); |
87 webbluetooth->connectGATT(id(), new CallbackPromiseAdapter<BluetoothGATTRemo
teServer, BluetoothError>(resolver)); | 104 webbluetooth->connectGATT(id(), new CallbackPromiseAdapter<BluetoothGATTRemo
teServer, BluetoothError>(resolver)); |
88 return promise; | 105 return promise; |
89 } | 106 } |
90 | 107 |
91 } // namespace blink | 108 } // namespace blink |
OLD | NEW |