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