File: bluetooth_chooser_controller.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (110 lines) | stat: -rw-r--r-- 3,883 bytes parent folder | download | duplicates (2)
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
// 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 COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_
#define COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_

#include <stddef.h>

#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "components/permissions/chooser_controller.h"
#include "content/public/browser/bluetooth_chooser.h"

namespace content {
class RenderFrameHost;
}

namespace permissions {

// BluetoothChooserController is a chooser that presents a list of
// Bluetooth device names, which come from |bluetooth_chooser_desktop_|.
// It can be used by WebBluetooth API to get the user's permission to
// access a Bluetooth device.
class BluetoothChooserController : public ChooserController {
 public:
  BluetoothChooserController(
      content::RenderFrameHost* owner,
      const content::BluetoothChooser::EventHandler& event_handler,
      std::u16string title);

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

  ~BluetoothChooserController() override;

  // ChooserController:
  bool ShouldShowIconBeforeText() const override;
  bool ShouldShowReScanButton() const override;
  std::u16string GetNoOptionsText() const override;
  std::u16string GetOkButtonLabel() const override;
  std::pair<std::u16string, std::u16string> GetThrobberLabelAndTooltip()
      const override;
  size_t NumOptions() const override;
  int GetSignalStrengthLevel(size_t index) const override;
  bool IsConnected(size_t index) const override;
  bool IsPaired(size_t index) const override;
  std::u16string GetOption(size_t index) const override;
  void RefreshOptions() override;
  void Select(const std::vector<size_t>& indices) override;
  void Cancel() override;
  void Close() override;

  // Update the state of the Bluetooth adapter.
  void OnAdapterPresenceChanged(
      content::BluetoothChooser::AdapterPresence presence);

  // Update the Bluetooth discovery state and let the user know whether
  // discovery is happening.
  void OnDiscoveryStateChanged(content::BluetoothChooser::DiscoveryState state);

  // Shows a new device in the chooser or updates its information.
  // The range of |signal_strength_level| is -1 to 4 inclusively.
  void AddOrUpdateDevice(const std::string& device_id,
                         bool should_update_name,
                         const std::u16string& device_name,
                         bool is_gatt_connected,
                         bool is_paired,
                         int signal_strength_level);

  // Tells the chooser that a device is no longer available.
  void RemoveDevice(const std::string& device_id);

  // Called when |event_handler_| is no longer valid and should not be used
  // any more.
  void ResetEventHandler();

  // Get a weak pointer to this controller.
  base::WeakPtr<BluetoothChooserController> GetWeakPtr();

 private:
  struct BluetoothDeviceInfo {
    std::string id;
    int signal_strength_level;
    bool is_connected;
    bool is_paired;
  };

  // Clears |device_names_and_ids_| and |device_name_counts_|. Called when
  // Bluetooth adapter is turned on or off, or when re-scan happens.
  void ClearAllDevices();

  std::vector<BluetoothDeviceInfo> devices_;
  std::unordered_map<std::string, std::u16string> device_id_to_name_map_;
  // Maps from device name to number of devices with that name.
  std::unordered_map<std::u16string, int> device_name_counts_;

  content::BluetoothChooser::EventHandler event_handler_;

  base::WeakPtrFactory<BluetoothChooserController> weak_factory_{this};
};

}  // namespace permissions

#endif  // COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_