blob: 774cb1362ae2ca6be1be7e99abdbcadedfa69695 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00005'use strict';
6
Torne (Richard Coles)58218062012-11-14 11:43:16 +00007/**
8 * Type of a root directory.
9 * @enum
10 */
11var RootType = {
12 DOWNLOADS: 'downloads',
13 ARCHIVE: 'archive',
14 REMOVABLE: 'removable',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015 DRIVE: 'drive',
16 DRIVE_OFFLINE: 'drive_offline', // A fake root. Not the actual filesystem.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017 DRIVE_SHARED_WITH_ME: 'drive_shared_with_me', // A fake root.
18 DRIVE_RECENT: 'drive_recent' // A fake root.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019};
20
21/**
22 * Top directory for each root type.
23 * @type {Object.<RootType,string>}
24 */
25var RootDirectory = {
26 DOWNLOADS: '/Downloads',
27 ARCHIVE: '/archive',
28 REMOVABLE: '/removable',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029 DRIVE: '/drive',
30 DRIVE_OFFLINE: '/drive_offline', // A fake root. Not the actual filesystem.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010031 DRIVE_SHARED_WITH_ME: '/drive_shared_with_me', // A fake root.
32 DRIVE_RECENT: '/drive_recent' // A fake root.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000033};
34
35/**
36 * Sub root directory for Drive. "root" and "other". This is not used now.
37 * TODO(haruki): Add namespaces support. http://crbug.com/174233.
38 * @enum
39 */
40var DriveSubRootDirectory = {
41 ROOT: 'root',
42 OTHER: 'other',
Torne (Richard Coles)58218062012-11-14 11:43:16 +000043};
44
45var PathUtil = {};
46
47/**
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000048 * Checks if the given path represents a special search. Fake entries in
49 * RootDirectory correspond to special searches.
50 * @param {string} path Path to check.
51 * @return {boolean} True if the given path represents a special search.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052 */
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000053PathUtil.isSpecialSearchRoot = function(path) {
54 var type = PathUtil.getRootType(path);
55 return type == RootType.DRIVE_OFFLINE ||
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010056 type == RootType.DRIVE_SHARED_WITH_ME ||
57 type == RootType.DRIVE_RECENT;
58};
59
60/**
61 * Checks |path| and return true if it is under Google Drive or a sepecial
62 * search root which represents a special search from Google Drive.
63 * @param {string} path Path to check.
64 * @return {boolean} True if the given path represents a Drive based path.
65 */
66PathUtil.isDriveBasedPath = function(path) {
67 var rootType = PathUtil.getRootType(path);
68 return rootType === RootType.DRIVE ||
69 rootType === RootType.DRIVE_SHARED_WITH_ME ||
70 rootType === RootType.DRIVE_RECENT ||
71 rootType === RootType.DRIVE_OFFLINE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000072};
73
74/**
75 * @param {string} path Path starting with '/'.
76 * @return {string} Top directory (starting with '/').
77 */
78PathUtil.getTopDirectory = function(path) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000079 var i = path.indexOf('/', 1);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080 return i === -1 ? path : path.substring(0, i);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000081};
82
83/**
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010084 * Obtains the parent path of the specified path.
85 * @param {string} path Path string.
86 * @return {string} Parent path.
87 */
88PathUtil.getParentDirectory = function(path) {
89 if (path[path.length - 1] == '/')
90 return PathUtil.getParentDirectory(path.substring(0, path.length - 1));
91 var index = path.lastIndexOf('/');
92 if (index == 0)
93 return '/';
94 else if (index == -1)
95 return '.';
96 return path.substring(0, index);
97};
98
99/**
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100 * @param {string} path Any unix-style path (may start or not start from root).
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000101 * @return {Array.<string>} Path components.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000102 */
103PathUtil.split = function(path) {
104 var fromRoot = false;
105 if (path[0] === '/') {
106 fromRoot = true;
107 path = path.substring(1);
108 }
109
110 var components = path.split('/');
111 if (fromRoot)
112 components[0] = '/' + components[0];
113 return components;
114};
115
116/**
Ben Murdochbbcdd452013-07-25 10:06:34 +0100117 * Returns a directory part of the given |path|. In other words, the path
118 * without its base name.
119 *
120 * Examples:
121 * PathUtil.dirname('abc') -> ''
122 * PathUtil.dirname('a/b') -> 'a'
123 * PathUtil.dirname('a/b/') -> 'a/b'
124 * PathUtil.dirname('a/b/c') -> 'a/b'
125 * PathUtil.dirname('/') -> '/'
126 * PathUtil.dirname('/abc') -> '/'
127 * PathUtil.dirname('/abc/def') -> '/abc'
128 * PathUtil.dirname('') -> ''
129 *
130 * @param {string} path The path to be parsed.
131 * @return {string} The directory path.
132 */
133PathUtil.dirname = function(path) {
134 var index = path.lastIndexOf('/');
135 if (index < 0)
136 return '';
137 if (index == 0)
138 return '/';
139 return path.substring(0, index);
140};
141
142/**
143 * Returns the base name (the last component) of the given |path|. If the
144 * |path| ends with '/', returns an empty component.
145 *
146 * Examples:
147 * PathUtil.basename('abc') -> 'abc'
148 * PathUtil.basename('a/b') -> 'b'
149 * PathUtil.basename('a/b/') -> ''
150 * PathUtil.basename('a/b/c') -> 'c'
151 * PathUtil.basename('/') -> ''
152 * PathUtil.basename('/abc') -> 'abc'
153 * PathUtil.basename('/abc/def') -> 'def'
154 * PathUtil.basename('') -> ''
155 *
156 * @param {string} path The path to be parsed.
157 * @return {string} The base name.
158 */
159PathUtil.basename = function(path) {
160 var index = path.lastIndexOf('/');
161 return index >= 0 ? path.substring(index + 1) : path;
162};
163
164/**
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000165 * Join path components into a single path. Can be called either with a list of
166 * components as arguments, or with an array of components as the only argument.
167 *
168 * Examples:
169 * Path.join('abc', 'def') -> 'abc/def'
170 * Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi'
171 * Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi'
172 *
173 * @return {string} Resulting path.
174 */
175PathUtil.join = function() {
176 var components;
177
178 if (arguments.length === 1 && typeof(arguments[0]) === 'object') {
179 components = arguments[0];
180 } else {
181 components = arguments;
182 }
183
184 var path = '';
185 for (var i = 0; i < components.length; i++) {
186 if (components[i][0] === '/') {
187 path = components[i];
188 continue;
189 }
190 if (path.length === 0 || path[path.length - 1] !== '/')
191 path += '/';
192 path += components[i];
193 }
194 return path;
195};
196
197/**
198 * @param {string} path Path starting with '/'.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000199 * @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000200 */
201PathUtil.getRootType = function(path) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000202 var rootDir = PathUtil.getTopDirectory(path);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000203 for (var type in RootDirectory) {
204 if (rootDir === RootDirectory[type])
205 return RootType[type];
206 }
207};
208
209/**
210 * @param {string} path Any path.
211 * @return {string} The root path.
212 */
213PathUtil.getRootPath = function(path) {
214 var type = PathUtil.getRootType(path);
215
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100216 if (type == RootType.DOWNLOADS || type == RootType.DRIVE_OFFLINE ||
217 type == RootType.DRIVE_SHARED_WITH_ME || type == RootType.DRIVE_RECENT)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000218 return PathUtil.getTopDirectory(path);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000219
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100220 if (type == RootType.DRIVE || type == RootType.ARCHIVE ||
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000221 type == RootType.REMOVABLE) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000222 var components = PathUtil.split(path);
223 if (components.length > 1) {
224 return PathUtil.join(components[0], components[1]);
225 } else {
226 return components[0];
227 }
228 }
229
230 return '/';
231};
232
233/**
234 * @param {string} path A path.
235 * @return {boolean} True if it is a path to the root.
236 */
237PathUtil.isRootPath = function(path) {
238 return PathUtil.getRootPath(path) === path;
239};
240
241/**
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000242 * @param {string} path A root path.
243 * @return {boolean} True if the given path is root and user can unmount it.
244 */
245PathUtil.isUnmountableByUser = function(path) {
246 if (!PathUtil.isRootPath(path))
247 return false;
248
249 var type = PathUtil.getRootType(path);
250 return (type == RootType.ARCHIVE || type == RootType.REMOVABLE);
251};
252
253/**
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000254 * @param {string} parent_path The parent path.
255 * @param {string} child_path The child path.
256 * @return {boolean} True if |parent_path| is parent file path of |child_path|.
257 */
258PathUtil.isParentPath = function(parent_path, child_path) {
259 if (!parent_path || parent_path.length == 0 ||
260 !child_path || child_path.length == 0)
261 return false;
262
263 if (parent_path[parent_path.length - 1] != '/')
264 parent_path += '/';
265
266 if (child_path[child_path.length - 1] != '/')
267 child_path += '/';
268
269 return child_path.indexOf(parent_path) == 0;
270};
271
272/**
273 * Return the localized name for the root.
274 * @param {string} path The full path of the root (starting with slash).
275 * @return {string} The localized name.
276 */
277PathUtil.getRootLabel = function(path) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000278 var str = function(id) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000279 return loadTimeData.getString(id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000280 };
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000281
282 if (path === RootDirectory.DOWNLOADS)
283 return str('DOWNLOADS_DIRECTORY_LABEL');
284
285 if (path === RootDirectory.ARCHIVE)
286 return str('ARCHIVE_DIRECTORY_LABEL');
287 if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path))
288 return path.substring(RootDirectory.ARCHIVE.length + 1);
289
290 if (path === RootDirectory.REMOVABLE)
291 return str('REMOVABLE_DIRECTORY_LABEL');
292 if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path))
293 return path.substring(RootDirectory.REMOVABLE.length + 1);
294
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000295 // TODO(haruki): Add support for "drive/root" and "drive/other".
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100296 if (path === RootDirectory.DRIVE + '/' + DriveSubRootDirectory.ROOT)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000297 return str('DRIVE_DIRECTORY_LABEL');
298
299 if (path === RootDirectory.DRIVE_OFFLINE)
300 return str('DRIVE_OFFLINE_COLLECTION_LABEL');
301
302 if (path === RootDirectory.DRIVE_SHARED_WITH_ME)
303 return str('DRIVE_SHARED_WITH_ME_COLLECTION_LABEL');
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000304
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100305 if (path === RootDirectory.DRIVE_RECENT)
306 return str('DRIVE_RECENT_COLLECTION_LABEL');
307
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000308 return path;
309};
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100310
311/**
312 * Return the label of the folder to be shown. Eg.
313 * - '/foo/bar/baz' -> 'baz'
314 * - '/hoge/fuga/ -> 'fuga'
315 * If the directory is root, returns the root label, which is same as
316 * PathUtil.getRootLabel().
317 *
318 * @param {string} directoryPath The full path of the folder.
319 * @return {string} The label to be shown.
320 */
321PathUtil.getFolderLabel = function(directoryPath) {
322 var label = '';
323 if (PathUtil.isRootPath(directoryPath))
324 label = PathUtil.getRootLabel(directoryPath);
325
326 if (label && label != directoryPath)
327 return label;
328
329 var matches = directoryPath.match(/([^\/]*)[\/]?$/);
330 if (matches[1])
331 return matches[1];
332
333 return directoryPath;
334};
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100335
336/**
337 * Returns if the given path can be a target path of folder shortcut.
338 *
339 * @param {string} directoryPath Diretcoty path to be checked.
340 * @return {boolean} True if the path can be a target path of the shortcut.
341 */
342PathUtil.isEligibleForFolderShortcut = function(directoryPath) {
343 return !PathUtil.isSpecialSearchRoot(directoryPath) &&
Ben Murdochbb1529c2013-08-08 10:24:53 +0100344 !PathUtil.isRootPath(directoryPath) &&
345 PathUtil.isDriveBasedPath(directoryPath);
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100346};