d039e86950d1ef763fb9dd4d1f61ffff0f22c925
[WebKit-https.git] / Source / WebCore / inspector / InspectorTimelineAgent.cpp
1 /*
2 * Copyright (C) 2013 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
33 #if ENABLE(INSPECTOR)
34
35 #include "InspectorTimelineAgent.h"
36
37 #include "Event.h"
38 #include "Frame.h"
39 #include "FrameView.h"
40 #include "IdentifiersFactory.h"
41 #include "InspectorClient.h"
42 #include "InspectorCounters.h"
43 #include "InspectorFrontend.h"
44 #include "InspectorInstrumentation.h"
45 #include "InspectorMemoryAgent.h"
46 #include "InspectorPageAgent.h"
47 #include "InspectorState.h"
48 #include "InstrumentingAgents.h"
49 #include "IntRect.h"
50 #include "MemoryUsageSupport.h"
51 #include "RenderObject.h"
52 #include "RenderView.h"
53 #include "ResourceRequest.h"
54 #include "ResourceResponse.h"
55 #include "TimelineRecordFactory.h"
56 #include "TimelineTraceEventProcessor.h"
57
58 #include <wtf/CurrentTime.h>
59
60 namespace WebCore {
61
62 namespace TimelineAgentState {
63 static const char timelineAgentEnabled[] = "timelineAgentEnabled";
64 static const char timelineMaxCallStackDepth[] = "timelineMaxCallStackDepth";
65 static const char includeDomCounters[] = "includeDomCounters";
66 static const char includeNativeMemoryStatistics[] = "includeNativeMemoryStatistics";
67 }
68
69 // Must be kept in sync with WebInspector.TimelineModel.RecordType in TimelineModel.js
70 namespace TimelineRecordType {
71 static const char Program[] = "Program";
72
73 static const char EventDispatch[] = "EventDispatch";
74 static const char BeginFrame[] = "BeginFrame";
75 static const char ScheduleStyleRecalculation[] = "ScheduleStyleRecalculation";
76 static const char RecalculateStyles[] = "RecalculateStyles";
77 static const char InvalidateLayout[] = "InvalidateLayout";
78 static const char Layout[] = "Layout";
79 static const char Paint[] = "Paint";
80 static const char ScrollLayer[] = "ScrollLayer";
81 static const char ResizeImage[] = "ResizeImage";
82 static const char CompositeLayers[] = "CompositeLayers";
83
84 static const char ParseHTML[] = "ParseHTML";
85
86 static const char TimerInstall[] = "TimerInstall";
87 static const char TimerRemove[] = "TimerRemove";
88 static const char TimerFire[] = "TimerFire";
89
90 static const char EvaluateScript[] = "EvaluateScript";
91
92 static const char MarkLoad[] = "MarkLoad";
93 static const char MarkDOMContent[] = "MarkDOMContent";
94
95 static const char TimeStamp[] = "TimeStamp";
96 static const char Time[] = "Time";
97 static const char TimeEnd[] = "TimeEnd";
98
99 static const char ScheduleResourceRequest[] = "ScheduleResourceRequest";
100 static const char ResourceSendRequest[] = "ResourceSendRequest";
101 static const char ResourceReceiveResponse[] = "ResourceReceiveResponse";
102 static const char ResourceReceivedData[] = "ResourceReceivedData";
103 static const char ResourceFinish[] = "ResourceFinish";
104
105 static const char XHRReadyStateChange[] = "XHRReadyStateChange";
106 static const char XHRLoad[] = "XHRLoad";
107
108 static const char FunctionCall[] = "FunctionCall";
109 static const char GCEvent[] = "GCEvent";
110
111 static const char RequestAnimationFrame[] = "RequestAnimationFrame";
112 static const char CancelAnimationFrame[] = "CancelAnimationFrame";
113 static const char FireAnimationFrame[] = "FireAnimationFrame";
114
115 static const char WebSocketCreate[] = "WebSocketCreate";
116 static const char WebSocketSendHandshakeRequest[] = "WebSocketSendHandshakeRequest";
117 static const char WebSocketReceiveHandshakeResponse[] = "WebSocketReceiveHandshakeResponse";
118 static const char WebSocketDestroy[] = "WebSocketDestroy";
119
120 // Event names visible to other modules.
121 const char DecodeImage[] = "DecodeImage";
122 const char Rasterize[] = "Rasterize";
123 }
124
125 void TimelineTimeConverter::reset()
126 {
127     m_startOffset = monotonicallyIncreasingTime() - currentTime();
128 }
129
130 void InspectorTimelineAgent::pushGCEventRecords()
131 {
132     if (!m_gcEvents.size())
133         return;
134
135     GCEvents events = m_gcEvents;
136     m_gcEvents.clear();
137     for (GCEvents::iterator i = events.begin(); i != events.end(); ++i) {
138         RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(m_timeConverter.fromMonotonicallyIncreasingTime(i->startTime), m_maxCallStackDepth);
139         record->setObject("data", TimelineRecordFactory::createGCEventData(i->collectedBytes));
140         record->setNumber("endTime", m_timeConverter.fromMonotonicallyIncreasingTime(i->endTime));
141         addRecordToTimeline(record.release(), TimelineRecordType::GCEvent);
142     }
143 }
144
145 void InspectorTimelineAgent::didGC(double startTime, double endTime, size_t collectedBytesCount)
146 {
147     m_gcEvents.append(GCEvent(startTime, endTime, collectedBytesCount));
148 }
149
150 InspectorTimelineAgent::~InspectorTimelineAgent()
151 {
152     clearFrontend();
153 }
154
155 void InspectorTimelineAgent::setFrontend(InspectorFrontend* frontend)
156 {
157     m_frontend = frontend->timeline();
158 }
159
160 void InspectorTimelineAgent::clearFrontend()
161 {
162     ErrorString error;
163     stop(&error);
164     m_frontend = 0;
165 }
166
167 void InspectorTimelineAgent::restore()
168 {
169     if (m_state->getBoolean(TimelineAgentState::timelineAgentEnabled)) {
170         m_maxCallStackDepth = m_state->getLong(TimelineAgentState::timelineMaxCallStackDepth);
171         ErrorString error;
172         bool includeDomCounters = m_state->getBoolean(TimelineAgentState::includeDomCounters);
173         bool includeNativeMemoryStatistics = m_state->getBoolean(TimelineAgentState::includeNativeMemoryStatistics);
174         start(&error, &m_maxCallStackDepth, &includeDomCounters, &includeNativeMemoryStatistics);
175     }
176 }
177
178 void InspectorTimelineAgent::start(ErrorString*, const int* maxCallStackDepth, const bool* includeDomCounters, const bool* includeNativeMemoryStatistics)
179 {
180     if (!m_frontend)
181         return;
182
183     if (maxCallStackDepth && *maxCallStackDepth > 0)
184         m_maxCallStackDepth = *maxCallStackDepth;
185     else
186         m_maxCallStackDepth = 5;
187     m_state->setLong(TimelineAgentState::timelineMaxCallStackDepth, m_maxCallStackDepth);
188     m_state->setBoolean(TimelineAgentState::includeDomCounters, includeDomCounters && *includeDomCounters);
189     m_state->setBoolean(TimelineAgentState::includeNativeMemoryStatistics, includeNativeMemoryStatistics && *includeNativeMemoryStatistics);
190     m_timeConverter.reset();
191
192     m_instrumentingAgents->setInspectorTimelineAgent(this);
193     ScriptGCEvent::addEventListener(this);
194     m_state->setBoolean(TimelineAgentState::timelineAgentEnabled, true);
195     if (m_client && m_pageAgent)
196         m_traceEventProcessor = adoptRef(new TimelineTraceEventProcessor(m_weakFactory.createWeakPtr(), m_client));
197 }
198
199 void InspectorTimelineAgent::stop(ErrorString*)
200 {
201     if (!m_state->getBoolean(TimelineAgentState::timelineAgentEnabled))
202         return;
203
204     m_traceEventProcessor->shutdown();
205     m_traceEventProcessor.clear();
206     m_weakFactory.revokeAll();
207     m_instrumentingAgents->setInspectorTimelineAgent(0);
208     ScriptGCEvent::removeEventListener(this);
209
210     clearRecordStack();
211     m_gcEvents.clear();
212
213     m_state->setBoolean(TimelineAgentState::timelineAgentEnabled, false);
214 }
215
216 void InspectorTimelineAgent::canMonitorMainThread(ErrorString*, bool* result)
217 {
218     *result = m_client && m_client->canMonitorMainThread();
219 }
220
221 void InspectorTimelineAgent::supportsFrameInstrumentation(ErrorString*, bool* result)
222 {
223     *result = m_client && m_client->supportsFrameInstrumentation();
224 }
225
226 void InspectorTimelineAgent::didBeginFrame()
227 {
228 #if PLATFORM(CHROMIUM)
229     TRACE_EVENT_INSTANT0("webkit", InstrumentationEvents::BeginFrame);
230 #endif
231     m_pendingFrameRecord = TimelineRecordFactory::createGenericRecord(timestamp(), 0);
232 }
233
234 void InspectorTimelineAgent::didCancelFrame()
235 {
236     m_pendingFrameRecord.clear();
237 }
238
239 void InspectorTimelineAgent::willCallFunction(const String& scriptName, int scriptLine, Frame* frame)
240 {
241     pushCurrentRecord(TimelineRecordFactory::createFunctionCallData(scriptName, scriptLine), TimelineRecordType::FunctionCall, true, frame);
242 }
243
244 void InspectorTimelineAgent::didCallFunction()
245 {
246     didCompleteCurrentRecord(TimelineRecordType::FunctionCall);
247 }
248
249 void InspectorTimelineAgent::willDispatchEvent(const Event& event, Frame* frame)
250 {
251     pushCurrentRecord(TimelineRecordFactory::createEventDispatchData(event), TimelineRecordType::EventDispatch, false, frame);
252 }
253
254 void InspectorTimelineAgent::didDispatchEvent()
255 {
256     didCompleteCurrentRecord(TimelineRecordType::EventDispatch);
257 }
258
259 void InspectorTimelineAgent::didInvalidateLayout(Frame* frame)
260 {
261     appendRecord(InspectorObject::create(), TimelineRecordType::InvalidateLayout, true, frame);
262 }
263
264 void InspectorTimelineAgent::willLayout(Frame* frame)
265 {
266     RenderObject* root = frame->view()->layoutRoot();
267     bool partialLayout = !!root;
268
269     if (!partialLayout)
270         root = frame->contentRenderer();
271
272     unsigned dirtyObjects = 0;
273     unsigned totalObjects = 0;
274     for (RenderObject* o = root; o; o = o->nextInPreOrder(root)) {
275         ++totalObjects;
276         if (o->needsLayout())
277             ++dirtyObjects;
278     }
279     pushCurrentRecord(TimelineRecordFactory::createLayoutData(dirtyObjects, totalObjects, partialLayout), TimelineRecordType::Layout, true, frame);
280 }
281
282 void InspectorTimelineAgent::didLayout(RenderObject* root)
283 {
284     if (m_recordStack.isEmpty())
285         return;
286     TimelineRecordEntry& entry = m_recordStack.last();
287     ASSERT(entry.type == TimelineRecordType::Layout);
288     Vector<FloatQuad> quads;
289     root->absoluteQuads(quads);
290     if (quads.size() >= 1)
291         TimelineRecordFactory::appendLayoutRoot(entry.data.get(), quads[0]);
292     else
293         ASSERT_NOT_REACHED();
294     didCompleteCurrentRecord(TimelineRecordType::Layout);
295 }
296
297 void InspectorTimelineAgent::didScheduleStyleRecalculation(Frame* frame)
298 {
299     appendRecord(InspectorObject::create(), TimelineRecordType::ScheduleStyleRecalculation, true, frame);
300 }
301
302 void InspectorTimelineAgent::willRecalculateStyle(Frame* frame)
303 {
304     pushCurrentRecord(InspectorObject::create(), TimelineRecordType::RecalculateStyles, true, frame);
305 }
306
307 void InspectorTimelineAgent::didRecalculateStyle()
308 {
309     didCompleteCurrentRecord(TimelineRecordType::RecalculateStyles);
310 }
311
312 void InspectorTimelineAgent::willPaint(Frame* frame)
313 {
314     pushCurrentRecord(InspectorObject::create(), TimelineRecordType::Paint, true, frame, true);
315 }
316
317 void InspectorTimelineAgent::didPaint(RenderObject* renderer, const LayoutRect& clipRect)
318 {
319     TimelineRecordEntry& entry = m_recordStack.last();
320     ASSERT(entry.type == TimelineRecordType::Paint);
321     FloatQuad quad;
322     localToPageQuad(*renderer, clipRect, &quad);
323     entry.data = TimelineRecordFactory::createPaintData(quad);
324     didCompleteCurrentRecord(TimelineRecordType::Paint);
325 }
326
327 void InspectorTimelineAgent::willScroll(Frame* frame)
328 {
329     pushCurrentRecord(InspectorObject::create(), TimelineRecordType::ScrollLayer, false, frame);
330 }
331
332 void InspectorTimelineAgent::didScroll()
333 {
334     didCompleteCurrentRecord(TimelineRecordType::ScrollLayer);
335 }
336
337 void InspectorTimelineAgent::willDecodeImage(const String& imageType)
338 {
339     pushCurrentRecord(TimelineRecordFactory::createDecodeImageData(imageType), TimelineRecordType::DecodeImage, true, 0);
340 }
341
342 void InspectorTimelineAgent::didDecodeImage()
343 {
344     didCompleteCurrentRecord(TimelineRecordType::DecodeImage);
345 }
346
347 void InspectorTimelineAgent::willResizeImage(bool shouldCache)
348 {
349     pushCurrentRecord(TimelineRecordFactory::createResizeImageData(shouldCache), TimelineRecordType::ResizeImage, true, 0);
350 }
351
352 void InspectorTimelineAgent::didResizeImage()
353 {
354     didCompleteCurrentRecord(TimelineRecordType::ResizeImage);
355 }
356
357 void InspectorTimelineAgent::willComposite()
358 {
359     pushCurrentRecord(InspectorObject::create(), TimelineRecordType::CompositeLayers, false, 0);
360 }
361
362 void InspectorTimelineAgent::didComposite()
363 {
364     didCompleteCurrentRecord(TimelineRecordType::CompositeLayers);
365 }
366
367 void InspectorTimelineAgent::willWriteHTML(unsigned startLine, Frame* frame)
368 {
369     pushCurrentRecord(TimelineRecordFactory::createParseHTMLData(startLine), TimelineRecordType::ParseHTML, true, frame);
370 }
371
372 void InspectorTimelineAgent::didWriteHTML(unsigned endLine)
373 {
374     if (!m_recordStack.isEmpty()) {
375         TimelineRecordEntry entry = m_recordStack.last();
376         entry.data->setNumber("endLine", endLine);
377         didCompleteCurrentRecord(TimelineRecordType::ParseHTML);
378     }
379 }
380
381 void InspectorTimelineAgent::didInstallTimer(int timerId, int timeout, bool singleShot, Frame* frame)
382 {
383     appendRecord(TimelineRecordFactory::createTimerInstallData(timerId, timeout, singleShot), TimelineRecordType::TimerInstall, true, frame);
384 }
385
386 void InspectorTimelineAgent::didRemoveTimer(int timerId, Frame* frame)
387 {
388     appendRecord(TimelineRecordFactory::createGenericTimerData(timerId), TimelineRecordType::TimerRemove, true, frame);
389 }
390
391 void InspectorTimelineAgent::willFireTimer(int timerId, Frame* frame)
392 {
393     pushCurrentRecord(TimelineRecordFactory::createGenericTimerData(timerId), TimelineRecordType::TimerFire, false, frame);
394 }
395
396 void InspectorTimelineAgent::didFireTimer()
397 {
398     didCompleteCurrentRecord(TimelineRecordType::TimerFire);
399 }
400
401 void InspectorTimelineAgent::willDispatchXHRReadyStateChangeEvent(const String& url, int readyState, Frame* frame)
402 {
403     pushCurrentRecord(TimelineRecordFactory::createXHRReadyStateChangeData(url, readyState), TimelineRecordType::XHRReadyStateChange, false, frame);
404 }
405
406 void InspectorTimelineAgent::didDispatchXHRReadyStateChangeEvent()
407 {
408     didCompleteCurrentRecord(TimelineRecordType::XHRReadyStateChange);
409 }
410
411 void InspectorTimelineAgent::willDispatchXHRLoadEvent(const String& url, Frame* frame)
412 {
413     pushCurrentRecord(TimelineRecordFactory::createXHRLoadData(url), TimelineRecordType::XHRLoad, true, frame);
414 }
415
416 void InspectorTimelineAgent::didDispatchXHRLoadEvent()
417 {
418     didCompleteCurrentRecord(TimelineRecordType::XHRLoad);
419 }
420
421 void InspectorTimelineAgent::willEvaluateScript(const String& url, int lineNumber, Frame* frame)
422 {
423     pushCurrentRecord(TimelineRecordFactory::createEvaluateScriptData(url, lineNumber), TimelineRecordType::EvaluateScript, true, frame);
424 }
425
426 void InspectorTimelineAgent::didEvaluateScript()
427 {
428     didCompleteCurrentRecord(TimelineRecordType::EvaluateScript);
429 }
430
431 void InspectorTimelineAgent::didScheduleResourceRequest(const String& url, Frame* frame)
432 {
433     appendRecord(TimelineRecordFactory::createScheduleResourceRequestData(url), TimelineRecordType::ScheduleResourceRequest, true, frame);
434 }
435
436 void InspectorTimelineAgent::willSendResourceRequest(unsigned long identifier, const ResourceRequest& request, Frame* frame)
437 {
438     String requestId = IdentifiersFactory::requestId(identifier);
439     appendRecord(TimelineRecordFactory::createResourceSendRequestData(requestId, request), TimelineRecordType::ResourceSendRequest, true, frame);
440 }
441
442 void InspectorTimelineAgent::willReceiveResourceData(unsigned long identifier, Frame* frame, int length)
443 {
444     String requestId = IdentifiersFactory::requestId(identifier);
445     pushCurrentRecord(TimelineRecordFactory::createReceiveResourceData(requestId, length), TimelineRecordType::ResourceReceivedData, false, frame);
446 }
447
448 void InspectorTimelineAgent::didReceiveResourceData()
449 {
450     didCompleteCurrentRecord(TimelineRecordType::ResourceReceivedData);
451 }
452
453 void InspectorTimelineAgent::willReceiveResourceResponse(unsigned long identifier, const ResourceResponse& response, Frame* frame)
454 {
455     String requestId = IdentifiersFactory::requestId(identifier);
456     pushCurrentRecord(TimelineRecordFactory::createResourceReceiveResponseData(requestId, response), TimelineRecordType::ResourceReceiveResponse, false, frame);
457 }
458
459 void InspectorTimelineAgent::didReceiveResourceResponse()
460 {
461     didCompleteCurrentRecord(TimelineRecordType::ResourceReceiveResponse);
462 }
463
464 void InspectorTimelineAgent::didFinishLoadingResource(unsigned long identifier, bool didFail, double finishTime, Frame* frame)
465 {
466     appendRecord(TimelineRecordFactory::createResourceFinishData(IdentifiersFactory::requestId(identifier), didFail, finishTime * 1000), TimelineRecordType::ResourceFinish, false, frame);
467 }
468
469 void InspectorTimelineAgent::didTimeStamp(Frame* frame, const String& message)
470 {
471     appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::TimeStamp, true, frame);
472 }
473
474 void InspectorTimelineAgent::time(Frame* frame, const String& message)
475 {
476     appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::Time, true, frame);
477 }
478
479 void InspectorTimelineAgent::timeEnd(Frame* frame, const String& message)
480 {
481     appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::TimeEnd, true, frame);
482 }
483
484 void InspectorTimelineAgent::didMarkDOMContentEvent(Frame* frame)
485 {
486     bool isMainFrame = frame && m_pageAgent && (frame == m_pageAgent->mainFrame());
487     appendRecord(TimelineRecordFactory::createMarkData(isMainFrame), TimelineRecordType::MarkDOMContent, false, frame);
488 }
489
490 void InspectorTimelineAgent::didMarkLoadEvent(Frame* frame)
491 {
492     bool isMainFrame = frame && m_pageAgent && (frame == m_pageAgent->mainFrame());
493     appendRecord(TimelineRecordFactory::createMarkData(isMainFrame), TimelineRecordType::MarkLoad, false, frame);
494 }
495
496 void InspectorTimelineAgent::didCommitLoad()
497 {
498     clearRecordStack();
499 }
500
501 void InspectorTimelineAgent::didRequestAnimationFrame(int callbackId, Frame* frame)
502 {
503     appendRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::RequestAnimationFrame, true, frame);
504 }
505
506 void InspectorTimelineAgent::didCancelAnimationFrame(int callbackId, Frame* frame)
507 {
508     appendRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::CancelAnimationFrame, true, frame);
509 }
510
511 void InspectorTimelineAgent::willFireAnimationFrame(int callbackId, Frame* frame)
512 {
513     pushCurrentRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::FireAnimationFrame, false, frame);
514 }
515
516 void InspectorTimelineAgent::didFireAnimationFrame()
517 {
518     didCompleteCurrentRecord(TimelineRecordType::FireAnimationFrame);
519 }
520
521 void InspectorTimelineAgent::willProcessTask()
522 {
523     pushCurrentRecord(InspectorObject::create(), TimelineRecordType::Program, false, 0);
524 }
525
526 void InspectorTimelineAgent::didProcessTask()
527 {
528     didCompleteCurrentRecord(TimelineRecordType::Program);
529 }
530
531 #if ENABLE(WEB_SOCKETS)
532 void InspectorTimelineAgent::didCreateWebSocket(unsigned long identifier, const KURL& url, const String& protocol, Frame* frame)
533 {
534     appendRecord(TimelineRecordFactory::createWebSocketCreateData(identifier, url, protocol), TimelineRecordType::WebSocketCreate, true, frame);
535 }
536
537 void InspectorTimelineAgent::willSendWebSocketHandshakeRequest(unsigned long identifier, Frame* frame)
538 {
539     appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketSendHandshakeRequest, true, frame);
540 }
541
542 void InspectorTimelineAgent::didReceiveWebSocketHandshakeResponse(unsigned long identifier, Frame* frame)
543 {
544     appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketReceiveHandshakeResponse, false, frame);
545 }
546
547 void InspectorTimelineAgent::didDestroyWebSocket(unsigned long identifier, Frame* frame)
548 {
549     appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketDestroy, true, frame);
550 }
551 #endif // ENABLE(WEB_SOCKETS)
552
553 void InspectorTimelineAgent::addRecordToTimeline(PassRefPtr<InspectorObject> record, const String& type)
554 {
555     commitFrameRecord();
556     innerAddRecordToTimeline(record, type);
557 }
558
559 void InspectorTimelineAgent::innerAddRecordToTimeline(PassRefPtr<InspectorObject> prpRecord, const String& type)
560 {
561     prpRecord->setString("type", type);
562     RefPtr<TypeBuilder::Timeline::TimelineEvent> record = TypeBuilder::Timeline::TimelineEvent::runtimeCast(prpRecord);
563     if (type == TimelineRecordType::Program)
564         setNativeHeapStatistics(record.get());
565     else
566         setDOMCounters(record.get());
567
568     if (m_recordStack.isEmpty())
569         sendEvent(record.release());
570     else {
571         TimelineRecordEntry parent = m_recordStack.last();
572         parent.children->pushObject(record.release());
573     }
574 }
575
576 static size_t getUsedHeapSize()
577 {
578     HeapInfo info;
579     ScriptGCEvent::getHeapSize(info);
580     return info.usedJSHeapSize;
581 }
582
583 void InspectorTimelineAgent::setDOMCounters(TypeBuilder::Timeline::TimelineEvent* record)
584 {
585     record->setUsedHeapSize(getUsedHeapSize());
586
587     if (m_state->getBoolean(TimelineAgentState::includeDomCounters)) {
588         int documentCount = 0;
589         int nodeCount = 0;
590         if (m_inspectorType == PageInspector) {
591             documentCount = InspectorCounters::counterValue(InspectorCounters::DocumentCounter);
592             nodeCount = InspectorCounters::counterValue(InspectorCounters::NodeCounter);
593         }
594         int listenerCount = ThreadLocalInspectorCounters::current().counterValue(ThreadLocalInspectorCounters::JSEventListenerCounter);
595         RefPtr<TypeBuilder::Timeline::DOMCounters> counters = TypeBuilder::Timeline::DOMCounters::create()
596             .setDocuments(documentCount)
597             .setNodes(nodeCount)
598             .setJsEventListeners(listenerCount);
599         record->setCounters(counters.release());
600     }
601 }
602
603 void InspectorTimelineAgent::setNativeHeapStatistics(TypeBuilder::Timeline::TimelineEvent* record)
604 {
605     if (!m_memoryAgent)
606         return;
607     if (!m_state->getBoolean(TimelineAgentState::includeNativeMemoryStatistics))
608         return;
609     HashMap<String, size_t> map;
610     m_memoryAgent->getProcessMemoryDistributionMap(&map);
611     RefPtr<InspectorObject> stats = InspectorObject::create();
612     for (HashMap<String, size_t>::iterator it = map.begin(); it != map.end(); ++it)
613         stats->setNumber(it->key, it->value);
614     size_t privateBytes = 0;
615     size_t sharedBytes = 0;
616     MemoryUsageSupport::processMemorySizesInBytes(&privateBytes, &sharedBytes);
617     stats->setNumber("PrivateBytes", privateBytes);
618     record->setNativeHeapStatistics(stats.release());
619 }
620
621 void InspectorTimelineAgent::setFrameIdentifier(InspectorObject* record, Frame* frame)
622 {
623     if (!frame || !m_pageAgent)
624         return;
625     String frameId;
626     if (frame && m_pageAgent)
627         frameId = m_pageAgent->frameId(frame);
628     record->setString("frameId", frameId);
629 }
630
631 void InspectorTimelineAgent::didCompleteCurrentRecord(const String& type)
632 {
633     // An empty stack could merely mean that the timeline agent was turned on in the middle of
634     // an event.  Don't treat as an error.
635     if (!m_recordStack.isEmpty()) {
636         if (m_platformInstrumentationClientInstalledAtStackDepth == m_recordStack.size()) {
637             m_platformInstrumentationClientInstalledAtStackDepth = 0;
638             PlatformInstrumentation::setClient(0);
639         }
640
641         pushGCEventRecords();
642         TimelineRecordEntry entry = m_recordStack.last();
643         m_recordStack.removeLast();
644         ASSERT(entry.type == type);
645         entry.record->setObject("data", entry.data);
646         entry.record->setArray("children", entry.children);
647         entry.record->setNumber("endTime", timestamp());
648         size_t usedHeapSizeDelta = getUsedHeapSize() - entry.usedHeapSizeAtStart;
649         if (usedHeapSizeDelta)
650             entry.record->setNumber("usedHeapSizeDelta", usedHeapSizeDelta);
651         addRecordToTimeline(entry.record, type);
652     }
653 }
654
655 InspectorTimelineAgent::InspectorTimelineAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorMemoryAgent* memoryAgent, InspectorCompositeState* state, InspectorType type, InspectorClient* client)
656     : InspectorBaseAgent<InspectorTimelineAgent>("Timeline", instrumentingAgents, state)
657     , m_pageAgent(pageAgent)
658     , m_memoryAgent(memoryAgent)
659     , m_frontend(0)
660     , m_id(1)
661     , m_maxCallStackDepth(5)
662     , m_platformInstrumentationClientInstalledAtStackDepth(0)
663     , m_inspectorType(type)
664     , m_client(client)
665     , m_weakFactory(this)
666 {
667 }
668
669 void InspectorTimelineAgent::appendRecord(PassRefPtr<InspectorObject> data, const String& type, bool captureCallStack, Frame* frame)
670 {
671     pushGCEventRecords();
672     RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(timestamp(), captureCallStack ? m_maxCallStackDepth : 0);
673     record->setObject("data", data);
674     setFrameIdentifier(record.get(), frame);
675     addRecordToTimeline(record.release(), type);
676 }
677
678 void InspectorTimelineAgent::sendEvent(PassRefPtr<InspectorObject> event)
679 {
680     // FIXME: runtimeCast is a hack. We do it because we can't build TimelineEvent directly now.
681     RefPtr<TypeBuilder::Timeline::TimelineEvent> recordChecked = TypeBuilder::Timeline::TimelineEvent::runtimeCast(event);
682     m_frontend->eventRecorded(recordChecked.release());
683 }
684
685 void InspectorTimelineAgent::pushCurrentRecord(PassRefPtr<InspectorObject> data, const String& type, bool captureCallStack, Frame* frame, bool hasLowLevelDetails)
686 {
687     pushGCEventRecords();
688     commitFrameRecord();
689     RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(timestamp(), captureCallStack ? m_maxCallStackDepth : 0);
690     setFrameIdentifier(record.get(), frame);
691     m_recordStack.append(TimelineRecordEntry(record.release(), data, InspectorArray::create(), type, getUsedHeapSize()));
692     if (hasLowLevelDetails && !m_platformInstrumentationClientInstalledAtStackDepth && !PlatformInstrumentation::hasClient()) {
693         m_platformInstrumentationClientInstalledAtStackDepth = m_recordStack.size();
694         PlatformInstrumentation::setClient(this);
695     }
696 }
697
698 void InspectorTimelineAgent::commitFrameRecord()
699 {
700     if (!m_pendingFrameRecord)
701         return;
702     
703     m_pendingFrameRecord->setObject("data", InspectorObject::create());
704     innerAddRecordToTimeline(m_pendingFrameRecord.release(), TimelineRecordType::BeginFrame);
705 }
706
707 void InspectorTimelineAgent::clearRecordStack()
708 {
709     if (m_platformInstrumentationClientInstalledAtStackDepth) {
710         m_platformInstrumentationClientInstalledAtStackDepth = 0;
711         PlatformInstrumentation::setClient(0);
712     }
713     m_pendingFrameRecord.clear();
714     m_recordStack.clear();
715     m_id++;
716 }
717
718 void InspectorTimelineAgent::localToPageQuad(const RenderObject& renderer, const LayoutRect& rect, FloatQuad* quad)
719 {
720     Frame* frame = renderer.frame();
721     FrameView* view = frame->view();
722     FloatQuad absolute = renderer.localToAbsoluteQuad(FloatQuad(rect));
723     quad->setP1(view->contentsToRootView(roundedIntPoint(absolute.p1())));
724     quad->setP2(view->contentsToRootView(roundedIntPoint(absolute.p2())));
725     quad->setP3(view->contentsToRootView(roundedIntPoint(absolute.p3())));
726     quad->setP4(view->contentsToRootView(roundedIntPoint(absolute.p4())));
727 }
728
729 double InspectorTimelineAgent::timestamp()
730 {
731     return m_timeConverter.fromMonotonicallyIncreasingTime(WTF::monotonicallyIncreasingTime());
732 }
733
734 Page* InspectorTimelineAgent::page()
735 {
736     return m_pageAgent ? m_pageAgent->page() : 0;
737 }
738
739 } // namespace WebCore
740
741 #endif // ENABLE(INSPECTOR)