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 */
35WebInspector.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.FileSystemMappingImpl();
42
43    if (this.supportsFileSystems())
44        this._requestFileSystems();
45}
46
47/** @typedef {{fileSystemName: string, rootURL: string, fileSystemPath: string}} */
48WebInspector.IsolatedFileSystemManager.FileSystem;
49
50WebInspector.IsolatedFileSystemManager.Events = {
51    FileSystemAdded: "FileSystemAdded",
52    FileSystemRemoved: "FileSystemRemoved"
53}
54
55WebInspector.IsolatedFileSystemManager.prototype = {
56    /**
57     * @return {WebInspector.FileSystemMapping}
58     */
59    mapping: function()
60    {
61        return this._fileSystemMapping;
62    },
63
64    /**
65     * @return {boolean}
66     */
67    supportsFileSystems: function()
68    {
69        return InspectorFrontendHost.supportsFileSystems();
70    },
71
72    _requestFileSystems: function()
73    {
74        console.assert(!this._loaded);
75        InspectorFrontendHost.requestFileSystems();
76    },
77
78    /**
79     * @param {function(?string)} callback
80     */
81    addFileSystem: function(callback)
82    {
83        this._selectFileSystemPathCallback = callback;
84        InspectorFrontendHost.addFileSystem();
85    },
86
87    /**
88     * @param {function()} callback
89     */
90    removeFileSystem: function(fileSystemPath, callback)
91    {
92        this._removeFileSystemCallback = callback;
93        InspectorFrontendHost.removeFileSystem(fileSystemPath);
94    },
95
96    /**
97     * @param {Array.<WebInspector.IsolatedFileSystemManager.FileSystem>} fileSystems
98     */
99    _fileSystemsLoaded: function(fileSystems)
100    {
101        for (var i = 0; i < fileSystems.length; ++i)
102            this._innerAddFileSystem(fileSystems[i]);
103        this._loaded = true;
104        this._processPendingFileSystemRequests();
105    },
106
107    /**
108     * @param {WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
109     */
110    _innerAddFileSystem: function(fileSystem)
111    {
112        var fileSystemPath = fileSystem.fileSystemPath;
113        this._fileSystemMapping.addFileSystemMapping(fileSystemPath);
114        var isolatedFileSystem = new WebInspector.IsolatedFileSystem(this, fileSystemPath, fileSystem.fileSystemName, fileSystem.rootURL);
115        this._fileSystems[fileSystemPath] = isolatedFileSystem;
116        this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, isolatedFileSystem);
117    },
118
119    /**
120     * @return {Array.<string>}
121     */
122    _fileSystemPaths: function()
123    {
124        return Object.keys(this._fileSystems);
125    },
126
127    _processPendingFileSystemRequests: function()
128    {
129        for (var fileSystemPath in this._pendingFileSystemRequests) {
130            var callbacks = this._pendingFileSystemRequests[fileSystemPath];
131            for (var i = 0; i < callbacks.length; ++i)
132                callbacks[i](this._isolatedFileSystem(fileSystemPath));
133        }
134        delete this._pendingFileSystemRequests;
135    },
136
137    /**
138     * @param {string} errorMessage
139     * @param {WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
140     */
141    _fileSystemAdded: function(errorMessage, fileSystem)
142    {
143        var fileSystemPath;
144        if (errorMessage)
145            WebInspector.showErrorMessage(errorMessage)
146        else if (fileSystem) {
147            this._innerAddFileSystem(fileSystem);
148            fileSystemPath = fileSystem.fileSystemPath;
149        }
150
151        if (this._selectFileSystemPathCallback) {
152            this._selectFileSystemPathCallback(fileSystemPath);
153            delete this._selectFileSystemPathCallback;
154        }
155    },
156
157    /**
158     * @param {string} fileSystemPath
159     */
160    _fileSystemRemoved: function(fileSystemPath)
161    {
162        this._fileSystemMapping.removeFileSystemMapping(fileSystemPath);
163        var isolatedFileSystem = this._fileSystems[fileSystemPath];
164        delete this._fileSystems[fileSystemPath];
165        if (this._removeFileSystemCallback) {
166            this._removeFileSystemCallback(fileSystemPath);
167            delete this._removeFileSystemCallback;
168        }
169        this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, isolatedFileSystem);
170    },
171
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    },
185
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    },
200
201    __proto__: WebInspector.Object.prototype
202}
203
204/**
205 * @type {?WebInspector.IsolatedFileSystemManager}
206 */
207WebInspector.isolatedFileSystemManager = null;
208
209/**
210 * @constructor
211 * @param {WebInspector.IsolatedFileSystemManager} IsolatedFileSystemManager
212 */
213WebInspector.IsolatedFileSystemDispatcher = function(IsolatedFileSystemManager)
214{
215    this._IsolatedFileSystemManager = IsolatedFileSystemManager;
216}
217
218WebInspector.IsolatedFileSystemDispatcher.prototype = {
219    /**
220     * @param {Array.<WebInspector.IsolatedFileSystemManager.FileSystem>} fileSystems
221     */
222    fileSystemsLoaded: function(fileSystems)
223    {
224        this._IsolatedFileSystemManager._fileSystemsLoaded(fileSystems);
225    },
226
227    /**
228     * @param {string} fileSystemPath
229     */
230    fileSystemRemoved: function(fileSystemPath)
231    {
232        this._IsolatedFileSystemManager._fileSystemRemoved(fileSystemPath);
233    },
234
235    /**
236     * @param {string} errorMessage
237     * @param {WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
238     */
239    fileSystemAdded: function(errorMessage, fileSystem)
240    {
241        this._IsolatedFileSystemManager._fileSystemAdded(errorMessage, fileSystem);
242    }
243}
244
245/**
246 * @type {?WebInspector.IsolatedFileSystemDispatcher}
247 */
248WebInspector.isolatedFileSystemDispatcher = null;
249