1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Type of a root directory.
* @enum
*/
var RootType = {
DOWNLOADS: 'downloads',
ARCHIVE: 'archive',
REMOVABLE: 'removable',
DRIVE: 'drive',
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.
};
/**
* Top directory for each root type.
* @type {Object.<RootType,string>}
*/
var RootDirectory = {
DOWNLOADS: '/Downloads',
ARCHIVE: '/archive',
REMOVABLE: '/removable',
DRIVE: '/drive',
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.
};
/**
* Sub root directory for Drive. "root" and "other". This is not used now.
* TODO(haruki): Add namespaces support. http://crbug.com/174233.
* @enum
*/
var DriveSubRootDirectory = {
ROOT: 'root',
OTHER: 'other',
};
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 sepecial
* 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;
};
/**
* 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;
};
/**
* @param {string} path A root path.
* @return {boolean} True if the given path is root and user can unmount it.
*/
PathUtil.isUnmountableByUser = function(path) {
if (!PathUtil.isRootPath(path))
return false;
var type = PathUtil.getRootType(path);
return (type == RootType.ARCHIVE || type == RootType.REMOVABLE);
};
/**
* @param {string} parent_path The parent path.
* @param {string} child_path The child path.
* @return {boolean} True if |parent_path| is parent file path of |child_path|.
*/
PathUtil.isParentPath = function(parent_path, child_path) {
if (!parent_path || parent_path.length == 0 ||
!child_path || child_path.length == 0)
return false;
if (parent_path[parent_path.length - 1] != '/')
parent_path += '/';
if (child_path[child_path.length - 1] != '/')
child_path += '/';
return child_path.indexOf(parent_path) == 0;
};
/**
* Return the localized name for the root.
* @param {string} path The full path of the root (starting with slash).
* @return {string} The localized name.
*/
PathUtil.getRootLabel = function(path) {
var str = function(id) {
return loadTimeData.getString(id);
};
if (path === RootDirectory.DOWNLOADS)
return str('DOWNLOADS_DIRECTORY_LABEL');
if (path === RootDirectory.ARCHIVE)
return str('ARCHIVE_DIRECTORY_LABEL');
if (PathUtil.isParentPath(RootDirectory.ARCHIVE, path))
return path.substring(RootDirectory.ARCHIVE.length + 1);
if (path === RootDirectory.REMOVABLE)
return str('REMOVABLE_DIRECTORY_LABEL');
if (PathUtil.isParentPath(RootDirectory.REMOVABLE, path))
return path.substring(RootDirectory.REMOVABLE.length + 1);
// TODO(haruki): Add support for "drive/root" and "drive/other".
if (path === RootDirectory.DRIVE + '/' + DriveSubRootDirectory.ROOT)
return str('DRIVE_DIRECTORY_LABEL');
if (path === RootDirectory.DRIVE_OFFLINE)
return str('DRIVE_OFFLINE_COLLECTION_LABEL');
if (path === RootDirectory.DRIVE_SHARED_WITH_ME)
return str('DRIVE_SHARED_WITH_ME_COLLECTION_LABEL');
if (path === RootDirectory.DRIVE_RECENT)
return str('DRIVE_RECENT_COLLECTION_LABEL');
return path;
};
|