OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 Bluetooth_h | 5 #ifndef Bluetooth_h |
6 #define Bluetooth_h | 6 #define Bluetooth_h |
7 | 7 |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptWrappable.h" | 9 #include "bindings/core/v8/ScriptWrappable.h" |
10 #include "modules/bluetooth/BluetoothDevice.h" | 10 #include "modules/bluetooth/BluetoothDevice.h" |
| 11 #include "mojo/public/cpp/bindings/associated_binding.h" |
11 #include "platform/heap/Handle.h" | 12 #include "platform/heap/Handle.h" |
| 13 #include "public/platform/modules/bluetooth/web_bluetooth.mojom-blink.h" |
12 #include <memory> | 14 #include <memory> |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
| 18 class BluetoothRemoteGATTCharacteristic; |
16 class RequestDeviceOptions; | 19 class RequestDeviceOptions; |
17 class ScriptPromise; | 20 class ScriptPromise; |
18 class ScriptState; | 21 class ScriptState; |
19 | 22 |
20 class Bluetooth : public GarbageCollected<Bluetooth>, public ScriptWrappable { | 23 class Bluetooth : public GarbageCollectedFinalized<Bluetooth>, |
| 24 public ScriptWrappable, |
| 25 public mojom::blink::WebBluetoothServiceClient { |
21 DEFINE_WRAPPERTYPEINFO(); | 26 DEFINE_WRAPPERTYPEINFO(); |
| 27 USING_PRE_FINALIZER(Bluetooth, dispose); |
22 | 28 |
23 public: | 29 public: |
24 static Bluetooth* create() { return new Bluetooth(); } | 30 static Bluetooth* create() { return new Bluetooth(); } |
25 | 31 |
| 32 void dispose(); |
| 33 |
26 // BluetoothDiscovery interface | 34 // BluetoothDiscovery interface |
27 ScriptPromise requestDevice(ScriptState*, | 35 ScriptPromise requestDevice(ScriptState*, |
28 const RequestDeviceOptions&, | 36 const RequestDeviceOptions&, |
29 ExceptionState&); | 37 ExceptionState&); |
30 | 38 |
| 39 mojom::blink::WebBluetoothService* service() { return m_service.get(); } |
| 40 |
| 41 void addDevice(const String& deviceId, BluetoothDevice*); |
| 42 |
| 43 void removeDevice(const String& deviceId); |
| 44 |
| 45 void registerCharacteristicObject(const String& characteristicInstanceId, |
| 46 BluetoothRemoteGATTCharacteristic*); |
| 47 void characteristicObjectRemoved(const String& characteristicInstanceId); |
| 48 |
31 // Interface required by Garbage Collection: | 49 // Interface required by Garbage Collection: |
32 DECLARE_VIRTUAL_TRACE(); | 50 DECLARE_VIRTUAL_TRACE(); |
33 | 51 |
34 private: | 52 private: |
35 friend class RequestDeviceCallback; | 53 Bluetooth(); |
36 | 54 |
37 BluetoothDevice* getBluetoothDeviceRepresentingDevice( | 55 // mojom::blink::WebBluetoothServiceClient: |
38 std::unique_ptr<WebBluetoothDeviceInit>, | 56 void RemoteCharacteristicValueChanged( |
39 ScriptPromiseResolver*); | 57 const WTF::String& characteristicInstanceId, |
| 58 const WTF::Vector<uint8_t>& value) override; |
| 59 void GattServerDisconnected(mojom::blink::WebBluetoothDeviceIdPtr) override; |
| 60 |
| 61 BluetoothDevice* getBluetoothDeviceRepresentingDevice(const String& id, |
| 62 const String& name, |
| 63 ScriptPromiseResolver*); |
| 64 |
| 65 void RequestDeviceCallback(ScriptPromiseResolver*, |
| 66 mojom::blink::WebBluetoothResult, |
| 67 mojom::blink::WebBluetoothDevicePtr); |
40 | 68 |
41 // Map of device ids to BluetoothDevice objects. | 69 // Map of device ids to BluetoothDevice objects. |
42 // Ensures only one BluetoothDevice instance represents each | 70 // Ensures only one BluetoothDevice instance represents each |
43 // Bluetooth device inside a single global object. | 71 // Bluetooth device inside a single global object. |
44 HeapHashMap<String, Member<BluetoothDevice>> m_deviceInstanceMap; | 72 HeapHashMap<String, Member<BluetoothDevice>> m_deviceInstanceMap; |
| 73 |
| 74 // Map of characteristic instance ids to BluetoothRemoteGATTCharacteristic. |
| 75 // When characteristicObjectRemoved is called the characteristic should be |
| 76 // removed from the map. Keeps track of what characteristics have listeners. |
| 77 HeapHashMap<String, Member<BluetoothRemoteGATTCharacteristic>> |
| 78 m_activeCharacteristics; |
| 79 |
| 80 // Map of device ids to BluetoothDevice. Added in |
| 81 // BluetoothRemoteGATTServer::connect() and removed in |
| 82 // BluetoothRemoteGATTServer::disconnect(). This means a device may not |
| 83 // actually be connected while in this map, but that it will definitely be |
| 84 // removed when the page navigates. |
| 85 HeapHashMap<String, Member<BluetoothDevice>> m_connectedDevices; |
| 86 |
| 87 mojom::blink::WebBluetoothServicePtr m_service; |
| 88 |
| 89 // Binding associated with |m_service|. |
| 90 mojo::AssociatedBinding<mojom::blink::WebBluetoothServiceClient> |
| 91 m_clientBinding; |
45 }; | 92 }; |
46 | 93 |
47 } // namespace blink | 94 } // namespace blink |
48 | 95 |
49 #endif // Bluetooth_h | 96 #endif // Bluetooth_h |
OLD | NEW |