OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved. | 3 * Copyright (C) 2013 Samsung Electronics. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 10 matching lines...) Expand all Loading... |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 30 */ |
| 31 /** |
| 32 * @unrestricted |
| 33 */ |
| 34 WebInspector.AuditController = class { |
| 35 /** |
| 36 * @param {!WebInspector.AuditsPanel} auditsPanel |
| 37 */ |
| 38 constructor(auditsPanel) { |
| 39 this._auditsPanel = auditsPanel; |
| 40 WebInspector.targetManager.addEventListener( |
| 41 WebInspector.TargetManager.Events.Load, this._didMainResourceLoad, this)
; |
| 42 WebInspector.targetManager.addModelListener( |
| 43 WebInspector.NetworkManager, WebInspector.NetworkManager.Events.RequestF
inished, this._didLoadResource, this); |
| 44 } |
31 | 45 |
32 /** | 46 /** |
33 * @constructor | 47 * @param {!WebInspector.Target} target |
34 * @param {!WebInspector.AuditsPanel} auditsPanel | 48 * @param {!Array.<!WebInspector.AuditCategory>} categories |
35 */ | 49 * @param {function(string, !Array.<!WebInspector.AuditCategoryResult>)} resul
tCallback |
36 WebInspector.AuditController = function(auditsPanel) | 50 */ |
37 { | 51 _executeAudit(target, categories, resultCallback) { |
38 this._auditsPanel = auditsPanel; | 52 this._progress.setTitle(WebInspector.UIString('Running audit')); |
39 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event
s.Load, this._didMainResourceLoad, this); | |
40 WebInspector.targetManager.addModelListener(WebInspector.NetworkManager, Web
Inspector.NetworkManager.Events.RequestFinished, this._didLoadResource, this); | |
41 }; | |
42 | |
43 WebInspector.AuditController.prototype = { | |
44 /** | |
45 * @param {!WebInspector.Target} target | |
46 * @param {!Array.<!WebInspector.AuditCategory>} categories | |
47 * @param {function(string, !Array.<!WebInspector.AuditCategoryResult>)} res
ultCallback | |
48 */ | |
49 _executeAudit: function(target, categories, resultCallback) | |
50 { | |
51 this._progress.setTitle(WebInspector.UIString("Running audit")); | |
52 | |
53 /** | |
54 * @param {!WebInspector.AuditCategoryResult} categoryResult | |
55 * @param {!WebInspector.AuditRuleResult} ruleResult | |
56 */ | |
57 function ruleResultReadyCallback(categoryResult, ruleResult) | |
58 { | |
59 if (ruleResult && ruleResult.children) | |
60 categoryResult.addRuleResult(ruleResult); | |
61 } | |
62 | |
63 var results = []; | |
64 var mainResourceURL = target.inspectedURL(); | |
65 var categoriesDone = 0; | |
66 | |
67 function categoryDoneCallback() | |
68 { | |
69 if (++categoriesDone !== categories.length) | |
70 return; | |
71 resultCallback(mainResourceURL, results); | |
72 } | |
73 | |
74 var networkLog = WebInspector.NetworkLog.fromTarget(target); | |
75 var requests = networkLog ? networkLog.requests().slice() : []; | |
76 var compositeProgress = new WebInspector.CompositeProgress(this._progres
s); | |
77 var subprogresses = []; | |
78 for (var i = 0; i < categories.length; ++i) | |
79 subprogresses.push(new WebInspector.ProgressProxy(compositeProgress.
createSubProgress(), categoryDoneCallback)); | |
80 for (var i = 0; i < categories.length; ++i) { | |
81 if (this._progress.isCanceled()) { | |
82 subprogresses[i].done(); | |
83 continue; | |
84 } | |
85 var category = categories[i]; | |
86 var result = new WebInspector.AuditCategoryResult(category); | |
87 results.push(result); | |
88 category.run(target, requests, ruleResultReadyCallback.bind(null, re
sult), subprogresses[i]); | |
89 } | |
90 }, | |
91 | 53 |
92 /** | 54 /** |
93 * @param {string} mainResourceURL | 55 * @param {!WebInspector.AuditCategoryResult} categoryResult |
94 * @param {!Array.<!WebInspector.AuditCategoryResult>} results | 56 * @param {!WebInspector.AuditRuleResult} ruleResult |
95 */ | 57 */ |
96 _auditFinishedCallback: function(mainResourceURL, results) | 58 function ruleResultReadyCallback(categoryResult, ruleResult) { |
97 { | 59 if (ruleResult && ruleResult.children) |
98 if (!this._progress.isCanceled()) | 60 categoryResult.addRuleResult(ruleResult); |
99 this._auditsPanel.auditFinishedCallback(mainResourceURL, results); | 61 } |
100 this._progress.done(); | |
101 }, | |
102 | 62 |
103 /** | 63 var results = []; |
104 * @param {!Array.<string>} categoryIds | 64 var mainResourceURL = target.inspectedURL(); |
105 * @param {!WebInspector.Progress} progress | 65 var categoriesDone = 0; |
106 * @param {boolean} runImmediately | |
107 * @param {function()} startedCallback | |
108 */ | |
109 initiateAudit: function(categoryIds, progress, runImmediately, startedCallba
ck) | |
110 { | |
111 var target = WebInspector.targetManager.mainTarget(); | |
112 if (!categoryIds || !categoryIds.length || !target) | |
113 return; | |
114 | 66 |
115 this._progress = progress; | 67 function categoryDoneCallback() { |
| 68 if (++categoriesDone !== categories.length) |
| 69 return; |
| 70 resultCallback(mainResourceURL, results); |
| 71 } |
116 | 72 |
117 var categories = []; | 73 var networkLog = WebInspector.NetworkLog.fromTarget(target); |
118 for (var i = 0; i < categoryIds.length; ++i) | 74 var requests = networkLog ? networkLog.requests().slice() : []; |
119 categories.push(this._auditsPanel.categoriesById[categoryIds[i]]); | 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.create
SubProgress(), categoryDoneCallback)); |
| 79 for (var i = 0; i < categories.length; ++i) { |
| 80 if (this._progress.isCanceled()) { |
| 81 subprogresses[i].done(); |
| 82 continue; |
| 83 } |
| 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]); |
| 88 } |
| 89 } |
120 | 90 |
121 if (runImmediately) | 91 /** |
122 this._startAuditWhenResourcesReady(target, categories, startedCallba
ck); | 92 * @param {string} mainResourceURL |
123 else | 93 * @param {!Array.<!WebInspector.AuditCategoryResult>} results |
124 this._reloadResources(this._startAuditWhenResourcesReady.bind(this,
target, categories, startedCallback)); | 94 */ |
| 95 _auditFinishedCallback(mainResourceURL, results) { |
| 96 if (!this._progress.isCanceled()) |
| 97 this._auditsPanel.auditFinishedCallback(mainResourceURL, results); |
| 98 this._progress.done(); |
| 99 } |
125 | 100 |
126 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.Aud
itsStarted); | 101 /** |
127 }, | 102 * @param {!Array.<string>} categoryIds |
| 103 * @param {!WebInspector.Progress} progress |
| 104 * @param {boolean} runImmediately |
| 105 * @param {function()} startedCallback |
| 106 */ |
| 107 initiateAudit(categoryIds, progress, runImmediately, startedCallback) { |
| 108 var target = WebInspector.targetManager.mainTarget(); |
| 109 if (!categoryIds || !categoryIds.length || !target) |
| 110 return; |
128 | 111 |
129 /** | 112 this._progress = progress; |
130 * @param {!WebInspector.Target} target | |
131 * @param {!Array<!WebInspector.AuditCategory>} categories | |
132 * @param {function()} startedCallback | |
133 */ | |
134 _startAuditWhenResourcesReady: function(target, categories, startedCallback) | |
135 { | |
136 if (this._progress.isCanceled()) { | |
137 this._progress.done(); | |
138 return; | |
139 } | |
140 startedCallback(); | |
141 this._executeAudit(target, categories, this._auditFinishedCallback.bind(
this)); | |
142 }, | |
143 | 113 |
144 /** | 114 var categories = []; |
145 * @param {function()=} callback | 115 for (var i = 0; i < categoryIds.length; ++i) |
146 */ | 116 categories.push(this._auditsPanel.categoriesById[categoryIds[i]]); |
147 _reloadResources: function(callback) | |
148 { | |
149 this._pageReloadCallback = callback; | |
150 WebInspector.targetManager.reloadPage(); | |
151 }, | |
152 | 117 |
153 _didLoadResource: function() | 118 if (runImmediately) |
154 { | 119 this._startAuditWhenResourcesReady(target, categories, startedCallback); |
155 if (this._pageReloadCallback && this._progress && this._progress.isCance
led()) | 120 else |
156 this._pageReloadCallback(); | 121 this._reloadResources(this._startAuditWhenResourcesReady.bind(this, target
, categories, startedCallback)); |
157 }, | |
158 | 122 |
159 _didMainResourceLoad: function() | 123 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.AuditsS
tarted); |
160 { | 124 } |
161 if (this._pageReloadCallback) { | |
162 var callback = this._pageReloadCallback; | |
163 delete this._pageReloadCallback; | |
164 callback(); | |
165 } | |
166 }, | |
167 | 125 |
168 clearResults: function() | 126 /** |
169 { | 127 * @param {!WebInspector.Target} target |
170 this._auditsPanel.clearResults(); | 128 * @param {!Array<!WebInspector.AuditCategory>} categories |
| 129 * @param {function()} startedCallback |
| 130 */ |
| 131 _startAuditWhenResourcesReady(target, categories, startedCallback) { |
| 132 if (this._progress.isCanceled()) { |
| 133 this._progress.done(); |
| 134 return; |
171 } | 135 } |
| 136 startedCallback(); |
| 137 this._executeAudit(target, categories, this._auditFinishedCallback.bind(this
)); |
| 138 } |
| 139 |
| 140 /** |
| 141 * @param {function()=} callback |
| 142 */ |
| 143 _reloadResources(callback) { |
| 144 this._pageReloadCallback = callback; |
| 145 WebInspector.targetManager.reloadPage(); |
| 146 } |
| 147 |
| 148 _didLoadResource() { |
| 149 if (this._pageReloadCallback && this._progress && this._progress.isCanceled(
)) |
| 150 this._pageReloadCallback(); |
| 151 } |
| 152 |
| 153 _didMainResourceLoad() { |
| 154 if (this._pageReloadCallback) { |
| 155 var callback = this._pageReloadCallback; |
| 156 delete this._pageReloadCallback; |
| 157 callback(); |
| 158 } |
| 159 } |
| 160 |
| 161 clearResults() { |
| 162 this._auditsPanel.clearResults(); |
| 163 } |
172 }; | 164 }; |
OLD | NEW |