Index: ui/file_manager/file_manager/background/js/media_import_handler.js |
diff --git a/ui/file_manager/file_manager/background/js/media_import_handler.js b/ui/file_manager/file_manager/background/js/media_import_handler.js |
index 864c0ff4b79fe6e19b384b7680ffda828deceab5..0e5c34dbbb40f42286fa377050b0b754c0739726 100644 |
--- a/ui/file_manager/file_manager/background/js/media_import_handler.js |
+++ b/ui/file_manager/file_manager/background/js/media_import_handler.js |
@@ -102,7 +102,7 @@ importer.MediaImportHandler.prototype.onTaskProgress_ = |
strf('CLOUD_IMPORT_ITEMS_REMAINING', task.remainingFilesCount); |
item.progressMax = task.totalBytes; |
item.cancelCallback = function() { |
- // TODO(kenobi): Deal with import cancellation. |
+ task.requestCancel(); |
}; |
} |
@@ -122,6 +122,10 @@ importer.MediaImportHandler.prototype.onTaskProgress_ = |
item.progressValue = item.progressMax; |
item.state = ProgressItemState.ERROR; |
break; |
+ case UpdateType.CANCELED: |
+ item.message = ''; |
+ item.state = ProgressItemState.CANCELED; |
+ break; |
} |
this.progressCenter_.updateItem(item); |
@@ -132,11 +136,6 @@ importer.MediaImportHandler.prototype.onTaskProgress_ = |
* the FileOperationManager (and thus *spawns* an associated |
* FileOperationManager.CopyTask) but this is a temporary state of affairs. |
* |
- * TODO(kenobi): Add a proper implementation that doesn't use |
- * FileOperationManager, but instead actually performs the copy using the |
- * fileManagerPrivate API directly. |
- * TODO(kenobi): Add task cancellation. |
- * |
* @constructor |
* @extends {importer.TaskQueue.BaseTask} |
* @struct |
@@ -177,6 +176,12 @@ importer.MediaImportHandler.ImportTask = function( |
/** @private {number} */ |
this.remainingFilesCount_ = 0; |
+ |
+ /** @private {?function()} */ |
+ this.cancelCallback_ = null; |
+ |
+ /** @private {boolean} Indicates whether this task was canceled. */ |
+ this.canceled_ = false; |
}; |
/** @struct */ |
@@ -207,6 +212,21 @@ importer.MediaImportHandler.ImportTask.prototype.run = function() { |
.then(this.importTo_.bind(this)); |
}; |
+/** |
+ * Request cancellation of this task. An update will be sent to observers once |
+ * the task is actually cancelled. |
+ */ |
+importer.MediaImportHandler.ImportTask.prototype.requestCancel = function() { |
+ this.canceled_ = true; |
+ if (this.cancelCallback_) { |
+ // Reset the callback before calling it, as the callback might do anything |
+ // (including calling #requestCancel again). |
+ var cancelCallback = this.cancelCallback_; |
+ this.cancelCallback_ = null; |
+ cancelCallback(); |
+ } |
+}; |
+ |
/** @private */ |
importer.MediaImportHandler.ImportTask.prototype.initialize_ = function() { |
this.remainingFilesCount_ = this.scanResult_.getFileEntries().length; |
@@ -251,7 +271,10 @@ importer.MediaImportHandler.ImportTask.prototype.importTo_ = |
*/ |
importer.MediaImportHandler.ImportTask.prototype.importOne_ = |
function(destination, completionCallback, entry, index) { |
- // TODO(kenobi): Check for cancellation. |
+ if (this.canceled_) { |
+ this.notify(importer.TaskQueue.UpdateType.CANCELED); |
+ return; |
+ } |
// A count of the current number of processed bytes for this entry. |
var currentBytes = 0; |
@@ -285,19 +308,26 @@ importer.MediaImportHandler.ImportTask.prototype.importOne_ = |
/** @this {importer.MediaImportHandler.ImportTask} */ |
var onComplete = function() { |
+ this.cancelCallback_ = null; |
this.markAsCopied_(entry, destination); |
this.notify(importer.TaskQueue.UpdateType.PROGRESS); |
completionCallback(); |
}; |
- fileOperationUtil.copyTo( |
+ /** @this {importer.MediaImportHandler.ImportTask} */ |
+ var onError = function(error) { |
+ this.cancelCallback_ = null; |
+ this.onError_(error); |
+ }; |
+ |
+ this.cancelCallback_ = fileOperationUtil.copyTo( |
entry, |
destination, |
entry.name, // TODO(kenobi): account for duplicate filenames |
onEntryChanged.bind(this), |
onProgress.bind(this), |
onComplete.bind(this), |
- this.onError_.bind(this)); |
+ onError.bind(this)); |
}; |
/** |
@@ -420,34 +450,3 @@ importer.MediaImportHandler.defaultDestination.getImportDestination = |
return defaultDestination.getDriveRoot_() |
.then(defaultDestination.getOrCreateImportDestination_); |
}; |
- |
-/** |
- * Sends events for progress updates and creation of file entries. |
- * |
- * TODO: File entry-related events might need to be handled via callback and not |
- * events - see crbug.com/358491 |
- * |
- * @constructor |
- * @extends {cr.EventTarget} |
- */ |
-importer.MediaImportHandler.EventRouter = function() { |
-}; |
- |
-/** |
- * Extends cr.EventTarget. |
- */ |
-importer.MediaImportHandler.EventRouter.prototype.__proto__ = |
- cr.EventTarget.prototype; |
- |
-/** |
- * @param {!importer.MediaImportHandler.ImportTask} task |
- */ |
-importer.MediaImportHandler.EventRouter.prototype.sendUpdate = function(task) { |
-}; |
- |
-/** |
- * @param {!FileEntry} entry The new entry. |
- */ |
-importer.MediaImportHandler.EventRouter.prototype.sendEntryCreated = |
- function(entry) { |
-}; |