1/*
2 * Copyright (C) 2009 280 North 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26/**
27 * @constructor
28 * @extends {WebInspector.ProfileDataGridNode}
29 * @param {!ProfilerAgent.CPUProfileNode} profileNode
30 * @param {!WebInspector.TopDownProfileDataGridTree} owningTree
31 */
32WebInspector.TopDownProfileDataGridNode = function(profileNode, owningTree)
33{
34    var hasChildren = !!(profileNode.children && profileNode.children.length);
35
36    WebInspector.ProfileDataGridNode.call(this, profileNode, owningTree, hasChildren);
37
38    this._remainingChildren = profileNode.children;
39}
40
41WebInspector.TopDownProfileDataGridNode.prototype = {
42    _sharedPopulate: function()
43    {
44        var children = this._remainingChildren;
45        var childrenLength = children.length;
46
47        for (var i = 0; i < childrenLength; ++i)
48            this.appendChild(new WebInspector.TopDownProfileDataGridNode(children[i], this.tree));
49
50        this._remainingChildren = null;
51    },
52
53    _exclude: function(aCallUID)
54    {
55        if (this._remainingChildren)
56            this.populate();
57
58        this._save();
59
60        var children = this.children;
61        var index = this.children.length;
62
63        while (index--)
64            children[index]._exclude(aCallUID);
65
66        var child = this.childrenByCallUID[aCallUID];
67
68        if (child)
69            this._merge(child, true);
70    },
71
72    __proto__: WebInspector.ProfileDataGridNode.prototype
73}
74
75/**
76 * @constructor
77 * @extends {WebInspector.ProfileDataGridTree}
78 * @param {WebInspector.CPUProfileView} profileView
79 * @param {ProfilerAgent.CPUProfileNode} rootProfileNode
80 */
81WebInspector.TopDownProfileDataGridTree = function(profileView, rootProfileNode)
82{
83    WebInspector.ProfileDataGridTree.call(this, profileView, rootProfileNode);
84
85    this._remainingChildren = rootProfileNode.children;
86
87    var any = /** @type{*} */(this);
88    var node = /** @type{WebInspector.ProfileDataGridNode} */(any);
89    WebInspector.TopDownProfileDataGridNode.prototype.populate.call(node);
90}
91
92WebInspector.TopDownProfileDataGridTree.prototype = {
93    /**
94     * @param {!WebInspector.ProfileDataGridNode} profileDataGridNode
95     */
96    focus: function(profileDataGridNode)
97    {
98        if (!profileDataGridNode)
99            return;
100
101        this._save();
102        profileDataGridNode.savePosition();
103
104        this.children = [profileDataGridNode];
105        this.totalTime = profileDataGridNode.totalTime;
106    },
107
108    /**
109     * @param {!WebInspector.ProfileDataGridNode} profileDataGridNode
110     */
111    exclude: function(profileDataGridNode)
112    {
113        if (!profileDataGridNode)
114            return;
115
116        this._save();
117
118        var excludedCallUID = profileDataGridNode.callUID;
119
120        var any = /** @type{*} */(this);
121        var node = /** @type{WebInspector.TopDownProfileDataGridNode} */(any);
122        WebInspector.TopDownProfileDataGridNode.prototype._exclude.call(node, excludedCallUID);
123
124        if (this.lastComparator)
125            this.sort(this.lastComparator, true);
126    },
127
128    restore: function()
129    {
130        if (!this._savedChildren)
131            return;
132
133        this.children[0].restorePosition();
134
135        WebInspector.ProfileDataGridTree.prototype.restore.call(this);
136    },
137
138    _merge: WebInspector.TopDownProfileDataGridNode.prototype._merge,
139
140    _sharedPopulate: WebInspector.TopDownProfileDataGridNode.prototype._sharedPopulate,
141
142    __proto__: WebInspector.ProfileDataGridTree.prototype
143}
144