Commit f064d888 authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

USB notification message and enable checks for multi VMs

Check each of crostini and plugin VM before adding buttons on the
notification, and update messages.

Bug: b/150341671
Change-Id: Id9973c27c9355b85bfe5d168ca8828500a7d5156
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2389303
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Reviewed-by: default avatarNicholas Verne <nverne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804484}
parent 78c15874
......@@ -4542,6 +4542,9 @@
Bluetooth
</message>
<message name="IDS_CROSTINI_LINUX" desc="Name shown for crostini Linux. Do not translate.">
Linux
</message>
<message name="IDS_CROSTINI_TERMINAL_APP_NAME" desc="Name of the Terminal app, which provides access to a Linux virtual machine.">
Terminal
</message>
......
0bf4088ed21c76c7dbd7776f91b879e5f74ebc47
\ No newline at end of file
......@@ -9886,13 +9886,16 @@ Please help our engineers fix this problem. Tell us what happened right before y
<if expr="chromeos">
<!-- CrOSUsb Notification -->
<message name="IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION" desc="Content for notification shown to the user when a USB device gets plugged in.">
Open Settings to connect <ph name="USB_DEVICE_NAME">$1<ex>Nexus 5</ex></ph> to Linux
Open Settings to connect <ph name="USB_DEVICE_NAME">$1<ex>Nexus 5</ex></ph> to <ph name="USB_VM_NAME">$2<ex>Linux</ex></ph>
</message>
<message name="IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION_MULTI_VM" desc="Content for notification shown to the user when a USB device gets plugged in.">
Open Settings to connect <ph name="USB_DEVICE_NAME">$1<ex>Nexus 5</ex></ph>
</message>
<message name="IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION_TITLE" desc="Title for notification shown to the user when a USB device gets plugged in.">
USB device detected
</message>
<message name="IDS_CROSUSB_NOTIFICATION_BUTTON_CONNECT_TO_LINUX" desc="Label for notification button shown to the user when a USB device gets plugged in to allowed Linux to use the device">
Connect to Linux
<message name="IDS_CROSUSB_NOTIFICATION_BUTTON_CONNECT_TO_VM" desc="Label for notification button shown to the user when a USB device gets plugged in to allow a virtual machine to use the device">
Connect to <ph name="USB_VM_NAME">$1<ex>Linux</ex></ph>
</message>
<message name="IDS_CROSUSB_UNKNOWN_DEVICE_FROM_MANUFACTURER" desc="String describing an unknown device in notification shown when a USB device gets plugged in.">
USB device from <ph name="MANUFACTURER_NAME">$1<ex>Google</ex></ph>
......
ad2cd6399c0c9e9a052d200241a0f6397ae35774
\ No newline at end of file
5c4510072ba629384962bb9d69573a3175388036
\ No newline at end of file
ad2cd6399c0c9e9a052d200241a0f6397ae35774
\ No newline at end of file
......@@ -16,6 +16,7 @@
#include "chrome/browser/chromeos/crostini/crostini_manager.h"
#include "chrome/browser/chromeos/crostini/crostini_pref_names.h"
#include "chrome/browser/chromeos/crostini/crostini_util.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_features.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
......@@ -141,20 +142,22 @@ class CrosUsbNotificationDelegate
public:
explicit CrosUsbNotificationDelegate(
const std::string& notification_id,
device::mojom::UsbDeviceInfoPtr device_info)
device::mojom::UsbDeviceInfoPtr device_info,
std::vector<std::string> vm_names,
std::string settings_sub_page)
: notification_id_(notification_id),
device_info_(std::move(device_info)),
vm_names_(std::move(vm_names)),
settings_sub_page_(std::move(settings_sub_page)),
disposition_(CrosUsbNotificationClosed::kUnknown) {}
void Click(const base::Optional<int>& button_index,
const base::Optional<base::string16>& reply) override {
disposition_ = CrosUsbNotificationClosed::kUnknown;
if (button_index && button_index.value() == 0) {
HandleConnectToVm(crostini::kCrostiniDefaultVmName);
} else if (button_index && button_index.value() == 1) {
HandleConnectToVm(plugin_vm::kPluginVmName);
if (button_index && *button_index < static_cast<int>(vm_names_.size())) {
HandleConnectToVm(vm_names_[*button_index]);
} else {
HandleShowSettings();
HandleShowSettings(settings_sub_page_);
}
}
......@@ -177,15 +180,16 @@ class CrosUsbNotificationDelegate
Close(false);
}
void HandleShowSettings() {
chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
profile(),
chromeos::settings::mojom::kCrostiniUsbPreferencesSubpagePath);
void HandleShowSettings(const std::string& sub_page) {
chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(profile(),
sub_page);
Close(false);
}
std::string notification_id_;
device::mojom::UsbDeviceInfoPtr device_info_;
std::vector<std::string> vm_names_;
std::string settings_sub_page_;
CrosUsbNotificationClosed disposition_;
base::WeakPtrFactory<CrosUsbNotificationDelegate> weak_ptr_factory_{this};
......@@ -227,28 +231,56 @@ device::mojom::UsbDeviceFilterPtr UsbFilterByClassCode(
void ShowNotificationForDevice(device::mojom::UsbDeviceInfoPtr device_info) {
message_center::RichNotificationData rich_notification_data;
std::vector<std::string> vm_names;
std::string settings_sub_page;
base::string16 vm_name;
rich_notification_data.small_image = gfx::Image(
gfx::CreateVectorIcon(vector_icons::kUsbIcon, 64, gfx::kGoogleBlue800));
rich_notification_data.accent_color = ash::kSystemNotificationColorNormal;
rich_notification_data.buttons.emplace_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_CROSUSB_NOTIFICATION_BUTTON_CONNECT_TO_LINUX)));
rich_notification_data.buttons.emplace_back(message_center::ButtonInfo(
l10n_util::GetStringUTF16(IDS_PLUGIN_VM_APP_NAME)));
if (crostini::CrostiniFeatures::Get()->IsEnabled(profile())) {
vm_name = l10n_util::GetStringUTF16(IDS_CROSTINI_LINUX);
rich_notification_data.buttons.emplace_back(
message_center::ButtonInfo(l10n_util::GetStringFUTF16(
IDS_CROSUSB_NOTIFICATION_BUTTON_CONNECT_TO_VM, vm_name)));
vm_names.emplace_back(crostini::kCrostiniDefaultVmName);
settings_sub_page =
chromeos::settings::mojom::kCrostiniUsbPreferencesSubpagePath;
}
if (plugin_vm::PluginVmFeatures::Get()->IsEnabled(profile())) {
vm_name = l10n_util::GetStringUTF16(IDS_PLUGIN_VM_APP_NAME);
rich_notification_data.buttons.emplace_back(
message_center::ButtonInfo(l10n_util::GetStringFUTF16(
IDS_CROSUSB_NOTIFICATION_BUTTON_CONNECT_TO_VM, vm_name)));
vm_names.emplace_back(plugin_vm::kPluginVmName);
settings_sub_page =
chromeos::settings::mojom::kPluginVmUsbPreferencesSubpagePath;
}
base::string16 message;
if (vm_names.size() == 1) {
message = l10n_util::GetStringFUTF16(
IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION,
ProductLabelFromDevice(device_info), vm_name);
} else {
message = l10n_util::GetStringFUTF16(
IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION_MULTI_VM,
ProductLabelFromDevice(device_info));
settings_sub_page = std::string();
}
std::string notification_id =
CrosUsbDetector::MakeNotificationId(device_info->guid);
message_center::Notification notification(
message_center::NOTIFICATION_TYPE_MULTIPLE, notification_id,
l10n_util::GetStringUTF16(IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION_TITLE),
l10n_util::GetStringFUTF16(IDS_CROSUSB_DEVICE_DETECTED_NOTIFICATION,
ProductLabelFromDevice(device_info)),
gfx::Image(), base::string16(), GURL(),
message, gfx::Image(), base::string16(), GURL(),
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierUsb),
rich_notification_data,
base::MakeRefCounted<CrosUsbNotificationDelegate>(
notification_id, std::move(device_info)));
notification_id, std::move(device_info), std::move(vm_names),
std::move(settings_sub_page)));
SystemNotificationHelper::GetInstance()->Display(notification);
}
......@@ -382,7 +414,8 @@ void CrosUsbDetector::ConnectToDeviceManager() {
bool CrosUsbDetector::ShouldShowNotification(
const device::mojom::UsbDeviceInfo& device_info,
uint32_t allowed_interfaces_mask) {
if (!crostini::CrostiniFeatures::Get()->IsEnabled(profile())) {
if (!crostini::CrostiniFeatures::Get()->IsEnabled(profile()) &&
!plugin_vm::PluginVmFeatures::Get()->IsEnabled(profile())) {
return false;
}
if (device::UsbDeviceFilterMatches(*adb_device_filter_, device_info) ||
......
......@@ -16,6 +16,9 @@
#include "chrome/browser/chromeos/crostini/crostini_manager.h"
#include "chrome/browser/chromeos/crostini/crostini_pref_names.h"
#include "chrome/browser/chromeos/crostini/crostini_test_helper.h"
#include "chrome/browser/chromeos/crostini/fake_crostini_features.h"
#include "chrome/browser/chromeos/plugin_vm/fake_plugin_vm_features.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_test_helper.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
......@@ -255,6 +258,47 @@ TEST_F(CrosUsbDetectorTest, UsbDeviceAddedAndRemoved) {
EXPECT_FALSE(display_service_->GetNotification(notification_id));
}
TEST_F(CrosUsbDetectorTest, NotificationShown) {
ConnectToDeviceManager();
base::RunLoop().RunUntilIdle();
auto device = base::MakeRefCounted<device::FakeUsbDeviceInfo>(
0, 1, kManufacturerName, kProductName_1, "002");
std::string notification_id =
chromeos::CrosUsbDetector::MakeNotificationId(device->guid());
// Notifications should not be shown if no VMs enabled.
crostini::FakeCrostiniFeatures crostini_features;
crostini_features.set_enabled(false);
device_manager_.AddDevice(device);
base::RunLoop().RunUntilIdle();
base::Optional<message_center::Notification> notification =
display_service_->GetNotification(notification_id);
EXPECT_FALSE(notification);
device_manager_.RemoveDevice(device);
base::RunLoop().RunUntilIdle();
// Notification should have 1 button when only crostini is enabled.
crostini_features.set_enabled(true);
device_manager_.AddDevice(device);
base::RunLoop().RunUntilIdle();
notification = display_service_->GetNotification(notification_id);
ASSERT_TRUE(notification);
EXPECT_EQ(notification->buttons().size(), 1u);
device_manager_.RemoveDevice(device);
base::RunLoop().RunUntilIdle();
// Should have 2 buttons when Plugin VM is enabled.
plugin_vm::FakePluginVmFeatures plugin_vm_features;
plugin_vm_features.set_enabled(true);
device_manager_.AddDevice(device);
base::RunLoop().RunUntilIdle();
notification = display_service_->GetNotification(notification_id);
ASSERT_TRUE(notification);
EXPECT_EQ(notification->buttons().size(), 2u);
}
TEST_F(CrosUsbDetectorTest, UsbNotificationClicked) {
ConnectToDeviceManager();
base::RunLoop().RunUntilIdle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment