1/*
2 * Copyright (C) 2013 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.ScriptTimelineDataGridNode = function(scriptTimelineRecord, baseStartTime, rangeStartTime, rangeEndTime)
27{
28    WebInspector.TimelineDataGridNode.call(this, false, null);
29
30    this._record = scriptTimelineRecord;
31    this._baseStartTime = baseStartTime || 0;
32    this._rangeStartTime = rangeStartTime || 0;
33    this._rangeEndTime = typeof rangeEndTime === "number" ? rangeEndTime : Infinity;
34};
35
36WebInspector.Object.addConstructorFunctions(WebInspector.ScriptTimelineDataGridNode);
37
38WebInspector.ScriptTimelineDataGridNode.IconStyleClassName = "icon";
39
40WebInspector.ScriptTimelineDataGridNode.prototype = {
41    constructor: WebInspector.ScriptTimelineDataGridNode,
42    __proto__: WebInspector.TimelineDataGridNode.prototype,
43
44    // Public
45
46    get record()
47    {
48        return this._record;
49    },
50
51    get records()
52    {
53        return [this._record];
54    },
55
56    get baseStartTime()
57    {
58        return this._baseStartTime;
59    },
60
61    get rangeStartTime()
62    {
63        return this._rangeStartTime;
64    },
65
66    get rangeEndTime()
67    {
68        return this._rangeEndTime;
69    },
70
71    get data()
72    {
73        var startTime = Math.max(this._rangeStartTime, this._record.startTime);
74        var duration = Math.min(this._record.startTime + this._record.duration, this._rangeEndTime) - startTime;
75        var callFrameOrSourceCodeLocation = this._record.initiatorCallFrame || this._record.sourceCodeLocation;
76
77        return {eventType: this._record.eventType, startTime: startTime, selfTime: duration, totalTime: duration,
78            averageTime: duration, callCount: 1, location: callFrameOrSourceCodeLocation};
79    },
80
81    updateRangeTimes: function(startTime, endTime)
82    {
83        var oldRangeStartTime = this._rangeStartTime;
84        var oldRangeEndTime = this._rangeEndTime;
85
86        if (oldRangeStartTime === startTime && oldRangeEndTime === endTime)
87            return;
88
89        this._rangeStartTime = startTime;
90        this._rangeEndTime = endTime;
91
92        // If we have no duration the range does not matter.
93        if (!this._record.duration)
94            return;
95
96        // We only need a refresh if the new range time changes the visible portion of this record.
97        var recordStart = this._record.startTime;
98        var recordEnd = this._record.startTime + this._record.duration;
99        var oldStartBoundary = clamp(recordStart, oldRangeStartTime, recordEnd);
100        var oldEndBoundary = clamp(recordStart, oldRangeEndTime, recordEnd);
101        var newStartBoundary = clamp(recordStart, startTime, recordEnd);
102        var newEndBoundary = clamp(recordStart, endTime, recordEnd);
103
104        if (oldStartBoundary !== newStartBoundary || oldEndBoundary !== newEndBoundary)
105            this.needsRefresh();
106    },
107
108    createCellContent: function(columnIdentifier, cell)
109    {
110        const emptyValuePlaceholderString = "\u2014";
111        var value = this.data[columnIdentifier];
112
113        switch (columnIdentifier) {
114        case "eventType":
115            return WebInspector.ScriptTimelineRecord.EventType.displayName(value, this._record.details);
116
117        case "startTime":
118            return isNaN(value) ? emptyValuePlaceholderString : Number.secondsToString(value - this._baseStartTime, true);
119
120        case "selfTime":
121        case "totalTime":
122        case "averageTime":
123            return isNaN(value) ? emptyValuePlaceholderString : Number.secondsToString(value, true);
124        }
125
126        return WebInspector.TimelineDataGridNode.prototype.createCellContent.call(this, columnIdentifier, cell);
127    }
128};
129