Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(fr): extract storage and service worker driver methods #12400

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 0 additions & 236 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const LHElement = require('../lib/lh-element.js');
const LHError = require('../lib/lh-error.js');
const NetworkRequest = require('../lib/network-request.js');
const EventEmitter = require('events').EventEmitter;
const i18n = require('../lib/i18n/i18n.js');
const URL = require('../lib/url-shim.js');
const constants = require('../config/constants.js');

const log = require('lighthouse-logger');
Expand All @@ -28,24 +26,6 @@ const Connection = require('./connections/connection.js');
const NetworkMonitor = require('./driver/network-monitor.js');
const {getBrowserVersion} = require('./driver/environment.js');

const UIStrings = {
/**
* @description A warning that previously-saved data may have affected the measured performance and instructions on how to avoid the problem. "locations" will be a list of possible types of data storage locations, e.g. "IndexedDB", "Local Storage", or "Web SQL".
* @example {IndexedDB, Local Storage} locations
*/
warningData: `{locationCount, plural,
=1 {There may be stored data affecting loading performance in this location: {locations}. ` +
`Audit this page in an incognito window to prevent those resources ` +
`from affecting your scores.}
other {There may be stored data affecting loading ` +
`performance in these locations: {locations}. ` +
`Audit this page in an incognito window to prevent those resources ` +
`from affecting your scores.}
}`,
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

// Controls how long to wait after FCP before continuing
const DEFAULT_PAUSE_AFTER_FCP = 0;
// Controls how long to wait after onLoad before continuing
Expand Down Expand Up @@ -110,7 +90,6 @@ class Driver {
this.on('Target.attachedToTarget', event => {
this._handleTargetAttached(event).catch(this._handleEventError);
});
this.on('Debugger.paused', () => this.sendCommand('Debugger.resume'));

connection.on('protocolevent', this._handleProtocolEvent.bind(this));

Expand Down Expand Up @@ -397,93 +376,6 @@ class Driver {
return !!this._domainEnabledCounts.get(domain);
}

/**
* @return {Promise<LH.Crdp.ServiceWorker.WorkerVersionUpdatedEvent>}
*/
getServiceWorkerVersions() {
return new Promise((resolve, reject) => {
/**
* @param {LH.Crdp.ServiceWorker.WorkerVersionUpdatedEvent} data
*/
const versionUpdatedListener = data => {
// find a service worker with runningStatus that looks like active
// on slow connections the serviceworker might still be installing
const activateCandidates = data.versions.filter(sw => {
return sw.status !== 'redundant';
});
const hasActiveServiceWorker = activateCandidates.find(sw => {
return sw.status === 'activated';
});

if (!activateCandidates.length || hasActiveServiceWorker) {
this.off('ServiceWorker.workerVersionUpdated', versionUpdatedListener);
this.sendCommand('ServiceWorker.disable')
.then(_ => resolve(data), reject);
}
};

this.on('ServiceWorker.workerVersionUpdated', versionUpdatedListener);

this.sendCommand('ServiceWorker.enable').catch(reject);
});
}

/**
* @return {Promise<LH.Crdp.ServiceWorker.WorkerRegistrationUpdatedEvent>}
*/
getServiceWorkerRegistrations() {
return new Promise((resolve, reject) => {
this.once('ServiceWorker.workerRegistrationUpdated', data => {
this.sendCommand('ServiceWorker.disable')
.then(_ => resolve(data), reject);
});

this.sendCommand('ServiceWorker.enable').catch(reject);
});
}

/**
* Rejects if any open tabs would share a service worker with the target URL.
* This includes the target tab, so navigation to something like about:blank
* should be done before calling.
* @param {string} pageUrl
* @return {Promise<void>}
*/
assertNoSameOriginServiceWorkerClients(pageUrl) {
/** @type {Array<LH.Crdp.ServiceWorker.ServiceWorkerRegistration>} */
let registrations;
/** @type {Array<LH.Crdp.ServiceWorker.ServiceWorkerVersion>} */
let versions;

return this.getServiceWorkerRegistrations().then(data => {
registrations = data.registrations;
}).then(_ => this.getServiceWorkerVersions()).then(data => {
versions = data.versions;
}).then(_ => {
const origin = new URL(pageUrl).origin;

registrations
.filter(reg => {
const swOrigin = new URL(reg.scopeURL).origin;

return origin === swOrigin;
})
.forEach(reg => {
versions.forEach(ver => {
// Ignore workers unaffiliated with this registration
if (ver.registrationId !== reg.registrationId) {
return;
}

// Throw if service worker for this origin has active controlledClients.
if (ver.controlledClients && ver.controlledClients.length > 0) {
throw new Error('You probably have multiple tabs open to the same origin.');
}
});
});
});
}

/**
* Navigate to the given URL. Direct use of this method isn't advised: if
* the current page is already at the given URL, navigation will not occur and
Expand Down Expand Up @@ -731,119 +623,6 @@ class Driver {
return this._devtoolsLog.messages;
}

/**
* @return {Promise<void>}
*/
enableRuntimeEvents() {
return this.sendCommand('Runtime.enable');
}

/**
* Enables `Debugger` domain to receive async stacktrace information on network request initiators.
* This is critical for tracing certain performance simulation situations.
*
* @return {Promise<void>}
*/
async enableAsyncStacks() {
await this.sendCommand('Debugger.enable');
await this.sendCommand('Debugger.setSkipAllPauses', {skip: true});
await this.sendCommand('Debugger.setAsyncCallStackDepth', {maxDepth: 8});
}

/**
* Clear the network cache on disk and in memory.
* @return {Promise<void>}
*/
async cleanBrowserCaches() {
const status = {msg: 'Cleaning browser cache', id: 'lh:driver:cleanBrowserCaches'};
log.time(status);

// Wipe entire disk cache
await this.sendCommand('Network.clearBrowserCache');
// Toggle 'Disable Cache' to evict the memory cache
await this.sendCommand('Network.setCacheDisabled', {cacheDisabled: true});
await this.sendCommand('Network.setCacheDisabled', {cacheDisabled: false});

log.timeEnd(status);
}

/**
* @param {LH.Crdp.Network.Headers|null} headers key/value pairs of HTTP Headers.
* @return {Promise<void>}
*/
async setExtraHTTPHeaders(headers) {
if (!headers) {
return;
}

return this.sendCommand('Network.setExtraHTTPHeaders', {headers});
}

/**
* @param {string} url
* @return {Promise<void>}
*/
async clearDataForOrigin(url) {
const origin = new URL(url).origin;

// Clear some types of storage.
// Cookies are not cleared, so the user isn't logged out.
// indexeddb, websql, and localstorage are not cleared to prevent loss of potentially important data.
// https://chromedevtools.github.io/debugger-protocol-viewer/tot/Storage/#type-StorageType
const typesToClear = [
'appcache',
// 'cookies',
'file_systems',
'shader_cache',
'service_workers',
'cache_storage',
].join(',');

// `Storage.clearDataForOrigin` is one of our PROTOCOL_TIMEOUT culprits and this command is also
// run in the context of PAGE_HUNG to cleanup. We'll keep the timeout low and just warn if it fails.
this.setNextProtocolTimeout(5000);

try {
await this.sendCommand('Storage.clearDataForOrigin', {
origin: origin,
storageTypes: typesToClear,
});
} catch (err) {
if (/** @type {LH.LighthouseError} */(err).code === 'PROTOCOL_TIMEOUT') {
log.warn('Driver', 'clearDataForOrigin timed out');
} else {
throw err;
}
}
}

/**
* @param {string} url
* @return {Promise<LH.IcuMessage | undefined>}
*/
async getImportantStorageWarning(url) {
const usageData = await this.sendCommand('Storage.getUsageAndQuota', {
origin: url,
});
/** @type {Record<string, string>} */
const storageTypeNames = {
local_storage: 'Local Storage',
indexeddb: 'IndexedDB',
websql: 'Web SQL',
};
const locations = usageData.usageBreakdown
.filter(usage => usage.usage)
.map(usage => storageTypeNames[usage.storageType] || '')
.filter(Boolean);
if (locations.length) {
// TODO(#11495): Use Intl.ListFormat with Node 12
return str_(
UIStrings.warningData,
{locations: locations.join(', '), locationCount: locations.length}
);
}
}

/**
* Use a RequestIdleCallback shim for tests run with simulated throttling, so that the deadline can be used without
* a penalty
Expand All @@ -859,20 +638,6 @@ class Driver {
}
}

/**
* @param {Array<string>} urls URL patterns to block. Wildcards ('*') are allowed.
* @return {Promise<void>}
*/
blockUrlPatterns(urls) {
return this.sendCommand('Network.setBlockedURLs', {urls})
.catch(err => {
// TODO(COMPAT): remove this handler once m59 hits stable
if (!/wasn't found/.test(err.message)) {
throw err;
}
});
}

/**
* Dismiss JavaScript dialogs (alert, confirm, prompt), providing a
* generic promptText in case the dialog is a prompt.
Expand All @@ -893,4 +658,3 @@ class Driver {
}

module.exports = Driver;
module.exports.UIStrings = UIStrings;
4 changes: 4 additions & 0 deletions lighthouse-core/gather/driver/execution-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ExecutionContext {

// We use isolated execution contexts for `evaluateAsync` that can be destroyed through navigation
// and other page actions. Cleanup our relevant bookkeeping as we see those events.
// Domains are enabled when a dedicated execution context is requested.
session.on('Page.frameNavigated', () => this.clearContextId());
session.on('Runtime.executionContextDestroyed', event => {
if (event.executionContextId === this._executionContextId) {
Expand Down Expand Up @@ -51,6 +52,9 @@ class ExecutionContext {
async _getOrCreateIsolatedContextId() {
if (typeof this._executionContextId === 'number') return this._executionContextId;

await this._session.sendCommand('Page.enable');
await this._session.sendCommand('Runtime.enable');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this dependency was an implicit global relationship, felt better to make it explicit.


const resourceTreeResponse = await this._session.sendCommand('Page.getResourceTree');
const mainFrameId = resourceTreeResponse.frameTree.frame.id;

Expand Down
53 changes: 53 additions & 0 deletions lighthouse-core/gather/driver/service-workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/**
* @param {LH.Gatherer.FRProtocolSession} session
* @return {Promise<LH.Crdp.ServiceWorker.WorkerVersionUpdatedEvent>}
*/
function getServiceWorkerVersions(session) {
return new Promise((resolve, reject) => {
/**
* @param {LH.Crdp.ServiceWorker.WorkerVersionUpdatedEvent} data
*/
const versionUpdatedListener = data => {
// find a service worker with runningStatus that looks like active
// on slow connections the serviceworker might still be installing
const activateCandidates = data.versions.filter(sw => {
return sw.status !== 'redundant';
});

const hasActiveServiceWorker = activateCandidates.find(sw => {
return sw.status === 'activated';
});

if (!activateCandidates.length || hasActiveServiceWorker) {
session.off('ServiceWorker.workerVersionUpdated', versionUpdatedListener);
session.sendCommand('ServiceWorker.disable').then(_ => resolve(data), reject);
}
};

session.on('ServiceWorker.workerVersionUpdated', versionUpdatedListener);

session.sendCommand('ServiceWorker.enable').catch(reject);
});
}

/**
* @param {LH.Gatherer.FRProtocolSession} session
* @return {Promise<LH.Crdp.ServiceWorker.WorkerRegistrationUpdatedEvent>}
*/
function getServiceWorkerRegistrations(session) {
return new Promise((resolve, reject) => {
session.once('ServiceWorker.workerRegistrationUpdated', data => {
session.sendCommand('ServiceWorker.disable').then(_ => resolve(data), reject);
});
session.sendCommand('ServiceWorker.enable').catch(reject);
});
}

module.exports = {getServiceWorkerVersions, getServiceWorkerRegistrations};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing should be changed here

Loading