Index: chrome/browser/resources/file_manager/js/file_copy_manager.js |
diff --git a/chrome/browser/resources/file_manager/js/file_copy_manager.js b/chrome/browser/resources/file_manager/js/file_copy_manager.js |
index 1099f5d1ab00989b03a7ca6c2929b096ad54b7f3..7b2038c3526b3d69392bcdb31d1d4aac306904d9 100644 |
--- a/chrome/browser/resources/file_manager/js/file_copy_manager.js |
+++ b/chrome/browser/resources/file_manager/js/file_copy_manager.js |
@@ -889,9 +889,17 @@ FileCopyManager.prototype.processCopyEntry_ = function( |
targetRelativePath, |
{create: true, exclusive: true}, |
function(targetEntry) { |
- self.copyFileEntry_( |
+ self.cancelCallback_ = self.copyFileEntry_( |
sourceEntry, targetEntry, |
- onCopyProgress, onCopyComplete, onFilesystemError); |
+ onCopyProgress, |
+ function(entry, size) { |
+ self.cancelCallback_ = null; |
+ onCopyComplete(entry, size); |
+ }, |
+ function(error) { |
+ self.cancelCallback_ = null; |
+ onFilesystemError(error); |
+ }); |
}, |
util.flog('Error getting file: ' + targetRelativePath, |
onFilesystemError)); |
@@ -991,6 +999,9 @@ FileCopyManager.prototype.processCopyEntry_ = function( |
* @param {function(FileError)} errorCallback Function that will be called |
* if an error is encountered. Takes error type and additional error data |
* as parameters. |
+ * @return {function()} Callback to cancel the current file copy operation. |
+ * When the cancel is done, errorCallback will be called. The returned |
+ * callback must not be called more than once. |
* @private |
*/ |
FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, |
@@ -998,24 +1009,37 @@ FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, |
progressCallback, |
successCallback, |
errorCallback) { |
- if (this.maybeCancel_()) |
- return; |
+ // Set to true when cancel is requested. |
+ var cancelRequested = false; |
- var self = this; |
+ sourceEntry.file(function(file) { |
+ if (cancelRequested) { |
+ errorCallback(util.createFileError(FileError.ABORT_ERR)); |
+ return; |
+ } |
+ |
+ targetEntry.createWriter(function(writer) { |
+ if (cancelRequested) { |
+ errorCallback(util.createFileError(FileError.ABORT_ERR)); |
mtomasz
2013/08/06 01:39:19
Can we remove the target file on cancel? It may be
hidehiko
2013/08/06 02:14:55
I want to keep this as is, because, IIRC, many OS
mtomasz
2013/08/06 02:21:38
As far as I remember on Windows the file gets remo
mtomasz
2013/08/06 02:22:29
I meant when downloading a file in Chrome.
|
+ return; |
+ } |
- var onSourceFileFound = function(file) { |
- var onWriterCreated = function(writer) { |
var reportedProgress = 0; |
- writer.onerror = function(progress) { |
- errorCallback(writer.error); |
+ writer.onerror = writer.onabort = function(progress) { |
+ errorCallback(cancelRequested ? |
+ util.createFileError(FileError.ABORT_ERR) : |
+ writer.error); |
}; |
writer.onprogress = function(progress) { |
- if (self.maybeCancel_()) { |
+ if (cancelRequested) { |
// If the copy was cancelled, we should abort the operation. |
+ // The errorCallback will be called by writer.onabort after the |
+ // termination. |
writer.abort(); |
return; |
} |
+ |
// |progress.loaded| will contain total amount of data copied by now. |
// |progressCallback| expects data amount delta from the last progress |
// update. |
@@ -1024,20 +1048,31 @@ FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, |
}; |
writer.onwrite = function() { |
+ if (cancelRequested) { |
+ errorCallback(util.createFileError(FileError.ABORT_ERR)); |
+ return; |
+ } |
+ |
sourceEntry.getMetadata(function(metadata) { |
- chrome.fileBrowserPrivate.setLastModified(targetEntry.toURL(), |
+ if (cancelRequested) { |
+ errorCallback(util.createFileError(FileError.ABORT_ERR)); |
+ return; |
+ } |
+ |
+ chrome.fileBrowserPrivate.setLastModified( |
+ targetEntry.toURL(), |
'' + Math.round(metadata.modificationTime.getTime() / 1000)); |
successCallback(targetEntry, file.size - reportedProgress); |
}); |
}; |
writer.write(file); |
- }; |
+ }, errorCallback); |
+ }, errorCallback); |
- targetEntry.createWriter(onWriterCreated, errorCallback); |
+ return function() { |
+ cancelRequested = true; |
}; |
- |
- sourceEntry.file(onSourceFileFound, errorCallback); |
}; |
/** |
@@ -1196,11 +1231,7 @@ FileCopyManager.prototype.serviceZipTask_ = function( |
onFilesystemError); |
} else { |
onFilesystemError( |
- Object.create(FileError.prototype, { |
- code: { |
- get: function() { return FileError.INVALID_MODIFICATION_ERR; } |
- } |
- })); |
+ util.createFileError(FileError.INVALID_MODIFICATION_ERR)); |
} |
}; |