File: bluetooth_device.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,975 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 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_DEVICE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_DEVICE_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/core/dom/abort_signal.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/bluetooth/bluetooth_remote_gatt_server.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_hash_map.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class Bluetooth;
class BluetoothAttributeInstanceMap;
class BluetoothRemoteGATTCharacteristic;
class BluetoothRemoteGATTDescriptor;
class BluetoothRemoteGATTServer;
class BluetoothRemoteGATTService;
class WatchAdvertisementsOptions;

// BluetoothDevice represents a physical bluetooth device in the DOM. See IDL.
class BluetoothDevice final
    : public EventTarget,
      public ExecutionContextClient,
      public ActiveScriptWrappable<BluetoothDevice>,
      public mojom::blink::WebBluetoothAdvertisementClient {
  DEFINE_WRAPPERTYPEINFO();

 public:
  BluetoothDevice(ExecutionContext*,
                  mojom::blink::WebBluetoothDevicePtr,
                  Bluetooth*);

  BluetoothRemoteGATTService* GetOrCreateRemoteGATTService(
      mojom::blink::WebBluetoothRemoteGATTServicePtr,
      bool is_primary,
      const String& device_instance_id);
  bool IsValidService(const String& service_instance_id);

  BluetoothRemoteGATTCharacteristic* GetOrCreateRemoteGATTCharacteristic(
      ExecutionContext*,
      mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr,
      BluetoothRemoteGATTService*);
  bool IsValidCharacteristic(const String& characteristic_instance_id);

  BluetoothRemoteGATTDescriptor* GetOrCreateBluetoothRemoteGATTDescriptor(
      mojom::blink::WebBluetoothRemoteGATTDescriptorPtr,
      BluetoothRemoteGATTCharacteristic*);
  bool IsValidDescriptor(const String& descriptor_instance_id);

  // We should disconnect from the device in all of the following cases:
  // 1. When the object gets GarbageCollected e.g. it went out of scope.
  // dispose() is called in this case.
  // 2. When the parent document gets detached e.g. reloading a page.
  // stop() is called in this case.
  // TODO(ortuno): Users should be able to turn on notifications for
  // events on navigator.bluetooth and still remain connected even if the
  // BluetoothDevice object is garbage collected.

  // Performs necessary cleanup when a device disconnects and fires
  // gattserverdisconnected event.
  void ClearAttributeInstanceMapAndFireEvent();

  // EventTarget methods:
  const AtomicString& InterfaceName() const override;
  ExecutionContext* GetExecutionContext() const override;

  Bluetooth* GetBluetooth() { return bluetooth_.Get(); }

  const mojom::blink::WebBluetoothDevicePtr& GetDevice() const {
    return device_;
  }

  // Interface required by Garbage Collection:
  void Trace(Visitor*) const override;

  // IDL exposed interface:
  ScriptPromise<IDLUndefined> watchAdvertisements(
      ScriptState*,
      const WatchAdvertisementsOptions*,
      ExceptionState&);
  ScriptPromise<IDLUndefined> forget(ScriptState*, ExceptionState&);
  String id() { return device_->id.DeviceIdInBase64().c_str(); }
  String name() { return device_->name; }
  BluetoothRemoteGATTServer* gatt() { return gatt_.Get(); }
  bool watchingAdvertisements() { return client_receiver_.is_bound(); }

  void AbortWatchAdvertisements(AbortSignal* signal);

  // WebBluetoothAdvertisementClient:
  void AdvertisingEvent(mojom::blink::WebBluetoothAdvertisingEventPtr) override;

  // ActiveScriptWrappable:
  bool HasPendingActivity() const override;

  DEFINE_ATTRIBUTE_EVENT_LISTENER(advertisementreceived, kAdvertisementreceived)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(gattserverdisconnected,
                                  kGattserverdisconnected)

 protected:
  // EventTarget overrides:
  void AddedEventListener(const AtomicString& eventType,
                          RegisteredEventListener&) override;

 private:
  void WatchAdvertisementsCallback(mojom::blink::WebBluetoothResult);

  // Holds all GATT Attributes associated with this BluetoothDevice.
  Member<BluetoothAttributeInstanceMap> attribute_instance_map_;

  mojom::blink::WebBluetoothDevicePtr device_;
  Member<BluetoothRemoteGATTServer> gatt_;
  Member<Bluetooth> bluetooth_;

  Member<ScriptPromiseResolver<IDLUndefined>> watch_advertisements_resolver_;

  HeapMojoAssociatedReceiver<mojom::blink::WebBluetoothAdvertisementClient,
                             BluetoothDevice>
      client_receiver_;

  // Any unaborted signal passed to watchAdvertisements() can abort
  // advertisements for this device, regardless of previous aborts from other
  // signals. Abort algorithm handles therefore need to remain alive as long as
  // both the device and associated signal remain alive, so we store the handles
  // in a map keyed weakly by AbortSignal.
  HeapHashMap<WeakMember<AbortSignal>, Member<AbortSignal::AlgorithmHandle>>
      abort_handle_map_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_DEVICE_H_