OLD | NEW |
(Empty) | |
| 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 /** |
| 6 * Type of a root directory. |
| 7 * @enum |
| 8 */ |
| 9 var RootType = { |
| 10 DOWNLOADS: 'downloads', |
| 11 ARCHIVE: 'archive', |
| 12 REMOVABLE: 'removable', |
| 13 GDATA: 'gdata' |
| 14 }; |
| 15 |
| 16 /** |
| 17 * Top directory for each root type. |
| 18 * @type {Object.<RootType,string>} |
| 19 */ |
| 20 var RootDirectory = { |
| 21 DOWNLOADS: '/Downloads', |
| 22 ARCHIVE: '/archive', |
| 23 REMOVABLE: '/removable', |
| 24 GDATA: '/drive' |
| 25 }; |
| 26 |
| 27 var PathUtil = {}; |
| 28 |
| 29 /** |
| 30 * @param {string} path Path starting with '/'. |
| 31 * @return {string} Root directory (starting with '/'). |
| 32 */ |
| 33 PathUtil.getRootDirectory = function(path) { |
| 34 var i = path.indexOf('/', 1); |
| 35 return i === -1 ? path.substring(0) : path.substring(0, i); |
| 36 }; |
| 37 |
| 38 /** |
| 39 * @param {string} path Any unix-style path (may start or not start from root). |
| 40 * @return {Array.<string>} path components |
| 41 */ |
| 42 PathUtil.split = function(path) { |
| 43 var fromRoot = false; |
| 44 if (path[0] === '/') { |
| 45 fromRoot = true; |
| 46 path = path.substring(1); |
| 47 } |
| 48 |
| 49 var components = path.split('/'); |
| 50 if (fromRoot) |
| 51 components[0] = '/' + components[0]; |
| 52 return components; |
| 53 }; |
| 54 |
| 55 /** |
| 56 * Join path components into a single path. Can be called either with a list of |
| 57 * components as arguments, or with an array of components as the only argument. |
| 58 * |
| 59 * Examples: |
| 60 * Path.join('abc', 'def') -> 'abc/def' |
| 61 * Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi' |
| 62 * Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi' |
| 63 * |
| 64 * @return {string} Resulting path. |
| 65 */ |
| 66 PathUtil.join = function() { |
| 67 var components; |
| 68 |
| 69 if (arguments.length === 1 && typeof(arguments[0]) === 'object') { |
| 70 components = arguments[0]; |
| 71 } else { |
| 72 components = arguments; |
| 73 } |
| 74 |
| 75 var path = ''; |
| 76 for (var i = 0; i < components.length; i++) { |
| 77 if (components[i][0] === '/') { |
| 78 path = components[i]; |
| 79 continue; |
| 80 } |
| 81 if (path.length === 0 || path[path.length - 1] !== '/') |
| 82 path += '/'; |
| 83 path += components[i]; |
| 84 } |
| 85 return path; |
| 86 }; |
| 87 |
| 88 /** |
| 89 * @param {string} path Path starting with '/'. |
| 90 * @return {RootType} RootType.DOWNLOADS, RootType.GDATA etc. |
| 91 */ |
| 92 PathUtil.getRootType = function(path) { |
| 93 var rootDir = PathUtil.getRootDirectory(path); |
| 94 for (var type in RootDirectory) { |
| 95 if (rootDir === RootDirectory[type]) |
| 96 return RootType[type]; |
| 97 } |
| 98 }; |
| 99 |
| 100 /** |
| 101 * @param {string} path Any path. |
| 102 * @return {string} The root path. |
| 103 */ |
| 104 PathUtil.getRootPath = function(path) { |
| 105 var type = PathUtil.getRootType(path); |
| 106 |
| 107 if (type == RootType.DOWNLOADS || type == RootType.GDATA) |
| 108 return PathUtil.getRootDirectory(path); |
| 109 |
| 110 if (type == RootType.ARCHIVE || type == RootType.REMOVABLE) { |
| 111 var components = PathUtil.split(path); |
| 112 if (components.length > 1) { |
| 113 return PathUtil.join(components[0], components[1]); |
| 114 } else { |
| 115 return components[0]; |
| 116 } |
| 117 } |
| 118 |
| 119 return '/'; |
| 120 }; |
| 121 |
| 122 /** |
| 123 * @param {string} path A path. |
| 124 * @return {boolean} True if it is a path to the root. |
| 125 */ |
| 126 PathUtil.isRootPath = function(path) { |
| 127 return PathUtil.getRootPath(path) === path; |
| 128 }; |
| 129 |
| 130 /** |
| 131 * @param {string} parent_path The parent path. |
| 132 * @param {string} child_path The child path. |
| 133 * @return {boolean} True if |parent_path| is parent file path of |child_path|. |
| 134 */ |
| 135 PathUtil.isParentPath = function(parent_path, child_path) { |
| 136 if (!parent_path || parent_path.length == 0 || |
| 137 !child_path || child_path.length == 0) |
| 138 return false; |
| 139 |
| 140 if (parent_path[parent_path.length - 1] != '/') |
| 141 parent_path += '/'; |
| 142 |
| 143 if (child_path[child_path.length - 1] != '/') |
| 144 child_path += '/'; |
| 145 |
| 146 return child_path.indexOf(parent_path) == 0; |
| 147 }; |
| 148 |
| 149 /** |
| 150 * Return the localized name for the root. |
| 151 * @param {string} path The full path of the root (starting with slash). |
| 152 * @return {string} The localized name. |
| 153 */ |
| 154 PathUtil.getRootLabel = function(path) { |
| 155 function str(id) { |
| 156 return loadTimeData.getString(id); |
| 157 } |
| 158 |
| 159 if (path === RootDirectory.DOWNLOADS) |
| 160 return str('DOWNLOADS_DIRECTORY_LABEL'); |
| 161 |
| 162 if (path === RootDirectory.ARCHIVE) |
| 163 return str('ARCHIVE_DIRECTORY_LABEL'); |
| 164 if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path)) |
| 165 return path.substring(RootDirectory.ARCHIVE.length + 1); |
| 166 |
| 167 if (path === RootDirectory.REMOVABLE) |
| 168 return str('REMOVABLE_DIRECTORY_LABEL'); |
| 169 if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path)) |
| 170 return path.substring(RootDirectory.REMOVABLE.length + 1); |
| 171 |
| 172 if (path === RootDirectory.GDATA) |
| 173 return str('GDATA_DIRECTORY_LABEL'); |
| 174 |
| 175 return path; |
| 176 }; |
| 177 |
| 178 |
OLD | NEW |