File: bluetooth_notification_controller.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 (114 lines) | stat: -rw-r--r-- 4,891 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 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 ASH_SYSTEM_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_
#define ASH_SYSTEM_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_

#include <stdint.h>

#include <set>
#include <string>

#include "ash/ash_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"

namespace message_center {
class MessageCenter;
}  // namespace message_center

namespace ash {

// The BluetoothNotificationController receives incoming pairing requests from
// the BluetoothAdapter, and notifications of changes to the adapter state and
// set of bonded devices. It presents incoming pairing requests in the form of
// rich notifications that the user can interact with to approve the request.
class ASH_EXPORT BluetoothNotificationController
    : public device::BluetoothAdapter::Observer,
      public device::BluetoothDevice::PairingDelegate {
 public:
  explicit BluetoothNotificationController(
      message_center::MessageCenter* message_center);

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

  ~BluetoothNotificationController() override;

  // device::BluetoothAdapter::Observer override.
  void AdapterDiscoverableChanged(device::BluetoothAdapter* adapter,
                                  bool discoverable) 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;

  // device::BluetoothDevice::PairingDelegate override.
  void RequestPinCode(device::BluetoothDevice* device) override;
  void RequestPasskey(device::BluetoothDevice* device) override;
  void DisplayPinCode(device::BluetoothDevice* device,
                      const std::string& pincode) override;
  void DisplayPasskey(device::BluetoothDevice* device,
                      uint32_t passkey) override;
  void KeysEntered(device::BluetoothDevice* device, uint32_t entered) override;
  void ConfirmPasskey(device::BluetoothDevice* device,
                      uint32_t passkey) override;
  void AuthorizePairing(device::BluetoothDevice* device) override;

 private:
  friend class BluetoothNotificationControllerTest;

  static const char kBluetoothDeviceDiscoverableToastId[];
  // Identifier for the pairing notification; the Bluetooth code ensures we
  // only receive one pairing request at a time, so a single id is sufficient
  // and means we "update" one notification if not handled rather than
  // continually bugging the user.
  static const char kBluetoothDevicePairingNotificationId[];

  // Internal method called by BluetoothAdapterFactory to provide the adapter
  // object.
  void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);

  // Presents a toast to the user when the adapter becomes discoverable to
  // other nearby devices.
  void NotifyAdapterDiscoverable();

  // Presents a notification to the user that a device |device| is making a
  // pairing request. The exact message to display is given in |message| and
  // should include all relevant instructions, if |with_buttons| is true then
  // the notification will have Accept and Reject buttons, if false only the
  // usual cancel/dismiss button will be present on the notification.
  void NotifyPairing(device::BluetoothDevice* device,
                     const std::u16string& message,
                     bool with_buttons);

  // Clears any shown pairing notification now that the device has been bonded.
  void NotifyBondedDevice(device::BluetoothDevice* device);

  const raw_ptr<message_center::MessageCenter, DanglingUntriaged>
      message_center_;

  // Reference to the underlying BluetoothAdapter object, holding this reference
  // ensures we stay around as the pairing delegate for that adapter.
  scoped_refptr<device::BluetoothAdapter> adapter_;

  // Set of currently bonded devices, stored by Bluetooth address, used to
  // filter out property changes for devices that were previously bonded.
  std::set<std::string> bonded_devices_;

  // 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<BluetoothNotificationController> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_SYSTEM_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_