summaryrefslogtreecommitdiffstats
path: root/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/LeaksViewer/LeaksViewer.js
blob: bd83f1ac30747954c679edee4efc350ad2f8dd0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

var LeaksViewer = {
    loaded: function() {
        this._loader = new LeaksLoader(this._didCountLeaksFiles.bind(this), this._didLoadLeaksFile.bind(this));
        this._parser = new LeaksParser(this._didParseLeaksFile.bind(this));

        this._loadingIndicator = document.getElementById("loading-indicator");
        this._loadingIndicatorLabel = document.getElementById("loading-indicator-label");

        this._profileView = new WebInspector.CPUProfileView({});
        document.getElementById("main-panels").appendChild(this._profileView.element);
        this._profileView.show();

        // From WebInspector.Panel.prototype.show
        var statusBarItems = this._profileView.statusBarItems;
        if (statusBarItems) {
            this._statusBarItemContainer = document.createElement("div");
            for (var i = 0; i < statusBarItems.length; ++i)
                this._statusBarItemContainer.appendChild(statusBarItems[i]);
            document.getElementById("main-status-bar").appendChild(this._statusBarItemContainer);
        }

        var url;
        var match = /url=([^&]+)/.exec(location.search);
        if (match)
            url = decodeURIComponent(match[1]);

        if (url)
            this._loadLeaksFromURL(url)
        else
            this._displayURLPrompt();
    },

    get filesLeftToParse() {
        if (!('_filesLeftToParse' in this))
            this._filesLeftToParse = 0;
        return this._filesLeftToParse;
    },

    set filesLeftToParse(x) {
        if (this._filesLeftToParse === x)
            return;
        this._filesLeftToParse = x;
        this._loadingStatusChanged();
    },

    get loading() {
        return this._isLoading;
    },

    set loading(x) {
        if (this._isLoading === x)
            return;
        this._isLoading = x;
        this._loadingStatusChanged();
    },

    get url() {
        return this._url;
    },

    set url(x) {
        if (this._url === x)
            return;

        this._url = x;
        this._updateTitle();
    },

    urlPromptButtonClicked: function(e) {
        this._urlChosenFromPrompt(document.getElementById("url").value);
    },

    _didCountLeaksFiles: function(fileCount) {
        this._fileCount = fileCount;
        this.filesLeftToParse = fileCount;
    },

    _didLoadLeaksFile: function(leaksText) {
        this._parser.addLeaksFile(leaksText);
    },

    _didLoadRecentBuilds: function(builds) {
        this._recentBuilds = builds;
        this._updateURLPrompt();
    },

    _didParseLeaksFile: function(profile) {
        if (--this.filesLeftToParse)
            return;
        ProfilerAgent.profileReady(profile);
        this.loading = false;
    },

    _displayURLPrompt: function() {
        document.getElementById("url-prompt-container").removeStyleClass("hidden");
        document.getElementById("url").focus();
        var loader = new RecentBuildsLoader(this._didLoadRecentBuilds.bind(this));
        loader.start("SnowLeopard Intel Leaks", this._numberOfRecentBuildsToLoad);
    },

    _loadLeaksFromURL: function(url) {
        this.url = url;
        this.loading = true;

        this._loader.start(this.url);
    },

    _loadingIndicatorText: function() {
        var text = "Loading";
        if (this.filesLeftToParse)
            text += " " + (this._fileCount - this.filesLeftToParse + 1) + "/" + this._fileCount + " files";
        text += "\u2026";
        return text;
    },

    _loadingStatusChanged: function() {
        this._setLoadingIndicatorHidden(!this.loading);
        this._updateLoadingIndicatorLabel();
        this._updateTitle();
    },

    _numberOfRecentBuildsToLoad: 10,

    _setLoadingIndicatorHidden: function(hidden) {
        if (hidden)
            this._loadingIndicator.addStyleClass("hidden");
        else
            this._loadingIndicator.removeStyleClass("hidden");
    },

    _updateLoadingIndicatorLabel: function() {
        this._loadingIndicatorLabel.innerText = this._loadingIndicatorText();
    },

    _updateTitle: function() {
        var title = "Leaks Viewer \u2014 ";
        if (this.loading)
            title += "(" + this._loadingIndicatorText() + ") ";
        title += this.url;
        document.title = title;
    },

    _updateURLPrompt: function() {
        var recentBuildsContainer = document.getElementById("recent-builds-container");
        recentBuildsContainer.removeChildren();
        if (this._recentBuilds && this._recentBuilds.length) {
            var list = document.createElement("ol");
            list.id = "recent-builds-list";

            var self = this;
            this._recentBuilds.forEach(function(build) {
                var link = document.createElement("a");
                link.href = document.location.href + "?url=" + encodeURIComponent(build.url);
                link.addEventListener("click", function(e) {
                    self._urlChosenFromPrompt(build.url);
                    e.preventDefault();
                });
                link.appendChild(document.createTextNode("r" + build.revision + ": " + build.leakCount + " leaks"));
                var item = document.createElement("li");
                item.appendChild(link);

                list.appendChild(item);
            });

            recentBuildsContainer.appendChild(list);
        } else
            recentBuildsContainer.appendChild(document.createTextNode("No recent leaky builds found."));
    },

    _urlChosenFromPrompt: function(url) {
        this._loadLeaksFromURL(url);
        document.getElementById("url-prompt-container").addStyleClass("hidden");
    },

};

addEventListener("load", LeaksViewer.loaded.bind(LeaksViewer));