summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-31 03:32:59 +0000
committerhidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-31 03:32:59 +0000
commitba8d873508726e9e0c9e66685c7d8e8adb748e6c (patch)
tree136ce0ecb5b02c9ab7c1e68678eff756fd430671
parentd1879ab06b52845b45f771ae104711b9c8fa1195 (diff)
downloadchromium_src-ba8d873508726e9e0c9e66685c7d8e8adb748e6c.zip
chromium_src-ba8d873508726e9e0c9e66685c7d8e8adb748e6c.tar.gz
chromium_src-ba8d873508726e9e0c9e66685c7d8e8adb748e6c.tar.bz2
Refactoring around entry changed event on FileCopyManager.
FileCopyManager had an event "copy-operation-complete". However, it is not actually an event for operation complete, rather it is an event to notify that some entries are changed. This CL renames the event, as well as callback name in FileManager. Also, entry change for 'delete' operation was notifyied in another way. The changed entries' urls are attached to the 'deleted' event, and ButterBar updated the metadata cache, thouth it is an UI component. By this CL, "entry-changed" is also used for the entry change for 'delete' operation, too. BUG=246976 TEST=Ran browser_tests --gtest_filter="*FileSystemExtensionApiTest*:*FileManagerBrowserTest*" and tested manually. R=mtomasz@chromium.org Review URL: https://codereview.chromium.org/21092003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214542 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/resources/file_manager/js/butter_bar.js21
-rw-r--r--chrome/browser/resources/file_manager/js/directory_model.js84
-rw-r--r--chrome/browser/resources/file_manager/js/file_copy_manager.js89
-rw-r--r--chrome/browser/resources/file_manager/js/file_manager.js81
-rw-r--r--chrome/browser/resources/file_manager/js/util.js9
5 files changed, 150 insertions, 134 deletions
diff --git a/chrome/browser/resources/file_manager/js/butter_bar.js b/chrome/browser/resources/file_manager/js/butter_bar.js
index 72aa0a4..f372539 100644
--- a/chrome/browser/resources/file_manager/js/butter_bar.js
+++ b/chrome/browser/resources/file_manager/js/butter_bar.js
@@ -9,15 +9,13 @@
* progress and other messages.
* @param {HTMLElement} dialogDom FileManager top-level div.
* @param {FileCopyManagerWrapper} copyManager The copy manager.
- * @param {MetadataCache} metadataCache The metadata cache.
* @constructor
*/
-function ButterBar(dialogDom, copyManager, metadataCache) {
+function ButterBar(dialogDom, copyManager) {
this.dialogDom_ = dialogDom;
this.butter_ = this.dialogDom_.querySelector('#butter-bar');
this.document_ = this.butter_.ownerDocument;
this.copyManager_ = copyManager;
- this.metadataCache_ = metadataCache;
this.hideTimeout_ = null;
this.showTimeout_ = null;
this.lastShowTime_ = 0;
@@ -345,23 +343,16 @@ ButterBar.prototype.onCopyProgress_ = function(event) {
* @private
*/
ButterBar.prototype.onDelete_ = function(event) {
- var urls = Array.prototype.slice.call(event.urls);
switch (event.reason) {
case 'BEGIN':
if (this.currentMode_ != ButterBar.Mode.DELETE)
this.totalDeleted_ = 0;
case 'PROGRESS':
- var props = [];
- for (var i = 0; i < urls.length; i++) {
- props[i] = {deleted: true};
- }
- this.metadataCache_.set(urls, 'internal', props);
-
- this.totalDeleted_ += urls.length;
+ this.totalDeleted_ += event.urls.length;
var title = strf('DELETED_MESSAGE_PLURAL', this.totalDeleted_);
if (this.totalDeleted_ == 1) {
- var fullPath = util.extractFilePath(urls[0]);
+ var fullPath = util.extractFilePath(event.urls[0]);
var fileName = PathUtil.split(fullPath).pop();
title = strf('DELETED_MESSAGE', fileName);
}
@@ -376,12 +367,6 @@ ButterBar.prototype.onDelete_ = function(event) {
break;
case 'ERROR':
- var props = [];
- for (var i = 0; i < urls.length; i++) {
- props[i] = {deleted: false};
- }
- this.metadataCache_.set(urls, 'internal', props);
-
this.showError_(str('DELETE_ERROR'));
break;
diff --git a/chrome/browser/resources/file_manager/js/directory_model.js b/chrome/browser/resources/file_manager/js/directory_model.js
index 4f9ff2c..efce99f 100644
--- a/chrome/browser/resources/file_manager/js/directory_model.js
+++ b/chrome/browser/resources/file_manager/js/directory_model.js
@@ -675,45 +675,50 @@ DirectoryModel.prototype.replaceDirectoryContents_ = function(dirContents) {
};
/**
- * @param {string} name Filename.
- */
-DirectoryModel.prototype.onEntryChanged = function(name) {
- var currentEntry = this.getCurrentDirEntry();
- var fileList = this.getFileList();
- var self = this;
+ * Callback when an entry is changed.
+ * @param {util.EntryChangedType} type How the entry is changed.
+ * @param {Entry} entry The changed entry.
+ */
+DirectoryModel.prototype.onEntryChanged = function(type, entry) {
+ // TODO(hidehiko): We should update directory model even the search result
+ // is shown.
+ var rootType = this.getCurrentRootType();
+ if ((rootType === RootType.DRIVE ||
+ rootType === RootType.DRIVE_SHARED_WITH_ME ||
+ rootType === RootType.DRIVE_RECENT ||
+ rootType === RootType.DRIVE_OFFLINE) &&
+ this.isSearching())
+ return;
- var onEntryFound = function(entry) {
- // Do nothing if current directory changed during async operations.
- if (self.getCurrentDirEntry() != currentEntry)
- return;
- self.currentDirContents_.prefetchMetadata([entry], function() {
- // Do nothing if current directory changed during async operations.
- if (self.getCurrentDirEntry() != currentEntry)
+ if (type == util.EntryChangedType.CREATED) {
+ entry.getParent(function(parentEntry) {
+ if (this.getCurrentDirEntry().fullPath != parentEntry.fullPath) {
+ // Do nothing if current directory changed during async operations.
return;
-
- var index = self.findIndexByName_(name);
- if (index >= 0)
- fileList.splice(index, 1, entry);
- else
- fileList.splice(fileList.length, 0, entry);
- });
- };
-
- var onError = function(err) {
- if (err.code != FileError.NOT_FOUND_ERR) {
- self.rescanLater();
- return;
- }
-
- var index = self.findIndexByName_(name);
+ }
+ this.currentDirContents_.prefetchMetadata([entry], function() {
+ if (this.getCurrentDirEntry().fullPath != parentEntry.fullPath) {
+ // Do nothing if current directory changed during async operations.
+ return;
+ }
+
+ var index = this.findIndexByEntry_(entry);
+ if (index >= 0)
+ fileList.splice(index, 1, entry);
+ else
+ fileList.push(entry);
+ }.bind(this));
+ }.bind(this));
+ } else {
+ // This is the delete event.
+ var index = this.findIndexByEntry_(entry);
if (index >= 0)
- fileList.splice(index, 1);
- };
-
- util.resolvePath(currentEntry, name, onEntryFound, onError);
+ this.getFileList().splice(index, 1);
+ }
};
/**
+ * TODO(hidehiko): Migrate this method into findIndexByEntry_ defined below.
* @param {string} name Filename.
* @return {number} The index in the fileList.
* @private
@@ -727,6 +732,19 @@ DirectoryModel.prototype.findIndexByName_ = function(name) {
};
/**
+ * @param {Entry} entry The entry to be searched.
+ * @return {number} The index in the fileList, or -1 if not found.
+ * @private
+ */
+DirectoryModel.prototype.findIndexByEntry_ = function(entry) {
+ var fileList = this.getFileList();
+ for (var i = 0; i < fileList.length; i++)
+ if (fileList.item(i).fullPath == entry.fullPath)
+ return i;
+ return -1;
+};
+
+/**
* Rename the entry in the filesystem and update the file list.
* @param {Entry} entry Entry to rename.
* @param {string} newName New name.
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 d62889f..9e60cc5 100644
--- a/chrome/browser/resources/file_manager/js/file_copy_manager.js
+++ b/chrome/browser/resources/file_manager/js/file_copy_manager.js
@@ -70,18 +70,16 @@ FileCopyManager.EventRouter.prototype.sendProgressEvent = function(
};
/**
- * Dispatches an event to notify that the entries in affectedEntries are
- * changed (created or deleted) by operation reason.
- *
- * @param {string} reason Completed file operation. One of "moved", "copied"
- * or "deleted". TODO(hidehiko): Use enum.
- * @param {Array.<Entry>} affectedEntries Entries which are created or deleted.
+ * Dispatches an event to notify that an entry is changed (created or deleted).
+ * @param {util.EntryChangedType} type The enum to represent if the entry
+ * is created or deleted.
+ * @param {Entry} entry The changed entry.
*/
-FileCopyManager.EventRouter.prototype.sendOperationEvent = function(
- reason, affectedEntries) {
- var event = new cr.Event('copy-operation-complete');
- event.reason = reason;
- event.affectedEntries = affectedEntries;
+FileCopyManager.EventRouter.prototype.sendEntryChangedEvent = function(
+ type, entry) {
+ var event = new cr.Event('entry-changed');
+ event.type = type;
+ event.entry = entry;
this.dispatchEvent(event);
};
@@ -757,7 +755,8 @@ FileCopyManager.prototype.serviceCopyTask_ = function(
var count = task.originalEntries.length;
var onEntryDeleted = function(entry) {
- self.eventRouter_.sendOperationEvent('deleted', [entry]);
+ self.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.DELETED, entry);
count--;
if (!count)
successCallback();
@@ -838,7 +837,8 @@ FileCopyManager.prototype.serviceNextCopyTaskEntry_ = function(
};
var onCopyComplete = function(entry, size) {
- self.eventRouter_.sendOperationEvent('copied', [entry]);
+ self.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.CREATED, entry);
onCopyCompleteBase(entry, size);
};
@@ -847,12 +847,13 @@ FileCopyManager.prototype.serviceNextCopyTaskEntry_ = function(
self.eventRouter_.sendProgressEvent('PROGRESS', self.getStatus());
};
- var onFilesystemCopyComplete = function(sourceEntry, targetEntry) {
+ var onFilesystemCopyComplete = function(targetEntry) {
// TODO(benchan): We currently do not know the size of data being
// copied by FileEntry.copyTo(), so task.completedBytes will not be
// increased. We will address this issue once we need to use
// task.completedBytes to track the progress.
- self.eventRouter_.sendOperationEvent('copied', [sourceEntry, targetEntry]);
+ self.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.CREATED, targetEntry);
onCopyCompleteBase(targetEntry, 0);
};
@@ -893,7 +894,7 @@ FileCopyManager.prototype.serviceNextCopyTaskEntry_ = function(
targetEntry.getMetadata(function(metadata) {
if (metadata.size > transferedBytes)
onCopyProgress(sourceEntry, metadata.size - transferedBytes);
- onFilesystemCopyComplete(sourceEntry, targetEntry);
+ onFilesystemCopyComplete(targetEntry);
});
};
@@ -1095,8 +1096,10 @@ FileCopyManager.prototype.serviceNextMoveTaskEntry_ = function(
sourceEntry.moveTo(
dirEntry, PathUtil.basename(targetRelativePath),
function(targetEntry) {
- self.eventRouter_.sendOperationEvent(
- 'moved', [sourceEntry, targetEntry]);
+ self.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.CREATED, targetEntry);
+ self.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.DELETED, sourceEntry);
task.markEntryComplete(targetEntry, 0);
successCallback();
},
@@ -1249,7 +1252,8 @@ FileCopyManager.prototype.deleteEntries = function(entries) {
FileCopyManager.prototype.serviceAllDeleteTasks_ = function() {
var self = this;
- var onTaskSuccess = function(task) {
+ var onTaskSuccess = function() {
+ var task = self.deleteTasks_[0];
self.deleteTasks_.shift();
if (!self.deleteTasks_.length) {
// All tasks have been serviced, clean up and exit.
@@ -1298,35 +1302,46 @@ FileCopyManager.prototype.serviceAllDeleteTasks_ = function() {
* Performs the deletion.
*
* @param {Object} task The delete task (see deleteEntries function).
- * @param {function(Object)} onComplete Completion callback with the task
- * as an argument.
- * @param {function(Object)} onFailure Failure callback with the task as an
- * argument.
+ * @param {function()} successCallback Callback run on success.
+ * @param {function(FileCopyManager.Error)} errorCallback Callback run on error.
* @private
*/
FileCopyManager.prototype.serviceDeleteTask_ = function(
- task, onComplete, onFailure) {
+ task, successCallback, errorCallback) {
var downcount = task.entries.length;
+ if (downcount == 0) {
+ successCallback();
+ return;
+ }
- var onEntryComplete = function() {
- if (--downcount == 0)
- onComplete(task);
- }.bind(this);
-
- var onEntryFailure = function() {
- if (--downcount == 0)
- onFailure(task);
- }.bind(this);
+ var filesystemError = null;
+ var onComplete = function() {
+ if (--downcount > 0)
+ return;
- if (downcount == 0)
- onComplete(task);
+ // All remove operations are processed. Run callback.
+ if (filesystemError) {
+ errorCallback(new FileCopyManager.Error(
+ util.FileOperationErrorType.FILESYSTEM_ERROR, filesystemError));
+ } else {
+ successCallback();
+ }
+ };
for (var i = 0; i < task.entries.length; i++) {
var entry = task.entries[i];
util.removeFileOrDirectory(
entry,
- onEntryComplete,
- onEntryFailure);
+ function(currentEntry) {
+ this.eventRouter_.sendEntryChangedEvent(
+ util.EntryChangedType.DELETED, currentEntry);
+ onComplete();
+ }.bind(this, entry),
+ function(error) {
+ if (!filesystemError)
+ filesystemError = error;
+ onComplete();
+ });
}
};
diff --git a/chrome/browser/resources/file_manager/js/file_manager.js b/chrome/browser/resources/file_manager/js/file_manager.js
index a80a5b2..7fa2247 100644
--- a/chrome/browser/resources/file_manager/js/file_manager.js
+++ b/chrome/browser/resources/file_manager/js/file_manager.js
@@ -428,8 +428,7 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
FileManager.prototype.initDataTransferOperations_ = function() {
this.copyManager_ = new FileCopyManagerWrapper.getInstance();
- this.butterBar_ = new ButterBar(this.dialogDom_, this.copyManager_,
- this.metadataCache_);
+ this.butterBar_ = new ButterBar(this.dialogDom_, this.copyManager_);
// CopyManager and ButterBar are required for 'Delete' operation in
// Open and Save dialogs. But drag-n-drop and copy-paste are not needed.
@@ -441,11 +440,10 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
this.copyManager_.addEventListener(
'copy-progress', this.onCopyProgressBound_);
- this.onCopyManagerOperationCompleteBound_ =
- this.onCopyManagerOperationComplete_.bind(this);
+ this.onCopyManagerEntryChangedBound_ =
+ this.onCopyManagerEntryChanged_.bind(this);
this.copyManager_.addEventListener(
- 'copy-operation-complete',
- this.onCopyManagerOperationCompleteBound_);
+ 'entry-changed', this.onCopyManagerEntryChangedBound_);
var controller = this.fileTransferController_ =
new FileTransferController(this.document_,
@@ -1386,44 +1384,36 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
};
/**
- * Handler of file manager operations. Update directory model
- * to reflect operation result immediatelly (not waiting directory
- * update event). Also, preloads thumbnails for the copied images.
+ * Handler of file manager operations. Called when an entry has been
+ * changed.
+ * This updates directory model to reflect operation result immediately (not
+ * waiting for directory update event). Also, preloads thumbnails for the
+ * images of new entries.
+ * See also FileCopyManager.EventRouter.
*
- * @param {Event} Operation completion event.
- * @private
- */
- FileManager.prototype.onCopyManagerOperationComplete_ = function(event) {
- var currentPath = this.directoryModel_.getCurrentDirPath();
- if (this.isOnDrive() && this.directoryModel_.isSearching())
- return;
-
- var inCurrentDirectory = function(entry) {
- var fullPath = entry.fullPath;
- var dirPath = fullPath.substr(0, fullPath.length -
- entry.name.length - 1);
- return dirPath == currentPath;
- };
- for (var i = 0; i < event.affectedEntries.length; i++) {
- var entry = event.affectedEntries[i];
- if (inCurrentDirectory(entry)) {
- this.directoryModel_.onEntryChanged(entry.name);
- } else if (event.reason == 'copied' && FileType.isImage(entry)) {
- // Preload a thumbnail if the new copied entry an image.
- var metadata = entry.getMetadata(function(metadata) {
- var url = entry.toURL();
- var thumbnailLoader_ = new ThumbnailLoader(
- url,
- ThumbnailLoader.LoaderType.CANVAS,
- metadata,
- undefined, // Media type.
- FileType.isOnDrive(url) ?
- ThumbnailLoader.UseEmbedded.USE_EMBEDDED :
- ThumbnailLoader.UseEmbedded.NO_EMBEDDED,
- 10); // Very low priority.
- thumbnailLoader_.loadDetachedImage(function(success) {});
- });
- }
+ * @param {cr.Event} event An event for the entry change.
+ * @private
+ */
+ FileManager.prototype.onCopyManagerEntryChanged_ = function(event) {
+ var type = event.type;
+ var entry = event.entry;
+ this.directoryModel_.onEntryChanged(type, entry);
+
+ if (type == util.EntryChangedType.CREATE && FileType.isImage(entry)) {
+ // Preload a thumbnail if the new copied entry an image.
+ var metadata = entry.getMetadata(function(metadata) {
+ var url = entry.toURL();
+ var thumbnailLoader_ = new ThumbnailLoader(
+ url,
+ ThumbnailLoader.LoaderType.CANVAS,
+ metadata,
+ undefined, // Media type.
+ FileType.isOnDrive(url) ?
+ ThumbnailLoader.UseEmbedded.USE_EMBEDDED :
+ ThumbnailLoader.UseEmbedded.NO_EMBEDDED,
+ 10); // Very low priority.
+ thumbnailLoader_.loadDetachedImage(function(success) {});
+ });
}
};
@@ -2492,10 +2482,9 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
this.copyManager_.removeEventListener(
'copy-progress', this.onCopyProgressBound_);
}
- if (this.onCopyManagerOperationCompleteBound_) {
+ if (this.onCopyManagerEntryChangedBound_) {
this.copyManager_.removeEventListener(
- 'copy-operation-complete',
- this.onCopyManagerOperationCompleteBound_);
+ 'entry-changed', this.onCopyManagerEntryChangedBound_);
}
}
};
diff --git a/chrome/browser/resources/file_manager/js/util.js b/chrome/browser/resources/file_manager/js/util.js
index 9f5a564..b31f404 100644
--- a/chrome/browser/resources/file_manager/js/util.js
+++ b/chrome/browser/resources/file_manager/js/util.js
@@ -1149,3 +1149,12 @@ util.FileOperationErrorType = {
TARGET_EXISTS: 1,
FILESYSTEM_ERROR: 2,
};
+
+/**
+ * The type of an entry changed event.
+ * @enum {number}
+ */
+util.EntryChangedType = {
+ CREATED: 0,
+ DELETED: 1,
+};