[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor-browser] 48/73: Bug 1766030 - Add an optional source URL when saving an URL. r=Gijs, a=RyanVM



This is an automated email from the git hooks/post-receive script.

richard pushed a commit to branch geckoview-102.3.0esr-12.0-1
in repository tor-browser.

commit 79e7732a6abeff0e217b983895aeefac850742b9
Author: Calixte <cdenizet@xxxxxxxxxxx>
AuthorDate: Tue May 31 17:25:29 2022 +0000

    Bug 1766030 - Add an optional source URL when saving an URL. r=Gijs, a=RyanVM
    
    In pdf.js, files are saved thanks to a blob but the original URL is lost.
    Consequently, the download panel doesn't contain any information about the
    origins of a saved pdf.
    The saveURL, internalSave and nsITransfer.init functions has now a parameter for this originalURL.
    
    Differential Revision: https://phabricator.services.mozilla.com/D147651
---
 browser/base/content/browser.js                             |  1 +
 browser/base/content/nsContextMenu.js                       |  4 ++++
 browser/base/content/pageinfo/pageInfo.js                   |  2 ++
 browser/base/content/utilityOverlay.js                      | 13 ++++++++++++-
 browser/components/downloads/DownloadsCommon.jsm            |  4 +++-
 browser/components/downloads/content/indicator.js           | 12 +++++++++++-
 devtools/startup/DevToolsStartup.jsm                        |  1 +
 toolkit/components/downloads/DownloadCore.jsm               |  9 +++++++++
 toolkit/components/downloads/DownloadLegacy.jsm             |  5 +++++
 toolkit/components/downloads/test/unit/head.js              |  1 +
 toolkit/components/pdfjs/content/PdfStreamConverter.jsm     |  1 +
 toolkit/components/pdfjs/content/PdfjsParent.jsm            |  1 +
 .../components/pdfjs/test/browser_pdfjs_download_button.js  | 11 ++++++++++-
 toolkit/content/contentAreaUtils.js                         | 11 +++++++++++
 toolkit/content/tests/browser/browser_saveImageURL.js       |  1 +
 .../content/tests/browser/browser_save_resend_postdata.js   |  1 +
 uriloader/base/nsITransfer.idl                              |  4 ++++
 uriloader/exthandler/nsExternalHelperAppService.cpp         |  4 ++--
 18 files changed, 80 insertions(+), 6 deletions(-)

diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js
index 801bab733ba63..628c58cc2208d 100644
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -7128,6 +7128,7 @@ function handleLinkClick(event, href, linkNode) {
   if (where == "save") {
     saveURL(
       href,
+      null,
       linkNode ? gatherTextUnder(linkNode) : "",
       null,
       true,
diff --git a/browser/base/content/nsContextMenu.js b/browser/base/content/nsContextMenu.js
index f4e105920391c..81e11043ecc58 100644
--- a/browser/base/content/nsContextMenu.js
+++ b/browser/base/content/nsContextMenu.js
@@ -1566,6 +1566,7 @@ class nsContextMenu {
       // FIXME can we switch this to a blob URL?
       internalSave(
         dataURL,
+        null, // originalURL
         null, // document
         name,
         null, // content disposition
@@ -1754,6 +1755,7 @@ class nsContextMenu {
           // it can without waiting.
           saveURL(
             linkURL,
+            null,
             linkText,
             dialogTitle,
             bypassCache,
@@ -1904,6 +1906,7 @@ class nsContextMenu {
       this._canvasToBlobURL(this.targetIdentifier).then(function(blobURL) {
         internalSave(
           blobURL,
+          null, // originalURL
           null, // document
           "canvas.png",
           null, // content disposition
@@ -1924,6 +1927,7 @@ class nsContextMenu {
       urlSecurityCheck(this.mediaURL, this.principal);
       internalSave(
         this.mediaURL,
+        null, // originalURL
         null, // document
         null, // file name; we'll take it from the URL
         this.contentData.contentDisposition,
diff --git a/browser/base/content/pageinfo/pageInfo.js b/browser/base/content/pageinfo/pageInfo.js
index 4b2756a222492..a0670995d0227 100644
--- a/browser/base/content/pageinfo/pageInfo.js
+++ b/browser/base/content/pageinfo/pageInfo.js
@@ -738,6 +738,7 @@ function saveMedia() {
       saveURL(
         url,
         null,
+        null,
         titleKey,
         false,
         false,
@@ -768,6 +769,7 @@ function saveMedia() {
             null,
             null,
             null,
+            null,
             false,
             "SaveImageTitle",
             aChosenData,
diff --git a/browser/base/content/utilityOverlay.js b/browser/base/content/utilityOverlay.js
index 7611185e25e2c..40a1fa59d0e2e 100644
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -319,6 +319,7 @@ function openLinkIn(url, where, params) {
         url,
         null,
         null,
+        null,
         true,
         true,
         aReferrerInfo,
@@ -335,7 +336,17 @@ function openLinkIn(url, where, params) {
         );
         return;
       }
-      saveURL(url, null, null, true, true, aReferrerInfo, null, aInitiatingDoc);
+      saveURL(
+        url,
+        null,
+        null,
+        null,
+        true,
+        true,
+        aReferrerInfo,
+        null,
+        aInitiatingDoc
+      );
     }
     return;
   }
diff --git a/browser/components/downloads/DownloadsCommon.jsm b/browser/components/downloads/DownloadsCommon.jsm
index a22c8378c4f98..422be505a3156 100644
--- a/browser/components/downloads/DownloadsCommon.jsm
+++ b/browser/components/downloads/DownloadsCommon.jsm
@@ -442,7 +442,9 @@ var DownloadsCommon = {
    * Copies the source URI of the given Download object to the clipboard.
    */
   copyDownloadLink(download) {
-    gClipboardHelper.copyString(download.source.url);
+    gClipboardHelper.copyString(
+      download.source.originalUrl || download.source.url
+    );
   },
 
   /**
diff --git a/browser/components/downloads/content/indicator.js b/browser/components/downloads/content/indicator.js
index c7a4d5bfe1950..b739cdd797a3b 100644
--- a/browser/components/downloads/content/indicator.js
+++ b/browser/components/downloads/content/indicator.js
@@ -584,7 +584,17 @@ const DownloadsIndicatorView = {
       if (link.url.startsWith("about:")) {
         continue;
       }
-      saveURL(link.url, link.name, null, true, true, null, null, sourceDoc);
+      saveURL(
+        link.url,
+        null,
+        link.name,
+        null,
+        true,
+        true,
+        null,
+        null,
+        sourceDoc
+      );
       handled = true;
     }
     if (handled) {
diff --git a/devtools/startup/DevToolsStartup.jsm b/devtools/startup/DevToolsStartup.jsm
index 775e6023a7594..069d9abecbd69 100644
--- a/devtools/startup/DevToolsStartup.jsm
+++ b/devtools/startup/DevToolsStartup.jsm
@@ -1178,6 +1178,7 @@ const JsonView = {
           const filename = chrome.getDefaultFileName(undefined, uri, doc, null);
           chrome.internalSave(
             message.data,
+            null /* originalURL */,
             null,
             filename,
             null,
diff --git a/toolkit/components/downloads/DownloadCore.jsm b/toolkit/components/downloads/DownloadCore.jsm
index 5504c3cc3c33a..5292828f593a1 100644
--- a/toolkit/components/downloads/DownloadCore.jsm
+++ b/toolkit/components/downloads/DownloadCore.jsm
@@ -1437,6 +1437,11 @@ DownloadSource.prototype = {
    */
   url: null,
 
+  /**
+   * String containing the original URL for the download source.
+   */
+  originalUrl: null,
+
   /**
    * Indicates whether the download originated from a private window.  This
    * determines the context of the network request that is made to retrieve the
@@ -1598,6 +1603,9 @@ DownloadSource.fromSerializable = function(aSerializable) {
         source[propName] = aSerializable[propName];
       }
     }
+    if ("originalUrl" in aSerializable) {
+      source.originalUrl = aSerializable.originalUrl;
+    }
     if ("referrerInfo" in aSerializable) {
       // Quick pass, pass directly nsIReferrerInfo, we don't need to serialize
       // and deserialize
@@ -1647,6 +1655,7 @@ DownloadSource.fromSerializable = function(aSerializable) {
       aSerializable,
       property =>
         property != "url" &&
+        property != "originalUrl" &&
         property != "isPrivate" &&
         property != "referrerInfo" &&
         property != "cookieJarSettings" &&
diff --git a/toolkit/components/downloads/DownloadLegacy.jsm b/toolkit/components/downloads/DownloadLegacy.jsm
index 6688ad7b172e3..14b9b8b8780fd 100644
--- a/toolkit/components/downloads/DownloadLegacy.jsm
+++ b/toolkit/components/downloads/DownloadLegacy.jsm
@@ -265,6 +265,7 @@ DownloadLegacyTransfer.prototype = {
   // nsITransfer
   init: function DLT_init(
     aSource,
+    aSourceOriginalURI,
     aTarget,
     aDisplayName,
     aMIMEInfo,
@@ -277,6 +278,7 @@ DownloadLegacyTransfer.prototype = {
   ) {
     return this._nsITransferInitInternal(
       aSource,
+      aSourceOriginalURI,
       aTarget,
       aDisplayName,
       aMIMEInfo,
@@ -315,6 +317,7 @@ DownloadLegacyTransfer.prototype = {
     }
     return this._nsITransferInitInternal(
       aSource,
+      null,
       aTarget,
       aDisplayName,
       aMIMEInfo,
@@ -333,6 +336,7 @@ DownloadLegacyTransfer.prototype = {
 
   _nsITransferInitInternal(
     aSource,
+    aSourceOriginalURI,
     aTarget,
     aDisplayName,
     aMIMEInfo,
@@ -377,6 +381,7 @@ DownloadLegacyTransfer.prototype = {
     let serialisedDownload = {
       source: {
         url: aSource.spec,
+        originalUrl: aSourceOriginalURI && aSourceOriginalURI.spec,
         isPrivate,
         userContextId,
         browsingContextId,
diff --git a/toolkit/components/downloads/test/unit/head.js b/toolkit/components/downloads/test/unit/head.js
index f920c80a47d50..dfdded11a879a 100644
--- a/toolkit/components/downloads/test/unit/head.js
+++ b/toolkit/components/downloads/test/unit/head.js
@@ -368,6 +368,7 @@ function promiseStartLegacyDownload(aSourceUrl, aOptions) {
         // the Download object to be created and added to the public downloads.
         transfer.init(
           sourceURI,
+          null,
           NetUtil.newURI(targetFile),
           null,
           mimeInfo,
diff --git a/toolkit/components/pdfjs/content/PdfStreamConverter.jsm b/toolkit/components/pdfjs/content/PdfStreamConverter.jsm
index 46ad896b51bd0..95cfb24eaecf2 100644
--- a/toolkit/components/pdfjs/content/PdfStreamConverter.jsm
+++ b/toolkit/components/pdfjs/content/PdfStreamConverter.jsm
@@ -353,6 +353,7 @@ class ChromeActions {
     let actor = getActor(this.domWindow);
     actor.sendAsyncMessage("PDFJS:Parent:saveURL", {
       blobUrl,
+      originalUrl,
       filename,
     });
   }
diff --git a/toolkit/components/pdfjs/content/PdfjsParent.jsm b/toolkit/components/pdfjs/content/PdfjsParent.jsm
index ee58a0b709e68..961285217003a 100644
--- a/toolkit/components/pdfjs/content/PdfjsParent.jsm
+++ b/toolkit/components/pdfjs/content/PdfjsParent.jsm
@@ -99,6 +99,7 @@ class PdfjsParent extends JSWindowActorParent {
     const data = aMsg.data;
     this.browser.ownerGlobal.saveURL(
       data.blobUrl /* aURL */,
+      data.originalUrl /* aOriginalURL */,
       data.filename /* aFileName */,
       null /* aFilePickerTitleKey */,
       true /* aShouldBypassCache */,
diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js b/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js
index 44bb8068bfd6c..5f21b241a223a 100644
--- a/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js
+++ b/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js
@@ -99,13 +99,22 @@ add_task(async function test_downloading_pdf_nonprivate_window() {
         "Check the download panel state is 'open'"
       );
       downloadList = await Downloads.getList(Downloads.PUBLIC);
-      let currentDownloadCount = (await downloadList.getAll()).length;
+      const allDownloads = await downloadList.getAll();
+      let currentDownloadCount = allDownloads.length;
       is(
         currentDownloadCount,
         initialDownloadCount + 1,
         "A download was added when we clicked download"
       );
 
+      const dl = allDownloads.find(dl => dl.source.originalUrl === pdfUrl);
+      ok(!!dl, "The pdf download has the correct url in source.originalUrl");
+
+      SpecialPowers.clipboardCopyString("");
+      DownloadsCommon.copyDownloadLink(dl);
+      const copiedUrl = SpecialPowers.getClipboardData("text/unicode");
+      is(copiedUrl, pdfUrl, "The copied url must be the original one");
+
       is(
         gBrowser.tabs.length,
         tabCount,
diff --git a/toolkit/content/contentAreaUtils.js b/toolkit/content/contentAreaUtils.js
index dd57520d8478f..68905b0a8e8d4 100644
--- a/toolkit/content/contentAreaUtils.js
+++ b/toolkit/content/contentAreaUtils.js
@@ -71,6 +71,7 @@ function urlSecurityCheck(
 //
 function saveURL(
   aURL,
+  aOriginalURL,
   aFileName,
   aFilePickerTitleKey,
   aShouldBypassCache,
@@ -83,6 +84,7 @@ function saveURL(
 ) {
   internalSave(
     aURL,
+    aOriginalURL,
     null,
     aFileName,
     null,
@@ -122,6 +124,7 @@ function saveBrowser(aBrowser, aSkipPrompt, aBrowsingContext = null) {
 
       internalSave(
         document.documentURI,
+        null, // originalURL
         document,
         null, // file name
         document.contentDisposition,
@@ -214,6 +217,9 @@ XPCOMUtils.defineConstant(this, "kSaveAsType_Text", kSaveAsType_Text);
  *
  * @param aURL
  *        The String representation of the URL of the document being saved
+ * @param aOriginalURL
+ *        The String representation of the original URL of the document being
+ *        saved. It can useful in case aURL is a blob.
  * @param aDocument
  *        The document to be saved
  * @param aDefaultFileName
@@ -256,6 +262,7 @@ XPCOMUtils.defineConstant(this, "kSaveAsType_Text", kSaveAsType_Text);
  */
 function internalSave(
   aURL,
+  aOriginalURL,
   aDocument,
   aDefaultFileName,
   aContentDisposition,
@@ -368,8 +375,11 @@ function internalSave(
       (aDocument && (aDocument.nodePrincipal || aDocument.principal)) ||
       (aInitiatingDocument && aInitiatingDocument.nodePrincipal);
 
+    let sourceOriginalURI = aOriginalURL ? makeURI(aOriginalURL) : null;
+
     var persistArgs = {
       sourceURI,
+      sourceOriginalURI,
       sourcePrincipal,
       sourceReferrerInfo: aReferrerInfo,
       sourceDocument: useSaveDocument ? aDocument : null,
@@ -447,6 +457,7 @@ function internalPersist(persistArgs) {
   var tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
   tr.init(
     persistArgs.sourceURI,
+    persistArgs.sourceOriginalURI,
     targetFileURL,
     "",
     null,
diff --git a/toolkit/content/tests/browser/browser_saveImageURL.js b/toolkit/content/tests/browser/browser_saveImageURL.js
index 764e7e9aa593f..e72cebf22b96f 100644
--- a/toolkit/content/tests/browser/browser_saveImageURL.js
+++ b/toolkit/content/tests/browser/browser_saveImageURL.js
@@ -40,6 +40,7 @@ add_task(async function preferred_API() {
       let filePickerPromise = waitForFilePicker();
       internalSave(
         url,
+        null, // originalURL
         null, // document
         "image.jpg",
         null, // content disposition
diff --git a/toolkit/content/tests/browser/browser_save_resend_postdata.js b/toolkit/content/tests/browser/browser_save_resend_postdata.js
index c519c19880885..de28c3c729cdc 100644
--- a/toolkit/content/tests/browser/browser_save_resend_postdata.js
+++ b/toolkit/content/tests/browser/browser_save_resend_postdata.js
@@ -77,6 +77,7 @@ function test() {
     // cache.
     internalSave(
       docToSave.location.href,
+      null,
       docToSave,
       null,
       null,
diff --git a/uriloader/base/nsITransfer.idl b/uriloader/base/nsITransfer.idl
index 7d2a20f50939a..ba701c982d7a0 100644
--- a/uriloader/base/nsITransfer.idl
+++ b/uriloader/base/nsITransfer.idl
@@ -27,6 +27,9 @@ interface nsITransfer : nsIWebProgressListener2 {
      *
      * @param aSource The source URI of the transfer. Must not be null.
      *
+     * @param aSourceOriginalURI The original URI of the transfer in case
+     *                           aSource is a blob URL. Can be null.
+     *
      * @param aTarget The target URI of the transfer. Must not be null.
      *
      * @param aDisplayName The user-readable description of the transfer.
@@ -62,6 +65,7 @@ interface nsITransfer : nsIWebProgressListener2 {
      * @param aReferrerInfo The Referrer this download is started with
      */
     void init(in nsIURI aSource,
+              in nsIURI aSourceOriginalURI,
               in nsIURI aTarget,
               in AString aDisplayName,
               in nsIMIMEInfo aMIMEInfo,
diff --git a/uriloader/exthandler/nsExternalHelperAppService.cpp b/uriloader/exthandler/nsExternalHelperAppService.cpp
index 41e1272f0c604..adb05b3c46b85 100644
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -2354,7 +2354,7 @@ nsresult nsExternalAppHandler::CreateTransfer() {
         mDownloadClassification, referrerInfo, mBrowsingContext,
         mHandleInternally, nullptr);
   } else {
-    rv = transfer->Init(mSourceUrl, target, u""_ns, mMimeInfo,
+    rv = transfer->Init(mSourceUrl, nullptr, target, u""_ns, mMimeInfo,
                         mTimeDownloadStarted, mTempFile, this,
                         channel && NS_UsePrivateBrowsing(channel),
                         mDownloadClassification, referrerInfo);
@@ -2446,7 +2446,7 @@ nsresult nsExternalAppHandler::CreateFailedTransfer() {
         mDownloadClassification, referrerInfo, mBrowsingContext,
         mHandleInternally, httpChannel);
   } else {
-    rv = transfer->Init(mSourceUrl, pseudoTarget, u""_ns, mMimeInfo,
+    rv = transfer->Init(mSourceUrl, nullptr, pseudoTarget, u""_ns, mMimeInfo,
                         mTimeDownloadStarted, mTempFile, this,
                         channel && NS_UsePrivateBrowsing(channel),
                         mDownloadClassification, referrerInfo);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits