Bug 1619403. Merge nsPrintJob's GetDisplayTitleAndURL and GetDocumentTitleAndURL. r=nordzilla
authorJonathan Watt <jwatt@jwatt.org>
Tue, 03 Mar 2020 20:08:53 +0000
changeset 517049 885a99d3d16c003091e94d4c195785778157bbb8
parent 517048 74f8c41627b66b59486d9dd8a93ae69a4d6e03db
child 517050 80d05dc950b1467e8b3c620e496e3aeb6748cffe
push id37185
push userdluca@mozilla.com
push dateThu, 05 Mar 2020 21:27:12 +0000
treeherdermozilla-central@c991b81a7833 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnordzilla
bugs1619403
milestone75.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1619403. Merge nsPrintJob's GetDisplayTitleAndURL and GetDocumentTitleAndURL. r=nordzilla This also removes nsPrintJob::mFallbackDocTitle since there really is no point in blindly calculating it ahead of time when we will virtually never use it, and even if there was there's no reason to store in on the nsPrintJob given that we create a new nsPrintJob every time we print! Differential Revision: https://phabricator.services.mozilla.com/D65014
layout/printing/nsPrintJob.cpp
layout/printing/nsPrintJob.h
--- a/layout/printing/nsPrintJob.cpp
+++ b/layout/printing/nsPrintJob.cpp
@@ -261,47 +261,16 @@ static nsPrintObject* FindPrintObjectByD
     if (po) {
       return po;
     }
   }
 
   return nullptr;
 }
 
