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 #ifndef BluetoothRemoteGATTServer_h | 5 #ifndef BluetoothRemoteGATTServer_h |
6 #define BluetoothRemoteGATTServer_h | 6 #define BluetoothRemoteGATTServer_h |
7 | 7 |
| 8 #include <list> |
| 9 #include <memory> |
8 #include "bindings/core/v8/ScriptWrappable.h" | 10 #include "bindings/core/v8/ScriptWrappable.h" |
9 #include "bindings/modules/v8/StringOrUnsignedLong.h" | 11 #include "bindings/modules/v8/StringOrUnsignedLong.h" |
| 12 #include "core/dom/ContextLifecycleObserver.h" |
10 #include "modules/bluetooth/BluetoothDevice.h" | 13 #include "modules/bluetooth/BluetoothDevice.h" |
| 14 #include "mojo/public/cpp/bindings/associated_binding.h" |
11 #include "platform/heap/Heap.h" | 15 #include "platform/heap/Heap.h" |
12 #include "public/platform/modules/bluetooth/web_bluetooth.mojom-blink.h" | 16 #include "public/platform/modules/bluetooth/web_bluetooth.mojom-blink.h" |
13 #include "wtf/text/WTFString.h" | 17 #include "wtf/text/WTFString.h" |
14 | 18 |
15 namespace blink { | 19 namespace blink { |
16 | 20 |
17 class BluetoothDevice; | 21 class BluetoothDevice; |
18 class ScriptPromise; | 22 class ScriptPromise; |
19 class ScriptPromiseResolver; | 23 class ScriptPromiseResolver; |
20 class ScriptState; | 24 class ScriptState; |
21 | 25 |
22 // BluetoothRemoteGATTServer provides a way to interact with a connected | 26 // BluetoothRemoteGATTServer provides a way to interact with a connected |
23 // bluetooth peripheral. | 27 // bluetooth peripheral. |
24 class BluetoothRemoteGATTServer final | 28 class BluetoothRemoteGATTServer |
25 : public GarbageCollected<BluetoothRemoteGATTServer>, | 29 : public GarbageCollectedFinalized<BluetoothRemoteGATTServer>, |
26 public ScriptWrappable { | 30 public ScriptWrappable, |
| 31 public ContextLifecycleObserver, |
| 32 public mojom::blink::WebBluetoothServerClient { |
| 33 USING_PRE_FINALIZER(BluetoothRemoteGATTServer, Dispose); |
27 DEFINE_WRAPPERTYPEINFO(); | 34 DEFINE_WRAPPERTYPEINFO(); |
| 35 USING_GARBAGE_COLLECTED_MIXIN(BluetoothRemoteGATTServer); |
28 | 36 |
29 public: | 37 public: |
30 BluetoothRemoteGATTServer(BluetoothDevice*); | 38 BluetoothRemoteGATTServer(ExecutionContext*, BluetoothDevice*); |
31 | 39 |
32 static BluetoothRemoteGATTServer* Create(BluetoothDevice*); | 40 static BluetoothRemoteGATTServer* Create(ExecutionContext*, BluetoothDevice*); |
| 41 |
| 42 // ContextLifecycleObserver: |
| 43 void contextDestroyed(ExecutionContext*) override; |
| 44 |
| 45 // mojom::blink::WebBluetoothServerClient: |
| 46 void GATTServerDisconnected() override; |
33 | 47 |
34 void SetConnected(bool connected) { m_connected = connected; } | 48 void SetConnected(bool connected) { m_connected = connected; } |
35 | 49 |
36 // The Active Algorithms set is maintained so that disconnection, i.e. | 50 // The Active Algorithms set is maintained so that disconnection, i.e. |
37 // disconnect() method or the device disconnecting by itself, can be detected | 51 // disconnect() method or the device disconnecting by itself, can be detected |
38 // by algorithms. They check via RemoveFromActiveAlgorithms that their | 52 // by algorithms. They check via RemoveFromActiveAlgorithms that their |
39 // resolvers is still in the set of active algorithms. | 53 // resolvers is still in the set of active algorithms. |
40 // | 54 // |
41 // Adds |resolver| to the set of Active Algorithms. CHECK-fails if | 55 // Adds |resolver| to the set of Active Algorithms. CHECK-fails if |
42 // |resolver| was already added. | 56 // |resolver| was already added. |
43 void AddToActiveAlgorithms(ScriptPromiseResolver*); | 57 void AddToActiveAlgorithms(ScriptPromiseResolver*); |
44 // Removes |resolver| from the set of Active Algorithms if it was in the set | 58 // Removes |resolver| from the set of Active Algorithms if it was in the set |
45 // and returns true, false otherwise. | 59 // and returns true, false otherwise. |
46 bool RemoveFromActiveAlgorithms(ScriptPromiseResolver*); | 60 bool RemoveFromActiveAlgorithms(ScriptPromiseResolver*); |
47 // Removes all ScriptPromiseResolvers from the set of Active Algorithms. | 61 // Removes all ScriptPromiseResolvers from the set of Active Algorithms. |
48 void ClearActiveAlgorithms() { m_activeAlgorithms.clear(); } | 62 void ClearActiveAlgorithms() { m_activeAlgorithms.clear(); } |
49 | 63 |
| 64 // If gatt is connected then sets gatt.connected to false and disconnects. |
| 65 // This function only performs the necessary steps to ensure a device |
| 66 // disconnects therefore it should only be used when the object is being |
| 67 // garbage collected or the context is being destroyed. |
| 68 void DisconnectIfConnected(); |
| 69 |
| 70 // Performs necessary cleanup when a device disconnects and fires |
| 71 // gattserverdisconnected event. |
| 72 void CleanupDisconnectedDeviceAndFireEvent(); |
| 73 |
| 74 void DispatchDisconnected(); |
| 75 |
| 76 // USING_PRE_FINALIZER interface. |
| 77 // Called before the object gets garbage collected. |
| 78 void Dispose(); |
| 79 |
50 // Interface required by Garbage Collectoin: | 80 // Interface required by Garbage Collectoin: |
51 DECLARE_VIRTUAL_TRACE(); | 81 DECLARE_VIRTUAL_TRACE(); |
52 | 82 |
53 // IDL exposed interface: | 83 // IDL exposed interface: |
54 BluetoothDevice* device() { return m_device; } | 84 BluetoothDevice* device() { return m_device; } |
55 bool connected() { return m_connected; } | 85 bool connected() { return m_connected; } |
56 ScriptPromise connect(ScriptState*); | 86 ScriptPromise connect(ScriptState*); |
57 void disconnect(ScriptState*); | 87 void disconnect(ScriptState*); |
58 ScriptPromise getPrimaryService(ScriptState*, | 88 ScriptPromise getPrimaryService(ScriptState*, |
59 const StringOrUnsignedLong& service, | 89 const StringOrUnsignedLong& service, |
(...skipping 12 matching lines...) Expand all Loading... |
72 void ConnectCallback(ScriptPromiseResolver*, | 102 void ConnectCallback(ScriptPromiseResolver*, |
73 mojom::blink::WebBluetoothResult); | 103 mojom::blink::WebBluetoothResult); |
74 void GetPrimaryServicesCallback( | 104 void GetPrimaryServicesCallback( |
75 const String& requestedServiceUUID, | 105 const String& requestedServiceUUID, |
76 mojom::blink::WebBluetoothGATTQueryQuantity, | 106 mojom::blink::WebBluetoothGATTQueryQuantity, |
77 ScriptPromiseResolver*, | 107 ScriptPromiseResolver*, |
78 mojom::blink::WebBluetoothResult, | 108 mojom::blink::WebBluetoothResult, |
79 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> | 109 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> |
80 services); | 110 services); |
81 | 111 |
| 112 void CloseClientBindings(); |
| 113 |
82 // Contains a ScriptPromiseResolver corresponding to each active algorithm | 114 // Contains a ScriptPromiseResolver corresponding to each active algorithm |
83 // using this server’s connection. | 115 // using this server’s connection. |
84 HeapHashSet<Member<ScriptPromiseResolver>> m_activeAlgorithms; | 116 HeapHashSet<Member<ScriptPromiseResolver>> m_activeAlgorithms; |
85 | 117 |
| 118 std::list<std::unique_ptr< |
| 119 mojo::AssociatedBinding<mojom::blink::WebBluetoothServerClient>>> |
| 120 m_clientBindings; |
| 121 |
86 Member<BluetoothDevice> m_device; | 122 Member<BluetoothDevice> m_device; |
87 bool m_connected; | 123 bool m_connected; |
88 }; | 124 }; |
89 | 125 |
90 } // namespace blink | 126 } // namespace blink |
91 | 127 |
92 #endif // BluetoothDevice_h | 128 #endif // BluetoothDevice_h |
OLD | NEW |