Skip to content

Commit

Permalink
Updated multiple tabs open logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Sep 10, 2016
1 parent 61d67ba commit 191a6c2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lighthouse-core/gather/drivers/cri.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class CriDriver extends Driver {
});
});
}

getCurrentTabId() {
return Promise.resolve(this._tab.id);
}
}

module.exports = CriDriver;
57 changes: 45 additions & 12 deletions lighthouse-core/gather/drivers/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class Driver {
return Promise.reject(new Error('Not implemented'));
}

/**
* Get current tab id
* @return {!Promise<string>}
*/
getCurrentTabId() {
return Promise.reject(new Error('Not implemented'));
}

evaluateScriptOnLoad(scriptSource) {
return this.sendCommand('Page.addScriptToEvaluateOnLoad', {
scriptSource
Expand Down Expand Up @@ -210,33 +218,58 @@ class Driver {
});
}

getServiceWorkerRegistrations() {
return new Promise(resolve => {
this.once('ServiceWorker.workerRegistrationUpdated', data => {
this.sendCommand('ServiceWorker.disable');
resolve(data);
});

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

/**
* Checks all serviceworkes and see if any duplications are running
* @param {string} pageUrl
* @return {Promise}
* @param {!string} pageUrl
* @return {!Promise}
*/
checkForMultipleTabsAttached(pageUrl) {
// Get necessary information of serviceWorkers
const getRegistrations = new Promise(resolve => {
this.once('ServiceWorker.workerRegistrationUpdated', resolve);
});
const getRegistrations = this.getServiceWorkerRegistrations();
const getVersions = this.getServiceWorkerVersions();
const getActiveTabId = this.getCurrentTabId();

return Promise.all([getRegistrations, getVersions]).then(res => {
return Promise.all([getRegistrations, getVersions, getActiveTabId]).then(res => {
const registrations = res[0].registrations;
const versions = res[1].versions;
const activePageId = res[2];
const parsedURL = parseURL(pageUrl);
const origin = `${parsedURL.protocol}//${parsedURL.hostname}` +
(parsedURL.port ? `:${parsedURL.port}` : '');

let activeServiceWorkers = [];
let swHasMoreThanOneClient = false;
registrations
.filter(reg => !reg.isDeleted)
.filter(reg => parseURL(reg.scopeURL).hostname === parseURL(pageUrl).hostname)
.filter(reg => {
const parsedURL = parseURL(reg.scopeURL);
const swOrigin = `${parsedURL.protocol}//${parsedURL.hostname}` +
(parsedURL.port ? `:${parsedURL.port}` : '');

return origin === swOrigin;
})
.forEach(reg => {
activeServiceWorkers = activeServiceWorkers.concat(versions
.filter(ver => ver.registrationId === reg.registrationId)
);
// Check if any of the service workers are the same (registration id)
// Check if the controlledClients are bigger than 1 and it's not the active tab
swHasMoreThanOneClient = !!versions.find(ver => {
return ver.registrationId === reg.registrationId &&
ver.controlledClients && (ver.controlledClients.length > 1 ||
(ver.controlledClients.length === 1 &&
!ver.controlledClients.find(sw => sw === activePageId)))
});
});

if (activeServiceWorkers.length > 1) {
if (swHasMoreThanOneClient) {
throw new Error(
'You probably have multiple tabs open.'
);
Expand Down
16 changes: 16 additions & 0 deletions lighthouse-core/gather/drivers/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ class ExtensionDriver extends Driver {
getCurrentTabURL() {
return this.queryCurrentTab_().then(tab => tab.url);
}

getCurrentTabId() {
return new Promise((resolve, reject) => {
this.queryCurrentTab_().then(currentTab => {
chrome.debugger.getTargets(targets => {
const tab = targets.find(tab => tab.tabId === currentTab.id);

if (!tab) {
reject(new Error(`We can't find a target id.`))
}

resolve(tab.id);
});
});
});
}
}

module.exports = ExtensionDriver;
4 changes: 0 additions & 4 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ class GatherRunner {
passes = this.instantiateGatherers(passes, options.config.configDir);

return driver.connect()
.then(_ => {
// Check Service Workers running
return driver.checkForMultipleTabsAttached(options.url);
})
.then(_ => GatherRunner.setupDriver(driver, options))

// Run each pass
Expand Down
57 changes: 45 additions & 12 deletions lighthouse-core/test/lib/drivers/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ describe('Browser Driver', () => {
it('will fail when multiple tabs are found with the same active serviceworker', () => {
const pageUrl = 'https://example.com/';
const swUrl = `${pageUrl}sw.js`;
const registrations = [1, 2].map(i =>
createSWRegistration(i, pageUrl));
const versions = [1, 2].map(i =>
createActiveWorker(i, swUrl));

const registrations = [
createSWRegistration(1, pageUrl),
];
const versions = [
createActiveWorker(1, swUrl, ['unique'])
];

driverStub.getCurrentTabId = () => Promise.resolve('unique2');
driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
Expand All @@ -105,14 +108,42 @@ describe('Browser Driver', () => {
.then(_ => assert.ok(false), _ => assert.ok(true));
});

it('will succeed when old sw are deleted', () => {
it('will succeed when service worker is already registered on current tab', () => {
const pageUrl = 'https://example.com/';
const swUrl = `${pageUrl}sw.js`;
const registrations = [1, 2].map(i =>
createSWRegistration(i, pageUrl, i === 1));
const versions = [1, 2].map(i =>
createActiveWorker(i, swUrl));
const registrations = [
createSWRegistration(1, pageUrl),
];
const versions = [
createActiveWorker(1, swUrl, ['unique'])
];

driverStub.getCurrentTabId = () => Promise.resolve('unique');
driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
},
'ServiceWorker.workerVersionUpdated': {
versions
}
});

return driverStub.checkForMultipleTabsAttached(pageUrl)
.then(_ => assert.ok(true), _ => assert.ok(false));
});

it('will succeed when old sw are deleted', () => {
const pageUrl = 'https://example.com/';
const swUrl = `${pageUrl}sw.js`;
const registrations = [
createSWRegistration(1, pageUrl, true),
createSWRegistration(1, pageUrl),
];
const versions = [
createActiveWorker(1, swUrl, [])
];

driverStub.getCurrentTabId = () => Promise.resolve('unique');
driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
Expand All @@ -130,8 +161,9 @@ describe('Browser Driver', () => {
const pageUrl = 'https://example.com/';
const swUrl = `${pageUrl}sw.js`;
const registrations = [createSWRegistration(1, pageUrl)];
const versions = [createSWRegistration(1, swUrl)];
const versions = [createActiveWorker(1, swUrl, [])];

driverStub.getCurrentTabId = () => Promise.resolve('unique');
driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
Expand Down Expand Up @@ -164,9 +196,10 @@ function createSWRegistration(id, url, isDeleted) {
};
}

function createActiveWorker(id, url) {
function createActiveWorker(id, url, controlledClients) {
return {
registrationId: id,
scriptURL: url,
controlledClients,
};
}

0 comments on commit 191a6c2

Please sign in to comment.