Commit 55f3eb10 authored by iseki's avatar iseki Committed by Commit bot

Add filter to prevent move operation in the same directory.

BUG=418014
TEST=manually
1. Open Files.app. Put a file name "test.txt" under /Downloads.
2. Start dragging the file, and drop to the blank area of the same
   directory (/Downloads).
3. Confirm to not start move operation.

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

Cr-Commit-Position: refs/heads/master@{#297354}
parent 26acebd1
......@@ -1177,6 +1177,47 @@ FileOperationManager.prototype.requestTaskCancel = function(taskId) {
}
};
/**
* Filters the entry in the same directory
*
* @param {Array.<Entry>} sourceEntries Entries of the source files.
* @param {DirectoryEntry} targetEntry The destination entry of the target
* directory.
* @param {boolean} isMove True if the operation is "move", otherwise (i.e.
* if the operation is "copy") false.
* @return {Promise} Promise fulfilled with the filtered entry. This is not
* rejected.
*/
FileOperationManager.prototype.filterSameDirectoryEntry = function(
sourceEntries, targetEntry, isMove) {
if (!isMove)
return Promise.resolve(sourceEntries);
// Utility function to concat arrays.
var compactArrays = function(arrays) {
return arrays.filter(function(element) { return !!element; });
};
// Call processEntry for each item of entries.
var processEntries = function(entries) {
var promises = entries.map(processFileOrDirectoryEntries);
return Promise.all(promises).then(compactArrays);
};
// Check all file entries and keeps only those need sharing operation.
var processFileOrDirectoryEntries = function(entry) {
return new Promise(function(resolve) {
entry.getParent(function(inParentEntry) {
if (!util.isSameEntry(inParentEntry, targetEntry))
resolve(entry);
else
resolve(null);
}, function(error) {
console.error(error.stack || error);
resolve(null);
});
});
};
return processEntries(sourceEntries);
}
/**
* Kick off pasting.
*
......@@ -1195,37 +1236,14 @@ FileOperationManager.prototype.paste = function(
if (sourceEntries.length === 0)
return;
var filteredEntries = [];
var resolveGroup = new AsyncUtil.Queue();
if (isMove) {
for (var index = 0; index < sourceEntries.length; index++) {
resolveGroup.run(function(sourceEntry, callback) {
sourceEntry.getParent(function(inParentEntry) {
if (!util.isSameEntry(inParentEntry, targetEntry))
filteredEntries.push(sourceEntry);
callback();
}, function() {
console.warn(
'Failed to resolve the parent for: ' + sourceEntry.toURL());
// Even if the parent is not available, try to move it.
filteredEntries.push(sourceEntry);
callback();
});
}.bind(this, sourceEntries[index]));
}
} else {
// Always copy all of the files.
filteredEntries = sourceEntries;
}
resolveGroup.run(function(callback) {
// Do nothing, if we have no entries to be pasted.
if (filteredEntries.length === 0)
return;
this.queueCopy_(targetEntry, filteredEntries, isMove, opt_taskId);
}.bind(this));
this.filterSameDirectoryEntry(sourceEntries, targetEntry, isMove).then(
function(entries) {
if (entries.length === 0)
return;
this.queueCopy_(targetEntry, entries, isMove, opt_taskId);
}.bind(this)).catch(function(error) {
console.error(error.stack || error);
});
};
/**
......
......@@ -347,21 +347,35 @@ FileTransferController.prototype = {
opt_effect === 'move');
var destinationEntry =
opt_destinationEntry || this.currentDirectoryContentEntry;
var entries;
var entries = [];
var failureUrls;
var taskId = this.fileOperationManager_.generateTaskId();
util.URLsToEntries(sourceURLs).then(function(result) {
this.pendingTaskIds.push(taskId);
entries = result.entries;
failureUrls = result.failureUrls;
return this.fileOperationManager_.filterSameDirectoryEntry(
result.entries, destinationEntry, toMove);
}.bind(this)).then(function(filteredEntries) {
entries = filteredEntries;
if (entries.length === 0)
return Promise.reject('ABORT');
this.pendingTaskIds.push(taskId);
var item = new ProgressCenterItem();
item.id = taskId;
item.type = ProgressItemType.COPY;
if (result.entries.length === 1)
item.message = strf('COPY_FILE_NAME', result.entries[0].name);
else
item.message = strf('COPY_ITEMS_REMAINING', result.entries.length);
if (toMove) {
item.type = ProgressItemType.MOVE;
if (entries.length === 1)
item.message = strf('MOVE_FILE_NAME', entries[0].name);
else
item.message = strf('MOVE_ITEMS_REMAINING', entries.length);
} else {
item.type = ProgressItemType.COPY;
if (entries.length === 1)
item.message = strf('COPY_FILE_NAME', entries[0].name);
else
item.message = strf('COPY_ITEMS_REMAINING', entries.length);
}
this.progressCenter_.updateItem(item);
// Check if cross share is needed or not.
return this.getMultiProfileShareEntries_(entries);
......
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