Bug 773942 - Create a new init method in nsITransfer that accepts a BrowsingContext and if the download should be handled internally for backwards-compat. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D75186
This commit is contained in:
Jared Wein 2020-05-16 04:26:12 +00:00
Родитель 6a71aa8029
Коммит 788718620f
4 изменённых файлов: 108 добавлений и 16 удалений

Просмотреть файл

@ -269,8 +269,68 @@ DownloadLegacyTransfer.prototype = {
aStartTime,
aTempFile,
aCancelable,
aIsPrivate
) {
return this._nsITransferInitInternal(
aSource,
aTarget,
aDisplayName,
aMIMEInfo,
aStartTime,
aTempFile,
aCancelable,
aIsPrivate
);
},
// nsITransfer
initWithBrowsingContext(
aSource,
aTarget,
aDisplayName,
aMIMEInfo,
aStartTime,
aTempFile,
aCancelable,
aIsPrivate,
aBrowsingContext,
aHandleInternally
) {
let browsingContextId;
let userContextId;
if (aBrowsingContext && aBrowsingContext.currentWindowGlobal) {
browsingContextId = aBrowsingContext.id;
let windowGlobal = aBrowsingContext.currentWindowGlobal;
let originAttributes = windowGlobal.documentPrincipal.originAttributes;
userContextId = originAttributes.userContextId;
}
return this._nsITransferInitInternal(
aSource,
aTarget,
aDisplayName,
aMIMEInfo,
aStartTime,
aTempFile,
aCancelable,
aIsPrivate,
userContextId,
browsingContextId,
aHandleInternally
);
},
_nsITransferInitInternal(
aSource,
aTarget,
aDisplayName,
aMIMEInfo,
aStartTime,
aTempFile,
aCancelable,
isPrivate,
userContextId = 0,
browsingContextId = 0,
handleInternally = false
) {
this._cancelable = aCancelable;
@ -295,11 +355,6 @@ 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,
@ -315,7 +370,7 @@ DownloadLegacyTransfer.prototype = {
launchWhenSucceeded,
contentType,
launcherPath,
handleInternally: aHandleInternally,
handleInternally,
})
.then(aDownload => {
// Legacy components keep partial data when they use a ".part" file.

Просмотреть файл

@ -68,6 +68,7 @@ MockTransfer.prototype = {
/* nsITransfer */
init() {},
initWithBrowsingContext() {},
setSha256Hash() {},
setSignatureInfo() {},
};

Просмотреть файл

@ -51,8 +51,6 @@ interface nsITransfer : nsIWebProgressListener2 {
* If true, indicates that the transfer was initiated from
* a source that desires privacy.
*
* @param aHandleInternally Set to true if the download should be opened within
* the browser.
*/
void init(in nsIURI aSource,
in nsIURI aTarget,
@ -61,8 +59,28 @@ interface nsITransfer : nsIWebProgressListener2 {
in PRTime startTime,
in nsIFile aTempFile,
in nsICancelable aCancelable,
in BrowsingContext aBrowsingContext,
in boolean aHandleInternally);
in boolean aIsPrivate);
/**
* Same as init, but allows for passing the browsingContext
* which will allow for opening the download with the same
* userContextId
*
* @param aBrowsingContext BrowsingContext of the initiating document.
*
* @param aHandleInternally Set to true if the download should be opened within
* the browser.
*/
void initWithBrowsingContext(in nsIURI aSource,
in nsIURI aTarget,
in AString aDisplayName,
in nsIMIMEInfo aMIMEInfo,
in PRTime startTime,
in nsIFile aTempFile,
in nsICancelable aCancelable,
in boolean aIsPrivate,
in BrowsingContext aBrowsingContext,
in boolean aHandleInternally);
/*
* Used to notify the transfer object of the hash of the downloaded file.

Просмотреть файл

@ -2109,9 +2109,18 @@ nsresult nsExternalAppHandler::CreateTransfer() {
rv = NS_NewFileURI(getter_AddRefs(target), mFinalFileDestination);
NS_ENSURE_SUCCESS(rv, rv);
rv = transfer->Init(mSourceUrl, target, EmptyString(), mMimeInfo,
mTimeDownloadStarted, mTempFile, this, mBrowsingContext,
mHandleInternally);
nsCOMPtr<nsIChannel> channel = do_QueryInterface(mRequest);
if (mBrowsingContext) {
rv = transfer->InitWithBrowsingContext(
mSourceUrl, target, EmptyString(), mMimeInfo, mTimeDownloadStarted,
mTempFile, this, channel && NS_UsePrivateBrowsing(channel),
mBrowsingContext, mHandleInternally);
} else {
rv = transfer->Init(mSourceUrl, target, EmptyString(), mMimeInfo,
mTimeDownloadStarted, mTempFile, this,
channel && NS_UsePrivateBrowsing(channel));
}
NS_ENSURE_SUCCESS(rv, rv);
// If we were cancelled since creating the transfer, just return. It is
@ -2168,9 +2177,18 @@ nsresult nsExternalAppHandler::CreateFailedTransfer() {
rv = NS_NewFileURI(getter_AddRefs(pseudoTarget), pseudoFile);
NS_ENSURE_SUCCESS(rv, rv);
rv = transfer->Init(mSourceUrl, pseudoTarget, EmptyString(), mMimeInfo,
mTimeDownloadStarted, nullptr, this, mBrowsingContext,
mHandleInternally);
nsCOMPtr<nsIChannel> channel = do_QueryInterface(mRequest);
if (mBrowsingContext) {
rv = transfer->InitWithBrowsingContext(
mSourceUrl, pseudoTarget, EmptyString(), mMimeInfo,
mTimeDownloadStarted, nullptr, this,
channel && NS_UsePrivateBrowsing(channel), mBrowsingContext,
mHandleInternally);
} else {
rv = transfer->Init(mSourceUrl, pseudoTarget, EmptyString(), mMimeInfo,
mTimeDownloadStarted, nullptr, this,
channel && NS_UsePrivateBrowsing(channel));
}
NS_ENSURE_SUCCESS(rv, rv);
// Our failed transfer is ready.