Bug 1321566 Part 1: Use UniquePtr to hold nsPrintData in nsPrintEngine. r=jwatt a=jcristau
authorBob Owen <bobowencode@gmail.com>
Tue, 03 Jan 2017 11:10:11 +0000
changeset 353454 9d7e7bc831d1e9114be01a5738284fd97ddf3d76
parent 353453 043b3b44cd5c8d07140588c2c614b022c4e162f8
child 353455 873a995535164f63c126a34cb91da4095559d67c
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwatt, jcristau
bugs1321566
milestone52.0a2
Bug 1321566 Part 1: Use UniquePtr to hold nsPrintData in nsPrintEngine. r=jwatt a=jcristau
layout/printing/nsPrintEngine.cpp
layout/printing/nsPrintEngine.h
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -212,21 +212,18 @@ NS_IMPL_ISUPPORTS(nsPrintEngine, nsIWebP
 //-- nsPrintEngine Class Impl
 //---------------------------------------------------
 nsPrintEngine::nsPrintEngine() :
   mIsCreatingPrintPreview(false),
   mIsDoingPrinting(false),
   mIsDoingPrintPreview(false),
   mProgressDialogIsShown(false),
   mScreenDPI(115.0f),
-  mPrt(nullptr),
   mPagePrintTimer(nullptr),
   mPageSeqFrame(nullptr),
-  mPrtPreview(nullptr),
-  mOldPrtPreview(nullptr),
   mDebugFile(nullptr),
   mLoadCounter(0),
   mDidLoadDataForPrinting(false),
   mIsDestroying(false),
   mDisallowSelectionPrint(false)
 {
 }
 
@@ -239,45 +236,29 @@ nsPrintEngine::~nsPrintEngine()
 //-------------------------------------------------------
 void nsPrintEngine::Destroy()
 {
   if (mIsDestroying) {
     return;
   }
   mIsDestroying = true;
 
-  if (mPrt) {
-    delete mPrt;
-    mPrt = nullptr;
-  }
+  mPrt = nullptr;
 
 #ifdef NS_PRINT_PREVIEW
-  if (mPrtPreview) {
-    delete mPrtPreview;
-    mPrtPreview = nullptr;
-  }
-
-  // This is insruance
-  if (mOldPrtPreview) {
-    delete mOldPrtPreview;
-    mOldPrtPreview = nullptr;
-  }
-
+  mPrtPreview = nullptr;
+  mOldPrtPreview = nullptr;
 #endif
   mDocViewerPrint = nullptr;
 }
 
 //-------------------------------------------------------
 void nsPrintEngine::DestroyPrintingData()
 {
-  if (mPrt) {
-    nsPrintData* data = mPrt;
-    mPrt = nullptr;
-    delete data;
-  }
+  mPrt = nullptr;
 }
 
 //---------------------------------------------------------------------------------
 //-- Section: Methods needed by the DocViewer
 //---------------------------------------------------------------------------------
 
 //--------------------------------------------------------
 nsresult nsPrintEngine::Initialize(nsIDocumentViewerPrint* aDocViewerPrint, 
@@ -410,17 +391,16 @@ nsPrintEngine::CommonPrint(bool         
     } else {
       SetIsPrinting(false);
     }
     if (mProgressDialogIsShown)
       CloseProgressDialog(aWebProgressListener);
     if (rv != NS_ERROR_ABORT && rv != NS_ERROR_OUT_OF_MEMORY) {
       FirePrintingErrorEvent(rv);
     }
-    delete mPrt;
     mPrt = nullptr;
   }
 
   return rv;
 }
 
 nsresult
 nsPrintEngine::DoCommonPrint(bool                    aIsPrintPreview,
@@ -432,25 +412,25 @@ nsPrintEngine::DoCommonPrint(bool       
 
   if (aIsPrintPreview) {
     // The WebProgressListener can be QI'ed to nsIPrintingPromptService
     // then that means the progress dialog is already being shown.
     nsCOMPtr<nsIPrintingPromptService> pps(do_QueryInterface(aWebProgressListener));
     mProgressDialogIsShown = pps != nullptr;
 
     if (mIsDoingPrintPreview) {
-      mOldPrtPreview = mPrtPreview;
-      mPrtPreview = nullptr;
+      mOldPrtPreview = Move(mPrtPreview);
     }
   } else {
     mProgressDialogIsShown = false;
   }
 
-  mPrt = new nsPrintData(aIsPrintPreview ? nsPrintData::eIsPrintPreview :
-                                           nsPrintData::eIsPrinting);
+  mPrt = mozilla::MakeUnique<nsPrintData>(aIsPrintPreview
+                                          ? nsPrintData::eIsPrintPreview
+                                          : nsPrintData::eIsPrinting);
   NS_ENSURE_TRUE(mPrt, NS_ERROR_OUT_OF_MEMORY);
 
   // if they don't pass in a PrintSettings, then get the Global PS
   mPrt->mPrintSettings = aPrintSettings;
   if (!mPrt->mPrintSettings) {
     rv = GetGlobalPrintSettings(getter_AddRefs(mPrt->mPrintSettings));
     NS_ENSURE_SUCCESS(rv, rv);
   }
@@ -873,19 +853,19 @@ nsPrintEngine::GetPrintPreviewNumPages(i
 
   nsPrintData* prt = nullptr;
   nsIFrame* seqFrame  = nullptr;
   *aPrintPreviewNumPages = 0;
 
   // When calling this function, the FinishPrintPreview() function might not
   // been called as there are still some 
   if (mPrtPreview) {
-    prt = mPrtPreview;
+    prt = mPrtPreview.get();
   } else {
-    prt = mPrt;
+    prt = mPrt.get();
   }
   if ((!prt) ||
       NS_FAILED(GetSeqFrameAndCountPagesInternal(prt->mPrintObject, seqFrame, *aPrintPreviewNumPages))) {
     return NS_ERROR_FAILURE;
   }
   return NS_OK;
 }
 
@@ -3355,20 +3335,20 @@ nsPrintEngine::TurnScriptingOn(bool aDoT
 {
   if (mIsDoingPrinting && aDoTurnOn && mDocViewerPrint &&
       mDocViewerPrint->GetIsPrintPreview()) {
     // We don't want to turn scripting on if print preview is shown still after
     // printing.
     return;
   }
 
-  nsPrintData* prt = mPrt;
+  nsPrintData* prt = mPrt.get();
 #ifdef NS_PRINT_PREVIEW
   if (!prt) {
-    prt = mPrtPreview;
+    prt = mPrtPreview.get();
   }
 #endif
   if (!prt) {
     return;
   }
 
   NS_ASSERTION(mDocument, "We MUST have a document.");
   // First, get the script global object from the document...
@@ -3461,27 +3441,25 @@ nsPrintEngine::FinishPrintPreview()
     return rv;
   }
 
   // At this point we are done preparing everything
   // before it is to be created
 
 
   if (mIsDoingPrintPreview && mOldPrtPreview) {
-    delete mOldPrtPreview;
     mOldPrtPreview = nullptr;
   }
 
 
   mPrt->OnEndPrinting();
 
   // PrintPreview was built using the mPrt (code reuse)
   // then we assign it over
-  mPrtPreview = mPrt;
-  mPrt        = nullptr;
+  mPrtPreview = Move(mPrt);
 
 #endif // NS_PRINT_PREVIEW
 
   return NS_OK;
 }
 
 //-----------------------------------------------------------------
 //-- Done: Finishing up or Cleaning up
--- a/layout/printing/nsPrintEngine.h
+++ b/layout/printing/nsPrintEngine.h
@@ -1,23 +1,23 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 #ifndef nsPrintEngine_h___
 #define nsPrintEngine_h___
 
 #include "mozilla/Attributes.h"
+#include "mozilla/UniquePtr.h"
 
 #include "nsCOMPtr.h"
 
 #include "nsPrintObject.h"
 #include "nsPrintData.h"
 #include "nsFrameList.h"
-#include "mozilla/Attributes.h"
 #include "nsIWebProgress.h"
 #include "mozilla/dom/HTMLCanvasElement.h"
 #include "nsIWebProgressListener.h"
 #include "nsWeakReference.h"
 
 // Interfaces
 #include "nsIObserver.h"
 
@@ -253,23 +253,23 @@ protected:
   bool mIsDoingPrinting;
   bool mIsDoingPrintPreview; // per DocumentViewer
   bool mProgressDialogIsShown;
 
   nsCOMPtr<nsIDocumentViewerPrint> mDocViewerPrint;
   nsWeakPtr               mContainer;
   float                   mScreenDPI;
   
-  nsPrintData*            mPrt;
+  mozilla::UniquePtr<nsPrintData> mPrt;
   nsPagePrintTimer*       mPagePrintTimer;
   nsIPageSequenceFrame*   mPageSeqFrame;
 
   // Print Preview
-  nsPrintData*            mPrtPreview;
-  nsPrintData*            mOldPrtPreview;
+  mozilla::UniquePtr<nsPrintData> mPrtPreview;
+  mozilla::UniquePtr<nsPrintData> mOldPrtPreview;
 
   nsCOMPtr<nsIDocument>   mDocument;
 
   FILE* mDebugFile;
 
   int32_t mLoadCounter;
   bool mDidLoadDataForPrinting;
   bool mIsDestroying;