Syncs deletion of site assignments and reassignments

This commit is contained in:
Kendall Werts 2019-12-31 13:59:00 -06:00
parent 0e45f06338
commit 0bdf8558f6

View file

@ -75,7 +75,7 @@ const assignManager = {
await this.area.set({ await this.area.set({
[siteStoreKey]: data [siteStoreKey]: data
}); });
await backup(); await backup({undelete: siteStoreKey});
return; return;
}, },
@ -84,7 +84,7 @@ const assignManager = {
// When we remove an assignment we should clear all the exemptions // When we remove an assignment we should clear all the exemptions
this.removeExempted(pageUrl); this.removeExempted(pageUrl);
await this.area.remove([siteStoreKey]); await this.area.remove([siteStoreKey]);
await backup(); await backup({siteStoreKey});
return; return;
}, },
@ -587,6 +587,8 @@ async function backup(options) {
await updateCookieStoreIdMap(); await updateCookieStoreIdMap();
await updateSyncSiteAssignments(); await updateSyncSiteAssignments();
if (options && options.uuid) await updateDeletedIdentityList(options.uuid); if (options && options.uuid) await updateDeletedIdentityList(options.uuid);
if (options && options.siteStoreKey) await addToDeletedSitesList(options.siteStoreKey);
if (options && options.undelete) await removeFromDeletedSitesList(options.undelete);
// for testing // for testing
const storage = await browser.storage.sync.get(); const storage = await browser.storage.sync.get();
console.log("in sync: ", storage); console.log("in sync: ", storage);
@ -616,10 +618,26 @@ async function updateSyncSiteAssignments() {
async function updateDeletedIdentityList(deletedIdentityUUID) { async function updateDeletedIdentityList(deletedIdentityUUID) {
let { deletedIdentityList } = await browser.storage.sync.get("deletedIdentityList"); let { deletedIdentityList } = await browser.storage.sync.get("deletedIdentityList");
if (!deletedIdentityList) deletedIdentityList = []; if (!deletedIdentityList) deletedIdentityList = [];
if (deletedIdentityList.find(element => element === deletedIdentityUUID)) return;
deletedIdentityList.push(deletedIdentityUUID); deletedIdentityList.push(deletedIdentityUUID);
await browser.storage.sync.set({ deletedIdentityList }); await browser.storage.sync.set({ deletedIdentityList });
} }
async function addToDeletedSitesList(siteStoreKey) {
let { deletedSiteList } = await browser.storage.sync.get("deletedSiteList");
if (!deletedSiteList) deletedSiteList = [];
if (deletedSiteList.find(element => element === siteStoreKey)) return;
deletedSiteList.push(siteStoreKey);
await browser.storage.sync.set({ deletedSiteList });
}
async function removeFromDeletedSitesList(siteStoreKey) {
let { deletedSiteList } = await browser.storage.sync.get("deletedSiteList");
if (!deletedSiteList) return;
deletedSiteList = deletedSiteList.filter(element => element !== siteStoreKey);
await browser.storage.sync.set({ deletedSiteList });
}
browser.resetMAC1 = async function () { browser.resetMAC1 = async function () {
// for debugging and testing: remove all containers except the default 4 and the first one created // for debugging and testing: remove all containers except the default 4 and the first one created
browser.storage.onChanged.removeListener(syncOnChangedListener); browser.storage.onChanged.removeListener(syncOnChangedListener);
@ -801,25 +819,27 @@ async function reconcileSiteAssignments(inSync, firstSync = false) {
console.log("reconcileSiteAssignments"); console.log("reconcileSiteAssignments");
const assignedSitesLocal = await assignManager.storageArea.getAssignedSites(); const assignedSitesLocal = await assignManager.storageArea.getAssignedSites();
const assignedSitesFromSync = inSync.assignedSites; const assignedSitesFromSync = inSync.assignedSites;
if (inSync.hasOwnProperty("deletedSiteList")){
for(const siteStoreKey of inSync.deletedSiteList) {
if (assignedSitesLocal.hasOwnProperty(siteStoreKey)) {
assignManager.storageArea.remove(siteStoreKey.replace(/^siteContainerMap@@_/, "https://"));
}
}
}
for(const urlKey of Object.keys(assignedSitesFromSync)) { for(const urlKey of Object.keys(assignedSitesFromSync)) {
if (assignedSitesLocal.hasOwnProperty(urlKey)) { if (assignedSitesLocal.hasOwnProperty(urlKey)) {
const syncCookieStoreId = "firefox-container-" + assignedSitesFromSync[urlKey].userContextId; const syncCookieStoreId = "firefox-container-" + assignedSitesFromSync[urlKey].userContextId;
const syncUUID = await inSync.cookieStoreIDmap[syncCookieStoreId]; const syncUUID = await inSync.cookieStoreIDmap[syncCookieStoreId];
const assignedSite = assignedSitesLocal[urlKey]; const assignedSite = assignedSitesLocal[urlKey];
const localCookieStoreId = "firefox-container-" + assignedSite.userContextId; const localCookieStoreId = "firefox-container-" + assignedSite.userContextId;
const localIdentityUUID = await identityState.storageArea.get(localCookieStoreId).macAddonUUID const localIdentityUUID = await identityState.storageArea.get(localCookieStoreId).macAddonUUID;
if (syncUUID === localIdentityUUID) { if (syncUUID === localIdentityUUID) {
continue; continue;
} }
if (!firstSync) { // overwrite with Sync data. Sync is the source of truth
// overwrite with Sync data
await setAsignmentWithUUID(syncUUID, assignedSite, urlKey); await setAsignmentWithUUID(syncUUID, assignedSite, urlKey);
continue; continue;
} }
// TODO: on First Sync only, if uuids are not the same,
// ask user where to assign the site.
continue;
}
const assignedSite = assignedSitesFromSync[urlKey]; const assignedSite = assignedSitesFromSync[urlKey];
console.log("new assignment ", assignedSite, ": ", assignedSite.userContextId) console.log("new assignment ", assignedSite, ": ", assignedSite.userContextId)
const newUUID = await inSync.cookieStoreIDmap["firefox-container-" + assignedSite.userContextId]; const newUUID = await inSync.cookieStoreIDmap["firefox-container-" + assignedSite.userContextId];
@ -829,11 +849,15 @@ async function reconcileSiteAssignments(inSync, firstSync = false) {
async function setAsignmentWithUUID (newUUID, assignedSite, urlKey) { async function setAsignmentWithUUID (newUUID, assignedSite, urlKey) {
const cookieStoreId = await identityState.lookupCookieStoreId(newUUID); const cookieStoreId = await identityState.lookupCookieStoreId(newUUID);
if (cookieStoreId) {
assignedSite.userContextId = cookieStoreId.replace(/^firefox-container-/, ""); assignedSite.userContextId = cookieStoreId.replace(/^firefox-container-/, "");
await assignManager.storageArea.set( await assignManager.storageArea.set(
urlKey.replace(/^siteContainerMap@@_/, "https://"), urlKey.replace(/^siteContainerMap@@_/, "https://"),
assignedSite assignedSite
); );
return;
}
throw new Error ("No cookieStoreId found for: ", newUUID, assignedSite, urlKey);
} }
async function runSync() { async function runSync() {