Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: third_party/WebKit/Source/modules/bluetooth/BluetoothGATTRemoteServer.cpp

Issue 1611773002: bluetooth: Disconnect if the page becomes hidden or is closed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Disconnect if page hidden when connected. Clean up Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 namespace {
21 BluetoothGATTRemoteServer::BluetoothGATTRemoteServer(PassOwnPtr<WebBluetoothGATT RemoteServer> webGATT) 22 bool isPageVisible(Page* page)
Jeffrey Yasskin 2016/01/21 23:44:33 Add a blank line after "namespace {". (Ditto from
ortuno 2016/01/22 21:34:12 Done.
22 : m_webGATT(webGATT)
23 { 23 {
24 return page->visibilityState() == PageVisibilityStateVisible;
24 } 25 }
25 26
26 BluetoothGATTRemoteServer* BluetoothGATTRemoteServer::take(ScriptPromiseResolver *, PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT) 27 } // namespace
28 BluetoothGATTRemoteServer::BluetoothGATTRemoteServer(ExecutionContext* context, PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT)
29 : ActiveDOMObject(context)
30 , PageLifecycleObserver(toDocument(context)->page())
31 , m_webGATT(webGATT)
32 {
33 // See example in Source/platform/heap/ThreadState.h
34 ThreadState::current()->registerPreFinalizer(this);
35 }
36
37 BluetoothGATTRemoteServer* BluetoothGATTRemoteServer::take(ScriptPromiseResolver * resolver, PassOwnPtr<WebBluetoothGATTRemoteServer> webGATT)
27 { 38 {
28 ASSERT(webGATT); 39 ASSERT(webGATT);
29 return new BluetoothGATTRemoteServer(webGATT); 40 BluetoothGATTRemoteServer* server = new BluetoothGATTRemoteServer(resolver-> executionContext(), webGATT);
41 if (!isPageVisible(server->page())) {
42 server->disconnectIfConnected();
43 }
44 server->suspendIfNeeded();
45 return server;
46 }
47
48 void BluetoothGATTRemoteServer::dispose()
49 {
50 disconnectIfConnected();
51 }
52
53 void BluetoothGATTRemoteServer::stop()
54 {
55 disconnectIfConnected();
56 }
57
58 void BluetoothGATTRemoteServer::pageVisibilityChanged()
59 {
60 if (!isPageVisible(page())) {
61 disconnectIfConnected();
62 }
63 }
64
65 void BluetoothGATTRemoteServer::disconnectIfConnected()
66 {
67 if (m_webGATT->connected) {
68 m_webGATT->connected = false;
Jeffrey Yasskin 2016/01/21 23:44:33 FWIW (not for this CL), I'm uncomfortable having '
ortuno 2016/01/22 21:34:12 I agree. I think things will become clearer once w
69 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e xecutionContext());
70 webbluetooth->disconnect(m_webGATT->deviceId);
71 }
72 }
73
74 DEFINE_TRACE(BluetoothGATTRemoteServer)
75 {
76 ActiveDOMObject::trace(visitor);
77 PageLifecycleObserver::trace(visitor);
30 } 78 }
31 79
32 void BluetoothGATTRemoteServer::disconnect(ScriptState* scriptState) 80 void BluetoothGATTRemoteServer::disconnect(ScriptState* scriptState)
33 { 81 {
34 m_webGATT->connected = false; 82 m_webGATT->connected = false;
35 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); 83 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e);
36 webbluetooth->disconnect(m_webGATT->deviceId); 84 webbluetooth->disconnect(m_webGATT->deviceId);
37 } 85 }
38 86
39 ScriptPromise BluetoothGATTRemoteServer::getPrimaryService(ScriptState* scriptSt ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState) 87 ScriptPromise BluetoothGATTRemoteServer::getPrimaryService(ScriptState* scriptSt ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState)
40 { 88 {
41 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e); 89 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat e);
42 90
43 String serviceUUID = BluetoothUUID::getService(service, exceptionState); 91 String serviceUUID = BluetoothUUID::getService(service, exceptionState);
44 if (exceptionState.hadException()) 92 if (exceptionState.hadException())
45 return exceptionState.reject(scriptState); 93 return exceptionState.reject(scriptState);
46 94
47 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 95 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
48 ScriptPromise promise = resolver->promise(); 96 ScriptPromise promise = resolver->promise();
49 webbluetooth->getPrimaryService(m_webGATT->deviceId, serviceUUID, new Callba ckPromiseAdapter<BluetoothGATTService, BluetoothError>(resolver)); 97 webbluetooth->getPrimaryService(m_webGATT->deviceId, serviceUUID, new Callba ckPromiseAdapter<BluetoothGATTService, BluetoothError>(resolver));
50 98
51 return promise; 99 return promise;
52 } 100 }
53 101
54 } // namespace blink 102 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698