1// 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 5'use strict'; 6 7/** 8 * Type of a root directory. 9 * @enum {string} 10 * @const 11 */ 12var RootType = Object.freeze({ 13 // Root of local directory. 14 DOWNLOADS: 'downloads', 15 16 // Root of mounted archive file. 17 ARCHIVE: 'archive', 18 19 // Root of removal volume. 20 REMOVABLE: 'removable', 21 22 // Root of drive directory. 23 DRIVE: 'drive', 24 25 // Root for entries that is not located under RootType.DRIVE. e.g. shared 26 // files. 27 DRIVE_OTHER: 'drive_other', 28 29 // Fake root for offline available files on the drive. 30 DRIVE_OFFLINE: 'drive_offline', 31 32 // Fake root for shared files on the drive. 33 DRIVE_SHARED_WITH_ME: 'drive_shared_with_me', 34 35 // Fake root for recent files on the drive. 36 DRIVE_RECENT: 'drive_recent' 37}); 38 39/** 40 * Top directory for each root type. 41 * TODO(mtomasz): Deprecated. Remove this. 42 * @enum {string} 43 * @const 44 */ 45var RootDirectory = Object.freeze({ 46 DOWNLOADS: '/Downloads', 47 ARCHIVE: '/archive', 48 REMOVABLE: '/removable', 49 DRIVE: '/drive', 50 DRIVE_OFFLINE: '/drive_offline', // A fake root. Not the actual filesystem. 51 DRIVE_SHARED_WITH_ME: '/drive_shared_with_me', // A fake root. 52 DRIVE_RECENT: '/drive_recent' // A fake root. 53}); 54 55/** 56 * Sub root directory for Drive. "root" and "other". This is not used now. 57 * TODO(haruki): Add namespaces support. http://crbug.com/174233. 58 * @enum {string} 59 * @const 60 */ 61var DriveSubRootDirectory = Object.freeze({ 62 ROOT: 'root', 63 OTHER: 'other', 64}); 65 66var PathUtil = {}; 67 68/** 69 * The default mount point. 70 * TODO(mtomasz): Deprecated. Use the volume manager instead. 71 * @type {string} 72 * @const 73 */ 74PathUtil.DEFAULT_MOUNT_POINT = '/Downloads'; 75 76/** 77 * Checks if the given path represents a special search. Fake entries in 78 * RootDirectory correspond to special searches. 79 * @param {string} path Path to check. 80 * @return {boolean} True if the given path represents a special search. 81 */ 82PathUtil.isSpecialSearchRoot = function(path) { 83 var type = PathUtil.getRootType(path); 84 return type == RootType.DRIVE_OFFLINE || 85 type == RootType.DRIVE_SHARED_WITH_ME || 86 type == RootType.DRIVE_RECENT; 87}; 88 89/** 90 * Checks |path| and return true if it is under Google Drive or a special 91 * search root which represents a special search from Google Drive. 92 * @param {string} path Path to check. 93 * @return {boolean} True if the given path represents a Drive based path. 94 */ 95PathUtil.isDriveBasedPath = function(path) { 96 var rootType = PathUtil.getRootType(path); 97 return rootType === RootType.DRIVE || 98 rootType === RootType.DRIVE_SHARED_WITH_ME || 99 rootType === RootType.DRIVE_RECENT || 100 rootType === RootType.DRIVE_OFFLINE; 101}; 102 103/** 104 * @param {string} path Path starting with '/'. 105 * @return {string} Top directory (starting with '/'). 106 */ 107PathUtil.getTopDirectory = function(path) { 108 var i = path.indexOf('/', 1); 109 return i === -1 ? path : path.substring(0, i); 110}; 111 112/** 113 * Obtains the parent path of the specified path. 114 * @param {string} path Path string. 115 * @return {string} Parent path. 116 */ 117PathUtil.getParentDirectory = function(path) { 118 if (path[path.length - 1] == '/') 119 return PathUtil.getParentDirectory(path.substring(0, path.length - 1)); 120 var index = path.lastIndexOf('/'); 121 if (index == 0) 122 return '/'; 123 else if (index == -1) 124 return '.'; 125 return path.substring(0, index); 126}; 127 128/** 129 * @param {string} path Any unix-style path (may start or not start from root). 130 * @return {Array.<string>} Path components. 131 */ 132PathUtil.split = function(path) { 133 var fromRoot = false; 134 if (path[0] === '/') { 135 fromRoot = true; 136 path = path.substring(1); 137 } 138 139 var components = path.split('/'); 140 if (fromRoot) 141 components[0] = '/' + components[0]; 142 return components; 143}; 144 145/** 146 * Returns a directory part of the given |path|. In other words, the path 147 * without its base name. 148 * 149 * Examples: 150 * PathUtil.dirname('abc') -> '' 151 * PathUtil.dirname('a/b') -> 'a' 152 * PathUtil.dirname('a/b/') -> 'a/b' 153 * PathUtil.dirname('a/b/c') -> 'a/b' 154 * PathUtil.dirname('/') -> '/' 155 * PathUtil.dirname('/abc') -> '/' 156 * PathUtil.dirname('/abc/def') -> '/abc' 157 * PathUtil.dirname('') -> '' 158 * 159 * @param {string} path The path to be parsed. 160 * @return {string} The directory path. 161 */ 162PathUtil.dirname = function(path) { 163 var index = path.lastIndexOf('/'); 164 if (index < 0) 165 return ''; 166 if (index == 0) 167 return '/'; 168 return path.substring(0, index); 169}; 170 171/** 172 * Returns the base name (the last component) of the given |path|. If the 173 * |path| ends with '/', returns an empty component. 174 * 175 * Examples: 176 * PathUtil.basename('abc') -> 'abc' 177 * PathUtil.basename('a/b') -> 'b' 178 * PathUtil.basename('a/b/') -> '' 179 * PathUtil.basename('a/b/c') -> 'c' 180 * PathUtil.basename('/') -> '' 181 * PathUtil.basename('/abc') -> 'abc' 182 * PathUtil.basename('/abc/def') -> 'def' 183 * PathUtil.basename('') -> '' 184 * 185 * @param {string} path The path to be parsed. 186 * @return {string} The base name. 187 */ 188PathUtil.basename = function(path) { 189 var index = path.lastIndexOf('/'); 190 return index >= 0 ? path.substring(index + 1) : path; 191}; 192 193/** 194 * Join path components into a single path. Can be called either with a list of 195 * components as arguments, or with an array of components as the only argument. 196 * 197 * Examples: 198 * Path.join('abc', 'def') -> 'abc/def' 199 * Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi' 200 * Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi' 201 * 202 * @return {string} Resulting path. 203 */ 204PathUtil.join = function() { 205 var components; 206 207 if (arguments.length === 1 && typeof(arguments[0]) === 'object') { 208 components = arguments[0]; 209 } else { 210 components = arguments; 211 } 212 213 var path = ''; 214 for (var i = 0; i < components.length; i++) { 215 if (components[i][0] === '/') { 216 path = components[i]; 217 continue; 218 } 219 if (path.length === 0 || path[path.length - 1] !== '/') 220 path += '/'; 221 path += components[i]; 222 } 223 return path; 224}; 225 226/** 227 * @param {string} path Path starting with '/'. 228 * @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc. 229 */ 230PathUtil.getRootType = function(path) { 231 var rootDir = PathUtil.getTopDirectory(path); 232 for (var type in RootDirectory) { 233 if (rootDir === RootDirectory[type]) 234 return RootType[type]; 235 } 236}; 237 238/** 239 * @param {string} path Any path. 240 * @return {string} The root path. 241 */ 242PathUtil.getRootPath = function(path) { 243 var type = PathUtil.getRootType(path); 244 245 if (type == RootType.DOWNLOADS || type == RootType.DRIVE_OFFLINE || 246 type == RootType.DRIVE_SHARED_WITH_ME || type == RootType.DRIVE_RECENT) 247 return PathUtil.getTopDirectory(path); 248 249 if (type == RootType.DRIVE || type == RootType.ARCHIVE || 250 type == RootType.REMOVABLE) { 251 var components = PathUtil.split(path); 252 if (components.length > 1) { 253 return PathUtil.join(components[0], components[1]); 254 } else { 255 return components[0]; 256 } 257 } 258 259 return '/'; 260}; 261 262/** 263 * @param {string} path A path. 264 * @return {boolean} True if it is a path to the root. 265 */ 266PathUtil.isRootPath = function(path) { 267 return PathUtil.getRootPath(path) === path; 268}; 269 270/** 271 * @param {string} path A root path. 272 * @return {boolean} True if the given path is root and user can unmount it. 273 */ 274PathUtil.isUnmountableByUser = function(path) { 275 if (!PathUtil.isRootPath(path)) 276 return false; 277 278 var type = PathUtil.getRootType(path); 279 return (type == RootType.ARCHIVE || type == RootType.REMOVABLE); 280}; 281 282/** 283 * @param {string} parent_path The parent path. 284 * @param {string} child_path The child path. 285 * @return {boolean} True if |parent_path| is parent file path of |child_path|. 286 */ 287PathUtil.isParentPath = function(parent_path, child_path) { 288 if (!parent_path || parent_path.length == 0 || 289 !child_path || child_path.length == 0) 290 return false; 291 292 if (parent_path[parent_path.length - 1] != '/') 293 parent_path += '/'; 294 295 if (child_path[child_path.length - 1] != '/') 296 child_path += '/'; 297 298 return child_path.indexOf(parent_path) == 0; 299}; 300 301/** 302 * Return the localized name for the root. 303 * TODO(hirono): Support all RootTypes and stop to use paths. 304 * 305 * @param {string|RootType} path The full path of the root (starting with slash) 306 * or root type. 307 * @return {string} The localized name. 308 */ 309PathUtil.getRootLabel = function(path) { 310 var str = function(id) { 311 return loadTimeData.getString(id); 312 }; 313 314 if (path === RootDirectory.DOWNLOADS) 315 return str('DOWNLOADS_DIRECTORY_LABEL'); 316 317 if (path === RootDirectory.ARCHIVE) 318 return str('ARCHIVE_DIRECTORY_LABEL'); 319 if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path)) 320 return path.substring(RootDirectory.ARCHIVE.length + 1); 321 322 if (path === RootDirectory.REMOVABLE) 323 return str('REMOVABLE_DIRECTORY_LABEL'); 324 if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path)) 325 return path.substring(RootDirectory.REMOVABLE.length + 1); 326 327 // TODO(haruki): Add support for "drive/root" and "drive/other". 328 if (path === RootDirectory.DRIVE + '/' + DriveSubRootDirectory.ROOT) 329 return str('DRIVE_MY_DRIVE_LABEL'); 330 331 if (path === RootDirectory.DRIVE_OFFLINE) 332 return str('DRIVE_OFFLINE_COLLECTION_LABEL'); 333 334 if (path === RootDirectory.DRIVE_SHARED_WITH_ME || 335 path === RootType.DRIVE_SHARED_WITH_ME) 336 return str('DRIVE_SHARED_WITH_ME_COLLECTION_LABEL'); 337 338 if (path === RootDirectory.DRIVE_RECENT) 339 return str('DRIVE_RECENT_COLLECTION_LABEL'); 340 341 return path; 342}; 343 344/** 345 * Return the label of the folder to be shown. Eg. 346 * - '/foo/bar/baz' -> 'baz' 347 * - '/hoge/fuga/ -> 'fuga' 348 * If the directory is root, returns the root label, which is same as 349 * PathUtil.getRootLabel(). 350 * 351 * @param {string} directoryPath The full path of the folder. 352 * @return {string} The label to be shown. 353 */ 354PathUtil.getFolderLabel = function(directoryPath) { 355 var label = ''; 356 if (PathUtil.isRootPath(directoryPath)) 357 label = PathUtil.getRootLabel(directoryPath); 358 359 if (label && label != directoryPath) 360 return label; 361 362 var matches = directoryPath.match(/([^\/]*)[\/]?$/); 363 if (matches[1]) 364 return matches[1]; 365 366 return directoryPath; 367}; 368 369/** 370 * Returns if the given path can be a target path of folder shortcut. 371 * 372 * @param {string} directoryPath Directory path to be checked. 373 * @return {boolean} True if the path can be a target path of the shortcut. 374 */ 375PathUtil.isEligibleForFolderShortcut = function(directoryPath) { 376 return !PathUtil.isSpecialSearchRoot(directoryPath) && 377 !PathUtil.isRootPath(directoryPath) && 378 PathUtil.isDriveBasedPath(directoryPath); 379}; 380 381/** 382 * Extracts the extension of the path. 383 * 384 * Examples: 385 * PathUtil.splitExtension('abc.ext') -> ['abc', '.ext'] 386 * PathUtil.splitExtension('a/b/abc.ext') -> ['a/b/abc', '.ext'] 387 * PathUtil.splitExtension('a/b') -> ['a/b', ''] 388 * PathUtil.splitExtension('.cshrc') -> ['', '.cshrc'] 389 * PathUtil.splitExtension('a/b.backup/hoge') -> ['a/b.backup/hoge', ''] 390 * 391 * @param {string} path Path to be extracted. 392 * @return {Array.<string>} Filename and extension of the given path. 393 */ 394PathUtil.splitExtension = function(path) { 395 var dotPosition = path.lastIndexOf('.'); 396 if (dotPosition <= path.lastIndexOf('/')) 397 dotPosition = -1; 398 399 var filename = dotPosition != -1 ? path.substr(0, dotPosition) : path; 400 var extension = dotPosition != -1 ? path.substr(dotPosition) : ''; 401 return [filename, extension]; 402}; 403 404/** 405 * Obtains location information from a path. 406 * 407 * @param {!VolumeInfo} volumeInfo Volume containing an entry pointed by path. 408 * @param {string} fullPath Full path. 409 * @return {EntryLocation} Location information. 410 */ 411PathUtil.getLocationInfo = function(volumeInfo, fullPath) { 412 var rootPath; 413 var rootType; 414 if (volumeInfo.volumeType === util.VolumeType.DRIVE) { 415 // If the volume is drive, root path can be either mountPath + '/root' or 416 // mountPath + '/other'. 417 if ((fullPath + '/').indexOf(volumeInfo.mountPath + '/root/') === 0) { 418 rootPath = volumeInfo.mountPath + '/root'; 419 rootType = RootType.DRIVE; 420 } else if ((fullPath + '/').indexOf( 421 volumeInfo.mountPath + '/other/') === 0) { 422 rootPath = volumeInfo.mountPath + '/other'; 423 rootType = RootType.DRIVE_OTHER; 424 } else { 425 throw new Error(fullPath + ' is an invalid drive path.'); 426 } 427 } else { 428 // Otherwise, root path is same with a mount path of the volume. 429 rootPath = volumeInfo.mountPath; 430 switch (volumeInfo.volumeType) { 431 case util.VolumeType.DOWNLOADS: rootType = RootType.DOWNLOADS; break; 432 case util.VolumeType.REMOVABLE: rootType = RootType.REMOVABLE; break; 433 case util.VolumeType.ARCHIVE: rootType = RootType.ARCHIVE; break; 434 default: throw new Error( 435 'Invalid volume type: ' + volumeInfo.volumeType); 436 } 437 } 438 var isRootEntry = (fullPath.substr(0, rootPath.length) || '/') === fullPath; 439 return new EntryLocation(volumeInfo, rootType, isRootEntry); 440}; 441 442/** 443 * Location information which shows where the path points in FileManager's 444 * file system. 445 * 446 * @param {!VolumeInfo} volumeInfo Volume information. 447 * @param {RootType} rootType Root type. 448 * @param {boolean} isRootEntry Whether the entry is root entry or not. 449 * @constructor 450 */ 451function EntryLocation(volumeInfo, rootType, isRootEntry) { 452 /** 453 * Volume information. 454 * @type {!VolumeInfo} 455 */ 456 this.volumeInfo = volumeInfo; 457 458 /** 459 * Root type. 460 * @type {RootType} 461 */ 462 this.rootType = rootType; 463 464 /** 465 * Whether the entry is root entry or not. 466 * @type {boolean} 467 */ 468 this.isRootEntry = isRootEntry; 469 470 Object.freeze(this); 471} 472