Bug 1342872 - part2: Name the nsPagePrintTimer runnable and associate its nsITimers with a DocGroup r=jwatt
authorFarmer Tseng <fatseng@mozilla.com>
Fri, 17 Mar 2017 15:22:41 +0800
changeset 350045 ee20d9c4b63c985582c6a971abaf9aff6912dd54
parent 350044 44eae28def02260e6a442bfe74a26bac89756f2c
child 350046 0867aa253746b354813b514dd84a8cb6ec63f153
push id31567
push userkwierso@gmail.com
push dateTue, 28 Mar 2017 20:16:07 +0000
treeherdermozilla-central@e23cf1b38ad4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwatt
bugs1342872
milestone55.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 1342872 - part2: Name the nsPagePrintTimer runnable and associate its nsITimers with a DocGroup r=jwatt MozReview-Commit-ID: 3f3n0l1LgHO
layout/printing/nsPagePrintTimer.cpp
layout/printing/nsPagePrintTimer.h
layout/printing/nsPrintEngine.cpp
--- a/layout/printing/nsPagePrintTimer.cpp
+++ b/layout/printing/nsPagePrintTimer.cpp
@@ -5,47 +5,50 @@
 
 #include "nsPagePrintTimer.h"
 
 #include "mozilla/Unused.h"
 #include "nsIContentViewer.h"
 #include "nsIServiceManager.h"
 #include "nsPrintEngine.h"
 
+using namespace mozilla;
+
 NS_IMPL_ISUPPORTS_INHERITED(nsPagePrintTimer, mozilla::Runnable, nsITimerCallback)
 
 nsPagePrintTimer::~nsPagePrintTimer()
 {
   // "Destroy" the document viewer; this normally doesn't actually
   // destroy it because of the IncrementDestroyRefCount call below
   // XXX This is messy; the document viewer should use a single approach
   // to keep itself alive during printing
   nsCOMPtr<nsIContentViewer> cv(do_QueryInterface(mDocViewerPrint));
   if (cv) {
     cv->Destroy();
   }
 }
 
-nsresult 
+nsresult
 nsPagePrintTimer::StartTimer(bool aUseDelay)
 {
   nsresult result;
   mTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
   if (NS_FAILED(result)) {
     NS_WARNING("unable to start the timer");
   } else {
     uint32_t delay = 0;
     if (aUseDelay) {
       if (mFiringCount < 10) {
         // Longer delay for the few first pages.
         delay = mDelay + ((10 - mFiringCount) * 100);
       } else {
         delay = mDelay;
       }
     }
+    mTimer->SetTarget(mDocument->EventTargetFor(TaskCategory::Other));
     mTimer->InitWithCallback(this, delay, nsITimer::TYPE_ONE_SHOT);
   }
   return result;
 }
 
 nsresult
 nsPagePrintTimer::StartWatchDogTimer()
 {
@@ -54,16 +57,17 @@ nsPagePrintTimer::StartWatchDogTimer()
     mWatchDogTimer->Cancel();
   }
   mWatchDogTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
   if (NS_FAILED(result)) {
     NS_WARNING("unable to start the timer");
   } else {
     // Instead of just doing one timer for a long period do multiple so we
     // can check if the user cancelled the printing.
+    mWatchDogTimer->SetTarget(mDocument->EventTargetFor(TaskCategory::Other));
     mWatchDogTimer->InitWithCallback(this, WATCH_DOG_INTERVAL,
                                      nsITimer::TYPE_ONE_SHOT);
   }
   return result;
 }
 
 void
 nsPagePrintTimer::StopWatchDogTimer()
