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
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
7 
8 #include "mojo/public/cpp/bindings/associated_receiver_set.h"
9 #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-blink-forward.h"
10 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
11 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
12 #include "third_party/blink/renderer/core/typed_arrays/dom_array_piece.h"
13 #include "third_party/blink/renderer/core/typed_arrays/dom_data_view.h"
14 #include "third_party/blink/renderer/modules/bluetooth/bluetooth_remote_gatt_service.h"
15 #include "third_party/blink/renderer/modules/event_target_modules.h"
16 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
17 #include "third_party/blink/renderer/platform/heap/handle.h"
18 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
19 
20 namespace blink {
21 
22 class BluetoothCharacteristicProperties;
23 class BluetoothDevice;
24 class ExceptionState;
25 class ExecutionContext;
26 class ScriptPromise;
27 class ScriptState;
28 
29 // BluetoothRemoteGATTCharacteristic represents a GATT Characteristic, which is
30 // a basic data element that provides further information about a peripheral's
31 // service.
32 //
33 // Callbacks providing WebBluetoothRemoteGATTCharacteristicInit objects are
34 // handled by CallbackPromiseAdapter templatized with this class. See this
35 // class's "Interface required by CallbackPromiseAdapter" section and the
36 // CallbackPromiseAdapter class comments.
37 class BluetoothRemoteGATTCharacteristic final
38     : public EventTargetWithInlineData,
39       public ActiveScriptWrappable<BluetoothRemoteGATTCharacteristic>,
40       public ExecutionContextLifecycleObserver,
41       public mojom::blink::WebBluetoothCharacteristicClient {
42   USING_PRE_FINALIZER(BluetoothRemoteGATTCharacteristic, Dispose);
43   DEFINE_WRAPPERTYPEINFO();
44   USING_GARBAGE_COLLECTED_MIXIN(BluetoothRemoteGATTCharacteristic);
45 
46  public:
47   explicit BluetoothRemoteGATTCharacteristic(
48       ExecutionContext*,
49       mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr,
50       BluetoothRemoteGATTService*,
51       BluetoothDevice*);
52 
53   // Save value.
54   void SetValue(DOMDataView*);
55 
56   // mojom::blink::WebBluetoothCharacteristicClient:
57   void RemoteCharacteristicValueChanged(
58       const WTF::Vector<uint8_t>& value) override;
59 
60   // ExecutionContextLifecycleObserver interface.
61   void ContextDestroyed() override;
62 
63   // USING_PRE_FINALIZER interface.
64   // Called before the object gets garbage collected.
65   void Dispose();
66 
67   // EventTarget methods:
68   const AtomicString& InterfaceName() const override;
69   ExecutionContext* GetExecutionContext() const override;
70 
71   // ActiveScriptWrappable methods:
72   bool HasPendingActivity() const override;
73 
74   // Interface required by garbage collection.
75   void Trace(Visitor*) override;
76 
77   // IDL exposed interface:
service()78   BluetoothRemoteGATTService* service() { return service_; }
uuid()79   String uuid() { return characteristic_->uuid; }
properties()80   BluetoothCharacteristicProperties* properties() { return properties_; }
value()81   DOMDataView* value() const { return value_; }
82   ScriptPromise getDescriptor(ScriptState*,
83                               const StringOrUnsignedLong& descriptor,
84                               ExceptionState&);
85   ScriptPromise getDescriptors(ScriptState*, ExceptionState&);
86   ScriptPromise getDescriptors(ScriptState*,
87                                const StringOrUnsignedLong& descriptor,
88                                ExceptionState&);
89   ScriptPromise readValue(ScriptState*, ExceptionState&);
90   ScriptPromise writeValue(ScriptState*, const DOMArrayPiece&, ExceptionState&);
91   ScriptPromise startNotifications(ScriptState*, ExceptionState&);
92   ScriptPromise stopNotifications(ScriptState*, ExceptionState&);
93 
94   DEFINE_ATTRIBUTE_EVENT_LISTENER(characteristicvaluechanged,
95                                   kCharacteristicvaluechanged)
96 
97  protected:
98   // EventTarget overrides.
99   void AddedEventListener(const AtomicString& event_type,
100                           RegisteredEventListener&) override;
101 
102  private:
103   friend class BluetoothRemoteGATTDescriptor;
104 
GetGatt()105   BluetoothRemoteGATTServer* GetGatt() { return service_->device()->gatt(); }
106 
107   void ReadValueCallback(ScriptPromiseResolver*,
108                          mojom::blink::WebBluetoothResult,
109                          const base::Optional<Vector<uint8_t>>& value);
110   void WriteValueCallback(ScriptPromiseResolver*,
111                           const Vector<uint8_t>& value,
112                           mojom::blink::WebBluetoothResult);
113   void NotificationsCallback(ScriptPromiseResolver*,
114                              mojom::blink::WebBluetoothResult);
115 
116   ScriptPromise GetDescriptorsImpl(ScriptState*,
117                                    ExceptionState&,
118                                    mojom::blink::WebBluetoothGATTQueryQuantity,
119                                    const String& descriptor_uuid = String());
120 
121   void GetDescriptorsCallback(
122       const String& requested_descriptor_uuid,
123       const String& characteristic_instance_id,
124       mojom::blink::WebBluetoothGATTQueryQuantity,
125       ScriptPromiseResolver*,
126       mojom::blink::WebBluetoothResult,
127       base::Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>>
128           descriptors);
129 
130   String CreateInvalidCharacteristicErrorMessage();
131 
132   mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic_;
133   Member<BluetoothRemoteGATTService> service_;
134   Member<BluetoothCharacteristicProperties> properties_;
135   Member<DOMDataView> value_;
136   Member<BluetoothDevice> device_;
137   mojo::AssociatedReceiverSet<mojom::blink::WebBluetoothCharacteristicClient>
138       receivers_;
139 };
140 
141 }  // namespace blink
142 
143 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
144