83176ce716137a5edbf18b25430f1debdf05b198
[WebKit-https.git] / Source / WebCore / inspector / InspectorTimelineAgent.h
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2014 University of Washington.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef InspectorTimelineAgent_h
33 #define InspectorTimelineAgent_h
34
35 #if ENABLE(INSPECTOR)
36
37 #include "InspectorWebAgentBase.h"
38 #include "InspectorWebBackendDispatchers.h"
39 #include "InspectorWebFrontendDispatchers.h"
40 #include "LayoutRect.h"
41 #include <inspector/InspectorValues.h>
42 #include <inspector/ScriptDebugListener.h>
43 #include <wtf/Vector.h>
44 #include <wtf/WeakPtr.h>
45
46 namespace WebCore {
47
48 class Event;
49 class FloatQuad;
50 class Frame;
51 class InspectorClient;
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     ProbeSample,
101
102     RequestAnimationFrame,
103     CancelAnimationFrame,
104     FireAnimationFrame,
105
106     WebSocketCreate,
107     WebSocketSendHandshakeRequest,
108     WebSocketReceiveHandshakeResponse,
109     WebSocketDestroy
110 };
111
112 class TimelineTimeConverter {
113 public:
114     TimelineTimeConverter()
115         : m_startOffset(0)
116     {
117     }
118     double fromMonotonicallyIncreasingTime(double time) const  { return (time - m_startOffset) * 1000.0; }
119     void reset();
120
121 private:
122     double m_startOffset;
123 };
124
125 class InspectorTimelineAgent
126     : public InspectorAgentBase
127     , public Inspector::InspectorTimelineBackendDispatcherHandler
128     , public Inspector::ScriptDebugListener {
129     WTF_MAKE_NONCOPYABLE(InspectorTimelineAgent);
130 public:
131     enum InspectorType { PageInspector, WorkerInspector };
132
133     InspectorTimelineAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorType, InspectorClient*);
134     ~InspectorTimelineAgent();
135
136     virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
137     virtual void willDestroyFrontendAndBackend(Inspector::InspectorDisconnectReason) override;
138
139     virtual void start(ErrorString*, const int* maxCallStackDepth) override;
140     virtual void stop(ErrorString*) override;
141     virtual void canMonitorMainThread(ErrorString*, bool*) override;
142     virtual void supportsFrameInstrumentation(ErrorString*, bool*) override;
143
144     int id() const { return m_id; }
145
146     void didCommitLoad();
147
148     // Methods called from WebCore.
149     void willCallFunction(const String& scriptName, int scriptLine, Frame*);
150     void didCallFunction(Frame*);
151
152     void willDispatchEvent(const Event&, Frame*);
153     void didDispatchEvent();
154
155     void didBeginFrame();
156     void didCancelFrame();
157
158     void didInvalidateLayout(Frame*);
159     void willLayout(Frame*);
160     void didLayout(RenderObject*);
161
162     void didScheduleStyleRecalculation(Frame*);
163     void willRecalculateStyle(Frame*);
164     void didRecalculateStyle();
165
166     void willPaint(Frame*);
167     void didPaint(RenderObject*, const LayoutRect&);
168
169     void willScroll(Frame*);
170     void didScroll();
171
172     void willComposite();
173     void didComposite();
174
175     void willWriteHTML(unsigned startLine, Frame*);
176     void didWriteHTML(unsigned endLine);
177
178     void didInstallTimer(int timerId, int timeout, bool singleShot, Frame*);
179     void didRemoveTimer(int timerId, Frame*);
180     void willFireTimer(int timerId, Frame*);
181     void didFireTimer();
182
183     void willDispatchXHRReadyStateChangeEvent(const String&, int, Frame*);
184     void didDispatchXHRReadyStateChangeEvent();
185     void willDispatchXHRLoadEvent(const String&, Frame*);
186     void didDispatchXHRLoadEvent();
187
188     void willEvaluateScript(const String&, int, Frame*);
189     void didEvaluateScript(Frame*);
190
191     void didTimeStamp(Frame*, const String&);
192     void didMarkDOMContentEvent(Frame*);
193     void didMarkLoadEvent(Frame*);
194
195     void time(Frame*, const String&);
196     void timeEnd(Frame*, const String&);
197
198     void didScheduleResourceRequest(const String& url, Frame*);
199     void willSendResourceRequest(unsigned long, const ResourceRequest&, Frame*);
200     void willReceiveResourceResponse(unsigned long, const ResourceResponse&, Frame*);
201     void didReceiveResourceResponse();
202     void didFinishLoadingResource(unsigned long, bool didFail, double finishTime, Frame*);
203     void willReceiveResourceData(unsigned long identifier, Frame*, int length);
204     void didReceiveResourceData();
205
206     void didRequestAnimationFrame(int callbackId, Frame*);
207     void didCancelAnimationFrame(int callbackId, Frame*);
208     void willFireAnimationFrame(int callbackId, Frame*);
209     void didFireAnimationFrame();
210
211 #if ENABLE(WEB_SOCKETS)
212     void didCreateWebSocket(unsigned long identifier, const URL&, const String& protocol, Frame*);
213     void willSendWebSocketHandshakeRequest(unsigned long identifier, Frame*);
214     void didReceiveWebSocketHandshakeResponse(unsigned long identifier, Frame*);
215     void didDestroyWebSocket(unsigned long identifier, Frame*);
216 #endif
217
218 protected:
219     // ScriptDebugListener. This is only used to create records for probe samples.
220     virtual void didParseSource(JSC::SourceID, const Script&) override { }
221     virtual void failedToParseSource(const String&, const String&, int, int, const String&) override { }
222     virtual void didPause(JSC::ExecState*, const Deprecated::ScriptValue&, const Deprecated::ScriptValue&) override { }
223     virtual void didContinue() override { }
224
225     virtual void breakpointActionLog(JSC::ExecState*, const String&) override { }
226     virtual void breakpointActionSound(int) override { }
227     virtual void breakpointActionProbe(JSC::ExecState*, const Inspector::ScriptBreakpointAction&, int hitCount, const Deprecated::ScriptValue& result) override;
228
229 private:
230     friend class TimelineRecordStack;
231
232     struct TimelineRecordEntry {
233         TimelineRecordEntry(PassRefPtr<Inspector::InspectorObject> record, PassRefPtr<Inspector::InspectorObject> data, PassRefPtr<Inspector::InspectorArray> children, TimelineRecordType type, size_t usedHeapSizeAtStart)
234             : record(record), data(data), children(children), type(type), usedHeapSizeAtStart(usedHeapSizeAtStart)
235         {
236         }
237         RefPtr<Inspector::InspectorObject> record;
238         RefPtr<Inspector::InspectorObject> data;
239         RefPtr<Inspector::InspectorArray> children;
240         TimelineRecordType type;
241         size_t usedHeapSizeAtStart;
242     };
243
244     void sendEvent(PassRefPtr<Inspector::InspectorObject>);
245     void appendRecord(PassRefPtr<Inspector::InspectorObject> data, TimelineRecordType, bool captureCallStack, Frame*);
246     void pushCurrentRecord(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType, bool captureCallStack, Frame*);
247
248     void setFrameIdentifier(Inspector::InspectorObject* record, Frame*);
249
250     void didCompleteCurrentRecord(TimelineRecordType);
251
252     void setHeapSizeStatistics(Inspector::InspectorObject* record);
253     void commitFrameRecord();
254
255     void addRecordToTimeline(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType);
256     void innerAddRecordToTimeline(PassRefPtr<Inspector::InspectorObject>, TimelineRecordType);
257     void clearRecordStack();
258
259     void localToPageQuad(const RenderObject&, const LayoutRect&, FloatQuad*);
260     const TimelineTimeConverter& timeConverter() const { return m_timeConverter; }
261     double timestamp();
262     Page* page();
263
264     InspectorPageAgent* m_pageAgent;
265     TimelineTimeConverter m_timeConverter;
266
267     std::unique_ptr<Inspector::InspectorTimelineFrontendDispatcher> m_frontendDispatcher;
268     RefPtr<Inspector::InspectorTimelineBackendDispatcher> m_backendDispatcher;
269     double m_timestampOffset;
270
271     Vector<TimelineRecordEntry> m_recordStack;
272
273     int m_id;
274     int m_maxCallStackDepth;
275     RefPtr<Inspector::InspectorObject> m_pendingFrameRecord;
276     InspectorType m_inspectorType;
277     InspectorClient* m_client;
278     WeakPtrFactory<InspectorTimelineAgent> m_weakFactory;
279
280     bool m_enabled;
281     bool m_recordingProfile;
282 };
283
284 } // namespace WebCore
285
286 #endif // !ENABLE(INSPECTOR)
287 #endif // !defined(InspectorTimelineAgent_h)