Bug 1342872 - part2: Name the nsPagePrintTimer runnable and associate its nsITimers with a DocGroup r=bevistseng,jwatt
authorFarmer Tseng <fatseng@mozilla.com>
Thu, 30 Mar 2017 22:49:43 +0800
changeset 351122 fc96236eed2d3eaa4418382f55942672e4046cdd
parent 351121 f0abce7b631fa389383b5b645fd687cb999d0fd0
child 351123 9fa0c7eb6bbeddebed93d0fb353a7dfa6740da7e
push id31601
push userkwierso@gmail.com
push dateTue, 04 Apr 2017 18:18:12 +0000
treeherdermozilla-central@9cb970f15203 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbevistseng, jwatt
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=bevistseng,jwatt MozReview-Commit-ID: AfC84m1CoDm
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
@@ -3493,18 +3493,23 @@ 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
     int32_t printPageDelay = 50;
     mPrt->mPrintSettings->GetPrintPageDelay(&printPageDelay);
 
+    nsCOMPtr<nsIContentViewer> cv = do_QueryInterface(mDocViewerPrint);
+    NS_ENSURE_TRUE(cv, NS_ERROR_FAILURE);
+    nsCOMPtr<nsIDocument> doc = cv->GetDocument();
+    NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
+
     RefPtr<nsPagePrintTimer> timer =
-      new nsPagePrintTimer(this, mDocViewerPrint, printPageDelay);
+      new nsPagePrintTimer(this, mDocViewerPrint, doc, 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) {