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
33#if ENABLE(INSPECTOR)
34
35#include "InspectorAgent.h"
36
37#include "Document.h"
38#include "DocumentLoader.h"
39#include "Frame.h"
40#include "GraphicsContext.h"
41#include "InjectedScriptHost.h"
42#include "InjectedScriptManager.h"
43#include "InspectorController.h"
44#include "InspectorFrontend.h"
45#include "InspectorInstrumentation.h"
46#include "InspectorState.h"
47#include "InspectorValues.h"
48#include "InstrumentingAgents.h"
49#include "Page.h"
50#include "ResourceRequest.h"
51#include "ScriptController.h"
52#include "ScriptFunctionCall.h"
53#include "ScriptObject.h"
54#include "SecurityOrigin.h"
55#include "Settings.h"
56#include <wtf/PassRefPtr.h>
57#include <wtf/RefPtr.h>
58
59namespace WebCore {
60
61namespace InspectorAgentState {
62static const char inspectorAgentEnabled[] = "inspectorAgentEnabled";
63}
64
65InspectorAgent::InspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
66    : InspectorBaseAgent<InspectorAgent>("Inspector", instrumentingAgents, state)
67    , m_inspectedPage(page)
68    , m_frontend(0)
69    , m_injectedScriptManager(injectedScriptManager)
70{
71    ASSERT_ARG(page, page);
72    m_instrumentingAgents->setInspectorAgent(this);
73}
74
75InspectorAgent::~InspectorAgent()
76{
77    m_instrumentingAgents->setInspectorAgent(0);
78}
79
80void InspectorAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
81{
82    if (world != mainThreadNormalWorld())
83        return;
84
85    if (m_injectedScriptForOrigin.isEmpty())
86        return;
87
88    String origin = frame->document()->securityOrigin()->toRawString();
89    String script = m_injectedScriptForOrigin.get(origin);
90    if (script.isEmpty())
91        return;
92    int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(mainWorldScriptState(frame));
93    StringBuilder scriptSource;
94    scriptSource.append(script);
95    scriptSource.append("(");
96    scriptSource.appendNumber(injectedScriptId);
97    scriptSource.append(")");
98    frame->script()->executeScript(scriptSource.toString());
99}
100
101void InspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend)
102{
103    m_frontend = inspectorFrontend;
104}
105
106void InspectorAgent::clearFrontend()
107{
108    m_pendingEvaluateTestCommands.clear();
109    m_frontend = 0;
110    m_injectedScriptManager->discardInjectedScripts();
111    ErrorString error;
112    disable(&error);
113}
114
115void InspectorAgent::didCommitLoad()
116{
117    m_injectedScriptManager->discardInjectedScripts();
118}
119
120void InspectorAgent::enable(ErrorString*)
121{
122    m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, true);
123
124    if (m_pendingInspectData.first)
125        inspect(m_pendingInspectData.first, m_pendingInspectData.second);
126
127    for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it)
128        m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
129    m_pendingEvaluateTestCommands.clear();
130}
131
132void InspectorAgent::disable(ErrorString*)
133{
134    m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, false);
135}
136
137void InspectorAgent::domContentLoadedEventFired()
138{
139    m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
140}
141
142bool InspectorAgent::isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl)
143{
144    return loader->frame() == m_inspectedPage->mainFrame() && requestUrl == loader->requestURL();
145}
146
147void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
148{
149    if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
150        m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>(callId), script);
151    else
152        m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
153}
154
155void InspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
156{
157    m_injectedScriptForOrigin.set(origin, source);
158}
159
160void InspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<InspectorObject> hints)
161{
162    if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled) && m_frontend) {
163        m_frontend->inspector()->inspect(objectToInspect, hints);
164        m_pendingInspectData.first = 0;
165        m_pendingInspectData.second = 0;
166        return;
167    }
168    m_pendingInspectData.first = objectToInspect;
169    m_pendingInspectData.second = hints;
170}
171
172KURL InspectorAgent::inspectedURL() const
173{
174    return m_inspectedPage->mainFrame()->document()->url();
175}
176
177KURL InspectorAgent::inspectedURLWithoutFragment() const
178{
179    KURL url = inspectedURL();
180    url.removeFragmentIdentifier();
181    return url;
182}
183
184bool InspectorAgent::developerExtrasEnabled() const
185{
186    if (!m_inspectedPage)
187        return false;
188    return m_inspectedPage->settings()->developerExtrasEnabled();
189}
190
191} // namespace WebCore
192
193#endif // ENABLE(INSPECTOR)
194