Bug 1356303 - Process background image into data: URL. r=aceman a=jorgk
authorJorg K <jorgk@jorgk.com>
Sat, 15 Apr 2017 13:44:21 +0200
changeset 24468 1694f6f2b300
parent 24467 d6ba4d8b1bcf
child 24469 d8949acb05a3
push id2059
push usermozilla@jorgk.com
push dateMon, 17 Apr 2017 06:53:03 +0000
treeherdercomm-aurora@9d19a9b732a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaceman, jorgk
bugs1356303
Bug 1356303 - Process background image into data: URL. r=aceman a=jorgk
mail/components/compose/content/MsgComposeCommands.js
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -5442,16 +5442,35 @@ function InitEditor()
       }
     }
     else {
       // For file:, and references to parts of random messages, show the
       // blocked content notification.
       gComposeNotificationBar.setBlockedContent(src);
     }
   }, true);
+
+  // Convert mailnews URL back to data: URL.
+  let background = editor.document.body.background;
+  if (background && gMsgCompose.originalMsgURI) {
+    // Check that background has the same URL as the message itself.
+    let msgSvc = Components.classes["@mozilla.org/messenger;1"]
+      .createInstance(Components.interfaces.nsIMessenger)
+      .messageServiceFromURI(gMsgCompose.originalMsgURI);
+    let originalMsgNeckoURI = {};
+    msgSvc.GetUrlForUri(gMsgCompose.originalMsgURI, originalMsgNeckoURI, null);
+    if (background.startsWith(originalMsgNeckoURI.value.spec)) {
+      try {
+        editor.document.body.background = loadBlockedImage(background, true);
+      } catch (e) {
+        // Couldn't load the referenced image.
+        Components.utils.reportError(e);
+      }
+    }
+  }
 }
 
 // This is used as event listener to spellcheck-changed event to update
 // document language.
 function updateDocumentLanguage(e)
 {
   document.documentElement.setAttribute("lang", e.detail.dictionary);
 }
@@ -5654,21 +5673,22 @@ function onUnblockResource(aURL, aNode) 
     }
   }
 }
 
 /**
  * Convert the blocked content to a data URL and swap the src to that for the
  * elements that were using it.
  *
- * @param {String} aURL - (necko) URL to unblock
- *
- * @throw Error()  if reading the data failed
+ * @param {String}  aURL - (necko) URL to unblock
+ * @param {Bool}    aReturnDataURL - return data: URL instead of processing image
+ * @return {String} the image as data: URL.
+ * @throw Error()   if reading the data failed
  */
-function loadBlockedImage(aURL) {
+function loadBlockedImage(aURL, aReturnDataURL = false) {
   let filename;
   if (/^file:/i.test(aURL)) {
     filename = aURL.substr(aURL.lastIndexOf("/") + 1);
   }
   else {
     let fnMatch = /[?&;]filename=([^?&]+)/.exec(aURL);
     filename = (fnMatch && fnMatch[1]) || "";
   }
@@ -5709,16 +5729,19 @@ function loadBlockedImage(aURL) {
     throw new Error("Couln't read all data from URL=" + aURL + " (" + e +")");
   }
   stream.close();
   let encoded = btoa(streamData);
   let dataURL = "data:" + contentType +
     (filename ? ";filename=" + encodeURIComponent(filename) : "") +
     ";base64," + encoded;
 
+  if (aReturnDataURL)
+    return dataURL;
+
   let editor = GetCurrentEditor();
   for (let img of editor.document.images) {
     if (img.src == aURL) {
       img.src = dataURL; // Swap to data URL.
       img.classList.remove("loading-internal");
     }
   }
 }