Update storage

This commit is contained in:
Lesley Norton 2021-10-22 12:30:10 -05:00
parent 3572bebbd9
commit 96bd0f4b4e
No known key found for this signature in database
GPG key ID: E98FBAEE3F13956E
5 changed files with 93 additions and 65 deletions

View file

@ -86,20 +86,11 @@ const messageHandler = {
m.url,
m.currentUserContextId,
m.newUserContextId,
m.tabIndex,
m.tabIndex,
m.active,
true
);
break;
case "mozillaVpnAttemptPort":
MozillaVPN_Background.maybeInitPort();
break;
case "getMozillaVpnServers":
MozillaVPN_Background.postToApp("servers");
break;
case "getMozillaVpnStatus":
response = MozillaVPN_Background.postToApp("status");
break;
case "assignAndReloadInContainer":
tab = await assignManager.reloadPageInContainer(
m.url,
@ -115,6 +106,22 @@ const messageHandler = {
return assignManager._setOrRemoveAssignment(tab.id, m.url, m.newUserContextId, m.value);
});
break;
case "MozillaVPN_attemptPort":
MozillaVPN_Background.maybeInitPort();
break;
case "MozillaVPN_queryServers":
MozillaVPN_Background.postToApp("servers");
break;
case "MozillaVPN_queryStatus":
response = MozillaVPN_Background.postToApp("status");
break;
case "MozillaVPN_getConnectionStatus":
response = MozillaVPN_Background.getConnectionStatus();
break;
case "MozillaVPN_getInstallationStatus":
response = MozillaVPN_Background.getInstallationStatus();
break;
}
return response;
});

View file

