| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/BluetoothRemoteGATTServer.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTServer.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" | |
| 12 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/page/Page.h" | |
| 14 #include "modules/bluetooth/BluetoothError.h" | 12 #include "modules/bluetooth/BluetoothError.h" |
| 15 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 13 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 16 #include "modules/bluetooth/BluetoothSupplement.h" | 14 #include "modules/bluetooth/BluetoothSupplement.h" |
| 17 #include "modules/bluetooth/BluetoothUUID.h" | 15 #include "modules/bluetooth/BluetoothUUID.h" |
| 18 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 19 #include "wtf/OwnPtr.h" | 17 #include "wtf/OwnPtr.h" |
| 20 | 18 |
| 21 namespace blink { | 19 namespace blink { |
| 22 namespace { | |
| 23 | |
| 24 const char kPageHiddenError[] = "Connection is only allowed while the page is vi
sible. This is a temporary measure until we are able to effectively communicate
to the user that a page is connected to a device."; | |
| 25 | |
| 26 } | |
| 27 | 20 |
| 28 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 21 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
| 29 : m_device(device) | 22 : m_device(device) |
| 30 , m_connected(false) | 23 , m_connected(false) |
| 31 { | 24 { |
| 32 } | 25 } |
| 33 | 26 |
| 34 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create(BluetoothDevice* de
vice) | 27 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create(BluetoothDevice* de
vice) |
| 35 { | 28 { |
| 36 return new BluetoothRemoteGATTServer(device); | 29 return new BluetoothRemoteGATTServer(device); |
| 37 } | 30 } |
| 38 | 31 |
| 39 DEFINE_TRACE(BluetoothRemoteGATTServer) | 32 DEFINE_TRACE(BluetoothRemoteGATTServer) |
| 40 { | 33 { |
| 41 visitor->trace(m_device); | 34 visitor->trace(m_device); |
| 42 } | 35 } |
| 43 | 36 |
| 44 class ConnectCallback : public WebBluetoothRemoteGATTServerConnectCallbacks { | 37 class ConnectCallback : public WebBluetoothRemoteGATTServerConnectCallbacks { |
| 45 public: | 38 public: |
| 46 ConnectCallback(BluetoothDevice* device, ScriptPromiseResolver* resolver) | 39 ConnectCallback(BluetoothDevice* device, ScriptPromiseResolver* resolver) |
| 47 : m_device(device) | 40 : m_device(device) |
| 48 , m_resolver(resolver) {} | 41 , m_resolver(resolver) {} |
| 49 | 42 |
| 50 void onSuccess() override | 43 void onSuccess() override |
| 51 { | 44 { |
| 52 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 45 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 53 return; | 46 return; |
| 54 m_device->gatt()->setConnected(true); | 47 m_device->gatt()->setConnected(true); |
| 55 if (!m_device->page()->isPageVisible()) { | 48 m_resolver->resolve(m_device->gatt()); |
| 56 // TODO(ortuno): Allow connections when the tab is in the background
. | |
| 57 // This is a short term solution instead of implementing a tab indic
ator | |
| 58 // for bluetooth connections. | |
| 59 // https://crbug.com/579746 | |
| 60 m_device->disconnectGATTIfConnected(); | |
| 61 m_resolver->reject(DOMException::create(SecurityError, kPageHiddenEr
ror)); | |
| 62 } else { | |
| 63 m_resolver->resolve(m_device->gatt()); | |
| 64 } | |
| 65 } | 49 } |
| 66 | 50 |
| 67 void onError(const WebBluetoothError& e) override | 51 void onError(const WebBluetoothError& e) override |
| 68 { | 52 { |
| 69 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 53 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 70 return; | 54 return; |
| 71 m_resolver->reject(BluetoothError::take(m_resolver, e)); | 55 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
| 72 } | 56 } |
| 73 private: | 57 private: |
| 74 Persistent<BluetoothDevice> m_device; | 58 Persistent<BluetoothDevice> m_device; |
| 75 Persistent<ScriptPromiseResolver> m_resolver; | 59 Persistent<ScriptPromiseResolver> m_resolver; |
| 76 }; | 60 }; |
| 77 | 61 |
| 78 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) | 62 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) |
| 79 { | 63 { |
| 80 // TODO(ortuno): Allow connections when the tab is in the background. | |
| 81 // This is a short term solution instead of implementing a tab indicator | |
| 82 // for bluetooth connections. | |
| 83 // https://crbug.com/579746 | |
| 84 if (!toDocument(scriptState->getExecutionContext())->page()->isPageVisible()
) { | |
| 85 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, kPageHiddenError)); | |
| 86 } | |
| 87 | |
| 88 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 64 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 89 if (!webbluetooth) | 65 if (!webbluetooth) |
| 90 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 66 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 91 | 67 |
| 92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 68 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 93 ScriptPromise promise = resolver->promise(); | 69 ScriptPromise promise = resolver->promise(); |
| 94 webbluetooth->connect(device()->id(), new ConnectCallback(device(), resolver
)); | 70 webbluetooth->connect(device()->id(), new ConnectCallback(device(), resolver
)); |
| 95 return promise; | 71 return promise; |
| 96 } | 72 } |
| 97 | 73 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 111 return exceptionState.reject(scriptState); | 87 return exceptionState.reject(scriptState); |
| 112 | 88 |
| 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 114 ScriptPromise promise = resolver->promise(); | 90 ScriptPromise promise = resolver->promise(); |
| 115 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); | 91 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); |
| 116 | 92 |
| 117 return promise; | 93 return promise; |
| 118 } | 94 } |
| 119 | 95 |
| 120 } // namespace blink | 96 } // namespace blink |
| OLD | NEW |