Home | History | Annotate | Download | only in workspace
      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     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemsLoaded, this._onFileSystemsLoaded, this);
     45     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemRemoved, this._onFileSystemRemoved, this);
     46     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FileSystemAdded, this._onFileSystemAdded, this);
     47 }
     48 
     49 /** @typedef {!{fileSystemName: string, rootURL: string, fileSystemPath: string}} */
     50 WebInspector.IsolatedFileSystemManager.FileSystem;
     51 
     52 WebInspector.IsolatedFileSystemManager.Events = {
     53     FileSystemAdded: "FileSystemAdded",
     54     FileSystemRemoved: "FileSystemRemoved"
     55 }
     56 
     57 WebInspector.IsolatedFileSystemManager.prototype = {
     58     /**
     59      * @return {!WebInspector.FileSystemMapping}
     60      */
     61     mapping: function()
     62     {
     63         return this._fileSystemMapping;
     64     },
     65 
     66     _requestFileSystems: function()
     67     {
     68         console.assert(!this._loaded);
     69         InspectorFrontendHost.requestFileSystems();
     70     },
     71 
     72     addFileSystem: function()
     73     {
     74         InspectorFrontendHost.addFileSystem();
     75     },
     76 
     77     /**
     78      * @param {string} fileSystemPath
     79      */
     80     removeFileSystem: function(fileSystemPath)
     81     {
     82         InspectorFrontendHost.removeFileSystem(fileSystemPath);
     83     },
     84 
     85     /**
     86      * @param {!WebInspector.Event} event
     87      */
     88     _onFileSystemsLoaded: function(event)
     89     {
     90         var fileSystems = /** @type {!Array.<!WebInspector.IsolatedFileSystemManager.FileSystem>} */ (event.data);
     91         var addedFileSystemPaths = {};
     92         for (var i = 0; i < fileSystems.length; ++i) {
     93             this._innerAddFileSystem(fileSystems[i]);
     94             addedFileSystemPaths[fileSystems[i].fileSystemPath] = true;
     95         }
     96         var fileSystemPaths = this._fileSystemMapping.fileSystemPaths();
     97         for (var i = 0; i < fileSystemPaths.length; ++i) {
     98             var fileSystemPath = fileSystemPaths[i];
     99             if (!addedFileSystemPaths[fileSystemPath])
    100                 this._fileSystemRemoved(fileSystemPath);
    101         }
    102 
    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.addFileSystem(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     _processPendingFileSystemRequests: function()
    120     {
    121         for (var fileSystemPath in this._pendingFileSystemRequests) {
    122             var callbacks = this._pendingFileSystemRequests[fileSystemPath];
    123             for (var i = 0; i < callbacks.length; ++i)
    124                 callbacks[i](this._isolatedFileSystem(fileSystemPath));
    125         }
    126         delete this._pendingFileSystemRequests;
    127     },
    128 
    129     /**
    130      * @param {!WebInspector.Event} event
    131      */
    132     _onFileSystemAdded: function(event)
    133     {
    134         var errorMessage = /** @type {string} */ (event.data["errorMessage"]);
    135         var fileSystem = /** @type {!WebInspector.IsolatedFileSystemManager.FileSystem} */ (event.data["fileSystem"]);
    136         var fileSystemPath;
    137         if (errorMessage) {
    138             WebInspector.console.error(errorMessage, true);
    139         } else {
    140             this._innerAddFileSystem(fileSystem);
    141             fileSystemPath = fileSystem.fileSystemPath;
    142         }
    143     },
    144 
    145     /**
    146      * @param {!WebInspector.Event} event
    147      */
    148     _onFileSystemRemoved: function(event)
    149     {
    150         this._fileSystemRemoved(/** @type {string} */ (event.data));
    151     },
    152 
    153     /**
    154      * @param {string} fileSystemPath
    155      */
    156     _fileSystemRemoved: function(fileSystemPath)
    157     {
    158         this._fileSystemMapping.removeFileSystem(fileSystemPath);
    159         var isolatedFileSystem = this._fileSystems[fileSystemPath];
    160         delete this._fileSystems[fileSystemPath];
    161         if (isolatedFileSystem)
    162             this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, isolatedFileSystem);
    163     },
    164 
    165     /**
    166      * @param {string} fileSystemPath
    167      * @return {?DOMFileSystem}
    168      */
    169     _isolatedFileSystem: function(fileSystemPath)
    170     {
    171         var fileSystem = this._fileSystems[fileSystemPath];
    172         if (!fileSystem)
    173             return null;
    174         if (!InspectorFrontendHost.isolatedFileSystem)
    175             return null;
    176         return InspectorFrontendHost.isolatedFileSystem(fileSystem.name(), fileSystem.rootURL());
    177     },
    178 
    179     /**
    180      * @param {string} fileSystemPath
    181      * @param {function(?DOMFileSystem)} callback
    182      */
    183     requestDOMFileSystem: function(fileSystemPath, callback)
    184     {
    185         if (!this._loaded) {
    186             if (!this._pendingFileSystemRequests[fileSystemPath])
    187                 this._pendingFileSystemRequests[fileSystemPath] = this._pendingFileSystemRequests[fileSystemPath] || [];
    188             this._pendingFileSystemRequests[fileSystemPath].push(callback);
    189             return;
    190         }
    191         callback(this._isolatedFileSystem(fileSystemPath));
    192     },
    193 
    194     __proto__: WebInspector.Object.prototype
    195 }
    196 
    197 /**
    198  * @type {!WebInspector.IsolatedFileSystemManager}
    199  */
    200 WebInspector.isolatedFileSystemManager;
    201