Home | History | Annotate | Download | only in inspector
      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 #include "config.h"
     32 #include "core/inspector/InspectorRuntimeAgent.h"
     33 
     34 #include "core/inspector/InjectedScript.h"
     35 #include "core/inspector/InjectedScriptManager.h"
     36 #include "core/platform/JSONValues.h"
     37 
     38 
     39 #include "bindings/v8/ScriptDebugServer.h"
     40 
     41 namespace WebCore {
     42 
     43 static bool asBool(const bool* const b)
     44 {
     45     return b ? *b : false;
     46 }
     47 
     48 InspectorRuntimeAgent::InspectorRuntimeAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InjectedScriptManager* injectedScriptManager, ScriptDebugServer* scriptDebugServer)
     49     : InspectorBaseAgent<InspectorRuntimeAgent>("Runtime", instrumentingAgents, state)
     50     , m_enabled(false)
     51     , m_injectedScriptManager(injectedScriptManager)
     52     , m_scriptDebugServer(scriptDebugServer)
     53 {
     54 }
     55 
     56 InspectorRuntimeAgent::~InspectorRuntimeAgent()
     57 {
     58 }
     59 
     60 static ScriptDebugServer::PauseOnExceptionsState setPauseOnExceptionsState(ScriptDebugServer* scriptDebugServer, ScriptDebugServer::PauseOnExceptionsState newState)
     61 {
     62     ASSERT(scriptDebugServer);
     63     ScriptDebugServer::PauseOnExceptionsState presentState = scriptDebugServer->pauseOnExceptionsState();
     64     if (presentState != newState)
     65         scriptDebugServer->setPauseOnExceptionsState(newState);
     66     return presentState;
     67 }
     68 
     69 void InspectorRuntimeAgent::evaluate(ErrorString* errorString, const String& expression, const String* const objectGroup, const bool* const includeCommandLineAPI, const bool* const doNotPauseOnExceptionsAndMuteConsole, const int* executionContextId, const bool* const returnByValue, const bool* generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
     70 {
     71     InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
     72     if (injectedScript.hasNoValue())
     73         return;
     74     ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
     75     if (asBool(doNotPauseOnExceptionsAndMuteConsole))
     76         previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
     77     if (asBool(doNotPauseOnExceptionsAndMuteConsole))
     78         muteConsole();
     79 
     80     injectedScript.evaluate(errorString, expression, objectGroup ? *objectGroup : "", asBool(includeCommandLineAPI), asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
     81 
     82     if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
     83         unmuteConsole();
     84         setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
     85     }
     86 }
     87 
     88 void InspectorRuntimeAgent::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const RefPtr<JSONArray>* const optionalArguments, const bool* const doNotPauseOnExceptionsAndMuteConsole, const bool* const returnByValue, const bool* generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
     89 {
     90     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
     91     if (injectedScript.hasNoValue()) {
     92         *errorString = "Inspected frame has gone";
     93         return;
     94     }
     95     String arguments;
     96     if (optionalArguments)
     97         arguments = (*optionalArguments)->toJSONString();
     98 
     99     ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
    100     if (asBool(doNotPauseOnExceptionsAndMuteConsole))
    101         previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
    102     if (asBool(doNotPauseOnExceptionsAndMuteConsole))
    103         muteConsole();
    104 
    105     injectedScript.callFunctionOn(errorString, objectId, expression, arguments, asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
    106 
    107     if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
    108         unmuteConsole();
    109         setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
    110     }
    111 }
    112 
    113 void InspectorRuntimeAgent::getProperties(ErrorString* errorString, const String& objectId, const bool* ownProperties, const bool* accessorPropertiesOnly, RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::PropertyDescriptor> >& result, RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::InternalPropertyDescriptor> >& internalProperties)
    114 {
    115     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
    116     if (injectedScript.hasNoValue()) {
    117         *errorString = "Inspected frame has gone";
    118         return;
    119     }
    120 
    121     ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
    122     muteConsole();
    123 
    124     bool accessorPropertiesOnlyValue = accessorPropertiesOnly && *accessorPropertiesOnly;
    125     injectedScript.getProperties(errorString, objectId, ownProperties && *ownProperties, accessorPropertiesOnlyValue, &result);
    126 
    127     if (!accessorPropertiesOnlyValue)
    128         injectedScript.getInternalProperties(errorString, objectId, &internalProperties);
    129 
    130     unmuteConsole();
    131     setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
    132 }
    133 
    134 void InspectorRuntimeAgent::releaseObject(ErrorString*, const String& objectId)
    135 {
    136     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
    137     if (!injectedScript.hasNoValue())
    138         injectedScript.releaseObject(objectId);
    139 }
    140 
    141 void InspectorRuntimeAgent::releaseObjectGroup(ErrorString*, const String& objectGroup)
    142 {
    143     m_injectedScriptManager->releaseObjectGroup(objectGroup);
    144 }
    145 
    146 void InspectorRuntimeAgent::run(ErrorString*)
    147 {
    148 }
    149 
    150 } // namespace WebCore
    151 
    152