Home | History | Annotate | Download | only in front_end
      1 /*
      2  * Copyright (C) 2011 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 var InspectorFrontendAPI = {
     32     _pendingCommands: [],
     33 
     34     // Methods called by the embedder on load, potentially before front-end is initialized.
     35     //////////////////////////////////////////////////////////////////////////////////////////////////
     36 
     37     showConsole: function()
     38     {
     39         InspectorFrontendAPI._runOnceLoaded(function() {
     40             WebInspector.inspectorView.showPanel("console");
     41         });
     42     },
     43 
     44     enterInspectElementMode: function()
     45     {
     46         InspectorFrontendAPI._runOnceLoaded(function() {
     47             WebInspector.inspectorView.showPanel("elements");
     48             if (WebInspector.inspectElementModeController)
     49                 WebInspector.inspectElementModeController.toggleSearch();
     50         });
     51     },
     52 
     53     /**
     54      * Focus on a particular line in the specified resource.
     55      * @param {string} url The url to the resource.
     56      * @param {number} lineNumber The line number to focus on.
     57      * @param {number} columnNumber The column number to focus on.
     58      */
     59     revealSourceLine: function(url, lineNumber, columnNumber)
     60     {
     61         InspectorFrontendAPI._runOnceLoaded(function() {
     62             var uiSourceCode = WebInspector.workspace.uiSourceCodeForURL(url);
     63             if (uiSourceCode) {
     64                 WebInspector.Revealer.reveal(uiSourceCode.uiLocation(lineNumber, columnNumber));
     65                 return;
     66             }
     67 
     68             /**
     69              * @param {!WebInspector.Event} event
     70              */
     71             function listener(event)
     72             {
     73                 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
     74                 if (uiSourceCode.url === url) {
     75                     WebInspector.Revealer.reveal(uiSourceCode.uiLocation(lineNumber, columnNumber));
     76                     WebInspector.workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, listener);
     77                 }
     78             }
     79 
     80             WebInspector.workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, listener);
     81         });
     82     },
     83 
     84     /**
     85      * @param {string} backgroundColor
     86      * @param {string} color
     87      */
     88     setToolbarColors: function(backgroundColor, color)
     89     {
     90         WebInspector.setToolbarColors(backgroundColor, color);
     91     },
     92 
     93     /**
     94      * @param {string} url
     95      */
     96     loadTimelineFromURL: function(url)
     97     {
     98         InspectorFrontendAPI._runOnceLoaded(function() {
     99             /** @type {!WebInspector.TimelinePanel} */ (WebInspector.inspectorView.showPanel("timeline")).loadFromURL(url);
    100         });
    101     },
    102 
    103     /**
    104      * @param {boolean} useSoftMenu
    105      */
    106     setUseSoftMenu: function(useSoftMenu)
    107     {
    108         WebInspector.ContextMenu.setUseSoftMenu(useSoftMenu);
    109     },
    110 
    111     dispatchMessage: function(messageObject)
    112     {
    113         InspectorBackend.connection().dispatch(messageObject);
    114     },
    115 
    116     // Callbacks to the methods called from within initialized front-end.
    117     //////////////////////////////////////////////////////////////////////////////////////////////////
    118 
    119     contextMenuItemSelected: function(id)
    120     {
    121         WebInspector.contextMenuItemSelected(id);
    122     },
    123 
    124     contextMenuCleared: function()
    125     {
    126         WebInspector.contextMenuCleared();
    127     },
    128 
    129     fileSystemsLoaded: function(fileSystems)
    130     {
    131         WebInspector.isolatedFileSystemDispatcher.fileSystemsLoaded(fileSystems);
    132     },
    133 
    134     fileSystemRemoved: function(fileSystemPath)
    135     {
    136         WebInspector.isolatedFileSystemDispatcher.fileSystemRemoved(fileSystemPath);
    137     },
    138 
    139     fileSystemAdded: function(errorMessage, fileSystem)
    140     {
    141         WebInspector.isolatedFileSystemDispatcher.fileSystemAdded(errorMessage, fileSystem);
    142     },
    143 
    144     /**
    145      * @param {number} requestId
    146      * @param {string} fileSystemPath
    147      * @param {number} totalWork
    148      */
    149     indexingTotalWorkCalculated: function(requestId, fileSystemPath, totalWork)
    150     {
    151         WebInspector.fileSystemWorkspaceBinding.indexingTotalWorkCalculated(requestId, fileSystemPath, totalWork);
    152     },
    153 
    154     /**
    155      * @param {number} requestId
    156      * @param {string} fileSystemPath
    157      * @param {number} worked
    158      */
    159     indexingWorked: function(requestId, fileSystemPath, worked)
    160     {
    161         WebInspector.fileSystemWorkspaceBinding.indexingWorked(requestId, fileSystemPath, worked);
    162     },
    163 
    164     /**
    165      * @param {number} requestId
    166      * @param {string} fileSystemPath
    167      */
    168     indexingDone: function(requestId, fileSystemPath)
    169     {
    170         WebInspector.fileSystemWorkspaceBinding.indexingDone(requestId, fileSystemPath);
    171     },
    172 
    173     /**
    174      * @param {number} requestId
    175      * @param {string} fileSystemPath
    176      * @param {!Array.<string>} files
    177      */
    178     searchCompleted: function(requestId, fileSystemPath, files)
    179     {
    180         WebInspector.fileSystemWorkspaceBinding.searchCompleted(requestId, fileSystemPath, files);
    181     },
    182 
    183     /**
    184      * @param {!InspectorFrontendAPI.ForwardedKeyboardEvent} event
    185      */
    186     keyEventUnhandled: function(event)
    187     {
    188         InspectorFrontendAPI._runOnceLoaded(function() {
    189             WebInspector.forwardedEventHandler.keyEventReceived(event.type, event.keyIdentifier, event.keyCode, event.modifiers);
    190         });
    191     },
    192 
    193     /**
    194      * @param {string} eventType
    195      * @param {*=} eventData
    196      * @return {boolean}
    197      */
    198     dispatchEventToListeners: function(eventType, eventData)
    199     {
    200         return WebInspector.inspectorFrontendEventSink.dispatchEventToListeners(eventType, eventData);
    201     },
    202 
    203     /**
    204      * @param {string} url
    205      */
    206     savedURL: function(url)
    207     {
    208         WebInspector.fileManager.savedURL(url);
    209     },
    210 
    211     /**
    212      * @param {string} url
    213      */
    214     canceledSaveURL: function(url)
    215     {
    216         WebInspector.fileManager.canceledSaveURL(url);
    217     },
    218 
    219     /**
    220      * @param {string} url
    221      */
    222     appendedToURL: function(url)
    223     {
    224         WebInspector.fileManager.appendedToURL(url);
    225     },
    226 
    227     /**
    228      * @param {number} id
    229      * @param {?string} error
    230      */
    231     embedderMessageAck: function(id, error)
    232     {
    233         InspectorFrontendHost.embedderMessageAck(id, error);
    234     },
    235 
    236     // Called from within front-end
    237     ///////////////////////////////
    238 
    239     loadCompleted: function()
    240     {
    241         InspectorFrontendAPI._isLoaded = true;
    242         for (var i = 0; i < InspectorFrontendAPI._pendingCommands.length; ++i)
    243             InspectorFrontendAPI._pendingCommands[i]();
    244         InspectorFrontendAPI._pendingCommands = [];
    245         if (window.opener)
    246             window.opener.postMessage(["loadCompleted"], "*");
    247     },
    248 
    249     // Implementation details
    250     /////////////////////////
    251 
    252     /**
    253      * @param {function()} command
    254      */
    255     _runOnceLoaded: function(command)
    256     {
    257         if (InspectorFrontendAPI._isLoaded) {
    258             command();
    259             return;
    260         }
    261         InspectorFrontendAPI._pendingCommands.push(command);
    262     }
    263 }
    264 
    265 /** @typedef {!Object.<{type: string, keyCode: (number|undefined), keyIdentifier: (string|undefined), modifiers: (number|undefined)}>} */
    266 InspectorFrontendAPI.ForwardedKeyboardEvent;
    267