Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorInstrumentation.cpp
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32 #include "core/inspector/InspectorInstrumentation.h"
33
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"
51
52 namespace blink {
53
54 namespace {
55 static HashSet<InstrumentingAgents*>* instrumentingAgentsSet = 0;
56 }
57
58 namespace InspectorInstrumentation {
59 int FrontendCounter::s_frontendCounter = 0;
60 }
61
62 InspectorInstrumentationCookie::InspectorInstrumentationCookie()
63     : m_instrumentingAgents(nullptr)
64     , m_timelineAgentId(0)
65 {
66 }
67
68 InspectorInstrumentationCookie::InspectorInstrumentationCookie(InstrumentingAgents* agents, int timelineAgentId)
69     : m_instrumentingAgents(agents)
70     , m_timelineAgentId(timelineAgentId)
71 {
72 }
73
74 InspectorInstrumentationCookie::InspectorInstrumentationCookie(const InspectorInstrumentationCookie& other)
75     : m_instrumentingAgents(other.m_instrumentingAgents)
76     , m_timelineAgentId(other.m_timelineAgentId)
77 {
78 }
79
80 InspectorInstrumentationCookie& InspectorInstrumentationCookie::operator=(const InspectorInstrumentationCookie& other)
81 {
82     if (this != &other) {
83         m_instrumentingAgents = other.m_instrumentingAgents;
84         m_timelineAgentId = other.m_timelineAgentId;
85     }
86     return *this;
87 }
88
89 InspectorInstrumentationCookie::~InspectorInstrumentationCookie()
90 {
91 }
92
93 namespace InspectorInstrumentation {
94
95 bool isDebuggerPausedImpl(InstrumentingAgents* instrumentingAgents)
96 {
97     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
98         return debuggerAgent->isPaused();
99     return false;
100 }
101
102 void didReceiveResourceResponseButCanceledImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
103 {
104     didReceiveResourceResponse(frame, identifier, loader, r, 0);
105 }
106
107 void continueAfterXFrameOptionsDeniedImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
108 {
109     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
110 }
111
112 void continueWithPolicyIgnoreImpl(LocalFrame* frame, DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r)
113 {
114     didReceiveResourceResponseButCanceledImpl(frame, loader, identifier, r);
115 }
116
117 void willDestroyResourceImpl(Resource* cachedResource)
118 {
119     if (!instrumentingAgentsSet)
120         return;
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);
126     }
127 }
128
129 bool collectingHTMLParseErrorsImpl(InstrumentingAgents* instrumentingAgents)
130 {
131     if (InspectorInspectorAgent* inspectorAgent = instrumentingAgents->inspectorInspectorAgent())
132         return inspectorAgent->hasFrontend();
133     return false;
134 }
135
136 PassOwnPtr<ScriptSourceCode> preprocessImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const ScriptSourceCode& sourceCode)
137 {
138     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
139         return debuggerAgent->preprocess(frame, sourceCode);
140     return PassOwnPtr<ScriptSourceCode>();
141 }
142
143 String preprocessEventListenerImpl(InstrumentingAgents* instrumentingAgents, LocalFrame* frame, const String& source, const String& url, const String& functionName)
144 {
145     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
146         return debuggerAgent->preprocessEventListener(frame, source, url, functionName);
147     return source;
148 }
149
150 void appendAsyncCallStack(ExecutionContext* executionContext, ScriptCallStack* callStack)
151 {
152     InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
153     if (!instrumentingAgents)
154         return;
155     if (InspectorDebuggerAgent* debuggerAgent = instrumentingAgents->inspectorDebuggerAgent())
156         callStack->setAsyncCallStack(debuggerAgent->currentAsyncStackTraceForConsole());
157 }
158
159 bool canvasAgentEnabled(ExecutionContext* executionContext)
160 {
161     InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
162     return instrumentingAgents && instrumentingAgents->inspectorCanvasAgent();
163 }
164
165 bool consoleAgentEnabled(ExecutionContext* executionContext)
166 {
167     InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
168     InspectorConsoleAgent* consoleAgent = instrumentingAgents ? instrumentingAgents->inspectorConsoleAgent() : 0;
169     return consoleAgent && consoleAgent->enabled();
170 }
171
172 bool timelineAgentEnabled(ExecutionContext* executionContext)
173 {
174     InstrumentingAgents* instrumentingAgents = instrumentingAgentsFor(executionContext);
175     return instrumentingAgents && instrumentingAgents->inspectorTimelineAgent();
176 }
177
178 void registerInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
179 {
180     if (!instrumentingAgentsSet)
181         instrumentingAgentsSet = new HashSet<InstrumentingAgents*>();
182     instrumentingAgentsSet->add(instrumentingAgents);
183 }
184
185 void unregisterInstrumentingAgents(InstrumentingAgents* instrumentingAgents)
186 {
187     if (!instrumentingAgentsSet)
188         return;
189     instrumentingAgentsSet->remove(instrumentingAgents);
190     if (instrumentingAgentsSet->isEmpty()) {
191         delete instrumentingAgentsSet;
192         instrumentingAgentsSet = 0;
193     }
194 }
195
196 InspectorTimelineAgent* retrieveTimelineAgent(const InspectorInstrumentationCookie& cookie)
197 {
198     if (!cookie.instrumentingAgents())
199         return 0;
200     InspectorTimelineAgent* timelineAgent = cookie.instrumentingAgents()->inspectorTimelineAgent();
201     if (timelineAgent && cookie.hasMatchingTimelineAgentId(timelineAgent->id()))
202         return timelineAgent;
203     return 0;
204 }
205
206 InstrumentingAgents* instrumentingAgentsFor(Page* page)
207 {
208     if (!page)
209         return 0;
210     return instrumentationForPage(page);
211 }
212
213 InstrumentingAgents* instrumentingAgentsFor(EventTarget* eventTarget)
214 {
215     if (!eventTarget)
216         return 0;
217     return instrumentingAgentsFor(eventTarget->executionContext());
218 }
219
220 InstrumentingAgents* instrumentingAgentsFor(RenderObject* renderer)
221 {
222     return instrumentingAgentsFor(renderer->frame());
223 }
224
225 InstrumentingAgents* instrumentingAgentsFor(WorkerGlobalScope* workerGlobalScope)
226 {
227     if (!workerGlobalScope)
228         return 0;
229     return instrumentationForWorkerGlobalScope(workerGlobalScope);
230 }
231
232 InstrumentingAgents* instrumentingAgentsFor(FrameHost* host)
233 {
234     return instrumentationForPage(&host->page());
235 }
236
237 InstrumentingAgents* instrumentingAgentsForNonDocumentContext(ExecutionContext* context)
238 {
239     if (context->isWorkerGlobalScope())
240         return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context));
241     return 0;
242 }
243
244 } // namespace InspectorInstrumentation
245
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";
256 };
257
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";
264 };
265
266 InstrumentingAgents* instrumentationForPage(Page* page)
267 {
268     ASSERT(isMainThread());
269     return page->inspectorController().m_instrumentingAgents.get();
270 }
271
272 InstrumentingAgents* instrumentationForWorkerGlobalScope(WorkerGlobalScope* workerGlobalScope)
273 {
274     if (WorkerInspectorController* controller = workerGlobalScope->workerInspectorController())
275         return controller->m_instrumentingAgents.get();
276     return 0;
277 }
278
279 } // namespace blink
280