| 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" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } | 42 } |
| 43 | 43 |
| 44 class ConnectCallback : public WebBluetoothRemoteGATTServerConnectCallbacks { | 44 class ConnectCallback : public WebBluetoothRemoteGATTServerConnectCallbacks { |
| 45 public: | 45 public: |
| 46 ConnectCallback(BluetoothDevice* device, ScriptPromiseResolver* resolver) | 46 ConnectCallback(BluetoothDevice* device, ScriptPromiseResolver* resolver) |
| 47 : m_device(device) | 47 : m_device(device) |
| 48 , m_resolver(resolver) {} | 48 , m_resolver(resolver) {} |
| 49 | 49 |
| 50 void onSuccess() override | 50 void onSuccess() override |
| 51 { | 51 { |
| 52 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) | 52 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 53 return; | 53 return; |
| 54 m_device->gatt()->setConnected(true); | 54 m_device->gatt()->setConnected(true); |
| 55 if (!m_device->page()->isPageVisible()) { | 55 if (!m_device->page()->isPageVisible()) { |
| 56 // TODO(ortuno): Allow connections when the tab is in the background
. | 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 | 57 // This is a short term solution instead of implementing a tab indic
ator |
| 58 // for bluetooth connections. | 58 // for bluetooth connections. |
| 59 // https://crbug.com/579746 | 59 // https://crbug.com/579746 |
| 60 m_device->disconnectGATTIfConnected(); | 60 m_device->disconnectGATTIfConnected(); |
| 61 m_resolver->reject(DOMException::create(SecurityError, kPageHiddenEr
ror)); | 61 m_resolver->reject(DOMException::create(SecurityError, kPageHiddenEr
ror)); |
| 62 } else { | 62 } else { |
| 63 m_resolver->resolve(m_device->gatt()); | 63 m_resolver->resolve(m_device->gatt()); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 void onError(const WebBluetoothError& e) override | 67 void onError(const WebBluetoothError& e) override |
| 68 { | 68 { |
| 69 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) | 69 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 70 return; | 70 return; |
| 71 m_resolver->reject(BluetoothError::take(m_resolver, e)); | 71 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
| 72 } | 72 } |
| 73 private: | 73 private: |
| 74 Persistent<BluetoothDevice> m_device; | 74 Persistent<BluetoothDevice> m_device; |
| 75 Persistent<ScriptPromiseResolver> m_resolver; | 75 Persistent<ScriptPromiseResolver> m_resolver; |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) | 78 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) |
| 79 { | 79 { |
| 80 // TODO(ortuno): Allow connections when the tab is in the background. | 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 | 81 // This is a short term solution instead of implementing a tab indicator |
| 82 // for bluetooth connections. | 82 // for bluetooth connections. |
| 83 // https://crbug.com/579746 | 83 // https://crbug.com/579746 |
| 84 if (!toDocument(scriptState->executionContext())->page()->isPageVisible()) { | 84 if (!toDocument(scriptState->getExecutionContext())->page()->isPageVisible()
) { |
| 85 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, kPageHiddenError)); | 85 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, kPageHiddenError)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 88 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 89 if (!webbluetooth) | 89 if (!webbluetooth) |
| 90 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 90 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 91 | 91 |
| 92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 93 ScriptPromise promise = resolver->promise(); | 93 ScriptPromise promise = resolver->promise(); |
| 94 webbluetooth->connect(device()->id(), new ConnectCallback(device(), resolver
)); | 94 webbluetooth->connect(device()->id(), new ConnectCallback(device(), resolver
)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 111 return exceptionState.reject(scriptState); | 111 return exceptionState.reject(scriptState); |
| 112 | 112 |
| 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 114 ScriptPromise promise = resolver->promise(); | 114 ScriptPromise promise = resolver->promise(); |
| 115 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); | 115 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); |
| 116 | 116 |
| 117 return promise; | 117 return promise; |
| 118 } | 118 } |
| 119 | 119 |
| 120 } // namespace blink | 120 } // namespace blink |
| OLD | NEW |