@ -1,9 +1,6 @@
const MozillaVPN_Background = {
MOZILLA_VPN_INSTALLED_KEY: "mozillaVpnInstalled",
MOZILLA_VPN_CONNECTED_KEY: "mozillaVpnConnected",
MOZILLA_VPN_COLLAPSE_EDIT_CONTAINER_TOUT_KEY: "mozillaVpnCollapseEditContainerTout",
MOZILLA_VPN_HIDE_MAIN_TOUT_KEY: "mozillaVpnHideMainTout",
MOZILLA_VPN_SERVERS_KEY: "mozillaVpnServers",
MOZILLA_VPN_HIDDEN_TOUTS_LIST_KEY: "mozillaVpnHiddenToutsList",
_isolationKey: 0,
@ -18,9 +15,9 @@ const MozillaVPN_Background = {
Which does is not caught by this try/catch
*/
this.port = await browser.runtime.connectNative("mozillavpn");
await browser.storage.local.set({ [this.MOZILLA_VPN_INSTALLED_KEY]: true});
this.port.onMessage.addListener(response => this.handleResponse(response));
this.port.onMessage.addListener(this.handleResponse);
this.postToApp("status");
this.postToApp("servers");
@ -31,23 +28,29 @@ const MozillaVPN_Background = {
this.port.onDisconnect.addListener(() => this.increaseIsolationKey());
} catch(e) {
browser.storage.local.set({ [this.MOZILLA_VPN_INSTALLED_KEY]: false });
browser.storage.local.set({ [this.MOZILLA_VPN_CONNECTED_KEY]: false });
this._installed = false;
this._connected = false;
}
},
async init() {
const mozillaVpnConnected = await browser.storage.local.get(this.MOZILLA_VPN_CONNECTED_KEY);
if (typeof(mozillaVpnConnected) === "undefined") {
browser.storage.local.set({ [this.MOZILLA_VPN_CONNECTED_KEY]: false });
browser.storage.local.set({ [this.MOZILLA_VPN_INSTALLED_KEY]: false });
browser.storage.local.set({ [this.MOZILLA_VPN_SERVERS_KEY]: [] });
browser.storage.local.set({ [this.MOZILLA_VPN_HIDE_MAIN_TOUT_KEY]: false });
browser.storage.local.set({ [this.MOZILLA_VPN_COLLAPSE_EDIT_CONTAINER_TOUT_KEY]: false });
const { mozillaVpnServers } = await browser.storage.local.get(this.MOZILLA_VPN_SERVERS_KEY);
if (typeof(mozillaVpnServers) === "undefined") {
await browser.storage.local.set({ [this.MOZILLA_VPN_SERVERS_KEY]:[] });
await browser.storage.local.set({ [this.MOZILLA_VPN_HIDDEN_TOUTS_LIST_KEY]:[] });
this._installed = false;
this._connected = false;
}
this.maybeInitPort();
},
async getConnectionStatus() {
return this._connected;
},
async getInstallationStatus() {
return this._installed;
},
// Post messages to MozillaVPN client
postToApp(message) {
@ -55,8 +58,8 @@ const MozillaVPN_Background = {
this.port.postMessage({t: message});
} catch(e) {
if (e.message === "Attempt to postMessage on disconnected port") {
browser.storage.local.set({ [this.MOZILLA_VPN_INSTALLED_KEY]: false });
browser.storage.local.set({ [this.MOZILLA_VPN_CONNECTED_KEY]: false });
this._installed = false;
this._connected = false;
}
}
},
@ -64,10 +67,10 @@ const MozillaVPN_Background = {
// Handle responses from MozillaVPN client
async handleResponse(response) {
if (response.error && response.error === "vpn-client-down") {
browser.storage.local.set({ [MozillaVPN_Background.MOZILLA_VPN_CONNECTED_KEY]: false });
this.increaseIsolationKey();
MozillaVPN_Background._connected = false;
return;
}
MozillaVPN_Background._installed = true;
if (response.servers) {
const servers = response.servers.countries;
browser.storage.local.set({ [MozillaVPN_Background.MOZILLA_VPN_SERVERS_KEY]: servers});
@ -75,20 +78,18 @@ const MozillaVPN_Background = {
}
if ((response.status && response.status.vpn) || response.t === "status") {
browser.storage.local.set({ [MozillaVPN_Background.MOZILLA_VPN_INSTALLED_KEY]: true });
const status = response.status ? response.status.vpn : response.vpn;
if (status === "StateOn") {
browser.storage.local.set({ [MozillaVPN_Background.MOZILLA_VPN_CONNECTED_KEY]: true });
MozillaVPN_Background._connected = true;
}
if (status === "StateOff" || status === "StateDisconnecting") {
browser.storage.local.set({ [MozillaVPN_Background.MOZILLA_VPN_CONNECTED_KEY]: false });
MozillaVPN_Background._connected = false;
}
// Let's increase the network key isolation at any vpn status change.
this.increaseIsolationKey();
MozillaVPN_Background.increaseIsolationKey();
}
},

View file

@ -1,8 +1,8 @@
const MozillaVPN = {
async handleContainerList(identities) {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const { mozillaVpnInstalled } = await browser.storage.local.get("mozillaVpnInstalled");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
const mozillaVpnInstalled = await browser.runtime.sendMessage({ method: "MozillaVPN_getInstallationStatus" });
this.handleStatusIndicatorsInContainerLists(mozillaVpnInstalled);
const proxies = await this.getProxies(identities);
@ -38,7 +38,7 @@ const MozillaVPN = {
},
async setStatusIndicatorIcons(mozillaVpnInstalled) {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
const statusIconEls = document.querySelectorAll(".moz-vpn-connection-status-indicator");
@ -127,7 +127,7 @@ const MozillaVPN = {
},
async getProxies(identities) {
const { mozillaVpnInstalled } = await browser.storage.local.get("mozillaVpnInstalled");
const mozillaVpnInstalled = await browser.runtime.sendMessage({ method: "MozillaVPN_getInstallationStatus" });
const proxies = {};
if (mozillaVpnInstalled) {
@ -152,7 +152,7 @@ const MozillaVPN = {
},
async getProxyWarnings(proxyObj) {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
if (!proxyObj) {
return "";
@ -170,8 +170,8 @@ const MozillaVPN = {
},
async getFlag(proxyObj) {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const { mozillaVpnInstalled } = await browser.storage.local.get("mozillaVpnInstalled");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
const mozillaVpnInstalled = await browser.runtime.sendMessage({ method: "MozillaVPN_getInstallationStatus" });
const flag = {
imgCode: "default",

View file

@ -63,7 +63,7 @@ const Logic = {
async init() {
browser.runtime.sendMessage({
method: "mozillaVpnAttemptPort"
method: "MozillaVPN_attemptPort"
}),
// Remove browserAction "upgraded" badge when opening panel
@ -662,14 +662,23 @@ Logic.registerPanel(P_CONTAINERS_LIST, {
// This method is called when the object is registered.
async initialize() {
await browser.runtime.sendMessage({ method: "getMozillaVpnStatus" });
const mozillaVpnToutName = "moz-tout-main-panel";
await browser.runtime.sendMessage({ method: "MozillaVPN_queryStatus" });
Utils.addEnterHandler(document.querySelector("#moz-vpn-learn-more"), () => {
MozillaVPN.handleMozillaCtaClick("mac-main-panel-btn");
window.close();
});
Utils.addEnterHandler(document.querySelector(".dismiss-moz-vpn-tout"), async() => {
const { mozillaVpnHiddenToutsList } = await browser.storage.local.get("mozillaVpnHiddenToutsList");
if (typeof(mozillaVpnHiddenToutsList) === "undefined") {
await browser.storage.local.set({ "mozillaVpnHiddenToutsList": [] });
}
document.querySelector("#moz-vpn-tout").classList.add("disappear");
browser.storage.local.set({ "mozillaVpnHideMainTout": true });
mozillaVpnHiddenToutsList.push({
name: mozillaVpnToutName
});
await browser.storage.local.set({ mozillaVpnHiddenToutsList });
});
Utils.addEnterHandler(document.querySelector("#manage-containers-link"), (e) => {
if (!e.target.classList.contains("disable-edit-containers")) {
@ -699,11 +708,11 @@ Logic.registerPanel(P_CONTAINERS_LIST, {
}
});
const { mozillaVpnHideMainTout } = await browser.storage.local.get("mozillaVpnHideMainTout");
const { mozillaVpnInstalled } = await browser.storage.local.get("mozillaVpnInstalled");
const { mozillaVpnHiddenToutsList } = await browser.storage.local.get("mozillaVpnHiddenToutsList");
const mozillaVpnToutShouldBeHidden = mozillaVpnHiddenToutsList && mozillaVpnHiddenToutsList.find(tout => tout.name === mozillaVpnToutName);
const mozVpnTout = document.getElementById("moz-vpn-tout");
if (mozillaVpnHideMainTout || mozillaVpnInstalled) {
if (mozillaVpnToutShouldBeHidden) {
mozVpnTout.remove();
}
},
@ -1391,8 +1400,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
async initialize() {
this.initializeRadioButtons();
await browser.runtime.sendMessage({ method: "getMozillaVpnServers" });
await browser.runtime.sendMessage({ method: "getMozillaVpnStatus" });
await browser.runtime.sendMessage({ method: "MozillaVPN_queryServers" });
await browser.runtime.sendMessage({ method: "MozillaVPN_queryStatus" });
class MozVpnContainerUi extends HTMLElement {
constructor() {
@ -1405,6 +1414,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
this.hideShowButton = this.querySelector(".expand-collapse");
this.primaryCta = this.querySelector("#get-mozilla-vpn");
this.advancedProxySettingsButton = document.querySelector(".advanced-proxy-settings-btn");
this.toutName = "moz-tout-edit-container-panel";
// Switch
this.switch = this.querySelector("#moz-vpn-switch");
@ -1425,7 +1435,9 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
}
async connectedCallback() {
const { mozillaVpnCollapseEditContainerTout } = await browser.storage.local.get("mozillaVpnCollapseEditContainerTout");
const { mozillaVpnHiddenToutsList } = await browser.storage.local.get("mozillaVpnHiddenToutsList");
const mozillaVpnCollapseEditContainerTout = mozillaVpnHiddenToutsList && mozillaVpnHiddenToutsList.find(tout => tout.name === this.toutName);
this.hideShowButton.addEventListener("click", this);
if (mozillaVpnCollapseEditContainerTout) {
@ -1502,8 +1514,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
}
async updateMozVpnStatusDependentUi() {
const { mozillaVpnInstalled } = await browser.storage.local.get("mozillaVpnInstalled");
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnInstalled = await browser.runtime.sendMessage({ method: "MozillaVPN_getInstallationStatus" });
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
if (!mozillaVpnInstalled) {
@ -1534,7 +1546,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
async enableDisableProxyButtons() {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
if (!this.switch.checked || this.switch.disabled || !mozillaVpnConnected) {
this.currentServerButton.disabled = true;
@ -1576,7 +1588,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
}
async updateProxyDependentUi(proxyInfo) {
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
const containerHasProxy = typeof(proxyInfo) !== "undefined";
@ -1633,7 +1645,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
});
}
handleEvent(e) {
async handleEvent(e) {
e.preventDefault();
e.stopPropagation();
if (e.type === "keyup" && e.key !== " ") {
@ -1641,12 +1653,19 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
}
this.classList.toggle("expanded");
if (!this.classList.contains("expanded")) {
browser.storage.local.set({ "mozillaVpnCollapseEditContainerTout": true });
return;
const { mozillaVpnHiddenToutsList } = await browser.storage.local.get("mozillaVpnHiddenToutsList");
if (typeof(mozillaVpnHiddenToutsList) === "undefined") {
await browser.storage.local.set({ "mozillaVpnHiddenToutsList":[] });
}
this.expandUi();
browser.storage.local.set({ "mozillaVpnCollapseEditContainerTout": false });
const toutIndex = mozillaVpnHiddenToutsList.findIndex(tout => tout.name === mozillaVpnUi.toutName);
if (toutIndex === -1) {
mozillaVpnHiddenToutsList.push({ name: mozillaVpnUi.toutName });
} else {
this.expandUi();
mozillaVpnHiddenToutsList.splice(toutIndex, 1);
}
return await browser.storage.local.set({ mozillaVpnHiddenToutsList });
}
}
@ -1765,8 +1784,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
// This method is called when the panel is shown.
async prepare() {
browser.runtime.sendMessage({ method: "getMozillaVpnServers" });
browser.runtime.sendMessage({ method: "getMozillaVpnStatus" });
browser.runtime.sendMessage({ method: "MozillaVPN_queryServers" });
browser.runtime.sendMessage({ method: "MozillaVPN_queryStatus" });
const identity = Logic.currentIdentity();
@ -1806,7 +1825,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
Utils.addEnterHandler(deleteButton, () => {
Logic.showPanel(P_CONTAINER_DELETE, this.getEditInProgressIdentity(), false, false);
});
const { mozillaVpnConnected } = await browser.storage.local.get("mozillaVpnConnected");
const mozillaVpnConnected = await browser.runtime.sendMessage({ method: "MozillaVPN_getConnectionStatus" });
const mozillaVpnUi = document.querySelector(".moz-vpn-controller-content");
mozillaVpnUi.updateMozVpnStatusDependentUi();
@ -1916,8 +1936,8 @@ Logic.registerPanel(P_ADVANCED_PROXY_SETTINGS, {
Logic.registerPanel(P_MOZILLA_VPN_SERVER_LIST, {
panelSelector: "#moz-vpn-server-list-panel",
async initialize() {
await browser.runtime.sendMessage({ method: "getMozillaVpnStatus" });
await browser.runtime.sendMessage({ method: "getMozillaVpnServers" });
await browser.runtime.sendMessage({ method: "MozillaVPN_queryStatus" });
await browser.runtime.sendMessage({ method: "MozillaVPN_queryServers" });
Utils.addEnterHandler(document.getElementById("moz-vpn-return"), async () => {
const identity = Logic.currentIdentity();

View file

@ -24,7 +24,7 @@ proxifiedContainers = {
let proxifiedContainersStore = await proxifiedContainers.retrieveAll();
if (!proxifiedContainersStore) proxifiedContainersStore = [];
let index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId);
const index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId);
if (index === -1) {
proxifiedContainersStore.push({
cookieStoreId: cookieStoreId,