Commit 8fca5822 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Files app: ES6 class for externs/../crostini.js, crostini.js and file_operation_manager_unittest.js

Bug: 778674
Change-Id: I2f2c30a259cd1ebddaa1c15b5c754cf165025260
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1763657Reviewed-by: default avatarJoel Hockey <joelhockey@chromium.org>
Commit-Queue: Luciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689377}
parent 0a105892
...@@ -7,80 +7,80 @@ ...@@ -7,80 +7,80 @@
* *
* @interface * @interface
*/ */
function Crostini() {} class Crostini {
/**
/**
* Initialize enabled settings. * Initialize enabled settings.
* Must be done after loadTimeData is available. * Must be done after loadTimeData is available.
*/ */
Crostini.prototype.initEnabled = function() {}; initEnabled() {}
/** /**
* Initialize Volume Manager. * Initialize Volume Manager.
* @param {!VolumeManager} volumeManager * @param {!VolumeManager} volumeManager
*/ */
Crostini.prototype.initVolumeManager = function(volumeManager) {}; initVolumeManager(volumeManager) {}
/** /**
* Register for any shared path changes. * Register for any shared path changes.
*/ */
Crostini.prototype.listen = function() {}; listen() {}
/** /**
* Set whether the specified VM is enabled. * Set whether the specified VM is enabled.
* @param {string} vmName * @param {string} vmName
* @param {boolean} enabled * @param {boolean} enabled
*/ */
Crostini.prototype.setEnabled = function(vmName, enabled) {}; setEnabled(vmName, enabled) {}
/** /**
* Returns true if the specified VM is enabled. * Returns true if the specified VM is enabled.
* @param {string} vmName * @param {string} vmName
* @return {boolean} * @return {boolean}
*/ */
Crostini.prototype.isEnabled = function(vmName) {}; isEnabled(vmName) {}
/** /**
* Set whether the specified VM allows root access. * Set whether the specified VM allows root access.
* @param {string} vmName * @param {string} vmName
* @param {boolean} allowed * @param {boolean} allowed
*/ */
Crostini.prototype.setRootAccessAllowed = function(vmName, allowed) {}; setRootAccessAllowed(vmName, allowed) {}
/** /**
* Returns true if root access to specified VM is allowed. * Returns true if root access to specified VM is allowed.
* @param {string} vmName * @param {string} vmName
* @return {boolean} * @return {boolean}
*/ */
Crostini.prototype.isRootAccessAllowed = function(vmName) {}; isRootAccessAllowed(vmName) {}
/** /**
* Registers an entry as a shared path for the specified VM. * Registers an entry as a shared path for the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
*/ */
Crostini.prototype.registerSharedPath = function(vmName, entry) {}; registerSharedPath(vmName, entry) {}
/** /**
* Unregisters entry as a shared path from the specified VM. * Unregisters entry as a shared path from the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
*/ */
Crostini.prototype.unregisterSharedPath = function(vmName, entry) {}; unregisterSharedPath(vmName, entry) {}
/** /**
* Returns true if entry is shared with the specified VM. * Returns true if entry is shared with the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
* @return {boolean} True if path is shared either by a direct * @return {boolean} True if path is shared either by a direct
* share or from one of its ancestor directories. * share or from one of its ancestor directories.
*/ */
Crostini.prototype.isPathShared = function(vmName, entry) {}; isPathShared(vmName, entry) {}
/** /**
* Returns true if entry can be shared with the specified VM. * Returns true if entry can be shared with the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
* @param {boolean} persist If path is to be persisted. * @param {boolean} persist If path is to be persisted.
*/ */
Crostini.prototype.canSharePath = function(vmName, entry, persist) {}; canSharePath(vmName, entry, persist) {}
}
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
/** /**
* Implementation of Crostini shared path state handler. * Implementation of Crostini shared path state handler.
* *
* @constructor
* @implements {Crostini} * @implements {Crostini}
*/ */
function CrostiniImpl() { class CrostiniImpl {
constructor() {
/** /**
* True if VM is enabled. * True if VM is enabled.
* @private {Object<boolean>} * @private {Object<boolean>}
...@@ -27,136 +27,93 @@ function CrostiniImpl() { ...@@ -27,136 +27,93 @@ function CrostiniImpl() {
* @private {Object<boolean>} * @private {Object<boolean>}
*/ */
this.rootAccessAllowed_ = {}; this.rootAccessAllowed_ = {};
}
/**
* Default Crostini VM is 'termina'.
* @const
*/
CrostiniImpl.DEFAULT_VM = 'termina';
/**
* Plugin VM 'PvmDefault'.
* @const
*/
CrostiniImpl.PLUGIN_VM = 'PvmDefault';
/**
* Keep in sync with histograms.xml:FileBrowserCrostiniSharedPathsDepth
* histogram_suffix.
* @type {!Map<VolumeManagerCommon.RootType, string>}
* @const
*/
CrostiniImpl.VALID_ROOT_TYPES_FOR_SHARE = new Map([
[VolumeManagerCommon.RootType.DOWNLOADS, 'Downloads'],
[VolumeManagerCommon.RootType.REMOVABLE, 'Removable'],
[VolumeManagerCommon.RootType.ANDROID_FILES, 'AndroidFiles'],
]);
/**
* Can be collapsed into VALID_ROOT_TYPES_FOR_SHARE once
* DriveFS flag is removed.
* Keep in sync with histograms.xml:FileBrowserCrostiniSharedPathsDepth
* histogram_suffix.
* @type {!Map<VolumeManagerCommon.RootType, string>}
* @const
*/
CrostiniImpl.VALID_DRIVE_FS_ROOT_TYPES_FOR_SHARE = new Map([
[VolumeManagerCommon.RootType.COMPUTERS_GRAND_ROOT, 'DriveComputers'],
[VolumeManagerCommon.RootType.COMPUTER, 'DriveComputers'],
[VolumeManagerCommon.RootType.DRIVE, 'MyDrive'],
[VolumeManagerCommon.RootType.SHARED_DRIVES_GRAND_ROOT, 'TeamDrive'],
[VolumeManagerCommon.RootType.SHARED_DRIVE, 'TeamDrive'],
]);
/** /** @private {?VolumeManager} */
* @private {string} this.volumeManager_ = null;
* @const }
*/
CrostiniImpl.UMA_ROOT_TYPE_OTHER = 'Other';
/** /**
* Initialize enabled settings. * Initialize enabled settings.
* Must be done after loadTimeData is available. * Must be done after loadTimeData is available.
*/ */
CrostiniImpl.prototype.initEnabled = function() { initEnabled() {
this.enabled_[CrostiniImpl.DEFAULT_VM] = this.enabled_[CrostiniImpl.DEFAULT_VM] =
loadTimeData.getBoolean('CROSTINI_ENABLED'); loadTimeData.getBoolean('CROSTINI_ENABLED');
this.enabled_[CrostiniImpl.PLUGIN_VM] = this.enabled_[CrostiniImpl.PLUGIN_VM] =
loadTimeData.getBoolean('PLUGIN_VM_ENABLED'); loadTimeData.getBoolean('PLUGIN_VM_ENABLED');
this.rootAccessAllowed_[CrostiniImpl.DEFAULT_VM] = this.rootAccessAllowed_[CrostiniImpl.DEFAULT_VM] =
loadTimeData.getBoolean('CROSTINI_ROOT_ACCESS_ALLOWED'); loadTimeData.getBoolean('CROSTINI_ROOT_ACCESS_ALLOWED');
}; }
/** /**
* Initialize Volume Manager. * Initialize Volume Manager.
* @param {!VolumeManager} volumeManager * @param {!VolumeManager} volumeManager
*/ */
CrostiniImpl.prototype.initVolumeManager = function(volumeManager) { initVolumeManager(volumeManager) {
this.volumeManager_ = volumeManager; this.volumeManager_ = volumeManager;
}; }
/** /**
* Register for any shared path changes. * Register for any shared path changes.
*/ */
CrostiniImpl.prototype.listen = function() { listen() {
chrome.fileManagerPrivate.onCrostiniChanged.addListener( chrome.fileManagerPrivate.onCrostiniChanged.addListener(
this.onCrostiniChanged_.bind(this)); this.onCrostiniChanged_.bind(this));
}; }
/** /**
* Set whether the specified VM is enabled. * Set whether the specified VM is enabled.
* @param {string} vmName * @param {string} vmName
* @param {boolean} enabled * @param {boolean} enabled
*/ */
CrostiniImpl.prototype.setEnabled = function(vmName, enabled) { setEnabled(vmName, enabled) {
this.enabled_[vmName] = enabled; this.enabled_[vmName] = enabled;
}; }
/** /**
* Returns true if crostini is enabled. * Returns true if crostini is enabled.
* @param {string} vmName * @param {string} vmName
* @return {boolean} * @return {boolean}
*/ */
CrostiniImpl.prototype.isEnabled = function(vmName) { isEnabled(vmName) {
return this.enabled_[vmName]; return this.enabled_[vmName];
}; }
/** /**
* Set whether the specified VM allows root access. * Set whether the specified VM allows root access.
* @param {string} vmName * @param {string} vmName
* @param {boolean} allowed * @param {boolean} allowed
*/ */
CrostiniImpl.prototype.setRootAccessAllowed = function(vmName, allowed) { setRootAccessAllowed(vmName, allowed) {
this.rootAccessAllowed_[vmName] = allowed; this.rootAccessAllowed_[vmName] = allowed;
}; }
/** /**
* Returns true if root access to specified VM is allowed. * Returns true if root access to specified VM is allowed.
* @param {string} vmName * @param {string} vmName
* @return {boolean} * @return {boolean}
*/ */
CrostiniImpl.prototype.isRootAccessAllowed = function(vmName) { isRootAccessAllowed(vmName) {
return this.rootAccessAllowed_[vmName]; return this.rootAccessAllowed_[vmName];
}; }
/** /**
* @param {!Entry} entry * @param {!Entry} entry
* @return {VolumeManagerCommon.RootType} * @return {?VolumeManagerCommon.RootType}
* @private * @private
*/ */
CrostiniImpl.prototype.getRoot_ = function(entry) { getRoot_(entry) {
const info = const info =
this.volumeManager_ && this.volumeManager_.getLocationInfo(entry); this.volumeManager_ && this.volumeManager_.getLocationInfo(entry);
return info && info.rootType; return info && info.rootType;
}; }
/** /**
* Registers an entry as a shared path for the specified VM. * Registers an entry as a shared path for the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
*/ */
CrostiniImpl.prototype.registerSharedPath = function(vmName, entry) { registerSharedPath(vmName, entry) {
const url = entry.toURL(); const url = entry.toURL();
// Remove any existing paths that are children of the new path. // Remove any existing paths that are children of the new path.
// These paths will still be shared as a result of a parent path being // These paths will still be shared as a result of a parent path being
...@@ -182,15 +139,15 @@ CrostiniImpl.prototype.registerSharedPath = function(vmName, entry) { ...@@ -182,15 +139,15 @@ CrostiniImpl.prototype.registerSharedPath = function(vmName, entry) {
metrics.recordSmallCount( metrics.recordSmallCount(
'CrostiniSharedPaths.Depth.' + suffix, 'CrostiniSharedPaths.Depth.' + suffix,
entry.fullPath.split('/').length - 1); entry.fullPath.split('/').length - 1);
}; }
/** /**
* Unregisters path as a shared path from the specified VM. * Unregisters path as a shared path from the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {string} path * @param {string} path
* @private * @private
*/ */
CrostiniImpl.prototype.unregisterSharedPath_ = function(vmName, path) { unregisterSharedPath_(vmName, path) {
const vms = this.shared_paths_[path]; const vms = this.shared_paths_[path];
if (vms) { if (vms) {
const newVms = vms.filter(vm => vm != vmName); const newVms = vms.filter(vm => vm != vmName);
...@@ -200,23 +157,23 @@ CrostiniImpl.prototype.unregisterSharedPath_ = function(vmName, path) { ...@@ -200,23 +157,23 @@ CrostiniImpl.prototype.unregisterSharedPath_ = function(vmName, path) {
delete this.shared_paths_[path]; delete this.shared_paths_[path];
} }
} }
}; }
/** /**
* Unregisters entry as a shared path from the specified VM. * Unregisters entry as a shared path from the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
*/ */
CrostiniImpl.prototype.unregisterSharedPath = function(vmName, entry) { unregisterSharedPath(vmName, entry) {
this.unregisterSharedPath_(vmName, entry.toURL()); this.unregisterSharedPath_(vmName, entry.toURL());
}; }
/** /**
* Handles events for enable/disable, share/unshare. * Handles events for enable/disable, share/unshare.
* @param {chrome.fileManagerPrivate.CrostiniEvent} event * @param {chrome.fileManagerPrivate.CrostiniEvent} event
* @private * @private
*/ */
CrostiniImpl.prototype.onCrostiniChanged_ = function(event) { onCrostiniChanged_(event) {
switch (event.eventType) { switch (event.eventType) {
case chrome.fileManagerPrivate.CrostiniEventType.ENABLE: case chrome.fileManagerPrivate.CrostiniEventType.ENABLE:
this.setEnabled(event.vmName, true); this.setEnabled(event.vmName, true);
...@@ -241,16 +198,16 @@ CrostiniImpl.prototype.onCrostiniChanged_ = function(event) { ...@@ -241,16 +198,16 @@ CrostiniImpl.prototype.onCrostiniChanged_ = function(event) {
} }
break; break;
} }
}; }
/** /**
* Returns true if entry is shared with the specified VM. * Returns true if entry is shared with the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
* @return {boolean} True if path is shared either by a direct * @return {boolean} True if path is shared either by a direct
* share or from one of its ancestor directories. * share or from one of its ancestor directories.
*/ */
CrostiniImpl.prototype.isPathShared = function(vmName, entry) { isPathShared(vmName, entry) {
// Check path and all ancestor directories. // Check path and all ancestor directories.
let path = entry.toURL(); let path = entry.toURL();
let root = path; let root = path;
...@@ -267,15 +224,15 @@ CrostiniImpl.prototype.isPathShared = function(vmName, entry) { ...@@ -267,15 +224,15 @@ CrostiniImpl.prototype.isPathShared = function(vmName, entry) {
} }
const rootVms = this.shared_paths_[root]; const rootVms = this.shared_paths_[root];
return !!rootVms && rootVms.includes(vmName); return !!rootVms && rootVms.includes(vmName);
}; }
/** /**
* Returns true if entry can be shared with the specified VM. * Returns true if entry can be shared with the specified VM.
* @param {string} vmName * @param {string} vmName
* @param {!Entry} entry * @param {!Entry} entry
* @param {boolean} persist If path is to be persisted. * @param {boolean} persist If path is to be persisted.
*/ */
CrostiniImpl.prototype.canSharePath = function(vmName, entry, persist) { canSharePath(vmName, entry, persist) {
if (!this.enabled_[vmName]) { if (!this.enabled_[vmName]) {
return false; return false;
} }
...@@ -313,4 +270,51 @@ CrostiniImpl.prototype.canSharePath = function(vmName, entry, persist) { ...@@ -313,4 +270,51 @@ CrostiniImpl.prototype.canSharePath = function(vmName, entry, persist) {
return CrostiniImpl.VALID_ROOT_TYPES_FOR_SHARE.has(root) || return CrostiniImpl.VALID_ROOT_TYPES_FOR_SHARE.has(root) ||
(loadTimeData.getBoolean('DRIVE_FS_ENABLED') && (loadTimeData.getBoolean('DRIVE_FS_ENABLED') &&
CrostiniImpl.VALID_DRIVE_FS_ROOT_TYPES_FOR_SHARE.has(root)); CrostiniImpl.VALID_DRIVE_FS_ROOT_TYPES_FOR_SHARE.has(root));
}; }
}
/**
* Default Crostini VM is 'termina'.
* @const
*/
CrostiniImpl.DEFAULT_VM = 'termina';
/**
* Plugin VM 'PvmDefault'.
* @const
*/
CrostiniImpl.PLUGIN_VM = 'PvmDefault';
/**
* Keep in sync with histograms.xml:FileBrowserCrostiniSharedPathsDepth
* histogram_suffix.
* @type {!Map<?VolumeManagerCommon.RootType, string>}
* @const
*/
CrostiniImpl.VALID_ROOT_TYPES_FOR_SHARE = new Map([
[VolumeManagerCommon.RootType.DOWNLOADS, 'Downloads'],
[VolumeManagerCommon.RootType.REMOVABLE, 'Removable'],
[VolumeManagerCommon.RootType.ANDROID_FILES, 'AndroidFiles'],
]);
/**
* Can be collapsed into VALID_ROOT_TYPES_FOR_SHARE once
* DriveFS flag is removed.
* Keep in sync with histograms.xml:FileBrowserCrostiniSharedPathsDepth
* histogram_suffix.
* @type {!Map<?VolumeManagerCommon.RootType, string>}
* @const
*/
CrostiniImpl.VALID_DRIVE_FS_ROOT_TYPES_FOR_SHARE = new Map([
[VolumeManagerCommon.RootType.COMPUTERS_GRAND_ROOT, 'DriveComputers'],
[VolumeManagerCommon.RootType.COMPUTER, 'DriveComputers'],
[VolumeManagerCommon.RootType.DRIVE, 'MyDrive'],
[VolumeManagerCommon.RootType.SHARED_DRIVES_GRAND_ROOT, 'TeamDrive'],
[VolumeManagerCommon.RootType.SHARED_DRIVE, 'TeamDrive'],
]);
/**
* @private {string}
* @const
*/
CrostiniImpl.UMA_ROOT_TYPE_OTHER = 'Other';
...@@ -37,26 +37,27 @@ mockChrome.fileManagerPrivate = { ...@@ -37,26 +37,27 @@ mockChrome.fileManagerPrivate = {
/** /**
* Logs copy-progress events from a file operation manager. * Logs copy-progress events from a file operation manager.
*/
class EventLogger {
/**
* @param {!FileOperationManager} fileOperationManager The target file * @param {!FileOperationManager} fileOperationManager The target file
* operation manager. * operation manager.
* @constructor
* @struct
*/ */
function EventLogger(fileOperationManager) { constructor(fileOperationManager) {
this.events = []; this.events = [];
this.numberOfBeginEvents = 0; this.numberOfBeginEvents = 0;
this.numberOfErrorEvents = 0; this.numberOfErrorEvents = 0;
this.numberOfSuccessEvents = 0; this.numberOfSuccessEvents = 0;
fileOperationManager.addEventListener( fileOperationManager.addEventListener(
'copy-progress', this.onCopyProgress_.bind(this)); 'copy-progress', this.onCopyProgress_.bind(this));
} }
/** /**
* Log file operation manager copy-progress event details. * Log file operation manager copy-progress event details.
* @param {Event} event An event. * @param {Event} event An event.
* @private * @private
*/ */
EventLogger.prototype.onCopyProgress_ = function(event) { onCopyProgress_(event) {
event = /** @type {FileOperationProgressEvent} */ (event); event = /** @type {FileOperationProgressEvent} */ (event);
if (event.reason === 'BEGIN') { if (event.reason === 'BEGIN') {
this.events.push(event); this.events.push(event);
...@@ -70,34 +71,35 @@ EventLogger.prototype.onCopyProgress_ = function(event) { ...@@ -70,34 +71,35 @@ EventLogger.prototype.onCopyProgress_ = function(event) {
this.events.push(event); this.events.push(event);
this.numberOfSuccessEvents++; this.numberOfSuccessEvents++;
} }
}; }
}
/** /**
* Provides fake implementation of chrome.fileManagerPrivate.startCopy. * Provides fake implementation of chrome.fileManagerPrivate.startCopy.
* @param {string} blockedDestination Destination url of an entry whose request */
* should be blocked. class BlockableFakeStartCopy {
/**
* @param {string} blockedDestination Destination url of an entry whose
* request should be blocked.
* @param {!Entry} sourceEntry Source entry. Single source entry is supported. * @param {!Entry} sourceEntry Source entry. Single source entry is supported.
* @param {!Array<!MockFileSystem>} fileSystems File systems array. * @param {!Array<!MockFileSystem>} fileSystems File systems array.
* @constructor
* @struct
*/ */
function BlockableFakeStartCopy(blockedDestination, sourceEntry, fileSystems) { constructor(blockedDestination, sourceEntry, fileSystems) {
this.resolveBlockedOperationCallback = null; this.resolveBlockedOperationCallback = null;
this.blockedDestination_ = blockedDestination; this.blockedDestination_ = blockedDestination;
this.sourceEntry_ = sourceEntry; this.sourceEntry_ = sourceEntry;
this.fileSystems_ = fileSystems; this.fileSystems_ = fileSystems;
this.startCopyId_ = 0; this.startCopyId_ = 0;
} }
/** /**
* Fake implementation of startCopy function. * Fake implementation of startCopy function.
* @param {!Entry} source * @param {!Entry} source
* @param {!Entry} destination * @param {!Entry} destination
* @param {string} newName * @param {string} newName
* @param {function(number)} callback * @param {function(number)} callback
*/ */
BlockableFakeStartCopy.prototype.startCopyFunc = function( startCopyFunc(source, destination, newName, callback) {
source, destination, newName, callback) {
const makeStatus = type => { const makeStatus = type => {
return { return {
type: type, type: type,
...@@ -130,23 +132,22 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function( ...@@ -130,23 +132,22 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function(
} else { } else {
completeCopyOperation(this.startCopyId_); completeCopyOperation(this.startCopyId_);
} }
}; }
}
/** /**
* Fake volume manager. * Fake volume manager.
* @constructor
* @struct
*/ */
function FakeVolumeManager() {} class FakeVolumeManager {
/**
/**
* Returns fake volume info. * Returns fake volume info.
* @param {!Entry} entry * @param {!Entry} entry
* @return {!Object} * @return {!Object}
*/ */
FakeVolumeManager.prototype.getVolumeInfo = function(entry) { getVolumeInfo(entry) {
return {volumeId: entry.filesystem.name}; return {volumeId: entry.filesystem.name};
}; }
}
/** /**
* Returns file system of the url. * Returns file system of the url.
......
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