Home | History | Annotate | Download | only in sdk
      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     _processPendingFileSystemRequests: function()
    115     {
    116         for (var fileSystemPath in this._pendingFileSystemRequests) {
    117             var callbacks = this._pendingFileSystemRequests[fileSystemPath];
    118             for (var i = 0; i < callbacks.length; ++i)
    119                 callbacks[i](this._isolatedFileSystem(fileSystemPath));
    120         }
    121         delete this._pendingFileSystemRequests;
    122     },
    123 
    124     /**
    125      * @param {string} errorMessage
    126      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
    127      */
    128     _fileSystemAdded: function(errorMessage, fileSystem)
    129     {
    130         var fileSystemPath;
    131         if (errorMessage) {
    132             WebInspector.messageSink.addErrorMessage(errorMessage, true);
    133         } else {
    134             this._innerAddFileSystem(fileSystem);
    135             fileSystemPath = fileSystem.fileSystemPath;
    136         }
    137     },
    138 
    139     /**
    140      * @param {string} fileSystemPath
    141      */
    142     _fileSystemRemoved: function(fileSystemPath)
    143     {
    144         this._fileSystemMapping.removeFileSystem(fileSystemPath);
    145         var isolatedFileSystem = this._fileSystems[fileSystemPath];
    146         delete this._fileSystems[fileSystemPath];
    147         if (isolatedFileSystem)
    148             this.dispatchEventToListeners(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, isolatedFileSystem);
    149     },
    150 
    151     /**
    152      * @param {string} fileSystemPath
    153      * @return {?DOMFileSystem}
    154      */
    155     _isolatedFileSystem: function(fileSystemPath)
    156     {
    157         var fileSystem = this._fileSystems[fileSystemPath];
    158         if (!fileSystem)
    159             return null;
    160         if (!InspectorFrontendHost.isolatedFileSystem)
    161             return null;
    162         return InspectorFrontendHost.isolatedFileSystem(fileSystem.name(), fileSystem.rootURL());
    163     },
    164 
    165     /**
    166      * @param {string} fileSystemPath
    167      * @param {function(?DOMFileSystem)} callback
    168      */
    169     requestDOMFileSystem: function(fileSystemPath, callback)
    170     {
    171         if (!this._loaded) {
    172             if (!this._pendingFileSystemRequests[fileSystemPath])
    173                 this._pendingFileSystemRequests[fileSystemPath] = this._pendingFileSystemRequests[fileSystemPath] || [];
    174             this._pendingFileSystemRequests[fileSystemPath].push(callback);
    175             return;
    176         }
    177         callback(this._isolatedFileSystem(fileSystemPath));
    178     },
    179 
    180     __proto__: WebInspector.Object.prototype
    181 }
    182 
    183 /**
    184  * @type {!WebInspector.IsolatedFileSystemManager}
    185  */
    186 WebInspector.isolatedFileSystemManager;
    187 
    188 /**
    189  * @constructor
    190  * @param {!WebInspector.IsolatedFileSystemManager} IsolatedFileSystemManager
    191  */
    192 WebInspector.IsolatedFileSystemDispatcher = function(IsolatedFileSystemManager)
    193 {
    194     this._IsolatedFileSystemManager = IsolatedFileSystemManager;
    195 }
    196 
    197 WebInspector.IsolatedFileSystemDispatcher.prototype = {
    198     /**
    199      * @param {!Array.<!WebInspector.IsolatedFileSystemManager.FileSystem>} fileSystems
    200      */
    201     fileSystemsLoaded: function(fileSystems)
    202     {
    203         this._IsolatedFileSystemManager._fileSystemsLoaded(fileSystems);
    204     },
    205 
    206     /**
    207      * @param {string} fileSystemPath
    208      */
    209     fileSystemRemoved: function(fileSystemPath)
    210     {
    211         this._IsolatedFileSystemManager._fileSystemRemoved(fileSystemPath);
    212     },
    213 
    214     /**
    215      * @param {string} errorMessage
    216      * @param {!WebInspector.IsolatedFileSystemManager.FileSystem} fileSystem
    217      */
    218     fileSystemAdded: function(errorMessage, fileSystem)
    219     {
    220         this._IsolatedFileSystemManager._fileSystemAdded(errorMessage, fileSystem);
    221     }
    222 }
    223 
    224 /**
    225  * @type {!WebInspector.IsolatedFileSystemDispatcher}
    226  */
    227 WebInspector.isolatedFileSystemDispatcher;
    228