Fix empty sync exclude regexp behavior

https://github.com/mozilla/multi-account-containers/pull/2383
This commit is contained in:
BPower0036 2022-07-29 16:02:44 +00:00 committed by GitHub
parent 61e5960c6f
commit 3d02d64589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -128,13 +128,12 @@ const sync = {
await sync.checkForListenersMaybeAdd();
async function updateSyncIdentities() {
const { syncExcludeRegExp } = await browser.storage.local.get("syncExcludeRegExp");
const excludeRegExp = new RegExp(syncExcludeRegExp, "i");
const excludeRegExp = await getExcludeRegExp();
const identities = await browser.contextualIdentities.query({});
for (const identity of identities) {
// skip excluded identities
if (identity.name.match(excludeRegExp)) {
if (excludeRegExpMatchesIdentity(excludeRegExp, identity)) {
continue;
}
@ -334,6 +333,7 @@ async function restore() {
*/
async function reconcileIdentities(){
if (SYNC_DEBUG) console.log("reconcileIdentities");
const excludeRegExp = await getExcludeRegExp();
// first delete any from the deleted list
const deletedIdentityList =
@ -343,6 +343,14 @@ async function reconcileIdentities(){
const deletedCookieStoreId =
await identityState.lookupCookieStoreId(deletedUUID);
if (deletedCookieStoreId){
if (excludeRegExp) {
const deletedIdentity = await identityState.get(deletedCookieStoreId);
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, deletedIdentity)) {
continue;
}
}
try{
await browser.contextualIdentities.remove(deletedCookieStoreId);
} catch (error) {
@ -357,6 +365,11 @@ async function reconcileIdentities(){
await sync.storageArea.getIdentities();
// find any local dupes created on sync storage and delete from sync storage
for (const localIdentity of localIdentities) {
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, localIdentity)) {
continue;
}
const syncIdentitiesOfName = syncIdentitiesRemoveDupes
.filter(identity => identity.name === localIdentity.name);
if (syncIdentitiesOfName.length > 1) {
@ -370,6 +383,11 @@ async function reconcileIdentities(){
await sync.storageArea.getIdentities();
// now compare all containers for matching names.
for (const syncIdentity of syncIdentities) {
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, syncIdentity)) {
continue;
}
if (syncIdentity.macAddonUUID){
const localMatch = localIdentities.find(
localIdentity => localIdentity.name === syncIdentity.name
@ -585,3 +603,24 @@ async function setAssignmentWithUUID(assignedSite, urlKey) {
}
throw new Error (`No cookieStoreId found for: ${uuid}, ${urlKey}`);
}
// Retrieve the sync exclude regexp from local storage.
async function getExcludeRegExp() {
const { syncExcludeRegExp } = await browser.storage.local.get("syncExcludeRegExp");
if (syncExcludeRegExp) {
return new RegExp(syncExcludeRegExp, "i");
} else {
return false;
}
}
// Matching the provided exclude regexp against the provided identity and return
// true if they match.
function excludeRegExpMatchesIdentity(excludeRegExp, identity) {
if (excludeRegExp && identity.name.match(excludeRegExp)) {
if (SYNC_DEBUG) console.log(`Exclude regexp matches identity '${identity.name}'`);
return true;
} else {
return false;
}
}