Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorTimelineAgent.h
1 /*
2 * Copyright (C) 2012 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 #ifndef InspectorTimelineAgent_h
32 #define InspectorTimelineAgent_h
33
34
35 #include "bindings/core/v8/ScriptGCEvent.h"
36 #include "core/InspectorFrontend.h"
37 #include "core/InspectorTypeBuilder.h"
38 #include "core/events/EventPath.h"
39 #include "core/inspector/InspectorBaseAgent.h"
40 #include "core/inspector/ScriptGCEventListener.h"
41 #include "core/inspector/TraceEventDispatcher.h"
42 #include "platform/JSONValues.h"
43 #include "platform/PlatformInstrumentation.h"
44 #include "platform/geometry/LayoutRect.h"
45 #include "wtf/HashMap.h"
46 #include "wtf/HashSet.h"
47 #include "wtf/PassOwnPtr.h"
48 #include "wtf/Vector.h"
49 #include "wtf/WeakPtr.h"
50
51 namespace blink {
52 struct FetchInitiatorInfo;
53 struct TimelineImageInfo;
54 struct TimelineRecordEntry;
55 struct TimelineThreadState;
56
57 class LocalDOMWindow;
58 class Document;
59 class DocumentLoader;
60 class Event;
61 class ExecutionContext;
62 class FloatQuad;
63 class LocalFrame;
64 class GraphicsContext;
65 class GraphicsLayer;
66 class InspectorClient;
67 class InspectorFrontend;
68 class InspectorOverlay;
69 class InspectorPageAgent;
70 class InspectorLayerTreeAgent;
71 class KURL;
72 class ScriptState;
73 class Node;
74 class RenderImage;
75 class RenderObject;
76 class ResourceError;
77 class ResourceLoader;
78 class ResourceRequest;
79 class ResourceResponse;
80 class TimelineRecordStack;
81 class WebSocketHandshakeRequest;
82 class WebSocketHandshakeResponse;
83 class XMLHttpRequest;
84
85 typedef String ErrorString;
86
87 class InspectorTimelineAgent final
88     : public InspectorBaseAgent<InspectorTimelineAgent>
89     , public ScriptGCEventListener
90     , public InspectorBackendDispatcher::TimelineCommandHandler
91     , public PlatformInstrumentationClient {
92     WTF_MAKE_NONCOPYABLE(InspectorTimelineAgent);
93 public:
94     enum InspectorType { PageInspector, WorkerInspector };
95
96     class GPUEvent {
97     public:
98         enum Phase { PhaseBegin, PhaseEnd };
99         GPUEvent(double timestamp, int phase, bool foreign, uint64_t usedGPUMemoryBytes, uint64_t limitGPUMemoryBytes) :
100             timestamp(timestamp),
101             phase(static_cast<Phase>(phase)),
102             foreign(foreign),
103             usedGPUMemoryBytes(usedGPUMemoryBytes),
104             limitGPUMemoryBytes(limitGPUMemoryBytes) { }
105         double timestamp;
106         Phase phase;
107         bool foreign;
108         uint64_t usedGPUMemoryBytes;
109         uint64_t limitGPUMemoryBytes;
110     };
111
112     static PassOwnPtrWillBeRawPtr<InspectorTimelineAgent> create(InspectorPageAgent* pageAgent, InspectorLayerTreeAgent* layerTreeAgent,
113         InspectorOverlay* overlay, InspectorType type, InspectorClient* client)
114     {
115         return adoptPtrWillBeNoop(new InspectorTimelineAgent(pageAgent, layerTreeAgent, overlay, type, client));
116     }
117
118     virtual ~InspectorTimelineAgent();
119     virtual void trace(Visitor*) override;
120
121     virtual void setFrontend(InspectorFrontend*) override;
122     virtual void clearFrontend() override;
123     virtual void restore() override;
124
125     virtual void enable(ErrorString*) override;
126     virtual void disable(ErrorString*) override;
127     virtual void start(ErrorString*, const int* maxCallStackDepth, const bool* bufferEvents, const String* liveEvents, const bool* includeCounters, const bool* includeGPUEvents) override;
128     virtual void stop(ErrorString*) override;
129
130     void setLayerTreeId(int layerTreeId) { m_layerTreeId = layerTreeId; }
131     int id() const { return m_id; }
132
133     void didCommitLoad();
134
135     // Methods called from WebCore.
136     bool willCallFunction(ExecutionContext*, int scriptId, const String& scriptName, int scriptLine);
137     void didCallFunction();
138
139     bool willDispatchEvent(Document* document, const Event& event, LocalDOMWindow* window, Node* node, const EventPath& eventPath);
140     bool willDispatchEventOnWindow(const Event& event, LocalDOMWindow* window);
141     void didDispatchEvent();
142     void didDispatchEventOnWindow();
143
144     void didBeginFrame(int frameId);
145     void didCancelFrame();
146
147     void didInvalidateLayout(LocalFrame*);
148     bool willLayout(LocalFrame*);
149     void didLayout(RenderObject*);
150
151     void willUpdateLayerTree();
152     void layerTreeDidChange();
153     void didUpdateLayerTree();
154
155     void didScheduleStyleRecalculation(Document*);
156     bool willRecalculateStyle(Document*);
157     void didRecalculateStyle(int elementCount);
158
159     void willPaint(RenderObject*, const GraphicsLayer*);
160     void didPaint(RenderObject*, const GraphicsLayer*, GraphicsContext*, const LayoutRect&);
161
162     void willPaintImage(RenderImage*);
163     void didPaintImage();
164
165     void willScrollLayer(RenderObject*);
166     void didScrollLayer();
167
168     void willComposite();
169     void didComposite();
170
171     bool willWriteHTML(Document*, unsigned startLine);
172     void didWriteHTML(unsigned endLine);
173
174     void didInstallTimer(ExecutionContext*, int timerId, int timeout, bool singleShot);
175     void didRemoveTimer(ExecutionContext*, int timerId);
176     bool willFireTimer(ExecutionContext*, int timerId);
177     void didFireTimer();
178
179     bool willDispatchXHRReadyStateChangeEvent(ExecutionContext*, XMLHttpRequest*);
180     void didDispatchXHRReadyStateChangeEvent();
181     bool willDispatchXHRLoadEvent(ExecutionContext*, XMLHttpRequest*);
182     void didDispatchXHRLoadEvent();
183
184     bool willEvaluateScript(LocalFrame*, const String&, int);
185     void didEvaluateScript();
186
187     void consoleTimeStamp(ExecutionContext*, const String& title);
188     void domContentLoadedEventFired(LocalFrame*);
189     void loadEventFired(LocalFrame*);
190
191     void consoleTime(ExecutionContext*, const String&);
192     void consoleTimeEnd(ExecutionContext*, const String&, ScriptState*);
193     void consoleTimeline(ExecutionContext*, const String& title, ScriptState*);
194     void consoleTimelineEnd(ExecutionContext*, const String& title, ScriptState*);
195
196     void willSendRequest(unsigned long, DocumentLoader*, const ResourceRequest&, const ResourceResponse&, const FetchInitiatorInfo&);
197     void didReceiveResourceResponse(LocalFrame*, unsigned long, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
198     void didFinishLoading(unsigned long, DocumentLoader*, double monotonicFinishTime, int64_t);
199     void didFailLoading(unsigned long identifier, const ResourceError&);
200     void didReceiveData(LocalFrame*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
201
202     void didRequestAnimationFrame(Document*, int callbackId);
203     void didCancelAnimationFrame(Document*, int callbackId);
204     bool willFireAnimationFrame(Document*, int callbackId);
205     void didFireAnimationFrame();
206
207     void willProcessTask();
208     void didProcessTask();
209
210     void didCreateWebSocket(Document*, unsigned long identifier, const KURL&, const String& protocol);
211     void willSendWebSocketHandshakeRequest(Document*, unsigned long identifier, const WebSocketHandshakeRequest*);
212     void didReceiveWebSocketHandshakeResponse(Document*, unsigned long identifier, const WebSocketHandshakeRequest*, const WebSocketHandshakeResponse*);
213     void didCloseWebSocket(Document*, unsigned long identifier);
214
215     void processGPUEvent(const GPUEvent&);
216
217     // ScriptGCEventListener methods.
218     virtual void didGC(double, double, size_t) override;
219
220     // PlatformInstrumentationClient methods.
221     virtual void willDecodeImage(const String& imageType) override;
222     virtual void didDecodeImage() override;
223     virtual void willResizeImage(bool shouldCache) override;
224     virtual void didResizeImage() override;
225
226 private:
227
228     friend class TimelineRecordStack;
229
230     InspectorTimelineAgent(InspectorPageAgent*, InspectorLayerTreeAgent*, InspectorOverlay*, InspectorType, InspectorClient*);
231
232     // Trace event handlers
233     void onBeginImplSideFrame(const TraceEventDispatcher::TraceEvent&);
234     void onPaintSetupBegin(const TraceEventDispatcher::TraceEvent&);
235     void onPaintSetupEnd(const TraceEventDispatcher::TraceEvent&);
236     void onRasterTaskBegin(const TraceEventDispatcher::TraceEvent&);
237     void onRasterTaskEnd(const TraceEventDispatcher::TraceEvent&);
238     void onImageDecodeBegin(const TraceEventDispatcher::TraceEvent&);
239     void onImageDecodeEnd(const TraceEventDispatcher::TraceEvent&);
240     void onLayerDeleted(const TraceEventDispatcher::TraceEvent&);
241     void onDrawLazyPixelRef(const TraceEventDispatcher::TraceEvent&);
242     void onDecodeLazyPixelRefBegin(const TraceEventDispatcher::TraceEvent&);
243     void onDecodeLazyPixelRefEnd(const TraceEventDispatcher::TraceEvent&);
244     void onRequestMainThreadFrame(const TraceEventDispatcher::TraceEvent&);
245     void onActivateLayerTree(const TraceEventDispatcher::TraceEvent&);
246     void onDrawFrame(const TraceEventDispatcher::TraceEvent&);
247     void onLazyPixelRefDeleted(const TraceEventDispatcher::TraceEvent&);
248     void onEmbedderCallbackBegin(const TraceEventDispatcher::TraceEvent&);
249     void onEmbedderCallbackEnd(const TraceEventDispatcher::TraceEvent&);
250
251     void didFinishLoadingResource(unsigned long, bool didFail, double finishTime);
252
253     void sendEvent(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>);
254     void appendRecord(PassRefPtr<JSONObject> data, const String& type, bool captureCallStack, LocalFrame*);
255     void pushCurrentRecord(PassRefPtr<JSONObject> data, const String& type, bool captureCallStack, LocalFrame*, bool hasLowLevelDetails = false);
256     TimelineThreadState& threadState(ThreadIdentifier);
257
258     void setFrameIdentifier(TypeBuilder::Timeline::TimelineEvent* record, LocalFrame*);
259     void populateImageDetails(JSONObject* data, const RenderImage&);
260
261     void didCompleteCurrentRecord(const String& type);
262     void unwindRecordStack();
263
264     void commitFrameRecord();
265
266     void addRecordToTimeline(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>, double ts);
267     void innerAddRecordToTimeline(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>);
268     PassRefPtr<TypeBuilder::Timeline::TimelineEvent> createCountersUpdate();
269     void clearRecordStack();
270     PassRefPtr<TypeBuilder::Timeline::TimelineEvent> createRecordForEvent(const TraceEventDispatcher::TraceEvent&, const String& type, PassRefPtr<JSONObject> data);
271
272     void localToPageQuad(const RenderObject& renderer, const LayoutRect&, FloatQuad*);
273     long long nodeId(Node*);
274     long long nodeId(RenderObject*);
275
276     double timestamp();
277
278     LocalFrame* mainFrame() const;
279
280     bool isStarted();
281     void innerStart();
282     void innerStop(bool fromConsole);
283     void setLiveEvents(const String&);
284
285     RawPtrWillBeMember<InspectorPageAgent> m_pageAgent;
286     RawPtrWillBeMember<InspectorLayerTreeAgent> m_layerTreeAgent;
287     InspectorFrontend::Timeline* m_frontend;
288     InspectorClient* m_client;
289     InspectorOverlay* m_overlay;
290     InspectorType m_inspectorType;
291
292     int m_id;
293     unsigned long long m_layerTreeId;
294
295     int m_maxCallStackDepth;
296
297     Vector<TimelineRecordEntry> m_recordStack;
298     RefPtr<TypeBuilder::Array<TypeBuilder::Timeline::TimelineEvent> > m_bufferedEvents;
299     Vector<String> m_consoleTimelines;
300
301     unsigned m_platformInstrumentationClientInstalledAtStackDepth;
302     RefPtr<TypeBuilder::Timeline::TimelineEvent> m_pendingFrameRecord;
303     RefPtr<TypeBuilder::Timeline::TimelineEvent> m_pendingGPURecord;
304     typedef HashMap<unsigned long long, TimelineImageInfo> PixelRefToImageInfoMap;
305     PixelRefToImageInfoMap m_pixelRefToImageInfo;
306     RenderImage* m_imageBeingPainted;
307     HashMap<unsigned long long, long long> m_layerToNodeMap;
308     double m_paintSetupStart;
309     double m_paintSetupEnd;
310     RefPtr<JSONObject> m_gpuTask;
311     RefPtr<JSONValue> m_pendingLayerTreeData;
312     typedef WillBeHeapHashMap<ThreadIdentifier, TimelineThreadState> ThreadStateMap;
313     ThreadStateMap m_threadStates;
314     bool m_mayEmitFirstPaint;
315     HashSet<String> m_liveEvents;
316     double m_lastProgressTimestamp;
317 };
318
319 } // namespace blink
320
321 #endif // !defined(InspectorTimelineAgent_h)