1/*
2 * Copyright (C) 2014 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.ProfileNodeDataGridNode = function(profileNode, baseStartTime, rangeStartTime, rangeEndTime)
27{
28    var hasChildren = !!profileNode.childNodes.length;
29
30    WebInspector.TimelineDataGridNode.call(this, false, null, hasChildren);
31
32    this._profileNode = profileNode;
33    this._baseStartTime = baseStartTime || 0;
34    this._rangeStartTime = rangeStartTime || 0;
35    this._rangeEndTime = typeof rangeEndTime === "number" ? rangeEndTime : Infinity;
36
37    this._data = this._profileNode.computeCallInfoForTimeRange(this._rangeStartTime, this._rangeEndTime);
38    this._data.location = this._profileNode.sourceCodeLocation;
39};
40
41WebInspector.Object.addConstructorFunctions(WebInspector.ProfileNodeDataGridNode);
42
43WebInspector.ProfileNodeDataGridNode.IconStyleClassName = "icon";
44
45WebInspector.ProfileNodeDataGridNode.prototype = {
46    constructor: WebInspector.ProfileNodeDataGridNode,
47    __proto__: WebInspector.TimelineDataGridNode.prototype,
48
49    // Public
50
51    get profileNode()
52    {
53        return this._profileNode;
54    },
55
56    get records()
57    {
58        return null;
59    },
60
61    get baseStartTime()
62    {
63        return this._baseStartTime;
64    },
65
66    get rangeStartTime()
67    {
68        return this._rangeStartTime;
69    },
70
71    get rangeEndTime()
72    {
73        return this._rangeEndTime;
74    },
75
76    get data()
77    {
78        return this._data;
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        // We only need a refresh if the new range time changes the visible portion of this record.
93        var profileStart = this._profileNode.startTime;
94        var profileEnd = this._profileNode.endTime;
95        var oldStartBoundary = clamp(profileStart, oldRangeStartTime, profileEnd);
96        var oldEndBoundary = clamp(profileStart, oldRangeEndTime, profileEnd);
97        var newStartBoundary = clamp(profileStart, startTime, profileEnd);
98        var newEndBoundary = clamp(profileStart, endTime, profileEnd);
99
100        if (oldStartBoundary !== newStartBoundary || oldEndBoundary !== newEndBoundary)
101            this.needsRefresh();
102    },
103
104    refresh: function()
105    {
106        this._data = this._profileNode.computeCallInfoForTimeRange(this._rangeStartTime, this._rangeEndTime);
107        this._data.location = this._profileNode.sourceCodeLocation;
108
109        WebInspector.TimelineDataGridNode.prototype.refresh.call(this);
110    },
111
112    createCellContent: function(columnIdentifier, cell)
113    {
114        const emptyValuePlaceholderString = "\u2014";
115        var value = this.data[columnIdentifier];
116
117        switch (columnIdentifier) {
118        case "startTime":
119            return isNaN(value) ? emptyValuePlaceholderString : Number.secondsToString(value - this._baseStartTime, true);
120
121        case "selfTime":
122        case "totalTime":
123        case "averageTime":
124            return isNaN(value) ? emptyValuePlaceholderString : Number.secondsToString(value, true);
125        }
126
127        return WebInspector.TimelineDataGridNode.prototype.createCellContent.call(this, columnIdentifier, cell);
128    }
129};
130