Bug 1692124 - Guess the userContextId for externally opened URLs;r=mossop
Differential Revision: https://phabricator.services.mozilla.com/D154812
This commit is contained in:
@@ -86,6 +86,7 @@ ChromeUtils.defineESModuleGetters(this, {
|
||||
TranslationsParent: "resource://gre/actors/TranslationsParent.sys.mjs",
|
||||
UITour: "resource:///modules/UITour.sys.mjs",
|
||||
UpdateUtils: "resource://gre/modules/UpdateUtils.sys.mjs",
|
||||
URILoadingHelper: "resource:///modules/URILoadingHelper.sys.mjs",
|
||||
UrlbarInput: "resource:///modules/UrlbarInput.sys.mjs",
|
||||
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
|
||||
UrlbarProviderSearchTips:
|
||||
@@ -6066,6 +6067,9 @@ nsBrowserAccess.prototype = {
|
||||
) {
|
||||
var browsingContext = null;
|
||||
var isExternal = !!(aFlags & Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL);
|
||||
var openingUserContextId =
|
||||
(isExternal && URILoadingHelper.guessUserContextId(aURI)) ||
|
||||
Ci.nsIScriptSecurityManager.DEFAULT_USER_CONTEXT_ID;
|
||||
|
||||
if (aOpenWindowInfo && isExternal) {
|
||||
console.error(
|
||||
@@ -6171,7 +6175,7 @@ nsBrowserAccess.prototype = {
|
||||
let forceNotRemote = aOpenWindowInfo && !aOpenWindowInfo.isRemote;
|
||||
let userContextId = aOpenWindowInfo
|
||||
? aOpenWindowInfo.originAttributes.userContextId
|
||||
: Ci.nsIScriptSecurityManager.DEFAULT_USER_CONTEXT_ID;
|
||||
: openingUserContextId;
|
||||
let browser = this._openURIInNewTab(
|
||||
aURI,
|
||||
referrerInfo,
|
||||
|
||||
@@ -38,6 +38,8 @@ skip-if = ["true"] # Bug 1541885
|
||||
["browser_imageCache.js"]
|
||||
skip-if = ["verify && debug && os == 'win'"]
|
||||
|
||||
["browser_guessusercontext.js"]
|
||||
|
||||
["browser_middleClick.js"]
|
||||
skip-if = [
|
||||
"verify && debug && os == 'linux'",
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
const PERSONAL = 1;
|
||||
const WORK = 2;
|
||||
const HOST_MOCHI = makeURI(
|
||||
"http://mochi.test:8888/browser/browser/components/contextualidentity/test/browser/blank.html"
|
||||
);
|
||||
const HOST_EXAMPLE = makeURI(
|
||||
"https://example.com/browser/browser/components/contextualidentity/test/browser/blank.html"
|
||||
);
|
||||
|
||||
const {
|
||||
URILoadingHelper: { guessUserContextId },
|
||||
} = ChromeUtils.importESModule("resource:///modules/URILoadingHelper.sys.mjs");
|
||||
|
||||
async function openTabInUserContext(uri, userContextId, win = window) {
|
||||
let { gBrowser } = win;
|
||||
let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });
|
||||
gBrowser.selectedTab = tab;
|
||||
tab.ownerGlobal.focus();
|
||||
await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));
|
||||
return tab;
|
||||
}
|
||||
|
||||
registerCleanupFunction(async function cleanup() {
|
||||
while (gBrowser.tabs.length > 1) {
|
||||
gBrowser.removeTab(gBrowser.selectedTab, { animate: false });
|
||||
}
|
||||
});
|
||||
|
||||
add_setup(async function () {
|
||||
await SpecialPowers.pushPrefEnv({
|
||||
set: [["privacy.userContext.enabled", true]],
|
||||
});
|
||||
});
|
||||
|
||||
add_task(async function test() {
|
||||
is(guessUserContextId(null), null, "invalid uri - null");
|
||||
is(guessUserContextId(HOST_EXAMPLE), null, "no tabs - null");
|
||||
await openTabInUserContext(HOST_EXAMPLE.spec, PERSONAL);
|
||||
is(guessUserContextId(HOST_EXAMPLE), PERSONAL, "one tab - matches container");
|
||||
is(guessUserContextId(HOST_MOCHI), null, "one tab - doesn't match container");
|
||||
await openTabInUserContext(HOST_EXAMPLE.spec, WORK);
|
||||
is(guessUserContextId(HOST_EXAMPLE), PERSONAL, "same number - use first");
|
||||
await openTabInUserContext(HOST_EXAMPLE.spec, WORK);
|
||||
is(guessUserContextId(HOST_EXAMPLE), WORK, "multiple per host - max");
|
||||
|
||||
let win = await BrowserTestUtils.openNewBrowserWindow();
|
||||
await openTabInUserContext(HOST_EXAMPLE.spec, PERSONAL, win);
|
||||
await openTabInUserContext(HOST_EXAMPLE.spec, PERSONAL, win);
|
||||
is(guessUserContextId(HOST_EXAMPLE), PERSONAL, "count across windows");
|
||||
|
||||
await BrowserTestUtils.closeWindow(win);
|
||||
is(guessUserContextId(HOST_EXAMPLE), WORK, "forgets closed window");
|
||||
|
||||
// Check the opener flow more directly
|
||||
let browsingContext = window.browserDOMWindow.openURI(
|
||||
makeURI(HOST_EXAMPLE.spec + "?new"),
|
||||
null,
|
||||
Ci.nsIBrowserDOMWindow.OPEN_NEWTAB,
|
||||
Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL,
|
||||
Services.scriptSecurityManager.getSystemPrincipal()
|
||||
);
|
||||
await BrowserTestUtils.browserLoaded(browsingContext.embedderElement);
|
||||
is(
|
||||
browsingContext.embedderElement,
|
||||
gBrowser.selectedBrowser,
|
||||
"opener selected"
|
||||
);
|
||||
is(
|
||||
gBrowser.selectedTab.getAttribute("usercontextid"),
|
||||
WORK.toString(),
|
||||
"opener flow"
|
||||
);
|
||||
is(guessUserContextId(HOST_EXAMPLE), WORK, "still the most common");
|
||||
is(guessUserContextId(HOST_MOCHI), null, "still doesn't match container");
|
||||
});
|
||||
@@ -735,4 +735,50 @@ export const URILoadingHelper = {
|
||||
params.forceForeground ??= true;
|
||||
this.openLinkIn(window, url, where, params);
|
||||
},
|
||||
|
||||
/**
|
||||
* Given a URI, guess which container to use to open it. This is used for external
|
||||
* openers as a quality of life improvement (e.g. to open a document into the container
|
||||
* where you are logged in to the service that hosts it).
|
||||
* matches will be returned.
|
||||
* For now this can only use currently-open tabs, until history is tagged with the
|
||||
* container id (https://bugzilla.mozilla.org/show_bug.cgi?id=1283320).
|
||||
*
|
||||
* @param {nsIURI} aURI - The URI being opened.
|
||||
* @returns {number | null} The guessed userContextId, or null if none.
|
||||
*/
|
||||
guessUserContextId(aURI) {
|
||||
let host;
|
||||
try {
|
||||
host = aURI.host;
|
||||
} catch (e) {}
|
||||
if (!host) {
|
||||
return null;
|
||||
}
|
||||
const containerScores = new Map();
|
||||
let guessedUserContextId = null;
|
||||
let maxCount = 0;
|
||||
for (let win of lazy.BrowserWindowTracker.orderedWindows) {
|
||||
for (let tab of win.gBrowser.visibleTabs) {
|
||||
let { userContextId } = tab;
|
||||
if (userContextId) {
|
||||
let currentURIHost = null;
|
||||
try {
|
||||
currentURIHost = tab.linkedBrowser.currentURI.host;
|
||||
} catch (e) {}
|
||||
|
||||
if (currentURIHost == host) {
|
||||
let count = (containerScores.get(userContextId) ?? 0) + 1;
|
||||
containerScores.set(userContextId, count);
|
||||
if (count > maxCount) {
|
||||
guessedUserContextId = userContextId;
|
||||
maxCount = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return guessedUserContextId;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user