Bug 1770539 p1 - Move nsPrintData::mPrintSettings to nsPrintJob. r=emilio
authorJonathan Watt <jwatt@jwatt.org>
Sun, 22 May 2022 09:37:55 +0000
changeset 618493 f6e9ace877bde122f61baa5d298aea48ad26f8b6
parent 618492 28d8297085fe0974241e612f2204a1d6a0169d4c
child 618494 5cd20e839a7322ad075dcc6d88585a28d36db87d
push id163296
push userjwatt@jwatt.org
push dateSun, 22 May 2022 09:41:02 +0000
treeherderautoland@d0bb4f6201a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1770539
milestone102.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 1770539 p1 - Move nsPrintData::mPrintSettings to nsPrintJob. r=emilio Differential Revision: https://phabricator.services.mozilla.com/D146973
layout/printing/nsPrintData.h
layout/printing/nsPrintJob.cpp
layout/printing/nsPrintJob.h
--- a/layout/printing/nsPrintData.h
+++ b/layout/printing/nsPrintData.h
@@ -62,18 +62,16 @@ class nsPrintData {
 
   bool mOnStartSent;
   bool mIsAborted;          // tells us the document is being aborted
   bool mPreparingForPrint;  // see comments above
   bool mShrinkToFit;
   int32_t mNumPrintablePages;
   float mShrinkRatio;
 
-  nsCOMPtr<nsIPrintSettings> mPrintSettings;
-
  private:
   nsPrintData() = delete;
   nsPrintData& operator=(const nsPrintData& aOther) = delete;
 
   ~nsPrintData();  // non-virtual
 };
 
 #endif /* nsPrintData_h___ */
