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