Bug 1081542 - Part 1: Add nsIBrowserDOMWindow.OPEN_NEWTAB_FOREGROUND. r=smaug,geckoview-reviewers,Gijs,amejiamarmol,tabbrowser-reviewers,dao

Given meta/ctrl key should use browser.tabs.loadInBackground pref instead of
browser.tabs.loadDivertedInBackground pref, the existing
nsIBrowserDOMWindow.OPEN_NEWTAB behavior doesn't match the requirement.

Add dedicate flag for "open in foreground tab" to make it controllable from
nsWindowWatcher::IsWindowOpenLocationModified.

Differential Revision: https://phabricator.services.mozilla.com/D201929
This commit is contained in:
Tooru Fujisawa 2024-07-10 06:16:52 +00:00
parent d9b74ea8da
commit a2325ca474
8 changed files with 41 additions and 20 deletions

View File

@ -4409,7 +4409,7 @@ nsBrowserAccess.prototype = {
aName = "",
aCsp = null,
aSkipLoad = false,
aForceLoadInBackground = false
aWhere = undefined
) {
let win, needToFocusWin;
@ -4432,11 +4432,20 @@ nsBrowserAccess.prototype = {
return win.gBrowser.selectedBrowser;
}
let loadInBackground = Services.prefs.getBoolPref(
// OPEN_NEWTAB_BACKGROUND and OPEN_NEWTAB_FOREGROUND are used by
// `window.open` with modifiers.
// The last case is OPEN_NEWTAB, which is used by:
// * a link with `target="_blank"`, without modifiers
// * `window.open` without features, without modifiers
let loadInBackground;
if (aWhere === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND) {
loadInBackground = true;
} else if (aWhere === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_FOREGROUND) {
loadInBackground = false;
} else {
loadInBackground = Services.prefs.getBoolPref(
"browser.tabs.loadDivertedInBackground"
);
if (aForceLoadInBackground) {
loadInBackground = true;
}
let tab = win.gBrowser.addTab(aURI ? aURI.spec : "about:blank", {
@ -4614,7 +4623,8 @@ nsBrowserAccess.prototype = {
break;
}
case Ci.nsIBrowserDOMWindow.OPEN_NEWTAB:
case Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND: {
case Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND:
case Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_FOREGROUND: {
// If we have an opener, that means that the caller is expecting access
// to the nsIDOMWindow of the opened tab right away. For e10s windows,
// this means forcing the newly opened browser to be non-remote so that
@ -4625,8 +4635,6 @@ nsBrowserAccess.prototype = {
let userContextId = aOpenWindowInfo
? aOpenWindowInfo.originAttributes.userContextId
: openingUserContextId;
let forceLoadInBackground =
aWhere == Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND;
let browser = this._openURIInNewTab(
aURI,
referrerInfo,
@ -4640,7 +4648,7 @@ nsBrowserAccess.prototype = {
"",
aCsp,
aSkipLoad,
forceLoadInBackground
aWhere
);
if (browser) {
browsingContext = browser.browsingContext;
@ -4741,7 +4749,8 @@ nsBrowserAccess.prototype = {
if (
aWhere != Ci.nsIBrowserDOMWindow.OPEN_NEWTAB &&
aWhere != Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND
aWhere != Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND &&
aWhere != Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_FOREGROUND
) {
dump("Error: openURIInFrame can only open in new tabs or print");
return null;
@ -4755,9 +4764,6 @@ nsBrowserAccess.prototype = {
? aParams.openerOriginAttributes.userContextId
: Ci.nsIScriptSecurityManager.DEFAULT_USER_CONTEXT_ID;
var forceLoadInBackground =
aWhere == Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND;
return this._openURIInNewTab(
aURI,
aParams.referrerInfo,
@ -4771,7 +4777,7 @@ nsBrowserAccess.prototype = {
aName,
aParams.csp,
aSkipLoad,
forceLoadInBackground
aWhere
);
},

View File

@ -11,6 +11,7 @@ add_task(async function () {
const tests = [
["OPEN_NEWTAB", false],
["OPEN_NEWTAB_BACKGROUND", true],
["OPEN_NEWTAB_FOREGROUND", false],
];
for (const [flag, isBackground] of tests) {

View File

@ -65,6 +65,8 @@ interface nsIBrowserDOMWindow : nsISupports
/**
* Open in a new content tab in the toplevel browser window corresponding to
* this nsIBrowserDOMWindow.
* Use browser.tabs.loadDivertedInBackground pref to choose whether to open
* in background tab or foreground tab.
*/
const short OPEN_NEWTAB = 3;
/**
@ -76,6 +78,11 @@ interface nsIBrowserDOMWindow : nsISupports
* corresponding to this nsIBrowserDOMWindow.
*/
const short OPEN_NEWTAB_BACKGROUND = 5;
/**
* Open in a new foreground content tab in the toplevel browser window
* corresponding to this nsIBrowserDOMWindow.
*/
const short OPEN_NEWTAB_FOREGROUND = 6;
/**
* Values for createContentWindow's and openURI's aFlags parameter.

View File

@ -5317,6 +5317,7 @@ mozilla::ipc::IPCResult ContentParent::CommonCreateWindow(
MOZ_ASSERT(aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWWINDOW ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_PRINT_BROWSER);
@ -5327,6 +5328,7 @@ mozilla::ipc::IPCResult ContentParent::CommonCreateWindow(
if (aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND ||
aOpenLocation == nsIBrowserDOMWindow::OPEN_PRINT_BROWSER) {
RefPtr<Element> openerElement = do_QueryObject(frame);
@ -5558,7 +5560,8 @@ mozilla::ipc::IPCResult ContentParent::RecvCreateWindow(
// do this work.
MOZ_ALWAYS_SUCCEEDS(newBC->SetHasSiblings(
openLocation == nsIBrowserDOMWindow::OPEN_NEWTAB ||
openLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND));
openLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND ||
openLocation == nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND));
newTab->SwapFrameScriptsFrom(cwi.frameScripts());
newTab->MaybeShowFrame();

View File

@ -650,7 +650,7 @@ public class GeckoSession {
case 0: // OPEN_DEFAULTWINDOW
case 1: // OPEN_CURRENTWINDOW
return NavigationDelegate.TARGET_WINDOW_CURRENT;
default: // OPEN_NEWWINDOW, OPEN_NEWTAB, OPEN_NEWTAB_BACKGROUND
default: // OPEN_NEWWINDOW, OPEN_NEWTAB, OPEN_NEWTAB_BACKGROUND, OPEN_NEWTAB_FOREGROUND
return NavigationDelegate.TARGET_WINDOW_NEW;
}
}
@ -5027,7 +5027,7 @@ public class GeckoSession {
case 0: // OPEN_DEFAULTWINDOW
case 1: // OPEN_CURRENTWINDOW
return TARGET_WINDOW_CURRENT;
default: // OPEN_NEWWINDOW, OPEN_NEWTAB, OPEN_NEWTAB_BACKGROUND
default: // OPEN_NEWWINDOW, OPEN_NEWTAB, OPEN_NEWTAB_BACKGROUND, OPEN_NEWTAB_FOREGROUND
return TARGET_WINDOW_NEW;
}
}

View File

@ -556,7 +556,8 @@ export class GeckoViewNavigation extends GeckoViewModule {
if (
where === Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW ||
where === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB ||
where === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND
where === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_BACKGROUND ||
where === Ci.nsIBrowserDOMWindow.OPEN_NEWTAB_FOREGROUND
) {
browser = this.handleNewSession(uri, openWindowInfo, where, flags, name);
}

View File

@ -1692,8 +1692,9 @@ static int32_t GetWhereToOpen(nsIChannel* aChannel, bool aIsDocumentLoad) {
where == nsIBrowserDOMWindow::OPEN_NEWTAB) {
return where;
}
// NOTE: nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND is not allowed as a pref
// value.
// NOTE: nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND and
// nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND are not allowed as pref
// values.
return nsIBrowserDOMWindow::OPEN_NEWTAB;
}
@ -1786,6 +1787,7 @@ static RefPtr<dom::BrowsingContextCallbackReceivedPromise> SwitchToNewTab(
CanonicalBrowsingContext* aLoadingBrowsingContext, int32_t aWhere) {
MOZ_ASSERT(aWhere == nsIBrowserDOMWindow::OPEN_NEWTAB ||
aWhere == nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND ||
aWhere == nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND ||
aWhere == nsIBrowserDOMWindow::OPEN_NEWWINDOW,
"Unsupported open location");

View File

@ -544,6 +544,7 @@ nsContentTreeOwner::ProvideWindow(
if (openLocation != nsIBrowserDOMWindow::OPEN_NEWTAB &&
openLocation != nsIBrowserDOMWindow::OPEN_NEWTAB_BACKGROUND &&
openLocation != nsIBrowserDOMWindow::OPEN_NEWTAB_FOREGROUND &&
openLocation != nsIBrowserDOMWindow::OPEN_CURRENTWINDOW &&
openLocation != nsIBrowserDOMWindow::OPEN_PRINT_BROWSER) {
// Just open a window normally