home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 July / CA07.iso / Multimedija / QuickTimeInstaller.exe / AppleApplicationSupport.msi / WebKit.resources_inspector_ProfileView.js < prev    next >
Encoding:
Text File  |  2010-03-15  |  20.2 KB  |  529 lines

  1. /*
  2.  * Copyright (C) 2008 Apple 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. WebInspector.ProfileView = function(profile)
  27. {
  28.     WebInspector.View.call(this);
  29.  
  30.     this.element.addStyleClass("profile-view");
  31.  
  32.     this.showSelfTimeAsPercent = true;
  33.     this.showTotalTimeAsPercent = true;
  34.     this.showAverageTimeAsPercent = true;
  35.  
  36.     var columns = { "self": { title: WebInspector.UIString("Self"), width: "72px", sort: "descending", sortable: true },
  37.                     "total": { title: WebInspector.UIString("Total"), width: "72px", sortable: true },
  38.                     "average": { title: WebInspector.UIString("Average"), width: "72px", sortable: true },
  39.                     "calls": { title: WebInspector.UIString("Calls"), width: "54px", sortable: true },
  40.                     "function": { title: WebInspector.UIString("Function"), disclosure: true, sortable: true } };
  41.  
  42.     this.dataGrid = new WebInspector.DataGrid(columns);
  43.     this.dataGrid.addEventListener("sorting changed", this._sortData, this);
  44.     this.dataGrid.element.addEventListener("mousedown", this._mouseDownInDataGrid.bind(this), true);
  45.     this.element.appendChild(this.dataGrid.element);
  46.  
  47.     this.viewSelectElement = document.createElement("select");
  48.     this.viewSelectElement.className = "status-bar-item";
  49.     this.viewSelectElement.addEventListener("change", this._changeView.bind(this), false);
  50.     this.view = "Heavy";
  51.  
  52.     var heavyViewOption = document.createElement("option");
  53.     heavyViewOption.label = WebInspector.UIString("Heavy (Bottom Up)");
  54.     var treeViewOption = document.createElement("option");
  55.     treeViewOption.label = WebInspector.UIString("Tree (Top Down)");
  56.     this.viewSelectElement.appendChild(heavyViewOption);
  57.     this.viewSelectElement.appendChild(treeViewOption);
  58.  
  59.     this.percentButton = document.createElement("button");
  60.     this.percentButton.className = "percent-time-status-bar-item status-bar-item";
  61.     this.percentButton.addEventListener("click", this._percentClicked.bind(this), false);
  62.  
  63.     this.focusButton = document.createElement("button");
  64.     this.focusButton.title = WebInspector.UIString("Focus selected function.");
  65.     this.focusButton.className = "focus-profile-node-status-bar-item status-bar-item";
  66.     this.focusButton.disabled = true;
  67.     this.focusButton.addEventListener("click", this._focusClicked.bind(this), false);
  68.  
  69.     this.excludeButton = document.createElement("button");
  70.     this.excludeButton.title = WebInspector.UIString("Exclude selected function.");
  71.     this.excludeButton.className = "exclude-profile-node-status-bar-item status-bar-item";
  72.     this.excludeButton.disabled = true;
  73.     this.excludeButton.addEventListener("click", this._excludeClicked.bind(this), false);
  74.  
  75.     this.resetButton = document.createElement("button");
  76.     this.resetButton.title = WebInspector.UIString("Restore all functions.");
  77.     this.resetButton.className = "reset-profile-status-bar-item status-bar-item hidden";
  78.     this.resetButton.addEventListener("click", this._resetClicked.bind(this), false);
  79.  
  80.     this.profile = profile;
  81.  
  82.     this.profileDataGridTree = this.bottomUpProfileDataGridTree;
  83.     this.profileDataGridTree.sort(WebInspector.ProfileDataGridTree.propertyComparator("selfTime", false));
  84.  
  85.     this.refresh();
  86.  
  87.     this._updatePercentButton();
  88. }
  89.  
  90. WebInspector.ProfileView.prototype = {
  91.     get statusBarItems()
  92.     {
  93.         return [this.viewSelectElement, this.percentButton, this.focusButton, this.excludeButton, this.resetButton];
  94.     },
  95.  
  96.     get profile()
  97.     {
  98.         return this._profile;
  99.     },
  100.  
  101.     set profile(profile)
  102.     {
  103.         this._profile = profile;
  104.     },
  105.  
  106.     get bottomUpProfileDataGridTree()
  107.     {
  108.         if (!this._bottomUpProfileDataGridTree)
  109.             this._bottomUpProfileDataGridTree = new WebInspector.BottomUpProfileDataGridTree(this, this.profile.head);
  110.         return this._bottomUpProfileDataGridTree;
  111.     },
  112.  
  113.     get topDownProfileDataGridTree()
  114.     {
  115.         if (!this._topDownProfileDataGridTree)
  116.             this._topDownProfileDataGridTree = new WebInspector.TopDownProfileDataGridTree(this, this.profile.head);
  117.         return this._topDownProfileDataGridTree;
  118.     },
  119.  
  120.     get currentTree()
  121.     {
  122.         return this._currentTree;
  123.     },
  124.  
  125.     set currentTree(tree)
  126.     {
  127.         this._currentTree = tree;
  128.         this.refresh();
  129.     },
  130.  
  131.     get topDownTree()
  132.     {
  133.         if (!this._topDownTree) {
  134.             this._topDownTree = WebInspector.TopDownTreeFactory.create(this.profile.head);
  135.             this._sortProfile(this._topDownTree);
  136.         }
  137.  
  138.         return this._topDownTree;
  139.     },
  140.  
  141.     get bottomUpTree()
  142.     {
  143.         if (!this._bottomUpTree) {
  144.             this._bottomUpTree = WebInspector.BottomUpTreeFactory.create(this.profile.head);
  145.             this._sortProfile(this._bottomUpTree);
  146.         }
  147.  
  148.         return this._bottomUpTree;
  149.     },
  150.  
  151.     hide: function()
  152.     {
  153.         WebInspector.View.prototype.hide.call(this);
  154.         this._currentSearchResultIndex = -1;
  155.     },
  156.  
  157.     refresh: function()
  158.     {
  159.         var selectedProfileNode = this.dataGrid.selectedNode ? this.dataGrid.selectedNode.profileNode : null;
  160.  
  161.         this.dataGrid.removeChildren();
  162.  
  163.         var children = this.profileDataGridTree.children;
  164.         var count = children.length;
  165.  
  166.         for (var index = 0; index < count; ++index)
  167.             this.dataGrid.appendChild(children[index]);
  168.  
  169.         if (selectedProfileNode)
  170.             selectedProfileNode.selected = true;
  171.     },
  172.  
  173.     refreshVisibleData: function()
  174.     {
  175.         var child = this.dataGrid.children[0];
  176.         while (child) {
  177.             child.refresh();
  178.             child = child.traverseNextNode(false, null, true);
  179.         }
  180.     },
  181.  
  182.     refreshShowAsPercents: function()
  183.     {
  184.         this._updatePercentButton();
  185.         this.refreshVisibleData();
  186.     },
  187.  
  188.     searchCanceled: function()
  189.     {
  190.         if (this._searchResults) {
  191.             for (var i = 0; i < this._searchResults.length; ++i) {
  192.                 var profileNode = this._searchResults[i].profileNode;
  193.  
  194.                 delete profileNode._searchMatchedSelfColumn;
  195.                 delete profileNode._searchMatchedTotalColumn;
  196.                 delete profileNode._searchMatchedCallsColumn;
  197.                 delete profileNode._searchMatchedFunctionColumn;
  198.  
  199.                 profileNode.refresh();
  200.             }
  201.         }
  202.  
  203.         delete this._searchFinishedCallback;
  204.         this._currentSearchResultIndex = -1;
  205.         this._searchResults = [];
  206.     },
  207.  
  208.     performSearch: function(query, finishedCallback)
  209.     {
  210.         // Call searchCanceled since it will reset everything we need before doing a new search.
  211.         this.searchCanceled();
  212.  
  213.         query = query.trimWhitespace();
  214.  
  215.         if (!query.length)
  216.             return;
  217.  
  218.         this._searchFinishedCallback = finishedCallback;
  219.  
  220.         var greaterThan = (query.indexOf(">") === 0);
  221.         var lessThan = (query.indexOf("<") === 0);
  222.         var equalTo = (query.indexOf("=") === 0 || ((greaterThan || lessThan) && query.indexOf("=") === 1));
  223.         var percentUnits = (query.lastIndexOf("%") === (query.length - 1));
  224.         var millisecondsUnits = (query.length > 2 && query.lastIndexOf("ms") === (query.length - 2));
  225.         var secondsUnits = (!millisecondsUnits && query.lastIndexOf("s") === (query.length - 1));
  226.  
  227.         var queryNumber = parseFloat(query);
  228.         if (greaterThan || lessThan || equalTo) {
  229.             if (equalTo && (greaterThan || lessThan))
  230.                 queryNumber = parseFloat(query.substring(2));
  231.             else
  232.                 queryNumber = parseFloat(query.substring(1));
  233.         }
  234.  
  235.         var queryNumberMilliseconds = (secondsUnits ? (queryNumber * 1000) : queryNumber);
  236.  
  237.         // Make equalTo implicitly true if it wasn't specified there is no other operator.
  238.         if (!isNaN(queryNumber) && !(greaterThan || lessThan))
  239.             equalTo = true;
  240.  
  241.         function matchesQuery(/*ProfileDataGridNode*/ profileDataGridNode)
  242.         {
  243.             delete profileDataGridNode._searchMatchedSelfColumn;
  244.             delete profileDataGridNode._searchMatchedTotalColumn;
  245.             delete profileDataGridNode._searchMatchedAverageColumn;
  246.             delete profileDataGridNode._searchMatchedCallsColumn;
  247.             delete profileDataGridNode._searchMatchedFunctionColumn;
  248.  
  249.             if (percentUnits) {
  250.                 if (lessThan) {
  251.                     if (profileDataGridNode.selfPercent < queryNumber)
  252.                         profileDataGridNode._searchMatchedSelfColumn = true;
  253.                     if (profileDataGridNode.totalPercent < queryNumber)
  254.                         profileDataGridNode._searchMatchedTotalColumn = true;
  255.                     if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
  256.                         profileDataGridNode._searchMatchedAverageColumn = true;
  257.                 } else if (greaterThan) {
  258.                     if (profileDataGridNode.selfPercent > queryNumber)
  259.                         profileDataGridNode._searchMatchedSelfColumn = true;
  260.                     if (profileDataGridNode.totalPercent > queryNumber)
  261.                         profileDataGridNode._searchMatchedTotalColumn = true;
  262.                     if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
  263.                         profileDataGridNode._searchMatchedAverageColumn = true;
  264.                 }
  265.  
  266.                 if (equalTo) {
  267.                     if (profileDataGridNode.selfPercent == queryNumber)
  268.                         profileDataGridNode._searchMatchedSelfColumn = true;
  269.                     if (profileDataGridNode.totalPercent == queryNumber)
  270.                         profileDataGridNode._searchMatchedTotalColumn = true;
  271.                     if (profileDataGridNode.averagePercent < queryNumberMilliseconds)
  272.                         profileDataGridNode._searchMatchedAverageColumn = true;
  273.                 }
  274.             } else if (millisecondsUnits || secondsUnits) {
  275.                 if (lessThan) {
  276.                     if (profileDataGridNode.selfTime < queryNumberMilliseconds)
  277.                         profileDataGridNode._searchMatchedSelfColumn = true;
  278.                     if (profileDataGridNode.totalTime < queryNumberMilliseconds)
  279.                         profileDataGridNode._searchMatchedTotalColumn = true;
  280.                     if (profileDataGridNode.averageTime < queryNumberMilliseconds)
  281.                         profileDataGridNode._searchMatchedAverageColumn = true;
  282.                 } else if (greaterThan) {
  283.                     if (profileDataGridNode.selfTime > queryNumberMilliseconds)
  284.                         profileDataGridNode._searchMatchedSelfColumn = true;
  285.                     if (profileDataGridNode.totalTime > queryNumberMilliseconds)
  286.                         profileDataGridNode._searchMatchedTotalColumn = true;
  287.                     if (profileDataGridNode.averageTime > queryNumberMilliseconds)
  288.                         profileDataGridNode._searchMatchedAverageColumn = true;
  289.                 }
  290.  
  291.                 if (equalTo) {
  292.                     if (profileDataGridNode.selfTime == queryNumberMilliseconds)
  293.                         profileDataGridNode._searchMatchedSelfColumn = true;
  294.                     if (profileDataGridNode.totalTime == queryNumberMilliseconds)
  295.                         profileDataGridNode._searchMatchedTotalColumn = true;
  296.                     if (profileDataGridNode.averageTime == queryNumberMilliseconds)
  297.                         profileDataGridNode._searchMatchedAverageColumn = true;
  298.                 }
  299.             } else {
  300.                 if (equalTo && profileDataGridNode.numberOfCalls == queryNumber)
  301.                     profileDataGridNode._searchMatchedCallsColumn = true;
  302.                 if (greaterThan && profileDataGridNode.numberOfCalls > queryNumber)
  303.                     profileDataGridNode._searchMatchedCallsColumn = true;
  304.                 if (lessThan && profileDataGridNode.numberOfCalls < queryNumber)
  305.                     profileDataGridNode._searchMatchedCallsColumn = true;
  306.             }
  307.  
  308.             if (profileDataGridNode.functionName.hasSubstring(query, true) || profileDataGridNode.url.hasSubstring(query, true))
  309.                 profileDataGridNode._searchMatchedFunctionColumn = true;
  310.  
  311.             if (profileDataGridNode._searchMatchedSelfColumn ||
  312.                 profileDataGridNode._searchMatchedTotalColumn ||
  313.                 profileDataGridNode._searchMatchedAverageColumn ||
  314.                 profileDataGridNode._searchMatchedCallsColumn ||
  315.                 profileDataGridNode._searchMatchedFunctionColumn)
  316.             {
  317.                 profileDataGridNode.refresh();
  318.                 return true;
  319.             }
  320.  
  321.             return false;
  322.         }
  323.  
  324.         var current = this.profileDataGridTree.children[0];
  325.  
  326.         while (current) {
  327.             if (matchesQuery(current)) {
  328.                 this._searchResults.push({ profileNode: current });
  329.             }
  330.  
  331.             current = current.traverseNextNode(false, null, false);
  332.         }
  333.  
  334.         finishedCallback(this, this._searchResults.length);
  335.     },
  336.  
  337.     jumpToFirstSearchResult: function()
  338.     {
  339.         if (!this._searchResults || !this._searchResults.length)
  340.             return;
  341.         this._currentSearchResultIndex = 0;
  342.         this._jumpToSearchResult(this._currentSearchResultIndex);
  343.     },
  344.  
  345.     jumpToLastSearchResult: function()
  346.     {
  347.         if (!this._searchResults || !this._searchResults.length)
  348.             return;
  349.         this._currentSearchResultIndex = (this._searchResults.length - 1);
  350.         this._jumpToSearchResult(this._currentSearchResultIndex);
  351.     },
  352.  
  353.     jumpToNextSearchResult: function()
  354.     {
  355.         if (!this._searchResults || !this._searchResults.length)
  356.             return;
  357.         if (++this._currentSearchResultIndex >= this._searchResults.length)
  358.             this._currentSearchResultIndex = 0;
  359.         this._jumpToSearchResult(this._currentSearchResultIndex);
  360.     },
  361.  
  362.     jumpToPreviousSearchResult: function()
  363.     {
  364.         if (!this._searchResults || !this._searchResults.length)
  365.             return;
  366.         if (--this._currentSearchResultIndex < 0)
  367.             this._currentSearchResultIndex = (this._searchResults.length - 1);
  368.         this._jumpToSearchResult(this._currentSearchResultIndex);
  369.     },
  370.  
  371.     showingFirstSearchResult: function()
  372.     {
  373.         return (this._currentSearchResultIndex === 0);
  374.     },
  375.  
  376.     showingLastSearchResult: function()
  377.     {
  378.         return (this._searchResults && this._currentSearchResultIndex === (this._searchResults.length - 1));
  379.     },
  380.  
  381.     _jumpToSearchResult: function(index)
  382.     {
  383.         var searchResult = this._searchResults[index];
  384.         if (!searchResult)
  385.             return;
  386.  
  387.         var profileNode = searchResult.profileNode;
  388.         profileNode.reveal();
  389.         profileNode.select();
  390.     },
  391.  
  392.     _changeView: function(event)
  393.     {
  394.         if (!event || !this.profile)
  395.             return;
  396.  
  397.         if (event.target.selectedIndex == 1 && this.view == "Heavy") {
  398.             this.profileDataGridTree = this.topDownProfileDataGridTree;
  399.             this._sortProfile();
  400.             this.view = "Tree";
  401.         } else if (event.target.selectedIndex == 0 && this.view == "Tree") {
  402.             this.profileDataGridTree = this.bottomUpProfileDataGridTree;
  403.             this._sortProfile();
  404.             this.view = "Heavy";
  405.         }
  406.  
  407.         if (!this.currentQuery || !this._searchFinishedCallback || !this._searchResults)
  408.             return;
  409.  
  410.         // The current search needs to be performed again. First negate out previous match
  411.         // count by calling the search finished callback with a negative number of matches.
  412.         // Then perform the search again the with same query and callback.
  413.         this._searchFinishedCallback(this, -this._searchResults.length);
  414.         this.performSearch(this.currentQuery, this._searchFinishedCallback);
  415.     },
  416.  
  417.     _percentClicked: function(event)
  418.     {
  419.         var currentState = this.showSelfTimeAsPercent && this.showTotalTimeAsPercent && this.showAverageTimeAsPercent;
  420.         this.showSelfTimeAsPercent = !currentState;
  421.         this.showTotalTimeAsPercent = !currentState;
  422.         this.showAverageTimeAsPercent = !currentState;
  423.         this.refreshShowAsPercents();
  424.     },
  425.  
  426.     _updatePercentButton: function()
  427.     {
  428.         if (this.showSelfTimeAsPercent && this.showTotalTimeAsPercent && this.showAverageTimeAsPercent) {
  429.             this.percentButton.title = WebInspector.UIString("Show absolute total and self times.");
  430.             this.percentButton.addStyleClass("toggled-on");
  431.         } else {
  432.             this.percentButton.title = WebInspector.UIString("Show total and self times as percentages.");
  433.             this.percentButton.removeStyleClass("toggled-on");
  434.         }
  435.     },
  436.  
  437.     _focusClicked: function(event)
  438.     {
  439.         if (!this.dataGrid.selectedNode)
  440.             return;
  441.  
  442.         this.resetButton.removeStyleClass("hidden");
  443.         this.profileDataGridTree.focus(this.dataGrid.selectedNode);
  444.         this.refresh();
  445.         this.refreshVisibleData();
  446.     },
  447.  
  448.     _excludeClicked: function(event)
  449.     {
  450.         var selectedNode = this.dataGrid.selectedNode
  451.  
  452.         if (!selectedNode)
  453.             return;
  454.  
  455.         selectedNode.deselect();
  456.  
  457.         this.resetButton.removeStyleClass("hidden");
  458.         this.profileDataGridTree.exclude(selectedNode);
  459.         this.refresh();
  460.         this.refreshVisibleData();
  461.     },
  462.  
  463.     _resetClicked: function(event)
  464.     {
  465.         this.resetButton.addStyleClass("hidden");
  466.         this.profileDataGridTree.restore();
  467.         this.refresh();
  468.         this.refreshVisibleData();
  469.     },
  470.  
  471.     _dataGridNodeSelected: function(node)
  472.     {
  473.         this.focusButton.disabled = false;
  474.         this.excludeButton.disabled = false;
  475.     },
  476.  
  477.     _dataGridNodeDeselected: function(node)
  478.     {
  479.         this.focusButton.disabled = true;
  480.         this.excludeButton.disabled = true;
  481.     },
  482.  
  483.     _sortData: function(event)
  484.     {
  485.         this._sortProfile(this.profile);
  486.     },
  487.  
  488.     _sortProfile: function()
  489.     {
  490.         var sortAscending = this.dataGrid.sortOrder === "ascending";
  491.         var sortColumnIdentifier = this.dataGrid.sortColumnIdentifier;
  492.         var sortProperty = {
  493.                 "average": "averageTime",
  494.                 "self": "selfTime",
  495.                 "total": "totalTime",
  496.                 "calls": "numberOfCalls",
  497.                 "function": "functionName"
  498.             }[sortColumnIdentifier];
  499.  
  500.         this.profileDataGridTree.sort(WebInspector.ProfileDataGridTree.propertyComparator(sortProperty, sortAscending));
  501.  
  502.         this.refresh();
  503.     },
  504.  
  505.     _mouseDownInDataGrid: function(event)
  506.     {
  507.         if (event.detail < 2)
  508.             return;
  509.  
  510.         var cell = event.target.enclosingNodeOrSelfWithNodeName("td");
  511.         if (!cell || (!cell.hasStyleClass("total-column") && !cell.hasStyleClass("self-column") && !cell.hasStyleClass("average-column")))
  512.             return;
  513.  
  514.         if (cell.hasStyleClass("total-column"))
  515.             this.showTotalTimeAsPercent = !this.showTotalTimeAsPercent;
  516.         else if (cell.hasStyleClass("self-column"))
  517.             this.showSelfTimeAsPercent = !this.showSelfTimeAsPercent;
  518.         else if (cell.hasStyleClass("average-column"))
  519.             this.showAverageTimeAsPercent = !this.showAverageTimeAsPercent;
  520.  
  521.         this.refreshShowAsPercents();
  522.  
  523.         event.preventDefault();
  524.         event.stopPropagation();
  525.     }
  526. }
  527.  
  528. WebInspector.ProfileView.prototype.__proto__ = WebInspector.View.prototype;
  529.