Commit 3d51f8cd authored by mtomasz@chromium.org's avatar mtomasz@chromium.org

Fix Javascript errors occuring when a file is deleted during calculating it's size.

These errors occur when deleting very quickly, just after selecting a file. Such situation happens especially in tests. Along the way fixed the cancellation code.

TEST=Run browser_tests --test_filter="*FileManagerBrowser*Delete*"
BUG=233460

Review URL: https://codereview.chromium.org/14367009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195203 0039d316-1c4b-4281-b951-d872f2087c98
parent aa8328fb
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
*/ */
function FileSelection(fileManager, indexes) { function FileSelection(fileManager, indexes) {
this.fileManager_ = fileManager; this.fileManager_ = fileManager;
this.computeBytesSequence_ = 0;
this.indexes = indexes; this.indexes = indexes;
this.entries = []; this.entries = [];
this.urls = []; this.urls = [];
...@@ -23,7 +24,6 @@ function FileSelection(fileManager, indexes) { ...@@ -23,7 +24,6 @@ function FileSelection(fileManager, indexes) {
this.showBytes = false; this.showBytes = false;
this.allDriveFilesPresent = false, this.allDriveFilesPresent = false,
this.iconType = null; this.iconType = null;
this.cancelled_ = false;
this.bytesKnown = false; this.bytesKnown = false;
// Synchronously compute what we can. // Synchronously compute what we can.
...@@ -83,7 +83,8 @@ FileSelection.prototype.createTasks = function(callback) { ...@@ -83,7 +83,8 @@ FileSelection.prototype.createTasks = function(callback) {
/** /**
* Computes the total size of selected files. * Computes the total size of selected files.
* *
* @param {function} callback The callback. * @param {function} callback Completion callback. Not called when cancelled,
* or a new call has been invoked in the meantime.
*/ */
FileSelection.prototype.computeBytes = function(callback) { FileSelection.prototype.computeBytes = function(callback) {
if (this.entries.length == 0) { if (this.entries.length == 0) {
...@@ -92,26 +93,30 @@ FileSelection.prototype.computeBytes = function(callback) { ...@@ -92,26 +93,30 @@ FileSelection.prototype.computeBytes = function(callback) {
return; return;
} }
var countdown = this.entries.length; var computeBytesSequence = ++this.computeBytesSequence_;
var pendingMetadataCount = 0; var pendingMetadataCount = 0;
var maybeDone = function() { var maybeDone = function() {
if (countdown == 0 && pendingMetadataCount == 0 && !this.cancelled_) { if (pendingMetadataCount == 0) {
this.bytesKnown = true; this.bytesKnown = true;
callback(); callback();
} }
}.bind(this); }.bind(this);
var onProps = function(filesystem) { var onProps = function(properties) {
this.bytes += filesystem.size; // Ignore if the call got cancelled, or there is another new one fired.
if (computeBytesSequence != this.computeBytesSequence_)
return;
// It may happen that the metadata is not available because a file has been
// deleted in the meantime.
if (properties)
this.bytes += properties.size;
pendingMetadataCount--; pendingMetadataCount--;
maybeDone(); maybeDone();
}.bind(this); }.bind(this);
for (var index = 0; index < this.entries.length; index++) { for (var index = 0; index < this.entries.length; index++) {
if (this.cancelled_)
break;
var entry = this.entries[index]; var entry = this.entries[index];
if (entry.isFile) { if (entry.isFile) {
this.showBytes |= !FileType.isHosted(entry); this.showBytes |= !FileType.isHosted(entry);
...@@ -121,21 +126,20 @@ FileSelection.prototype.computeBytes = function(callback) { ...@@ -121,21 +126,20 @@ FileSelection.prototype.computeBytes = function(callback) {
// Don't compute the directory size as it's expensive. // Don't compute the directory size as it's expensive.
// crbug.com/179073. // crbug.com/179073.
this.showBytes = false; this.showBytes = false;
countdown = 0;
break; break;
} }
countdown--;
} }
maybeDone(); maybeDone();
}; };
/** /**
* Cancels any async computation. * Cancels any async computation by increasing the sequence number. Results
* of any previous call to computeBytes() will be discarded.
* *
* @private * @private
*/ */
FileSelection.prototype.cancelComputing_ = function() { FileSelection.prototype.cancelComputing_ = function() {
this.cancelled_ = true; this.computeBytesSequence_++;
}; };
/** /**
......
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