Bug 1524640 - Fix font loading during mozPrintCallback. r=smaug a=lizzard
authorBrendan Dahl <bdahl@mozilla.com>
Wed, 06 Feb 2019 23:46:58 +0000
changeset 516128 aab4fc980d3cda9ea781c95f4e331d7291ef75ed
parent 516127 8dbc1d23274ba19af8a4de8d65e00d78a1c71364
child 516129 2589bf262f6576b3e0bce80179c54691fc28c21e
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, lizzard
bugs1524640
milestone66.0
Bug 1524640 - Fix font loading during mozPrintCallback. r=smaug a=lizzard Copy fonts loaded during a mozPrintCallback into the cloned document, so they are available during printing. Differential Revision: https://phabricator.services.mozilla.com/D18613
dom/base/Document.cpp
dom/base/Document.h
layout/base/tests/chrome/chrome.ini
layout/base/tests/chrome/printpreview_font_mozprintcallback.html
layout/base/tests/chrome/printpreview_font_mozprintcallback_ref.html
layout/base/tests/chrome/printpreview_helper.xul
layout/style/FontFaceSet.cpp
--- a/dom/base/Document.cpp
+++ b/dom/base/Document.cpp
@@ -7509,16 +7509,20 @@ void Document::Destroy() {
     MOZ_ASSERT(child->GetParentNode() == this);
   }
   MOZ_ASSERT(oldChildCount == GetChildCount());
 
   mInUnlinkOrDeletion = oldVal;
 
   mLayoutHistoryState = nullptr;
 
