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

CRX: disable extensions before running lighthouse #1492

Merged
merged 5 commits into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lighthouse-extension/app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"permissions": [
"activeTab",
"debugger",
"management",
"storage"
],
"browser_action": {
Expand Down
55 changes: 49 additions & 6 deletions lighthouse-extension/app/src/lighthouse-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,36 @@ 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) {
const str = enable ? 'enabling' : 'disabling';
log.log('Chrome', `${str} ${installedExtensions.length} extensions.`);

return Promise.all(installedExtensions.map(info => {
return new Promise((resolve, reject) => {
chrome.management.setEnabled(info.id, enable, _ => {
Copy link
Member

Choose a reason for hiding this comment

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

since the extension is running this after a button click, I assume this gets around the docs warning about "In most cases this function must be called in the context of a user gesture"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea. Still falls within that click.

if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
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.
Expand Down Expand Up @@ -124,8 +149,7 @@ window.runLighthouseForConnection = function(connection, url, options, requested
lighthouseIsRunning = true;
updateBadgeUI(url);

// Run Lighthouse.
return Runner.run(connection, runOptions)
return Runner.run(connection, runOptions) // Run Lighthouse.
Copy link
Member

Choose a reason for hiding this comment

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

can revert this block of changes?

Copy link
Contributor Author

@ebidel ebidel Jan 24, 2017

Choose a reason for hiding this comment

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

It's just a proper 4-space indent. No code changes.

https://github.com/GoogleChrome/lighthouse/pull/1492/files?w=1

Copy link
Member

Choose a reason for hiding this comment

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

only two spaces for chaining though, just to keep everyone on their toes :)

Copy link
Member

Choose a reason for hiding this comment

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

(I think this is one place we diverge from Google style, but it's so common in open source JS that we've pretty much unquestioningly done it throughout LH)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're not consistent, but I'll revert to be consistent with the file.

If we want to enforce something other than Goog style, there really should be an eslint rule.

.then(result => {
lighthouseIsRunning = false;
updateBadgeUI();
Expand All @@ -148,11 +172,18 @@ 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 => {
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 => {
return enableOtherChromeExtensions(true).then(_ => {
throw err;
});
});
};

Expand Down Expand Up @@ -277,7 +308,19 @@ window.isRunning = function() {
return lighthouseIsRunning;
};

// 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;
});
});
});

chrome.runtime.onInstalled.addListener(details => {
if (details.previousVersion) {
// eslint-disable-next-line no-console
Expand Down