Skip to content

Commit

Permalink
More extensive MAC tests with preferences-combinations
Browse files Browse the repository at this point in the history
Part of #29
  • Loading branch information
stoically committed Feb 23, 2018
1 parent 6e6d4ed commit 3733e42
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"eslint": "^4.11.0",
"mocha": "^4.0.1",
"require-reload": "^0.2.2",
"sinon": "^4.1.2",
"sinon": "^4.4.0",
"sinon-chai": "^2.14.0",
"web-ext": "^2.2.2",
"webpack": "^3.10.0"
Expand Down
24 changes: 16 additions & 8 deletions src/background/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class MultiAccountContainers {
targetContainer: queryParams[1][1],
currentContainer: queryParams[2] ? queryParams[2][1] : false
};
debug('[handleConfirmPage] parsed url', parsedURL, queryParams, confirmPage);
debug('[handleConfirmPage] parsed url', queryParams, confirmPage);
if (this.waitingForConfirmPage[confirmPage.targetContainer]) {
debug('[handleConfirmPage] we are already waiting for this confirm page, maybe reopen', confirmPage.targetContainer);
this._maybeReopenConfirmPage(this.waitingForConfirmPage[confirmPage.targetContainer]);
this._maybeReopenConfirmPage(this.waitingForConfirmPage[confirmPage.targetContainer], confirmPage);
} else {
debug('[handleConfirmPage] we remember that we saw this confirm page, maybe it needs to be reopened', confirmPage.targetContainer);
this.confirmPage[confirmPage.targetContainer] = confirmPage;
Expand All @@ -47,7 +47,7 @@ class MultiAccountContainers {
debug('[maybeReopenConfirmPage] we saw a mac confirm page for the target container already', targetContainer);
this._maybeReopenConfirmPage({targetContainer, request, tab, deletesHistoryContainer});
} else {
debug('[maybeReopenConfirmPage] we didnt saw a mac confirm page yet, waiting', targetContainer);
debug('[maybeReopenConfirmPage] we didnt saw a mac confirm page yet, waiting', targetContainer, tab);
this.waitingForConfirmPage[targetContainer] = {targetContainer, request, tab, deletesHistoryContainer};
delay(2000).then(() => {
debug('[maybeReopenConfirmPage] cleaning up', targetContainer);
Expand All @@ -56,14 +56,22 @@ class MultiAccountContainers {
}
}

_maybeReopenConfirmPage({targetContainer, request, tab, deletesHistoryContainer}) {
_maybeReopenConfirmPage({targetContainer, request, tab, deletesHistoryContainer}, confirmPage) {
debug('[_maybeReopenConfirmPage]', targetContainer, request, tab, deletesHistoryContainer);
if (this.waitingForConfirmPage[targetContainer]) {
delete this.waitingForConfirmPage[targetContainer];
}
const currentContainer = this.confirmPage[targetContainer].currentContainer;
if (!confirmPage) {
confirmPage = this.confirmPage[targetContainer];
}
if (!confirmPage) {
debug('[_maybeReopenConfirmPage] something went wrong, aborting');
return;
}
const currentContainer = confirmPage.currentContainer;
if (currentContainer) {
if (this.storage.local.tempContainers[currentContainer].clean) {
if (this.storage.local.tempContainers[currentContainer] &&
this.storage.local.tempContainers[currentContainer].clean) {
debug('[_maybeReopenConfirmPage] the currentContainer mac confirm wants to open is a clean tmp container, we do nothing');
return;
} else {
Expand All @@ -74,9 +82,9 @@ class MultiAccountContainers {
}

this.container.reloadTabInTempContainer(
this.confirmPage[targetContainer].tab,
confirmPage.tab ? confirmPage.tab : undefined,
request.url,
this.confirmPage[targetContainer].tab.active,
confirmPage.tab ? confirmPage.tab.active : undefined,
deletesHistoryContainer,
request
);
Expand Down
11 changes: 10 additions & 1 deletion src/background/mouseclick.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { debug } = require('./log');
class MouseClick {
constructor() {
this.linksClicked = {};
this.unhandledLinksClicked = {};
}


Expand Down Expand Up @@ -37,9 +38,17 @@ class MouseClick {
clickType = 'ctrlleft';
}
if (!this.checkClick(clickType, message, sender)) {
this.unhandledLinksClicked[url] = {};
setTimeout(() => {
debug('[linkClicked] cleaning up unhandledLinksClicked', url);
delete this.unhandledLinksClicked[url];
}, 1000);
return;
}
}
if (this.unhandledLinksClicked[url]) {
delete this.unhandledLinksClicked[url];
}
const tab = sender.tab;

if (!this.linksClicked[url]) {
Expand All @@ -56,7 +65,7 @@ class MouseClick {
this.linksClicked[url].count++;

setTimeout(() => {
debug('[runtimeOnMessage] cleaning up', url);
debug('[linkClicked] cleaning up linksClicked', url);
delete this.linksClicked[url];
delete this.container.urlCreatedContainer[url];
}, 1000);
Expand Down
108 changes: 59 additions & 49 deletions src/background/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ class Request {


async webRequestOnBeforeRequest(request) {
const returnVal = await this._webRequestOnBeforeRequest(request);
if (returnVal && returnVal.cancel) {
this.cancelRequest(request);
} else {
const cookieStoreId = this.storage.local.tabContainerMap[request.tabId];
if (cookieStoreId && this.storage.local.tempContainers[cookieStoreId] &&
this.storage.local.tempContainers[cookieStoreId].clean) {
debug('[webRequestOnBeforeRequest] marking tmp container as not clean anymore', cookieStoreId, request);
this.storage.local.tempContainers[cookieStoreId].clean = false;
}
}
return returnVal;
}


async _webRequestOnBeforeRequest(request) {
debug('[browser.webRequest.onBeforeRequest] incoming request', request);
if (request.tabId === -1) {
debug('[browser.webRequest.onBeforeRequest] onBeforeRequest request doesnt belong to a tab, why are you main_frame?', request);
Expand Down Expand Up @@ -77,27 +93,27 @@ class Request {
return;
}

let tab = {
id: request.tabId,
cookieStoreId: 'firefox-default'
};
let tab;
if (macAssignment) {
if (macAssignment.neverAsk) {
debug('[webRequestOnBeforeRequest] mac neverask assigned, we do nothing', macAssignment);
return;
} else {
debug('[webRequestOnBeforeRequest] mac assigned', macAssignment);
// we just assume that we cant get the tab informations anymore
// so we dont even try
}
}
try {
tab = await browser.tabs.get(request.tabId);
debug('[webRequestOnBeforeRequest] onbeforeRequest requested tab information', tab);
if (macAssignment && tab.cookieStoreId === macAssignment.cookieStoreId) {
debug('[webRequestOnBeforeRequest] the request url is mac assigned to this container, we do nothing');
return;
} else {
try {
tab = await browser.tabs.get(request.tabId);
debug('[webRequestOnBeforeRequest] onbeforeRequest requested tab information', tab);
if (macAssignment && tab.cookieStoreId === macAssignment.cookieStoreId) {
debug('[webRequestOnBeforeRequest] the request url is mac assigned to this container, we do nothing');
return;
}
} catch (error) {
debug('[webRequestOnBeforeRequest] onbeforeRequest retrieving tab information failed, mac was probably faster', error);
}
} catch (error) {
debug('[webRequestOnBeforeRequest] onbeforeRequest retrieving tab information failed, mac was probably faster', error);
}

this.container.maybeAddHistory(tab, request.url);
Expand All @@ -110,9 +126,12 @@ class Request {
!this.mouseclick.linksClicked[request.url]) {
debug('[webRequestOnBeforeRequest] automatic mode disabled and no link clicked', request);
return;
} else if (this.mouseclick.unhandledLinksClicked[request.url]) {
debug('[webRequestOnBeforeRequest] we saw an unhandled click for that url', request);
return;
}

if (alwaysOpenIn && !this.mouseclick.linksClicked[request.url] && tab.openerTabId) {
if (alwaysOpenIn && !this.mouseclick.linksClicked[request.url] && tab && tab.openerTabId) {
// TODO probably macLegacy-related
debug('[webRequestOnBeforeRequest] always open in tmpcontainer request, simulating click', request);
this.linkClicked(request.url, {
Expand All @@ -121,40 +140,28 @@ class Request {
});
}

if (tab.incognito) {
if (tab && tab.incognito) {
debug('[webRequestOnBeforeRequest] tab is incognito, ignore it', tab);
return;
}

if (!alwaysOpenIn && this.background.noContainerTabs[tab.id]) {
if (!alwaysOpenIn && this.background.noContainerTabs[request.tabId]) {
debug('[webRequestOnBeforeRequest] no container tab, we ignore that', tab);
return;
}

let returnVal;
if (this.mouseclick.linksClicked[request.url]) {
returnVal = await this.handleClickedLink(request, tab, macAssignment);
return this.handleClickedLink(request, tab, macAssignment);
} else {
returnVal = await this.handleNotClickedLink(request, tab, alwaysOpenIn, macAssignment);
return this.handleNotClickedLink(request, tab, alwaysOpenIn, macAssignment);
}

if (returnVal && returnVal.cancel) {
this.cancelRequest(request);
} else {
if (this.storage.local.tempContainers[tab.cookieStoreId] &&
this.storage.local.tempContainers[tab.cookieStoreId].clean) {
debug('[webRequestOnBeforeRequest] marking tmp container as not clean anymore', tab);
this.storage.local.tempContainers[tab.cookieStoreId].clean = false;
}
}
return returnVal;
}


async handleClickedLink(request, tab, macAssignment) {
debug('[handleClickedLink] onBeforeRequest', request, tab);

if (tab.cookieStoreId !== 'firefox-default' &&
if (tab && tab.cookieStoreId !== 'firefox-default' &&
this.container.urlCreatedContainer[request.url] === tab.cookieStoreId) {
debug('[handleClickedLink] link click already created this container, we can stop here', request, tab);
return;
Expand All @@ -166,7 +173,8 @@ class Request {
}

let deletesHistoryContainer = false;
if (this.storage.local.tempContainers[tab.cookieStoreId] &&
if (tab &&
this.storage.local.tempContainers[tab.cookieStoreId] &&
this.storage.local.tempContainers[tab.cookieStoreId].deletesHistory &&
this.storage.local.preferences.deletesHistoryContainerMouseClicks === 'automatic') {
deletesHistoryContainer = true;
Expand All @@ -183,7 +191,7 @@ class Request {
this.mouseclick.linksClicked[request.url].clickType === 'left') {
debug('[handleClickedLink] creating new container because request got left clicked', this.mouseclick.linksClicked[request.url], tab);
newTab = await this.container.createTabInTempContainer({tab, active: true, url: request.url, deletesHistory: deletesHistoryContainer, request});
if (this.mouseclick.linksClicked[request.url].tab.id !== tab.id) {
if (tab && this.mouseclick.linksClicked[request.url].tab.id !== tab.id) {
debug('[handleClickedLink] looks like the left clicked opened a new tab, remove it', tab);
await this.container.removeTab(tab);
}
Expand All @@ -197,7 +205,7 @@ class Request {


async handleNotClickedLink(request, tab, alwaysOpenIn, macAssignment) {
if (tab.cookieStoreId === 'firefox-default' && tab.openerTabId && !alwaysOpenIn) {
if (tab && tab.cookieStoreId === 'firefox-default' && tab.openerTabId && !alwaysOpenIn) {
debug('[webRequestOnBeforeRequest] default container and openerTabId set', tab);
const openerTab = await browser.tabs.get(tab.openerTabId);
if (!openerTab.url.startsWith('about:') && !openerTab.url.startsWith('moz-extension:')) {
Expand All @@ -210,22 +218,24 @@ class Request {
return;
}

let containerExists = false;
if (tab.cookieStoreId === 'firefox-default') {
containerExists = true;
} else {
try {
containerExists = await browser.contextualIdentities.get(tab.cookieStoreId);
} catch (error) {
debug('[handleNotClickedLink] container doesnt exist anymore, probably undo close tab', tab);
if (!macAssignment) {
let containerExists = false;
if (tab.cookieStoreId === 'firefox-default') {
containerExists = true;
} else {
try {
containerExists = await browser.contextualIdentities.get(tab.cookieStoreId);
} catch (error) {
debug('[handleNotClickedLink] container doesnt exist anymore', tab);
}
}
}
if (tab.cookieStoreId !== 'firefox-default' && containerExists) {
if (this.shouldCancelRequest(request)) {
return { cancel: true };
if (tab && tab.cookieStoreId !== 'firefox-default' && containerExists) {
if (this.shouldCancelRequest(request)) {
return { cancel: true };
}
debug('[handleNotClickedLink] onBeforeRequest tab belongs to a non-default container', tab, request);
return;
}
debug('[handleNotClickedLink] onBeforeRequest tab belongs to a non-default container', tab, request);
return;
}

if (this.cancelRequest(request)) {
Expand All @@ -240,7 +250,7 @@ class Request {
if (macAssignment) {
debug('[handleNotClickedLink] decided to reopen but mac assigned, reopen confirmpage', request, tab, macAssignment);
this.mac.maybeReopenConfirmPage(macAssignment, request, tab, deletesHistoryContainer);
return;
return { cancel: true };
}

debug('[handleNotClickedLink] onBeforeRequest reload in temp tab', tab, request);
Expand Down
Loading

0 comments on commit 3733e42

Please sign in to comment.