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/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" |
12 #include "modules/bluetooth/BluetoothError.h" | 13 #include "modules/bluetooth/BluetoothError.h" |
13 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 14 #include "modules/bluetooth/BluetoothRemoteGATTService.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 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 }; | 61 }; |
61 | 62 |
62 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) | 63 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) |
63 { | 64 { |
64 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 65 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
65 if (!webbluetooth) | 66 if (!webbluetooth) |
66 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 67 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
67 | 68 |
68 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 69 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
69 ScriptPromise promise = resolver->promise(); | 70 ScriptPromise promise = resolver->promise(); |
70 webbluetooth->connect(device()->id(), new ConnectCallback(device(), resolver
)); | 71 webbluetooth->connect(device()->id(), device(), new ConnectCallback(device()
, resolver)); |
71 return promise; | 72 return promise; |
72 } | 73 } |
73 | 74 |
74 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) | 75 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) |
75 { | 76 { |
| 77 if (!m_connected) |
| 78 return; |
76 m_connected = false; | 79 m_connected = false; |
77 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 80 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
78 webbluetooth->disconnect(device()->id()); | 81 webbluetooth->disconnect(device()->id()); |
| 82 device()->dispatchEvent(Event::createBubble(EventTypeNames::gattserverdiscon
nected)); |
79 } | 83 } |
80 | 84 |
81 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService(ScriptState* scriptSt
ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState) | 85 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService(ScriptState* scriptSt
ate, const StringOrUnsignedLong& service, ExceptionState& exceptionState) |
82 { | 86 { |
83 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 87 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
84 | 88 |
85 String serviceUUID = BluetoothUUID::getService(service, exceptionState); | 89 String serviceUUID = BluetoothUUID::getService(service, exceptionState); |
86 if (exceptionState.hadException()) | 90 if (exceptionState.hadException()) |
87 return exceptionState.reject(scriptState); | 91 return exceptionState.reject(scriptState); |
88 | 92 |
89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 93 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
90 ScriptPromise promise = resolver->promise(); | 94 ScriptPromise promise = resolver->promise(); |
91 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); | 95 webbluetooth->getPrimaryService(device()->id(), serviceUUID, new CallbackPro
miseAdapter<BluetoothRemoteGATTService, BluetoothError>(resolver)); |
92 | 96 |
93 return promise; | 97 return promise; |
94 } | 98 } |
95 | 99 |
96 } // namespace blink | 100 } // namespace blink |
OLD | NEW |