Skip to content

Commit

Permalink
Detect group tab generated in different session #1762
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Feb 7, 2018
1 parent a252956 commit f7b67af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 44 additions & 1 deletion webextensions/common/tree/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,34 @@ function updateTab(aTab, aNewState = {}, aOptions = {}) {
url: aOptions.tab.url.replace(kSHORTHAND_ABOUT_URI, kSHORTHAND_URIS[shorthand] || 'about:blank')
}).catch(handleMissingTabError);
aTab.classList.add(kTAB_STATE_GROUP_TAB);
addSpecialTabState(aTab, kTAB_STATE_GROUP_TAB);
return;
}
else if ('url' in aNewState &&
aNewState.url.indexOf(kGROUP_TAB_URI) == 0) {
aTab.classList.add(kTAB_STATE_GROUP_TAB);
addSpecialTabState(aTab, kTAB_STATE_GROUP_TAB);
window.onGroupTabDetected && onGroupTabDetected(aTab);
}
else if (aTab.apiTab &&
aTab.apiTab.url.indexOf(kGROUP_TAB_URI) != 0) {
aTab.classList.remove(kTAB_STATE_GROUP_TAB);
// Detect group tab from different session - which can have different UUID for the URL.
getSpecialTabState(aTab).then(aStates => {
const PREFIX_REMOVER = /^moz-extension:\/\/[^\/]+/;
const pathPart = aTab.apiTab.url.replace(PREFIX_REMOVER, '');
if (aStates.indexOf(kTAB_STATE_GROUP_TAB) > -1 &&
pathPart == kGROUP_TAB_URI.replace(PREFIX_REMOVER, '')) {
const parameters = pathPart.replace(/^[^\?]+\?/, '');
browser.tabs.update(aTab.apiTab.id, {
url: `${kGROUP_TAB_URI}?${parameters}`
}).catch(handleMissingTabError);
aTab.classList.add(kTAB_STATE_GROUP_TAB);
}
else {
removeSpecialTabState(aTab, kTAB_STATE_GROUP_TAB);
aTab.classList.remove(kTAB_STATE_GROUP_TAB);
}
});
}

if (aOptions.forceApply ||
Expand Down Expand Up @@ -1000,6 +1018,31 @@ function hideTabs(aTabs = []) {
}


async function getSpecialTabState(aTab) {
const states = await browser.sessions.getTabValue(aTab.apiTab.id, kPERSISTENT_SPECIAL_TAB_STATES);
return states || [];
}

async function addSpecialTabState(aTab, aState) {
const states = await getSpecialTabState(aTab);
if (states.indexOf(aState) > -1)
return states;
states.push(aState);
await browser.sessions.setTabValue(aTab.apiTab.id, kPERSISTENT_SPECIAL_TAB_STATES, states);
return states;
}

async function removeSpecialTabState(aTab, aState) {
const states = await getSpecialTabState(aTab);
const index = states.indexOf(aState);
if (index < 0)
return states;
states.splice(index, 1);
await browser.sessions.setTabValue(aTab.apiTab.id, kPERSISTENT_SPECIAL_TAB_STATES, states);
return states;
}


/* TST API Helpers */

function serializeTabForTSTAPI(aTab) {
Expand Down
1 change: 1 addition & 0 deletions webextensions/common/tree/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const kPERSISTENT_CHILDREN = 'children';
const kPERSISTENT_INSERT_BEFORE = 'insert-before';
const kPERSISTENT_INSERT_AFTER = 'isnert-after';
const kPERSISTENT_SUBTREE_COLLAPSED = 'subtree-collapsed';
const kPERSISTENT_SPECIAL_TAB_STATES = 'special-tab-states';

const kFAVICON = 'favicon';
const kFAVICON_IMAGE = 'favicon-image';
Expand Down

0 comments on commit f7b67af

Please sign in to comment.