| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Type of a root directory. | 6 * Type of a root directory. |
| 7 * @enum | 7 * @enum |
| 8 */ | 8 */ |
| 9 var RootType = { | 9 var RootType = { |
| 10 DOWNLOADS: 'downloads', | 10 DOWNLOADS: 'downloads', |
| 11 ARCHIVE: 'archive', | 11 ARCHIVE: 'archive', |
| 12 REMOVABLE: 'removable', | 12 REMOVABLE: 'removable', |
| 13 DRIVE: 'drive', | 13 DRIVE: 'drive', |
| 14 DRIVE_OFFLINE: 'drive_offline' // A fake root. Not the actual filesystem. | 14 DRIVE_OFFLINE: 'drive_offline' // A fake root. Not the actual filesystem. |
| 15 }; | 15 }; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Top directory for each root type. | 18 * Top directory for each root type. |
| 19 * @type {Object.<RootType,string>} | 19 * @type {Object.<RootType,string>} |
| 20 */ | 20 */ |
| 21 var RootDirectory = { | 21 var RootDirectory = { |
| 22 DOWNLOADS: '/Downloads', | 22 DOWNLOADS: '/Downloads', |
| 23 ARCHIVE: '/archive', | 23 ARCHIVE: '/archive', |
| 24 REMOVABLE: '/removable', | 24 REMOVABLE: '/removable', |
| 25 DRIVE: '/drive', | 25 DRIVE: '/drive', |
| 26 DRIVE_OFFLINE: '/drive_offline' // A fake root. Not the actual filesystem. | 26 DRIVE_OFFLINE: '/drive_offline' // A fake root. Not the actual filesystem. |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 /** |
| 30 * Sub root directory for Drive. "root" and "other". This is not used now. |
| 31 * TODO(haruki): Add namespaces support. http://crbug.com/174233. |
| 32 * @enum |
| 33 */ |
| 34 var DriveSubRootDirectory = { |
| 35 ROOT: 'root', |
| 36 OTHER: 'other', |
| 37 }; |
| 38 |
| 29 var PathUtil = {}; | 39 var PathUtil = {}; |
| 30 | 40 |
| 31 /** | 41 /** |
| 32 * @param {string} path Path starting with '/'. | 42 * @param {string} path Path starting with '/'. |
| 33 * @return {string} Root directory (starting with '/'). | 43 * @return {string} Top directory (starting with '/'). |
| 34 */ | 44 */ |
| 35 PathUtil.getRootDirectory = function(path) { | 45 PathUtil.getTopDirectory = function(path) { |
| 36 var i = path.indexOf('/', 1); | 46 var i = path.indexOf('/', 1); |
| 37 return i === -1 ? path.substring(0) : path.substring(0, i); | 47 return i === -1 ? path : path.substring(0, i); |
| 38 }; | 48 }; |
| 39 | 49 |
| 40 /** | 50 /** |
| 41 * @param {string} path Any unix-style path (may start or not start from root). | 51 * @param {string} path Any unix-style path (may start or not start from root). |
| 42 * @return {Array.<string>} Path components. | 52 * @return {Array.<string>} Path components. |
| 43 */ | 53 */ |
| 44 PathUtil.split = function(path) { | 54 PathUtil.split = function(path) { |
| 45 var fromRoot = false; | 55 var fromRoot = false; |
| 46 if (path[0] === '/') { | 56 if (path[0] === '/') { |
| 47 fromRoot = true; | 57 fromRoot = true; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 path += components[i]; | 95 path += components[i]; |
| 86 } | 96 } |
| 87 return path; | 97 return path; |
| 88 }; | 98 }; |
| 89 | 99 |
| 90 /** | 100 /** |
| 91 * @param {string} path Path starting with '/'. | 101 * @param {string} path Path starting with '/'. |
| 92 * @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc. | 102 * @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc. |
| 93 */ | 103 */ |
| 94 PathUtil.getRootType = function(path) { | 104 PathUtil.getRootType = function(path) { |
| 95 var rootDir = PathUtil.getRootDirectory(path); | 105 var rootDir = PathUtil.getTopDirectory(path); |
| 96 for (var type in RootDirectory) { | 106 for (var type in RootDirectory) { |
| 97 if (rootDir === RootDirectory[type]) | 107 if (rootDir === RootDirectory[type]) |
| 98 return RootType[type]; | 108 return RootType[type]; |
| 99 } | 109 } |
| 100 }; | 110 }; |
| 101 | 111 |
| 102 /** | 112 /** |
| 103 * @param {string} path Any path. | 113 * @param {string} path Any path. |
| 104 * @return {string} The root path. | 114 * @return {string} The root path. |
| 105 */ | 115 */ |
| 106 PathUtil.getRootPath = function(path) { | 116 PathUtil.getRootPath = function(path) { |
| 107 var type = PathUtil.getRootType(path); | 117 var type = PathUtil.getRootType(path); |
| 108 | 118 |
| 119 // TODO(haruki): Add support for "drive/root" and "drive/other". |
| 109 if (type == RootType.DOWNLOADS || type == RootType.DRIVE || | 120 if (type == RootType.DOWNLOADS || type == RootType.DRIVE || |
| 110 type == RootType.DRIVE_OFFLINE) | 121 type == RootType.DRIVE_OFFLINE) |
| 111 return PathUtil.getRootDirectory(path); | 122 return PathUtil.getTopDirectory(path); |
| 112 | 123 |
| 113 if (type == RootType.ARCHIVE || type == RootType.REMOVABLE) { | 124 if (type == RootType.ARCHIVE || |
| 125 type == RootType.REMOVABLE) { |
| 114 var components = PathUtil.split(path); | 126 var components = PathUtil.split(path); |
| 115 if (components.length > 1) { | 127 if (components.length > 1) { |
| 116 return PathUtil.join(components[0], components[1]); | 128 return PathUtil.join(components[0], components[1]); |
| 117 } else { | 129 } else { |
| 118 return components[0]; | 130 return components[0]; |
| 119 } | 131 } |
| 120 } | 132 } |
| 121 | 133 |
| 122 return '/'; | 134 return '/'; |
| 123 }; | 135 }; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 if (path === RootDirectory.ARCHIVE) | 177 if (path === RootDirectory.ARCHIVE) |
| 166 return str('ARCHIVE_DIRECTORY_LABEL'); | 178 return str('ARCHIVE_DIRECTORY_LABEL'); |
| 167 if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path)) | 179 if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path)) |
| 168 return path.substring(RootDirectory.ARCHIVE.length + 1); | 180 return path.substring(RootDirectory.ARCHIVE.length + 1); |
| 169 | 181 |
| 170 if (path === RootDirectory.REMOVABLE) | 182 if (path === RootDirectory.REMOVABLE) |
| 171 return str('REMOVABLE_DIRECTORY_LABEL'); | 183 return str('REMOVABLE_DIRECTORY_LABEL'); |
| 172 if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path)) | 184 if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path)) |
| 173 return path.substring(RootDirectory.REMOVABLE.length + 1); | 185 return path.substring(RootDirectory.REMOVABLE.length + 1); |
| 174 | 186 |
| 187 // TODO(haruki): Add support for "drive/root" and "drive/other". |
| 175 if (path === RootDirectory.DRIVE) | 188 if (path === RootDirectory.DRIVE) |
| 176 return str('DRIVE_DIRECTORY_LABEL'); | 189 return str('DRIVE_DIRECTORY_LABEL'); |
| 177 | 190 |
| 178 if (path === RootDirectory.DRIVE_OFFLINE) | 191 if (path === RootDirectory.DRIVE_OFFLINE) |
| 179 return str('DRIVE_OFFLINE_COLLECTION_LABEL'); | 192 return str('DRIVE_OFFLINE_COLLECTION_LABEL'); |
| 180 | 193 |
| 181 return path; | 194 return path; |
| 182 }; | 195 }; |
| OLD | NEW |