Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / workspace / IsolatedFileSystemManager.js
blobe58ab60eb5529dddf09c06ab31011ff6b96d32df
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
17  *
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.
29  */
31 /**
32  * @constructor
33  * @extends {WebInspector.Object}
34  */
35 WebInspector.IsolatedFileSystemManager = function()
37     /** @type {!Object.<string, !WebInspector.IsolatedFileSystem>} */
38     this._fileSystems = {};
39     /** @type {!Object.<string, !Array.<function(?DOMFileSystem)>>} */
40     this._pendingFileSystemRequests = {};
41     this._fileSystemMapping = new WebInspector.FileSystemMapping();
42     this._excludedFolderManager = new WebInspector.ExcludedFolderManager();
43     this._requestFileSystems();
45     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemsLoaded, this._onFileSystemsLoaded, this);
46     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemRemoved, this._onFileSystemRemoved, this);
47     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemAdded, this._onFileSystemAdded, this);
50 /** @typedef {!{fileSystemName: string, rootURL: string, fileSystemPath: string}} */
51 WebInspector.IsolatedFileSystemManager.FileSystem;
53 WebInspector.IsolatedFileSystemManager.Events = {
54     FileSystemAdded: "FileSystemAdded",
55     FileSystemRemoved: "FileSystemRemoved"
58 WebInspector.IsolatedFileSystemManager.prototype = {
59     /**
60      * @return {!WebInspector.FileSystemMapping}
61      */
62     mapping: function()
63     {
64         return this._fileSystemMapping;
65     },
67     /**
68      * @return {!WebInspector.ExcludedFolderManager}
69      */
70     excludedFolderManager: function()
71     {
72         return this._excludedFolderManager;
73     },
75     _requestFileSystems: function()
76     {
77         console.assert(!this._loaded);
78         InspectorFrontendHost.requestFileSystems();
79     },
81     addFileSystem: function()
82     {
83         InspectorFrontendHost.addFileSystem();
84     },
86     /**
87      * @param {string} fileSystemPath
88      */
89     removeFileSystem: function(fileSystemPath)
90     {
91         InspectorFrontendHost.removeFileSystem(fileSystemPath);
92     },
94     /**
95      * @param {!WebInspector.Event} event
96      */
97     _onFileSystemsLoaded: function(event)
98     {
99         var fileSystems = /** @type {!Array.<!WebInspector.IsolatedFileSystemManager.FileSystem>} */ (event.data);
100         var addedFileSystemPaths = {};
101         for (var i = 0; i < fileSystems.length; ++i) {
102             this._innerAddFileSystem(fileSystems[i]);
103             addedFileSystemPaths[fileSystems[i].fileSystemPath] = true;
104         }
105         var fileSystemPaths = this._fileSystemMapping.fileSystemPaths();
106         for (var i = 0; i < fileSystemPaths.length; ++i) {
107             var fileSystemPath = fileSystemPaths[i];
108             if (!addedFileSystemPaths[fileSystemPath])
109                 this._fileSystemRemoved(fileSystemPath);
110         }
112         this._loaded = true;
113         this._processPendingFileSystemRequests();
114     },
116     /**
117      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
118      */
119     _innerAddFileSystem: function(fileSystem)
120     {
121         var fileSystemPath = fileSystem.fileSystemPath;
122         this._fileSystemMapping.addFileSystem(fileSystemPath);
123         var isolatedFileSystem = new WebInspector.IsolatedFileSystem(this, fileSystemPath, fileSystem.fileSystemName, fileSystem.rootURL);
124         this._fileSystems[fileSystemPath] = isolatedFileSystem;
125         this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, isolatedFileSystem);
126     },
128     _processPendingFileSystemRequests: function()
129     {
130         for (var fileSystemPath in this._pendingFileSystemRequests) {
131             var callbacks = this._pendingFileSystemRequests[fileSystemPath];
132             for (var i = 0; i < callbacks.length; ++i)
133                 callbacks[i](this._isolatedFileSystem(fileSystemPath));
134         }
135         delete this._pendingFileSystemRequests;
136     },
138     /**
139      * @param {!WebInspector.Event} event
140      */
141     _onFileSystemAdded: function(event)
142     {
143         var errorMessage = /** @type {string} */ (event.data["errorMessage"]);
144         var fileSystem = /** @type {?WebInspector.IsolatedFileSystemManager.FileSystem} */ (event.data["fileSystem"]);
145         if (errorMessage)
146             WebInspector.console.error(errorMessage, true);
147         else if (fileSystem)
148             this._innerAddFileSystem(fileSystem);
149     },
151     /**
152      * @param {!WebInspector.Event} event
153      */
154     _onFileSystemRemoved: function(event)
155     {
156         this._fileSystemRemoved(/** @type {string} */ (event.data));
157     },
159     /**
160      * @param {string} fileSystemPath
161      */
162     _fileSystemRemoved: function(fileSystemPath)
163     {
164         this._fileSystemMapping.removeFileSystem(fileSystemPath);
165         this._excludedFolderManager.removeFileSystem(fileSystemPath);
166         var isolatedFileSystem = this._fileSystems[fileSystemPath];
167         delete this._fileSystems[fileSystemPath];
168         if (isolatedFileSystem)
169             this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, isolatedFileSystem);
170     },
172     /**
173      * @param {string} fileSystemPath
174      * @return {?DOMFileSystem}
175      */
176     _isolatedFileSystem: function(fileSystemPath)
177     {
178         var fileSystem = this._fileSystems[fileSystemPath];
179         if (!fileSystem)
180             return null;
181         if (!InspectorFrontendHost.isolatedFileSystem)
182             return null;
183         return InspectorFrontendHost.isolatedFileSystem(fileSystem.name(), fileSystem.rootURL());
184     },
186     /**
187      * @param {string} fileSystemPath
188      * @param {function(?DOMFileSystem)} callback
189      */
190     requestDOMFileSystem: function(fileSystemPath, callback)
191     {
192         if (!this._loaded) {
193             if (!this._pendingFileSystemRequests[fileSystemPath])
194                 this._pendingFileSystemRequests[fileSystemPath] = this._pendingFileSystemRequests[fileSystemPath] || [];
195             this._pendingFileSystemRequests[fileSystemPath].push(callback);
196             return;
197         }
198         callback(this._isolatedFileSystem(fileSystemPath));
199     },
201     __proto__: WebInspector.Object.prototype
205  * @type {!WebInspector.IsolatedFileSystemManager}
206  */
207 WebInspector.isolatedFileSystemManager;