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/PageRuntimeAgent.h"
34 #include "bindings/v8/DOMWrapperWorld.h"
35 #include "bindings/v8/ScriptController.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "core/frame/FrameConsole.h"
38 #include "core/frame/LocalFrame.h"
39 #include "core/inspector/InjectedScript.h"
40 #include "core/inspector/InjectedScriptManager.h"
41 #include "core/inspector/InspectorPageAgent.h"
42 #include "core/inspector/InstrumentingAgents.h"
43 #include "core/page/Page.h"
44 #include "platform/weborigin/SecurityOrigin.h"
48 PageRuntimeAgent::PageRuntimeAgent(InjectedScriptManager* injectedScriptManager, ScriptDebugServer* scriptDebugServer, Page* page, InspectorPageAgent* pageAgent)
49 : InspectorRuntimeAgent(injectedScriptManager, scriptDebugServer)
50 , m_inspectedPage(page)
51 , m_pageAgent(pageAgent)
52 , m_mainWorldContextCreated(false)
56 PageRuntimeAgent::~PageRuntimeAgent()
58 m_instrumentingAgents->setPageRuntimeAgent(0);
61 void PageRuntimeAgent::init()
63 m_instrumentingAgents->setPageRuntimeAgent(this);
66 void PageRuntimeAgent::enable(ErrorString* errorString)
71 InspectorRuntimeAgent::enable(errorString);
73 // Only report existing contexts if the page did commit load, otherwise we may
74 // unintentionally initialize contexts in the frames which may trigger some listeners
75 // that are expected to be triggered only after the load is committed, see http://crbug.com/131623
76 if (m_mainWorldContextCreated)
77 reportExecutionContextCreation();
80 void PageRuntimeAgent::didClearDocumentOfWindowObject(LocalFrame* frame)
82 m_mainWorldContextCreated = true;
88 if (frame == m_inspectedPage->mainFrame()) {
89 m_scriptStateToId.clear();
90 m_frontend->executionContextsCleared();
92 String frameId = m_pageAgent->frameId(frame);
93 addExecutionContextToFrontend(ScriptState::forMainWorld(frame), true, "", frameId);
96 void PageRuntimeAgent::didCreateIsolatedContext(LocalFrame* frame, ScriptState* scriptState, SecurityOrigin* origin)
101 String frameId = m_pageAgent->frameId(frame);
102 addExecutionContextToFrontend(scriptState, false, origin->toRawString(), frameId);
105 InjectedScript PageRuntimeAgent::injectedScriptForEval(ErrorString* errorString, const int* executionContextId)
107 if (!executionContextId) {
108 ScriptState* scriptState = ScriptState::forMainWorld(m_inspectedPage->mainFrame());
109 InjectedScript result = injectedScriptManager()->injectedScriptFor(scriptState);
110 if (result.isEmpty())
111 *errorString = "Internal error: main world execution context not found.";
114 InjectedScript injectedScript = injectedScriptManager()->injectedScriptForId(*executionContextId);
115 if (injectedScript.isEmpty())
116 *errorString = "Execution context with given id not found.";
117 return injectedScript;
120 void PageRuntimeAgent::muteConsole()
122 FrameConsole::mute();
125 void PageRuntimeAgent::unmuteConsole()
127 FrameConsole::unmute();
130 void PageRuntimeAgent::reportExecutionContextCreation()
132 Vector<std::pair<ScriptState*, SecurityOrigin*> > isolatedContexts;
133 for (LocalFrame* frame = m_inspectedPage->mainFrame(); frame; frame = frame->tree().traverseNext()) {
134 if (!frame->script().canExecuteScripts(NotAboutToExecuteScript))
136 String frameId = m_pageAgent->frameId(frame);
138 ScriptState* scriptState = ScriptState::forMainWorld(frame);
139 addExecutionContextToFrontend(scriptState, true, "", frameId);
140 frame->script().collectIsolatedContexts(isolatedContexts);
141 if (isolatedContexts.isEmpty())
143 for (size_t i = 0; i< isolatedContexts.size(); i++)
144 addExecutionContextToFrontend(isolatedContexts[i].first, false, isolatedContexts[i].second->toRawString(), frameId);
145 isolatedContexts.clear();
149 void PageRuntimeAgent::frameWindowDiscarded(DOMWindow* window)
151 Vector<RefPtr<ScriptState> > scriptStatesToRemove;
152 for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
153 RefPtr<ScriptState> scriptState = it->key;
154 if (scriptState->contextIsEmpty() || window == scriptState->domWindow()) {
155 scriptStatesToRemove.append(scriptState);
156 m_frontend->executionContextDestroyed(it->value);
159 m_scriptStateToId.removeAll(scriptStatesToRemove);
162 } // namespace WebCore