Skip to content

Commit

Permalink
Merge pull request #2909
Browse files Browse the repository at this point in the history
Handle messages received before the event page
or service worker background process is ready.
  • Loading branch information
ghostwords committed Sep 7, 2023
2 parents 4c230c3 + 3e8c490 commit 9096d4f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
8 changes: 0 additions & 8 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,14 +1035,6 @@ $(function () {
chrome.runtime.sendMessage({
type: "getOptionsData",
}, (response) => {
if (!response) {
// event page/extension service worker is still starting up, retry
// async w/ non-zero delay to avoid locking up the messaging channel
setTimeout(function () {
getOptionsData();
}, 10);
return;
}
OPTIONS_DATA = response;
loadOptions();
});
Expand Down
8 changes: 0 additions & 8 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,14 +881,6 @@ $(function () {
tabId: tab.id,
tabUrl: tab.url
}, (response) => {
if (!response) {
// event page/extension service worker is still starting up, retry
// async w/ non-zero delay to avoid locking up the messaging channel
setTimeout(function () {
getPopupData(tab);
}, 10);
return;
}
setPopupData(response);
refreshPopup();
init();
Expand Down
27 changes: 19 additions & 8 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,12 @@ function onHeadersReceived(details) {
/**
* Event handler when a tab gets removed
*
* @param {Integer} tab_id Id of the tab
* @param {Integer} tab_id ID of the tab
*/
function onTabRemoved(tab_id) {
if (badger.INITIALIZED) {
setTimeout(function () {
forgetTab(tab_id);
}, 20000);
}
setTimeout(function () {
forgetTab(tab_id);
}, utils.oneSecond() * 20);
}

/**
Expand Down Expand Up @@ -1186,8 +1184,16 @@ function getSurrogateWidget(name, data, frame_url) {

// NOTE: sender.tab is available for content script (not popup) messages only
function dispatcher(request, sender, sendResponse) {
// messages may arrive before Privacy Badger is ready
// for example, user clicks to open the popup when the ephemeral background
// process is not running; the getPopupData message from the popup causes
// the background to start but the background is not yet ready to respond
if (!badger.INITIALIZED) {
return sendResponse();
setTimeout(function () {
dispatcher(request, sender, sendResponse);
}, 50);
// indicate this is an async response to chrome.runtime.onMessage
return true;
}

// messages from content scripts are to be treated with greater caution:
Expand All @@ -1210,7 +1216,12 @@ function dispatcher(request, sender, sendResponse) {
"widgetFromSurrogate",
"widgetReplacementReady",
];
if (!KNOWN_CONTENT_SCRIPT_MESSAGES.includes(request.type)) {
if (KNOWN_CONTENT_SCRIPT_MESSAGES.includes(request.type)) {
if (!sender.tab) {
console.error("Dropping malformed content script message %o from %s", request, sender.url);
return sendResponse();
}
} else {
console.error("Rejected unknown message %o from %s", request, sender.url);
return sendResponse();
}
Expand Down

0 comments on commit 9096d4f

Please sign in to comment.