+  if (mOriginalDocument) {
+    mOriginalDocument->mLatestStaticClone = nullptr;
+  }
+
   // Shut down our external resource map.  We might not need this for
   // leak-fixing if we fix nsDocumentViewer to do cycle-collection, but
   // tearing down all those frame trees right now is the right thing to do.
   mExternalResourceMap.Shutdown();
 }
 
 void Document::RemovedFromDocShell() {
   if (mRemovedFromDocShell) return;
@@ -8881,18 +8885,20 @@ already_AddRefed<Document> Document::Cre
   if (rv.Failed()) {
     // Don't return yet; we need to reset mCreatingStaticClone
     rv.SuppressException();
   } else {
     clonedDoc = do_QueryInterface(clonedNode);
     if (clonedDoc) {
       if (IsStaticDocument()) {
         clonedDoc->mOriginalDocument = mOriginalDocument;
+        mOriginalDocument->mLatestStaticClone = clonedDoc;
       } else {
         clonedDoc->mOriginalDocument = this;
+        mLatestStaticClone = clonedDoc;
       }
 
       clonedDoc->mOriginalDocument->mStaticCloneCount++;
 
       size_t sheetsCount = SheetCount();
       for (size_t i = 0; i < sheetsCount; ++i) {
         RefPtr<StyleSheet> sheet = SheetAt(i);
         if (sheet) {
--- a/dom/base/Document.h
+++ b/dom/base/Document.h
@@ -434,29 +434,32 @@ class ExternalResourceMap {
 // Gecko.
 class Document : public nsINode,
                  public DocumentOrShadowRoot,
                  public nsSupportsWeakReference,
                  public nsIRadioGroupContainer,
                  public nsIScriptObjectPrincipal,
                  public nsIApplicationCacheContainer,
                  public nsStubMutationObserver,
-                 public DispatcherTrait {
+                 public DispatcherTrait,
+                 public SupportsWeakPtr<Document> {
  protected:
   explicit Document(const char* aContentType);
   virtual ~Document();
 
   Document(const Document&) = delete;
   Document& operator=(const Document&) = delete;
 
  public:
   typedef mozilla::dom::ExternalResourceMap::ExternalResourceLoad
       ExternalResourceLoad;
   typedef net::ReferrerPolicy ReferrerPolicyEnum;
 
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(Document)
+
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_IDOCUMENT_IID)
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
 
   NS_DECL_ADDSIZEOFEXCLUDINGTHIS
 
   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Document,
                                                                    nsINode)
@@ -607,16 +610,18 @@ class Document : public nsINode,
                              bool aNotify) override;
   void RemoveChildNode(nsIContent* aKid, bool aNotify) final;
   nsresult Clone(mozilla::dom::NodeInfo* aNodeInfo,
                  nsINode** aResult) const override {
     return NS_ERROR_NOT_IMPLEMENTED;
   }
   nsresult CloneDocHelper(Document* clone) const;
 
+  Document* GetLatestStaticClone() const { return mLatestStaticClone; }
+
   /**
    * Signal that the document title may have changed
    * (see Document::GetTitle).
    * @param aBoundTitleElement true if an HTML or SVG <title> element
    * has just been bound to the document.
    */
   virtual void NotifyPossibleTitleChange(bool aBoundTitleElement);
 
@@ -4240,16 +4245,21 @@ class Document : public nsINode,
   /**
    * The current frame request callback handle
    */
   int32_t mFrameRequestCallbackCounter;
 
   // Count of live static clones of this document.
   uint32_t mStaticCloneCount;
 
+  // If the document is currently printing (or in print preview) this will point
+  // to the current static clone of this document. This is weak since the clone
+  // also has a reference to this document.
+  WeakPtr<Document> mLatestStaticClone;
+
   // Array of nodes that have been blocked to prevent user tracking.
   // They most likely have had their nsIChannel canceled by the URL
   // classifier. (Safebrowsing)
   //
   // Weak nsINode pointers are used to allow nodes to disappear.
   nsTArray<nsWeakPtr> mBlockedTrackingNodes;
 
   // Weak reference to mScriptGlobalObject QI:d to nsPIDOMWindow,
--- a/layout/base/tests/chrome/chrome.ini
+++ b/layout/base/tests/chrome/chrome.ini
@@ -8,16 +8,18 @@ support-files =
   default_background_window.xul
   dialog_with_positioning_window.xul
   file_bug458898.html
   printpreview_bug396024_helper.xul
   printpreview_bug482976_helper.xul
   printpreview_helper.xul
   printpreview_font_api.html
   printpreview_font_api_ref.html
+  printpreview_font_mozprintcallback.html
+  printpreview_font_mozprintcallback_ref.html
   file_bug1018265.xul
 
 [test_bug396367-1.html]
 [test_bug396367-2.html]
 [test_bug420499.xul]
 [test_bug458898.html]
 [test_bug504311.xul]
 [test_bug514660.xul]
new file mode 100644
--- /dev/null
+++ b/layout/base/tests/chrome/printpreview_font_mozprintcallback.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+</head>
+<body>
+  <canvas id="canvas" width="200" height="200"></canvas>
+  <script>
+    const fontData = "data:font/opentype;base64,T1RUTwAJAIAAAwAQQ0ZGICjPfaIAAACcAAAgsU9TLzJlXAb2AAAhUAAAAGBjbWFwiDMtcQAAIbAAAAMoaGVhZKspT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AEECQAAACAAegADAAEECQABADoAmgADAAEECQACAA4A1AADAAEECQADABAA4gADAAEECQAEADoA8gADAAEECQAFABgBLAADAAEECQAGAAABRAADAAEECQAHAA4BRAADAAEECQAIAA4BUgADAAEECQAJAA4BYE9yaWdpbmFsIGxpY2VuY2VOV0JKWkwrTmltYnVzUm9tTm85TC1NZWRpSXRhbFVua25vd251bmlxdWVJRE5XQkpaTCtOaW1idXNSb21ObzlMLU1lZGlJdGFsVmVyc2lvbiAwLjExVW5rbm93blVua25vd25Vbmtub3duAE8AcgBpAGcAaQBuAGEAbAAgAGwAaQBjAGUAbgBjAGUATgBXAEIASgBaAEwAKwBOAGkAbQBiAHUAcwBSAG8AbQBOAG8AOQBMAC0ATQBlAGQAaQBJAHQAYQBsAFUAbgBrAG4AbwB3AG4AdQBuAGkAcQB1AGUASQBEAE4AVwBCAEoAWgBMACsATgBpAG0AYgB1AHMAUgBvAG0ATgBvADkATAAtAE0AZQBkAGkASQB0AGEAbABWAGUAcgBzAGkAbwBuACAAMAAuADEAMQBVAG4AawBuAG8AdwBuAFUAbgBrAG4AbwB3AG4AVQBuAGsAbgBvAHcAbgADAAD/8LMzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+    let canvas = document.getElementById('canvas');
+    canvas.mozPrintCallback = (obj) => {
+      let testFontFace = new FontFace('test', 'url(' + fontData + ')');
+      document.fonts.add(testFontFace);
+      testFontFace.load().then(() => {
+        let ctx = obj.context;
+        ctx.font = '10px test, monospace';
+        ctx.fillText("lmnop", 20, 20);
+        obj.done();
+        window.postMessage("ready", "*");
+      }).catch((e) => {
+        obj.done();
+        window.postMessage("error", "*");
+      });
+    };
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/base/tests/chrome/printpreview_font_mozprintcallback_ref.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <style type="text/css">
+    @font-face {
+      font-family: test;
+      src: url(data:font/opentype;base64,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AAAAAEAB0APAABAAAAAAAFAAwAWQABAAAAAAAGAAAAZQABAAAAAAAHAAcAZQABAAAAAAAIAAcAbAABAAAAAAAJAAcAcwADAAEECQAAACAAegADAAEECQABADoAmgADAAEECQACAA4A1AADAAEECQADABAA4gADAAEECQAEADoA8gADAAEECQAFABgBLAADAAEECQAGAAABRAADAAEECQAHAA4BRAADAAEECQAIAA4BUgADAAEECQAJAA4BYE9yaWdpbmFsIGxpY2VuY2VOV0JKWkwrTmltYnVzUm9tTm85TC1NZWRpSXRhbFVua25vd251bmlxdWVJRE5XQkpaTCtOaW1idXNSb21ObzlMLU1lZGlJdGFsVmVyc2lvbiAwLjExVW5rbm93blVua25vd25Vbmtub3duAE8AcgBpAGcAaQBuAGEAbAAgAGwAaQBjAGUAbgBjAGUATgBXAEIASgBaAEwAKwBOAGkAbQBiAHUAcwBSAG8AbQBOAG8AOQBMAC0ATQBlAGQAaQBJAHQAYQBsAFUAbgBrAG4AbwB3AG4AdQBuAGkAcQB1AGUASQBEAE4AVwBCAEoAWgBMACsATgBpAG0AYgB1AHMAUgBvAG0ATgBvADkATAAtAE0AZQBkAGkASQB0AGEAbABWAGUAcgBzAGkAbwBuACAAMAAuADEAMQBVAG4AawBuAG8AdwBuAFUAbgBrAG4AbwB3AG4AVQBuAGsAbgBvAHcAbgADAAD/8LMzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA);
+    }
+  </style>
+</head>
+<body>
+  <canvas id="canvas" width="200" height="200"></canvas>
+  <script>
+    let canvas = document.getElementById('canvas');
+    canvas.mozPrintCallback = (obj) => {
+      let ctx = obj.context;
+      // Intentionally use a different fallback font than the test file so that
+      // if the font fails to load the test and reference will still be
+      // different.
+      ctx.font = '10px test, sans-serif';
+      ctx.fillText("lmnop", 20, 20);
+      obj.done();
+      window.postMessage("ready", "*");
+    };
+  </script>
+</body>
+</html>
--- a/layout/base/tests/chrome/printpreview_helper.xul
+++ b/layout/base/tests/chrome/printpreview_helper.xul
@@ -18,17 +18,17 @@ var ctx1;
 var ctx2;
 var counter = 0;
 
 var file = Cc["@mozilla.org/file/directory_service;1"]
              .getService(Ci.nsIProperties)
              .get("TmpD", Ci.nsIFile);
 filePath = file.path;
 
-function printpreview() {
+function printpreview(hasMozPrintCallback) {
   gWbp = window.frames[1].docShell.printPreview;
   var listener = {
     onLocationChange: function(webProgress, request, location, flags) { },
     onProgressChange: function(webProgress, request, curSelfProgress, 
                                maxSelfProgress, curTotalProgress,
                                maxTotalProgress) { },
     onSecurityChange: function(webProgress, request, state) { },
     onStateChange: function(webProgress, request, stateFlags, status) { },
@@ -49,17 +49,21 @@ function printpreview() {
   var before = 0;
   var after = 0;
   function beforeprint() { ++before; }
   function afterprint() { ++after; }
   window.frames[0].addEventListener("beforeprint", beforeprint, true);
   window.frames[0].addEventListener("afterprint", afterprint, true);
   gWbp.printPreview(gWbp.globalPrintSettings, window.frames[0], listener);
   is(before, 1, "Should have called beforeprint listener!");
-  is(after, 1, "Should have called afterprint listener!");
+  if (!hasMozPrintCallback) {
+    // If there's a mozPrintCallback the after print event won't fire until
+    // later.
+    is(after, 1, "Should have called afterprint listener!");
+  }
   window.frames[0].removeEventListener("beforeprint", beforeprint, true);
   window.frames[0].removeEventListener("afterprint", afterprint, true);
   prefs.clearUserPref('print.show_print_progress');
 }
 
 function exitprintpreview() {
   window.frames[1].docShell.printPreview.exitPrintPreview();
 }
@@ -340,16 +344,54 @@ async function runTest9() {
   // Ensure the <use> shadow tree is created so we test what we want to test.
   window.frames[0].document.body.offsetTop;
 
   printpreview();
   ctx2.drawWindow(window.frames[1], 0, 0, 400, 400, "rgb(255,255,255)");
   exitprintpreview();
   ok(compareCanvases(), "Printing <use> subtrees should create same output");
 
+  requestAnimationFrame(function() { setTimeout(runTest10); } );
+}
+
+// Test for bug 1524640
+async function runTest10() {
+  // Test that fonts loaded during mozprint callback are loaded into the cloned
+  // document.
+  const iframeElement = document.getElementsByTagName("iframe")[0];
+
+  // First, snapshot the page with font defined in CSS.
+  await new Promise((resolve) => {
+    iframeElement.addEventListener("load", resolve, { capture: true, once: true });
+    iframeElement.setAttribute("src", "printpreview_font_mozprintcallback_ref.html");
+  });
+  let mozPrintCallbackDone = new Promise((resolve) => {
+    iframeElement.addEventListener("message", resolve, { capture: true, once: true });
+  });
+  printpreview(true);
+  await mozPrintCallbackDone;
+  ctx1.drawWindow(window.frames[1], 0, 0, 400, 400, "rgb(255,255,255)");
+  exitprintpreview();
+
+  // Second, snapshot the page with font loaded in JS.
+  await new Promise((resolve) => {
+    iframeElement.addEventListener("load", resolve, { capture: true, once: true });
+    iframeElement.setAttribute("src", "printpreview_font_mozprintcallback.html");
+  });
+  mozPrintCallbackDone = new Promise((resolve) => {
+    iframeElement.addEventListener("message", resolve, { capture: true, once: true });
+  });
+  printpreview(true);
+  // Wait for the mozprintcallback to finish.
+  await mozPrintCallbackDone;
+  ctx2.drawWindow(window.frames[1], 0, 0, 400, 400, "rgb(255,255,255)");
+
+  exitprintpreview();
+  ok(compareCanvases(), "Printing pages with fonts loaded from a mozPrintCallback should be the same.");
+
   finish();
 }
 
 ]]></script>
 <table style="border: 1px solid black;" xmlns="http://www.w3.org/1999/xhtml">
 <tr><th>Print preview canvas 1</th><th>Print preview canvas 2</th></tr>
 <tr>
 <td><canvas height="400" width="400"></canvas></td>
--- a/layout/style/FontFaceSet.cpp
+++ b/layout/style/FontFaceSet.cpp
@@ -412,16 +412,26 @@ bool FontFaceSet::HasRuleFontFace(FontFa
     if (mRuleFaces[i].mFontFace == aFontFace) {
       return true;
     }
   }
   return false;
 }
 #endif
 
+bool IsPdfJs(nsIPrincipal* aPrincipal) {
+  if (!aPrincipal) {
+    return false;
+  }
+  nsCOMPtr<nsIURI> uri;
+  aPrincipal->GetURI(getter_AddRefs(uri));
+  return uri && uri->GetSpecOrDefault().EqualsLiteral(
+                    "resource://pdf.js/web/viewer.html");
+}
+
 void FontFaceSet::Add(FontFace& aFontFace, ErrorResult& aRv) {
   FlushUserFontSet();
 
   if (aFontFace.IsInFontFaceSet(this)) {
     return;
   }
 
   if (aFontFace.HasRule()) {
@@ -444,16 +454,26 @@ void FontFaceSet::Add(FontFace& aFontFac
   rec->mLoadEventShouldFire =
       aFontFace.Status() == FontFaceLoadStatus::Unloaded ||
       aFontFace.Status() == FontFaceLoadStatus::Loading;
 
   mNonRuleFacesDirty = true;
   MarkUserFontSetDirty();
   mHasLoadingFontFacesIsDirty = true;
   CheckLoadingStarted();
+  RefPtr<dom::Document> clonedDoc = mDocument->GetLatestStaticClone();
+  if (clonedDoc) {
+    // The document is printing, copy the font to the static clone as well.
+    nsCOMPtr<nsIPrincipal> principal = mDocument->GetPrincipal();
+    if (principal->IsSystemPrincipal() || IsPdfJs(principal)) {
+      ErrorResult rv;
+      clonedDoc->Fonts()->Add(aFontFace, rv);
+      MOZ_ASSERT(!rv.Failed());
+    }
+  }
 }
 
 void FontFaceSet::Clear() {
   FlushUserFontSet();
 
   if (mNonRuleFaces.IsEmpty()) {
     return;
   }