2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @extends {WebInspector.PanelWithSidebarTree}
35 WebInspector.AuditsPanel = function()
37 WebInspector.PanelWithSidebarTree.call(this, "audits");
38 this.registerRequiredCSS("panelEnablerView.css");
39 this.registerRequiredCSS("auditsPanel.css");
40 this.setMainElementConstraints(215);
42 this.auditsTreeElement = new WebInspector.SidebarSectionTreeElement("", {}, true);
43 this.sidebarTree.appendChild(this.auditsTreeElement);
44 this.auditsTreeElement.listItemElement.classList.add("hidden");
46 this.auditsItemTreeElement = new WebInspector.AuditsSidebarTreeElement(this);
47 this.auditsTreeElement.appendChild(this.auditsItemTreeElement);
49 this.auditResultsTreeElement = new WebInspector.SidebarSectionTreeElement(WebInspector.UIString("RESULTS"), {}, true);
50 this.sidebarTree.appendChild(this.auditResultsTreeElement);
51 this.auditResultsTreeElement.expand();
53 this._constructCategories();
55 this._auditController = new WebInspector.AuditController(this);
56 this._launcherView = new WebInspector.AuditLauncherView(this._auditController);
57 for (var id in this.categoriesById)
58 this._launcherView.addCategory(this.categoriesById[id]);
61 WebInspector.AuditsPanel.prototype = {
71 * @return {!Object.<string, !WebInspector.AuditCategory>}
75 return this._auditCategoriesById;
79 * @param {!WebInspector.AuditCategory} category
81 addCategory: function(category)
83 this.categoriesById[category.id] = category;
84 this._launcherView.addCategory(category);
89 * @return {!WebInspector.AuditCategory}
91 getCategory: function(id)
93 return this.categoriesById[id];
96 _constructCategories: function()
98 this._auditCategoriesById = {};
99 for (var categoryCtorID in WebInspector.AuditCategories) {
100 var auditCategory = new WebInspector.AuditCategories[categoryCtorID]();
101 auditCategory._id = categoryCtorID;
102 this.categoriesById[categoryCtorID] = auditCategory;
107 * @param {string} mainResourceURL
108 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
110 auditFinishedCallback: function(mainResourceURL, results)
112 var children = this.auditResultsTreeElement.children;
114 for (var i = 0; i < children.length; ++i) {
115 if (children[i].mainResourceURL === mainResourceURL)
119 var resultTreeElement = new WebInspector.AuditResultSidebarTreeElement(this, results, mainResourceURL, ordinal);
120 this.auditResultsTreeElement.appendChild(resultTreeElement);
121 resultTreeElement.revealAndSelect();
125 * @param {!Array.<!WebInspector.AuditCategoryResult>} categoryResults
127 showResults: function(categoryResults)
129 if (!categoryResults._resultView)
130 categoryResults._resultView = new WebInspector.AuditResultView(categoryResults);
132 this.visibleView = categoryResults._resultView;
135 showLauncherView: function()
137 this.visibleView = this._launcherView;
142 return this._visibleView;
147 if (this._visibleView === x)
150 if (this._visibleView)
151 this._visibleView.detach();
153 this._visibleView = x;
156 x.show(this.mainElement());
161 WebInspector.Panel.prototype.wasShown.call(this);
162 if (!this._visibleView)
163 this.auditsItemTreeElement.select();
166 clearResults: function()
168 this.auditsItemTreeElement.revealAndSelect();
169 this.auditResultsTreeElement.removeChildren();
172 __proto__: WebInspector.PanelWithSidebarTree.prototype
177 * @implements {WebInspector.AuditCategory}
178 * @param {string} displayName
180 WebInspector.AuditCategoryImpl = function(displayName)
182 this._displayName = displayName;
186 WebInspector.AuditCategoryImpl.prototype = {
193 // this._id value is injected at construction time.
203 return this._displayName;
207 * @param {!WebInspector.AuditRule} rule
208 * @param {!WebInspector.AuditRule.Severity} severity
210 addRule: function(rule, severity)
212 rule.severity = severity;
213 this._rules.push(rule);
218 * @param {!Array.<!WebInspector.NetworkRequest>} requests
219 * @param {function(!WebInspector.AuditRuleResult)} ruleResultCallback
220 * @param {function()} categoryDoneCallback
221 * @param {!WebInspector.Progress} progress
223 run: function(requests, ruleResultCallback, categoryDoneCallback, progress)
225 this._ensureInitialized();
226 var remainingRulesCount = this._rules.length;
227 progress.setTotalWork(remainingRulesCount);
228 function callbackWrapper(result)
230 ruleResultCallback(result);
232 if (!--remainingRulesCount)
233 categoryDoneCallback();
235 for (var i = 0; i < this._rules.length; ++i)
236 this._rules[i].run(requests, callbackWrapper, progress);
239 _ensureInitialized: function()
241 if (!this._initialized) {
242 if ("initialize" in this)
244 this._initialized = true;
252 * @param {string} displayName
254 WebInspector.AuditRule = function(id, displayName)
257 this._displayName = displayName;
263 WebInspector.AuditRule.Severity = {
272 WebInspector.AuditRule.SeverityOrder = {
278 WebInspector.AuditRule.prototype = {
286 return this._displayName;
290 * @param {!WebInspector.AuditRule.Severity} severity
292 set severity(severity)
294 this._severity = severity;
298 * @param {!Array.<!WebInspector.NetworkRequest>} requests
299 * @param {function(!WebInspector.AuditRuleResult)} callback
300 * @param {!WebInspector.Progress} progress
302 run: function(requests, callback, progress)
304 if (progress.isCanceled())
307 var result = new WebInspector.AuditRuleResult(this.displayName);
308 result.severity = this._severity;
309 this.doRun(requests, result, callback, progress);
313 * @param {!Array.<!WebInspector.NetworkRequest>} requests
314 * @param {!WebInspector.AuditRuleResult} result
315 * @param {function(!WebInspector.AuditRuleResult)} callback
316 * @param {!WebInspector.Progress} progress
318 doRun: function(requests, result, callback, progress)
320 throw new Error("doRun() not implemented");
326 * @param {!WebInspector.AuditCategory} category
328 WebInspector.AuditCategoryResult = function(category)
330 this.title = category.displayName;
331 this.ruleResults = [];
334 WebInspector.AuditCategoryResult.prototype = {
336 * @param {!WebInspector.AuditRuleResult} ruleResult
338 addRuleResult: function(ruleResult)
340 this.ruleResults.push(ruleResult);
346 * @param {(string|boolean|number|!Object)} value
347 * @param {boolean=} expanded
348 * @param {string=} className
350 WebInspector.AuditRuleResult = function(value, expanded, className)
353 this.className = className;
354 this.expanded = expanded;
355 this.violationCount = 0;
357 r: WebInspector.AuditRuleResult.linkifyDisplayName
359 var standardFormatters = Object.keys(String.standardFormatters);
360 for (var i = 0; i < standardFormatters.length; ++i)
361 this._formatters[standardFormatters[i]] = String.standardFormatters[standardFormatters[i]];
365 * @param {string} url
368 WebInspector.AuditRuleResult.linkifyDisplayName = function(url)
370 return WebInspector.linkifyURLAsNode(url, WebInspector.displayNameForURL(url));
373 WebInspector.AuditRuleResult.resourceDomain = function(domain)
375 return domain || WebInspector.UIString("[empty domain]");
378 WebInspector.AuditRuleResult.prototype = {
380 * @param {(string|boolean|number|!Object)} value
381 * @param {boolean=} expanded
382 * @param {string=} className
383 * @return {!WebInspector.AuditRuleResult}
385 addChild: function(value, expanded, className)
389 var entry = new WebInspector.AuditRuleResult(value, expanded, className);
390 this.children.push(entry);
395 * @param {string} url
397 addURL: function(url)
399 this.addChild(WebInspector.AuditRuleResult.linkifyDisplayName(url));
403 * @param {!Array.<string>} urls
405 addURLs: function(urls)
407 for (var i = 0; i < urls.length; ++i)
408 this.addURL(urls[i]);
412 * @param {string} snippet
414 addSnippet: function(snippet)
416 this.addChild(snippet, false, "source-code");
420 * @param {string} format
421 * @param {...*} vararg
422 * @return {!WebInspector.AuditRuleResult}
424 addFormatted: function(format, vararg)
426 var substitutions = Array.prototype.slice.call(arguments, 1);
427 var fragment = document.createDocumentFragment();
429 function append(a, b)
431 if (!(b instanceof Node))
432 b = document.createTextNode(b);
437 var formattedResult = String.format(format, substitutions, this._formatters, fragment, append).formattedResult;
438 if (formattedResult instanceof Node)
439 formattedResult.normalize();
440 return this.addChild(formattedResult);
446 * @extends {WebInspector.SidebarTreeElement}
447 * @param {!WebInspector.AuditsPanel} panel
449 WebInspector.AuditsSidebarTreeElement = function(panel)
453 WebInspector.SidebarTreeElement.call(this, "audits-sidebar-tree-item", WebInspector.UIString("Audits"), "", null, false);
456 WebInspector.AuditsSidebarTreeElement.prototype = {
459 WebInspector.SidebarTreeElement.prototype.onattach.call(this);
464 this._panel.showLauncherView();
474 this.refreshTitles();
477 __proto__: WebInspector.SidebarTreeElement.prototype
482 * @extends {WebInspector.SidebarTreeElement}
483 * @param {!WebInspector.AuditsPanel} panel
484 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
485 * @param {string} mainResourceURL
486 * @param {number} ordinal
488 WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
491 this.results = results;
492 this.mainResourceURL = mainResourceURL;
493 WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal), "", {}, false);
496 WebInspector.AuditResultSidebarTreeElement.prototype = {
499 this._panel.showResults(this.results);
507 __proto__: WebInspector.SidebarTreeElement.prototype
510 // Contributed audit rules should go into this namespace.
511 WebInspector.AuditRules = {};
514 * Contributed audit categories should go into this namespace.
515 * @type {!Object.<string, function(new:WebInspector.AuditCategory)>}
517 WebInspector.AuditCategories = {};
519 importScript("AuditCategory.js");
520 importScript("AuditCategories.js");
521 importScript("AuditController.js");
522 importScript("AuditFormatters.js");
523 importScript("AuditLauncherView.js");
524 importScript("AuditResultView.js");
525 importScript("AuditRules.js");