layout/printing/nsPagePrintTimer.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsPagePrintTimer.h"
michael@0 7 #include "nsIContentViewer.h"
michael@0 8 #include "nsIServiceManager.h"
michael@0 9 #include "nsPrintEngine.h"
michael@0 10
michael@0 11 NS_IMPL_ISUPPORTS_INHERITED(nsPagePrintTimer, nsRunnable, nsITimerCallback)
michael@0 12
michael@0 13 nsPagePrintTimer::~nsPagePrintTimer()
michael@0 14 {
michael@0 15 // "Destroy" the document viewer; this normally doesn't actually
michael@0 16 // destroy it because of the IncrementDestroyRefCount call below
michael@0 17 // XXX This is messy; the document viewer should use a single approach
michael@0 18 // to keep itself alive during printing
michael@0 19 nsCOMPtr<nsIContentViewer> cv(do_QueryInterface(mDocViewerPrint));
michael@0 20 if (cv) {
michael@0 21 cv->Destroy();
michael@0 22 }
michael@0 23 }
michael@0 24
michael@0 25 nsresult
michael@0 26 nsPagePrintTimer::StartTimer(bool aUseDelay)
michael@0 27 {
michael@0 28 nsresult result;
michael@0 29 mTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
michael@0 30 if (NS_FAILED(result)) {
michael@0 31 NS_WARNING("unable to start the timer");
michael@0 32 } else {
michael@0 33 uint32_t delay = 0;
michael@0 34 if (aUseDelay) {
michael@0 35 if (mFiringCount < 10) {
michael@0 36 // Longer delay for the few first pages.
michael@0 37 delay = mDelay + ((10 - mFiringCount) * 100);
michael@0 38 } else {
michael@0 39 delay = mDelay;
michael@0 40 }
michael@0 41 }
michael@0 42 mTimer->InitWithCallback(this, delay, nsITimer::TYPE_ONE_SHOT);
michael@0 43 }
michael@0 44 return result;
michael@0 45 }
michael@0 46
michael@0 47 nsresult
michael@0 48 nsPagePrintTimer::StartWatchDogTimer()
michael@0 49 {
michael@0 50 nsresult result;
michael@0 51 if (mWatchDogTimer) {
michael@0 52 mWatchDogTimer->Cancel();
michael@0 53 }
michael@0 54 mWatchDogTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
michael@0 55 if (NS_FAILED(result)) {
michael@0 56 NS_WARNING("unable to start the timer");
michael@0 57 } else {
michael@0 58 // Instead of just doing one timer for a long period do multiple so we
michael@0 59 // can check if the user cancelled the printing.
michael@0 60 mWatchDogTimer->InitWithCallback(this, WATCH_DOG_INTERVAL,
michael@0 61 nsITimer::TYPE_ONE_SHOT);
michael@0 62 }
michael@0 63 return result;
michael@0 64 }
michael@0 65
michael@0 66 void
michael@0 67 nsPagePrintTimer::StopWatchDogTimer()
michael@0 68 {
michael@0 69 if (mWatchDogTimer) {
michael@0 70 mWatchDogTimer->Cancel();
michael@0 71 mWatchDogTimer = nullptr;
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 //nsRunnable
michael@0 76 NS_IMETHODIMP
michael@0 77 nsPagePrintTimer::Run()
michael@0 78 {
michael@0 79 bool initNewTimer = true;
michael@0 80 // Check to see if we are done
michael@0 81 // inRange will be true if a page is actually printed
michael@0 82 bool inRange;
michael@0 83 bool donePrinting;
michael@0 84
michael@0 85 // donePrinting will be true if it completed successfully or
michael@0 86 // if the printing was cancelled
michael@0 87 donePrinting = mPrintEngine->PrintPage(mPrintObj, inRange);
michael@0 88 if (donePrinting) {
michael@0 89 // now clean up print or print the next webshell
michael@0 90 if (mPrintEngine->DonePrintingPages(mPrintObj, NS_OK)) {
michael@0 91 initNewTimer = false;
michael@0 92 mDone = true;
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 // Note that the Stop() destroys this after the print job finishes
michael@0 97 // (The PrintEngine stops holding a reference when DonePrintingPages
michael@0 98 // returns true.)
michael@0 99 Stop();
michael@0 100 if (initNewTimer) {
michael@0 101 ++mFiringCount;
michael@0 102 nsresult result = StartTimer(inRange);
michael@0 103 if (NS_FAILED(result)) {
michael@0 104 mDone = true; // had a failure.. we are finished..
michael@0 105 mPrintEngine->SetIsPrinting(false);
michael@0 106 }
michael@0 107 }
michael@0 108 return NS_OK;
michael@0 109 }
michael@0 110
michael@0 111 // nsITimerCallback
michael@0 112 NS_IMETHODIMP
michael@0 113 nsPagePrintTimer::Notify(nsITimer *timer)
michael@0 114 {
michael@0 115 // When finished there may be still pending notifications, which we can just
michael@0 116 // ignore.
michael@0 117 if (mDone) {
michael@0 118 return NS_OK;
michael@0 119 }
michael@0 120
michael@0 121 // There are three things that call Notify with different values for timer:
michael@0 122 // 1) the delay between pages (timer == mTimer)
michael@0 123 // 2) canvasPrintState done (timer == null)
michael@0 124 // 3) the watch dog timer (timer == mWatchDogTimer)
michael@0 125 if (timer && timer == mWatchDogTimer) {
michael@0 126 mWatchDogCount++;
michael@0 127 if (mWatchDogCount > WATCH_DOG_MAX_COUNT) {
michael@0 128 Fail();
michael@0 129 return NS_OK;
michael@0 130 }
michael@0 131 } else if(!timer) {
michael@0 132 // Reset the counter since a mozPrintCallback has finished.
michael@0 133 mWatchDogCount = 0;
michael@0 134 }
michael@0 135
michael@0 136 if (mDocViewerPrint) {
michael@0 137 bool donePrePrint = mPrintEngine->PrePrintPage();
michael@0 138
michael@0 139 if (donePrePrint) {
michael@0 140 StopWatchDogTimer();
michael@0 141 NS_DispatchToMainThread(this);
michael@0 142 } else {
michael@0 143 // Start the watch dog if we're waiting for preprint to ensure that if any
michael@0 144 // mozPrintCallbacks take to long we error out.
michael@0 145 StartWatchDogTimer();
michael@0 146 }
michael@0 147
michael@0 148 }
michael@0 149 return NS_OK;
michael@0 150 }
michael@0 151
michael@0 152 nsresult
michael@0 153 nsPagePrintTimer::Start(nsPrintObject* aPO)
michael@0 154 {
michael@0 155 mPrintObj = aPO;
michael@0 156 mWatchDogCount = 0;
michael@0 157 mDone = false;
michael@0 158 return StartTimer(false);
michael@0 159 }
michael@0 160
michael@0 161
michael@0 162 void
michael@0 163 nsPagePrintTimer::Stop()
michael@0 164 {
michael@0 165 if (mTimer) {
michael@0 166 mTimer->Cancel();
michael@0 167 mTimer = nullptr;
michael@0 168 }
michael@0 169 StopWatchDogTimer();
michael@0 170 }
michael@0 171
michael@0 172 void
michael@0 173 nsPagePrintTimer::Fail()
michael@0 174 {
michael@0 175 mDone = true;
michael@0 176 Stop();
michael@0 177 if (mPrintEngine) {
michael@0 178 mPrintEngine->CleanupOnFailure(NS_OK, false);
michael@0 179 }
michael@0 180 }

mercurial