File: bluetooth_event_router.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (198 lines) | stat: -rw-r--r-- 8,322 bytes parent folder | download | duplicates (5)
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
195
196
197
198
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
#define EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_

#include <map>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "extensions/browser/extension_host_registry.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/api/bluetooth.h"
#include "extensions/common/api/bluetooth_private.h"
#include "extensions/common/extension_id.h"

namespace content {
class BrowserContext;
}

namespace device {

class BluetoothDevice;
class BluetoothDiscoverySession;

}  // namespace device

namespace extensions {
class BluetoothApiPairingDelegate;
struct EventListenerInfo;

class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
                             public ExtensionRegistryObserver,
                             public ExtensionHostRegistry::Observer {
 public:
  explicit BluetoothEventRouter(content::BrowserContext* context);

  BluetoothEventRouter(const BluetoothEventRouter&) = delete;
  BluetoothEventRouter& operator=(const BluetoothEventRouter&) = delete;

  ~BluetoothEventRouter() override;

  // Returns true if adapter_ has been initialized for testing or bluetooth
  // adapter is available for the current platform.
  bool IsBluetoothSupported() const;

  void GetAdapter(device::BluetoothAdapterFactory::AdapterCallback callback);

  // Requests that a new device discovery session be initiated for extension
  // with id `extension_id`. `callback` is called, if a session has been
  // initiated. `error_callback` is called, if the adapter failed to initiate
  // the session or if an active session already exists for the extension.
  void StartDiscoverySession(device::BluetoothAdapter* adapter,
                             const ExtensionId& extension_id,
                             base::OnceClosure callback,
                             base::OnceClosure error_callback);

  // Requests that the active discovery session that belongs to the extension
  // with id `extension_id` be terminated. `callback` is called, if the session
  // successfully ended. `error_callback` is called, if the adapter failed to
  // terminate the session or if no active discovery session exists for the
  // extension.
  void StopDiscoverySession(device::BluetoothAdapter* adapter,
                            const ExtensionId& extension_id,
                            base::OnceClosure callback,
                            base::OnceClosure error_callback);

  // Requests that the filter associated with discovery session that belongs
  // to the extension with id `extension_id` be set to `discovery_filter`.
  // `callback` is called, if the filter was successfully updated.
  // `error_callback` is called, if filter update failed.
  void SetDiscoveryFilter(
      std::unique_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
      device::BluetoothAdapter* adapter,
      const ExtensionId& extension_id,
      base::OnceClosure callback,
      base::OnceClosure error_callback);

  // Called when a bluetooth event listener is added.
  void OnListenerAdded(const EventListenerInfo& details);

  // Called when a bluetooth event listener is removed.
  void OnListenerRemoved(const EventListenerInfo& details);

  // Adds a pairing delegate for an extension.
  void AddPairingDelegate(const ExtensionId& extension_id);

  // Removes the pairing delegate for an extension.
  void RemovePairingDelegate(const ExtensionId& extension_id);

  // Returns the pairing delegate for an extension or NULL if it doesn't have a
  // pairing delegate.
  BluetoothApiPairingDelegate* GetPairingDelegate(
      const ExtensionId& extension_id);

  // Exposed for testing.
  void SetAdapterForTest(device::BluetoothAdapter* adapter) {
    adapter_ = adapter;
  }

  // Override from device::BluetoothAdapter::Observer.
  void AdapterPresentChanged(device::BluetoothAdapter* adapter,
                             bool present) override;
  void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
                             bool has_power) override;
  void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
                                 bool discovering) override;
  void DeviceAdded(device::BluetoothAdapter* adapter,
                   device::BluetoothDevice* device) override;
  void DeviceChanged(device::BluetoothAdapter* adapter,
                     device::BluetoothDevice* device) override;
  void DeviceRemoved(device::BluetoothAdapter* adapter,
                     device::BluetoothDevice* device) override;
  void DeviceAddressChanged(device::BluetoothAdapter* adapter,
                            device::BluetoothDevice* device,
                            const std::string& old_address) override;

  // Overridden from ExtensionRegistryObserver.
  void OnExtensionUnloaded(content::BrowserContext* browser_context,
                           const Extension* extension,
                           UnloadedExtensionReason reason) override;

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() { return "BluetoothEventRouter"; }
  static const bool kServiceRedirectedInIncognito = true;
  static const bool kServiceIsNULLWhileTesting = true;

 private:
  void StartDiscoverySessionImpl(device::BluetoothAdapter* adapter,
                                 const ExtensionId& extension_id,
                                 base::OnceClosure callback,
                                 base::OnceClosure error_callback);
  void AddPairingDelegateImpl(const ExtensionId& extension_id);

  void OnAdapterInitialized(
      device::BluetoothAdapterFactory::AdapterCallback callback,
      scoped_refptr<device::BluetoothAdapter> adapter);
  void MaybeReleaseAdapter();
  void DispatchAdapterStateEvent();
  void DispatchDeviceEvent(events::HistogramValue histogram_value,
                           const std::string& event_name,
                           device::BluetoothDevice* device);
  void CleanUpForExtension(const ExtensionId& extension_id);
  void CleanUpAllExtensions();
  void OnStartDiscoverySession(
      const ExtensionId& extension_id,
      base::OnceClosure callback,
      std::unique_ptr<device::BluetoothDiscoverySession> discovery_session);

  // ExtensionHostRegistry::Observer:
  void OnExtensionHostDestroyed(content::BrowserContext* browser_context,
                                ExtensionHost* host) override;

  raw_ptr<content::BrowserContext> browser_context_;
  scoped_refptr<device::BluetoothAdapter> adapter_;

  // Map of listener id -> listener count.
  std::map<std::string, int> event_listener_count_;

  // A map that maps extension ids to BluetoothDiscoverySession pointers.
  typedef std::map<std::string,
                   raw_ptr<device::BluetoothDiscoverySession, CtnExperimental>>
      DiscoverySessionMap;
  DiscoverySessionMap discovery_session_map_;

  typedef std::map<std::string,
                   raw_ptr<device::BluetoothDiscoveryFilter, CtnExperimental>>
      PreSetFilterMap;

  // Maps an extension id to it's pre-set discovery filter.
  PreSetFilterMap pre_set_filter_map_;

  // Maps an extension id to its pairing delegate.
  typedef std::map<std::string,
                   raw_ptr<BluetoothApiPairingDelegate, CtnExperimental>>
      PairingDelegateMap;
  PairingDelegateMap pairing_delegate_map_;

  base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
      extension_registry_observation_{this};
  base::ScopedObservation<ExtensionHostRegistry,
                          ExtensionHostRegistry::Observer>
      extension_host_registry_observation_{this};

  base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_{this};
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_