Home | History | Annotate | Download | only in front-end
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  * Copyright (C) 2009 Joseph Pecoraro
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 function InjectedScriptAccess(injectedScriptId) {
     33     this._injectedScriptId = injectedScriptId;
     34 }
     35 
     36 InjectedScriptAccess.get = function(injectedScriptId)
     37 {
     38     return new InjectedScriptAccess(injectedScriptId);
     39 }
     40 
     41 InjectedScriptAccess.getDefault = function()
     42 {
     43     return InjectedScriptAccess.get(0);
     44 }
     45 
     46 InjectedScriptAccess.prototype = {};
     47 
     48 InjectedScriptAccess._installHandler = function(methodName, async)
     49 {
     50     InjectedScriptAccess.prototype[methodName] = function()
     51     {
     52         var allArgs = Array.prototype.slice.call(arguments);
     53         var callback = allArgs[allArgs.length - 1];
     54         var argsString = JSON.stringify(Array.prototype.slice.call(allArgs, 0, allArgs.length - 1));
     55 
     56         function myCallback(result, isException)
     57         {
     58             if (!isException)
     59                 callback(result);
     60             else
     61                 WebInspector.console.addMessage(new WebInspector.ConsoleTextMessage("Error dispatching: " + methodName));
     62         }
     63         var callId = WebInspector.Callback.wrap(myCallback);
     64 
     65         InspectorBackend.dispatchOnInjectedScript(callId, this._injectedScriptId, methodName, argsString, !!async);
     66     };
     67 }
     68 
     69 // InjectedScriptAccess message forwarding puts some constraints on the way methods are implemented and called:
     70 // - Make sure corresponding methods in InjectedScript return non-null and non-undefined values,
     71 // - Make sure last parameter of all the InjectedSriptAccess.* calls is a callback function.
     72 // We keep these sorted.
     73 InjectedScriptAccess._installHandler("addInspectedNode");
     74 InjectedScriptAccess._installHandler("addStyleSelector");
     75 InjectedScriptAccess._installHandler("applyStyleRuleText");
     76 InjectedScriptAccess._installHandler("applyStyleText");
     77 InjectedScriptAccess._installHandler("clearConsoleMessages");
     78 InjectedScriptAccess._installHandler("evaluate");
     79 InjectedScriptAccess._installHandler("evaluateInCallFrame");
     80 InjectedScriptAccess._installHandler("getCompletions");
     81 InjectedScriptAccess._installHandler("getComputedStyle");
     82 InjectedScriptAccess._installHandler("getInlineStyle");
     83 InjectedScriptAccess._installHandler("getNodePropertyValue");
     84 InjectedScriptAccess._installHandler("getProperties");
     85 InjectedScriptAccess._installHandler("getPrototypes");
     86 InjectedScriptAccess._installHandler("getStyles");
     87 InjectedScriptAccess._installHandler("openInInspectedWindow");
     88 InjectedScriptAccess._installHandler("performSearch");
     89 InjectedScriptAccess._installHandler("pushNodeToFrontend");
     90 InjectedScriptAccess._installHandler("nodeByPath");
     91 InjectedScriptAccess._installHandler("searchCanceled");
     92 InjectedScriptAccess._installHandler("setOuterHTML");
     93 InjectedScriptAccess._installHandler("setPropertyValue");
     94 InjectedScriptAccess._installHandler("setStyleProperty");
     95 InjectedScriptAccess._installHandler("setStyleText");
     96 InjectedScriptAccess._installHandler("toggleStyleEnabled");
     97 InjectedScriptAccess._installHandler("evaluateOnSelf");
     98 
     99 // Some methods can't run synchronously even on the injected script side (such as DB transactions).
    100 // Mark them as asynchronous here.
    101 InjectedScriptAccess._installHandler("executeSql", true);
    102 
    103 WebInspector.didDispatchOnInjectedScript = WebInspector.Callback.processCallback;
    104