| 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/BluetoothGATTRemoteServer.h" | 5 #include "modules/bluetooth/BluetoothGATTRemoteServer.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" |
| 12 #include "modules/bluetooth/BluetoothError.h" | 13 #include "modules/bluetooth/BluetoothError.h" |
| 13 #include "modules/bluetooth/BluetoothGATTService.h" | 14 #include "modules/bluetooth/BluetoothGATTService.h" |
| 14 #include "modules/bluetooth/BluetoothSupplement.h" | 15 #include "modules/bluetooth/BluetoothSupplement.h" |
| 15 #include "modules/bluetooth/BluetoothUUID.h" | 16 #include "modules/bluetooth/BluetoothUUID.h" |
| 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 17 #include "wtf/OwnPtr.h" | 18 #include "wtf/OwnPtr.h" |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 BluetoothGATTRemoteServer::BluetoothGATTRemoteServer(PassOwnPtr<WebBluetoothGATT
RemoteServer> webGATT) | 22 BluetoothGATTRemoteServer::BluetoothGATTRemoteServer(ExecutionContext* context,
PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT) |
| 22 : m_webGATT(webGATT) | 23 : ActiveDOMObject(context) |
| 24 , PageLifecycleObserver(toDocument(context)->page()) |
| 25 , m_webGATT(webGATT) |
| 23 { | 26 { |
| 27 // See example in Source/platform/heap/ThreadState.h |
| 28 ThreadState::current()->registerPreFinalizer(this); |
| 24 } | 29 } |
| 25 | 30 |
| 26 BluetoothGATTRemoteServer* BluetoothGATTRemoteServer::take(ScriptPromiseResolver
*, PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT) | 31 BluetoothGATTRemoteServer* BluetoothGATTRemoteServer::take(ScriptPromiseResolver
* resolver, PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT) |
| 27 { | 32 { |
| 28 ASSERT(webGATT); | 33 ASSERT(webGATT); |
| 29 return new BluetoothGATTRemoteServer(webGATT); | 34 BluetoothGATTRemoteServer* server = new BluetoothGATTRemoteServer(resolver->
executionContext(), webGATT); |
| 35 if (!server->page()->isPageVisible()) { |
| 36 server->disconnectIfConnected(); |
| 37 } |
| 38 server->suspendIfNeeded(); |
| 39 return server; |
| 40 } |
| 41 |
| 42 void BluetoothGATTRemoteServer::dispose() |
| 43 { |
| 44 disconnectIfConnected(); |
| 45 } |
| 46 |
| 47 void BluetoothGATTRemoteServer::stop() |
| 48 { |
| 49 disconnectIfConnected(); |
| 50 } |
| 51 |
| 52 void BluetoothGATTRemoteServer::pageVisibilityChanged() |
| 53 { |
| 54 if (!page()->isPageVisible()) { |
| 55 disconnectIfConnected(); |
| 56 } |
| 57 } |
| 58 |
| 59 void BluetoothGATTRemoteServer::disconnectIfConnected() |
| 60 { |
| 61 if (m_webGATT->connected) { |
| 62 m_webGATT->connected = false; |
| 63 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e
xecutionContext()); |
| 64 webbluetooth->disconnect(m_webGATT->deviceId); |
| 65 } |
| 66 } |
| 67 |
| 68 DEFINE_TRACE(BluetoothGATTRemoteServer) |
| 69 { |
| 70 ActiveDOMObject::trace(visitor); |
| 71 PageLifecycleObserver::trace(visitor); |
| 30 } | 72 } |
| 31 | 73 |
| 32 void BluetoothGATTRemoteServer::disconnect(ScriptState* scriptState) | 74 void BluetoothGATTRemoteServer::disconnect(ScriptState* scriptState) |
| 33 { | 75 { |
| 34 m_webGATT->connected = false; | 76 m_webGATT->connected = false; |
| 35 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 77 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 36 webbluetooth->disconnect(m_webGATT->deviceId); | 78 webbluetooth->disconnect(m_webGATT->deviceId); |
| 37 } | 79 } |
| 38 | 80 |
| 39 ScriptPromise BluetoothGATTRemoteServer::getPrimaryService(ScriptState* scriptSt
ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState) | 81 ScriptPromise BluetoothGATTRemoteServer::getPrimaryService(ScriptState* scriptSt
ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState) |
| 40 { | 82 { |
| 41 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 83 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 42 | 84 |
| 43 String serviceUUID = BluetoothUUID::getService(service, exceptionState); | 85 String serviceUUID = BluetoothUUID::getService(service, exceptionState); |
| 44 if (exceptionState.hadException()) | 86 if (exceptionState.hadException()) |
| 45 return exceptionState.reject(scriptState); | 87 return exceptionState.reject(scriptState); |
| 46 | 88 |
| 47 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 48 ScriptPromise promise = resolver->promise(); | 90 ScriptPromise promise = resolver->promise(); |
| 49 webbluetooth->getPrimaryService(m_webGATT->deviceId, serviceUUID, new Callba
ckPromiseAdapter<BluetoothGATTService, BluetoothError>(resolver)); | 91 webbluetooth->getPrimaryService(m_webGATT->deviceId, serviceUUID, new Callba
ckPromiseAdapter<BluetoothGATTService, BluetoothError>(resolver)); |
| 50 | 92 |
| 51 return promise; | 93 return promise; |
| 52 } | 94 } |
| 53 | 95 |
| 54 } // namespace blink | 96 } // namespace blink |
| OLD | NEW |