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 @@
*
* @interface
*/
function Crostini() {}
/**
class Crostini {
/**
* Initialize enabled settings.
* Must be done after loadTimeData is available.
*/
Crostini.prototype.initEnabled = function() {};
initEnabled() {}
/**
/**
* Initialize Volume Manager.
* @param {!VolumeManager} volumeManager
*/
Crostini.prototype.initVolumeManager = function(volumeManager) {};
initVolumeManager(volumeManager) {}
/**
/**
* Register for any shared path changes.
*/
Crostini.prototype.listen = function() {};
listen() {}
/**
/**
* Set whether the specified VM is enabled.
* @param {string} vmName
* @param {boolean} enabled
*/
Crostini.prototype.setEnabled = function(vmName, enabled) {};
setEnabled(vmName, enabled) {}
/**
/**
* Returns true if the specified VM is enabled.
* @param {string} vmName
* @return {boolean}
*/
Crostini.prototype.isEnabled = function(vmName) {};
isEnabled(vmName) {}
/**
/**
* Set whether the specified VM allows root access.
* @param {string} vmName
* @param {boolean} allowed
*/
Crostini.prototype.setRootAccessAllowed = function(vmName, allowed) {};
setRootAccessAllowed(vmName, allowed) {}
/**
/**
* Returns true if root access to specified VM is allowed.
* @param {string} vmName
* @return {boolean}
*/
Crostini.prototype.isRootAccessAllowed = function(vmName) {};
isRootAccessAllowed(vmName) {}
/**
/**
* Registers an entry as a shared path for the specified VM.
* @param {string} vmName
* @param {!Entry} entry
*/
Crostini.prototype.registerSharedPath = function(vmName, entry) {};
registerSharedPath(vmName, entry) {}
/**
/**
* Unregisters entry as a shared path from the specified VM.
* @param {string} vmName
* @param {!Entry} entry
*/
Crostini.prototype.unregisterSharedPath = function(vmName, entry) {};
unregisterSharedPath(vmName, entry) {}
/**
/**
* Returns true if entry is shared with the specified VM.
* @param {string} vmName
* @param {!Entry} entry
* @return {boolean} True if path is shared either by a direct
* 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.
* @param {string} vmName
* @param {!Entry} entry
* @param {boolean} persist If path is to be persisted.
*/
Crostini.prototype.canSharePath = function(vmName, entry, persist) {};
canSharePath(vmName, entry, persist) {}
}
......@@ -5,10 +5,10 @@
/**
* Implementation of Crostini shared path state handler.
*
* @constructor
* @implements {Crostini}
*/
function CrostiniImpl() {
class CrostiniImpl {
constructor() {
/**
* True if VM is enabled.
* @private {Object<boolean>}
......@@ -27,136 +27,93 @@ function CrostiniImpl() {
* @private {Object<boolean>}
*/
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 {string}
* @const
*/
CrostiniImpl.UMA_ROOT_TYPE_OTHER = 'Other';
/** @private {?VolumeManager} */
this.volumeManager_ = null;
}
/**
/**
* Initialize enabled settings.
* Must be done after loadTimeData is available.
*/
CrostiniImpl.prototype.initEnabled = function() {
initEnabled() {
this.enabled_[CrostiniImpl.DEFAULT_VM] =
loadTimeData.getBoolean('CROSTINI_ENABLED');
this.enabled_[CrostiniImpl.PLUGIN_VM] =
loadTimeData.getBoolean('PLUGIN_VM_ENABLED');
this.rootAccessAllowed_[CrostiniImpl.DEFAULT_VM] =
loadTimeData.getBoolean('CROSTINI_ROOT_ACCESS_ALLOWED');
};
}
/**
/**
* Initialize Volume Manager.
* @param {!VolumeManager} volumeManager
*/
CrostiniImpl.prototype.initVolumeManager = function(volumeManager) {
initVolumeManager(volumeManager) {
this.volumeManager_ = volumeManager;
};
}
/**
/**
* Register for any shared path changes.
*/
CrostiniImpl.prototype.listen = function() {
listen() {
chrome.fileManagerPrivate.onCrostiniChanged.addListener(
this.onCrostiniChanged_.bind(this));
};
}
/**
/**
* Set whether the specified VM is enabled.
* @param {string} vmName
* @param {boolean} enabled
*/
CrostiniImpl.prototype.setEnabled = function(vmName, enabled) {
setEnabled(vmName, enabled) {
this.enabled_[vmName] = enabled;
};
}
/**
/**
* Returns true if crostini is enabled.
* @param {string} vmName
* @return {boolean}
*/
CrostiniImpl.prototype.isEnabled = function(vmName) {
isEnabled(vmName) {
return this.enabled_[vmName];
};
}
/**
/**
* Set whether the specified VM allows root access.
* @param {string} vmName
* @param {boolean} allowed
*/
CrostiniImpl.prototype.setRootAccessAllowed = function(vmName, allowed) {
setRootAccessAllowed(vmName, allowed) {
this.rootAccessAllowed_[vmName] = allowed;
};
}
/**
/**
* Returns true if root access to specified VM is allowed.
* @param {string} vmName
* @return {boolean}
*/
CrostiniImpl.prototype.isRootAccessAllowed = function(vmName) {
isRootAccessAllowed(vmName) {
return this.rootAccessAllowed_[vmName];
};
}
/**
/**
* @param {!Entry} entry
* @return {VolumeManagerCommon.RootType}
* @return {?VolumeManagerCommon.RootType}
* @private
*/
CrostiniImpl.prototype.getRoot_ = function(entry) {
getRoot_(entry) {
const info =
this.volumeManager_ && this.volumeManager_.getLocationInfo(entry);
return info && info.rootType;
};
}
/**
/**
* Registers an entry as a shared path for the specified VM.
* @param {string} vmName
* @param {!Entry} entry
*/
CrostiniImpl.prototype.registerSharedPath = function(vmName, entry) {
registerSharedPath(vmName, entry) {
const url = entry.toURL();
// 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
......@@ -182,15 +139,15 @@ CrostiniImpl.prototype.registerSharedPath = function(vmName, entry) {
metrics.recordSmallCount(
'CrostiniSharedPaths.Depth.' + suffix,
entry.fullPath.split('/').length - 1);
};
}
/**
/**
* Unregisters path as a shared path from the specified VM.
* @param {string} vmName
* @param {string} path
* @private
*/
CrostiniImpl.prototype.unregisterSharedPath_ = function(vmName, path) {
unregisterSharedPath_(vmName, path) {
const vms = this.shared_paths_[path];
if (vms) {
const newVms = vms.filter(vm => vm != vmName);
......@@ -200,23 +157,23 @@ CrostiniImpl.prototype.unregisterSharedPath_ = function(vmName, path) {
delete this.shared_paths_[path];
}
}
};
}
/**
/**
* Unregisters entry as a shared path from the specified VM.
* @param {string} vmName
* @param {!Entry} entry
*/
CrostiniImpl.prototype.unregisterSharedPath = function(vmName, entry) {
unregisterSharedPath(vmName, entry) {
this.unregisterSharedPath_(vmName, entry.toURL());
};
}
/**
/**
* Handles events for enable/disable, share/unshare.
* @param {chrome.fileManagerPrivate.CrostiniEvent} event
* @private
*/
CrostiniImpl.prototype.onCrostiniChanged_ = function(event) {
onCrostiniChanged_(event) {
switch (event.eventType) {
case chrome.fileManagerPrivate.CrostiniEventType.ENABLE:
this.setEnabled(event.vmName, true);
......@@ -241,16 +198,16 @@ CrostiniImpl.prototype.onCrostiniChanged_ = function(event) {
}
break;
}
};
}
/**
/**
* Returns true if entry is shared with the specified VM.
* @param {string} vmName
* @param {!Entry} entry
* @return {boolean} True if path is shared either by a direct
* share or from one of its ancestor directories.
*/
CrostiniImpl.prototype.isPathShared = function(vmName, entry) {
isPathShared(vmName, entry) {
// Check path and all ancestor directories.
let path = entry.toURL();
let root = path;
......@@ -267,15 +224,15 @@ CrostiniImpl.prototype.isPathShared = function(vmName, entry) {
}
const rootVms = this.shared_paths_[root];
return !!rootVms && rootVms.includes(vmName);
};
}
/**
/**
* Returns true if entry can be shared with the specified VM.
* @param {string} vmName
* @param {!Entry} entry
* @param {boolean} persist If path is to be persisted.
*/
CrostiniImpl.prototype.canSharePath = function(vmName, entry, persist) {
canSharePath(vmName, entry, persist) {
if (!this.enabled_[vmName]) {
return false;
}
......@@ -313,4 +270,51 @@ CrostiniImpl.prototype.canSharePath = function(vmName, entry, persist) {
return CrostiniImpl.VALID_ROOT_TYPES_FOR_SHARE.has(root) ||
(loadTimeData.getBoolean('DRIVE_FS_ENABLED') &&
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 = {
/**
* Logs copy-progress events from a file operation manager.
*/
class EventLogger {
/**
* @param {!FileOperationManager} fileOperationManager The target file
* operation manager.
* @constructor
* @struct
*/
function EventLogger(fileOperationManager) {
constructor(fileOperationManager) {
this.events = [];
this.numberOfBeginEvents = 0;
this.numberOfErrorEvents = 0;
this.numberOfSuccessEvents = 0;
fileOperationManager.addEventListener(
'copy-progress', this.onCopyProgress_.bind(this));
}
}
/**
/**
* Log file operation manager copy-progress event details.
* @param {Event} event An event.
* @private
*/
EventLogger.prototype.onCopyProgress_ = function(event) {
onCopyProgress_(event) {
event = /** @type {FileOperationProgressEvent} */ (event);
if (event.reason === 'BEGIN') {
this.events.push(event);
......@@ -70,34 +71,35 @@ EventLogger.prototype.onCopyProgress_ = function(event) {
this.events.push(event);
this.numberOfSuccessEvents++;
}
};
}
}
/**
* 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 {!Array<!MockFileSystem>} fileSystems File systems array.
* @constructor
* @struct
*/
function BlockableFakeStartCopy(blockedDestination, sourceEntry, fileSystems) {
constructor(blockedDestination, sourceEntry, fileSystems) {
this.resolveBlockedOperationCallback = null;
this.blockedDestination_ = blockedDestination;
this.sourceEntry_ = sourceEntry;
this.fileSystems_ = fileSystems;
this.startCopyId_ = 0;
}
}
/**
/**
* Fake implementation of startCopy function.
* @param {!Entry} source
* @param {!Entry} destination
* @param {string} newName
* @param {function(number)} callback
*/
BlockableFakeStartCopy.prototype.startCopyFunc = function(
source, destination, newName, callback) {
startCopyFunc(source, destination, newName, callback) {
const makeStatus = type => {
return {
type: type,
......@@ -130,23 +132,22 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function(
} else {
completeCopyOperation(this.startCopyId_);
}
};
}
}
/**
* Fake volume manager.
* @constructor
* @struct
*/
function FakeVolumeManager() {}
/**
class FakeVolumeManager {
/**
* Returns fake volume info.
* @param {!Entry} entry
* @return {!Object}
*/
FakeVolumeManager.prototype.getVolumeInfo = function(entry) {
getVolumeInfo(entry) {
return {volumeId: entry.filesystem.name};
};
}
}
/**
* 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