peripheral_notification_manager.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_
  5. #define ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_
  6. #include <memory>
  7. #include "base/component_export.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "base/observer_list_types.h"
  11. #include "chromeos/ash/components/dbus/pciguard/pciguard_client.h"
  12. #include "chromeos/ash/components/dbus/typecd/typecd_client.h"
  13. #include "third_party/cros_system_api/dbus/typecd/dbus-constants.h"
  14. namespace device {
  15. namespace mojom {
  16. class UsbDeviceInfo;
  17. } // namespace mojom
  18. } // namespace device
  19. namespace ash {
  20. // This class is responsible for listening to TypeCd and Pciguard D-Bus calls
  21. // and translating those signals to notification observer events. It handles
  22. // additional logic such determining if notifications are required or whether a
  23. // guest-session notification is needed.
  24. class COMPONENT_EXPORT(ASH_PERIPHERAL_NOTIFICATION)
  25. PeripheralNotificationManager : public TypecdClient::Observer,
  26. public PciguardClient::Observer {
  27. public:
  28. class Observer : public base::CheckedObserver {
  29. public:
  30. ~Observer() override = default;
  31. // Called to notify observers, primarily notification controllers, that a
  32. // recently plugged in Thunderbolt/USB4 device is running at limited
  33. // performance. This can be called multiple times.
  34. virtual void OnLimitedPerformancePeripheralReceived() = 0;
  35. // Called to notify observers, primarily notification controllers, that a
  36. // Thunderbolt/USB4 device has been plugged in during a guest session. Can
  37. // be called multiple times.
  38. virtual void OnGuestModeNotificationReceived(bool is_thunderbolt_only) = 0;
  39. // Called to notify observers, primarily notification controllers, that the
  40. // recently plugged in Thunderbolt/USB4 device is in the block list. The
  41. // block list is specified by the Pciguard Daemon.
  42. virtual void OnPeripheralBlockedReceived() = 0;
  43. // Called to notify observers, primarily notification controllers, that the
  44. // recently plugged in Thunderbolt/USB4 device is a billboard device that is
  45. // not supported by the board.
  46. virtual void OnBillboardDeviceConnected() = 0;
  47. // Called to notify user of possibly invalid dp cable. This signal will be
  48. // sent by typecd when the partner meets the conditions for DP alternate
  49. // mode, but the cable does not.
  50. virtual void OnInvalidDpCableWarning() = 0;
  51. // Called to notify the user that their USB4 device is unable to establish a
  52. // USB4 connection because of the cable. In this case, the connection will
  53. // fall back to Thunderbolt.
  54. virtual void OnInvalidUSB4ValidTBTCableWarning() = 0;
  55. // Called to notify the user that their USB4 device is unable to establish a
  56. // USB4 connection because of the cable. It is similar to
  57. // OnUSB4ToThundeboltCableWarning, but in this case the connection will
  58. // fall back to DisplayPort, USB 3.2 or USB 2.0.
  59. virtual void OnInvalidUSB4CableWarning() = 0;
  60. // Called to notify the user that their Thubderbolt device is unable to
  61. // establish a Thunderbolt connection, and will instead use DisplayPort,
  62. // USB 3.2 or USB 2.0.
  63. virtual void OnInvalidTBTCableWarning() = 0;
  64. // Called to notify the user when their 40 Gbps USB4 device is unable to use
  65. // 40 Gbps data transmission because of the cable. Transmissions speeds will
  66. // decrease to 20 Gbps, 10 Gbps or 5 Gbps.
  67. virtual void OnSpeedLimitingCableWarning() = 0;
  68. };
  69. // These values are persisted to logs. Entries should not be renumbered and
  70. // numeric values should never be reused.
  71. enum PeripheralConnectivityResults {
  72. kTBTSupportedAndAllowed = 0,
  73. kTBTOnlyAndBlockedByPciguard = 1,
  74. kTBTOnlyAndBlockedInGuestSession = 2,
  75. kAltModeFallbackDueToPciguard = 3,
  76. kAltModeFallbackInGuestSession = 4,
  77. kPeripheralBlocked = 5,
  78. kBillboardDevice = 6,
  79. kInvalidDpCable = 7,
  80. kInvalidUSB4ValidTBTCable = 8,
  81. kInvalidUSB4Cable = 9,
  82. kInvalidTBTCable = 10,
  83. kSpeedLimitingCable = 11,
  84. kMaxValue = kSpeedLimitingCable,
  85. };
  86. // Sets the global instance. Must be called before any calls to Get().
  87. static void Initialize(bool is_guest_profile, bool is_pcie_tunneling_allowed);
  88. // Destroys the global instance.
  89. static void Shutdown();
  90. // Gets the global instance pointer.
  91. static PeripheralNotificationManager* Get();
  92. // Returns true if the global instance is initialized.
  93. static bool IsInitialized();
  94. void SetPcieTunnelingAllowedState(bool is_pcie_tunneling_allowed);
  95. void AddObserver(Observer* observer);
  96. void RemoveObserver(Observer* observer);
  97. void OnDeviceConnected(device::mojom::UsbDeviceInfo* device);
  98. private:
  99. friend class PeripheralNotificationManagerTest;
  100. PeripheralNotificationManager(bool is_guest_profile,
  101. bool is_pcie_tunneling_allowed);
  102. PeripheralNotificationManager(const PeripheralNotificationManager&) = delete;
  103. PeripheralNotificationManager& operator=(
  104. const PeripheralNotificationManager&) = delete;
  105. ~PeripheralNotificationManager() override;
  106. // TypecdClient::Observer:
  107. void OnThunderboltDeviceConnected(bool is_thunderbolt_only) override;
  108. void OnCableWarning(typecd::CableWarningType cable_warning_type) override;
  109. // PciguardClient::Observer:
  110. void OnBlockedThunderboltDeviceConnected(
  111. const std::string& device_name) override;
  112. // Call to notify observers that a new notification is needed.
  113. void NotifyLimitedPerformancePeripheralReceived();
  114. void NotifyGuestModeNotificationReceived(bool is_thunderbolt_only);
  115. void NotifyPeripheralBlockedReceived();
  116. void OnBillboardDeviceConnected(bool billboard_is_supported);
  117. void NotifyInvalidDpCable();
  118. void NotifyInvalidUSB4ValidTBTCableWarning();
  119. void NotifyInvalidUSB4CableWarning();
  120. void NotifyInvalidTBTCableWarning();
  121. void NotifySpeedLimitingCableWarning();
  122. // Called by unit tests to set up root_prefix_ for simulating the existence
  123. // of a system folder.
  124. void SetRootPrefixForTesting(const std::string& prefix);
  125. const bool is_guest_profile_;
  126. // Pcie tunneling refers to allowing Thunderbolt/USB4 peripherals to run at
  127. // full capacity by utilizing the PciExpress protocol. If this is set to
  128. // false, we anticipate that the plugged in Thunderbolt/USB4 periphal is
  129. // operating at either Alt-mode (i.e. fallback to an older protocol) or
  130. // in a restricted state (e.g. certain devices are Thunderbolt only).
  131. bool is_pcie_tunneling_allowed_;
  132. base::ObserverList<Observer> observer_list_;
  133. std::string root_prefix_ = "";
  134. // Used for callbacks.
  135. base::WeakPtrFactory<PeripheralNotificationManager> weak_ptr_factory_{this};
  136. };
  137. } // namespace ash
  138. #endif // ASH_COMPONENTS_PERIPHERAL_NOTIFICATION_PERIPHERAL_NOTIFICATION_MANAGER_H_