diff --git a/src/js/background/identityState.js b/src/js/background/identityState.js index d032790..f4fb282 100644 --- a/src/js/background/identityState.js +++ b/src/js/background/identityState.js @@ -79,13 +79,10 @@ const identityState = { async getCookieStoreIDuuidMap() { const containers = {}; - const containerInfo = await identityState.storageArea.area.get(); - for(const configKey of Object.keys(containerInfo)) { - if (configKey.includes("identitiesState@@_")) { - const container = containerInfo[configKey]; - const cookieStoreId = configKey.replace(/^identitiesState@@_/, ""); - containers[cookieStoreId] = container.macAddonUUID; - } + const identities = await browser.contextualIdentities.query({}); + for(const identity of identities) { + const containerInfo = await this.storageArea.get(identity.cookieStoreId); + containers[identity.cookieStoreId] = containerInfo.macAddonUUID; } return containers; }, diff --git a/src/js/background/sync.js b/src/js/background/sync.js index f12eaed..5a418eb 100644 --- a/src/js/background/sync.js +++ b/src/js/background/sync.js @@ -62,14 +62,14 @@ const sync = { await addToDeletedSitesList(options.siteStoreKey); if (options && options.undelete) await removeFromDeletedSitesList(options.undelete); - if (SYNC_DEBUG) { - const storage = await sync.storageArea.get(); - console.log("inSync: ", storage); - const localStorage = await browser.storage.local.get(); - console.log("inLocal:", localStorage); - console.log("idents: ", await browser.contextualIdentities.query({})); - } - + // if (SYNC_DEBUG) { + // const storage = await sync.storageArea.get(); + // console.log("inSync: ", storage); + // const localStorage = await browser.storage.local.get(); + // console.log("inLocal:", localStorage); + // console.log("idents: ", await browser.contextualIdentities.query({})); + // } + console.log("Backed up!"); await sync.checkForListenersMaybeAdd(); async function updateSyncIdentities() { @@ -159,12 +159,14 @@ const sync = { await browser.storage.onChanged.hasListener( sync.storageArea.onChangedListener ); + + const hasCIListener = await hasContextualIdentityListeners(); - if (! await hasContextualIdentityListeners()) { + if (!hasCIListener) { addContextualIdentityListeners(); } - if (! hasStorageListener) { + if (!hasStorageListener) { browser.storage.onChanged.addListener( sync.storageArea.onChangedListener); } @@ -175,8 +177,10 @@ const sync = { await browser.storage.onChanged.hasListener( sync.storageArea.onChangedListener ); - - if (await hasContextualIdentityListeners()) { + + const hasCIListener = await hasContextualIdentityListeners(); + + if (hasCIListener) { removeContextualIdentityListeners(); } @@ -190,9 +194,8 @@ const sync = { if (SYNC_DEBUG) { const syncInfo = await sync.storageArea.get(); const localInfo = await browser.storage.local.get(); - console.log("inSync: ", syncInfo); - console.log("inLocal: ", localInfo); - console.log("indents: ", await browser.contextualIdentities.query({})); + const idents = await browser.contextualIdentities.query({}); + console.log("Initial State:", {syncInfo, localInfo, idents}); } await sync.checkForListenersMaybeRemove(); console.log("runSync"); @@ -252,22 +255,25 @@ async function reconcileIdentities(){ // now compare all containers for matching names. for (const syncIdentity of syncIdentities) { syncIdentity.macAddonUUID = cookieStoreIDmap[syncIdentity.cookieStoreId]; - const localMatch = localIdentities.find( - localIdentity => localIdentity.name === syncIdentity.name - ); - if (!localMatch) { - // if there's no name match found, check on uuid, - const localCookieStoreID = - await identityState.lookupCookieStoreId(syncIdentity.macAddonUUID); - if (localCookieStoreID) { - await ifUUIDMatch(syncIdentity, localCookieStoreID); + if (syncIdentity.macAddonUUID){ + const localMatch = localIdentities.find( + localIdentity => localIdentity.name === syncIdentity.name + ); + if (!localMatch) { + // if there's no name match found, check on uuid, + const localCookieStoreID = + await identityState.lookupCookieStoreId(syncIdentity.macAddonUUID); + if (localCookieStoreID) { + await ifUUIDMatch(syncIdentity, localCookieStoreID); + continue; + } + await ifNoMatch(syncIdentity); continue; } - await ifNoMatch(syncIdentity); + await ifNamesMatch(syncIdentity, localMatch); continue; } - await ifNamesMatch(syncIdentity, localMatch); - continue; + // if no macAddonUUID, there is a problem with the sync info and it needs to be ignored. } } @@ -291,7 +297,6 @@ async function ifNamesMatch(syncIdentity, localMatch) { } } } - // Sync is truth. If all is the same, update the local uuid to match sync await identityState.updateUUID( localMatch.cookieStoreId, diff --git a/src/js/background/test.js b/src/js/background/test.js index bc05fbe..1634f3f 100644 --- a/src/js/background/test.js +++ b/src/js/background/test.js @@ -243,6 +243,64 @@ browser.tests = { console.log("Finished!"); }, + async CIerrorTest() { + await browser.tests.stopSyncListeners(); + console.log("Test state from sync that duped everything initially"); + + await this.setState( + CI_ERROR_TEST_SYNC, + CI_ERROR_TEST_LOCAL, + CI_ERROR_TEST_IDENTS, + CI_ERROR_TEST_SITES + ); + + await sync.runSync(); + + const getSync = await browser.storage.sync.get(); + const getAssignedSites = + await assignManager.storageArea.getAssignedSites(); + + const identities = await browser.contextualIdentities.query({}); + + const localCookieStoreIDmap = + await identityState.getCookieStoreIDuuidMap(); + + console.assert( + Object.keys(getSync.cookieStoreIDmap).length === 7, + "cookieStoreIDmap should have 7 entries" + ); + + console.assert( + Object.keys(localCookieStoreIDmap).length === 8, + "localCookieStoreIDmap should have 8 entries" + ); + + console.assert( + identities.length === 7, + "There should be 7 identities" + ); + + console.assert( + Object.keys(getAssignedSites).length === 5, + "There should be 5 site assignments" + ); + + const personalContainer = + this.lookupIdentityBy(identities, {name: "Personal"}); + console.log(personalContainer); + console.assert( + personalContainer.color === "red", + "Personal Container should be red" + ); + const mozillaContainer = + this.lookupIdentityBy(identities, {name: "Mozilla"}); + console.assert( + mozillaContainer.icon === "pet", + "Mozilla Container should be pet" + ); + console.log("Finished!"); + }, + lookupIdentityBy(identities, options) { for (const identity of identities) { if (options && options.name) { @@ -555,3 +613,132 @@ const DUPE_TEST_IDENTS = [ "color": "yellow", } ]; + +const CI_ERROR_TEST_SYNC = { + "identities": [ + { + "name": "Personal", + "icon": "fingerprint", + "iconUrl": "resource://usercontext-content/fingerprint.svg", + "color": "blue", + "colorCode": "#37adff", + "cookieStoreId": "firefox-container-6" + }, + { + "name": "Mozilla", + "icon": "fruit", + "iconUrl": "resource://usercontext-content/fruit.svg", + "color": "purple", + "colorCode": "#af51f5", + "cookieStoreId": "firefox-container-8" + }, + { + "name": "Groceries, obviously", + "icon": "cart", + "iconUrl": "resource://usercontext-content/cart.svg", + "color": "yellow", + "colorCode": "#ffcb00", + "cookieStoreId": "firefox-container-9" + }, + { + "name": "Facebook", + "icon": "circle", + "iconUrl": "resource://usercontext-content/circle.svg", + "color": "blue", + "colorCode": "#37adff", + "cookieStoreId": "firefox-container-10" + }, + { + "name": "Work", + "icon": "briefcase", + "iconUrl": "resource://usercontext-content/briefcase.svg", + "color": "orange", + "colorCode": "#ff9f00", + "cookieStoreId": "firefox-container-11" + }, + { + "name": "Greg's container", + "icon": "vacation", + "iconUrl": "resource://usercontext-content/vacation.svg", + "color": "yellow", + "colorCode": "#ffcb00", + "cookieStoreId": "firefox-container-14" + } + ], + "deletedIdentityList": [ + "8098140e-d406-4321-b4f5-24763b4f9513", + "73aebc7a-286f-408a-9a94-a06d29b288e0", + "8f153224-bbe8-4664-ba02-0293ddec3e78" + ], + "cookieStoreIDmap": { + "firefox-container-10": "58956e95-43fb-44af-95c0-1ec8d83e1e13", + "firefox-container-11": "0269558d-6be7-487b-beb1-b720b346d09b", + "firefox-container-14": "e48d04cf-6277-4236-8f3d-611287d0caf2", + "firefox-container-6": "869a7563-030d-4a63-8a84-209270561d3c", + "firefox-container-8": "73aebc7a-286f-408a-9a94-a06d29b288e0", + "firefox-container-9": "4831fef4-6f43-47fb-a578-ccdc3ee7f883" + }, + "assignedSites": { + "siteContainerMap@@_bugzilla.mozilla.org": { + "userContextId": "11", + "neverAsk": true, + "hostname": "bugzilla.mozilla.org" + }, + "siteContainerMap@@_www.amazon.com": { + "userContextId": "14", + "neverAsk": false, + "hostname": "www.amazon.com" + } + }, + "deletedSiteList": [ + "siteContainerMap@@_www.facebook.com" + ] +}; + +const CI_ERROR_TEST_LOCAL = { + "browserActionBadgesClicked": [ + "6.1.1" + ], + "containerTabsOpened": 6, + "onboarding-stage": 5, +}; + +const CI_ERROR_TEST_SITES = [ + "siteContainerMap@@_bugzilla.mozilla.org", + "siteContainerMap@@_www.bankofoklahoma.com", + "siteContainerMap@@_www.mozilla.org", + "siteContainerMap@@_www.reddit.com" +]; + +const CI_ERROR_TEST_IDENTS = [ + { + "name": "Personal", + "icon": "fingerprint", + "color": "blue", + }, + { + "name": "Work", + "icon": "briefcase", + "color": "orange", + }, + { + "name": "Banking", + "icon": "dollar", + "color": "green", + }, + { + "name": "Mozilla", + "icon": "fruit", + "color": "purple", + }, + { + "name": "Groceries, obviously", + "icon": "cart", + "color": "yellow", + }, + { + "name": "Facebook", + "icon": "circle", + "color": "blue", + } +];