-static void GetDocumentTitleAndURL(Document* aDoc, nsAString& aTitle,
-                                   nsAString& aURLStr) {
-  NS_ASSERTION(aDoc, "Pointer is null!");
-
-  aTitle.Truncate();
-  aURLStr.Truncate();
-
-  aDoc->GetTitle(aTitle);
-
-  nsIURI* url = aDoc->GetDocumentURI();
-  if (!url) return;
-
-  nsCOMPtr<nsIURIFixup> urifixup(components::URIFixup::Service());
-  if (!urifixup) return;
-
-  nsCOMPtr<nsIURI> exposableURI;
-  urifixup->CreateExposableURI(url, getter_AddRefs(exposableURI));
-
-  if (!exposableURI) return;
-
-  nsAutoCString urlCStr;
-  nsresult rv = exposableURI->GetSpec(urlCStr);
-  if (NS_FAILED(rv)) return;
-
-  nsCOMPtr<nsITextToSubURI> textToSubURI =
-      do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return;
-
-  textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), urlCStr, aURLStr);
-}
-
 static nsresult GetSeqFrameAndCountPagesInternal(
     const UniquePtr<nsPrintObject>& aPO, nsIFrame*& aSeqFrame,
     int32_t& aCount) {
   NS_ENSURE_ARG_POINTER(aPO);
 
   // This is sometimes incorrectly called before the pres shell has been created
   // (bug 1141756). MOZ_DIAGNOSTIC_ASSERT so we'll still see the crash in
   // Nightly/Aurora in case the other patch fixes this.
@@ -619,30 +588,16 @@ nsresult nsPrintJob::Initialize(nsIDocum
   }
 
   bool hasMozPrintCallback = false;
   DocHasPrintCallbackCanvas(*aOriginalDoc,
                             static_cast<void*>(&hasMozPrintCallback));
   mHasMozPrintCallback =
       hasMozPrintCallback || AnySubdocHasPrintCallbackCanvas(*aOriginalDoc);
 
-  nsCOMPtr<nsIStringBundle> brandBundle;
-  nsCOMPtr<nsIStringBundleService> svc =
-      mozilla::services::GetStringBundleService();
-  if (svc) {
-    svc->CreateBundle("chrome://branding/locale/brand.properties",
-                      getter_AddRefs(brandBundle));
-    if (brandBundle) {
-      brandBundle->GetStringFromName("brandShortName", mFallbackDocTitle);
-    }
-  }
-  if (mFallbackDocTitle.IsEmpty()) {
-    mFallbackDocTitle.AssignLiteral(u"Mozilla Document");
-  }
-
   return NS_OK;
 }
 
 //-------------------------------------------------------
 nsresult nsPrintJob::Cancel() {
   if (mPrt && mPrt->mPrintSettings) {
     return mPrt->mPrintSettings->SetIsCancelled(true);
   }
@@ -1273,59 +1228,85 @@ void nsPrintJob::SetPrintPO(nsPrintObjec
   // Set whether to print flag
   aPO->mDontPrint = !aPrint;
 
   for (const UniquePtr<nsPrintObject>& kid : aPO->mKids) {
     SetPrintPO(kid.get(), aPrint);
   }
 }
 
-//---------------------------------------------------------------------
-// This will first use a Title and/or URL from the PrintSettings
-// if one isn't set then it uses the one from the document
-// then if not title is there we will make sure we send something back
-// depending on the situation.
-void nsPrintJob::GetDisplayTitleAndURL(const UniquePtr<nsPrintObject>& aPO,
-                                       nsAString& aTitle, nsAString& aURLStr,
-                                       eDocTitleDefault aDefType) {
-  NS_ASSERTION(aPO, "Pointer is null!");
-
-  if (!mPrt) return;
-
+// static
+void nsPrintJob::GetDisplayTitleAndURL(Document& aDoc,
+                                       nsIPrintSettings* aSettings,
+                                       DocTitleDefault aTitleDefault,
+                                       nsAString& aTitle, nsAString& aURLStr) {
   aTitle.Truncate();
   aURLStr.Truncate();
 
-  // First check to see if the PrintSettings has defined an alternate title
-  // and use that if it did
-  if (mPrt->mPrintSettings) {
-    mPrt->mPrintSettings->GetTitle(aTitle);
-    mPrt->mPrintSettings->GetDocURL(aURLStr);
-  }
-
-  nsAutoString docTitle;
-  nsAutoString docUrl;
-  GetDocumentTitleAndURL(aPO->mDocument, docTitle, docUrl);
-
-  if (aURLStr.IsEmpty() && !docUrl.IsEmpty()) {
-    aURLStr = docUrl;
+  if (aSettings) {
+    aSettings->GetTitle(aTitle);
+    aSettings->GetDocURL(aURLStr);
   }
 
   if (aTitle.IsEmpty()) {
-    if (!docTitle.IsEmpty()) {
-      aTitle = docTitle;
-    } else {
-      if (aDefType == eDocTitleDefURLDoc) {
-        if (!aURLStr.IsEmpty()) {
-          aTitle = aURLStr;
-        } else if (!mFallbackDocTitle.IsEmpty()) {
-          aTitle = mFallbackDocTitle;
+    aDoc.GetTitle(aTitle);
+    if (aTitle.IsEmpty()) {
+      if (!aURLStr.IsEmpty() &&
+          aTitleDefault == DocTitleDefault::eDocURLElseFallback) {
+        aTitle = aURLStr;
+      } else {
+        nsCOMPtr<nsIStringBundle> brandBundle;
+        nsCOMPtr<nsIStringBundleService> svc =
+            mozilla::services::GetStringBundleService();
+        if (svc) {
+          svc->CreateBundle("chrome://branding/locale/brand.properties",
+                            getter_AddRefs(brandBundle));
+          if (brandBundle) {
+            brandBundle->GetStringFromName("brandShortName", aTitle);
+          }
+        }
+        if (aTitle.IsEmpty()) {
+          aTitle.AssignLiteral(u"Mozilla Document");
         }
       }
     }
   }
+
+  if (aURLStr.IsEmpty()) {
+    nsIURI* url = aDoc.GetDocumentURI();
+    if (!url) {
+      return;
+    }
+
+    nsCOMPtr<nsIURIFixup> urifixup(components::URIFixup::Service());
+    if (!urifixup) {
+      return;
+    }
+
+    nsCOMPtr<nsIURI> exposableURI;
+    urifixup->CreateExposableURI(url, getter_AddRefs(exposableURI));
+    if (!exposableURI) {
+      return;
+    }
+
+    nsAutoCString urlCStr;
+    nsresult rv = exposableURI->GetSpec(urlCStr);
+    if (NS_FAILED(rv)) {
+      return;
+    }
+
+    nsCOMPtr<nsITextToSubURI> textToSubURI =
+        do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
+    if (NS_FAILED(rv)) {
+      return;
+    }
+
+    textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), urlCStr,
+                                   aURLStr);
+  }
 }
 
 //---------------------------------------------------------------------
 nsresult nsPrintJob::DocumentReadyForPrinting() {
   // Send the document to the printer...
   nsresult rv = SetupToPrintContent();
   if (NS_FAILED(rv)) {
     // The print job was canceled or there was a problem
@@ -1646,18 +1627,19 @@ nsresult nsPrintJob::SetupToPrintContent
   printData->mPrintSettings->GetPrintToFile(&isPrintToFile);
   if (isPrintToFile) {
     // On some platforms The BeginDocument needs to know the name of the file.
     printData->mPrintSettings->GetToFileName(fileNameStr);
   }
 
   nsAutoString docTitleStr;
   nsAutoString docURLStr;
-  GetDisplayTitleAndURL(printData->mPrintObject, docTitleStr, docURLStr,
-                        eDocTitleDefURLDoc);
+  GetDisplayTitleAndURL(
+      *printData->mPrintObject->mDocument, printData->mPrintSettings,
+      DocTitleDefault::eDocURLElseFallback, docTitleStr, docURLStr);
 
   int32_t startPage = 1;
   int32_t endPage = printData->mNumPrintablePages;
 
   int16_t printRangeType = nsIPrintSettings::kRangeAllPages;
   printData->mPrintSettings->GetPrintRange(&printRangeType);
   if (printRangeType == nsIPrintSettings::kRangeSpecifiedPageRange) {
     printData->mPrintSettings->GetStartPageRange(&startPage);
@@ -2392,17 +2374,18 @@ nsresult nsPrintJob::DoPrint(const Uniqu
     if (!printData->mPrintSettings) {
       // not sure what to do here!
       SetIsPrinting(false);
       return NS_ERROR_FAILURE;
     }
 
     nsAutoString docTitleStr;
     nsAutoString docURLStr;
-    GetDisplayTitleAndURL(aPO, docTitleStr, docURLStr, eDocTitleDefBlank);
+    GetDisplayTitleAndURL(*aPO->mDocument, mPrt->mPrintSettings,
+                          DocTitleDefault::eFallback, docTitleStr, docURLStr);
 
     if (!seqFrame) {
       SetIsPrinting(false);
       return NS_ERROR_FAILURE;
     }
 
     mPageSeqFrame = seqFrame;
     seqFrame->StartPrint(poPresContext, printData->mPrintSettings, docTitleStr,
@@ -2425,17 +2408,19 @@ void nsPrintJob::SetURLAndTitleOnProgres
 
   if (!aPO || !aPO->mDocShell || !aParams) {
     return;
   }
   const uint32_t kTitleLength = 64;
 
   nsAutoString docTitleStr;
   nsAutoString docURLStr;
-  GetDisplayTitleAndURL(aPO, docTitleStr, docURLStr, eDocTitleDefURLDoc);
+  GetDisplayTitleAndURL(*aPO->mDocument, mPrt->mPrintSettings,
+                        DocTitleDefault::eDocURLElseFallback, docTitleStr,
+                        docURLStr);
 
   // Make sure the Titles & URLS don't get too long for the progress dialog
   EllipseLongString(docTitleStr, kTitleLength, false);
   EllipseLongString(docURLStr, kTitleLength, true);
 
   aParams->SetDocTitle(docTitleStr);
   aParams->SetDocURL(docURLStr);
 }
--- a/layout/printing/nsPrintJob.h
+++ b/layout/printing/nsPrintJob.h
@@ -121,20 +121,16 @@ class nsPrintJob final : public nsIObser
 
   // The setters here also update the DocViewer
   void SetIsPrinting(bool aIsPrinting);
   bool GetIsPrinting() { return mIsDoingPrinting; }
   void SetIsPrintPreview(bool aIsPrintPreview);
   bool GetIsPrintPreview() { return mIsDoingPrintPreview; }
   bool GetIsCreatingPrintPreview() { return mIsCreatingPrintPreview; }
 
-  // This enum tells indicates what the default should be for the title
-  // if the title from the document is null
-  enum eDocTitleDefault { eDocTitleDefBlank, eDocTitleDefURLDoc };
-
   nsresult GetSeqFrameAndCountPages(nsIFrame*& aSeqFrame, int32_t& aCount);
 
   void TurnScriptingOn(bool aDoTurnOn);
 
   /**
    * Checks to see if the document this print engine is associated with has any
    * canvases that have a mozPrintCallback.
    * https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement#Properties
@@ -206,19 +202,34 @@ class nsPrintJob final : public nsIObser
   bool IsWindowsInOurSubTree(nsPIDOMWindowOuter* aDOMWindow) const;
   bool IsThereAnIFrameSelected(nsIDocShell* aDocShell,
                                nsPIDOMWindowOuter* aDOMWin,
                                bool& aIsParentFrameSet);
 
   // get the currently infocus frame for the document viewer
   already_AddRefed<nsPIDOMWindowOuter> FindFocusedDOMWindow() const;
 
-  void GetDisplayTitleAndURL(const mozilla::UniquePtr<nsPrintObject>& aPO,
-                             nsAString& aTitle, nsAString& aURLStr,
-                             eDocTitleDefault aDefType);
+  /// Customizes the behaviour of GetDisplayTitleAndURL.
+  enum class DocTitleDefault : uint32_t { eDocURLElseFallback, eFallback };
+
+  /**
+   * Gets the title and URL of the document for display in save-to-PDF dialogs,
+   * print spooler lists and page headers/footers.  This will get the title/URL
+   * from the PrintSettings, if set, otherwise it will get them from the
+   * document.
+   *
+   * For the title specifically, if a value is not provided by the settings
+   * object or the document then, if eDocURLElseFallback is passed, the document
+   * URL will be returned as the title if it's non-empty (which should always be
+   * the case).  Otherwise a non-empty fallback title will be returned.
+   */
+  static void GetDisplayTitleAndURL(mozilla::dom::Document& aDoc,
+                                    nsIPrintSettings* aSettings,
+                                    DocTitleDefault aTitleDefault,
+                                    nsAString& aTitle, nsAString& aURLStr);
 
   MOZ_CAN_RUN_SCRIPT nsresult
   CommonPrint(bool aIsPrintPreview, nsIPrintSettings* aPrintSettings,
               nsIWebProgressListener* aWebProgressListener,
               mozilla::dom::Document* aSourceDoc);
 
   MOZ_CAN_RUN_SCRIPT nsresult
   DoCommonPrint(bool aIsPrintPreview, nsIPrintSettings* aPrintSettings,
@@ -277,18 +288,16 @@ class nsPrintJob final : public nsIObser
   // The nsPrintData for our last print preview (replaced every time the
   // user changes settings in the print preview window).
   // Note: Our new print preview nsPrintData is stored in mPtr until we move it
   // to mPrtPreview once we've finish creating the print preview.
   RefPtr<nsPrintData> mPrtPreview;
 
   nsPagePrintTimer* mPagePrintTimer = nullptr;
 
-  nsString mFallbackDocTitle;
-
   float mScreenDPI = 115.0f;
   int32_t mLoadCounter = 0;
 
   bool mIsCreatingPrintPreview = false;
   bool mIsDoingPrinting = false;
   bool mIsDoingPrintPreview = false;
   bool mProgressDialogIsShown = false;
   bool mDidLoadDataForPrinting = false;