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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// 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.
#include "chromeos/ash/components/peripheral_notification/peripheral_notification_manager.h"
#include "ash/constants/ash_features.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "third_party/cros_system_api/dbus/typecd/dbus-constants.h"
namespace ash {
namespace {
PeripheralNotificationManager* g_instance = nullptr;
const int kBillboardDeviceClassCode = 17;
constexpr char thunderbolt_file_path[] = "/sys/bus/thunderbolt/devices/0-0";
void RecordConnectivityMetric(
PeripheralNotificationManager::PeripheralConnectivityResults results) {
base::UmaHistogramEnumeration("Ash.Peripheral.ConnectivityResults", results);
}
// Checks if the board supports Thunderbolt.
bool CheckIfThunderboltFilepathExists(std::string root_prefix) {
return base::PathExists(base::FilePath(root_prefix + thunderbolt_file_path));
}
} // namespace
PeripheralNotificationManager::PeripheralNotificationManager(
bool is_guest_profile,
bool is_pcie_tunneling_allowed)
: is_guest_profile_(is_guest_profile),
is_pcie_tunneling_allowed_(is_pcie_tunneling_allowed) {
DCHECK(TypecdClient::Get());
DCHECK(PciguardClient::Get());
TypecdClient::Get()->AddObserver(this);
PciguardClient::Get()->AddObserver(this);
}
PeripheralNotificationManager::~PeripheralNotificationManager() {
TypecdClient::Get()->RemoveObserver(this);
PciguardClient::Get()->RemoveObserver(this);
CHECK_EQ(this, g_instance);
g_instance = nullptr;
}
void PeripheralNotificationManager::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void PeripheralNotificationManager::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void PeripheralNotificationManager::
NotifyLimitedPerformancePeripheralReceived() {
// Show no notifications if PCIe tunneling is allowed.
if (is_pcie_tunneling_allowed_)
return;
for (auto& observer : observer_list_)
observer.OnLimitedPerformancePeripheralReceived();
}
void PeripheralNotificationManager::NotifyGuestModeNotificationReceived(
bool is_thunderbolt_only) {
for (auto& observer : observer_list_)
observer.OnGuestModeNotificationReceived(is_thunderbolt_only);
}
void PeripheralNotificationManager::NotifyPeripheralBlockedReceived() {
for (auto& observer : observer_list_)
observer.OnPeripheralBlockedReceived();
}
void PeripheralNotificationManager::OnBillboardDeviceConnected(
bool billboard_is_supported) {
if (!features::IsPcieBillboardNotificationEnabled()) {
return;
}
if (!billboard_is_supported) {
for (auto& observer : observer_list_)
observer.OnBillboardDeviceConnected();
RecordConnectivityMetric(PeripheralConnectivityResults::kBillboardDevice);
}
}
void PeripheralNotificationManager::OnThunderboltDeviceConnected(
bool is_thunderbolt_only) {
if (is_guest_profile_) {
NotifyGuestModeNotificationReceived(is_thunderbolt_only);
RecordConnectivityMetric(
is_thunderbolt_only
? PeripheralConnectivityResults::kTBTOnlyAndBlockedInGuestSession
: PeripheralConnectivityResults::kAltModeFallbackInGuestSession);
return;
}
// Only show notifications if the peripheral is operating at limited
// performance.
if (!is_pcie_tunneling_allowed_) {
NotifyLimitedPerformancePeripheralReceived();
RecordConnectivityMetric(
is_thunderbolt_only
? PeripheralConnectivityResults::kTBTOnlyAndBlockedByPciguard
: PeripheralConnectivityResults::kAltModeFallbackDueToPciguard);
return;
}
RecordConnectivityMetric(
PeripheralConnectivityResults::kTBTSupportedAndAllowed);
}
void PeripheralNotificationManager::OnBlockedThunderboltDeviceConnected(
const std::string& name) {
// Currently the device name is not shown in the notification.
NotifyPeripheralBlockedReceived();
RecordConnectivityMetric(PeripheralConnectivityResults::kPeripheralBlocked);
}
void PeripheralNotificationManager::OnCableWarning(
typecd::CableWarningType cable_warning_type) {
// Decode cable warnging signal.
switch (cable_warning_type) {
case typecd::CableWarningType::kInvalidDpCable:
NotifyInvalidDpCable();
RecordConnectivityMetric(PeripheralConnectivityResults::kInvalidDpCable);
break;
case typecd::CableWarningType::kInvalidUSB4ValidTBTCable:
NotifyInvalidUSB4ValidTBTCableWarning();
RecordConnectivityMetric(
PeripheralConnectivityResults::kInvalidUSB4ValidTBTCable);
break;
case typecd::CableWarningType::kInvalidUSB4Cable:
NotifyInvalidUSB4CableWarning();
RecordConnectivityMetric(
PeripheralConnectivityResults::kInvalidUSB4Cable);
break;
case typecd::CableWarningType::kInvalidTBTCable:
NotifyInvalidTBTCableWarning();
RecordConnectivityMetric(PeripheralConnectivityResults::kInvalidTBTCable);
break;
case typecd::CableWarningType::kSpeedLimitingCable:
NotifySpeedLimitingCableWarning();
RecordConnectivityMetric(
PeripheralConnectivityResults::kSpeedLimitingCable);
break;
default:
break;
}
}
void PeripheralNotificationManager::NotifyInvalidDpCable() {
for (auto& observer : observer_list_)
observer.OnInvalidDpCableWarning();
}
void PeripheralNotificationManager::NotifyInvalidUSB4ValidTBTCableWarning() {
for (auto& observer : observer_list_)
observer.OnInvalidUSB4ValidTBTCableWarning();
}
void PeripheralNotificationManager::NotifyInvalidUSB4CableWarning() {
for (auto& observer : observer_list_)
observer.OnInvalidUSB4CableWarning();
}
void PeripheralNotificationManager::NotifyInvalidTBTCableWarning() {
for (auto& observer : observer_list_)
observer.OnInvalidTBTCableWarning();
}
void PeripheralNotificationManager::NotifySpeedLimitingCableWarning() {
for (auto& observer : observer_list_)
observer.OnSpeedLimitingCableWarning();
}
void PeripheralNotificationManager::OnUsbLimit(
typecd::UsbLimitType usb_limit_type) {
if (usb_limit_type == typecd::UsbLimitType::kDeviceLimit ||
usb_limit_type == typecd::UsbLimitType::kEndpointLimit) {
NotifyUsbDeviceOrEndpointLimit();
RecordConnectivityMetric(
PeripheralConnectivityResults::kUsbDeviceOrEndpointLimit);
}
}
void PeripheralNotificationManager::NotifyUsbDeviceOrEndpointLimit() {
for (auto& observer : observer_list_) {
observer.OnUsbDeviceOrEndpointLimit();
}
}
void PeripheralNotificationManager::OnDeviceConnected(
device::mojom::UsbDeviceInfo* device) {
if (device->class_code == kBillboardDeviceClassCode) {
// PathExist is a blocking call. PostTask it and wait on the result.
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&CheckIfThunderboltFilepathExists, root_prefix_),
base::BindOnce(
&PeripheralNotificationManager::OnBillboardDeviceConnected,
weak_ptr_factory_.GetWeakPtr()));
}
}
void PeripheralNotificationManager::SetPcieTunnelingAllowedState(
bool is_pcie_tunneling_allowed) {
is_pcie_tunneling_allowed_ = is_pcie_tunneling_allowed;
}
void PeripheralNotificationManager::SetRootPrefixForTesting(
const std::string& prefix) {
root_prefix_ = prefix;
}
// static
void PeripheralNotificationManager::Initialize(bool is_guest_profile,
bool is_pcie_tunneling_allowed) {
CHECK(!g_instance);
g_instance = new PeripheralNotificationManager(is_guest_profile,
is_pcie_tunneling_allowed);
}
// static
void PeripheralNotificationManager::Shutdown() {
CHECK(g_instance);
delete g_instance;
g_instance = NULL;
}
// static
PeripheralNotificationManager* PeripheralNotificationManager::Get() {
CHECK(g_instance);
return g_instance;
}
// static
bool PeripheralNotificationManager::IsInitialized() {
return g_instance;
}
} // namespace ash
|