Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / ProfileLauncherView.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.VBox}
34  * @param {!WebInspector.ProfilesPanel} profilesPanel
35  */
36 WebInspector.ProfileLauncherView = function(profilesPanel)
37 {
38     WebInspector.VBox.call(this);
39
40     this._panel = profilesPanel;
41
42     this.element.classList.add("profile-launcher-view");
43     this.element.classList.add("panel-enabler-view");
44
45     this._contentElement = this.element.createChild("div", "profile-launcher-view-content");
46     this._innerContentElement = this._contentElement.createChild("div");
47
48     this._controlButton = this._contentElement.createChild("button", "control-profiling");
49     this._controlButton.addEventListener("click", this._controlButtonClicked.bind(this), false);
50
51     this._loadButton = this._contentElement.createChild("button", "load-profile");
52     this._loadButton.textContent = WebInspector.UIString("Load");
53     this._loadButton.addEventListener("click", this._loadButtonClicked.bind(this), false);
54 }
55
56 WebInspector.ProfileLauncherView.prototype = {
57     /**
58      * @param {!WebInspector.ProfileType} profileType
59      */
60     addProfileType: function(profileType)
61     {
62         var descriptionElement = this._innerContentElement.createChild("h1");
63         descriptionElement.textContent = profileType.description;
64         var decorationElement = profileType.decorationElement();
65         if (decorationElement)
66             this._innerContentElement.appendChild(decorationElement);
67         this._isInstantProfile = profileType.isInstantProfile();
68         this._isEnabled = profileType.isEnabled();
69         this._profileTypeId = profileType.id;
70     },
71
72     _controlButtonClicked: function()
73     {
74         this._panel.toggleRecordButton();
75     },
76
77     _loadButtonClicked: function()
78     {
79         this._panel.showLoadFromFileDialog();
80     },
81
82     _updateControls: function()
83     {
84         if (this._isEnabled)
85             this._controlButton.removeAttribute("disabled");
86         else
87             this._controlButton.setAttribute("disabled", "");
88         if (this._isInstantProfile) {
89             this._controlButton.classList.remove("running");
90             this._controlButton.textContent = WebInspector.UIString("Take Snapshot");
91         } else if (this._isProfiling) {
92             this._controlButton.classList.add("running");
93             this._controlButton.textContent = WebInspector.UIString("Stop");
94         } else {
95             this._controlButton.classList.remove("running");
96             this._controlButton.textContent = WebInspector.UIString("Start");
97         }
98     },
99
100     profileStarted: function()
101     {
102         this._isProfiling = true;
103         this._updateControls();
104     },
105
106     profileFinished: function()
107     {
108         this._isProfiling = false;
109         this._updateControls();
110     },
111
112     /**
113      * @param {!WebInspector.ProfileType} profileType
114      */
115     updateProfileType: function(profileType)
116     {
117         this._isInstantProfile = profileType.isInstantProfile();
118         this._isEnabled = profileType.isEnabled();
119         this._profileTypeId = profileType.id;
120         this._updateControls();
121     },
122
123     __proto__: WebInspector.VBox.prototype
124 }
125
126
127 /**
128  * @constructor
129  * @extends {WebInspector.ProfileLauncherView}
130  * @param {!WebInspector.ProfilesPanel} profilesPanel
131  */
132 WebInspector.MultiProfileLauncherView = function(profilesPanel)
133 {
134     WebInspector.ProfileLauncherView.call(this, profilesPanel);
135
136     WebInspector.settings.selectedProfileType = WebInspector.settings.createSetting("selectedProfileType", "CPU");
137
138     var header = this._innerContentElement.createChild("h1");
139     header.textContent = WebInspector.UIString("Select profiling type");
140
141     this._profileTypeSelectorForm = this._innerContentElement.createChild("form");
142
143     this._innerContentElement.createChild("div", "flexible-space");
144
145     this._typeIdToOptionElement = {};
146 }
147
148 WebInspector.MultiProfileLauncherView.EventTypes = {
149     ProfileTypeSelected: "profile-type-selected"
150 }
151
152 WebInspector.MultiProfileLauncherView.prototype = {
153     /**
154      * @override
155      * @param {!WebInspector.ProfileType} profileType
156      */
157     addProfileType: function(profileType)
158     {
159         var labelElement = this._profileTypeSelectorForm.createChild("label");
160         labelElement.textContent = profileType.name;
161         var optionElement = document.createElement("input");
162         labelElement.insertBefore(optionElement, labelElement.firstChild);
163         this._typeIdToOptionElement[profileType.id] = optionElement;
164         optionElement._profileType = profileType;
165         optionElement.type = "radio";
166         optionElement.name = "profile-type";
167         optionElement.style.hidden = true;
168         optionElement.addEventListener("change", this._profileTypeChanged.bind(this, profileType), false);
169         var descriptionElement = labelElement.createChild("p");
170         descriptionElement.textContent = profileType.description;
171         var decorationElement = profileType.decorationElement();
172         if (decorationElement)
173             labelElement.appendChild(decorationElement);
174     },
175
176     restoreSelectedProfileType: function()
177     {
178         var typeId = WebInspector.settings.selectedProfileType.get();
179         if (!(typeId in this._typeIdToOptionElement))
180             typeId = Object.keys(this._typeIdToOptionElement)[0];
181         this._typeIdToOptionElement[typeId].checked = true;
182         var type = this._typeIdToOptionElement[typeId]._profileType;
183         this.dispatchEventToListeners(WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, type);
184     },
185
186     _controlButtonClicked: function()
187     {
188         this._panel.toggleRecordButton();
189     },
190
191     _updateControls: function()
192     {
193         WebInspector.ProfileLauncherView.prototype._updateControls.call(this);
194         var items = this._profileTypeSelectorForm.elements;
195         for (var i = 0; i < items.length; ++i) {
196             if (items[i].type === "radio")
197                 items[i].disabled = this._isProfiling;
198         }
199     },
200
201     /**
202      * @param {!WebInspector.ProfileType} profileType
203      */
204     _profileTypeChanged: function(profileType, event)
205     {
206         this.dispatchEventToListeners(WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, profileType);
207         this._isInstantProfile = profileType.isInstantProfile();
208         this._isEnabled = profileType.isEnabled();
209         this._profileTypeId = profileType.id;
210         this._updateControls();
211         WebInspector.settings.selectedProfileType.set(profileType.id);
212     },
213
214     profileStarted: function()
215     {
216         this._isProfiling = true;
217         this._updateControls();
218     },
219
220     profileFinished: function()
221     {
222         this._isProfiling = false;
223         this._updateControls();
224     },
225
226     __proto__: WebInspector.ProfileLauncherView.prototype
227 }
228