e39999300f013ba766c847be1592412bb430d9a4
[WebKit-https.git] / Source / WebCore / 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 #if ENABLE(INSPECTOR)
35
36 #include "InspectorWebAgentBase.h"
37 #include "InspectorWebBackendDispatchers.h"
38 #include "InspectorWebFrontendDispatchers.h"
39 #include "LayoutRect.h"
40 #include <inspector/InspectorValues.h>
41 #include <wtf/PassOwnPtr.h>
42 #include <wtf/Vector.h>
43 #include <wtf/WeakPtr.h>
44
45 namespace WebCore {
46
47 class Event;
48 class FloatQuad;
49 class Frame;
50 class InspectorClient;
51 class InspectorMemoryAgent;
52 class InspectorPageAgent;
53 class InstrumentingAgents;
54 class IntRect;
55 class URL;
56 class Page;
57 class RenderObject;
58 class ResourceRequest;
59 class ResourceResponse;
60
61 typedef String ErrorString;
62
63 enum class TimelineRecordType {
64     EventDispatch,
65     BeginFrame,
66     ScheduleStyleRecalculation,
67     RecalculateStyles,
68     InvalidateLayout,
69     Layout,
70     Paint,
71     ScrollLayer,
72     ResizeImage,
73     CompositeLayers,
74
75     ParseHTML,
76
77     TimerInstall,
78     TimerRemove,
79     TimerFire,
80
81     EvaluateScript,
82
83     MarkLoad,
84     MarkDOMContent,
85
86     TimeStamp,
87     Time,
88     TimeEnd,
89
90     ScheduleResourceRequest,
91     ResourceSendRequest,
92     ResourceReceiveResponse,
93     ResourceReceivedData,
94     ResourceFinish,
95
96     XHRReadyStateChange,
97     XHRLoad,
98
99     FunctionCall,
100
101     RequestAnimationFrame,
102     CancelAnimationFrame,
103     FireAnimationFrame,
104
105     WebSocketCreate,
106     WebSocketSendHandshakeRequest,
107     WebSocketReceiveHandshakeResponse,
108     WebSocketDestroy
109 };
110
111 class TimelineTimeConverter {
112 public:
113     TimelineTimeConverter()
114         : m_startOffset(0)
115     {
116     }
117     double fromMonotonicallyIncreasingTime(double time) const  { return (time - m_startOffset) * 1000.0; }
118     void reset();
119
120 private:
121     double m_startOffset;
122 };
123
124 class InspectorTimelineAgent
125     : public InspectorAgentBase
126     , public Inspector::InspectorTimelineBackendDispatcherHandler {
127     WTF_MAKE_NONCOPYABLE(InspectorTimelineAgent);
128 public:
129     enum InspectorType { PageInspector, WorkerInspector };
130
131     InspectorTimelineAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorMemoryAgent*, InspectorType, InspectorClient*);
132     ~InspectorTimelineAgent();
133
134     virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
135     virtual void willDestroyFrontendAndBackend() override;
136
137     virtual void start(ErrorString*, const int* maxCallStackDepth, const bool* includeDomCounters);
138     virtual void stop(ErrorString*);
139     virtual void canMonitorMainThread(ErrorString*, bool*);
140     virtual void supportsFrameInstrumentation(ErrorString*, bool*);
141
142     int id() const { return m_id; }
143
144     void didCommitLoad();
145
146     // Methods called from WebCore.
147     void willCallFunction(const String& scriptName, int scriptLine, Frame*);
148     void didCallFunction();
149
150     void willDispatchEvent(const Event&, Frame*);
151     void didDispatchEvent();
152
153     void didBeginFrame();
154     void didCancelFrame();
155
156     void didInvalidateLayout(Frame*);
157     void willLayout(Frame*);
158     void didLayout(RenderObject*);
159
160     void didScheduleStyleRecalculation(Frame*);
161     void willRecalculateStyle(Frame*);
162     void didRecalculateStyle();
163
164     void willPaint(Frame*);
165     void didPaint(RenderObject*, const LayoutRect&);
166
167     void willScroll(Frame*);
168     void didScroll();
169
170     void willComposite();
171     void didComposite();
172
173     void willWriteHTML(unsigned startLine, Frame*);
174     void didWriteHTML(unsigned endLine);
175
176     void didInstallTimer(int timerId, int timeout, bool singleShot, Frame*);
177     void didRemoveTimer(int timerId, Frame*);
178     void willFireTimer(int timerId, Frame*);
179     void didFireTimer();
180
181     void willDispatchXHRReadyStateChangeEvent(const String&, int, Frame*);
182     void didDispatchXHRReadyStateChangeEvent();
183     void willDispatchXHRLoadEvent(const String&, Frame*);
184     void didDispatchXHRLoadEvent();
185
186     void willEvaluateScript(const String&, int, Frame*);
187     void didEvaluateScript();
188
189     void didTimeStamp(Frame*, const String&);
190     void didMarkDOMContentEvent(Frame*);
191     void didMarkLoadEvent(Frame*);
192
193     void time(Frame*, const String&);
194     void timeEnd(Frame*, const String&);
195
196     void didScheduleResourceRequest(const String& url, Frame*);
197     void willSendResourceRequest(unsigned long, const ResourceRequest&, Frame*);
198     void willReceiveResourceResponse(unsigned long, const ResourceResponse&, Frame*);
199     void didReceiveResourceResponse();
200     void didFinishLoadingResource(unsigned long, bool didFail, double finishTime, Frame*);
201     void willReceiveResourceData(unsigned long identifier, Frame*, int length);
202     void didReceiveResourceData();
203
204     void didRequestAnimationFrame(int callbackId, Frame*);
205     void didCancelAnimationFrame(int callbackId, Frame*);
206     void willFireAnimationFrame(int callbackId, Frame*);
207     void didFireAnimationFrame();
208
209 #if ENABLE(WEB_SOCKETS)
210     void didCreateWebSocket(unsigned long identifier, const URL&, const String& protocol, Frame*);
211     void willSendWebSocketHandshakeRequest(unsigned long identifier, Frame*);
212     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, Frame*);
213     void didDestroyWebSocket(unsigned long identifier, Frame*);
214 #endif
215
216 private:
217     friend class TimelineRecordStack;
218
219     struct TimelineRecordEntry {
220         TimelineRecordEntry(PassRefPtr<Inspector::InspectorObject> record, PassRefPtr<Inspector::InspectorObject> data, PassRefPtr<Inspector::InspectorArray> children, TimelineRecordType type, size_t usedHeapSizeAtStart)
221             : record(record), data(data), children(children), type(type), usedHeapSizeAtStart(usedHeapSizeAtStart)
222         {
223         }
224         RefPtr<Inspector::InspectorObject> record;
225         RefPtr<Inspector::InspectorObject> data;
226         RefPtr<Inspector::InspectorArray> children;
227         TimelineRecordType type;
228         size_t usedHeapSizeAtStart;
229     };
230
231     void sendEvent(PassRefPtr<Inspector::InspectorObject>);
232     void appendRecord(PassRefPtr<Inspector::InspectorObject> data, TimelineRecordType, bool captureCallStack, Frame*);
233     void pushCurrentRecord(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType, bool captureCallStack, Frame*);
234
235     void setDOMCounters(Inspector::TypeBuilder::Timeline::TimelineEvent* record);
236     void setFrameIdentifier(Inspector::InspectorObject* record, Frame*);
237
238     void didCompleteCurrentRecord(TimelineRecordType);
239
240     void setHeapSizeStatistics(Inspector::InspectorObject* record);
241     void commitFrameRecord();
242
243     void addRecordToTimeline(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType);
244     void innerAddRecordToTimeline(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType);
245     void clearRecordStack();
246
247     void localToPageQuad(const RenderObject&, const LayoutRect&, FloatQuad*);
248     const TimelineTimeConverter& timeConverter() const { return m_timeConverter; }
249     double timestamp();
250     Page* page();
251
252     InspectorPageAgent* m_pageAgent;
253     InspectorMemoryAgent* m_memoryAgent;
254     TimelineTimeConverter m_timeConverter;
255
256     std::unique_ptr<Inspector::InspectorTimelineFrontendDispatcher> m_frontendDispatcher;
257     RefPtr<Inspector::InspectorTimelineBackendDispatcher> m_backendDispatcher;
258     double m_timestampOffset;
259
260     Vector<TimelineRecordEntry> m_recordStack;
261
262     int m_id;
263     int m_maxCallStackDepth;
264     RefPtr<Inspector::InspectorObject> m_pendingFrameRecord;
265     InspectorType m_inspectorType;
266     InspectorClient* m_client;
267     WeakPtrFactory<InspectorTimelineAgent> m_weakFactory;
268
269     bool m_enabled;
270     bool m_includeDOMCounters;
271 };
272
273 } // namespace WebCore
274
275 #endif // !ENABLE(INSPECTOR)
276 #endif // !defined(InspectorTimelineAgent_h)