1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-blink-forward.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_typedefs.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_piece.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_data_view.h"
#include "third_party/blink/renderer/modules/bluetooth/bluetooth_remote_gatt_service.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver_set.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class BluetoothCharacteristicProperties;
class BluetoothDevice;
class ExceptionState;
class ExecutionContext;
class ScriptState;
// BluetoothRemoteGATTCharacteristic represents a GATT Characteristic, which is
// a basic data element that provides further information about a peripheral's
// service.
class BluetoothRemoteGATTCharacteristic final
: public EventTarget,
public ActiveScriptWrappable<BluetoothRemoteGATTCharacteristic>,
public ExecutionContextLifecycleObserver,
public mojom::blink::WebBluetoothCharacteristicClient {
DEFINE_WRAPPERTYPEINFO();
public:
explicit BluetoothRemoteGATTCharacteristic(
ExecutionContext*,
mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr,
BluetoothRemoteGATTService*,
BluetoothDevice*);
// mojom::blink::WebBluetoothCharacteristicClient:
void RemoteCharacteristicValueChanged(
base::span<const uint8_t> value) override;
// ExecutionContextLifecycleObserver interface.
void ContextDestroyed() override {}
// EventTarget methods:
const AtomicString& InterfaceName() const override;
ExecutionContext* GetExecutionContext() const override;
// ActiveScriptWrappable methods:
bool HasPendingActivity() const override;
// Interface required by garbage collection.
void Trace(Visitor*) const override;
// IDL exposed interface:
BluetoothRemoteGATTService* service() { return service_.Get(); }
String uuid() { return characteristic_->uuid; }
BluetoothCharacteristicProperties* properties() { return properties_.Get(); }
NotShared<DOMDataView> value() const { return value_; }
ScriptPromise<BluetoothRemoteGATTDescriptor> getDescriptor(
ScriptState* script_state,
const V8BluetoothDescriptorUUID* descriptor_uuid,
ExceptionState& exception_state);
ScriptPromise<IDLSequence<BluetoothRemoteGATTDescriptor>> getDescriptors(
ScriptState*,
ExceptionState&);
ScriptPromise<IDLSequence<BluetoothRemoteGATTDescriptor>> getDescriptors(
ScriptState* script_state,
const V8BluetoothDescriptorUUID* descriptor_uuid,
ExceptionState& exception_state);
ScriptPromise<NotShared<DOMDataView>> readValue(ScriptState*,
ExceptionState&);
ScriptPromise<IDLUndefined> writeValue(ScriptState*,
base::span<const uint8_t> value,
ExceptionState&);
ScriptPromise<IDLUndefined> writeValueWithResponse(
ScriptState*,
base::span<const uint8_t> value,
ExceptionState&);
ScriptPromise<IDLUndefined> writeValueWithoutResponse(
ScriptState*,
base::span<const uint8_t> value,
ExceptionState&);
ScriptPromise<BluetoothRemoteGATTCharacteristic> startNotifications(
ScriptState*,
ExceptionState&);
ScriptPromise<BluetoothRemoteGATTCharacteristic> stopNotifications(
ScriptState*,
ExceptionState&);
DEFINE_ATTRIBUTE_EVENT_LISTENER(characteristicvaluechanged,
kCharacteristicvaluechanged)
protected:
// EventTarget overrides.
void AddedEventListener(const AtomicString& event_type,
RegisteredEventListener&) override;
private:
friend class BluetoothRemoteGATTDescriptor;
struct DeferredValueChange : public GarbageCollected<DeferredValueChange> {
DeferredValueChange(Member<Event> event,
NotShared<DOMDataView> dom_data_view,
ScriptPromiseResolver<NotShared<DOMDataView>>* resolver)
: event(event), dom_data_view(dom_data_view), resolver(resolver) {}
// GarbageCollectedMixin:
void Trace(Visitor*) const;
Member<Event> event; // Event to dispatch before resolving promise.
NotShared<DOMDataView> dom_data_view;
// Possibly null.
Member<ScriptPromiseResolver<NotShared<DOMDataView>>> resolver;
};
BluetoothRemoteGATTServer* GetGatt() const {
return service_->device()->gatt();
}
Bluetooth* GetBluetooth() const { return device_->GetBluetooth(); }
void ReadValueCallback(ScriptPromiseResolver<NotShared<DOMDataView>>*,
mojom::blink::WebBluetoothResult,
base::span<const uint8_t> value);
void WriteValueCallback(ScriptPromiseResolver<IDLUndefined>*,
DOMDataView* new_value,
mojom::blink::WebBluetoothResult);
// Callback for startNotifictions/stopNotifications.
// |started| is true if called as a result of startNotifictions() and
// false if called as a result of stopNotifications().
void NotificationsCallback(
ScriptPromiseResolver<BluetoothRemoteGATTCharacteristic>*,
bool started,
mojom::blink::WebBluetoothResult);
ScriptPromise<IDLUndefined> WriteCharacteristicValue(
ScriptState*,
base::span<const uint8_t> value,
mojom::blink::WebBluetoothWriteType,
ExceptionState&);
ScriptPromise<IDLSequence<BluetoothRemoteGATTDescriptor>> GetDescriptorsImpl(
ScriptState*,
ExceptionState&,
const String& descriptor_uuid = String());
void GetDescriptorsCallback(
const String& requested_descriptor_uuid,
const String& characteristic_instance_id,
mojom::blink::WebBluetoothGATTQueryQuantity,
ScriptPromiseResolverBase*,
mojom::blink::WebBluetoothResult,
std::optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>>
descriptors);
String CreateInvalidCharacteristicErrorMessage();
// Still waiting for acknowledgement from device of request for notifications?
bool notification_registration_in_progress() const {
return num_in_flight_notification_registrations_ > 0;
}
mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic_;
Member<BluetoothRemoteGATTService> service_;
Member<BluetoothCharacteristicProperties> properties_;
NotShared<DOMDataView> value_;
Member<BluetoothDevice> device_;
HeapMojoAssociatedReceiverSet<mojom::blink::WebBluetoothCharacteristicClient,
BluetoothRemoteGATTCharacteristic>
receivers_;
uint32_t num_in_flight_notification_registrations_ = 0;
// Queue of characteristicvaluechanged events created if a value changes
// while startNotificications() is in the process of registering a listener.
HeapVector<Member<DeferredValueChange>> deferred_value_change_data_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
|