Home | History | Annotate | Download | only in bindings
      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.ContentProviderBasedProjectDelegate}
     34  * @param {!WebInspector.Workspace} workspace
     35  * @param {string} projectId
     36  * @param {string} projectName
     37  * @param {!WebInspector.projectTypes} projectType
     38  */
     39 WebInspector.NetworkProjectDelegate = function(workspace, projectId, projectName, projectType)
     40 {
     41     this._name = projectName;
     42     this._id = projectId;
     43     WebInspector.ContentProviderBasedProjectDelegate.call(this, workspace, projectId, projectType);
     44     this._lastUniqueSuffix = 0;
     45 }
     46 
     47 WebInspector.NetworkProjectDelegate.prototype = {
     48     /**
     49      * @return {string}
     50      */
     51     id: function()
     52     {
     53         return this._id;
     54     },
     55 
     56     /**
     57      * @return {string}
     58      */
     59     displayName: function()
     60     {
     61         if (typeof this._displayName !== "undefined")
     62             return this._displayName;
     63         if (!this._name) {
     64             this._displayName = WebInspector.UIString("(no domain)");
     65             return this._displayName;
     66         }
     67         var parsedURL = new WebInspector.ParsedURL(this._name);
     68         if (parsedURL.isValid) {
     69             this._displayName = parsedURL.host + (parsedURL.port ? (":" + parsedURL.port) : "");
     70             if (!this._displayName)
     71                 this._displayName = this._name;
     72         }
     73         else
     74             this._displayName = this._name;
     75         return this._displayName;
     76     },
     77 
     78     /**
     79      * @return {string}
     80      */
     81     url: function()
     82     {
     83         return this._name;
     84     },
     85 
     86     /**
     87      * @param {string} parentPath
     88      * @param {string} name
     89      * @param {string} url
     90      * @param {!WebInspector.ContentProvider} contentProvider
     91      * @return {string}
     92      */
     93     addFile: function(parentPath, name, url, contentProvider)
     94     {
     95         return this.addContentProvider(parentPath, name, url, contentProvider);
     96     },
     97 
     98     __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
     99 }
    100 
    101 /**
    102  * @constructor
    103  * @extends {WebInspector.Object}
    104  * @param {!WebInspector.Workspace} workspace
    105  */
    106 WebInspector.NetworkWorkspaceBinding = function(workspace)
    107 {
    108     this._workspace = workspace;
    109     this._projectDelegates = {};
    110 }
    111 
    112 WebInspector.NetworkWorkspaceBinding.prototype = {
    113     /**
    114      * @param {string} projectName
    115      * @param {boolean} isContentScripts
    116      * @return {!WebInspector.NetworkProjectDelegate}
    117      */
    118     _projectDelegate: function(projectName, isContentScripts)
    119     {
    120         var projectId = (isContentScripts ? "contentscripts:" : "") + projectName;
    121         var projectType = isContentScripts ? WebInspector.projectTypes.ContentScripts : WebInspector.projectTypes.Network;
    122 
    123         if (this._projectDelegates[projectId])
    124             return this._projectDelegates[projectId];
    125         var projectDelegate = new WebInspector.NetworkProjectDelegate(this._workspace, projectId, projectName, projectType);
    126         this._projectDelegates[projectId] = projectDelegate;
    127         return projectDelegate;
    128     },
    129 
    130     /**
    131      * @param {string} url
    132      * @param {!WebInspector.ContentProvider} contentProvider
    133      * @param {boolean=} isContentScript
    134      * @return {!WebInspector.UISourceCode}
    135      */
    136     addFileForURL: function(url, contentProvider, isContentScript)
    137     {
    138         var splitURL = WebInspector.ParsedURL.splitURLIntoPathComponents(url);
    139         var projectName = splitURL[0];
    140         var parentPath = splitURL.slice(1, -1).join("/");
    141         try {
    142             parentPath = parentPath;
    143         } catch (e) { }
    144         var name = splitURL.peekLast() || "";
    145         try {
    146             name = name;
    147         } catch (e) { }
    148         var projectDelegate = this._projectDelegate(projectName, isContentScript || false);
    149         var path = projectDelegate.addFile(parentPath, name, url, contentProvider);
    150         var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (this._workspace.uiSourceCode(projectDelegate.id(), path));
    151         console.assert(uiSourceCode);
    152         return uiSourceCode;
    153     },
    154 
    155     reset: function()
    156     {
    157         for (var projectId in this._projectDelegates)
    158             this._projectDelegates[projectId].reset();
    159         this._projectDelegates = {};
    160     },
    161 
    162     __proto__: WebInspector.Object.prototype
    163 }
    164