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/InspectorInstrumentation.h"
34 #include "core/events/EventTarget.h"
35 #include "core/fetch/FetchInitiatorInfo.h"
36 #include "core/frame/FrameHost.h"
37 #include "core/inspector/InspectorCSSAgent.h"
38 #include "core/inspector/InspectorConsoleAgent.h"
39 #include "core/inspector/InspectorController.h"
40 #include "core/inspector/InspectorDebuggerAgent.h"
41 #include "core/inspector/InspectorInspectorAgent.h"
42 #include "core/inspector/InspectorProfilerAgent.h"
43 #include "core/inspector/InspectorResourceAgent.h"
44 #include "core/inspector/InspectorTimelineAgent.h"
45 #include "core/inspector/InstrumentingAgents.h"
46 #include "core/inspector/ScriptAsyncCallStack.h"
47 #include "core/inspector/ScriptCallStack.h"
48 #include "core/inspector/WorkerInspectorController.h"
49 #include "core/page/Page.h"
50 #include "core/workers/WorkerGlobalScope.h"
55 static HashSet<InstrumentingAgents*>* instrumentingAgentsSet = 0;
58 namespace InspectorInstrumentation {
59 int FrontendCounter::s_frontendCounter = 0;
62 InspectorInstrumentationCookie::InspectorInstrumentationCookie()
63 : m_instrumentingAgents(nullptr)
64 , m_timelineAgentId(0)
68 InspectorInstrumentationCookie::InspectorInstrumentationCookie(InstrumentingAgents* agents, int timelineAgentId)
69 : m_instrumentingAgents(agents)
70 , m_timelineAgentId(timelineAgentId)
74 InspectorInstrumentationCookie::InspectorInstrumentationCookie(const InspectorInstrumentationCookie& other)
75 : m_instrumentingAgents(other.m_instrumentingAgents)
76 , m_timelineAgentId(other.m_timelineAgentId)
80 InspectorInstrumentationCookie& InspectorInstrumentationCookie::operator=(const InspectorInstrumentationCookie& other)
83 m_instrumentingAgents = other.m_instrumentingAgents;
84 m_timelineAgentId = other.m_timelineAgentId;
89 InspectorInstrumentationCookie::~InspectorInstrumentationCookie()
93 namespace InspectorInstrumentation {
95 bool isDebuggerPausedImpl(InstrumentingAgents* instrumentingAgents)
97 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
98 return debuggerAgent->isPaused();
102 void didReceiveResourceResponseButCanceledImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
104 didReceiveResourceResponse(frame, identifier, loader, r, 0);
107 void continueAfterXFrameOptionsDeniedImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
109 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
112 void continueWithPolicyIgnoreImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
114 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
117 void willDestroyResourceImpl(Resource* cachedResource)
119 if (!instrumentingAgentsSet)
121 HashSet<InstrumentingAgents*>::iterator end = instrumentingAgentsSet->end();
122 for (HashSet<InstrumentingAgents*>::iterator it = instrumentingAgentsSet->begin(); it != end; ++it) {
123 InstrumentingAgents* instrumentingAgents = *it;
124 if (InspectorResourceAgent* inspectorResourceAgent = instrumentingAgents->inspectorResourceAgent())
125 inspectorResourceAgent->willDestroyResource(cachedResource);
129 bool collectingHTMLParseErrorsImpl(InstrumentingAgents* instrumentingAgents)
131 if (InspectorInspectorAgent* inspectorAgent = instrumentingAgents->inspectorInspectorAgent())
132 return inspectorAgent->hasFrontend();
136 PassOwnPtr<ScriptSourceCode> preprocessImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const ScriptSourceCode& sourceCode)
138 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
139 return debuggerAgent->preprocess(frame, sourceCode);
140 return PassOwnPtr<ScriptSourceCode>();
143 String preprocessEventListenerImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const String& source, const String& url, const String& functionName)
145 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
146 return debuggerAgent->preprocessEventListener(frame, source, url, functionName);
150 void appendAsyncCallStack(ExecutionContext* executionContext, ScriptCallStack* callStack)
152 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
153 if (!instrumentingAgents)
155 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
156 callStack->setAsyncCallStack(debuggerAgent->currentAsyncStackTraceForConsole());
159 bool canvasAgentEnabled(ExecutionContext* executionContext)
161 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
162 return instrumentingAgents && instrumentingAgents->inspectorCanvasAgent();
165 bool consoleAgentEnabled(ExecutionContext* executionContext)
167 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
168 InspectorConsoleAgent* consoleAgent = instrumentingAgents ? instrumentingAgents->inspectorConsoleAgent() : 0;
169 return consoleAgent && consoleAgent->enabled();
172 bool timelineAgentEnabled(ExecutionContext* executionContext)
174 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
175 return instrumentingAgents && instrumentingAgents->inspectorTimelineAgent();
178 void registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
180 if (!instrumentingAgentsSet)
181 instrumentingAgentsSet = new HashSet<InstrumentingAgents*>();
182 instrumentingAgentsSet->add(instrumentingAgents);
185 void unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
187 if (!instrumentingAgentsSet)
189 instrumentingAgentsSet->remove(instrumentingAgents);
190 if (instrumentingAgentsSet->isEmpty()) {
191 delete instrumentingAgentsSet;
192 instrumentingAgentsSet = 0;
196 InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
198 if (!cookie.instrumentingAgents())
200 InspectorTimelineAgent* timelineAgent = cookie.instrumentingAgents()->inspectorTimelineAgent();
201 if (timelineAgent && cookie.hasMatchingTimelineAgentId(timelineAgent->id()))
202 return timelineAgent;
206 InstrumentingAgents* instrumentingAgentsFor(Page* page)
210 return instrumentationForPage(page);
213 InstrumentingAgents* instrumentingAgentsFor(EventTarget* eventTarget)
217 return instrumentingAgentsFor(eventTarget->executionContext());
220 InstrumentingAgents* instrumentingAgentsFor(RenderObject* renderer)
222 return instrumentingAgentsFor(renderer->frame());
225 InstrumentingAgents* instrumentingAgentsFor(WorkerGlobalScope* workerGlobalScope)
227 if (!workerGlobalScope)
229 return instrumentationForWorkerGlobalScope(workerGlobalScope);
232 InstrumentingAgents* instrumentingAgentsFor(FrameHost* host)
234 return instrumentationForPage(&host->page());
237 InstrumentingAgents* instrumentingAgentsForNonDocumentContext(ExecutionContext* context)
239 if (context->isWorkerGlobalScope())
240 return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context));
244 } // namespace InspectorInstrumentation
246 namespace InstrumentationEvents {
247 const char PaintSetup[] = "PaintSetup";
248 const char RasterTask[] = "RasterTask";
249 const char Paint[] = "Paint";
250 const char Layer[] = "Layer";
251 const char RequestMainThreadFrame[] = "RequestMainThreadFrame";
252 const char BeginFrame[] = "BeginFrame";
253 const char ActivateLayerTree[] = "ActivateLayerTree";
254 const char DrawFrame[] = "DrawFrame";
255 const char EmbedderCallback[] = "EmbedderCallback";
258 namespace InstrumentationEventArguments {
259 const char FrameId[] = "frameId";
260 const char LayerId[] = "layerId";
261 const char LayerTreeId[] = "layerTreeId";
262 const char PageId[] = "pageId";
263 const char CallbackName[] = "callbackName";
266 InstrumentingAgents* instrumentationForPage(Page* page)
268 ASSERT(isMainThread());
269 return page->inspectorController().m_instrumentingAgents.get();
272 InstrumentingAgents* instrumentationForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope)
274 if (WorkerInspectorController* controller = workerGlobalScope->workerInspectorController())
275 return controller->m_instrumentingAgents.get();