--- a/layout/printing/nsPrintJob.cpp
+++ b/layout/printing/nsPrintJob.cpp
@@ -145,51 +145,46 @@ static void DumpPrintObjectsTreeLayout(c
 #endif
 
 // -------------------------------------------------------
 // Helpers
 // -------------------------------------------------------
 
 /**
  * Build a tree of nsPrintObjects under aPO. It also appends a (depth first)
- * flat list of all the nsPrintObjects created to aPrintData->mPrintDocList. If
+ * flat list of all the nsPrintObjects created to mPrt->mPrintDocList. If
  * one of the nsPrintObject's document is the focused document, then the print
- * object is set as aPrintData->mSelectionRoot.
+ * object is set as mPrt->mSelectionRoot.
  * @param aParentPO The parent nsPrintObject to populate, must not be null.
- * @param aFocusedDoc Document from the window that had focus when print was
- *                    initiated.
- * @param aPrintData nsPrintData for the current print, must not be null.
  */
-static void BuildNestedPrintObjects(const UniquePtr<nsPrintObject>& aParentPO,
-                                    RefPtr<nsPrintData>& aPrintData) {
+void nsPrintJob::BuildNestedPrintObjects(
+    const UniquePtr<nsPrintObject>& aParentPO) {
   MOZ_ASSERT(aParentPO);
-  MOZ_ASSERT(aPrintData);
 
   // If aParentPO is for an iframe and its original document was the document
   // that had focus then always set as the selection root.
   if (aParentPO->mFrameType == eIFrame &&
       aParentPO->mDocument->GetProperty(nsGkAtoms::printisfocuseddoc)) {
-    aPrintData->mSelectionRoot = aParentPO.get();
-  } else if (!aPrintData->mSelectionRoot && aParentPO->HasSelection()) {
+    mPrt->mSelectionRoot = aParentPO.get();
+  } else if (!mPrt->mSelectionRoot && aParentPO->HasSelection()) {
     // If there is no focused iframe but there is a selection in one or more
     // frames then we want to set the root nsPrintObject as the focus root so
     // that later EnablePrintingSelectionOnly can search for and enable all
     // nsPrintObjects containing selections.
-    aPrintData->mSelectionRoot = aPrintData->mPrintObject.get();
+    mPrt->mSelectionRoot = mPrt->mPrintObject.get();
   }
 
   for (auto& bc : aParentPO->mDocShell->GetBrowsingContext()->Children()) {
     nsCOMPtr<nsIDocShell> docShell = bc->GetDocShell();
     if (!docShell) {
       if (auto* cc = dom::ContentChild::GetSingleton()) {
         nsCOMPtr<nsIPrintSettingsService> printSettingsService =
             do_GetService(sPrintSettingsServiceContractID);
         embedding::PrintData printData;
-        printSettingsService->SerializeToPrintData(aPrintData->mPrintSettings,
-                                                   &printData);
+        printSettingsService->SerializeToPrintData(mPrintSettings, &printData);
         Unused << cc->SendUpdateRemotePrintSettings(bc, printData);
       }
       continue;
     }
 
     RefPtr<Document> doc = docShell->GetDocument();
     MOZ_DIAGNOSTIC_ASSERT(doc);
     // We might find non-static documents here if the fission remoteness change
@@ -201,18 +196,18 @@ static void BuildNestedPrintObjects(cons
     }
 
     auto childPO = MakeUnique<nsPrintObject>();
     nsresult rv = childPO->InitAsNestedObject(docShell, doc, aParentPO.get());
     if (NS_FAILED(rv)) {
       MOZ_ASSERT_UNREACHABLE("Init failed?");
     }
 
-    aPrintData->mPrintDocList.AppendElement(childPO.get());
-    BuildNestedPrintObjects(childPO, aPrintData);
+    mPrt->mPrintDocList.AppendElement(childPO.get());
+    BuildNestedPrintObjects(childPO);
     aParentPO->mKids.AppendElement(std::move(childPO));
   }
 }
 
 static nsresult GetDefaultPrintSettings(nsIPrintSettings** aSettings) {
   *aSettings = nullptr;
 
   nsresult rv = NS_ERROR_FAILURE;
@@ -402,72 +397,71 @@ nsresult nsPrintJob::DoCommonPrint(bool 
     printData->mPrintProgressListeners.AppendElement(mRemotePrintJob);
   }
 
   // Get the docshell for this documentviewer
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // if they don't pass in a PrintSettings, then get the Global PS
-  printData->mPrintSettings = aPrintSettings;
-  if (!printData->mPrintSettings) {
-    MOZ_TRY(GetDefaultPrintSettings(getter_AddRefs(printData->mPrintSettings)));
+  mPrintSettings = aPrintSettings;
+  if (!mPrintSettings) {
+    MOZ_TRY(GetDefaultPrintSettings(getter_AddRefs(mPrintSettings)));
   }
 
   {
     nsAutoScriptBlocker scriptBlocker;
     printData->mPrintObject = MakeUnique<nsPrintObject>();
     rv = printData->mPrintObject->InitAsRootObject(docShell, aDoc,
                                                    mIsCreatingPrintPreview);
     NS_ENSURE_SUCCESS(rv, rv);
 
     printData->mPrintDocList.AppendElement(printData->mPrintObject.get());
 
     printData->mPrintObject->mFrameType = eDoc;
 
-    BuildNestedPrintObjects(printData->mPrintObject, printData);
+    BuildNestedPrintObjects(printData->mPrintObject);
   }
 
   // The nsAutoScriptBlocker above will now have been destroyed, which may
   // cause our print/print-preview operation to finish. In this case, we
   // should immediately return an error code so that the root caller knows
   // it shouldn't continue to do anything with this instance.
   if (mIsDestroying) {
     return NS_ERROR_FAILURE;
   }
 
   // XXX This isn't really correct...
   if (!printData->mPrintObject->mDocument ||
       !printData->mPrintObject->mDocument->GetRootElement())
     return NS_ERROR_GFX_PRINTER_STARTDOC;
 
-  printData->mPrintSettings->GetShrinkToFit(&printData->mShrinkToFit);
+  mPrintSettings->GetShrinkToFit(&printData->mShrinkToFit);
 
   bool printingViaParent =
       XRE_IsContentProcess() && StaticPrefs::print_print_via_parent();
   nsCOMPtr<nsIDeviceContextSpec> devspec;
   if (printingViaParent) {
     devspec = new nsDeviceContextSpecProxy(mRemotePrintJob);
   } else {
     devspec = do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   bool printSilently = false;
-  printData->mPrintSettings->GetPrintSilent(&printSilently);
+  mPrintSettings->GetPrintSilent(&printSilently);
   if (StaticPrefs::print_always_print_silent()) {
     printSilently = true;
   }
 
   if (mIsDoingPrinting && printSilently) {
     Telemetry::ScalarAdd(Telemetry::ScalarID::PRINTING_SILENT_PRINT, 1);
   }
 
-  MOZ_TRY(devspec->Init(nullptr, printData->mPrintSettings,
-                        mIsCreatingPrintPreview));
+  MOZ_TRY(devspec->Init(nullptr, mPrintSettings, mIsCreatingPrintPreview));
 
   printData->mPrintDC = new nsDeviceContext();
   MOZ_TRY(printData->mPrintDC->InitForPrinting(devspec));
 
   MOZ_TRY(EnablePOsForPrinting());
 
   if (!mIsCreatingPrintPreview) {
     printData->OnStartPrinting();
@@ -894,33 +888,33 @@ nsresult nsPrintJob::SetupToPrintContent
   // If the printing was canceled or restarted with different data,
   // let's stop doing this printing.
   if (NS_WARN_IF(mPrt != printData)) {
     return NS_ERROR_FAILURE;
   }
 
   nsAutoString fileNameStr;
   // check to see if we are printing to a file
-  if (printData->mPrintSettings->GetOutputDestination() ==
+  if (mPrintSettings->GetOutputDestination() ==
       nsIPrintSettings::kOutputDestinationFile) {
     // On some platforms the BeginDocument needs to know the name of the file.
-    printData->mPrintSettings->GetToFileName(fileNameStr);
+    mPrintSettings->GetToFileName(fileNameStr);
   }
 
   nsAutoString docTitleStr;
   nsAutoString docURLStr;
-  GetDisplayTitleAndURL(
-      *printData->mPrintObject->mDocument, printData->mPrintSettings,
-      DocTitleDefault::eDocURLElseFallback, docTitleStr, docURLStr);
+  GetDisplayTitleAndURL(*printData->mPrintObject->mDocument, mPrintSettings,
+                        DocTitleDefault::eDocURLElseFallback, docTitleStr,
+                        docURLStr);
 
   int32_t startPage = 1;
   int32_t endPage = printData->mNumPrintablePages;
 
   nsTArray<int32_t> ranges;
-  printData->mPrintSettings->GetPageRanges(ranges);
+  mPrintSettings->GetPageRanges(ranges);
   for (size_t i = 0; i < ranges.Length(); i += 2) {
     startPage = std::max(1, std::min(startPage, ranges[i]));
     endPage = std::min(printData->mNumPrintablePages,
                        std::max(endPage, ranges[i + 1]));
   }
 
   nsresult rv = NS_OK;
   // BeginDocument may pass back a FAILURE code
@@ -934,17 +928,17 @@ nsresult nsPrintJob::SetupToPrintContent
 
   if (mIsCreatingPrintPreview) {
     // Copy docTitleStr and docURLStr to the pageSequenceFrame, to be displayed
     // in the header
     nsPageSequenceFrame* seqFrame =
         printData->mPrintObject->mPresShell->GetPageSequenceFrame();
     if (seqFrame) {
       seqFrame->StartPrint(printData->mPrintObject->mPresContext,
-                           printData->mPrintSettings, docTitleStr, docURLStr);
+                           mPrintSettings, docTitleStr, docURLStr);
     }
   }
 
   PR_PL(("****************** Begin Document ************************\n"));
 
   if (NS_FAILED(rv)) {
     NS_WARNING_ASSERTION(rv == NS_ERROR_ABORT,
                          "Failed to begin document for printing");
@@ -1147,17 +1141,17 @@ void nsPrintJob::UpdateZoomRatio(nsPrint
       // If we're actually going to scale (the factor is less than 1), we
       // reduce the scale factor slightly to avoid the possibility of floating
       // point rounding error causing slight clipping of the longest lines.
       if (aPO->mZoomRatio != 1.0f) {
         aPO->mZoomRatio -= 0.005f;
       }
     } else {
       double scaling;
-      mPrt->mPrintSettings->GetScaling(&scaling);
+      mPrintSettings->GetScaling(&scaling);
       aPO->mZoomRatio = float(scaling);
     }
   }
 }
 
 nsresult nsPrintJob::UpdateSelectionAndShrinkPrintObject(
     nsPrintObject* aPO, bool aDocumentIsTopLevel) {
   PresShell* displayPresShell = aPO->mDocShell->GetPresShell();
@@ -1304,34 +1298,34 @@ nsresult nsPrintJob::ReflowPrintObject(c
   nsPresContext::nsPresContextType type =
       mIsCreatingPrintPreview ? nsPresContext::eContext_PrintPreview
                               : nsPresContext::eContext_Print;
   const bool shouldBeRoot =
       (!aPO->mParent || !aPO->mParent->PrintingIsEnabled()) &&
       !GetParentViewForRoot();
   aPO->mPresContext = shouldBeRoot ? new nsRootPresContext(aPO->mDocument, type)
                                    : new nsPresContext(aPO->mDocument, type);
-  aPO->mPresContext->SetPrintSettings(printData->mPrintSettings);
+  aPO->mPresContext->SetPrintSettings(mPrintSettings);
 
   // init it with the DC
   MOZ_TRY(aPO->mPresContext->Init(printData->mPrintDC));
 
   aPO->mViewManager = new nsViewManager();
 
   MOZ_TRY(aPO->mViewManager->Init(printData->mPrintDC));
 
   aPO->mPresShell =
       aPO->mDocument->CreatePresShell(aPO->mPresContext, aPO->mViewManager);
   if (!aPO->mPresShell) {
     return NS_ERROR_FAILURE;
   }
 
   // If we're printing selection then remove the nonselected nodes from our
   // cloned document.
-  if (printData->mPrintSettings->GetPrintSelectionOnly()) {
+  if (mPrintSettings->GetPrintSelectionOnly()) {
     // If we fail to remove the nodes then we should fail to print, because if
     // the user was trying to print a small selection from a large document,
     // sending the whole document to a real printer would be very frustrating.
     MOZ_TRY(DeleteNonSelectedNodes(*aPO->mDocument));
   }
 
   bool doReturn = false;
   bool documentIsTopLevel = false;
@@ -1351,17 +1345,17 @@ nsresult nsPrintJob::ReflowPrintObject(c
 
   // Here, we inform nsPresContext of the page size. Note that 'adjSize' is
   // *usually* the page size, but we need to check. Strictly speaking, adjSize
   // is the *device output size*, which is really the dimensions of a "sheet"
   // rather than a "page" (an important distinction in an N-pages-per-sheet
   // scenario). For some pages-per-sheet values, the pages are orthogonal to
   // the sheet; we adjust for that here by swapping the width with the height.
   nsSize pageSize = adjSize;
-  if (printData->mPrintSettings->HasOrthogonalSheetsAndPages()) {
+  if (mPrintSettings->HasOrthogonalSheetsAndPages()) {
     std::swap(pageSize.width, pageSize.height);
   }
 
   // If the document has a specified CSS page-size, we rotate the page to
   // reflect this. Changing the orientation is reflected by the result of
   // FinishPrintPreview, so that the frontend can reflect this.
   // The new document has not yet been reflowed, so we have to query the
   // original document for any CSS page-size.
@@ -1683,35 +1677,35 @@ nsresult nsPrintJob::DoPrint(const Uniqu
       nsAutoCString docStr;
       nsAutoCString urlStr;
       GetDocTitleAndURL(aPO, docStr, urlStr);
       DumpLayoutData(docStr.get(), urlStr.get(), poPresContext,
                      printData->mPrintDC, rootFrame, aPO->mDocShell, nullptr);
     }
 #endif
 
-    if (!printData->mPrintSettings) {
+    if (!mPrintSettings) {
       // not sure what to do here!
       SetIsPrinting(false);
       return NS_ERROR_FAILURE;
     }
 
     nsAutoString docTitleStr;
     nsAutoString docURLStr;
-    GetDisplayTitleAndURL(*aPO->mDocument, mPrt->mPrintSettings,
+    GetDisplayTitleAndURL(*aPO->mDocument, mPrintSettings,
                           DocTitleDefault::eFallback, docTitleStr, docURLStr);
 
     if (!seqFrame) {
       SetIsPrinting(false);
       return NS_ERROR_FAILURE;
     }
 
     // For telemetry, get paper size being used; convert the dimensions to
     // points and ensure they reflect portrait orientation.
-    nsIPrintSettings* settings = printData->mPrintSettings;
+    nsIPrintSettings* settings = mPrintSettings;
     double paperWidth, paperHeight;
     settings->GetPaperWidth(&paperWidth);
     settings->GetPaperHeight(&paperHeight);
     int16_t sizeUnit;
     settings->GetPaperSizeUnit(&sizeUnit);
     switch (sizeUnit) {
       case nsIPrintSettings::kPaperSizeInches:
         paperWidth *= 72.0;
@@ -1937,24 +1931,24 @@ bool nsPrintJob::DonePrintingSheets(nsPr
 //-------------------------------------------------------
 nsresult nsPrintJob::EnablePOsForPrinting() {
   // Guarantee that mPrt and the objects it owns won't be deleted.
   RefPtr<nsPrintData> printData = mPrt;
 
   // NOTE: All POs have been "turned off" for printing
   // this is where we decided which POs get printed.
 
-  if (!printData || !printData->mPrintSettings) {
+  if (!printData || !mPrintSettings) {
     return NS_ERROR_FAILURE;
   }
 
   PR_PL(("\n"));
   PR_PL(("********* nsPrintJob::EnablePOsForPrinting *********\n"));
 
-  if (!printData->mPrintSettings->GetPrintSelectionOnly()) {
+  if (!mPrintSettings->GetPrintSelectionOnly()) {
     printData->mPrintObject->EnablePrinting(true);
     return NS_OK;
   }
 
   // This means we are either printing a selected iframe or
   // we are printing the current selection.
   NS_ENSURE_STATE(!mDisallowSelectionPrint && printData->mSelectionRoot);
 
@@ -2046,17 +2040,17 @@ nsresult nsPrintJob::FinishPrintPreview(
 //-- Done: Finishing up or Cleaning up
 //-----------------------------------------------------------------
 
 /*=============== Timer Related Code ======================*/
 nsresult nsPrintJob::StartPagePrintTimer(const UniquePtr<nsPrintObject>& aPO) {
   if (!mPagePrintTimer) {
     // Get the delay time in between the printing of each page
     // this gives the user more time to press cancel
-    int32_t printPageDelay = mPrt->mPrintSettings->GetPrintPageDelay();
+    int32_t printPageDelay = mPrintSettings->GetPrintPageDelay();
 
     nsCOMPtr<nsIContentViewer> cv = do_QueryInterface(mDocViewerPrint);
     NS_ENSURE_TRUE(cv, NS_ERROR_FAILURE);
     nsCOMPtr<Document> doc = cv->GetDocument();
     NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
 
     mPagePrintTimer =
         new nsPagePrintTimer(this, mDocViewerPrint, doc, printPageDelay);
--- a/layout/printing/nsPrintJob.h
+++ b/layout/printing/nsPrintJob.h
@@ -157,16 +157,19 @@ class nsPrintJob final : public nsIWebPr
   nsPrintJob& operator=(const nsPrintJob& aOther) = delete;
 
   ~nsPrintJob();
 
   MOZ_CAN_RUN_SCRIPT nsresult DocumentReadyForPrinting();
   MOZ_CAN_RUN_SCRIPT nsresult SetupToPrintContent();
   nsresult EnablePOsForPrinting();
 
+  void BuildNestedPrintObjects(
+      const mozilla::UniquePtr<nsPrintObject>& aParentPO);
+
   bool PrintDocContent(const mozilla::UniquePtr<nsPrintObject>& aPO,
                        nsresult& aStatus);
   nsresult DoPrint(const mozilla::UniquePtr<nsPrintObject>& aPO);
 
   nsresult ReflowDocList(const mozilla::UniquePtr<nsPrintObject>& aPO);
 
   MOZ_CAN_RUN_SCRIPT_BOUNDARY
   nsresult ReflowPrintObject(const mozilla::UniquePtr<nsPrintObject>& aPO);
@@ -226,16 +229,18 @@ class nsPrintJob final : public nsIWebPr
   MOZ_CAN_RUN_SCRIPT nsresult ReconstructAndReflow();
   MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult UpdateSelectionAndShrinkPrintObject(
       nsPrintObject* aPO, bool aDocumentIsTopLevel);
   MOZ_CAN_RUN_SCRIPT nsresult InitPrintDocConstruction(bool aHandleError);
   void FirePrintPreviewUpdateEvent();
 
   void PageDone(nsresult aResult);
 
+  nsCOMPtr<nsIPrintSettings> mPrintSettings;
+
   // The docViewer that owns us, and its docShell.
   nsCOMPtr<nsIDocumentViewerPrint> mDocViewerPrint;
   nsWeakPtr mDocShell;
 
   WeakFrame mPageSeqFrame;
 
   // We are the primary owner of our nsPrintData member vars.  These vars
   // are refcounted so that functions (e.g. nsPrintData methods) can create