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() { ...@@ -74,10 +74,8 @@ cr.define('settings', function() {
*/ */
getCrostiniSharedPathsDisplayText(paths) {} getCrostiniSharedPathsDisplayText(paths) {}
/** /** Called when page is ready. */
* @return {!Promise<!Array<CrostiniSharedUsbDevice>>} notifyCrostiniSharedUsbDevicesPageReady() {}
*/
getCrostiniSharedUsbDevices() {}
/** /**
* @param {string} guid Unique device identifier. * @param {string} guid Unique device identifier.
...@@ -268,8 +266,8 @@ cr.define('settings', function() { ...@@ -268,8 +266,8 @@ cr.define('settings', function() {
} }
/** @override */ /** @override */
getCrostiniSharedUsbDevices() { notifyCrostiniSharedUsbDevicesPageReady() {
return cr.sendWithPromise('getCrostiniSharedUsbDevices'); return cr.sendWithPromise('notifyCrostiniSharedUsbDevicesPageReady');
} }
/** @override */ /** @override */
......
...@@ -21,14 +21,16 @@ Polymer({ ...@@ -21,14 +21,16 @@ Polymer({
sharedUsbDevices_: Array, sharedUsbDevices_: Array,
}, },
/** @private {settings.CrostiniBrowserProxy} */
browserProxy_: null,
/** @override */ /** @override */
attached() { ready() {
this.browserProxy_ = settings.CrostiniBrowserProxyImpl.getInstance();
this.addWebUIListener( this.addWebUIListener(
'crostini-shared-usb-devices-changed', 'crostini-shared-usb-devices-changed',
this.onCrostiniSharedUsbDevicesChanged_.bind(this)); this.onCrostiniSharedUsbDevicesChanged_.bind(this));
settings.CrostiniBrowserProxyImpl.getInstance() this.browserProxy_.notifyCrostiniSharedUsbDevicesPageReady();
.getCrostiniSharedUsbDevices()
.then(this.onCrostiniSharedUsbDevicesChanged_.bind(this));
}, },
/** /**
...@@ -45,7 +47,7 @@ Polymer({ ...@@ -45,7 +47,7 @@ Polymer({
*/ */
onDeviceSharedChange_(event) { onDeviceSharedChange_(event) {
const deviceInfo = event.model.item; const deviceInfo = event.model.item;
settings.CrostiniBrowserProxyImpl.getInstance().setCrostiniUsbDeviceShared( this.browserProxy_.setCrostiniUsbDeviceShared(
deviceInfo.guid, event.target.checked); deviceInfo.guid, event.target.checked);
settings.recordSettingChange(); settings.recordSettingChange();
}, },
......
...@@ -74,9 +74,10 @@ void CrostiniHandler::RegisterMessages() { ...@@ -74,9 +74,10 @@ void CrostiniHandler::RegisterMessages() {
base::BindRepeating(&CrostiniHandler::HandleRemoveCrostiniSharedPath, base::BindRepeating(&CrostiniHandler::HandleRemoveCrostiniSharedPath,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"getCrostiniSharedUsbDevices", "notifyCrostiniSharedUsbDevicesPageReady",
base::BindRepeating(&CrostiniHandler::HandleGetCrostiniSharedUsbDevices, base::BindRepeating(
weak_ptr_factory_.GetWeakPtr())); &CrostiniHandler::HandleNotifyCrostiniSharedUsbDevicesPageReady,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"setCrostiniUsbDeviceShared", "setCrostiniUsbDeviceShared",
base::BindRepeating(&CrostiniHandler::HandleSetCrostiniUsbDeviceShared, base::BindRepeating(&CrostiniHandler::HandleSetCrostiniUsbDeviceShared,
...@@ -325,22 +326,10 @@ base::Value CrostiniDiskInfoToValue( ...@@ -325,22 +326,10 @@ base::Value CrostiniDiskInfoToValue(
} }
} // namespace } // namespace
void CrostiniHandler::HandleGetCrostiniSharedUsbDevices( void CrostiniHandler::HandleNotifyCrostiniSharedUsbDevicesPageReady(
const base::ListValue* args) { const base::ListValue* args) {
AllowJavascript(); AllowJavascript();
CHECK_EQ(1U, args->GetList().size()); OnUsbDevicesChanged();
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()));
} }
void CrostiniHandler::HandleSetCrostiniUsbDeviceShared( void CrostiniHandler::HandleSetCrostiniUsbDeviceShared(
......
...@@ -58,8 +58,9 @@ class CrostiniHandler : public ::settings::SettingsPageUIHandler, ...@@ -58,8 +58,9 @@ class CrostiniHandler : public ::settings::SettingsPageUIHandler,
const std::string& path, const std::string& path,
bool result, bool result,
const std::string& failure_reason); const std::string& failure_reason);
// Returns a list of available USB devices. // Called when the shared USB devices page is ready.
void HandleGetCrostiniSharedUsbDevices(const base::ListValue* args); void HandleNotifyCrostiniSharedUsbDevicesPageReady(
const base::ListValue* args);
// Set the share state of a USB device. // Set the share state of a USB device.
void HandleSetCrostiniUsbDeviceShared(const base::ListValue* args); void HandleSetCrostiniUsbDeviceShared(const base::ListValue* args);
// chromeos::SharedUsbDeviceObserver. // chromeos::SharedUsbDeviceObserver.
......
...@@ -9,7 +9,7 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy { ...@@ -9,7 +9,7 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy {
'requestCrostiniInstallerView', 'requestCrostiniInstallerView',
'requestRemoveCrostini', 'requestRemoveCrostini',
'getCrostiniSharedPathsDisplayText', 'getCrostiniSharedPathsDisplayText',
'getCrostiniSharedUsbDevices', 'notifyCrostiniSharedUsbDevicesPageReady',
'setCrostiniUsbDeviceShared', 'setCrostiniUsbDeviceShared',
'removeCrostiniSharedPath', 'removeCrostiniSharedPath',
'exportCrostiniContainer', 'exportCrostiniContainer',
...@@ -84,9 +84,10 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy { ...@@ -84,9 +84,10 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy {
} }
/** @override */ /** @override */
getCrostiniSharedUsbDevices() { notifyCrostiniSharedUsbDevicesPageReady() {
this.methodCalled('getCrostiniSharedUsbDevices'); this.methodCalled('notifyCrostiniSharedUsbDevicesPageReady');
return Promise.resolve(this.sharedUsbDevices); cr.webUIListenerCallback(
'crostini-shared-usb-devices-changed', this.sharedUsbDevices);
} }
/** @override */ /** @override */
...@@ -227,4 +228,4 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy { ...@@ -227,4 +228,4 @@ class TestCrostiniBrowserProxy extends TestBrowserProxy {
this.methodCalled('getCrostiniMicSharingEnabled'); this.methodCalled('getCrostiniMicSharingEnabled');
return Promise.resolve(this.CrostiniMicSharingEnabled); return Promise.resolve(this.CrostiniMicSharingEnabled);
} }
} }
\ No newline at end of file
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