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("components/panelEnablerView.css");
39 this.registerRequiredCSS("audits/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]);
59 var extensionCategories = WebInspector.extensionServer.auditCategories();
60 for (var i = 0; i < extensionCategories.length; ++i) {
61 var category = extensionCategories[i];
62 this.addCategory(new WebInspector.AuditExtensionCategory(category.extensionOrigin, category.id, category.displayName, category.ruleCount));
64 WebInspector.extensionServer.addEventListener(WebInspector.ExtensionServer.Events.AuditCategoryAdded, this._extensionAuditCategoryAdded, this);
67 WebInspector.AuditsPanel.prototype = {
77 * @return {!Object.<string, !WebInspector.AuditCategory>}
81 return this._auditCategoriesById;
85 * @param {!WebInspector.AuditCategory} category
87 addCategory: function(category)
89 this.categoriesById[category.id] = category;
90 this._launcherView.addCategory(category);
95 * @return {!WebInspector.AuditCategory}
97 getCategory: function(id)
99 return this.categoriesById[id];
102 _constructCategories: function()
104 this._auditCategoriesById = {};
105 for (var categoryCtorID in WebInspector.AuditCategories) {
106 var auditCategory = new WebInspector.AuditCategories[categoryCtorID]();
107 auditCategory._id = categoryCtorID;
108 this.categoriesById[categoryCtorID] = auditCategory;
113 * @param {string} mainResourceURL
114 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
116 auditFinishedCallback: function(mainResourceURL, results)
118 var children = this.auditResultsTreeElement.children;
120 for (var i = 0; i < children.length; ++i) {
121 if (children[i].mainResourceURL === mainResourceURL)
125 var resultTreeElement = new WebInspector.AuditResultSidebarTreeElement(this, results, mainResourceURL, ordinal);
126 this.auditResultsTreeElement.appendChild(resultTreeElement);
127 resultTreeElement.revealAndSelect();
131 * @param {!Array.<!WebInspector.AuditCategoryResult>} categoryResults
133 showResults: function(categoryResults)
135 if (!categoryResults._resultView)
136 categoryResults._resultView = new WebInspector.AuditResultView(categoryResults);
138 this.visibleView = categoryResults._resultView;
141 showLauncherView: function()
143 this.visibleView = this._launcherView;
148 return this._visibleView;
153 if (this._visibleView === x)
156 if (this._visibleView)
157 this._visibleView.detach();
159 this._visibleView = x;
162 x.show(this.mainElement());
167 WebInspector.Panel.prototype.wasShown.call(this);
168 if (!this._visibleView)
169 this.auditsItemTreeElement.select();
172 clearResults: function()
174 this.auditsItemTreeElement.revealAndSelect();
175 this.auditResultsTreeElement.removeChildren();
179 * @param {!WebInspector.Event} event
181 _extensionAuditCategoryAdded: function(event)
183 var category = /** @type {!WebInspector.ExtensionAuditCategory} */ (event.data);
184 this.addCategory(new WebInspector.AuditExtensionCategory(category.extensionOrigin, category.id, category.displayName, category.ruleCount));
187 __proto__: WebInspector.PanelWithSidebarTree.prototype
192 * @implements {WebInspector.AuditCategory}
193 * @param {string} displayName
195 WebInspector.AuditCategoryImpl = function(displayName)
197 this._displayName = displayName;
201 WebInspector.AuditCategoryImpl.prototype = {
208 // this._id value is injected at construction time.
218 return this._displayName;
222 * @param {!WebInspector.AuditRule} rule
223 * @param {!WebInspector.AuditRule.Severity} severity
225 addRule: function(rule, severity)
227 rule.severity = severity;
228 this._rules.push(rule);
233 * @param {!WebInspector.Target} target
234 * @param {!Array.<!WebInspector.NetworkRequest>} requests
235 * @param {function(!WebInspector.AuditRuleResult)} ruleResultCallback
236 * @param {function()} categoryDoneCallback
237 * @param {!WebInspector.Progress} progress
239 run: function(target, requests, ruleResultCallback, categoryDoneCallback, progress)
241 this._ensureInitialized();
242 var remainingRulesCount = this._rules.length;
243 progress.setTotalWork(remainingRulesCount);
244 function callbackWrapper(result)
246 ruleResultCallback(result);
248 if (!--remainingRulesCount)
249 categoryDoneCallback();
251 for (var i = 0; i < this._rules.length; ++i)
252 this._rules[i].run(target, requests, callbackWrapper, progress);
255 _ensureInitialized: function()
257 if (!this._initialized) {
258 if ("initialize" in this)
260 this._initialized = true;
268 * @param {string} displayName
270 WebInspector.AuditRule = function(id, displayName)
273 this._displayName = displayName;
279 WebInspector.AuditRule.Severity = {
285 WebInspector.AuditRule.SeverityOrder = {
291 WebInspector.AuditRule.prototype = {
299 return this._displayName;
303 * @param {!WebInspector.AuditRule.Severity} severity
305 set severity(severity)
307 this._severity = severity;
311 * @param {!WebInspector.Target} target
312 * @param {!Array.<!WebInspector.NetworkRequest>} requests
313 * @param {function(!WebInspector.AuditRuleResult)} callback
314 * @param {!WebInspector.Progress} progress
316 run: function(target, requests, callback, progress)
318 if (progress.isCanceled())
321 var result = new WebInspector.AuditRuleResult(this.displayName);
322 result.severity = this._severity;
323 this.doRun(target, requests, result, callback, progress);
327 * @param {!WebInspector.Target} target
328 * @param {!Array.<!WebInspector.NetworkRequest>} requests
329 * @param {!WebInspector.AuditRuleResult} result
330 * @param {function(!WebInspector.AuditRuleResult)} callback
331 * @param {!WebInspector.Progress} progress
333 doRun: function(target, requests, result, callback, progress)
335 throw new Error("doRun() not implemented");
341 * @param {!WebInspector.AuditCategory} category
343 WebInspector.AuditCategoryResult = function(category)
345 this.title = category.displayName;
346 this.ruleResults = [];
349 WebInspector.AuditCategoryResult.prototype = {
351 * @param {!WebInspector.AuditRuleResult} ruleResult
353 addRuleResult: function(ruleResult)
355 this.ruleResults.push(ruleResult);
361 * @param {(string|boolean|number|!Object)} value
362 * @param {boolean=} expanded
363 * @param {string=} className
365 WebInspector.AuditRuleResult = function(value, expanded, className)
368 this.className = className;
369 this.expanded = expanded;
370 this.violationCount = 0;
372 r: WebInspector.AuditRuleResult.linkifyDisplayName
374 var standardFormatters = Object.keys(String.standardFormatters);
375 for (var i = 0; i < standardFormatters.length; ++i)
376 this._formatters[standardFormatters[i]] = String.standardFormatters[standardFormatters[i]];
380 * @param {string} url
383 WebInspector.AuditRuleResult.linkifyDisplayName = function(url)
385 return WebInspector.linkifyURLAsNode(url, WebInspector.displayNameForURL(url));
389 * @param {string} domain
392 WebInspector.AuditRuleResult.resourceDomain = function(domain)
394 return domain || WebInspector.UIString("[empty domain]");
397 WebInspector.AuditRuleResult.prototype = {
399 * @param {(string|boolean|number|!Object)} value
400 * @param {boolean=} expanded
401 * @param {string=} className
402 * @return {!WebInspector.AuditRuleResult}
404 addChild: function(value, expanded, className)
408 var entry = new WebInspector.AuditRuleResult(value, expanded, className);
409 this.children.push(entry);
414 * @param {string} url
416 addURL: function(url)
418 this.addChild(WebInspector.AuditRuleResult.linkifyDisplayName(url));
422 * @param {!Array.<string>} urls
424 addURLs: function(urls)
426 for (var i = 0; i < urls.length; ++i)
427 this.addURL(urls[i]);
431 * @param {string} snippet
433 addSnippet: function(snippet)
435 this.addChild(snippet, false, "source-code");
439 * @param {string} format
440 * @param {...*} vararg
441 * @return {!WebInspector.AuditRuleResult}
443 addFormatted: function(format, vararg)
445 var substitutions = Array.prototype.slice.call(arguments, 1);
446 var fragment = createDocumentFragment();
448 function append(a, b)
450 if (!(b instanceof Node))
451 b = createTextNode(b);
456 var formattedResult = String.format(format, substitutions, this._formatters, fragment, append).formattedResult;
457 if (formattedResult instanceof Node)
458 formattedResult.normalize();
459 return this.addChild(formattedResult);
465 * @extends {WebInspector.SidebarTreeElement}
466 * @param {!WebInspector.AuditsPanel} panel
468 WebInspector.AuditsSidebarTreeElement = function(panel)
472 WebInspector.SidebarTreeElement.call(this, "audits-sidebar-tree-item", WebInspector.UIString("Audits"), "", null, false);
475 WebInspector.AuditsSidebarTreeElement.prototype = {
478 WebInspector.SidebarTreeElement.prototype.onattach.call(this);
486 this._panel.showLauncherView();
497 this.refreshTitles();
500 __proto__: WebInspector.SidebarTreeElement.prototype
505 * @extends {WebInspector.SidebarTreeElement}
506 * @param {!WebInspector.AuditsPanel} panel
507 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
508 * @param {string} mainResourceURL
509 * @param {number} ordinal
511 WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
514 this.results = results;
515 this.mainResourceURL = mainResourceURL;
516 WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal), "", {}, false);
519 WebInspector.AuditResultSidebarTreeElement.prototype = {
525 this._panel.showResults(this.results);
534 __proto__: WebInspector.SidebarTreeElement.prototype
537 WebInspector.AuditsPanel.show = function()
539 WebInspector.inspectorView.setCurrentPanel(WebInspector.AuditsPanel.instance());
543 * @return {!WebInspector.AuditsPanel}
545 WebInspector.AuditsPanel.instance = function()
547 if (!WebInspector.AuditsPanel._instanceObject)
548 WebInspector.AuditsPanel._instanceObject = new WebInspector.AuditsPanel();
549 return WebInspector.AuditsPanel._instanceObject;
554 * @implements {WebInspector.PanelFactory}
556 WebInspector.AuditsPanelFactory = function()
560 WebInspector.AuditsPanelFactory.prototype = {
562 * @return {!WebInspector.Panel}
564 createPanel: function()
566 return WebInspector.AuditsPanel.instance();
570 // Contributed audit rules should go into this namespace.
571 WebInspector.AuditRules = {};
574 * Contributed audit categories should go into this namespace.
575 * @type {!Object.<string, function(new:WebInspector.AuditCategory)>}
577 WebInspector.AuditCategories = {};