File: bluetooth_adapter_factory_wrapper.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (114 lines) | stat: -rw-r--r-- 4,955 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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_
#define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_

#include <unordered_set>

#include "base/memory/raw_ptr.h"
#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace content {

class WebBluetoothServiceImpl;

// Wrapper around BluetoothAdapterFactory that allows us to change
// the underlying BluetoothAdapter object and have the observers
// observe the new instance of the object.
// TODO(ortuno): Once there is no need to swap the adapter to change its
// behavior observers should add/remove themselves to/from the adapter.
// http://crbug.com/603291
class CONTENT_EXPORT BluetoothAdapterFactoryWrapper {
 public:
  using AcquireAdapterCallback =
      base::OnceCallback<void(scoped_refptr<device::BluetoothAdapter>)>;

  BluetoothAdapterFactoryWrapper();
  BluetoothAdapterFactoryWrapper(const BluetoothAdapterFactoryWrapper& other) =
      delete;
  BluetoothAdapterFactoryWrapper& operator=(
      const BluetoothAdapterFactoryWrapper& other) = delete;
  ~BluetoothAdapterFactoryWrapper();

  static BluetoothAdapterFactoryWrapper& Get();

  // Returns true if the platform supports Bluetooth Low Energy or if
  // SetBluetoothAdapterOverride has been called.
  bool IsLowEnergySupported();

  // Adds |service| to the set of adapter observers if this is the first time
  // the service has tried to acquire the adapter. If another service has
  // acquired the adapter in the past it adds |service| as an observer to that
  // adapter, otherwise it gets a new adapter and adds |service| to it. Runs
  // |callback| with the adapter |service| has been added to.
  void AcquireAdapter(WebBluetoothServiceImpl* service,
                      AcquireAdapterCallback callback);
  // Removes |service| from the list of adapter observers if |service|
  // has acquired the adapter in the past. If there are no more observers
  // it deletes the reference to the adapter.
  void ReleaseAdapter(WebBluetoothServiceImpl* service);

  // Returns an adapter if |service| has acquired an adapter in the past and
  // this instance holds a reference to an adapter. Otherwise returns nullptr.
  device::BluetoothAdapter* GetAdapter(WebBluetoothServiceImpl* service);

  // Sets a new BluetoothAdapter to be returned by GetAdapter. When setting
  // a new adapter all observers from the old adapter are removed and added
  // to |test_adapter|.
  void SetBluetoothAdapterOverride(
      scoped_refptr<device::BluetoothAdapter> test_adapter);

 private:
  friend class WebBluetoothServiceImplTestWithBaseAdapter;

  void OnGetAdapter(AcquireAdapterCallback continuation,
                    scoped_refptr<device::BluetoothAdapter> adapter);
  void OnGetOverrideAdapter(
      AcquireAdapterCallback continuation,
      scoped_refptr<device::BluetoothAdapter> override_adapter);

  bool HasAdapter(WebBluetoothServiceImpl* service);
  void MaybeAddAdapterObserver(WebBluetoothServiceImpl* service);
  void RemoveAdapterObserver(WebBluetoothServiceImpl* service);
  scoped_refptr<device::BluetoothAdapter> GetActiveAdapter();

  // Sets |adapter_| to a BluetoothAdapter instance and register observers,
  // releasing references to previous |adapter_|.
  void SetAdapterInternal(scoped_refptr<device::BluetoothAdapter> adapter,
                          bool is_override_adapter);

  // A BluetoothAdapter instance representing an adapter of the system.
  scoped_refptr<device::BluetoothAdapter> adapter_;

  // A BluetoothAdapter override instance configured for testing purposes.
  scoped_refptr<device::BluetoothAdapter> override_adapter_;

  // A BluetoothAdapter instance configured for testing purposes which will be
  // activated by the first call to AcquireAdapter(). A pending override adapter
  // is required to support graceful removal of an already supplied override
  // adapter.
  scoped_refptr<device::BluetoothAdapter> pending_override_adapter_;

  // We keep a list of all observers so that when the adapter gets swapped,
  // we can remove all observers from the old adapter and add them to the
  // new adapter.
  std::unordered_set<raw_ptr<WebBluetoothServiceImpl, CtnExperimental>>
      adapter_observers_;

  // Should only be called on the UI thread.
  THREAD_CHECKER(thread_checker_);

  // Weak pointer factory for generating 'this' pointers that might live longer
  // than we do.
  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<BluetoothAdapterFactoryWrapper> weak_ptr_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ADAPTER_FACTORY_WRAPPER_H_