Skip to content

Commit

Permalink
Put enable/disable extension in normal promise flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Jan 24, 2017
1 parent e283ae2 commit 138a3df
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions lighthouse-extension/app/src/lighthouse-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}));
}
Expand Down Expand Up @@ -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;
});
};

/**
Expand All @@ -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;
});
};
Expand Down

0 comments on commit 138a3df

Please sign in to comment.