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 var target = /** @type {!WebInspector.Target} */ (WebInspector.targetManager.mainTarget());
55 this._auditController = new WebInspector.AuditController(target, 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 {!WebInspector.Target} target
219 * @param {!Array.<!WebInspector.NetworkRequest>} requests
220 * @param {function(!WebInspector.AuditRuleResult)} ruleResultCallback
221 * @param {function()} categoryDoneCallback
222 * @param {!WebInspector.Progress} progress
224 run: function(target, requests, ruleResultCallback, categoryDoneCallback, progress)
226 this._ensureInitialized();
227 var remainingRulesCount = this._rules.length;
228 progress.setTotalWork(remainingRulesCount);
229 function callbackWrapper(result)
231 ruleResultCallback(result);
233 if (!--remainingRulesCount)
234 categoryDoneCallback();
236 for (var i = 0; i < this._rules.length; ++i)
237 this._rules[i].run(target, requests, callbackWrapper, progress);
240 _ensureInitialized: function()
242 if (!this._initialized) {
243 if ("initialize" in this)
245 this._initialized = true;
253 * @param {string} displayName
255 WebInspector.AuditRule = function(id, displayName)
258 this._displayName = displayName;
264 WebInspector.AuditRule.Severity = {
270 WebInspector.AuditRule.SeverityOrder = {
276 WebInspector.AuditRule.prototype = {
284 return this._displayName;
288 * @param {!WebInspector.AuditRule.Severity} severity
290 set severity(severity)
292 this._severity = severity;
296 * @param {!WebInspector.Target} target
297 * @param {!Array.<!WebInspector.NetworkRequest>} requests
298 * @param {function(!WebInspector.AuditRuleResult)} callback
299 * @param {!WebInspector.Progress} progress
301 run: function(target, requests, callback, progress)
303 if (progress.isCanceled())
306 var result = new WebInspector.AuditRuleResult(this.displayName);
307 result.severity = this._severity;
308 this.doRun(target, requests, result, callback, progress);
312 * @param {!WebInspector.Target} target
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(target, 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));
374 * @param {string} domain
377 WebInspector.AuditRuleResult.resourceDomain = function(domain)
379 return domain || WebInspector.UIString("[empty domain]");
382 WebInspector.AuditRuleResult.prototype = {
384 * @param {(string|boolean|number|!Object)} value
385 * @param {boolean=} expanded
386 * @param {string=} className
387 * @return {!WebInspector.AuditRuleResult}
389 addChild: function(value, expanded, className)
393 var entry = new WebInspector.AuditRuleResult(value, expanded, className);
394 this.children.push(entry);
399 * @param {string} url
401 addURL: function(url)
403 this.addChild(WebInspector.AuditRuleResult.linkifyDisplayName(url));
407 * @param {!Array.<string>} urls
409 addURLs: function(urls)
411 for (var i = 0; i < urls.length; ++i)
412 this.addURL(urls[i]);
416 * @param {string} snippet
418 addSnippet: function(snippet)
420 this.addChild(snippet, false, "source-code");
424 * @param {string} format
425 * @param {...*} vararg
426 * @return {!WebInspector.AuditRuleResult}
428 addFormatted: function(format, vararg)
430 var substitutions = Array.prototype.slice.call(arguments, 1);
431 var fragment = document.createDocumentFragment();
433 function append(a, b)
435 if (!(b instanceof Node))
436 b = document.createTextNode(b);
441 var formattedResult = String.format(format, substitutions, this._formatters, fragment, append).formattedResult;
442 if (formattedResult instanceof Node)
443 formattedResult.normalize();
444 return this.addChild(formattedResult);
450 * @extends {WebInspector.SidebarTreeElement}
451 * @param {!WebInspector.AuditsPanel} panel
453 WebInspector.AuditsSidebarTreeElement = function(panel)
457 WebInspector.SidebarTreeElement.call(this, "audits-sidebar-tree-item", WebInspector.UIString("Audits"), "", null, false);
460 WebInspector.AuditsSidebarTreeElement.prototype = {
463 WebInspector.SidebarTreeElement.prototype.onattach.call(this);
468 this._panel.showLauncherView();
478 this.refreshTitles();
481 __proto__: WebInspector.SidebarTreeElement.prototype
486 * @extends {WebInspector.SidebarTreeElement}
487 * @param {!WebInspector.AuditsPanel} panel
488 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
489 * @param {string} mainResourceURL
490 * @param {number} ordinal
492 WebInspector.AuditResultSidebarTreeElement = function(panel, results, mainResourceURL, ordinal)
495 this.results = results;
496 this.mainResourceURL = mainResourceURL;
497 WebInspector.SidebarTreeElement.call(this, "audit-result-sidebar-tree-item", String.sprintf("%s (%d)", mainResourceURL, ordinal), "", {}, false);
500 WebInspector.AuditResultSidebarTreeElement.prototype = {
503 this._panel.showResults(this.results);
511 __proto__: WebInspector.SidebarTreeElement.prototype
514 // Contributed audit rules should go into this namespace.
515 WebInspector.AuditRules = {};
518 * Contributed audit categories should go into this namespace.
519 * @type {!Object.<string, function(new:WebInspector.AuditCategory)>}
521 WebInspector.AuditCategories = {};