From e1e4b98a6b91e166ce7fbbfcd7f555f9b1acec79 Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Wed, 18 Jan 2017 11:36:28 -0800 Subject: [PATCH 1/5] CRX: disable extensions before running lighthouse --- lighthouse-extension/app/manifest.json | 1 + .../app/src/lighthouse-background.js | 73 +++++++++++++++---- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/lighthouse-extension/app/manifest.json b/lighthouse-extension/app/manifest.json index 3d0a0e680a1a..83c213c4b8e6 100644 --- a/lighthouse-extension/app/manifest.json +++ b/lighthouse-extension/app/manifest.json @@ -19,6 +19,7 @@ "permissions": [ "activeTab", "debugger", + "management", "storage" ], "browser_action": { diff --git a/lighthouse-extension/app/src/lighthouse-background.js b/lighthouse-extension/app/src/lighthouse-background.js index d747d23cb44a..5ee1c2605b43 100644 --- a/lighthouse-extension/app/src/lighthouse-background.js +++ b/lighthouse-extension/app/src/lighthouse-background.js @@ -26,11 +26,34 @@ const log = require('../../../lighthouse-core/lib/log'); const ReportGenerator = require('../../../lighthouse-core/report/report-generator'); const STORAGE_KEY = 'lighthouse_audits'; -const _flatten = arr => [].concat(...arr); +let installedExtensions = []; let lighthouseIsRunning = false; let latestStatusLog = []; +const _flatten = arr => [].concat(...arr); + +/** + * Enables or disables all other installed chrome extensions. The initial list + * of the user's extension is created when the background page is started. + * @param {!boolean} enable If true, enables all other installed extensions. + * False disables them. + * @param {!Promise} + */ +function enableOtherChromeExtensions(enable) { + return new Promise(resolve => { + let remaining = installedExtensions.length; + installedExtensions.forEach(info => { + chrome.management.setEnabled(info.id, enable, _ => { + remaining -= 1; + if (!remaining) { + resolve(); + } + }); + }); + }); +} + /** * Filter out any unrequested aggregations from the config. If any audits are * no longer needed by any remaining aggregations, filter out those as well. @@ -124,19 +147,30 @@ window.runLighthouseForConnection = function(connection, url, options, requested lighthouseIsRunning = true; updateBadgeUI(url); - // Run Lighthouse. - return Runner.run(connection, runOptions) - .then(result => { - lighthouseIsRunning = false; - updateBadgeUI(); - filterOutArtifacts(result); - return result; - }) - .catch(err => { - lighthouseIsRunning = false; - updateBadgeUI(); - throw err; - }); + return enableOtherChromeExtensions(false) + .then(_ => Runner.run(connection, runOptions)) // Run Lighthouse. + .then(result => { + lighthouseIsRunning = false; + updateBadgeUI(); + filterOutArtifacts(result); + + // Deliver results even if enabling extensions fails. + enableOtherChromeExtensions(true).catch(err => { + log.warn('Chrome', `Could not disable some extensions. ${err.message}`); + }); + + return result; + }) + .catch(err => { + lighthouseIsRunning = false; + updateBadgeUI(); + + enableOtherChromeExtensions(true).catch(err => { + log.warn('Chrome', `Could not enable/disable some extensions. ${err.message}`); + }); + + throw err; + }); }; /** @@ -277,6 +311,17 @@ window.isRunning = function() { return lighthouseIsRunning; }; +// Get list of installed extensions that are enabled and can be disabled. +// Extensions are not allowed to be disabled if they are under an admin policy. +chrome.management.getAll(installs => { + chrome.management.getSelf(lighthouseCrxInfo => { + installedExtensions = installs.filter(info => { + return info.id !== lighthouseCrxInfo.id && info.type === 'extension' && + info.enabled && info.mayDisable; + }); + }); +}); + if (window.chrome && chrome.runtime) { chrome.runtime.onInstalled.addListener(details => { if (details.previousVersion) { From 54088a15fe854718f6fd9fa3ca207a451a815c67 Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Fri, 20 Jan 2017 15:20:03 -0800 Subject: [PATCH 2/5] feedback --- .../app/src/lighthouse-background.js | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/lighthouse-extension/app/src/lighthouse-background.js b/lighthouse-extension/app/src/lighthouse-background.js index 5ee1c2605b43..27cfe00361cd 100644 --- a/lighthouse-extension/app/src/lighthouse-background.js +++ b/lighthouse-extension/app/src/lighthouse-background.js @@ -41,6 +41,9 @@ const _flatten = arr => [].concat(...arr); * @param {!Promise} */ function enableOtherChromeExtensions(enable) { + const str = enable ? 'enabling' : 'disabling'; + log.warn('Chrome', `${str} ${installedExtensions.length} extensions.`); + return new Promise(resolve => { let remaining = installedExtensions.length; installedExtensions.forEach(info => { @@ -147,28 +150,16 @@ window.runLighthouseForConnection = function(connection, url, options, requested lighthouseIsRunning = true; updateBadgeUI(url); - return enableOtherChromeExtensions(false) - .then(_ => Runner.run(connection, runOptions)) // Run Lighthouse. + return Runner.run(connection, runOptions) // Run Lighthouse. .then(result => { lighthouseIsRunning = false; updateBadgeUI(); filterOutArtifacts(result); - - // Deliver results even if enabling extensions fails. - enableOtherChromeExtensions(true).catch(err => { - log.warn('Chrome', `Could not disable some extensions. ${err.message}`); - }); - return result; }) .catch(err => { lighthouseIsRunning = false; updateBadgeUI(); - - enableOtherChromeExtensions(true).catch(err => { - log.warn('Chrome', `Could not enable/disable some extensions. ${err.message}`); - }); - throw err; }); }; @@ -182,11 +173,22 @@ window.runLighthouseInExtension = function(options, requestedAggregations) { // Default to 'info' logging level. log.setLevel('info'); const connection = new ExtensionProtocol(); - return connection.getCurrentTabURL() + return enableOtherChromeExtensions(false) + .then(_ => connection.getCurrentTabURL()) .then(url => window.runLighthouseForConnection(connection, url, options, requestedAggregations)) .then(results => { + // Deliver results even if enabling extensions fails. + enableOtherChromeExtensions(true).catch(err => { + log.warn('Chrome', `Could not enable some extensions. ${err.message}`); + }); + const blobURL = window.createReportPageAsBlob(results, 'extension'); chrome.tabs.create({url: blobURL}); + }).catch(err => { + enableOtherChromeExtensions(true).catch(err => { + log.warn('Chrome', `Could not enable/disable some extensions. ${err.message}`); + }); + throw err; }); }; @@ -311,18 +313,19 @@ window.isRunning = function() { return lighthouseIsRunning; }; -// Get list of installed extensions that are enabled and can be disabled. -// Extensions are not allowed to be disabled if they are under an admin policy. -chrome.management.getAll(installs => { - chrome.management.getSelf(lighthouseCrxInfo => { - installedExtensions = installs.filter(info => { - return info.id !== lighthouseCrxInfo.id && info.type === 'extension' && - info.enabled && info.mayDisable; +// Run when in extension context, but not in devtools. +if (window.chrome && chrome.runtime) { + // Get list of installed extensions that are enabled and can be disabled. + // Extensions are not allowed to be disabled if they are under an admin policy. + chrome.management.getAll(installs => { + chrome.management.getSelf(lighthouseCrxInfo => { + installedExtensions = installs.filter(info => { + return info.id !== lighthouseCrxInfo.id && info.type === 'extension' && + info.enabled && info.mayDisable; + }); }); }); -}); -if (window.chrome && chrome.runtime) { chrome.runtime.onInstalled.addListener(details => { if (details.previousVersion) { // eslint-disable-next-line no-console From a4052dfe70ddda405b5aba798de089a4b73ed3ab Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Mon, 23 Jan 2017 12:06:11 -0800 Subject: [PATCH 3/5] use promise --- .../app/src/lighthouse-background.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lighthouse-extension/app/src/lighthouse-background.js b/lighthouse-extension/app/src/lighthouse-background.js index 27cfe00361cd..799f75b6dcb1 100644 --- a/lighthouse-extension/app/src/lighthouse-background.js +++ b/lighthouse-extension/app/src/lighthouse-background.js @@ -44,17 +44,11 @@ function enableOtherChromeExtensions(enable) { const str = enable ? 'enabling' : 'disabling'; log.warn('Chrome', `${str} ${installedExtensions.length} extensions.`); - return new Promise(resolve => { - let remaining = installedExtensions.length; - installedExtensions.forEach(info => { - chrome.management.setEnabled(info.id, enable, _ => { - remaining -= 1; - if (!remaining) { - resolve(); - } - }); + return Promise.all(installedExtensions.map(info => { + return new Promise(resolve => { + chrome.management.setEnabled(info.id, enable, resolve); }); - }); + })); } /** From 701ecce5f5878f240f5f50687d4d35c40b98ccf2 Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Mon, 23 Jan 2017 21:06:26 -0800 Subject: [PATCH 4/5] Put enable/disable extension in normal promise flow --- .../app/src/lighthouse-background.js | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/lighthouse-extension/app/src/lighthouse-background.js b/lighthouse-extension/app/src/lighthouse-background.js index 799f75b6dcb1..dd1c66308965 100644 --- a/lighthouse-extension/app/src/lighthouse-background.js +++ b/lighthouse-extension/app/src/lighthouse-background.js @@ -42,11 +42,16 @@ const _flatten = arr => [].concat(...arr); */ function enableOtherChromeExtensions(enable) { const str = enable ? 'enabling' : 'disabling'; - log.warn('Chrome', `${str} ${installedExtensions.length} extensions.`); + log.log('Chrome', `${str} ${installedExtensions.length} extensions.`); return Promise.all(installedExtensions.map(info => { - return new Promise(resolve => { - chrome.management.setEnabled(info.id, enable, resolve); + return new Promise((resolve, reject) => { + chrome.management.setEnabled(info.id, enable, _ => { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError); + } + resolve(); + }); }); })); } @@ -145,17 +150,17 @@ window.runLighthouseForConnection = function(connection, url, options, requested updateBadgeUI(url); return Runner.run(connection, runOptions) // Run Lighthouse. - .then(result => { - lighthouseIsRunning = false; - updateBadgeUI(); - filterOutArtifacts(result); - return result; - }) - .catch(err => { - lighthouseIsRunning = false; - updateBadgeUI(); - throw err; - }); + .then(result => { + lighthouseIsRunning = false; + updateBadgeUI(); + filterOutArtifacts(result); + return result; + }) + .catch(err => { + lighthouseIsRunning = false; + updateBadgeUI(); + throw err; + }); }; /** @@ -171,17 +176,11 @@ window.runLighthouseInExtension = function(options, requestedAggregations) { .then(_ => connection.getCurrentTabURL()) .then(url => window.runLighthouseForConnection(connection, url, options, requestedAggregations)) .then(results => { - // Deliver results even if enabling extensions fails. - enableOtherChromeExtensions(true).catch(err => { - log.warn('Chrome', `Could not enable some extensions. ${err.message}`); - }); - + enableOtherChromeExtensions(true); const blobURL = window.createReportPageAsBlob(results, 'extension'); chrome.tabs.create({url: blobURL}); }).catch(err => { - enableOtherChromeExtensions(true).catch(err => { - log.warn('Chrome', `Could not enable/disable some extensions. ${err.message}`); - }); + enableOtherChromeExtensions(true); throw err; }); }; From 77ad6ad48fdf3ec270834aa5f0a4dbfdb7527e1e Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Tue, 24 Jan 2017 15:57:38 -0800 Subject: [PATCH 5/5] chain it up --- .../app/src/lighthouse-background.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lighthouse-extension/app/src/lighthouse-background.js b/lighthouse-extension/app/src/lighthouse-background.js index dd1c66308965..7f2ad4c29711 100644 --- a/lighthouse-extension/app/src/lighthouse-background.js +++ b/lighthouse-extension/app/src/lighthouse-background.js @@ -176,12 +176,14 @@ window.runLighthouseInExtension = function(options, requestedAggregations) { .then(_ => connection.getCurrentTabURL()) .then(url => window.runLighthouseForConnection(connection, url, options, requestedAggregations)) .then(results => { - enableOtherChromeExtensions(true); - const blobURL = window.createReportPageAsBlob(results, 'extension'); - chrome.tabs.create({url: blobURL}); + return enableOtherChromeExtensions(true).then(_ => { + const blobURL = window.createReportPageAsBlob(results, 'extension'); + chrome.tabs.create({url: blobURL}); + }); }).catch(err => { - enableOtherChromeExtensions(true); - throw err; + return enableOtherChromeExtensions(true).then(_ => { + throw err; + }); }); };