InspectorAgent.cpp 5.94 KB
Newer Older
1
2
3
/*
 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4
 * Copyright (C) 2011 Google Inc. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(INSPECTOR)

35
36
#include "InspectorAgent.h"

37
38
39
40
#include "Document.h"
#include "DocumentLoader.h"
#include "GraphicsContext.h"
#include "InjectedScriptHost.h"
41
#include "InjectedScriptManager.h"
42
43
44
45
#include "InspectorController.h"
#include "InspectorFrontend.h"
#include "InspectorInstrumentation.h"
#include "InspectorValues.h"
46
#include "InstrumentingAgents.h"
darin@apple.com's avatar
darin@apple.com committed
47
#include "MainFrame.h"
48
49
#include "Page.h"
#include "ResourceRequest.h"
50
#include "ScriptController.h"
51
52
#include "ScriptFunctionCall.h"
#include "ScriptObject.h"
53
#include "SecurityOrigin.h"
54
#include "Settings.h"
55
56
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
57
58
59

namespace WebCore {

60
InspectorAgent::InspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents)
61
    : InspectorBaseAgent(ASCIILiteral("Inspector"), instrumentingAgents)
62
    , m_inspectedPage(page)
63
    , m_injectedScriptManager(injectedScriptManager)
64
    , m_enabled(false)
65
66
{
    ASSERT_ARG(page, page);
67
    m_instrumentingAgents->setInspectorAgent(this);
68
69
70
71
}

InspectorAgent::~InspectorAgent()
{
72
    m_instrumentingAgents->setInspectorAgent(0);
73
74
}

75
void InspectorAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld& world)
76
{
77
    if (&world != &mainThreadNormalWorld())
78
79
        return;

80
81
82
    if (m_injectedScriptForOrigin.isEmpty())
        return;

83
    String origin = frame->document()->securityOrigin()->toRawString();
84
    String script = m_injectedScriptForOrigin.get(origin);
85
86
    if (script.isEmpty())
        return;
87
    int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(mainWorldExecState(frame));
88
89
90
91
92
    StringBuilder scriptSource;
    scriptSource.append(script);
    scriptSource.append("(");
    scriptSource.appendNumber(injectedScriptId);
    scriptSource.append(")");
93
    frame->script().executeScript(scriptSource.toString());
94
95
}

96
void InspectorAgent::didCreateFrontendAndBackend(InspectorFrontendChannel* frontendChannel, InspectorBackendDispatcher* backendDispatcher)
97
{
98
    m_frontendDispatcher = std::make_unique<InspectorInspectorFrontendDispatcher>(frontendChannel);
99
    m_backendDispatcher = InspectorInspectorBackendDispatcher::create(backendDispatcher, this);
100
101
}

102
void InspectorAgent::willDestroyFrontendAndBackend()
103
{
104
    m_frontendDispatcher = nullptr;
105
    m_backendDispatcher.clear();
106

107
    m_pendingEvaluateTestCommands.clear();
108
    m_injectedScriptManager->discardInjectedScripts();
109
110
    ErrorString error;
    disable(&error);
111
112
}

113
void InspectorAgent::didCommitLoad()
114
{
115
    m_injectedScriptManager->discardInjectedScripts();
116
117
}

118
119
void InspectorAgent::enable(ErrorString*)
{
120
    m_enabled = true;
121

122
123
124
    if (m_pendingInspectData.first)
        inspect(m_pendingInspectData.first, m_pendingInspectData.second);

125
126
    for (Vector<pair<long, String>>::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontendDispatcher && it != m_pendingEvaluateTestCommands.end(); ++it)
        m_frontendDispatcher->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
127
    m_pendingEvaluateTestCommands.clear();
128
129
130
131
}

void InspectorAgent::disable(ErrorString*)
{
132
    m_enabled = false;
133
134
}

135
void InspectorAgent::domContentLoadedEventFired()
136
{
137
    m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
138
139
140
141
}

void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
{
142
143
    if (m_enabled && m_frontendDispatcher)
        m_frontendDispatcher->evaluateForTestInFrontend(static_cast<int>(callId), script);
144
145
    else
        m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
146
147
}

148
void InspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
149
{
150
    m_injectedScriptForOrigin.set(origin, source);
151
152
}

153
void InspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<InspectorObject> hints)
154
{
155
156
    if (m_enabled && m_frontendDispatcher) {
        m_frontendDispatcher->inspect(objectToInspect, hints);
157
158
159
160
161
162
163
164
        m_pendingInspectData.first = 0;
        m_pendingInspectData.second = 0;
        return;
    }
    m_pendingInspectData.first = objectToInspect;
    m_pendingInspectData.second = hints;
}

165
bool InspectorAgent::developerExtrasEnabled() const
166
{
167
168
    if (!m_inspectedPage)
        return false;
169
    return m_inspectedPage->settings().developerExtrasEnabled();
170
171
}

172
173
174
} // namespace WebCore

#endif // ENABLE(INSPECTOR)