Add "isIsolated" to JSON backups

This commit is contained in:
Minigugus 2022-06-12 11:37:21 +02:00
parent 407cfaa21b
commit b8cf955e1f

View file

@ -277,9 +277,21 @@ const backgroundLogic = {
return Promise.all(
identities.map(async ({ cookieStoreId, color, icon, name }) => {
const userContextId = this.getUserContextIdFromCookieStoreId(cookieStoreId);
const sitesByContainer = await assignManager.storageArea.getAssignedSites(userContextId);
const [
{ isIsolated },
sitesByContainer
] = await Promise.all([
identityState.storageArea.get(cookieStoreId),
assignManager.storageArea.getAssignedSites(userContextId)
]);
const sites = Object.values(sitesByContainer).map(({ neverAsk, hostname }) => ({ neverAsk, hostname }));
return ({ color, icon, name, sites });
return ({
color,
icon,
name,
isolated: isIsolated && true, // either `true` or `undefined`
sites
});
})
);
},
@ -288,13 +300,19 @@ const backgroundLogic = {
const backup = await browser.contextualIdentities.query({});
const incomplete = [];
let allSucceed = true;
const identitiesPromise = identities.map(async ({ color, icon, name, sites }) => {
const identitiesPromise = identities.map(async ({ color, icon, name, isolated, sites }) => {
try {
if (typeof color !== "string" || typeof icon !== "string" || typeof name !== "string" || !Array.isArray((sites)))
if (
typeof color !== "string" ||
typeof icon !== "string" ||
typeof name !== "string" ||
(isolated !== true && isolated !== undefined) ||
!Array.isArray((sites))
)
throw new Error("Corrupted container backup");
const identity = await browser.contextualIdentities.create({ color, icon, name });
try {
await identityState.storageArea.get(identity.cookieStoreId);
await identityState.storageArea.get(identity.cookieStoreId); // to create identity state
const userContextId = this.getUserContextIdFromCookieStoreId(identity.cookieStoreId);
for (const { neverAsk, hostname } of sites) {
if (typeof neverAsk !== "boolean" || typeof hostname !== "string" || hostname === "")
@ -305,6 +323,8 @@ const backgroundLogic = {
userContextId
});
}
if (isolated)
await identityState.storageArea.set(identity.cookieStoreId, { isIsolated: "locked" });
} catch (err) {
incomplete.push(name); // site association damaged
}
@ -314,6 +334,7 @@ const backgroundLogic = {
return null;
}
});
const created = await Promise.all(identitiesPromise);
if (!allSucceed) { // Importation failed, restore previous state
await Promise.all(
@ -325,14 +346,16 @@ const backgroundLogic = {
})
);
throw new Error("Some containers couldn't be created");
} else { // Importation succeed, remove old identities
await Promise.all(
backup.map(async (identity) => {
await identityState.storageArea.remove(identity.cookieStoreId);
await browser.contextualIdentities.remove(identity.cookieStoreId);
})
);
}
// Importation succeed, remove old identities
await Promise.all(
backup.map(async (identity) => {
await identityState.storageArea.remove(identity.cookieStoreId);
await browser.contextualIdentities.remove(identity.cookieStoreId);
})
);
return { created: created.length, incomplete };
},