Commit 566118a0 authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

Refactor Crostini USB page to replace getter with ready event

Replace getCrostiniSharedUsbDevices with event sent when page is ready.
This matches the Plugin VM USB page.

Bug: 1129266
Change-Id: I97ee3e60129a59c3f616552d6ffa27f7be443767
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2415708Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarRegan Hsu <hsuregan@chromium.org>
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808175}
parent 6ad8400f
......@@ -74,10 +74,8 @@ cr.define('settings', function() {
*/
getCrostiniSharedPathsDisplayText(paths) {}
/**
* @return {!Promise<!Array<CrostiniSharedUsbDevice>>}
*/
getCrostiniSharedUsbDevices() {}
/** Called when page is ready. */
notifyCrostiniSharedUsbDevicesPageReady() {}
/**
* @param {string} guid Unique device identifier.
......@@ -268,8 +266,8 @@ cr.define('settings', function() {
}
/** @override */
getCrostiniSharedUsbDevices() {
return cr.sendWithPromise('getCrostiniSharedUsbDevices');
notifyCrostiniSharedUsbDevicesPageReady() {
return cr.sendWithPromise('notifyCrostiniSharedUsbDevicesPageReady');
}
/** @override */
......
......@@ -21,14 +21,16 @@ Polymer({
sharedUsbDevices_: Array,
},
/** @private {settings.CrostiniBrowserProxy} */
browserProxy_: null,
/** @override */
attached() {
ready() {
this.browserProxy_ = settings.CrostiniBrowserProxyImpl.getInstance();
this.addWebUIListener(
'crostini-shared-usb-devices-changed',
this.onCrostiniSharedUsbDevicesChanged_.bind(this));
settings.CrostiniBrowserProxyImpl.getInstance()
.getCrostiniSharedUsbDevices()
.then(this.onCrostiniSharedUsbDevicesChanged_.bind(this));
this.browserProxy_.notifyCrostiniSharedUsbDevicesPageReady();
},
/**
......@@ -45,7 +47,7 @@ Polymer({
*/
onDeviceSharedChange_(event) {
const deviceInfo = event.model.item;
settings.CrostiniBrowserProxyImpl.getInstance().setCrostiniUsbDeviceShared(
this.browserProxy_.setCrostiniUsbDeviceShared(
deviceInfo.guid, event.target.checked);
settings.recordSettingChange();
},
......
......@@ -74,8 +74,9 @@ void CrostiniHandler::RegisterMessages() {
base::BindRepeating(&CrostiniHandler::HandleRemoveCrostiniSharedPath,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"getCrostiniSharedUsbDevices",
base::BindRepeating(&CrostiniHandler::HandleGetCrostiniSharedUsbDevices,
"notifyCrostiniSharedUsbDevicesPageReady",
base::BindRepeating(
&CrostiniHandler::HandleNotifyCrostiniSharedUsbDevicesPageReady,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"setCrostiniUsbDeviceShared",
......@@ -325,22 +326,10 @@ base::Value CrostiniDiskInfoToValue(
}
} // namespace
void CrostiniHandler::HandleGetCrostiniSharedUsbDevices(
void CrostiniHandler::HandleNotifyCrostiniSharedUsbDevicesPageReady(
const base::ListValue* args) {
AllowJavascript();
CHECK_EQ(1U, args->GetList().size());
std::string callback_id = args->GetList()[0].GetString();
chromeos::CrosUsbDetector* detector = chromeos::CrosUsbDetector::Get();
if (!detector) {
ResolveJavascriptCallback(base::Value(callback_id), base::ListValue());
return;
}
ResolveJavascriptCallback(
base::Value(callback_id),
UsbDevicesToListValue(detector->GetDevicesSharableWithCrostini()));
OnUsbDevicesChanged();
}
void CrostiniHandler::HandleSetCrostiniUsbDeviceShared(
......
......@@ -58,8 +58,9 @@ class CrostiniHandler : public ::settings::SettingsPageUIHandler,
const std::string& path,
bool result,
const std::string& failure_reason);
// Returns a list of available USB devices.
void HandleGetCrostiniSharedUsbDevices(const base::ListValue* args);
// Called when the shared USB devices page is ready.
void HandleNotifyCrostiniSharedUsbDevicesPageReady(
const base::ListValue* args);
// Set the share state of a USB device.
void HandleSetCrostiniUsbDeviceShared(const base::ListValue* args);
// chromeos::SharedUsbDeviceObserver.
......
......@@ -9,7 +9,7 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy {
'requestCrostiniInstallerView',
'requestRemoveCrostini',
'getCrostiniSharedPathsDisplayText',
'getCrostiniSharedUsbDevices',
'notifyCrostiniSharedUsbDevicesPageReady',
'setCrostiniUsbDeviceShared',
'removeCrostiniSharedPath',
'exportCrostiniContainer',
......@@ -84,9 +84,10 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy {
}
/** @override */
getCrostiniSharedUsbDevices() {
this.methodCalled('getCrostiniSharedUsbDevices');
return Promise.resolve(this.sharedUsbDevices);
notifyCrostiniSharedUsbDevicesPageReady() {
this.methodCalled('notifyCrostiniSharedUsbDevicesPageReady');
cr.webUIListenerCallback(
'crostini-shared-usb-devices-changed', this.sharedUsbDevices);
}
/** @override */
......
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