Migrate from cookieStoreId to userContextId

This commit is contained in:
baku 2017-01-06 19:35:18 +01:00
parent 404ee3304e
commit a6f9175224
2 changed files with 41 additions and 77 deletions

View file

@ -21,63 +21,32 @@ prefs.forEach((pref) => {
prefService.set(pref[0], pref[1]);
});
const CONTAINER_STORE = 'firefox-container-';
const identitiesState = {
};
function getCookieStoreIdForContainer(containerId) {
if (containerId == 0) {
return 'firefox-default';
}
return CONTAINER_STORE + containerId;
}
function convert(identity) {
const cookieStoreId = getCookieStoreIdForContainer(identity.userContextId);
let hiddenTabUrls = [];
if (cookieStoreId in identitiesState) {
hiddenTabUrls = identitiesState[cookieStoreId].hiddenTabUrls;
if (identity.userContextId in identitiesState) {
hiddenTabUrls = identitiesState[identity.userContextId].hiddenTabUrls;
}
const result = {
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
icon: identity.icon,
color: identity.color,
cookieStoreId: cookieStoreId,
userContextId: identity.userContextId,
hiddenTabUrls: hiddenTabUrls
};
return result;
}
function isContainerCookieStoreId(storeId) {
return storeId !== null && storeId.startsWith(CONTAINER_STORE);
}
function getContainerForCookieStoreId(storeId) {
if (!isContainerCookieStoreId(storeId)) {
return null;
}
const containerId = storeId.substring(CONTAINER_STORE.length);
if (ContextualIdentityService.getIdentityFromId(containerId)) {
return parseInt(containerId, 10);
}
return null;
}
function getContainer(cookieStoreId) {
const containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
function getContainer(userContextId) {
if (!userContextId) {
return Promise.resolve(null);
}
const identity = ContextualIdentityService.getIdentityFromId(containerId);
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
return Promise.resolve(convert(identity));
}
@ -94,8 +63,8 @@ function queryContainers(details) {
const convertedIdentity = convert(identity);
identities.push(convertedIdentity);
if (!(convertedIdentity.cookieStoreId in identitiesState)) {
identitiesState[convertedIdentity.cookieStoreId] = {hiddenTabUrls: []};
if (!(convertedIdentity.userContextId in identitiesState)) {
identitiesState[convertedIdentity.userContextId] = {hiddenTabUrls: []};
}
});
@ -110,14 +79,12 @@ function createContainer(details) {
return Promise.resolve(convert(identity));
}
function updateContainer(cookieStoreId, details) {
const containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
function updateContainer(userContextId, details) {
if (!userContextId) {
return Promise.resolve(null);
}
const identity = ContextualIdentityService.getIdentityFromId(containerId);
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
if (!identity) {
return Promise.resolve(null);
@ -144,14 +111,12 @@ function updateContainer(cookieStoreId, details) {
return Promise.resolve(convert(identity));
}
function removeContainer(cookieStoreId) {
const containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
function removeContainer(userContextId) {
if (!userContextId) {
return Promise.resolve(null);
}
const identity = ContextualIdentityService.getIdentityFromId(containerId);
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
if (!identity) {
return Promise.resolve(null);
@ -184,8 +149,8 @@ function openTab(args) {
}
let userContextId = 0;
if ('cookieStoreId' in args) {
userContextId = getContainerForCookieStoreId(args.cookieStoreId);
if ('userContextId' in args) {
userContextId = args.userContextId;
}
let tab = browserWin.gBrowser.addTab(args.url || null,
@ -200,17 +165,16 @@ function queryTabs(args) {
for (let tab of tabs) {
let xulTab = viewFor(tab);
let userContextId = xulTab.getAttribute('usercontextid') || 0;
let cookieStoreId = getCookieStoreIdForContainer(userContextId);
let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10);
if ("cookieStoreId" in args && args.cookieStoreId != cookieStoreId) {
if ("userContextId" in args && args.userContextId != userContextId) {
continue;
}
tabList.push({
id: tab.id,
url: tab.url,
cookieStoreId: cookieStoreId,
userContextId: userContextId,
});
}
@ -237,15 +201,15 @@ function handleWebExtensionMessage(message, sender, sendReply) {
sendReply(queryTabs(message));
break;
case 'hideTab':
identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave;
identitiesState[message.userContextId].hiddenTabUrls = message.tabUrlsToSave;
break;
case 'showTab':
sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls);
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
sendReply(identitiesState[message.userContextId].hiddenTabUrls);
identitiesState[message.userContextId].hiddenTabUrls = [];
break;
case 'removeTabs':
sendReply(removeTabs(message.tabIds));
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
identitiesState[message.userContextId].hiddenTabUrls = [];
break;
case 'getTab':
sendReply(contextualIdentities.get(message.arguments));

View file

@ -2,14 +2,14 @@
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
function hideContainerTabs(containerId) {
function hideContainerTabs(userContextId) {
const tabIdsToRemove = [];
const tabUrlsToSave = [];
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
browser.runtime.sendMessage({
method: 'queryTabs',
cookieStoreId: containerId
userContextId: userContextId
}).then(tabs=> {
tabs.forEach(tab=> {
tabIdsToRemove.push(tab.id);
@ -17,7 +17,7 @@ function hideContainerTabs(containerId) {
});
browser.runtime.sendMessage({
method: 'hideTab',
cookieStoreId: containerId,
userContextId: userContextId,
tabUrlsToSave: tabUrlsToSave
}).then(()=> {
browser.runtime.sendMessage({
@ -29,17 +29,17 @@ function hideContainerTabs(containerId) {
});
}
function showContainerTabs(containerId) {
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
function showContainerTabs(userContextId) {
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
browser.runtime.sendMessage({
method: 'showTab',
cookieStoreId: containerId
userContextId: userContextId
}).then(hiddenTabUrls=> {
hiddenTabUrls.forEach(url=> {
browser.runtime.sendMessage({
method: 'openTab',
cookieStoreId: containerId,
userContextId: userContextId,
url: url
});
});
@ -84,7 +84,7 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
}
const identityRow = `
<tr data-identity-cookie-store-id="${identity.cookieStoreId}" >
<tr data-identity-cookie-store-id="${identity.userContextId}" >
<td>
<div class="userContext-icon"
data-identity-icon="${identity.icon}"
@ -101,8 +101,8 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
<td class="hideorshow" >
<img
title="Hide or show ${identity.name} container tabs"
data-identity-cookie-store-id="${identity.cookieStoreId}"
id="${identity.cookieStoreId}-hideorshow-icon"
data-identity-cookie-store-id="${identity.userContextId}"
id="uci-${identity.userContextId}-hideorshow-icon"
class="icon hideorshow-icon"
src="${hideOrShowIconSrc}"
/>
@ -117,18 +117,18 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
rows.forEach(row=> {
row.addEventListener('click', e=> {
const containerId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
const userContextId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
if (e.target.matches('.hideorshow-icon')) {
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
if (identitiesState[containerId].hiddenTabUrls.length) {
showContainerTabs(containerId);
if (identitiesState[userContextId].hiddenTabUrls.length) {
showContainerTabs(userContextId);
} else {
hideContainerTabs(containerId);
hideContainerTabs(userContextId);
}
});
} else if (e.target.matches('.newtab-icon')) {
browser.runtime.sendMessage({method: 'openTab', cookieStoreId: containerId});
browser.runtime.sendMessage({method: 'openTab', userContextId: userContextId});
window.close();
}
});
@ -152,14 +152,14 @@ function moveTabs(sortedTabsArray) {
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
identities.unshift({cookieStoreId: 'firefox-default'});
identities.unshift({userContextId: 0});
browser.runtime.sendMessage({method: 'queryTabs'}).then(tabsArray=> {
const sortedTabsArray = [];
identities.forEach(identity=> {
tabsArray.forEach(tab=> {
if (tab.cookieStoreId === identity.cookieStoreId) {
if (tab.userContextId === identity.userContextId) {
sortedTabsArray.push(tab.id);
}
});