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/inspector/InspectorCSSAgent.h"
37 #include "core/inspector/InspectorConsoleAgent.h"
38 #include "core/inspector/InspectorController.h"
39 #include "core/inspector/InspectorDebuggerAgent.h"
40 #include "core/inspector/InspectorInspectorAgent.h"
41 #include "core/inspector/InspectorProfilerAgent.h"
42 #include "core/inspector/InspectorResourceAgent.h"
43 #include "core/inspector/InspectorTimelineAgent.h"
44 #include "core/inspector/InstrumentingAgents.h"
45 #include "core/inspector/ScriptAsyncCallStack.h"
46 #include "core/inspector/ScriptCallStack.h"
47 #include "core/inspector/WorkerInspectorController.h"
48 #include "core/page/Page.h"
49 #include "core/workers/WorkerGlobalScope.h"
54 static HashSet<InstrumentingAgents*>* instrumentingAgentsSet = 0;
57 namespace InspectorInstrumentation {
58 int FrontendCounter::s_frontendCounter = 0;
61 InspectorInstrumentationCookie::InspectorInstrumentationCookie()
62 : m_instrumentingAgents(nullptr)
63 , m_timelineAgentId(0)
67 InspectorInstrumentationCookie::InspectorInstrumentationCookie(InstrumentingAgents* agents, int timelineAgentId)
68 : m_instrumentingAgents(agents)
69 , m_timelineAgentId(timelineAgentId)
73 InspectorInstrumentationCookie::InspectorInstrumentationCookie(const InspectorInstrumentationCookie& other)
74 : m_instrumentingAgents(other.m_instrumentingAgents)
75 , m_timelineAgentId(other.m_timelineAgentId)
79 InspectorInstrumentationCookie& InspectorInstrumentationCookie::operator=(const InspectorInstrumentationCookie& other)
82 m_instrumentingAgents = other.m_instrumentingAgents;
83 m_timelineAgentId = other.m_timelineAgentId;
88 InspectorInstrumentationCookie::~InspectorInstrumentationCookie()
92 namespace InspectorInstrumentation {
94 bool isDebuggerPausedImpl(InstrumentingAgents* instrumentingAgents)
96 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
97 return debuggerAgent->isPaused();
101 void didReceiveResourceResponseButCanceledImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
103 didReceiveResourceResponse(frame, identifier, loader, r, 0);
106 void continueAfterXFrameOptionsDeniedImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
108 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
111 void continueWithPolicyIgnoreImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
113 didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
116 void willDestroyResourceImpl(Resource* cachedResource)
118 if (!instrumentingAgentsSet)
120 HashSet<InstrumentingAgents*>::iterator end = instrumentingAgentsSet->end();
121 for (HashSet<InstrumentingAgents*>::iterator it = instrumentingAgentsSet->begin(); it != end; ++it) {
122 InstrumentingAgents* instrumentingAgents = *it;
123 if (InspectorResourceAgent* inspectorResourceAgent = instrumentingAgents->inspectorResourceAgent())
124 inspectorResourceAgent->willDestroyResource(cachedResource);
128 bool collectingHTMLParseErrorsImpl(InstrumentingAgents* instrumentingAgents)
130 if (InspectorInspectorAgent* inspectorAgent = instrumentingAgents->inspectorInspectorAgent())
131 return inspectorAgent->hasFrontend();
135 PassOwnPtr<ScriptSourceCode> preprocessImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const ScriptSourceCode& sourceCode)
137 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
138 return debuggerAgent->preprocess(frame, sourceCode);
139 return PassOwnPtr<ScriptSourceCode>();
142 String preprocessEventListenerImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const String& source, const String& url, const String& functionName)
144 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
145 return debuggerAgent->preprocessEventListener(frame, source, url, functionName);
149 void appendAsyncCallStack(ExecutionContext* executionContext, ScriptCallStack* callStack)
151 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
152 if (!instrumentingAgents)
154 if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
155 callStack->setAsyncCallStack(debuggerAgent->currentAsyncStackTraceForConsole());
158 bool canvasAgentEnabled(ExecutionContext* executionContext)
160 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
161 return instrumentingAgents && instrumentingAgents->inspectorCanvasAgent();
164 bool consoleAgentEnabled(ExecutionContext* executionContext)
166 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
167 InspectorConsoleAgent* consoleAgent = instrumentingAgents ? instrumentingAgents->inspectorConsoleAgent() : 0;
168 return consoleAgent && consoleAgent->enabled();
171 bool timelineAgentEnabled(ExecutionContext* executionContext)
173 InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
174 return instrumentingAgents && instrumentingAgents->inspectorTimelineAgent();
177 void registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
179 if (!instrumentingAgentsSet)
180 instrumentingAgentsSet = new HashSet<InstrumentingAgents*>();
181 instrumentingAgentsSet->add(instrumentingAgents);
184 void unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
186 if (!instrumentingAgentsSet)
188 instrumentingAgentsSet->remove(instrumentingAgents);
189 if (instrumentingAgentsSet->isEmpty()) {
190 delete instrumentingAgentsSet;
191 instrumentingAgentsSet = 0;
195 InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
197 if (!cookie.instrumentingAgents())
199 InspectorTimelineAgent* timelineAgent = cookie.instrumentingAgents()->inspectorTimelineAgent();
200 if (timelineAgent && cookie.hasMatchingTimelineAgentId(timelineAgent->id()))
201 return timelineAgent;
205 InstrumentingAgents* instrumentingAgentsFor(Page* page)
209 return instrumentationForPage(page);
212 InstrumentingAgents* instrumentingAgentsFor(EventTarget* eventTarget)
216 return instrumentingAgentsFor(eventTarget->executionContext());
219 InstrumentingAgents* instrumentingAgentsFor(RenderObject* renderer)
221 return instrumentingAgentsFor(renderer->frame());
224 InstrumentingAgents* instrumentingAgentsFor(WorkerGlobalScope* workerGlobalScope)
226 if (!workerGlobalScope)
228 return instrumentationForWorkerGlobalScope(workerGlobalScope);
231 InstrumentingAgents* instrumentingAgentsForNonDocumentContext(ExecutionContext* context)
233 if (context->isWorkerGlobalScope())
234 return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context));
238 } // namespace InspectorInstrumentation
240 namespace InstrumentationEvents {
241 const char PaintSetup[] = "PaintSetup";
242 const char RasterTask[] = "RasterTask";
243 const char Paint[] = "Paint";
244 const char Layer[] = "Layer";
245 const char RequestMainThreadFrame[] = "RequestMainThreadFrame";
246 const char BeginFrame[] = "BeginFrame";
247 const char ActivateLayerTree[] = "ActivateLayerTree";
248 const char DrawFrame[] = "DrawFrame";
249 const char EmbedderCallback[] = "EmbedderCallback";
252 namespace InstrumentationEventArguments {
253 const char FrameId[] = "frameId";
254 const char LayerId[] = "layerId";
255 const char LayerTreeId[] = "layerTreeId";
256 const char PageId[] = "pageId";
257 const char CallbackName[] = "callbackName";
260 InstrumentingAgents* instrumentationForPage(Page* page)
262 ASSERT(isMainThread());
263 return page->inspectorController().m_instrumentingAgents.get();
266 InstrumentingAgents* instrumentationForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope)
268 if (WorkerInspectorController* controller = workerGlobalScope->workerInspectorController())
269 return controller->m_instrumentingAgents.get();