Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / audits / AuditController.js
blob7fe9983600f41526ac2b6ae7537c6030f01d244c
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 /**
33 * @constructor
34 * @param {!WebInspector.AuditsPanel} auditsPanel
36 WebInspector.AuditController = function(auditsPanel)
38 this._auditsPanel = auditsPanel;
39 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Events.Load, this._didMainResourceLoad, this);
40 WebInspector.targetManager.addModelListener(WebInspector.NetworkManager, WebInspector.NetworkManager.EventTypes.RequestFinished, this._didLoadResource, this);
43 WebInspector.AuditController.prototype = {
44 /**
45 * @param {!WebInspector.Target} target
46 * @param {!Array.<!WebInspector.AuditCategory>} categories
47 * @param {function(string, !Array.<!WebInspector.AuditCategoryResult>)} resultCallback
49 _executeAudit: function(target, categories, resultCallback)
51 this._progress.setTitle(WebInspector.UIString("Running audit"));
53 /**
54 * @param {!WebInspector.AuditCategoryResult} categoryResult
55 * @param {!WebInspector.AuditRuleResult} ruleResult
57 function ruleResultReadyCallback(categoryResult, ruleResult)
59 if (ruleResult && ruleResult.children)
60 categoryResult.addRuleResult(ruleResult);
63 var results = [];
64 var mainResourceURL = target.resourceTreeModel.inspectedPageURL();
65 var categoriesDone = 0;
67 function categoryDoneCallback()
69 if (++categoriesDone !== categories.length)
70 return;
71 resultCallback(mainResourceURL, results);
74 var requests = target.networkLog.requests().slice();
75 var compositeProgress = new WebInspector.CompositeProgress(this._progress);
76 var subprogresses = [];
77 for (var i = 0; i < categories.length; ++i)
78 subprogresses.push(new WebInspector.ProgressProxy(compositeProgress.createSubProgress(), categoryDoneCallback));
79 for (var i = 0; i < categories.length; ++i) {
80 if (this._progress.isCanceled()) {
81 subprogresses[i].done();
82 continue;
84 var category = categories[i];
85 var result = new WebInspector.AuditCategoryResult(category);
86 results.push(result);
87 category.run(target, requests, ruleResultReadyCallback.bind(null, result), subprogresses[i]);
91 /**
92 * @param {string} mainResourceURL
93 * @param {!Array.<!WebInspector.AuditCategoryResult>} results
95 _auditFinishedCallback: function(mainResourceURL, results)
97 if (!this._progress.isCanceled())
98 this._auditsPanel.auditFinishedCallback(mainResourceURL, results);
99 this._progress.done();
103 * @param {!Array.<string>} categoryIds
104 * @param {!WebInspector.Progress} progress
105 * @param {boolean} runImmediately
106 * @param {function()} startedCallback
108 initiateAudit: function(categoryIds, progress, runImmediately, startedCallback)
110 var target = /** @type {!WebInspector.Target} */ (WebInspector.targetManager.mainTarget());
111 if (!categoryIds || !categoryIds.length || !target)
112 return;
114 this._progress = progress;
116 var categories = [];
117 for (var i = 0; i < categoryIds.length; ++i)
118 categories.push(this._auditsPanel.categoriesById[categoryIds[i]]);
121 * @this {WebInspector.AuditController}
123 function startAuditWhenResourcesReady()
125 if (this._progress.isCanceled()) {
126 this._progress.done();
127 return;
129 startedCallback();
130 this._executeAudit(target, categories, this._auditFinishedCallback.bind(this));
133 if (runImmediately)
134 startAuditWhenResourcesReady.call(this);
135 else
136 this._reloadResources(startAuditWhenResourcesReady.bind(this));
138 WebInspector.userMetrics.AuditsStarted.record();
142 * @param {function()=} callback
144 _reloadResources: function(callback)
146 this._pageReloadCallback = callback;
147 WebInspector.targetManager.reloadPage();
150 _didLoadResource: function()
152 if (this._pageReloadCallback && this._progress && this._progress.isCanceled())
153 this._pageReloadCallback();
156 _didMainResourceLoad: function()
158 if (this._pageReloadCallback) {
159 var callback = this._pageReloadCallback;
160 delete this._pageReloadCallback;
161 callback();
165 clearResults: function()
167 this._auditsPanel.clearResults();