Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / resources / FileSystemModel.js
blob5ded5ad0e7f04f994d4b81229cc82989f13bdab2
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /**
32 * @constructor
33 * @extends {WebInspector.SDKObject}
34 * @param {!WebInspector.Target} target
36 WebInspector.FileSystemModel = function(target)
38 WebInspector.SDKObject.call(this, target);
40 this._fileSystemsForOrigin = {};
42 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
43 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this);
44 this._agent = target.fileSystemAgent();
45 this._agent.enable();
47 this._reset();
50 WebInspector.FileSystemModel.prototype = {
51 _reset: function()
53 for (var securityOrigin in this._fileSystemsForOrigin)
54 this._removeOrigin(securityOrigin);
55 var securityOrigins = this.target().resourceTreeModel.securityOrigins();
56 for (var i = 0; i < securityOrigins.length; ++i)
57 this._addOrigin(securityOrigins[i]);
60 /**
61 * @param {!WebInspector.Event} event
63 _securityOriginAdded: function(event)
65 var securityOrigin = /** @type {string} */ (event.data);
66 this._addOrigin(securityOrigin);
69 /**
70 * @param {!WebInspector.Event} event
72 _securityOriginRemoved: function(event)
74 var securityOrigin = /** @type {string} */ (event.data);
75 this._removeOrigin(securityOrigin);
78 /**
79 * @param {string} securityOrigin
81 _addOrigin: function(securityOrigin)
83 this._fileSystemsForOrigin[securityOrigin] = {};
85 var types = ["persistent", "temporary"];
86 for (var i = 0; i < types.length; ++i)
87 this._requestFileSystemRoot(securityOrigin, types[i], this._fileSystemRootReceived.bind(this, securityOrigin, types[i], this._fileSystemsForOrigin[securityOrigin]));
90 /**
91 * @param {string} securityOrigin
93 _removeOrigin: function(securityOrigin)
95 for (var type in this._fileSystemsForOrigin[securityOrigin]) {
96 var fileSystem = this._fileSystemsForOrigin[securityOrigin][type];
97 delete this._fileSystemsForOrigin[securityOrigin][type];
98 this._fileSystemRemoved(fileSystem);
100 delete this._fileSystemsForOrigin[securityOrigin];
104 * @param {string} origin
105 * @param {string} type
106 * @param {function(number, !FileSystemAgent.Entry=)} callback
108 _requestFileSystemRoot: function(origin, type, callback)
111 * @param {?Protocol.Error} error
112 * @param {number} errorCode
113 * @param {!FileSystemAgent.Entry=} backendRootEntry
115 function innerCallback(error, errorCode, backendRootEntry)
117 if (error) {
118 callback(FileError.SECURITY_ERR);
119 return;
122 callback(errorCode, backendRootEntry);
125 this._agent.requestFileSystemRoot(origin, type, innerCallback);
129 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
131 _fileSystemAdded: function(fileSystem)
133 this.dispatchEventToListeners(WebInspector.FileSystemModel.EventTypes.FileSystemAdded, fileSystem);
137 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
139 _fileSystemRemoved: function(fileSystem)
141 this.dispatchEventToListeners(WebInspector.FileSystemModel.EventTypes.FileSystemRemoved, fileSystem);
144 refreshFileSystemList: function()
146 this._reset();
150 * @param {string} origin
151 * @param {string} type
152 * @param {!Object.<string, !WebInspector.FileSystemModel.FileSystem>} store
153 * @param {number} errorCode
154 * @param {!FileSystemAgent.Entry=} backendRootEntry
156 _fileSystemRootReceived: function(origin, type, store, errorCode, backendRootEntry)
158 if (!errorCode && backendRootEntry && this._fileSystemsForOrigin[origin] === store) {
159 var fileSystem = new WebInspector.FileSystemModel.FileSystem(this, origin, type, backendRootEntry);
160 store[type] = fileSystem;
161 this._fileSystemAdded(fileSystem);
166 * @param {!WebInspector.FileSystemModel.Directory} directory
167 * @param {function(number, !Array.<!WebInspector.FileSystemModel.Entry>=)} callback
169 requestDirectoryContent: function(directory, callback)
171 this._requestDirectoryContent(directory.url, this._directoryContentReceived.bind(this, directory, callback));
175 * @param {string} url
176 * @param {function(number, !Array.<!FileSystemAgent.Entry>=)} callback
178 _requestDirectoryContent: function(url, callback)
181 * @param {?Protocol.Error} error
182 * @param {number} errorCode
183 * @param {!Array.<!FileSystemAgent.Entry>=} backendEntries
185 function innerCallback(error, errorCode, backendEntries)
187 if (error) {
188 callback(FileError.SECURITY_ERR);
189 return;
192 if (errorCode !== 0) {
193 callback(errorCode);
194 return;
197 callback(errorCode, backendEntries);
200 this._agent.requestDirectoryContent(url, innerCallback);
204 * @param {!WebInspector.FileSystemModel.Directory} parentDirectory
205 * @param {function(number, !Array.<!WebInspector.FileSystemModel.Entry>=)} callback
206 * @param {number} errorCode
207 * @param {!Array.<!FileSystemAgent.Entry>=} backendEntries
209 _directoryContentReceived: function(parentDirectory, callback, errorCode, backendEntries)
211 if (!backendEntries) {
212 callback(errorCode);
213 return;
216 var entries = [];
217 for (var i = 0; i < backendEntries.length; ++i) {
218 if (backendEntries[i].isDirectory)
219 entries.push(new WebInspector.FileSystemModel.Directory(this, parentDirectory.fileSystem, backendEntries[i]));
220 else
221 entries.push(new WebInspector.FileSystemModel.File(this, parentDirectory.fileSystem, backendEntries[i]));
224 callback(errorCode, entries);
228 * @param {!WebInspector.FileSystemModel.Entry} entry
229 * @param {function(number, !FileSystemAgent.Metadata=)} callback
231 requestMetadata: function(entry, callback)
234 * @param {?Protocol.Error} error
235 * @param {number} errorCode
236 * @param {!FileSystemAgent.Metadata=} metadata
238 function innerCallback(error, errorCode, metadata)
240 if (error) {
241 callback(FileError.SECURITY_ERR);
242 return;
245 callback(errorCode, metadata);
248 this._agent.requestMetadata(entry.url, innerCallback);
252 * @param {!WebInspector.FileSystemModel.File} file
253 * @param {boolean} readAsText
254 * @param {number=} start
255 * @param {number=} end
256 * @param {string=} charset
257 * @param {function(number, string=, string=)=} callback
259 requestFileContent: function(file, readAsText, start, end, charset, callback)
261 this._requestFileContent(file.url, readAsText, start, end, charset, callback);
265 * @param {string} url
266 * @param {boolean} readAsText
267 * @param {number=} start
268 * @param {number=} end
269 * @param {string=} charset
270 * @param {function(number, string=, string=)=} callback
272 _requestFileContent: function(url, readAsText, start, end, charset, callback)
275 * @param {?Protocol.Error} error
276 * @param {number} errorCode
277 * @param {string=} content
278 * @param {string=} charset
280 function innerCallback(error, errorCode, content, charset)
282 if (error) {
283 if (callback)
284 callback(FileError.SECURITY_ERR);
285 return;
288 if (callback)
289 callback(errorCode, content, charset);
292 this._agent.requestFileContent(url, readAsText, start, end, charset, innerCallback);
295 * @param {!WebInspector.FileSystemModel.Entry} entry
296 * @param {function(number)=} callback
298 deleteEntry: function(entry, callback)
300 var fileSystemModel = this;
301 if (entry === entry.fileSystem.root)
302 this._deleteEntry(entry.url, hookFileSystemDeletion);
303 else
304 this._deleteEntry(entry.url, callback);
306 function hookFileSystemDeletion(errorCode)
308 callback(errorCode);
309 if (!errorCode)
310 fileSystemModel._removeFileSystem(entry.fileSystem);
315 * @param {string} url
316 * @param {function(number)=} callback
318 _deleteEntry: function(url, callback)
321 * @param {?Protocol.Error} error
322 * @param {number} errorCode
324 function innerCallback(error, errorCode)
326 if (error) {
327 if (callback)
328 callback(FileError.SECURITY_ERR);
329 return;
332 if (callback)
333 callback(errorCode);
336 this._agent.deleteEntry(url, innerCallback);
340 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
342 _removeFileSystem: function(fileSystem)
344 var origin = fileSystem.origin;
345 var type = fileSystem.type;
346 if (this._fileSystemsForOrigin[origin] && this._fileSystemsForOrigin[origin][type]) {
347 delete this._fileSystemsForOrigin[origin][type];
348 this._fileSystemRemoved(fileSystem);
350 if (Object.isEmpty(this._fileSystemsForOrigin[origin]))
351 delete this._fileSystemsForOrigin[origin];
355 __proto__: WebInspector.SDKObject.prototype
359 WebInspector.FileSystemModel.EventTypes = {
360 FileSystemAdded: "FileSystemAdded",
361 FileSystemRemoved: "FileSystemRemoved"
365 * @constructor
366 * @param {!WebInspector.FileSystemModel} fileSystemModel
367 * @param {string} origin
368 * @param {string} type
369 * @param {!FileSystemAgent.Entry} backendRootEntry
371 WebInspector.FileSystemModel.FileSystem = function(fileSystemModel, origin, type, backendRootEntry)
373 this.origin = origin;
374 this.type = type;
376 this.root = new WebInspector.FileSystemModel.Directory(fileSystemModel, this, backendRootEntry);
379 WebInspector.FileSystemModel.FileSystem.prototype = {
381 * @type {string}
383 get name()
385 return "filesystem:" + this.origin + "/" + this.type;
390 * @constructor
391 * @param {!WebInspector.FileSystemModel} fileSystemModel
392 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
393 * @param {!FileSystemAgent.Entry} backendEntry
395 WebInspector.FileSystemModel.Entry = function(fileSystemModel, fileSystem, backendEntry)
397 this._fileSystemModel = fileSystemModel;
398 this._fileSystem = fileSystem;
400 this._url = backendEntry.url;
401 this._name = backendEntry.name;
402 this._isDirectory = backendEntry.isDirectory;
406 * @param {!WebInspector.FileSystemModel.Entry} x
407 * @param {!WebInspector.FileSystemModel.Entry} y
408 * @return {number}
410 WebInspector.FileSystemModel.Entry.compare = function(x, y)
412 if (x.isDirectory != y.isDirectory)
413 return y.isDirectory ? 1 : -1;
414 return x.name.compareTo(y.name);
417 WebInspector.FileSystemModel.Entry.prototype = {
419 * @type {!WebInspector.FileSystemModel}
421 get fileSystemModel()
423 return this._fileSystemModel;
427 * @type {!WebInspector.FileSystemModel.FileSystem}
429 get fileSystem()
431 return this._fileSystem;
435 * @type {string}
437 get url()
439 return this._url;
443 * @type {string}
445 get name()
447 return this._name;
451 * @type {boolean}
453 get isDirectory()
455 return this._isDirectory;
459 * @param {function(number, !FileSystemAgent.Metadata)} callback
461 requestMetadata: function(callback)
463 this.fileSystemModel.requestMetadata(this, callback);
467 * @param {function(number)} callback
469 deleteEntry: function(callback)
471 this.fileSystemModel.deleteEntry(this, callback);
476 * @constructor
477 * @extends {WebInspector.FileSystemModel.Entry}
478 * @param {!WebInspector.FileSystemModel} fileSystemModel
479 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
480 * @param {!FileSystemAgent.Entry} backendEntry
482 WebInspector.FileSystemModel.Directory = function(fileSystemModel, fileSystem, backendEntry)
484 WebInspector.FileSystemModel.Entry.call(this, fileSystemModel, fileSystem, backendEntry);
487 WebInspector.FileSystemModel.Directory.prototype = {
489 * @param {function(number, !Array.<!WebInspector.FileSystemModel.Directory>)} callback
491 requestDirectoryContent: function(callback)
493 this.fileSystemModel.requestDirectoryContent(this, callback);
496 __proto__: WebInspector.FileSystemModel.Entry.prototype
500 * @constructor
501 * @extends {WebInspector.FileSystemModel.Entry}
502 * @param {!WebInspector.FileSystemModel} fileSystemModel
503 * @param {!WebInspector.FileSystemModel.FileSystem} fileSystem
504 * @param {!FileSystemAgent.Entry} backendEntry
506 WebInspector.FileSystemModel.File = function(fileSystemModel, fileSystem, backendEntry)
508 WebInspector.FileSystemModel.Entry.call(this, fileSystemModel, fileSystem, backendEntry);
510 this._mimeType = backendEntry.mimeType;
511 this._resourceType = WebInspector.resourceTypes[backendEntry.resourceType];
512 this._isTextFile = backendEntry.isTextFile;
515 WebInspector.FileSystemModel.File.prototype = {
517 * @type {string}
519 get mimeType()
521 return this._mimeType;
525 * @type {!WebInspector.ResourceType}
527 get resourceType()
529 return this._resourceType;
533 * @type {boolean}
535 get isTextFile()
537 return this._isTextFile;
541 * @param {boolean} readAsText
542 * @param {number=} start
543 * @param {number=} end
544 * @param {string=} charset
545 * @param {function(number, string=)=} callback
547 requestFileContent: function(readAsText, start, end, charset, callback)
549 this.fileSystemModel.requestFileContent(this, readAsText, start, end, charset, callback);
552 __proto__: WebInspector.FileSystemModel.Entry.prototype