File: peripheral_notification_manager.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 (179 lines) | stat: -rw-r--r-- 7,440 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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_
#define CHROMEOS_ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_

#include <memory>

#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chromeos/ash/components/dbus/pciguard/pciguard_client.h"
#include "chromeos/ash/components/dbus/typecd/typecd_client.h"
#include "third_party/cros_system_api/dbus/typecd/dbus-constants.h"

namespace device {
namespace mojom {
class UsbDeviceInfo;
}  // namespace mojom
}  // namespace device

namespace ash {

// This class is responsible for listening to TypeCd and Pciguard D-Bus calls
// and translating those signals to notification observer events. It handles
// additional logic such determining if notifications are required or whether a
// guest-session notification is needed.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_PERIPHERAL_NOTIFICATION)
    PeripheralNotificationManager : public TypecdClient::Observer,
                                    public PciguardClient::Observer {
 public:
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    // Called to notify observers, primarily notification controllers, that a
    // recently plugged in Thunderbolt/USB4 device is running at limited
    // performance. This can be called multiple times.
    virtual void OnLimitedPerformancePeripheralReceived() = 0;

    // Called to notify observers, primarily notification controllers, that a
    // Thunderbolt/USB4 device has been plugged in during a guest session. Can
    // be called multiple times.
    virtual void OnGuestModeNotificationReceived(bool is_thunderbolt_only) = 0;

    // Called to notify observers, primarily notification controllers, that the
    // recently plugged in Thunderbolt/USB4 device is in the block list. The
    // block list is specified by the Pciguard Daemon.
    virtual void OnPeripheralBlockedReceived() = 0;

    // Called to notify observers, primarily notification controllers, that the
    // recently plugged in Thunderbolt/USB4 device is a billboard device that is
    // not supported by the board.
    virtual void OnBillboardDeviceConnected() = 0;

    // Called to notify user of possibly invalid dp cable. This signal will be
    // sent by typecd when the partner meets the conditions for DP alternate
    // mode, but the cable does not.
    virtual void OnInvalidDpCableWarning() = 0;

    // Called to notify the user that their USB4 device is unable to establish a
    // USB4 connection because of the cable. In this case, the connection will
    // fall back to Thunderbolt.
    virtual void OnInvalidUSB4ValidTBTCableWarning() = 0;

    // Called to notify the user that their USB4 device is unable to establish a
    // USB4 connection because of the cable. It is similar to
    // OnUSB4ToThundeboltCableWarning, but in this case the connection will
    // fall back to DisplayPort, USB 3.2 or USB 2.0.
    virtual void OnInvalidUSB4CableWarning() = 0;

    // Called to notify the user that their Thubderbolt device is unable to
    // establish a Thunderbolt connection, and will instead use DisplayPort,
    // USB 3.2 or USB 2.0.
    virtual void OnInvalidTBTCableWarning() = 0;

    // Called to notify the user when their 40 Gbps USB4 device is unable to use
    // 40 Gbps data transmission because of the cable. Transmissions speeds will
    // decrease to 20 Gbps, 10 Gbps or 5 Gbps.
    virtual void OnSpeedLimitingCableWarning() = 0;

    // Called to notify the user when their device has reached a USB device or
    // endpoint limit, and any more connected USB devices may not work.
    virtual void OnUsbDeviceOrEndpointLimit() = 0;
  };

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class PeripheralConnectivityResults {
    kTBTSupportedAndAllowed = 0,
    kTBTOnlyAndBlockedByPciguard = 1,
    kTBTOnlyAndBlockedInGuestSession = 2,
    kAltModeFallbackDueToPciguard = 3,
    kAltModeFallbackInGuestSession = 4,
    kPeripheralBlocked = 5,
    kBillboardDevice = 6,
    kInvalidDpCable = 7,
    kInvalidUSB4ValidTBTCable = 8,
    kInvalidUSB4Cable = 9,
    kInvalidTBTCable = 10,
    kSpeedLimitingCable = 11,
    kUsbDeviceOrEndpointLimit = 12,
    kMaxValue = kUsbDeviceOrEndpointLimit,
  };

  // Sets the global instance. Must be called before any calls to Get().
  static void Initialize(bool is_guest_profile, bool is_pcie_tunneling_allowed);

  // Destroys the global instance.
  static void Shutdown();

  // Gets the global instance pointer.
  static PeripheralNotificationManager* Get();

  // Returns true if the global instance is initialized.
  static bool IsInitialized();

  void SetPcieTunnelingAllowedState(bool is_pcie_tunneling_allowed);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  void OnDeviceConnected(device::mojom::UsbDeviceInfo* device);

 private:
  friend class PeripheralNotificationManagerTest;

  PeripheralNotificationManager(bool is_guest_profile,
                                bool is_pcie_tunneling_allowed);
  PeripheralNotificationManager(const PeripheralNotificationManager&) = delete;
  PeripheralNotificationManager& operator=(
      const PeripheralNotificationManager&) = delete;
  ~PeripheralNotificationManager() override;

  // TypecdClient::Observer:
  void OnThunderboltDeviceConnected(bool is_thunderbolt_only) override;
  void OnCableWarning(typecd::CableWarningType cable_warning_type) override;
  void OnUsbLimit(typecd::UsbLimitType usb_limit_type) override;

  // PciguardClient::Observer:
  void OnBlockedThunderboltDeviceConnected(
      const std::string& device_name) override;

  // Call to notify observers that a new notification is needed.
  void NotifyLimitedPerformancePeripheralReceived();
  void NotifyGuestModeNotificationReceived(bool is_thunderbolt_only);
  void NotifyPeripheralBlockedReceived();
  void OnBillboardDeviceConnected(bool billboard_is_supported);
  void NotifyInvalidDpCable();
  void NotifyInvalidUSB4ValidTBTCableWarning();
  void NotifyInvalidUSB4CableWarning();
  void NotifyInvalidTBTCableWarning();
  void NotifySpeedLimitingCableWarning();
  void NotifyUsbDeviceOrEndpointLimit();

  // Called by unit tests to set up root_prefix_ for simulating the existence
  // of a system folder.
  void SetRootPrefixForTesting(const std::string& prefix);

  const bool is_guest_profile_;
  // Pcie tunneling refers to allowing Thunderbolt/USB4 peripherals to run at
  // full capacity by utilizing the PciExpress protocol. If this is set to
  // false, we anticipate that the plugged in Thunderbolt/USB4 periphal is
  // operating at either Alt-mode (i.e. fallback to an older protocol) or
  // in a restricted state (e.g. certain devices are Thunderbolt only).
  bool is_pcie_tunneling_allowed_;
  base::ObserverList<Observer> observer_list_;

  std::string root_prefix_ = "";

  // Used for callbacks.
  base::WeakPtrFactory<PeripheralNotificationManager> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_