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.ProfileNodeTreeElement = function(profileNode, delegate)
27{
28    console.assert(profileNode);
29
30    this._profileNode = profileNode;
31    this._delegate = delegate || null;
32
33    var title = profileNode.functionName;
34    var subtitle = "";
35
36    if (!title) {
37        switch (profileNode.type) {
38        case WebInspector.ProfileNode.Type.Function:
39            title = WebInspector.UIString("(anonymous function)");
40            break;
41        case WebInspector.ProfileNode.Type.Program:
42            title = WebInspector.UIString("(program)");
43            break;
44        default:
45            title = WebInspector.UIString("(anonymous function)");
46            console.error("Unknown ProfileNode type: " + profileNode.type);
47        }
48    }
49
50    var sourceCodeLocation = this._profileNode.sourceCodeLocation;
51    if (sourceCodeLocation) {
52        subtitle = document.createElement("span");
53        sourceCodeLocation.populateLiveDisplayLocationString(subtitle, "textContent");
54    }
55
56    var className;
57
58    switch (this._profileNode.type) {
59    case WebInspector.ProfileNode.Type.Function:
60        className = WebInspector.CallFrameTreeElement.FunctionIconStyleClassName;
61        if (!sourceCodeLocation)
62            className = WebInspector.CallFrameTreeElement.NativeIconStyleClassName;
63        break;
64    case WebInspector.ProfileNode.Type.Program:
65        className = WebInspector.TimelineRecordTreeElement.EvaluatedRecordIconStyleClass;
66        break;
67    }
68
69    console.assert(className);
70
71    // This is more than likely an event listener function with an "on" prefix and it is
72    // as long or longer than the shortest event listener name -- "oncut".
73    if (profileNode.functionName && profileNode.functionName.startsWith("on") && profileNode.functionName.length >= 5)
74        className = WebInspector.CallFrameTreeElement.EventListenerIconStyleClassName;
75
76    var hasChildren = !!profileNode.childNodes.length;
77
78    WebInspector.GeneralTreeElement.call(this, [className], title, subtitle, profileNode, hasChildren);
79
80    this.small = true;
81    this.shouldRefreshChildren = true;
82
83    if (sourceCodeLocation)
84        this.tooltipHandledSeparately = true;
85};
86
87WebInspector.ProfileNodeTreeElement.prototype = {
88    constructor: WebInspector.ProfileNodeTreeElement,
89    __proto__: WebInspector.GeneralTreeElement.prototype,
90
91    // Public
92
93    get profileNode()
94    {
95        return this._profileNode;
96    },
97
98    get filterableData()
99    {
100        var url = this._profileNode.sourceCodeLocation ? this._profileNode.sourceCodeLocation.sourceCode.url : "";
101        return {text: [this.mainTitle, url || ""]};
102    },
103
104    // Protected
105
106    onattach: function()
107    {
108        WebInspector.GeneralTreeElement.prototype.onattach.call(this);
109
110        console.assert(this.element);
111
112        if (!this.tooltipHandledSeparately)
113            return;
114
115        var tooltipPrefix = this.mainTitle + "\n";
116        this._profileNode.sourceCodeLocation.populateLiveDisplayLocationTooltip(this.element, tooltipPrefix);
117    },
118
119    onpopulate: function()
120    {
121        if (!this.hasChildren || !this.shouldRefreshChildren)
122            return;
123
124        this.shouldRefreshChildren = false;
125
126        this.removeChildren();
127
128        if (this._delegate && typeof this._delegate.populateProfileNodeTreeElement === "function") {
129            this._delegate.populateProfileNodeTreeElement(this);
130            return;
131        }
132
133        for (var childProfileNode of this._profileNode.childNodes) {
134            var childTreeElement = new WebInspector.ProfileNodeTreeElement(childProfileNode);
135            this.appendChild(childTreeElement);
136        }
137    }
138};
139