2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/inspector/InspectorRuntimeAgent.h"
34 #include "bindings/v8/ScriptDebugServer.h"
35 #include "bindings/v8/ScriptState.h"
36 #include "core/inspector/InjectedScript.h"
37 #include "core/inspector/InjectedScriptManager.h"
38 #include "core/inspector/InspectorState.h"
39 #include "platform/JSONValues.h"
41 using WebCore::TypeBuilder::Runtime::ExecutionContextDescription;
45 namespace InspectorRuntimeAgentState {
46 static const char runtimeEnabled[] = "runtimeEnabled";
49 static bool asBool(const bool* const b)
51 return b ? *b : false;
54 InspectorRuntimeAgent::InspectorRuntimeAgent(InjectedScriptManager* injectedScriptManager, ScriptDebugServer* scriptDebugServer)
55 : InspectorBaseAgent<InspectorRuntimeAgent>("Runtime")
58 , m_injectedScriptManager(injectedScriptManager)
59 , m_scriptDebugServer(scriptDebugServer)
63 InspectorRuntimeAgent::~InspectorRuntimeAgent()
67 static ScriptDebugServer::PauseOnExceptionsState setPauseOnExceptionsState(ScriptDebugServer* scriptDebugServer, ScriptDebugServer::PauseOnExceptionsState newState)
69 ASSERT(scriptDebugServer);
70 ScriptDebugServer::PauseOnExceptionsState presentState = scriptDebugServer->pauseOnExceptionsState();
71 if (presentState != newState)
72 scriptDebugServer->setPauseOnExceptionsState(newState);
76 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)
78 InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
79 if (injectedScript.isEmpty())
81 ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
82 if (asBool(doNotPauseOnExceptionsAndMuteConsole))
83 previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
84 if (asBool(doNotPauseOnExceptionsAndMuteConsole))
87 injectedScript.evaluate(errorString, expression, objectGroup ? *objectGroup : "", asBool(includeCommandLineAPI), asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
89 if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
91 setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
95 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)
97 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
98 if (injectedScript.isEmpty()) {
99 *errorString = "Inspected frame has gone";
103 if (optionalArguments)
104 arguments = (*optionalArguments)->toJSONString();
106 ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
107 if (asBool(doNotPauseOnExceptionsAndMuteConsole))
108 previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
109 if (asBool(doNotPauseOnExceptionsAndMuteConsole))
112 injectedScript.callFunctionOn(errorString, objectId, expression, arguments, asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
114 if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
116 setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
120 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)
122 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
123 if (injectedScript.isEmpty()) {
124 *errorString = "Inspected frame has gone";
128 ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
131 bool accessorPropertiesOnlyValue = accessorPropertiesOnly && *accessorPropertiesOnly;
132 injectedScript.getProperties(errorString, objectId, ownProperties && *ownProperties, accessorPropertiesOnlyValue, &result);
134 if (!accessorPropertiesOnlyValue)
135 injectedScript.getInternalProperties(errorString, objectId, &internalProperties);
138 setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
141 void InspectorRuntimeAgent::releaseObject(ErrorString*, const String& objectId)
143 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
144 if (!injectedScript.isEmpty())
145 injectedScript.releaseObject(objectId);
148 void InspectorRuntimeAgent::releaseObjectGroup(ErrorString*, const String& objectGroup)
150 m_injectedScriptManager->releaseObjectGroup(objectGroup);
153 void InspectorRuntimeAgent::run(ErrorString*)
157 void InspectorRuntimeAgent::setFrontend(InspectorFrontend* frontend)
159 m_frontend = frontend->runtime();
162 void InspectorRuntimeAgent::clearFrontend()
166 disable(&errorString);
169 void InspectorRuntimeAgent::restore()
171 if (m_state->getBoolean(InspectorRuntimeAgentState::runtimeEnabled)) {
172 m_scriptStateToId.clear();
173 m_frontend->executionContextsCleared();
179 void InspectorRuntimeAgent::enable(ErrorString* errorString)
185 m_state->setBoolean(InspectorRuntimeAgentState::runtimeEnabled, true);
188 void InspectorRuntimeAgent::disable(ErrorString* errorString)
193 m_scriptStateToId.clear();
195 m_state->setBoolean(InspectorRuntimeAgentState::runtimeEnabled, false);
198 void InspectorRuntimeAgent::addExecutionContextToFrontend(ScriptState* scriptState, bool isPageContext, const String& name, const String& frameId)
200 int executionContextId = injectedScriptManager()->injectedScriptIdFor(scriptState);
201 m_scriptStateToId.set(scriptState, executionContextId);
202 m_frontend->executionContextCreated(ExecutionContextDescription::create()
203 .setId(executionContextId)
204 .setIsPageContext(isPageContext)
210 } // namespace WebCore