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");
41 this.auditsTreeElement = new WebInspector.SidebarSectionTreeElement("", {}, true);
42 this.sidebarTree.appendChild(this.auditsTreeElement);
43 this.auditsTreeElement.listItemElement.classList.add("hidden");
45 this.auditsItemTreeElement = new WebInspector.AuditsSidebarTreeElement(this);
46 this.auditsTreeElement.appendChild(this.auditsItemTreeElement);
48 this.auditResultsTreeElement = new WebInspector.SidebarSectionTreeElement(WebInspector.UIString("RESULTS"), {}, true);
49 this.sidebarTree.appendChild(this.auditResultsTreeElement);
50 this.auditResultsTreeElement.expand();
52 this._constructCategories();
54 this._auditController = new WebInspector.AuditController(this);
55 this._launcherView = new WebInspector.AuditLauncherView(this._auditController);
56 for (var id in this.categoriesById)
57 this._launcherView.addCategory(this.categoriesById[id]);
60 WebInspector.AuditsPanel.prototype = {
70 * @return {!Object.<string, !WebInspector.AuditCategory>}
74 return this._auditCategoriesById;
78 * @param {!WebInspector.AuditCategory} category
80 addCategory: function(category)
82 this.categoriesById[category.id] = category;
83 this._launcherView.addCategory(category);
88 * @return {!WebInspector.AuditCategory}
90 getCategory: function(id)
92 return this.categoriesById[id];
95 _constructCategories: function()
97 this._auditCategoriesById = {};
98 for (var categoryCtorID in WebInspector.AuditCategories) {
99 var auditCategory = new WebInspector.AuditCategories[categoryCtorID]();
100 auditCategory._id = categoryCtorID;
101 this.categoriesById[categoryCtorID] = auditCategory;
106 * @param {string} mainResourceURL
107 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
109 auditFinishedCallback: function(mainResourceURL, results)
111 var children = this.auditResultsTreeElement.children;
113 for (var i = 0; i < children.length; ++i) {
114 if (children[i].mainResourceURL === mainResourceURL)
118 var resultTreeElement = new WebInspector.AuditResultSidebarTreeElement(this, results, mainResourceURL, ordinal);
119 this.auditResultsTreeElement.appendChild(resultTreeElement);
120 resultTreeElement.revealAndSelect();
124 * @param {!Array.<!WebInspector.AuditCategoryResult>} categoryResults
126 showResults: function(categoryResults)
128 if (!categoryResults._resultView)
129 categoryResults._resultView = new WebInspector.AuditResultView(categoryResults);
131 this.visibleView = categoryResults._resultView;
134 showLauncherView: function()
136 this.visibleView = this._launcherView;
141 return this._visibleView;
146 if (this._visibleView === x)
149 if (this._visibleView)
150 this._visibleView.detach();
152 this._visibleView = x;
155 x.show(this.mainElement());
160 WebInspector.Panel.prototype.wasShown.call(this);
161 if (!this._visibleView)
162 this.auditsItemTreeElement.select();
165 clearResults: function()
167 this.auditsItemTreeElement.revealAndSelect();
168 this.auditResultsTreeElement.removeChildren();
171 __proto__: WebInspector.PanelWithSidebarTree.prototype
176 * @implements {WebInspector.AuditCategory}
177 * @param {string} displayName
179 WebInspector.AuditCategoryImpl = function(displayName)
181 this._displayName = displayName;
185 WebInspector.AuditCategoryImpl.prototype = {
192 // this._id value is injected at construction time.
202 return this._displayName;
206 * @param {!WebInspector.AuditRule} rule
207 * @param {!WebInspector.AuditRule.Severity} severity
209 addRule: function(rule, severity)
211 rule.severity = severity;
212 this._rules.push(rule);
217 * @param {!WebInspector.Target} target
218 * @param {!Array.<!WebInspector.NetworkRequest>} requests
219 * @param {function(!WebInspector.AuditRuleResult)} ruleResultCallback
220 * @param {function()} categoryDoneCallback
221 * @param {!WebInspector.Progress} progress
223 run: function(target, 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(target, 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 = {
269 WebInspector.AuditRule.SeverityOrder = {
275 WebInspector.AuditRule.prototype = {
283 return this._displayName;
287 * @param {!WebInspector.AuditRule.Severity} severity
289 set severity(severity)
291 this._severity = severity;
295 * @param {!WebInspector.Target} target
296 * @param {!Array.<!WebInspector.NetworkRequest>} requests
297 * @param {function(!WebInspector.AuditRuleResult)} callback
298 * @param {!WebInspector.Progress} progress
300 run: function(target, requests, callback, progress)
302 if (progress.isCanceled())
305 var result = new WebInspector.AuditRuleResult(this.displayName);
306 result.severity = this._severity;
307 this.doRun(target, requests, result, callback, progress);
311 * @param {!WebInspector.Target} target
312 * @param {!Array.<!WebInspector.NetworkRequest>} requests
313 * @param {!WebInspector.AuditRuleResult} result
314 * @param {function(!WebInspector.AuditRuleResult)} callback
315 * @param {!WebInspector.Progress} progress
317 doRun: function(target, requests, result, callback, progress)
319 throw new Error("doRun() not implemented");
325 * @param {!WebInspector.AuditCategory} category
327 WebInspector.AuditCategoryResult = function(category)
329 this.title = category.displayName;
330 this.ruleResults = [];
333 WebInspector.AuditCategoryResult.prototype = {
335 * @param {!WebInspector.AuditRuleResult} ruleResult
337 addRuleResult: function(ruleResult)
339 this.ruleResults.push(ruleResult);
345 * @param {(string|boolean|number|!Object)} value
346 * @param {boolean=} expanded
347 * @param {string=} className
349 WebInspector.AuditRuleResult = function(value, expanded, className)
352 this.className = className;
353 this.expanded = expanded;
354 this.violationCount = 0;
356 r: WebInspector.AuditRuleResult.linkifyDisplayName
358 var standardFormatters = Object.keys(String.standardFormatters);
359 for (var i = 0; i < standardFormatters.length; ++i)
360 this._formatters[standardFormatters[i]] = String.standardFormatters[standardFormatters[i]];
364 * @param {string} url
367 WebInspector.AuditRuleResult.linkifyDisplayName = function(url)
369 return WebInspector.linkifyURLAsNode(url, WebInspector.displayNameForURL(url));
373 * @param {string} domain
376 WebInspector.AuditRuleResult.resourceDomain = function(domain)
378 return domain || WebInspector.UIString("[empty domain]");
381 WebInspector.AuditRuleResult.prototype = {
383 * @param {(string|boolean|number|!Object)} value
384 * @param {boolean=} expanded
385 * @param {string=} className
386 * @return {!WebInspector.AuditRuleResult}
388 addChild: function(value, expanded, className)
392 var entry = new WebInspector.AuditRuleResult(value, expanded, className);
393 this.children.push(entry);
398 * @param {string} url
400 addURL: function(url)
402 this.addChild(WebInspector.AuditRuleResult.linkifyDisplayName(url));
406 * @param {!Array.<string>} urls
408 addURLs: function(urls)
410 for (var i = 0; i < urls.length; ++i)
411 this.addURL(urls[i]);
415 * @param {string} snippet
417 addSnippet: function(snippet)
419 this.addChild(snippet, false, "source-code");
423 * @param {string} format
424 * @param {...*} vararg
425 * @return {!WebInspector.AuditRuleResult}
427 addFormatted: function(format, vararg)
429 var substitutions = Array.prototype.slice.call(arguments, 1);
430 var fragment = document.createDocumentFragment();
432 function append(a, b)
434 if (!(b instanceof Node))
435 b = document.createTextNode(b);
440 var formattedResult = String.format(format, substitutions, this._formatters, fragment, append).formattedResult;
441 if (formattedResult instanceof Node)
442 formattedResult.normalize();
443 return this.addChild(formattedResult);
449 * @extends {WebInspector.SidebarTreeElement}
450 * @param {!WebInspector.AuditsPanel} panel
452 WebInspector.AuditsSidebarTreeElement = function(panel)
456 WebInspector.SidebarTreeElement.call(this, "audits-sidebar-tree-item", WebInspector.UIString("Audits"), "", null, false);
459 WebInspector.AuditsSidebarTreeElement.prototype = {
462 WebInspector.SidebarTreeElement.prototype.onattach.call(this);
470 this._panel.showLauncherView();
481 this.refreshTitles();
484 __proto__: WebInspector.SidebarTreeElement.prototype
489 * @extends {WebInspector.SidebarTreeElement}
490 * @param {!WebInspector.AuditsPanel} panel
491 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
492 * @param {string} mainResourceURL
493 * @param {number} ordinal
495 WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
498 this.results = results;
499 this.mainResourceURL = mainResourceURL;
500 WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal), "", {}, false);
503 WebInspector.AuditResultSidebarTreeElement.prototype = {
509 this._panel.showResults(this.results);
518 __proto__: WebInspector.SidebarTreeElement.prototype
521 // Contributed audit rules should go into this namespace.
522 WebInspector.AuditRules = {};
525 * Contributed audit categories should go into this namespace.
526 * @type {!Object.<string, function(new:WebInspector.AuditCategory)>}
528 WebInspector.AuditCategories = {};