Commit 2e3d1f6b authored by mtomasz@chromium.org's avatar mtomasz@chromium.org

Removed most of path utility functions in Files app.

This patch removes some random dependencies to PathUtils and migrates to LocationInfo in that places. Also, most of the path utilities were removed.

TEST=Tested manually. Partly browser_tests.
BUG=320967

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247951 0039d316-1c4b-4281-b951-d872f2087c98
parent 00ff88d2
......@@ -42,19 +42,19 @@ function VolumeInfo(
if (volumeType === util.VolumeType.DRIVE) {
this.fakeEntries[RootType.DRIVE_OFFLINE] = {
fullPath: RootDirectory.DRIVE_OFFLINE,
fullPath: '/drive_offline',
isDirectory: true,
rootType: RootType.DRIVE_OFFLINE,
toURL: function() { return 'fake-entry://' + this.fullPath; }
};
this.fakeEntries[RootType.DRIVE_SHARED_WITH_ME] = {
fullPath: RootDirectory.DRIVE_SHARED_WITH_ME,
fullPath: '/drive_shared_with_me',
isDirectory: true,
rootType: RootType.DRIVE_SHARED_WITH_ME,
toURL: function() { return 'fake-entry://' + this.fullPath; }
};
this.fakeEntries[RootType.DRIVE_RECENT] = {
fullPath: RootDirectory.DRIVE_RECENT,
fullPath: '/drive_recent',
isDirectory: true,
rootType: RootType.DRIVE_RECENT,
toURL: function() { return 'fake-entry://' + this.fullPath; }
......@@ -629,27 +629,6 @@ VolumeManager.prototype.unmount = function(volumeInfo,
this.startRequest_(requestKey, successCallback, errorCallback);
};
/**
* Resolves the absolute path to its entry. Shouldn't be used outside of the
* Files app's initialization.
* @param {string} path The path to be resolved.
* @param {function(Entry)} successCallback Called with the resolved entry on
* success.
* @param {function(FileError)} errorCallback Called on error.
*/
VolumeManager.prototype.resolveAbsolutePath = function(
path, successCallback, errorCallback) {
// Make sure the path is in the mounted volume.
var volumeInfo = this.getVolumeInfo(path);
if (!volumeInfo || !volumeInfo.root) {
errorCallback(util.createDOMError(util.FileError.NOT_FOUND_ERR));
return;
}
webkitResolveLocalFileSystemURL(
util.makeFilesystemUrl(path), successCallback, errorCallback);
};
/**
* Obtains the information of the volume that containing an entry pointed by the
* specified path.
......
......@@ -39,219 +39,8 @@ var RootType = Object.freeze({
DRIVE_RECENT: 'drive_recent'
});
/**
* Top directory for each root type.
* TODO(mtomasz): Deprecated. Remove this.
* @enum {string}
* @const
*/
var RootDirectory = Object.freeze({
DOWNLOADS: '/Downloads',
ARCHIVE: '/archive',
REMOVABLE: '/removable',
DRIVE: '/drive',
CLOUD_DEVICE: '/privet',
DRIVE_OFFLINE: '/drive_offline', // A fake root. Not the actual filesystem.
DRIVE_SHARED_WITH_ME: '/drive_shared_with_me', // A fake root.
DRIVE_RECENT: '/drive_recent' // A fake root.
});
var PathUtil = {};
/**
* Checks if the given path represents a special search. Fake entries in
* RootDirectory correspond to special searches.
* @param {string} path Path to check.
* @return {boolean} True if the given path represents a special search.
*/
PathUtil.isSpecialSearchRoot = function(path) {
var type = PathUtil.getRootType(path);
return type == RootType.DRIVE_OFFLINE ||
type == RootType.DRIVE_SHARED_WITH_ME ||
type == RootType.DRIVE_RECENT;
};
/**
* Checks |path| and return true if it is under Google Drive or a special
* search root which represents a special search from Google Drive.
* @param {string} path Path to check.
* @return {boolean} True if the given path represents a Drive based path.
*/
PathUtil.isDriveBasedPath = function(path) {
var rootType = PathUtil.getRootType(path);
return rootType === RootType.DRIVE ||
rootType === RootType.DRIVE_SHARED_WITH_ME ||
rootType === RootType.DRIVE_RECENT ||
rootType === RootType.DRIVE_OFFLINE;
};
/**
* @param {string} path Path starting with '/'.
* @return {string} Top directory (starting with '/').
*/
PathUtil.getTopDirectory = function(path) {
var i = path.indexOf('/', 1);
return i === -1 ? path : path.substring(0, i);
};
/**
* Obtains the parent path of the specified path.
* @param {string} path Path string.
* @return {string} Parent path.
*/
PathUtil.getParentDirectory = function(path) {
if (path[path.length - 1] == '/')
return PathUtil.getParentDirectory(path.substring(0, path.length - 1));
var index = path.lastIndexOf('/');
if (index == 0)
return '/';
else if (index == -1)
return '.';
return path.substring(0, index);
};
/**
* @param {string} path Any unix-style path (may start or not start from root).
* @return {Array.<string>} Path components.
*/
PathUtil.split = function(path) {
var fromRoot = false;
if (path[0] === '/') {
fromRoot = true;
path = path.substring(1);
}
var components = path.split('/');
if (fromRoot)
components[0] = '/' + components[0];
return components;
};
/**
* Returns a directory part of the given |path|. In other words, the path
* without its base name.
*
* Examples:
* PathUtil.dirname('abc') -> ''
* PathUtil.dirname('a/b') -> 'a'
* PathUtil.dirname('a/b/') -> 'a/b'
* PathUtil.dirname('a/b/c') -> 'a/b'
* PathUtil.dirname('/') -> '/'
* PathUtil.dirname('/abc') -> '/'
* PathUtil.dirname('/abc/def') -> '/abc'
* PathUtil.dirname('') -> ''
*
* @param {string} path The path to be parsed.
* @return {string} The directory path.
*/
PathUtil.dirname = function(path) {
var index = path.lastIndexOf('/');
if (index < 0)
return '';
if (index == 0)
return '/';
return path.substring(0, index);
};
/**
* Returns the base name (the last component) of the given |path|. If the
* |path| ends with '/', returns an empty component.
*
* Examples:
* PathUtil.basename('abc') -> 'abc'
* PathUtil.basename('a/b') -> 'b'
* PathUtil.basename('a/b/') -> ''
* PathUtil.basename('a/b/c') -> 'c'
* PathUtil.basename('/') -> ''
* PathUtil.basename('/abc') -> 'abc'
* PathUtil.basename('/abc/def') -> 'def'
* PathUtil.basename('') -> ''
*
* @param {string} path The path to be parsed.
* @return {string} The base name.
*/
PathUtil.basename = function(path) {
var index = path.lastIndexOf('/');
return index >= 0 ? path.substring(index + 1) : path;
};
/**
* Join path components into a single path. Can be called either with a list of
* components as arguments, or with an array of components as the only argument.
*
* Examples:
* Path.join('abc', 'def') -> 'abc/def'
* Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi'
* Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi'
*
* @return {string} Resulting path.
*/
PathUtil.join = function() {
var components;
if (arguments.length === 1 && typeof(arguments[0]) === 'object') {
components = arguments[0];
} else {
components = arguments;
}
var path = '';
for (var i = 0; i < components.length; i++) {
if (components[i][0] === '/') {
path = components[i];
continue;
}
if (path.length === 0 || path[path.length - 1] !== '/')
path += '/';
path += components[i];
}
return path;
};
/**
* @param {string} path Path starting with '/'.
* @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc.
*/
PathUtil.getRootType = function(path) {
var rootDir = PathUtil.getTopDirectory(path);
for (var type in RootDirectory) {
if (rootDir === RootDirectory[type])
return RootType[type];
}
};
/**
* @param {string} path Any path.
* @return {string} The root path.
*/
PathUtil.getRootPath = function(path) {
var type = PathUtil.getRootType(path);
if (type == RootType.DOWNLOADS || type == RootType.DRIVE_OFFLINE ||
type == RootType.DRIVE_SHARED_WITH_ME || type == RootType.DRIVE_RECENT)
return PathUtil.getTopDirectory(path);
if (type == RootType.DRIVE || type == RootType.ARCHIVE ||
type == RootType.REMOVABLE) {
var components = PathUtil.split(path);
if (components.length > 1) {
return PathUtil.join(components[0], components[1]);
} else {
return components[0];
}
}
return '/';
};
/**
* @param {string} path A path.
* @return {boolean} True if it is a path to the root.
*/
PathUtil.isRootPath = function(path) {
return PathUtil.getRootPath(path) === path;
};
/**
* Returns the localized name for the root type. If not available, then returns
* null.
......
......@@ -316,67 +316,6 @@ util.resolvePath = function(root, path, resultCallback, errorCallback) {
});
};
/**
* Locate the file referred to by path, creating directories or the file
* itself if necessary.
* @param {DirEntry} root The root entry.
* @param {string} path The file path.
* @param {function(FileEntry)} successCallback The callback.
* @param {function(FileError)} errorCallback The callback.
*/
util.getOrCreateFile = function(root, path, successCallback, errorCallback) {
var dirname = null;
var basename = null;
var onDirFound = function(dirEntry) {
dirEntry.getFile(basename, { create: true },
successCallback, errorCallback);
};
var i = path.lastIndexOf('/');
if (i > -1) {
dirname = path.substr(0, i);
basename = path.substr(i + 1);
} else {
basename = path;
}
if (!dirname) {
onDirFound(root);
return;
}
util.getOrCreateDirectory(root, dirname, onDirFound, errorCallback);
};
/**
* Locate the directory referred to by path, creating directories along the
* way.
* @param {DirEntry} root The root entry.
* @param {string} path The directory path.
* @param {function(FileEntry)} successCallback The callback.
* @param {function(FileError)} errorCallback The callback.
*/
util.getOrCreateDirectory = function(root, path, successCallback,
errorCallback) {
var names = path.split('/');
var getOrCreateNextName = function(dir) {
if (!names.length)
return successCallback(dir);
var name;
do {
name = names.shift();
} while (!name || name == '.');
dir.getDirectory(name, { create: true }, getOrCreateNextName,
errorCallback);
};
getOrCreateNextName(root);
};
/**
* Renames the entry to newName.
* @param {Entry} entry The entry to be renamed.
......
......@@ -779,27 +779,6 @@ DirectoryModel.prototype.onVolumeInfoListUpdated_ = function(event) {
}
};
/**
* Check if the root of the given path is mountable or not.
*
* @param {string} path Path.
* @return {boolean} Return true, if the given path is under mountable root.
* Otherwise, return false.
*/
DirectoryModel.isMountableRoot = function(path) {
var rootType = PathUtil.getRootType(path);
switch (rootType) {
case RootType.DOWNLOADS:
return false;
case RootType.ARCHIVE:
case RootType.REMOVABLE:
case RootType.DRIVE:
return true;
default:
throw new Error('Unknown root type!');
}
};
/**
* Performs search and displays results. The search type is dependent on the
* current directory. If we are currently on drive, server side content search
......
......@@ -1410,7 +1410,6 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
// Resolve the selectionURL to selectionEntry or to currentDirectoryEntry
// in case of being a display root.
queue.run(function(callback) {
// TODO(mtomasz): Migrate to URLs, and stop calling resolveAbsoluteURL.
if (!this.initSelectionURL_) {
callback();
return;
......@@ -1419,10 +1418,16 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
this.initSelectionURL_,
function(inEntry) {
var locationInfo = this.volumeManager_.getLocationInfo(inEntry);
// If location information is not available, then the volume is
// no longer (or never) available.
if (!locationInfo) {
callback();
return;
}
// If the selection is root, then use it as a current directory
// instead. This is because, selecting a root entry is done as
// opening it.
if (locationInfo && locationInfo.isRootEntry)
if (locationInfo.isRootEntry)
nextCurrentDirEntry = inEntry;
else
selectionEntry = inEntry;
......@@ -1439,9 +1444,14 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
webkitResolveLocalFileSystemURL(
this.initCurrentDirectoryURL_,
function(inEntry) {
var locationInfo = this.volumeManager_.getLocationInfo(inEntry);
if (!locationInfo) {
callback();
return;
}
nextCurrentDirEntry = inEntry;
callback();
}, callback);
}.bind(this), callback);
// TODO(mtomasz): Implement reopening on special search, when fake
// entries are converted to directory providers.
}.bind(this));
......
......@@ -211,9 +211,10 @@ FileTasks.prototype.processTasks_ = function(tasks) {
this.tasks_ = [];
var id = chrome.runtime.id;
var isOnDrive = false;
var fm = this.fileManager_;
for (var index = 0; index < this.entries_.length; ++index) {
// TODO(mtomasz): Use Entry instead of paths.
if (PathUtil.isDriveBasedPath(this.entries_[index].fullPath)) {
var locationInfo = fm.volumeManager.getLocationInfo(this.entries_[index]);
if (locationInfo && locationInfo.isDriveBased) {
isOnDrive = true;
break;
}
......
......@@ -31,7 +31,9 @@ function FolderShortcutsDataModel(volumeManager) {
var path = list[index];
group.add(function(path, callback) {
// TODO(mtomasz): Migrate to URL.
volumeManager.resolveAbsolutePath(path, function(entry) {
// TODO(mtomasz): Do not resolve, if volumeInfo for Drive is missing.
var url = util.makeFilesystemUrl(path);
webkitResolveLocalFileSystemURL(url, function(entry) {
if (entry.fullPath in this.pendingPaths_)
delete this.pendingPaths_[entry.fullPath];
this.addInternal_(entry);
......
......@@ -259,7 +259,13 @@ VolumeManagerWrapper.prototype.getDefaultDisplayRoot =
* @return {EntryLocation} Location information.
*/
VolumeManagerWrapper.prototype.getLocationInfo = function(entry) {
return this.volumeManager_ && this.volumeManager_.getLocationInfo(entry);
var locationInfo =
this.volumeManager_ && this.volumeManager_.getLocationInfo(entry);
if (!locationInfo)
return null;
if (!this.filterDisabledDriveVolume_(locationInfo.volumeInfo))
return null;
return locationInfo;
};
/**
......@@ -299,31 +305,6 @@ VolumeManagerWrapper.prototype.unmount = function(
this.volumeManager_.unmount(volumeInfo, successCallback, errorCallback);
};
/**
* Resolves the absolute path to an entry instance.
* @param {string} path The path to be resolved.
* @param {function(Entry)} successCallback Called with the resolved entry
* on success.
* @param {function(FileError)} errorCallback Called with the error on error.
*/
VolumeManagerWrapper.prototype.resolveAbsolutePath = function(
path, successCallback, errorCallback) {
if (this.pendingTasks_) {
this.pendingTasks_.push(this.resolveAbsolutePath.bind(
this, path, successCallback, errorCallback));
return;
}
// If the drive is disabled, any resolving the path under drive should be
// failed.
if (!this.driveEnabled_ && PathUtil.isDriveBasedPath(path)) {
errorCallback(util.createDOMError(util.FileError.NOT_FOUND_ERR));
return;
}
this.volumeManager_.resolveAbsolutePath(path, successCallback, errorCallback);
};
/**
* Filters volume info by referring driveEnabled.
*
......
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