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.View} 34 * @param {!WebInspector.ProfilesPanel} profilesPanel 35 */ 36 WebInspector.ProfileLauncherView = function(profilesPanel) 37 { 38 WebInspector.View.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 52 WebInspector.ProfileLauncherView.prototype = { 53 /** 54 * @param {!WebInspector.ProfileType} profileType 55 */ 56 addProfileType: function(profileType) 57 { 58 var descriptionElement = this._innerContentElement.createChild("h1"); 59 descriptionElement.textContent = profileType.description; 60 var decorationElement = profileType.decorationElement(); 61 if (decorationElement) 62 this._innerContentElement.appendChild(decorationElement); 63 this._isInstantProfile = profileType.isInstantProfile(); 64 this._isEnabled = profileType.isEnabled(); 65 this._profileTypeId = profileType.id; 66 }, 67 68 _controlButtonClicked: function() 69 { 70 this._panel.toggleRecordButton(); 71 }, 72 73 _updateControls: function() 74 { 75 if (this._isEnabled) 76 this._controlButton.removeAttribute("disabled"); 77 else 78 this._controlButton.setAttribute("disabled", ""); 79 if (this._isInstantProfile) { 80 this._controlButton.classList.remove("running"); 81 this._controlButton.textContent = WebInspector.UIString("Take Snapshot"); 82 } else if (this._isProfiling) { 83 this._controlButton.classList.add("running"); 84 this._controlButton.textContent = WebInspector.UIString("Stop"); 85 } else { 86 this._controlButton.classList.remove("running"); 87 this._controlButton.textContent = WebInspector.UIString("Start"); 88 } 89 }, 90 91 profileStarted: function() 92 { 93 this._isProfiling = true; 94 this._updateControls(); 95 }, 96 97 profileFinished: function() 98 { 99 this._isProfiling = false; 100 this._updateControls(); 101 }, 102 103 /** 104 * @param {!WebInspector.ProfileType} profileType 105 */ 106 updateProfileType: function(profileType) 107 { 108 this._isInstantProfile = profileType.isInstantProfile(); 109 this._isEnabled = profileType.isEnabled(); 110 this._profileTypeId = profileType.id; 111 this._updateControls(); 112 }, 113 114 __proto__: WebInspector.View.prototype 115 } 116 117 118 /** 119 * @constructor 120 * @extends {WebInspector.ProfileLauncherView} 121 * @param {!WebInspector.ProfilesPanel} profilesPanel 122 */ 123 WebInspector.MultiProfileLauncherView = function(profilesPanel) 124 { 125 WebInspector.ProfileLauncherView.call(this, profilesPanel); 126 127 WebInspector.settings.selectedProfileType = WebInspector.settings.createSetting("selectedProfileType", "CPU"); 128 129 var header = this._innerContentElement.createChild("h1"); 130 header.textContent = WebInspector.UIString("Select profiling type"); 131 132 this._profileTypeSelectorForm = this._innerContentElement.createChild("form"); 133 134 this._innerContentElement.createChild("div", "flexible-space"); 135 136 this._typeIdToOptionElement = {}; 137 } 138 139 WebInspector.MultiProfileLauncherView.EventTypes = { 140 ProfileTypeSelected: "profile-type-selected" 141 } 142 143 WebInspector.MultiProfileLauncherView.prototype = { 144 /** 145 * @override 146 * @param {!WebInspector.ProfileType} profileType 147 */ 148 addProfileType: function(profileType) 149 { 150 var labelElement = this._profileTypeSelectorForm.createChild("label"); 151 labelElement.textContent = profileType.name; 152 var optionElement = document.createElement("input"); 153 labelElement.insertBefore(optionElement, labelElement.firstChild); 154 this._typeIdToOptionElement[profileType.id] = optionElement; 155 optionElement.type = "radio"; 156 optionElement.name = "profile-type"; 157 optionElement.style.hidden = true; 158 optionElement.addEventListener("change", this._profileTypeChanged.bind(this, profileType), false); 159 var descriptionElement = labelElement.createChild("p"); 160 descriptionElement.textContent = profileType.description; 161 var decorationElement = profileType.decorationElement(); 162 if (decorationElement) 163 labelElement.appendChild(decorationElement); 164 }, 165 166 restoreSelectedProfileType: function() 167 { 168 var typeName = WebInspector.settings.selectedProfileType.get(); 169 if (!(typeName in this._typeIdToOptionElement)) 170 typeName = Object.keys(this._typeIdToOptionElement)[0]; 171 this._typeIdToOptionElement[typeName].checked = true; 172 this.dispatchEventToListeners( 173 WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, 174 this._panel.getProfileType(typeName)); 175 }, 176 177 _controlButtonClicked: function() 178 { 179 this._panel.toggleRecordButton(); 180 }, 181 182 _updateControls: function() 183 { 184 WebInspector.ProfileLauncherView.prototype._updateControls.call(this); 185 var items = this._profileTypeSelectorForm.elements; 186 for (var i = 0; i < items.length; ++i) { 187 if (items[i].type === "radio") 188 items[i].disabled = this._isProfiling; 189 } 190 }, 191 192 /** 193 * @param {!WebInspector.ProfileType} profileType 194 */ 195 _profileTypeChanged: function(profileType, event) 196 { 197 this.dispatchEventToListeners(WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, profileType); 198 this._isInstantProfile = profileType.isInstantProfile(); 199 this._isEnabled = profileType.isEnabled(); 200 this._profileTypeId = profileType.id; 201 this._updateControls(); 202 WebInspector.settings.selectedProfileType.set(profileType.id); 203 }, 204 205 profileStarted: function() 206 { 207 this._isProfiling = true; 208 this._updateControls(); 209 }, 210 211 profileFinished: function() 212 { 213 this._isProfiling = false; 214 this._updateControls(); 215 }, 216 217 __proto__: WebInspector.ProfileLauncherView.prototype 218 } 219 220