Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @implements {SDK.SDKModelObserver<!SDK.ServiceWorkerManager>} |
| 7 | * @unrestricted |
| 8 | */ |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 9 | Audits.AuditController = class extends Common.Object { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 10 | constructor(protocolService) { |
| 11 | super(); |
| 12 | |
| 13 | protocolService.registerStatusCallback( |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 14 | message => this.dispatchEventToListeners(Audits.Events.AuditProgressChanged, {message})); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 15 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 16 | for (const preset of Audits.Presets) |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 17 | preset.setting.addChangeListener(this.recomputePageAuditability.bind(this)); |
| 18 | |
| 19 | SDK.targetManager.observeModels(SDK.ServiceWorkerManager, this); |
| 20 | SDK.targetManager.addEventListener( |
| 21 | SDK.TargetManager.Events.InspectedURLChanged, this.recomputePageAuditability, this); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @override |
| 26 | * @param {!SDK.ServiceWorkerManager} serviceWorkerManager |
| 27 | */ |
| 28 | modelAdded(serviceWorkerManager) { |
| 29 | if (this._manager) |
| 30 | return; |
| 31 | |
| 32 | this._manager = serviceWorkerManager; |
| 33 | this._serviceWorkerListeners = [ |
| 34 | this._manager.addEventListener( |
| 35 | SDK.ServiceWorkerManager.Events.RegistrationUpdated, this.recomputePageAuditability, this), |
| 36 | this._manager.addEventListener( |
| 37 | SDK.ServiceWorkerManager.Events.RegistrationDeleted, this.recomputePageAuditability, this), |
| 38 | ]; |
| 39 | |
| 40 | this.recomputePageAuditability(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @override |
| 45 | * @param {!SDK.ServiceWorkerManager} serviceWorkerManager |
| 46 | */ |
| 47 | modelRemoved(serviceWorkerManager) { |
| 48 | if (this._manager !== serviceWorkerManager) |
| 49 | return; |
| 50 | |
| 51 | Common.EventTarget.removeEventListeners(this._serviceWorkerListeners); |
| 52 | this._manager = null; |
| 53 | this.recomputePageAuditability(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return {boolean} |
| 58 | */ |
| 59 | _hasActiveServiceWorker() { |
| 60 | if (!this._manager) |
| 61 | return false; |
| 62 | |
| 63 | const mainTarget = this._manager.target(); |
| 64 | if (!mainTarget) |
| 65 | return false; |
| 66 | |
| 67 | const inspectedURL = mainTarget.inspectedURL().asParsedURL(); |
| 68 | const inspectedOrigin = inspectedURL && inspectedURL.securityOrigin(); |
| 69 | for (const registration of this._manager.registrations().values()) { |
| 70 | if (registration.securityOrigin !== inspectedOrigin) |
| 71 | continue; |
| 72 | |
| 73 | for (const version of registration.versions.values()) { |
| 74 | if (version.controlledClients.length > 1) |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return {boolean} |
| 84 | */ |
| 85 | _hasAtLeastOneCategory() { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 86 | return Audits.Presets.some(preset => preset.setting.get()); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return {?string} |
| 91 | */ |
| 92 | _unauditablePageMessage() { |
| 93 | if (!this._manager) |
| 94 | return null; |
| 95 | |
| 96 | const mainTarget = this._manager.target(); |
| 97 | const inspectedURL = mainTarget && mainTarget.inspectedURL(); |
| 98 | if (inspectedURL && !/^(http|chrome-extension)/.test(inspectedURL)) { |
| 99 | return Common.UIString( |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 100 | 'Can only audit HTTP/HTTPS pages and Chrome extensions. Navigate to a different page to start an audit.'); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 103 | return null; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return {!Promise<string>} |
| 108 | */ |
| 109 | async _evaluateInspectedURL() { |
| 110 | const mainTarget = this._manager.target(); |
| 111 | const runtimeModel = mainTarget.model(SDK.RuntimeModel); |
| 112 | const executionContext = runtimeModel && runtimeModel.defaultExecutionContext(); |
| 113 | let inspectedURL = mainTarget.inspectedURL(); |
| 114 | if (!executionContext) |
| 115 | return inspectedURL; |
| 116 | |
| 117 | // Evaluate location.href for a more specific URL than inspectedURL provides so that SPA hash navigation routes |
| 118 | // will be respected and audited. |
| 119 | try { |
| 120 | const result = await executionContext.evaluate( |
| 121 | { |
| 122 | expression: 'window.location.href', |
| 123 | objectGroup: 'audits', |
| 124 | includeCommandLineAPI: false, |
| 125 | silent: false, |
| 126 | returnByValue: true, |
| 127 | generatePreview: false |
| 128 | }, |
| 129 | /* userGesture */ false, /* awaitPromise */ false); |
| 130 | if (!result.exceptionDetails && result.object) { |
| 131 | inspectedURL = result.object.value; |
| 132 | result.object.release(); |
| 133 | } |
| 134 | } catch (err) { |
| 135 | console.error(err); |
| 136 | } |
| 137 | |
| 138 | return inspectedURL; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return {!Object} |
| 143 | */ |
| 144 | getFlags() { |
Connor Clark | 3f70034 | 2019-07-25 02:10:41 +0000 | [diff] [blame^] | 145 | const flags = { |
| 146 | // DevTools handles all the emulation. This tells Lighthouse to not bother with emulation. |
| 147 | deviceScreenEmulationMethod: 'provided' |
| 148 | }; |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 149 | for (const runtimeSetting of Audits.RuntimeSettings) |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 150 | runtimeSetting.setFlags(flags, runtimeSetting.setting.get()); |
| 151 | return flags; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return {!Array<string>} |
| 156 | */ |
| 157 | getCategoryIDs() { |
| 158 | const categoryIDs = []; |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 159 | for (const preset of Audits.Presets) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 160 | if (preset.setting.get()) |
| 161 | categoryIDs.push(preset.configID); |
| 162 | } |
| 163 | return categoryIDs; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @param {{force: boolean}=} options |
| 168 | * @return {!Promise<string>} |
| 169 | */ |
| 170 | async getInspectedURL(options) { |
| 171 | if (options && options.force || !this._inspectedURL) |
| 172 | this._inspectedURL = await this._evaluateInspectedURL(); |
| 173 | return this._inspectedURL; |
| 174 | } |
| 175 | |
| 176 | recomputePageAuditability() { |
| 177 | const hasActiveServiceWorker = this._hasActiveServiceWorker(); |
| 178 | const hasAtLeastOneCategory = this._hasAtLeastOneCategory(); |
| 179 | const unauditablePageMessage = this._unauditablePageMessage(); |
| 180 | |
| 181 | let helpText = ''; |
| 182 | if (hasActiveServiceWorker) { |
| 183 | helpText = Common.UIString( |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 184 | 'Multiple tabs are being controlled by the same service worker. Close your other tabs on the same origin to audit this page.'); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 185 | } else if (!hasAtLeastOneCategory) { |
| 186 | helpText = Common.UIString('At least one category must be selected.'); |
| 187 | } else if (unauditablePageMessage) { |
| 188 | helpText = unauditablePageMessage; |
| 189 | } |
| 190 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 191 | this.dispatchEventToListeners(Audits.Events.PageAuditabilityChanged, {helpText}); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
| 195 | |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 196 | /** @typedef {{setting: !Common.Setting, configID: string, title: string, description: string}} */ |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 197 | Audits.Preset; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 198 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 199 | /** @type {!Array.<!Audits.Preset>} */ |
| 200 | Audits.Presets = [ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 201 | // configID maps to Lighthouse's Object.keys(config.categories)[0] value |
| 202 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 203 | setting: Common.settings.createSetting('audits.cat_perf', true), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 204 | configID: 'performance', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 205 | title: ls`Performance`, |
| 206 | description: ls`How long does this app take to show content and become usable` |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 207 | }, |
| 208 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 209 | setting: Common.settings.createSetting('audits.cat_pwa', true), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 210 | configID: 'pwa', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 211 | title: ls`Progressive Web App`, |
| 212 | description: ls`Does this page meet the standard of a Progressive Web App` |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 213 | }, |
| 214 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 215 | setting: Common.settings.createSetting('audits.cat_best_practices', true), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 216 | configID: 'best-practices', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 217 | title: ls`Best practices`, |
| 218 | description: ls`Does this page follow best practices for modern web development` |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 219 | }, |
| 220 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 221 | setting: Common.settings.createSetting('audits.cat_a11y', true), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 222 | configID: 'accessibility', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 223 | title: ls`Accessibility`, |
| 224 | description: ls`Is this page usable by people with disabilities or impairments` |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 225 | }, |
| 226 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 227 | setting: Common.settings.createSetting('audits.cat_seo', true), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 228 | configID: 'seo', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 229 | title: ls`SEO`, |
| 230 | description: ls`Is this page optimized for search engine results ranking` |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 231 | }, |
| 232 | ]; |
| 233 | |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 234 | /** @typedef {{setting: !Common.Setting, description: string, setFlags: function(!Object, string), options: (!Array|undefined), title: (string|undefined)}} */ |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 235 | Audits.RuntimeSetting; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 236 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 237 | /** @type {!Array.<!Audits.RuntimeSetting>} */ |
| 238 | Audits.RuntimeSettings = [ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 239 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 240 | setting: Common.settings.createSetting('audits.device_type', 'mobile'), |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 241 | description: ls`Apply mobile emulation during auditing`, |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 242 | setFlags: (flags, value) => { |
Paul Irish | d849501 | 2019-07-16 23:51:47 +0000 | [diff] [blame] | 243 | // See Audits.AuditsPanel._setupEmulationAndProtocolConnection() |
Connor Clark | 3f70034 | 2019-07-25 02:10:41 +0000 | [diff] [blame^] | 244 | flags.emulatedFormFactor = value; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 245 | }, |
| 246 | options: [ |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 247 | {label: ls`Mobile`, value: 'mobile'}, |
| 248 | {label: ls`Desktop`, value: 'desktop'}, |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 249 | ], |
| 250 | }, |
| 251 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 252 | setting: Common.settings.createSetting('audits.throttling', 'default'), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 253 | setFlags: (flags, value) => { |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 254 | switch (value) { |
| 255 | case 'devtools': |
| 256 | flags.throttlingMethod = 'devtools'; |
| 257 | break; |
| 258 | case 'off': |
| 259 | flags.throttlingMethod = 'provided'; |
| 260 | break; |
| 261 | default: |
| 262 | flags.throttlingMethod = 'simulate'; |
| 263 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 264 | }, |
| 265 | options: [ |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 266 | { |
cjamcl@google.com | bbf3f9b | 2019-04-19 19:25:18 +0000 | [diff] [blame] | 267 | label: ls`Simulated Slow 4G, 4x CPU Slowdown`, |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 268 | value: 'default', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 269 | title: ls`Throttling is simulated, resulting in faster audit runs with similar measurement accuracy` |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 270 | }, |
| 271 | { |
cjamcl@google.com | bbf3f9b | 2019-04-19 19:25:18 +0000 | [diff] [blame] | 272 | label: ls`Applied Slow 4G, 4x CPU Slowdown`, |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 273 | value: 'devtools', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 274 | title: ls`Typical DevTools throttling, with actual traffic shaping and CPU slowdown applied` |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 275 | }, |
| 276 | { |
| 277 | label: ls`No throttling`, |
| 278 | value: 'off', |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame] | 279 | title: ls`No network or CPU throttling used. (Useful when not evaluating performance)` |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 280 | }, |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 281 | ], |
| 282 | }, |
| 283 | { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 284 | setting: Common.settings.createSetting('audits.clear_storage', true), |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 285 | title: ls`Clear storage`, |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 286 | description: ls`Reset storage (localStorage, IndexedDB, etc) before auditing. (Good for performance & PWA testing)`, |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 287 | setFlags: (flags, value) => { |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 288 | flags.disableStorageReset = !value; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 289 | }, |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 290 | }, |
| 291 | ]; |
| 292 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 293 | Audits.Events = { |
Trent Apted | ba184a6 | 2018-05-25 02:13:48 +0000 | [diff] [blame] | 294 | PageAuditabilityChanged: Symbol('PageAuditabilityChanged'), |
| 295 | AuditProgressChanged: Symbol('AuditProgressChanged'), |
| 296 | RequestAuditStart: Symbol('RequestAuditStart'), |
| 297 | RequestAuditCancel: Symbol('RequestAuditCancel'), |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 298 | }; |