@@ -153,17 +157,18 @@ nsPagePrintTimer::Notify(nsITimer *timer
   if (mDocViewerPrint) {
     bool donePrePrint = true;
     if (mPrintEngine) {
       donePrePrint = mPrintEngine->PrePrintPage();
     }
 
     if (donePrePrint && !mWaitingForRemotePrint) {
       StopWatchDogTimer();
-      NS_DispatchToMainThread(this);
+      // Pass nullptr here since name already was set in constructor.
+      mDocument->Dispatch(nullptr, TaskCategory::Other, do_AddRef(this));
     } else {
       // Start the watch dog if we're waiting for preprint to ensure that if any
       // mozPrintCallbacks take to long we error out.
       StartWatchDogTimer();
     }
 
   }
   return NS_OK;
@@ -183,30 +188,32 @@ nsPagePrintTimer::WaitForRemotePrint()
 
 void
 nsPagePrintTimer::RemotePrintFinished()
 {
   if (!mWaitingForRemotePrint) {
     return;
   }
 
+  mWaitingForRemotePrint->SetTarget(
+    mDocument->EventTargetFor(mozilla::TaskCategory::Other));
   mozilla::Unused <<
     mWaitingForRemotePrint->InitWithCallback(this, 0, nsITimer::TYPE_ONE_SHOT);
 }
 
-nsresult 
+nsresult
 nsPagePrintTimer::Start(nsPrintObject* aPO)
 {
   mPrintObj = aPO;
   mDone = false;
   return StartTimer(false);
 }
 
 
-void  
+void
 nsPagePrintTimer::Stop()
 {
   if (mTimer) {
     mTimer->Cancel();
     mTimer = nullptr;
   }
   StopWatchDogTimer();
 }
--- a/layout/printing/nsPagePrintTimer.h
+++ b/layout/printing/nsPagePrintTimer.h
@@ -9,38 +9,43 @@
 #include "nsITimer.h"
 
 #include "nsIDocumentViewerPrint.h"
 #include "nsPrintObject.h"
 #include "mozilla/Attributes.h"
 #include "nsThreadUtils.h"
 
 class nsPrintEngine;
+class nsIDocument;
 
 //---------------------------------------------------
 //-- Page Timer Class
 //---------------------------------------------------
 class nsPagePrintTimer final : public mozilla::Runnable,
                                public nsITimerCallback
 {
 public:
 
   NS_DECL_ISUPPORTS_INHERITED
 
   nsPagePrintTimer(nsPrintEngine* aPrintEngine,
                    nsIDocumentViewerPrint* aDocViewerPrint,
+                   nsIDocument* aDocument,
                    uint32_t aDelay)
-    : mPrintEngine(aPrintEngine)
+    : Runnable("nsPagePrintTimer")
+    , mPrintEngine(aPrintEngine)
     , mDocViewerPrint(aDocViewerPrint)
+    , mDocument(aDocument)
     , mDelay(aDelay)
     , mFiringCount(0)
     , mPrintObj(nullptr)
     , mWatchDogCount(0)
     , mDone(false)
   {
+    MOZ_ASSERT(aDocument);
     mDocViewerPrint->IncrementDestroyRefCount();
   }
 
   NS_DECL_NSITIMERCALLBACK
 
   nsresult Start(nsPrintObject* aPO);
 
   NS_IMETHOD Run() override;
@@ -57,16 +62,17 @@ private:
 
   nsresult StartTimer(bool aUseDelay);
   nsresult StartWatchDogTimer();
   void     StopWatchDogTimer();
   void     Fail();
 
   nsPrintEngine*             mPrintEngine;
   nsCOMPtr<nsIDocumentViewerPrint> mDocViewerPrint;
+  nsCOMPtr<nsIDocument>      mDocument;
   nsCOMPtr<nsITimer>         mTimer;
   nsCOMPtr<nsITimer>         mWatchDogTimer;
   nsCOMPtr<nsITimer>         mWaitingForRemotePrint;
   uint32_t                   mDelay;
   uint32_t                   mFiringCount;
   nsPrintObject *            mPrintObj;
   uint32_t                   mWatchDogCount;
   bool                       mDone;
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -3490,21 +3490,25 @@ nsPrintEngine::FinishPrintPreview()
 
 /*=============== Timer Related Code ======================*/
 nsresult
 nsPrintEngine::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
+    if (!mDocument) {
+      MOZ_LOG(gPrintingLog, LogLevel::Error,("Error! mDocument is NULL"));
+      return NS_ERROR_FAILURE;
+    }
     int32_t printPageDelay = 50;
     mPrt->mPrintSettings->GetPrintPageDelay(&printPageDelay);
 
     RefPtr<nsPagePrintTimer> timer =
-      new nsPagePrintTimer(this, mDocViewerPrint, printPageDelay);
+      new nsPagePrintTimer(this, mDocViewerPrint, mDocument, printPageDelay);
     timer.forget(&mPagePrintTimer);
 
     nsCOMPtr<nsIPrintSession> printSession;
     nsresult rv = mPrt->mPrintSettings->GetPrintSession(getter_AddRefs(printSession));
     if (NS_SUCCEEDED(rv) && printSession) {
       RefPtr<mozilla::layout::RemotePrintJobChild> remotePrintJob;
       printSession->GetRemotePrintJob(getter_AddRefs(remotePrintJob));
       if (NS_SUCCEEDED(rv) && remotePrintJob) {