1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsPagePrintTimer.h"
7 #include "nsIContentViewer.h"
8 #include "nsIServiceManager.h"
9 #include "nsPrintEngine.h"
11 NS_IMPL_ISUPPORTS_INHERITED(nsPagePrintTimer
, nsRunnable
, nsITimerCallback
)
13 nsPagePrintTimer::~nsPagePrintTimer()
15 // "Destroy" the document viewer; this normally doesn't actually
16 // destroy it because of the IncrementDestroyRefCount call below
17 // XXX This is messy; the document viewer should use a single approach
18 // to keep itself alive during printing
19 nsCOMPtr
<nsIContentViewer
> cv(do_QueryInterface(mDocViewerPrint
));
26 nsPagePrintTimer::StartTimer(bool aUseDelay
)
29 mTimer
= do_CreateInstance("@mozilla.org/timer;1", &result
);
30 if (NS_FAILED(result
)) {
31 NS_WARNING("unable to start the timer");
35 if (mFiringCount
< 10) {
36 // Longer delay for the few first pages.
37 delay
= mDelay
+ ((10 - mFiringCount
) * 100);
42 mTimer
->InitWithCallback(this, delay
, nsITimer::TYPE_ONE_SHOT
);
48 nsPagePrintTimer::StartWatchDogTimer()
52 mWatchDogTimer
->Cancel();
54 mWatchDogTimer
= do_CreateInstance("@mozilla.org/timer;1", &result
);
55 if (NS_FAILED(result
)) {
56 NS_WARNING("unable to start the timer");
58 // Instead of just doing one timer for a long period do multiple so we
59 // can check if the user cancelled the printing.
60 mWatchDogTimer
->InitWithCallback(this, WATCH_DOG_INTERVAL
,
61 nsITimer::TYPE_ONE_SHOT
);
67 nsPagePrintTimer::StopWatchDogTimer()
70 mWatchDogTimer
->Cancel();
71 mWatchDogTimer
= nullptr;
77 nsPagePrintTimer::Run()
79 bool initNewTimer
= true;
80 // Check to see if we are done
81 // inRange will be true if a page is actually printed
85 // donePrinting will be true if it completed successfully or
86 // if the printing was cancelled
87 donePrinting
= mPrintEngine
->PrintPage(mPrintObj
, inRange
);
89 // now clean up print or print the next webshell
90 if (mPrintEngine
->DonePrintingPages(mPrintObj
, NS_OK
)) {
96 // Note that the Stop() destroys this after the print job finishes
97 // (The PrintEngine stops holding a reference when DonePrintingPages
102 nsresult result
= StartTimer(inRange
);
103 if (NS_FAILED(result
)) {
104 mDone
= true; // had a failure.. we are finished..
105 mPrintEngine
->SetIsPrinting(false);
113 nsPagePrintTimer::Notify(nsITimer
*timer
)
115 // When finished there may be still pending notifications, which we can just
121 // There are three things that call Notify with different values for timer:
122 // 1) the delay between pages (timer == mTimer)
123 // 2) canvasPrintState done (timer == null)
124 // 3) the watch dog timer (timer == mWatchDogTimer)
125 if (timer
&& timer
== mWatchDogTimer
) {
127 if (mWatchDogCount
> WATCH_DOG_MAX_COUNT
) {
132 // Reset the counter since a mozPrintCallback has finished.
136 if (mDocViewerPrint
) {
137 bool donePrePrint
= mPrintEngine
->PrePrintPage();
141 NS_DispatchToMainThread(this);
143 // Start the watch dog if we're waiting for preprint to ensure that if any
144 // mozPrintCallbacks take to long we error out.
145 StartWatchDogTimer();
153 nsPagePrintTimer::Start(nsPrintObject
* aPO
)
158 return StartTimer(false);
163 nsPagePrintTimer::Stop()
173 nsPagePrintTimer::Fail()
178 mPrintEngine
->CleanupOnFailure(NS_OK
, false);