Commit b6ecfc5b authored by François Degros's avatar François Degros Committed by Commit Bot

[Files app] Simplified FilteredVolumeManager

Removed member pendingTasks_ and the use of AsyncUtil.Queue through
the judicious use of Promises.

Fixed potential bug in whenVolumeInfoReady().

Change-Id: Ie3bf3301c3c4b8a723f0740afcebf7f65a96e44d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1627270
Auto-Submit: François Degros <fdegros@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Commit-Queue: François Degros <fdegros@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662871}
parent fc7af3fb
...@@ -82,40 +82,23 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -82,40 +82,23 @@ class FilteredVolumeManager extends cr.EventTarget {
// Public VolumeManager.volumeInfoList property accessed by callers. // Public VolumeManager.volumeInfoList property accessed by callers.
this.volumeInfoList = new FilteredVolumeInfoList(this.list_); this.volumeInfoList = new FilteredVolumeInfoList(this.list_);
/** @private {?VolumeManager} */
this.volumeManager_ = null; this.volumeManager_ = null;
/** @private {?Array<function()>} */
this.pendingTasks_ = [];
this.onEventBound_ = this.onEvent_.bind(this); this.onEventBound_ = this.onEvent_.bind(this);
this.onVolumeInfoListUpdatedBound_ = this.onVolumeInfoListUpdatedBound_ =
this.onVolumeInfoListUpdated_.bind(this); this.onVolumeInfoListUpdated_.bind(this);
this.disposed_ = false; this.disposed_ = false;
// Start initialize the VolumeManager. /** private {Window} */
const queue = new AsyncUtil.Queue(); this.backgroundPage_ = opt_backgroundPage;
if (opt_backgroundPage) { /**
this.backgroundPage_ = opt_backgroundPage; * Tracks async initialization of volume manager.
} else { * @private @const {!Promise<void> }
queue.run(callNextStep => { */
chrome.runtime.getBackgroundPage( this.initialized_ = this.initialize_();
/** @type {function(Window=)} */ (opt_backgroundPage => {
this.backgroundPage_ = opt_backgroundPage;
callNextStep();
}));
});
}
queue.run(async (callNextStep) => {
try {
const volumeManager =
await this.backgroundPage_.volumeManagerFactory.getInstance();
this.onReady_(volumeManager);
} finally {
callNextStep();
}
});
} }
/** /**
...@@ -160,18 +143,22 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -160,18 +143,22 @@ class FilteredVolumeManager extends cr.EventTarget {
} }
/** /**
* Called when the VolumeManager gets ready for post initialization. * Async part of the initialization.
* @param {VolumeManager} volumeManager The initialized VolumeManager
* instance.
* @private * @private
*/ */
onReady_(volumeManager) { async initialize_() {
if (!this.backgroundPage_) {
this.backgroundPage_ = await new Promise(
resolve => chrome.runtime.getBackgroundPage(resolve));
}
this.volumeManager_ =
await this.backgroundPage_.volumeManagerFactory.getInstance();
if (this.disposed_) { if (this.disposed_) {
return; return;
} }
this.volumeManager_ = volumeManager;
// Subscribe to VolumeManager. // Subscribe to VolumeManager.
this.volumeManager_.addEventListener( this.volumeManager_.addEventListener(
'drive-connection-changed', this.onEventBound_); 'drive-connection-changed', this.onEventBound_);
...@@ -202,13 +189,6 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -202,13 +189,6 @@ class FilteredVolumeManager extends cr.EventTarget {
// In VolumeInfoList, we only use 'splice' event. // In VolumeInfoList, we only use 'splice' event.
this.volumeManager_.volumeInfoList.addEventListener( this.volumeManager_.volumeInfoList.addEventListener(
'splice', this.onVolumeInfoListUpdatedBound_); 'splice', this.onVolumeInfoListUpdatedBound_);
// Run pending tasks.
const pendingTasks = this.pendingTasks_;
this.pendingTasks_ = null;
for (var i = 0; i < pendingTasks.length; i++) {
pendingTasks[i]();
}
} }
/** /**
...@@ -289,14 +269,6 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -289,14 +269,6 @@ class FilteredVolumeManager extends cr.EventTarget {
this.list_, [index, numRemovedVolumes].concat(addedVolumes)); this.list_, [index, numRemovedVolumes].concat(addedVolumes));
} }
/**
* Returns whether the VolumeManager is initialized or not.
* @return {boolean} True if the VolumeManager is initialized.
*/
isInitialized() {
return this.pendingTasks_ === null;
}
/** /**
* Ensures the VolumeManager is initialized, and then invokes callback. * Ensures the VolumeManager is initialized, and then invokes callback.
* If the VolumeManager is already initialized, callback will be called * If the VolumeManager is already initialized, callback will be called
...@@ -304,12 +276,7 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -304,12 +276,7 @@ class FilteredVolumeManager extends cr.EventTarget {
* @param {function()} callback Called on initialization completion. * @param {function()} callback Called on initialization completion.
*/ */
ensureInitialized(callback) { ensureInitialized(callback) {
if (!this.isInitialized()) { this.initialized_.then(callback);
this.pendingTasks_.push(this.ensureInitialized.bind(this, callback));
return;
}
callback();
} }
/** /**
...@@ -350,16 +317,17 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -350,16 +317,17 @@ class FilteredVolumeManager extends cr.EventTarget {
this.ensureInitialized(() => { this.ensureInitialized(() => {
const defaultVolume = this.getCurrentProfileVolumeInfo( const defaultVolume = this.getCurrentProfileVolumeInfo(
VolumeManagerCommon.VolumeType.DOWNLOADS); VolumeManagerCommon.VolumeType.DOWNLOADS);
if (defaultVolume) { if (!defaultVolume) {
defaultVolume.resolveDisplayRoot(callback, () => { console.warn('Cannot get default display root');
// defaultVolume is DOWNLOADS and resolveDisplayRoot should succeed.
throw new Error(
'Unexpectedly failed to obtain the default display root.');
});
} else {
console.warn('Unexpectedly failed to obtain the default display root.');
callback(null); callback(null);
return;
} }
defaultVolume.resolveDisplayRoot(callback, () => {
// defaultVolume is DOWNLOADS and resolveDisplayRoot should succeed.
console.error('Cannot resolve default display root');
callback(null);
});
}); });
} }
...@@ -402,6 +370,8 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -402,6 +370,8 @@ class FilteredVolumeManager extends cr.EventTarget {
* if the volume is never mounted. * if the volume is never mounted.
*/ */
async whenVolumeInfoReady(volumeId) { async whenVolumeInfoReady(volumeId) {
await this.initialized_;
const volumeInfo = this.filterDisallowedVolume_( const volumeInfo = this.filterDisallowedVolume_(
await this.volumeManager_.whenVolumeInfoReady(volumeId)); await this.volumeManager_.whenVolumeInfoReady(volumeId));
...@@ -421,13 +391,9 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -421,13 +391,9 @@ class FilteredVolumeManager extends cr.EventTarget {
* when an error occurs. * when an error occurs.
*/ */
mountArchive(fileUrl, successCallback, errorCallback) { mountArchive(fileUrl, successCallback, errorCallback) {
if (this.pendingTasks_) { this.ensureInitialized(() => {
this.pendingTasks_.push(this.mountArchive.bind( this.volumeManager_.mountArchive(fileUrl, successCallback, errorCallback);
this, fileUrl, successCallback, errorCallback)); });
return;
}
this.volumeManager_.mountArchive(fileUrl, successCallback, errorCallback);
} }
/** /**
...@@ -438,13 +404,9 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -438,13 +404,9 @@ class FilteredVolumeManager extends cr.EventTarget {
* when an error occurs. * when an error occurs.
*/ */
unmount(volumeInfo, successCallback, errorCallback) { unmount(volumeInfo, successCallback, errorCallback) {
if (this.pendingTasks_) { this.ensureInitialized(() => {
this.pendingTasks_.push( this.volumeManager_.unmount(volumeInfo, successCallback, errorCallback);
this.unmount.bind(this, volumeInfo, successCallback, errorCallback)); });
return;
}
this.volumeManager_.unmount(volumeInfo, successCallback, errorCallback);
} }
/** /**
...@@ -453,15 +415,8 @@ class FilteredVolumeManager extends cr.EventTarget { ...@@ -453,15 +415,8 @@ class FilteredVolumeManager extends cr.EventTarget {
* @return {!Promise} Fulfilled on success, otherwise rejected with an error * @return {!Promise} Fulfilled on success, otherwise rejected with an error
* message. * message.
*/ */
configure(volumeInfo) { async configure(volumeInfo) {
if (this.pendingTasks_) { await this.initialized_;
return new Promise((fulfill, reject) => {
this.pendingTasks_.push(() => {
this.volumeManager_.configure(volumeInfo).then(fulfill, reject);
});
});
}
return this.volumeManager_.configure(volumeInfo); return this.volumeManager_.configure(volumeInfo);
} }
......
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