1/*
2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2011 Google Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 *     its contributors may be used to endorse or promote products derived
17 *     from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/inspector/InspectorInspectorAgent.h"
33
34#include "bindings/core/v8/DOMWrapperWorld.h"
35#include "bindings/core/v8/ScriptController.h"
36#include "core/InspectorFrontend.h"
37#include "core/dom/Document.h"
38#include "core/frame/LocalFrame.h"
39#include "core/inspector/InjectedScriptHost.h"
40#include "core/inspector/InjectedScriptManager.h"
41#include "core/inspector/InspectorController.h"
42#include "core/inspector/InspectorState.h"
43#include "core/inspector/InstrumentingAgents.h"
44#include "core/loader/DocumentLoader.h"
45#include "core/page/Page.h"
46#include "platform/weborigin/SecurityOrigin.h"
47#include "wtf/text/StringBuilder.h"
48
49namespace blink {
50
51namespace InspectorAgentState {
52static const char inspectorAgentEnabled[] = "inspectorAgentEnabled";
53}
54
55InspectorInspectorAgent::InspectorInspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager)
56    : InspectorBaseAgent<InspectorInspectorAgent>("Inspector")
57    , m_inspectedPage(page)
58    , m_frontend(0)
59    , m_injectedScriptManager(injectedScriptManager)
60{
61    ASSERT_ARG(page, page);
62}
63
64InspectorInspectorAgent::~InspectorInspectorAgent()
65{
66#if !ENABLE(OILPAN)
67    m_instrumentingAgents->setInspectorInspectorAgent(0);
68#endif
69}
70
71void InspectorInspectorAgent::trace(Visitor* visitor)
72{
73    visitor->trace(m_inspectedPage);
74    visitor->trace(m_injectedScriptManager);
75    InspectorBaseAgent::trace(visitor);
76}
77
78void InspectorInspectorAgent::didClearDocumentOfWindowObject(LocalFrame* frame)
79{
80    if (m_injectedScriptForOrigin.isEmpty())
81        return;
82
83    String origin = frame->document()->securityOrigin()->toRawString();
84    String script = m_injectedScriptForOrigin.get(origin);
85    if (script.isEmpty())
86        return;
87    int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(ScriptState::forMainWorld(frame));
88    StringBuilder scriptSource;
89    scriptSource.append(script);
90    scriptSource.append('(');
91    scriptSource.appendNumber(injectedScriptId);
92    scriptSource.append(')');
93    frame->script().executeScriptInMainWorld(scriptSource.toString());
94}
95
96void InspectorInspectorAgent::init()
97{
98    m_instrumentingAgents->setInspectorInspectorAgent(this);
99}
100
101void InspectorInspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend)
102{
103    m_frontend = inspectorFrontend->inspector();
104}
105
106void InspectorInspectorAgent::clearFrontend()
107{
108    m_pendingEvaluateTestCommands.clear();
109    m_frontend = 0;
110    m_injectedScriptManager->discardInjectedScripts();
111    ErrorString error;
112    disable(&error);
113}
114
115void InspectorInspectorAgent::enable(ErrorString*)
116{
117    m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, true);
118
119    if (m_pendingInspectData.first)
120        inspect(m_pendingInspectData.first, m_pendingInspectData.second);
121
122    for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it)
123        m_frontend->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
124    m_pendingEvaluateTestCommands.clear();
125}
126
127void InspectorInspectorAgent::disable(ErrorString*)
128{
129    m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, false);
130}
131
132void InspectorInspectorAgent::reset(ErrorString*)
133{
134    m_inspectedPage->inspectorController().reconnectFrontend();
135}
136
137void InspectorInspectorAgent::domContentLoadedEventFired(LocalFrame* frame)
138{
139    if (frame->page()->mainFrame() != frame)
140        return;
141
142    m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
143}
144
145void InspectorInspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
146{
147    if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled)) {
148        m_frontend->evaluateForTestInFrontend(static_cast<int>(callId), script);
149        m_frontend->flush();
150    } else {
151        m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
152    }
153}
154
155void InspectorInspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
156{
157    m_injectedScriptForOrigin.set(origin, source);
158}
159
160void InspectorInspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<JSONObject> hints)
161{
162    if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled) && m_frontend) {
163        m_frontend->inspect(objectToInspect, hints);
164        m_pendingInspectData.first = nullptr;
165        m_pendingInspectData.second = nullptr;
166        return;
167    }
168    m_pendingInspectData.first = objectToInspect;
169    m_pendingInspectData.second = hints;
170}
171
172} // namespace blink
173