nsPagePrintTimer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/. */
  5. #include "nsPagePrintTimer.h"
  6. #include "mozilla/Unused.h"
  7. #include "nsIContentViewer.h"
  8. #include "nsIServiceManager.h"
  9. #include "nsPrintEngine.h"
  10. NS_IMPL_ISUPPORTS_INHERITED(nsPagePrintTimer, mozilla::Runnable, nsITimerCallback)
  11. nsPagePrintTimer::~nsPagePrintTimer()
  12. {
  13. // This matches the IncrementDestroyRefCount call in the constructor.
  14. mDocViewerPrint->DecrementDestroyRefCount();
  15. }
  16. nsresult
  17. nsPagePrintTimer::StartTimer(bool aUseDelay)
  18. {
  19. nsresult result;
  20. mTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
  21. if (NS_FAILED(result)) {
  22. NS_WARNING("unable to start the timer");
  23. } else {
  24. uint32_t delay = 0;
  25. if (aUseDelay) {
  26. if (mFiringCount < 10) {
  27. // Longer delay for the few first pages.
  28. delay = mDelay + ((10 - mFiringCount) * 100);
  29. } else {
  30. delay = mDelay;
  31. }
  32. }
  33. mTimer->InitWithCallback(this, delay, nsITimer::TYPE_ONE_SHOT);
  34. }
  35. return result;
  36. }
  37. nsresult
  38. nsPagePrintTimer::StartWatchDogTimer()
  39. {
  40. nsresult result;
  41. if (mWatchDogTimer) {
  42. mWatchDogTimer->Cancel();
  43. }
  44. mWatchDogTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
  45. if (NS_FAILED(result)) {
  46. NS_WARNING("unable to start the timer");
  47. } else {
  48. // Instead of just doing one timer for a long period do multiple so we
  49. // can check if the user cancelled the printing.
  50. mWatchDogTimer->InitWithCallback(this, WATCH_DOG_INTERVAL,
  51. nsITimer::TYPE_ONE_SHOT);
  52. }
  53. return result;
  54. }
  55. void
  56. nsPagePrintTimer::StopWatchDogTimer()
  57. {
  58. if (mWatchDogTimer) {
  59. mWatchDogTimer->Cancel();
  60. mWatchDogTimer = nullptr;
  61. }
  62. }
  63. //nsRunnable
  64. NS_IMETHODIMP
  65. nsPagePrintTimer::Run()
  66. {
  67. bool initNewTimer = true;
  68. // Check to see if we are done
  69. // inRange will be true if a page is actually printed
  70. bool inRange;
  71. bool donePrinting;
  72. // donePrinting will be true if it completed successfully or
  73. // if the printing was cancelled
  74. donePrinting = !mPrintEngine || mPrintEngine->PrintPage(mPrintObj, inRange);
  75. if (donePrinting) {
  76. // now clean up print or print the next webshell
  77. if (!mPrintEngine || mPrintEngine->DonePrintingPages(mPrintObj, NS_OK)) {
  78. initNewTimer = false;
  79. mDone = true;
  80. }
  81. }
  82. // Note that the Stop() destroys this after the print job finishes
  83. // (The PrintEngine stops holding a reference when DonePrintingPages
  84. // returns true.)
  85. Stop();
  86. if (initNewTimer) {
  87. ++mFiringCount;
  88. nsresult result = StartTimer(inRange);
  89. if (NS_FAILED(result)) {
  90. mDone = true; // had a failure.. we are finished..
  91. if (mPrintEngine) {
  92. mPrintEngine->SetIsPrinting(false);
  93. }
  94. }
  95. }
  96. return NS_OK;
  97. }
  98. // nsITimerCallback
  99. NS_IMETHODIMP
  100. nsPagePrintTimer::Notify(nsITimer *timer)
  101. {
  102. // When finished there may be still pending notifications, which we can just
  103. // ignore.
  104. if (mDone) {
  105. return NS_OK;
  106. }
  107. // There are four things that call Notify with different values for timer:
  108. // 1) the delay between pages (timer == mTimer)
  109. // 2) canvasPrintState done (timer == null)
  110. // 3) the watch dog timer (timer == mWatchDogTimer)
  111. // 4) the waiting for remote print "timer" (timer == mWaitingForRemotePrint)
  112. if (!timer) {
  113. // Reset the counter since a mozPrintCallback has finished.
  114. mWatchDogCount = 0;
  115. } else if (timer == mTimer) {
  116. // Reset the watchdog timer before the start of every page.
  117. mWatchDogCount = 0;
  118. mTimer = nullptr;
  119. } else if (timer == mWaitingForRemotePrint) {
  120. mWaitingForRemotePrint = nullptr;
  121. // If we are still waiting for the page delay timer, don't let the
  122. // notification from the remote print job trigger the next page.
  123. if (mTimer) {
  124. return NS_OK;
  125. }
  126. } else if (timer == mWatchDogTimer) {
  127. mWatchDogCount++;
  128. if (mWatchDogCount > WATCH_DOG_MAX_COUNT) {
  129. Fail();
  130. return NS_OK;
  131. }
  132. }
  133. if (mDocViewerPrint) {
  134. bool donePrePrint = true;
  135. if (mPrintEngine) {
  136. donePrePrint = mPrintEngine->PrePrintPage();
  137. }
  138. if (donePrePrint && !mWaitingForRemotePrint) {
  139. StopWatchDogTimer();
  140. NS_DispatchToMainThread(this);
  141. } else {
  142. // Start the watch dog if we're waiting for preprint to ensure that if any
  143. // mozPrintCallbacks take to long we error out.
  144. StartWatchDogTimer();
  145. }
  146. }
  147. return NS_OK;
  148. }
  149. void
  150. nsPagePrintTimer::WaitForRemotePrint()
  151. {
  152. nsresult result;
  153. mWaitingForRemotePrint = do_CreateInstance("@mozilla.org/timer;1", &result);
  154. if (NS_FAILED(result)) {
  155. NS_WARNING("Failed to wait for remote print, we might time-out.");
  156. mWaitingForRemotePrint = nullptr;
  157. }
  158. }
  159. void
  160. nsPagePrintTimer::RemotePrintFinished()
  161. {
  162. if (!mWaitingForRemotePrint) {
  163. return;
  164. }
  165. mozilla::Unused <<
  166. mWaitingForRemotePrint->InitWithCallback(this, 0, nsITimer::TYPE_ONE_SHOT);
  167. }
  168. nsresult
  169. nsPagePrintTimer::Start(nsPrintObject* aPO)
  170. {
  171. mPrintObj = aPO;
  172. mDone = false;
  173. return StartTimer(false);
  174. }
  175. void
  176. nsPagePrintTimer::Stop()
  177. {
  178. if (mTimer) {
  179. mTimer->Cancel();
  180. mTimer = nullptr;
  181. }
  182. StopWatchDogTimer();
  183. }
  184. void
  185. nsPagePrintTimer::Fail()
  186. {
  187. NS_WARNING("nsPagePrintTimer::Fail called");
  188. mDone = true;
  189. Stop();
  190. if (mPrintEngine) {
  191. mPrintEngine->CleanupOnFailure(NS_OK, false);
  192. }
  193. }