Bug 773942 - Use the browsingContext to open the new tab with correct userContextId, private-ness, and ownerTab. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D72778
This commit is contained in:
Jared Wein 2020-05-16 05:59:53 +00:00
parent e2e5ff74a3
commit 3ab25176e5
6 changed files with 41 additions and 15 deletions

View File

@ -1429,8 +1429,10 @@ DownloadSource.fromSerializable = function(aSerializable) {
} else {
// Convert String objects to primitive strings at this point.
source.url = aSerializable.url.toString();
if ("isPrivate" in aSerializable) {
source.isPrivate = aSerializable.isPrivate;
for (let propName of ["isPrivate", "userContextId", "browsingContextId"]) {
if (propName in aSerializable) {
source[propName] = aSerializable[propName];
}
}
if ("referrerInfo" in aSerializable) {
// Quick pass, pass directly nsIReferrerInfo, we don't need to serialize

View File

@ -786,22 +786,36 @@ var DownloadIntegration = {
if (aDownload.handleInternally) {
let win = Services.wm.getMostRecentBrowserWindow();
let browsingContext =
win && win.BrowsingContext.get(aDownload.source.browsingContextId);
win = Services.wm.getOuterWindowWithId(
browsingContext &&
browsingContext.embedderWindowGlobal &&
browsingContext.embedderWindowGlobal.outerWindowId
);
let fileURI = Services.io.newFileURI(file);
if (win) {
// TODO: Replace openTrustedLinkIn with openUILink once
// we have access to the event.
win.openTrustedLinkIn(fileURI.spec, "tab");
win.openTrustedLinkIn(fileURI.spec, "tab", {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
userContextId: aDownload.source.userContextId,
});
return;
}
let features = "chrome,dialog=no,all";
if (aDownload.source.isPrivate) {
features += ",private";
}
let args = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
args.data = fileURI.spec;
Services.ww.openWindow(
win = Services.ww.openWindow(
null,
AppConstants.BROWSER_CHROME_URL,
"_blank",
"chrome,dialog=no,all",
features,
args
);
return;

View File

@ -269,7 +269,7 @@ DownloadLegacyTransfer.prototype = {
aStartTime,
aTempFile,
aCancelable,
aIsPrivate,
aBrowsingContext,
aHandleInternally
) {
this._cancelable = aCancelable;
@ -295,8 +295,18 @@ DownloadLegacyTransfer.prototype = {
// Create a new Download object associated to a DownloadLegacySaver, and
// wait for it to be available. This operation may cause the entire
// download system to initialize before the object is created.
let browsingContextId = aBrowsingContext.id;
let windowGlobal = aBrowsingContext.currentWindowGlobal;
let originAttributes = windowGlobal.documentPrincipal.originAttributes;
let isPrivate = originAttributes.privateBrowsingId > 0;
let { userContextId } = originAttributes;
Downloads.createDownload({
source: { url: aSource.spec, isPrivate: aIsPrivate },
source: {
url: aSource.spec,
isPrivate,
userContextId,
browsingContextId,
},
target: {
path: aTarget.QueryInterface(Ci.nsIFileURL).file.path,
partFilePath: aTempFile && aTempFile.path,

View File

@ -10,6 +10,7 @@ interface nsIURI;
interface nsICancelable;
interface nsIMIMEInfo;
interface nsIFile;
webidl BrowsingContext;
[scriptable, uuid(37ec75d3-97ad-4da8-afaa-eabe5b4afd73)]
interface nsITransfer : nsIWebProgressListener2 {
@ -60,7 +61,7 @@ interface nsITransfer : nsIWebProgressListener2 {
in PRTime startTime,
in nsIFile aTempFile,
in nsICancelable aCancelable,
in boolean aIsPrivate,
in BrowsingContext aBrowsingContext,
in boolean aHandleInternally);
/*

View File

@ -1583,7 +1583,7 @@ NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIRequest* request) {
if (NS_FAILED(rv)) {
nsresult transferError = rv;
rv = CreateFailedTransfer(aChannel && NS_UsePrivateBrowsing(aChannel));
rv = CreateFailedTransfer();
if (NS_FAILED(rv)) {
LOG(
("Failed to create transfer to report failure."
@ -2011,7 +2011,7 @@ nsExternalAppHandler::OnSaveComplete(nsIBackgroundFileSaver* aSaver,
// have to.
if (!mTransfer) {
// We don't care if this fails.
CreateFailedTransfer(channel && NS_UsePrivateBrowsing(channel));
CreateFailedTransfer();
}
SendStatusChange(kWriteError, aStatus, nullptr, path);
@ -2104,8 +2104,7 @@ nsresult nsExternalAppHandler::CreateTransfer() {
NS_ENSURE_SUCCESS(rv, rv);
rv = transfer->Init(mSourceUrl, target, EmptyString(), mMimeInfo,
mTimeDownloadStarted, mTempFile, this,
channel && NS_UsePrivateBrowsing(channel),
mTimeDownloadStarted, mTempFile, this, mBrowsingContext,
mHandleInternally);
NS_ENSURE_SUCCESS(rv, rv);
@ -2142,7 +2141,7 @@ nsresult nsExternalAppHandler::CreateTransfer() {
return rv;
}
nsresult nsExternalAppHandler::CreateFailedTransfer(bool aIsPrivateBrowsing) {
nsresult nsExternalAppHandler::CreateFailedTransfer() {
nsresult rv;
nsCOMPtr<nsITransfer> transfer =
do_CreateInstance(NS_TRANSFER_CONTRACTID, &rv);
@ -2164,7 +2163,7 @@ nsresult nsExternalAppHandler::CreateFailedTransfer(bool aIsPrivateBrowsing) {
NS_ENSURE_SUCCESS(rv, rv);
rv = transfer->Init(mSourceUrl, pseudoTarget, EmptyString(), mMimeInfo,
mTimeDownloadStarted, nullptr, this, aIsPrivateBrowsing,
mTimeDownloadStarted, nullptr, this, mBrowsingContext,
mHandleInternally);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -388,7 +388,7 @@ class nsExternalAppHandler final : public nsIStreamListener,
* If we fail to create the necessary temporary file to initiate a transfer
* we will report the failure by creating a failed nsITransfer.
*/
nsresult CreateFailedTransfer(bool aIsPrivateBrowsing);
nsresult CreateFailedTransfer();
/*
* The following two functions are part of the split of SaveToDisk