Bug 1502448 - Work around principal issues with pdfjs and saving local files. r=jkt,jaws, a=RyanVM
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 07 Jan 2019 16:59:26 +0000
changeset 506593 274d224478736b7ff659c3d1f38dc8c2b2285a8a
parent 506592 c777b4b5cd07b6575bacf5a2309b384c0c7c9c6f
child 506594 2046aa36055220d9187f968fca3ab070d2de7665
push id10476
push userryanvm@gmail.com
push dateWed, 09 Jan 2019 15:36:04 +0000
treeherdermozilla-beta@40a7055f712b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjkt, jaws, RyanVM
bugs1502448
milestone65.0
Bug 1502448 - Work around principal issues with pdfjs and saving local files. r=jkt,jaws, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D15568
toolkit/content/contentAreaUtils.js
--- a/toolkit/content/contentAreaUtils.js
+++ b/toolkit/content/contentAreaUtils.js
@@ -157,16 +157,35 @@ function saveImageURL(aURL, aFileName, a
 // This is like saveDocument, but takes any browser/frame-like element
 // and saves the current document inside it,
 // whether in-process or out-of-process.
 function saveBrowser(aBrowser, aSkipPrompt, aOuterWindowID = 0) {
   if (!aBrowser) {
     throw "Must have a browser when calling saveBrowser";
   }
   let persistable = aBrowser.frameLoader;
+  // Because of how pdf.js deals with principals, saving the document the "normal"
+  // way won't work. Work around this by saving the pdf's URL directly:
+  if (aBrowser.contentPrincipal.URI &&
+      aBrowser.contentPrincipal.URI.spec == "resource://pdf.js/web/viewer.html" &&
+      aBrowser.currentURI.schemeIs("file")) {
+    let correctPrincipal = Services.scriptSecurityManager.createCodebasePrincipal(
+        aBrowser.currentURI, aBrowser.contentPrincipal.originAttributes);
+    internalSave(aBrowser.currentURI.spec,
+                 null /* no document */, null /* automatically determine filename */,
+                 null /* no content disposition */,
+                 "application/pdf", false /* don't bypass cache */,
+                 null /* no alternative title */, null /* no auto-chosen file info */,
+                 null /* null referrer will be OK for file: */,
+                 null /* no document */, aSkipPrompt /* caller decides about prompting */,
+                 null /* no cache key because the one for the document will be for pdfjs */,
+                 PrivateBrowsingUtils.isWindowPrivate(aBrowser.ownerGlobal),
+                 correctPrincipal);
+    return;
+  }
   let stack = Components.stack.caller;
   persistable.startPersistence(aOuterWindowID, {
     onDocumentReady(document) {
       saveDocument(document, aSkipPrompt);
     },
     onError(status) {
       throw new Components.Exception("saveBrowser failed asynchronously in startPersistence",
                                      status, stack);