added key to sync for each install of MAC.

Keeps a list of installed identities and site assignments of reach MAC install
This commit is contained in:
Kendall Werts 2020-01-22 10:57:27 -06:00
parent 361e49d553
commit d7b66eca52
2 changed files with 86 additions and 11 deletions

View file

@ -72,7 +72,7 @@ const assignManager = {
await this.area.set({ await this.area.set({
[siteStoreKey]: data [siteStoreKey]: data
}); });
if (backup) await sync.storageArea.backup({undelete: siteStoreKey}); if (backup) await sync.storageArea.backup({undeleteSiteStoreKey: siteStoreKey});
return; return;
}, },

View file

@ -44,6 +44,16 @@ const sync = {
return false; return false;
}, },
async getAllInstanceInfo() {
const instanceList = {};
const allSyncInfo = await this.get();
for (const objectKey of Object.keys(allSyncInfo)) {
if (objectKey.includes("MACinstance")) {
instanceList[objectKey] = allSyncInfo[objectKey]; }
}
return instanceList;
},
async hasSyncStorage(){ async hasSyncStorage(){
const inSync = await this.get(); const inSync = await this.get();
return !(Object.entries(inSync).length === 0); return !(Object.entries(inSync).length === 0);
@ -53,15 +63,18 @@ const sync = {
// remove listeners to avoid an infinite loop! // remove listeners to avoid an infinite loop!
await sync.checkForListenersMaybeRemove(); await sync.checkForListenersMaybeRemove();
await updateSyncIdentities(); const identities = await updateSyncIdentities();
await updateCookieStoreIdMap(); await updateCookieStoreIdMap();
await updateSyncSiteAssignments(); const siteAssignments = await updateSyncSiteAssignments();
await updateInstanceInfo(identities, siteAssignments);
if (options && options.uuid) if (options && options.uuid)
await updateDeletedIdentityList(options.uuid); await updateDeletedIdentityList(options.uuid);
if (options && options.undeleteUUID)
await removeFromDeletedIdentityList(options.undeleteUUID);
if (options && options.siteStoreKey) if (options && options.siteStoreKey)
await addToDeletedSitesList(options.siteStoreKey); await addToDeletedSitesList(options.siteStoreKey);
if (options && options.undelete) if (options && options.undeleteSiteStoreKey)
await removeFromDeletedSitesList(options.undelete); await removeFromDeletedSitesList(options.undeleteSiteStoreKey);
// if (SYNC_DEBUG) { // if (SYNC_DEBUG) {
// const storage = await sync.storageArea.get(); // const storage = await sync.storageArea.get();
// console.log("inSync: ", storage); // console.log("inSync: ", storage);
@ -74,7 +87,14 @@ const sync = {
async function updateSyncIdentities() { async function updateSyncIdentities() {
const identities = await browser.contextualIdentities.query({}); const identities = await browser.contextualIdentities.query({});
for (const identity of identities) {
delete identity.colorCode;
delete identity.iconUrl;
identity.macAddonUUID = await identityState.lookupMACaddonUUID(identity.cookieStoreId);
}
await sync.storageArea.set({ identities }); await sync.storageArea.set({ identities });
return identities;
} }
async function updateCookieStoreIdMap() { async function updateCookieStoreIdMap() {
@ -87,6 +107,22 @@ const sync = {
const assignedSites = const assignedSites =
await assignManager.storageArea.getAssignedSites(); await assignManager.storageArea.getAssignedSites();
await sync.storageArea.set({ assignedSites }); await sync.storageArea.set({ assignedSites });
return assignedSites;
}
async function updateInstanceInfo(identitiesInput, siteAssignmentsInput) {
const installUUID = browser.runtime.getURL("")
.replace(/moz-extension:\/\//, "MACinstance:")
.replace(/\//, "");
const identities = [];
const siteAssignments = [];
for (const identity of identitiesInput) {
identities.push(identity.macAddonUUID);
}
for (const siteAssignmentKey of Object.keys(siteAssignmentsInput)) {
siteAssignments.push(siteAssignmentKey);
}
await sync.storageArea.set({ [installUUID]: { identities, siteAssignments } });
} }
async function updateDeletedIdentityList(deletedIdentityUUID) { async function updateDeletedIdentityList(deletedIdentityUUID) {
@ -99,6 +135,14 @@ const sync = {
await sync.storageArea.set({ deletedIdentityList }); await sync.storageArea.set({ deletedIdentityList });
} }
async function removeFromDeletedIdentityList(identityUUID) {
const deletedIdentityList =
await sync.storageArea.getDeletedIdentityList();
const newDeletedIdentityList = deletedIdentityList
.filter(element => element !== identityUUID);
await sync.storageArea.set({ deletedIdentityList: newDeletedIdentityList });
}
async function addToDeletedSitesList(siteStoreKey) { async function addToDeletedSitesList(siteStoreKey) {
const deletedSiteList = const deletedSiteList =
await sync.storageArea.getDeletedSiteList(); await sync.storageArea.getDeletedSiteList();
@ -143,8 +187,12 @@ const sync = {
}, },
init() { init() {
browser.runtime.onInstalled.addListener(this.errorHandledRunSync); // Add listener to sync storage and containers.
browser.runtime.onStartup.addListener(this.errorHandledRunSync); // Works for all installs that have any sync storage.
// Waits for sync storage change before kicking off the restore/backup
// initial sync must be kicked off by user.
this.checkForListenersMaybeAdd();
}, },
async errorHandledRunSync () { async errorHandledRunSync () {
@ -208,7 +256,8 @@ const sync = {
if (hasSyncStorage && dataIsReliable) await restore(); if (hasSyncStorage && dataIsReliable) await restore();
await sync.storageArea.backup(); await sync.storageArea.backup();
return; await removeOldDeletedItems();
return;
}, },
async addContextualIdentityListeners(listenerList) { async addContextualIdentityListeners(listenerList) {
@ -232,7 +281,7 @@ const sync = {
await browser.contextualIdentities.onRemoved.hasListener(listenerList[1]) && await browser.contextualIdentities.onRemoved.hasListener(listenerList[1]) &&
await browser.contextualIdentities.onUpdated.hasListener(listenerList[2]) await browser.contextualIdentities.onUpdated.hasListener(listenerList[2])
); );
} },
}; };
@ -415,8 +464,6 @@ async function reconcileSiteAssignments() {
for(const urlKey of Object.keys(assignedSitesFromSync)) { for(const urlKey of Object.keys(assignedSitesFromSync)) {
const assignedSite = assignedSitesFromSync[urlKey]; const assignedSite = assignedSitesFromSync[urlKey];
try{ try{
const syncUUID = assignedSite.identityMacAddonUUID;
console.log("syncUUID, ", syncUUID);
if (assignedSite.identityMacAddonUUID) { if (assignedSite.identityMacAddonUUID) {
// Sync is truth. // Sync is truth.
// Not even looking it up. Just overwrite // Not even looking it up. Just overwrite
@ -436,6 +483,34 @@ async function reconcileSiteAssignments() {
} }
} }
async function removeOldDeletedItems() {
const instanceList = await sync.storageArea.getAllInstanceInfo();
const deletedSiteList = await sync.storageArea.getDeletedSiteList();
const deletedIdentityList = await sync.storageArea.getDeletedIdentityList();
for (const siteStoreKey of deletedSiteList) {
let hasMatch = false;
for (const instance of Object.values(instanceList)) {
const match = instance.siteAssignments.find(element => element === siteStoreKey);
if (!match) continue;
hasMatch = true;
}
if (!hasMatch) {
await sync.storageArea.backup({undeleteSiteStoreKey: siteStoreKey});
}
}
for (const identityUUID of deletedIdentityList) {
let hasMatch = false;
for (const instance of Object.values(instanceList)) {
const match = instance.identities.find(element => element === identityUUID);
if (!match) continue;
hasMatch = true;
}
if (!hasMatch) {
await sync.storageArea.backup({undeleteUUID: identityUUID});
}
}
}
async function setAssignmentWithUUID (assignedSite, urlKey) { async function setAssignmentWithUUID (assignedSite, urlKey) {
const uuid = assignedSite.identityMacAddonUUID; const uuid = assignedSite.identityMacAddonUUID;
const cookieStoreId = await identityState.lookupCookieStoreId(uuid); const cookieStoreId = await identityState.lookupCookieStoreId(uuid);