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.PanelWithSidebar}
35 WebInspector.AuditsPanel = function()
37 WebInspector.PanelWithSidebar.call(this, "audits");
38 this.registerRequiredCSS("ui/panelEnablerView.css");
39 this.registerRequiredCSS("audits/auditsPanel.css");
41 var sidebarTree = new TreeOutline();
42 sidebarTree.element.classList.add("sidebar-tree");
43 this.panelSidebarElement().appendChild(sidebarTree.element);
44 this.setDefaultFocusedElement(sidebarTree.element);
46 this.auditsTreeElement = new WebInspector.SidebarSectionTreeElement("");
47 sidebarTree.appendChild(this.auditsTreeElement);
48 this.auditsTreeElement.listItemElement.classList.add("hidden");
50 this.auditsItemTreeElement = new WebInspector.AuditsSidebarTreeElement(this);
51 this.auditsTreeElement.appendChild(this.auditsItemTreeElement);
53 this.auditResultsTreeElement = new WebInspector.SidebarSectionTreeElement(WebInspector.UIString("RESULTS"));
54 sidebarTree.appendChild(this.auditResultsTreeElement);
55 this.auditResultsTreeElement.expand();
57 this._constructCategories();
59 this._auditController = new WebInspector.AuditController(this);
60 this._launcherView = new WebInspector.AuditLauncherView(this._auditController);
61 for (var id in this.categoriesById)
62 this._launcherView.addCategory(this.categoriesById[id]);
64 var extensionCategories = WebInspector.extensionServer.auditCategories();
65 for (var i = 0; i < extensionCategories.length; ++i) {
66 var category = extensionCategories[i];
67 this.addCategory(new WebInspector.AuditExtensionCategory(category.extensionOrigin, category.id, category.displayName, category.ruleCount));
69 WebInspector.extensionServer.addEventListener(WebInspector.ExtensionServer.Events.AuditCategoryAdded, this._extensionAuditCategoryAdded, this);
72 WebInspector.AuditsPanel.prototype = {
75 * @return {!Object.<string, !WebInspector.AuditCategory>}
79 return this._auditCategoriesById;
83 * @param {!WebInspector.AuditCategory} category
85 addCategory: function(category)
87 this.categoriesById[category.id] = category;
88 this._launcherView.addCategory(category);
93 * @return {!WebInspector.AuditCategory}
95 getCategory: function(id)
97 return this.categoriesById[id];
100 _constructCategories: function()
102 this._auditCategoriesById = {};
103 for (var categoryCtorID in WebInspector.AuditCategories) {
104 var auditCategory = new WebInspector.AuditCategories[categoryCtorID]();
105 auditCategory._id = categoryCtorID;
106 this.categoriesById[categoryCtorID] = auditCategory;
111 * @param {string} mainResourceURL
112 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
114 auditFinishedCallback: function(mainResourceURL, results)
117 for (var child of this.auditResultsTreeElement.children()) {
118 if (child.mainResourceURL === mainResourceURL)
122 var resultTreeElement = new WebInspector.AuditResultSidebarTreeElement(this, results, mainResourceURL, ordinal);
123 this.auditResultsTreeElement.appendChild(resultTreeElement);
124 resultTreeElement.revealAndSelect();
128 * @param {!Array.<!WebInspector.AuditCategoryResult>} categoryResults
130 showResults: function(categoryResults)
132 if (!categoryResults._resultView)
133 categoryResults._resultView = new WebInspector.AuditResultView(categoryResults);
135 this.visibleView = categoryResults._resultView;
138 showLauncherView: function()
140 this.visibleView = this._launcherView;
145 return this._visibleView;
150 if (this._visibleView === x)
153 if (this._visibleView)
154 this._visibleView.detach();
156 this._visibleView = x;
159 this.splitWidget().setMainWidget(x);
164 WebInspector.Panel.prototype.wasShown.call(this);
165 if (!this._visibleView)
166 this.auditsItemTreeElement.select();
169 clearResults: function()
171 this.auditsItemTreeElement.revealAndSelect();
172 this.auditResultsTreeElement.removeChildren();
176 * @param {!WebInspector.Event} event
178 _extensionAuditCategoryAdded: function(event)
180 var category = /** @type {!WebInspector.ExtensionAuditCategory} */ (event.data);
181 this.addCategory(new WebInspector.AuditExtensionCategory(category.extensionOrigin, category.id, category.displayName, category.ruleCount));
184 __proto__: WebInspector.PanelWithSidebar.prototype
189 * @implements {WebInspector.AuditCategory}
190 * @param {string} displayName
192 WebInspector.AuditCategoryImpl = function(displayName)
194 this._displayName = displayName;
198 WebInspector.AuditCategoryImpl.prototype = {
205 // this._id value is injected at construction time.
215 return this._displayName;
219 * @param {!WebInspector.AuditRule} rule
220 * @param {!WebInspector.AuditRule.Severity} severity
222 addRule: function(rule, severity)
224 rule.severity = severity;
225 this._rules.push(rule);
230 * @param {!WebInspector.Target} target
231 * @param {!Array.<!WebInspector.NetworkRequest>} requests
232 * @param {function(!WebInspector.AuditRuleResult)} ruleResultCallback
233 * @param {!WebInspector.Progress} progress
235 run: function(target, requests, ruleResultCallback, progress)
237 this._ensureInitialized();
238 var remainingRulesCount = this._rules.length;
239 progress.setTotalWork(remainingRulesCount);
240 function callbackWrapper(result)
242 ruleResultCallback(result);
244 if (!--remainingRulesCount)
247 for (var i = 0; i < this._rules.length; ++i) {
248 if (!progress.isCanceled())
249 this._rules[i].run(target, requests, callbackWrapper, progress);
251 callbackWrapper(null);
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"));
475 WebInspector.AuditsSidebarTreeElement.prototype = {
478 WebInspector.SidebarTreeElement.prototype.onattach.call(this);
487 this._panel.showLauncherView();
498 this.refreshTitles();
501 __proto__: WebInspector.SidebarTreeElement.prototype
506 * @extends {WebInspector.SidebarTreeElement}
507 * @param {!WebInspector.AuditsPanel} panel
508 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
509 * @param {string} mainResourceURL
510 * @param {number} ordinal
512 WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
515 this.results = results;
516 this.mainResourceURL = mainResourceURL;
517 WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal));
520 WebInspector.AuditResultSidebarTreeElement.prototype = {
527 this._panel.showResults(this.results);
536 __proto__: WebInspector.SidebarTreeElement.prototype
539 WebInspector.AuditsPanel.show = function()
541 WebInspector.inspectorView.setCurrentPanel(WebInspector.AuditsPanel.instance());
545 * @return {!WebInspector.AuditsPanel}
547 WebInspector.AuditsPanel.instance = function()
549 if (!WebInspector.AuditsPanel._instanceObject)
550 WebInspector.AuditsPanel._instanceObject = new WebInspector.AuditsPanel();
551 return WebInspector.AuditsPanel._instanceObject;
556 * @implements {WebInspector.PanelFactory}
558 WebInspector.AuditsPanelFactory = function()
562 WebInspector.AuditsPanelFactory.prototype = {
565 * @return {!WebInspector.Panel}
567 createPanel: function()
569 return WebInspector.AuditsPanel.instance();
573 // Contributed audit rules should go into this namespace.
574 WebInspector.AuditRules = {};
577 * Contributed audit categories should go into this namespace.
578 * @type {!Object.<string, function(new:WebInspector.AuditCategory)>}
580 WebInspector.AuditCategories = {};