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