Follow up commit to add all messages to be promise safe including background.js messages. Fixe #670
This commit is contained in:
parent
9907be9537
commit
1a592f7f1c
3 changed files with 61 additions and 62 deletions
2
index.js
2
index.js
|
@ -276,7 +276,7 @@ const ContainerService = {
|
||||||
const api = await webExtension.startup();
|
const api = await webExtension.startup();
|
||||||
api.browser.runtime.onMessage.addListener(async function (message, sender, sendReply) {
|
api.browser.runtime.onMessage.addListener(async function (message, sender, sendReply) {
|
||||||
if ("method" in message && methods.indexOf(message.method) !== -1) {
|
if ("method" in message && methods.indexOf(message.method) !== -1) {
|
||||||
const response = ContainerService[message.method](message);
|
const response = ContainerService[message.method](message.message);
|
||||||
if (response instanceof Promise) {
|
if (response instanceof Promise) {
|
||||||
const responseValue = await response;
|
const responseValue = await response;
|
||||||
ContainerService.triggerBackgroundCallback({
|
ContainerService.triggerBackgroundCallback({
|
||||||
|
|
|
@ -419,7 +419,9 @@ const backgroundLogic = {
|
||||||
// Unhide all hidden tabs
|
// Unhide all hidden tabs
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: "showTabs",
|
method: "showTabs",
|
||||||
userContextId: options.userContextId
|
message: {
|
||||||
|
userContextId: options.userContextId
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return browser.tabs.create({
|
return browser.tabs.create({
|
||||||
url,
|
url,
|
||||||
|
@ -456,7 +458,7 @@ const messageHandler = {
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
// Handles messages from webextension code
|
// Handles messages from webextension code
|
||||||
browser.runtime.onMessage.addListener((m) => {
|
browser.runtime.onMessage.addListener(async function (m) {
|
||||||
let response;
|
let response;
|
||||||
|
|
||||||
switch (m.method) {
|
switch (m.method) {
|
||||||
|
@ -474,7 +476,7 @@ const messageHandler = {
|
||||||
assignManager._neverAsk(m);
|
assignManager._neverAsk(m);
|
||||||
break;
|
break;
|
||||||
case "getAssignment":
|
case "getAssignment":
|
||||||
response = browser.tabs.get(m.tabId).then((tab) => {
|
response = browser.tabs.get(m.message.tabId).then((tab) => {
|
||||||
return assignManager._getAssignment(tab);
|
return assignManager._getAssignment(tab);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
@ -485,13 +487,22 @@ const messageHandler = {
|
||||||
// m.tabId is used for where to place the in content message
|
// m.tabId is used for where to place the in content message
|
||||||
// m.url is the assignment to be removed/added
|
// m.url is the assignment to be removed/added
|
||||||
response = browser.tabs.get(m.tabId).then((tab) => {
|
response = browser.tabs.get(m.tabId).then((tab) => {
|
||||||
return assignManager._setOrRemoveAssignment(tab.id, m.url, m.userContextId, m.value);
|
return assignManager._setOrRemoveAssignment(tab.id, m.message.url, m.message.userContextId, m.message.value);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "exemptContainerAssignment":
|
case "exemptContainerAssignment":
|
||||||
response = assignManager._exemptTab(m);
|
response = assignManager._exemptTab(m);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (response instanceof Promise) {
|
||||||
|
const resolvedResponse = await response;
|
||||||
|
browser.runtime.sendMessage({
|
||||||
|
method: "async-popup-response",
|
||||||
|
message: resolvedResponse,
|
||||||
|
uuid: m.uuid
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -500,11 +511,11 @@ const messageHandler = {
|
||||||
port.onMessage.addListener(m => {
|
port.onMessage.addListener(m => {
|
||||||
switch (m.type) {
|
switch (m.type) {
|
||||||
case "async-background-response":
|
case "async-background-response":
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: "async-popup-response",
|
method: "async-popup-response",
|
||||||
message: m.message.response,
|
message: m.message.response,
|
||||||
uuid: m.message.uuid
|
uuid: m.message.uuid
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "lightweight-theme-changed":
|
case "lightweight-theme-changed":
|
||||||
themeManager.update(m.message);
|
themeManager.update(m.message);
|
||||||
|
@ -707,7 +718,9 @@ messageHandler.init();
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: "getPreference",
|
method: "getPreference",
|
||||||
pref: "browser.privatebrowsing.autostart"
|
message: {
|
||||||
|
pref: "browser.privatebrowsing.autostart"
|
||||||
|
}
|
||||||
}).then(pbAutoStart => {
|
}).then(pbAutoStart => {
|
||||||
|
|
||||||
// We don't want to disable the addon if we are in auto private-browsing.
|
// We don't want to disable the addon if we are in auto private-browsing.
|
||||||
|
|
|
@ -84,12 +84,12 @@ const Logic = {
|
||||||
// Get the onboarding variation
|
// Get the onboarding variation
|
||||||
const variationPromise = this.getShieldStudyVariation();
|
const variationPromise = this.getShieldStudyVariation();
|
||||||
browser.runtime.onMessage.addListener((m) => {
|
browser.runtime.onMessage.addListener((m) => {
|
||||||
if (m.method === "async-popup-response" && m.uuid) {
|
if (m.method === "async-popup-response" && m.uuid) {
|
||||||
const uuid = m.uuid;
|
const uuid = m.uuid;
|
||||||
if (uuid in this.asyncMessageQueue) {
|
if (uuid in this.asyncMessageQueue) {
|
||||||
this.asyncMessageQueue[uuid].response = m.message;
|
this.asyncMessageQueue[uuid].response = m.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -199,28 +199,30 @@ const Logic = {
|
||||||
// UUID SO result, as you do
|
// UUID SO result, as you do
|
||||||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
||||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||||
)
|
);
|
||||||
},
|
},
|
||||||
|
asyncNotResponded: "@@_not_responded_@@",
|
||||||
|
|
||||||
asyncCreateMessage() {
|
asyncCreateMessage() {
|
||||||
const guid = this.asyncId();
|
const guid = this.asyncId();
|
||||||
this.asyncMessageQueue[guid] = {
|
this.asyncMessageQueue[guid] = {
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
response: null
|
response: this.asyncNotResponded
|
||||||
};
|
};
|
||||||
return guid;
|
return guid;
|
||||||
},
|
},
|
||||||
|
|
||||||
awaitMessage(methodName) {
|
awaitMessage(methodName, message) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const messageId = this.asyncCreateMessage();
|
const messageId = this.asyncCreateMessage();
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: methodName,
|
method: methodName,
|
||||||
uuid: messageId
|
uuid: messageId,
|
||||||
|
message
|
||||||
});
|
});
|
||||||
const checkState = () => {
|
const checkState = () => {
|
||||||
const messageState = this.asyncMessageQueue[messageId];
|
const messageState = this.asyncMessageQueue[messageId];
|
||||||
if (messageState.response !== null) {
|
if (messageState.response !== this.asyncNotResponded) {
|
||||||
resolve(messageState.response);
|
resolve(messageState.response);
|
||||||
delete this.asyncMessageQueue[messageId];
|
delete this.asyncMessageQueue[messageId];
|
||||||
return;
|
return;
|
||||||
|
@ -330,28 +332,25 @@ const Logic = {
|
||||||
return Promise.reject("removeIdentity must be called with userContextId argument.");
|
return Promise.reject("removeIdentity must be called with userContextId argument.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("deleteContainer", {
|
||||||
method: "deleteContainer",
|
userContextId
|
||||||
message: {userContextId}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getAssignment(tab) {
|
getAssignment(tab) {
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("getAssignment", {
|
||||||
method: "getAssignment",
|
|
||||||
tabId: tab.id
|
tabId: tab.id
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getAssignmentObjectByContainer(userContextId) {
|
getAssignmentObjectByContainer(userContextId) {
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("getAssignmentObjectByContainer", {
|
||||||
method: "getAssignmentObjectByContainer",
|
userContextId
|
||||||
message: {userContextId}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
setOrRemoveAssignment(tabId, url, userContextId, value) {
|
setOrRemoveAssignment(tabId, url, userContextId, value) {
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("setOrRemoveAssignment", {
|
||||||
method: "setOrRemoveAssignment",
|
method: "setOrRemoveAssignment",
|
||||||
tabId,
|
tabId,
|
||||||
url,
|
url,
|
||||||
|
@ -525,9 +524,7 @@ Logic.registerPanel(P_CONTAINERS_LIST, {
|
||||||
});
|
});
|
||||||
|
|
||||||
Logic.addEnterHandler(document.querySelector("#sort-containers-link"), () => {
|
Logic.addEnterHandler(document.querySelector("#sort-containers-link"), () => {
|
||||||
browser.runtime.sendMessage({
|
Logic.awaitMessage("sortTabs").then(() => {
|
||||||
method: "sortTabs"
|
|
||||||
}).then(() => {
|
|
||||||
window.close();
|
window.close();
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -659,12 +656,9 @@ Logic.registerPanel(P_CONTAINERS_LIST, {
|
||||||
if (e.target.matches(".open-newtab")
|
if (e.target.matches(".open-newtab")
|
||||||
|| e.target.parentNode.matches(".open-newtab")
|
|| e.target.parentNode.matches(".open-newtab")
|
||||||
|| e.type === "keydown") {
|
|| e.type === "keydown") {
|
||||||
browser.runtime.sendMessage({
|
Logic.awaitMessage("openTab", {
|
||||||
method: "openTab",
|
userContextId: Logic.userContextId(identity.cookieStoreId),
|
||||||
message: {
|
source: "pop-up"
|
||||||
userContextId: Logic.userContextId(identity.cookieStoreId),
|
|
||||||
source: "pop-up"
|
|
||||||
}
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
window.close();
|
window.close();
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
|
@ -711,8 +705,8 @@ Logic.registerPanel(P_CONTAINER_INFO, {
|
||||||
|
|
||||||
Logic.addEnterHandler(document.querySelector("#container-info-hideorshow"), () => {
|
Logic.addEnterHandler(document.querySelector("#container-info-hideorshow"), () => {
|
||||||
const identity = Logic.currentIdentity();
|
const identity = Logic.currentIdentity();
|
||||||
browser.runtime.sendMessage({
|
const method = identity.hasHiddenTabs ? "showTabs" : "hideTabs";
|
||||||
method: identity.hasHiddenTabs ? "showTabs" : "hideTabs",
|
Logic.awaitMessage(method, {
|
||||||
userContextId: Logic.currentUserContextId()
|
userContextId: Logic.currentUserContextId()
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -722,9 +716,7 @@ Logic.registerPanel(P_CONTAINER_INFO, {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if the user has incompatible add-ons installed
|
// Check if the user has incompatible add-ons installed
|
||||||
browser.runtime.sendMessage({
|
Logic.awaitMessage("checkIncompatibleAddons").then(incompatible => {
|
||||||
method: "checkIncompatibleAddons"
|
|
||||||
}).then(incompatible => {
|
|
||||||
const moveTabsEl = document.querySelector("#container-info-movetabs");
|
const moveTabsEl = document.querySelector("#container-info-movetabs");
|
||||||
if (incompatible) {
|
if (incompatible) {
|
||||||
const fragment = document.createDocumentFragment();
|
const fragment = document.createDocumentFragment();
|
||||||
|
@ -741,8 +733,7 @@ Logic.registerPanel(P_CONTAINER_INFO, {
|
||||||
moveTabsEl.parentNode.insertBefore(fragment, moveTabsEl.nextSibling);
|
moveTabsEl.parentNode.insertBefore(fragment, moveTabsEl.nextSibling);
|
||||||
} else {
|
} else {
|
||||||
Logic.addEnterHandler(moveTabsEl, () => {
|
Logic.addEnterHandler(moveTabsEl, () => {
|
||||||
browser.runtime.sendMessage({
|
Logic.awaitMessage("moveTabsToWindow", {
|
||||||
method: "moveTabsToWindow",
|
|
||||||
userContextId: Logic.userContextId(Logic.currentIdentity().cookieStoreId),
|
userContextId: Logic.userContextId(Logic.currentIdentity().cookieStoreId),
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
window.close();
|
window.close();
|
||||||
|
@ -783,9 +774,8 @@ Logic.registerPanel(P_CONTAINER_INFO, {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's retrieve the list of tabs.
|
// Let's retrieve the list of tabs.
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("getTabs", {
|
||||||
method: "getTabs",
|
userContextId: Logic.currentUserContextId()
|
||||||
userContextId: Logic.currentUserContextId(),
|
|
||||||
}).then(this.buildInfoTable);
|
}).then(this.buildInfoTable);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -805,9 +795,8 @@ Logic.registerPanel(P_CONTAINER_INFO, {
|
||||||
if (tab.active) {
|
if (tab.active) {
|
||||||
tr.classList.add("clickable");
|
tr.classList.add("clickable");
|
||||||
Logic.addEnterHandler(tr, () => {
|
Logic.addEnterHandler(tr, () => {
|
||||||
browser.runtime.sendMessage({
|
Logic.awaitMessage("showTab", {
|
||||||
method: "showTab",
|
tabId: tab.id
|
||||||
tabId: tab.id,
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
window.close();
|
window.close();
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
|
@ -925,15 +914,12 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
|
||||||
|
|
||||||
_submitForm() {
|
_submitForm() {
|
||||||
const formValues = new FormData(this._editForm);
|
const formValues = new FormData(this._editForm);
|
||||||
return browser.runtime.sendMessage({
|
return Logic.awaitMessage("createOrUpdateContainer", {
|
||||||
method: "createOrUpdateContainer",
|
userContextId: formValues.get("container-id") || NEW_CONTAINER_ID,
|
||||||
message: {
|
params: {
|
||||||
userContextId: formValues.get("container-id") || NEW_CONTAINER_ID,
|
name: document.getElementById("edit-container-panel-name-input").value || Logic.generateIdentityName(),
|
||||||
params: {
|
icon: formValues.get("container-icon") || DEFAULT_ICON,
|
||||||
name: document.getElementById("edit-container-panel-name-input").value || Logic.generateIdentityName(),
|
color: formValues.get("container-color") || DEFAULT_COLOR,
|
||||||
icon: formValues.get("container-icon") || DEFAULT_ICON,
|
|
||||||
color: formValues.get("container-color") || DEFAULT_COLOR,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return Logic.refreshIdentities();
|
return Logic.refreshIdentities();
|
||||||
|
|
Loading…
Add table
Reference in a new issue