Query() moved to the parent process

This commit is contained in:
baku 2017-01-06 18:52:35 +01:00
parent 0b03b67f65
commit 404ee3304e
2 changed files with 55 additions and 3 deletions

View file

@ -6,6 +6,7 @@ Cu.import("resource://gre/modules/Services.jsm");
const tabs = require('sdk/tabs'); const tabs = require('sdk/tabs');
const webExtension = require('sdk/webextension'); const webExtension = require('sdk/webextension');
const { viewFor } = require("sdk/view/core");
/* Let's start enabling Containers */ /* Let's start enabling Containers */
var prefs = [ var prefs = [
@ -26,6 +27,10 @@ const identitiesState = {
}; };
function getCookieStoreIdForContainer(containerId) { function getCookieStoreIdForContainer(containerId) {
if (containerId == 0) {
return 'firefox-default';
}
return CONTAINER_STORE + containerId; return CONTAINER_STORE + containerId;
} }
@ -189,11 +194,48 @@ function openTab(args) {
return Promise.resolve(true); return Promise.resolve(true);
} }
function queryTabs(args) {
return new Promise((resolve, reject) => {
let tabList = [];
for (let tab of tabs) {
let xulTab = viewFor(tab);
let userContextId = xulTab.getAttribute('usercontextid') || 0;
let cookieStoreId = getCookieStoreIdForContainer(userContextId);
if ("cookieStoreId" in args && args.cookieStoreId != cookieStoreId) {
continue;
}
tabList.push({
id: tab.id,
url: tab.url,
cookieStoreId: cookieStoreId,
});
}
resolve(tabList);
});
}
function removeTabs(ids) {
for (let tab of tabs) {
if (ids.indexOf(tab.id) != -1) {
tab.close();
}
}
return Promise.resolve(null);
}
function handleWebExtensionMessage(message, sender, sendReply) { function handleWebExtensionMessage(message, sender, sendReply) {
switch (message.method) { switch (message.method) {
case 'queryIdentities': case 'queryIdentities':
sendReply(contextualIdentities.query(message.arguments)); sendReply(contextualIdentities.query(message.arguments));
break; break;
case 'queryTabs':
sendReply(queryTabs(message));
break;
case 'hideTab': case 'hideTab':
identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave; identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave;
break; break;
@ -201,6 +243,10 @@ function handleWebExtensionMessage(message, sender, sendReply) {
sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls); sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls);
identitiesState[message.cookieStoreId].hiddenTabUrls = []; identitiesState[message.cookieStoreId].hiddenTabUrls = [];
break; break;
case 'removeTabs':
sendReply(removeTabs(message.tabIds));
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
break;
case 'getTab': case 'getTab':
sendReply(contextualIdentities.get(message.arguments)); sendReply(contextualIdentities.get(message.arguments));
break; break;

View file

@ -7,7 +7,10 @@ function hideContainerTabs(containerId) {
const tabUrlsToSave = []; const tabUrlsToSave = [];
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`); const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
browser.tabs.query({cookieStoreId: containerId}).then(tabs=> { browser.runtime.sendMessage({
method: 'queryTabs',
cookieStoreId: containerId
}).then(tabs=> {
tabs.forEach(tab=> { tabs.forEach(tab=> {
tabIdsToRemove.push(tab.id); tabIdsToRemove.push(tab.id);
tabUrlsToSave.push(tab.url); tabUrlsToSave.push(tab.url);
@ -17,7 +20,10 @@ function hideContainerTabs(containerId) {
cookieStoreId: containerId, cookieStoreId: containerId,
tabUrlsToSave: tabUrlsToSave tabUrlsToSave: tabUrlsToSave
}).then(()=> { }).then(()=> {
browser.tabs.remove(tabIdsToRemove); browser.runtime.sendMessage({
method: 'removeTabs',
tabIds: tabIdsToRemove
});
hideorshowIcon.src = CONTAINER_UNHIDE_SRC; hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
}); });
}); });
@ -148,7 +154,7 @@ document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> { browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
identities.unshift({cookieStoreId: 'firefox-default'}); identities.unshift({cookieStoreId: 'firefox-default'});
browser.tabs.query({}).then(tabsArray=> { browser.runtime.sendMessage({method: 'queryTabs'}).then(tabsArray=> {
const sortedTabsArray = []; const sortedTabsArray = [];
identities.forEach(identity=> { identities.forEach(identity=> {