Commit 0633e9ba authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Introduce fileOperationUtil.

This CL introduces fileOperationUtil namespace and move/extracts some helper
methods from FileCopyManager to it.
This is preparation to move copyTo helper to it, so that moving the copyTo
from js-side to c++-side should be simpler task. The moving copyTo will be done
in following CLs.

BUG=246976
TEST=Ran browser_tests --gtest_filter="*FileSystemExtensionApiTest*:*FileManagerBrowserTest*" and tested manually.

Review URL: https://chromiumcodereview.appspot.com/22291004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@215854 0039d316-1c4b-4281-b951-d872f2087c98
parent 04e56030
...@@ -4,6 +4,88 @@ ...@@ -4,6 +4,88 @@
'use strict'; 'use strict';
/**
* Utilities for FileCopyManager.
*/
var fileOperationUtil = {};
/**
* Simple wrapper for util.deduplicatePath. On error, this method translates
* the FileError to FileCopyManager.Error object.
*
* @param {DirectoryEntry} dirEntry The target directory entry.
* @param {string} relativePath The path to be deduplicated.
* @param {function(string)} successCallback Callback run with the deduplicated
* path on success.
* @param {function(FileCopyManager.Error)} errorCallback Callback run on error.
*/
fileOperationUtil.deduplicatePath = function(
dirEntry, relativePath, successCallback, errorCallback) {
util.deduplicatePath(
dirEntry, relativePath, successCallback,
function(err) {
var onFileSystemError = function(error) {
errorCallback(new FileCopyManager.Error(
util.FileOperationErrorType.FILESYSTEM_ERROR, error));
};
if (err.code == FileError.PATH_EXISTS_ERR) {
// Failed to uniquify the file path. There should be an existing
// entry, so return the error with it.
util.resolvePath(
dirEntry, relativePath,
function(entry) {
errorCallback(new FileCopyManager.Error(
util.FileOperationErrorType.TARGET_EXISTS, entry));
},
onFileSystemError);
return;
}
onFileSystemError(err);
});
};
/**
* Sets last modified date to the entry.
* @param {Entry} entry The entry to which the last modified is set.
* @param {Date} modificationTime The last modified time.
*/
fileOperationUtil.setLastModified = function(entry, modificationTime) {
chrome.fileBrowserPrivate.setLastModified(
entry.toURL(), '' + Math.round(modificationTime.getTime() / 1000));
};
/**
* Thin wrapper of chrome.fileBrowserPrivate.zipSelection to adapt its
* interface similar to copyTo().
*
* @param {Array.<Entry>} sources The array of entries to be archived.
* @param {DirectoryEntry} parent The entry of the destination directory.
* @param {string} newName The name of the archive to be created.
* @param {function(FileEntry)} successCallback Callback invoked when the
* operation is successfully done with the entry of the created archive.
* @param {function(FileError)} errorCallback Callback invoked when an error
* is found.
*/
fileOperationUtil.zipSelection = function(
sources, parent, newName, successCallback, errorCallback) {
chrome.fileBrowserPrivate.zipSelection(
parent.toURL(),
sources.map(function(e) { return e.toURL(); }),
newName, function(success) {
if (!success) {
// Failed to create a zip archive.
errorCallback(
util.createFileError(FileError.INVALID_MODIFICATION_ERR));
return;
}
// Returns the created entry via callback.
parent.getFile(
newName, {create: false}, successCallback, errorCallback);
});
};
/** /**
* @constructor * @constructor
*/ */
...@@ -136,41 +218,6 @@ FileCopyManager.Task = function(targetDirEntry, opt_zipBaseDirEntry) { ...@@ -136,41 +218,6 @@ FileCopyManager.Task = function(targetDirEntry, opt_zipBaseDirEntry) {
this.renamedDirectories_ = []; this.renamedDirectories_ = [];
}; };
/**
* Simple wrapper for util.deduplicatePath. On error, this method translates
* the FileError to FileCopyManager.Error object.
*
* @param {DirectoryEntry} dirEntry The target directory entry.
* @param {string} relativePath The path to be deduplicated.
* @param {function(string)} successCallback Callback run with the deduplicated
* path on success.
* @param {function(FileCopyManager.Error)} errorCallback Callback run on error.
*/
FileCopyManager.Task.deduplicatePath = function(
dirEntry, relativePath, successCallback, errorCallback) {
util.deduplicatePath(
dirEntry, relativePath, successCallback,
function(err) {
var onFileSystemError = function(error) {
errorCallback(new FileCopyManager.Error(
util.FileOperationErrorType.FILESYSTEM_ERROR, error));
};
if (err.code == FileError.PATH_EXISTS_ERR) {
// Failed to uniquify the file path. There should be an existing
// entry, so return the error with it.
util.resolvePath(
dirEntry, relativePath,
function(entry) {
errorCallback(new FileCopyManager.Error(
util.FileOperationErrorType.TARGET_EXISTS, entry));
},
onFileSystemError);
return;
}
onFileSystemError(err);
});
};
/** /**
* @param {Array.<Entry>} entries Entries. * @param {Array.<Entry>} entries Entries.
...@@ -906,6 +953,8 @@ FileCopyManager.prototype.processCopyEntry_ = function( ...@@ -906,6 +953,8 @@ FileCopyManager.prototype.processCopyEntry_ = function(
return; return;
} }
// TODO(hidehiko): Move following code to fileOperationUtil.
// Sending a file from a) Drive to Drive, b) Drive to local or c) local to // Sending a file from a) Drive to Drive, b) Drive to local or c) local to
// Drive. // Drive.
var sourceFileUrl = sourceEntry.toURL(); var sourceFileUrl = sourceEntry.toURL();
...@@ -980,12 +1029,13 @@ FileCopyManager.prototype.processCopyEntry_ = function( ...@@ -980,12 +1029,13 @@ FileCopyManager.prototype.processCopyEntry_ = function(
onFilesystemError); onFilesystemError);
}; };
FileCopyManager.Task.deduplicatePath( fileOperationUtil.deduplicatePath(
targetDirEntry, originalPath, onDeduplicated, errorCallback); targetDirEntry, originalPath, onDeduplicated, errorCallback);
}; };
/** /**
* Copies the contents of sourceEntry into targetEntry. * Copies the contents of sourceEntry into targetEntry.
* TODO(hidehiko): Move this method into fileOperationUtil.
* *
* @param {FileEntry} sourceEntry The file entry that will be copied. * @param {FileEntry} sourceEntry The file entry that will be copied.
* @param {FileEntry} targetEntry The file entry to which sourceEntry will be * @param {FileEntry} targetEntry The file entry to which sourceEntry will be
...@@ -1059,9 +1109,8 @@ FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, ...@@ -1059,9 +1109,8 @@ FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry,
return; return;
} }
chrome.fileBrowserPrivate.setLastModified( fileOperationUtil.setLastModified(
targetEntry.toURL(), targetEntry, metadata.modificationTime);
'' + Math.round(metadata.modificationTime.getTime() / 1000));
successCallback(targetEntry, file.size - reportedProgress); successCallback(targetEntry, file.size - reportedProgress);
}); });
}; };
...@@ -1151,7 +1200,7 @@ FileCopyManager.prototype.processMoveEntry_ = function( ...@@ -1151,7 +1200,7 @@ FileCopyManager.prototype.processMoveEntry_ = function(
return; return;
} }
FileCopyManager.Task.deduplicatePath( fileOperationUtil.deduplicatePath(
task.targetDirEntry, task.targetDirEntry,
task.applyRenames(sourceEntry.fullPath.substr(sourcePath.length + 1)), task.applyRenames(sourceEntry.fullPath.substr(sourcePath.length + 1)),
function(targetRelativePath) { function(targetRelativePath) {
...@@ -1196,13 +1245,6 @@ FileCopyManager.prototype.processMoveEntry_ = function( ...@@ -1196,13 +1245,6 @@ FileCopyManager.prototype.processMoveEntry_ = function(
FileCopyManager.prototype.serviceZipTask_ = function( FileCopyManager.prototype.serviceZipTask_ = function(
task, entryChangedCallback, progressCallback, successCallback, task, entryChangedCallback, progressCallback, successCallback,
errorCallback) { errorCallback) {
var dirURL = task.zipBaseDirEntry.toURL();
var selectionURLs = [];
for (var i = 0; i < task.pendingDirectories.length; i++)
selectionURLs.push(task.pendingDirectories[i].toURL());
for (var i = 0; i < task.pendingFiles.length; i++)
selectionURLs.push(task.pendingFiles[i].toURL());
// TODO(hidehiko): we should localize the name. // TODO(hidehiko): we should localize the name.
var destName = 'Archive'; var destName = 'Archive';
if (task.originalEntries.length == 1) { if (task.originalEntries.length == 1) {
...@@ -1213,35 +1255,25 @@ FileCopyManager.prototype.serviceZipTask_ = function( ...@@ -1213,35 +1255,25 @@ FileCopyManager.prototype.serviceZipTask_ = function(
destName = ((i < 0) ? basename : basename.substr(0, i)); destName = ((i < 0) ? basename : basename.substr(0, i));
} }
var onDeduplicated = function(destPath) { fileOperationUtil.deduplicatePath(
var onZipSelectionComplete = function(success) { task.targetDirEntry, destName + '.zip',
var onFilesystemError = function(err) { function(destPath) {
errorCallback(new FileCopyManager.Error( progressCallback();
util.FileOperationErrorType.FILESYSTEM_ERROR,
err));
};
if (success) { fileOperationUtil.zipSelection(
task.targetDirEntry.getFile( task.pendingDirectories.concat(task.pendingFiles),
destPath, {create: false}, task.zipBaseDirEntry,
destPath,
function(entry) { function(entry) {
entryChangedCallback(util.EntryChangedType.CREATE, entry); entryChangedCallback(util.EntryChangedType.CREATE, entry);
successCallback(); successCallback();
}, },
onFilesystemError); function(error) {
} else { errorCallback(new FileCopyManager.Error(
onFilesystemError( util.FileOperationErrorType.FILESYSTEM_ERROR, error));
util.createFileError(FileError.INVALID_MODIFICATION_ERR)); });
} },
}; errorCallback);
progressCallback();
chrome.fileBrowserPrivate.zipSelection(dirURL, selectionURLs, destPath,
onZipSelectionComplete);
};
FileCopyManager.Task.deduplicatePath(
task.targetDirEntry, destName + '.zip', onDeduplicated, errorCallback);
}; };
/** /**
...@@ -1378,7 +1410,7 @@ FileCopyManager.prototype.zipSelection = function(dirEntry, selectionEntries) { ...@@ -1378,7 +1410,7 @@ FileCopyManager.prototype.zipSelection = function(dirEntry, selectionEntries) {
// TODO: per-entry zip progress update with accurate byte count. // TODO: per-entry zip progress update with accurate byte count.
// For now just set completedBytes to same value as totalBytes so that the // For now just set completedBytes to same value as totalBytes so that the
// progress bar is full. // progress bar is full.
zipTask.completedBytes = zip.Task.totalBytes; zipTask.completedBytes = zipTask.totalBytes;
self.copyTasks_.push(zipTask); self.copyTasks_.push(zipTask);
if (self.copyTasks_.length == 1) { if (self.copyTasks_.length == 1) {
// Assume self.cancelRequested_ == false. // Assume self.cancelRequested_ == false.
......
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