Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / IsolatedFileSystemManager.js
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  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.Object}
34  */
35 WebInspector.IsolatedFileSystemManager = function()
36 {
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._requestFileSystems();
43 }
44
45 /** @typedef {!{fileSystemName: string, rootURL: string, fileSystemPath: string}} */
46 WebInspector.IsolatedFileSystemManager.FileSystem;
47
48 WebInspector.IsolatedFileSystemManager.Events = {
49     FileSystemAdded: "FileSystemAdded",
50     FileSystemRemoved: "FileSystemRemoved"
51 }
52
53 WebInspector.IsolatedFileSystemManager.prototype = {
54     /**
55      * @return {!WebInspector.FileSystemMapping}
56      */
57     mapping: function()
58     {
59         return this._fileSystemMapping;
60     },
61
62     _requestFileSystems: function()
63     {
64         console.assert(!this._loaded);
65         InspectorFrontendHost.requestFileSystems();
66     },
67
68     addFileSystem: function()
69     {
70         InspectorFrontendHost.addFileSystem();
71     },
72
73     /**
74      * @param {string} fileSystemPath
75      */
76     removeFileSystem: function(fileSystemPath)
77     {
78         InspectorFrontendHost.removeFileSystem(fileSystemPath);
79     },
80
81     /**
82      * @param {!Array.<!WebInspector.IsolatedFileSystemManager.FileSystem>} fileSystems
83      */
84     _fileSystemsLoaded: function(fileSystems)
85     {
86         var addedFileSystemPaths = {};
87         for (var i = 0; i < fileSystems.length; ++i) {
88             this._innerAddFileSystem(fileSystems[i]);
89             addedFileSystemPaths[fileSystems[i].fileSystemPath] = true;
90         }
91         var fileSystemPaths = this._fileSystemMapping.fileSystemPaths();
92         for (var i = 0; i < fileSystemPaths.length; ++i) {
93             var fileSystemPath = fileSystemPaths[i];
94             if (!addedFileSystemPaths[fileSystemPath])
95                 this._fileSystemRemoved(fileSystemPath);
96         }
97
98         this._loaded = true;
99         this._processPendingFileSystemRequests();
100     },
101
102     /**
103      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
104      */
105     _innerAddFileSystem: function(fileSystem)
106     {
107         var fileSystemPath = fileSystem.fileSystemPath;
108         this._fileSystemMapping.addFileSystem(fileSystemPath);
109         var isolatedFileSystem = new WebInspector.IsolatedFileSystem(this, fileSystemPath, fileSystem.fileSystemName, fileSystem.rootURL);
110         this._fileSystems[fileSystemPath] = isolatedFileSystem;
111         this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, isolatedFileSystem);
112     },
113
114     /**
115      * @return {!Array.<string>}
116      */
117     _fileSystemPaths: function()
118     {
119         return Object.keys(this._fileSystems);
120     },
121
122     _processPendingFileSystemRequests: function()
123     {
124         for (var fileSystemPath in this._pendingFileSystemRequests) {
125             var callbacks = this._pendingFileSystemRequests[fileSystemPath];
126             for (var i = 0; i < callbacks.length; ++i)
127                 callbacks[i](this._isolatedFileSystem(fileSystemPath));
128         }
129         delete this._pendingFileSystemRequests;
130     },
131
132     /**
133      * @param {string} errorMessage
134      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
135      */
136     _fileSystemAdded: function(errorMessage, fileSystem)
137     {
138         var fileSystemPath;
139         if (errorMessage)
140             WebInspector.showErrorMessage(errorMessage)
141         else if (fileSystem) {
142             this._innerAddFileSystem(fileSystem);
143             fileSystemPath = fileSystem.fileSystemPath;
144         }
145     },
146
147     /**
148      * @param {string} fileSystemPath
149      */
150     _fileSystemRemoved: function(fileSystemPath)
151     {
152         this._fileSystemMapping.removeFileSystem(fileSystemPath);
153         var isolatedFileSystem = this._fileSystems[fileSystemPath];
154         delete this._fileSystems[fileSystemPath];
155         if (isolatedFileSystem)
156             this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, isolatedFileSystem);
157     },
158
159     /**
160      * @param {string} fileSystemPath
161      * @return {?DOMFileSystem}
162      */
163     _isolatedFileSystem: function(fileSystemPath)
164     {
165         var fileSystem = this._fileSystems[fileSystemPath];
166         if (!fileSystem)
167             return null;
168         if (!InspectorFrontendHost.isolatedFileSystem)
169             return null;
170         return InspectorFrontendHost.isolatedFileSystem(fileSystem.name(), fileSystem.rootURL());
171     },
172
173     /**
174      * @param {string} fileSystemPath
175      * @param {function(?DOMFileSystem)} callback
176      */
177     requestDOMFileSystem: function(fileSystemPath, callback)
178     {
179         if (!this._loaded) {
180             if (!this._pendingFileSystemRequests[fileSystemPath])
181                 this._pendingFileSystemRequests[fileSystemPath] = this._pendingFileSystemRequests[fileSystemPath] || [];
182             this._pendingFileSystemRequests[fileSystemPath].push(callback);
183             return;
184         }
185         callback(this._isolatedFileSystem(fileSystemPath));
186     },
187
188     __proto__: WebInspector.Object.prototype
189 }
190
191 /**
192  * @type {!WebInspector.IsolatedFileSystemManager}
193  */
194 WebInspector.isolatedFileSystemManager;
195
196 /**
197  * @constructor
198  * @param {!WebInspector.IsolatedFileSystemManager} IsolatedFileSystemManager
199  */
200 WebInspector.IsolatedFileSystemDispatcher = function(IsolatedFileSystemManager)
201 {
202     this._IsolatedFileSystemManager = IsolatedFileSystemManager;
203 }
204
205 WebInspector.IsolatedFileSystemDispatcher.prototype = {
206     /**
207      * @param {!Array.<!WebInspector.IsolatedFileSystemManager.FileSystem>} fileSystems
208      */
209     fileSystemsLoaded: function(fileSystems)
210     {
211         this._IsolatedFileSystemManager._fileSystemsLoaded(fileSystems);
212     },
213
214     /**
215      * @param {string} fileSystemPath
216      */
217     fileSystemRemoved: function(fileSystemPath)
218     {
219         this._IsolatedFileSystemManager._fileSystemRemoved(fileSystemPath);
220     },
221
222     /**
223      * @param {string} errorMessage
224      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
225      */
226     fileSystemAdded: function(errorMessage, fileSystem)
227     {
228         this._IsolatedFileSystemManager._fileSystemAdded(errorMessage, fileSystem);
229     }
230 }
231
232 /**
233  * @type {!WebInspector.IsolatedFileSystemDispatcher}
234  */
235 WebInspector.isolatedFileSystemDispatcher;