2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-08-21 16:01:05 +04:00
|
|
|
|
|
|
|
#include "nsPagePrintTimer.h"
|
2016-01-05 13:08:57 +03:00
|
|
|
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2002-08-21 16:01:05 +04:00
|
|
|
#include "nsIContentViewer.h"
|
2002-09-07 09:38:16 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2017-12-04 20:44:17 +03:00
|
|
|
#include "nsPrintJob.h"
|
2002-08-21 16:01:05 +04:00
|
|
|
|
2017-03-30 17:49:43 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsPagePrintTimer, mozilla::Runnable,
|
|
|
|
nsITimerCallback)
|
2002-08-21 16:01:05 +04:00
|
|
|
|
|
|
|
nsPagePrintTimer::~nsPagePrintTimer() {
|
2018-01-08 19:59:31 +03:00
|
|
|
// This matches the IncrementDestroyBlockedCount call in the constructor.
|
|
|
|
mDocViewerPrint->DecrementDestroyBlockedCount();
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsPagePrintTimer::StartTimer(bool aUseDelay) {
|
2017-10-16 09:13:31 +03:00
|
|
|
uint32_t delay = 0;
|
|
|
|
if (aUseDelay) {
|
|
|
|
if (mFiringCount < 10) {
|
|
|
|
// Longer delay for the few first pages.
|
|
|
|
delay = mDelay + ((10 - mFiringCount) * 100);
|
|
|
|
} else {
|
|
|
|
delay = mDelay;
|
2009-12-19 13:32:53 +03:00
|
|
|
}
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
2017-10-16 09:13:31 +03:00
|
|
|
return NS_NewTimerWithCallback(
|
|
|
|
getter_AddRefs(mTimer), this, delay, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
mDocument->EventTargetFor(TaskCategory::Other));
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 21:42:21 +04:00
|
|
|
nsresult nsPagePrintTimer::StartWatchDogTimer() {
|
|
|
|
if (mWatchDogTimer) {
|
|
|
|
mWatchDogTimer->Cancel();
|
|
|
|
}
|
2017-10-16 09:13:31 +03:00
|
|
|
// Instead of just doing one timer for a long period do multiple so we
|
|
|
|
// can check if the user cancelled the printing.
|
|
|
|
return NS_NewTimerWithCallback(
|
|
|
|
getter_AddRefs(mWatchDogTimer), this, WATCH_DOG_INTERVAL,
|
|
|
|
nsITimer::TYPE_ONE_SHOT, mDocument->EventTargetFor(TaskCategory::Other));
|
2012-10-02 21:42:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsPagePrintTimer::StopWatchDogTimer() {
|
|
|
|
if (mWatchDogTimer) {
|
|
|
|
mWatchDogTimer->Cancel();
|
|
|
|
mWatchDogTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 04:24:35 +04:00
|
|
|
// nsRunnable
|
|
|
|
NS_IMETHODIMP
|
2017-07-06 15:00:35 +03:00
|
|
|
nsPagePrintTimer::Run() {
|
2012-08-31 04:24:35 +04:00
|
|
|
bool initNewTimer = true;
|
|
|
|
// Check to see if we are done
|
|
|
|
// inRange will be true if a page is actually printed
|
|
|
|
bool inRange;
|
|
|
|
bool donePrinting;
|
|
|
|
|
|
|
|
// donePrinting will be true if it completed successfully or
|
|
|
|
// if the printing was cancelled
|
2017-12-04 20:44:17 +03:00
|
|
|
donePrinting = !mPrintJob || mPrintJob->PrintPage(mPrintObj, inRange);
|
2017-12-08 14:09:56 +03:00
|
|
|
if (donePrinting) {
|
2017-11-06 11:28:51 +03:00
|
|
|
if (mWaitingForRemotePrint ||
|
|
|
|
// If we are not waiting for the remote printing, it is the time to
|
|
|
|
// end printing task by calling DonePrintingPages.
|
2017-12-04 20:44:17 +03:00
|
|
|
(!mPrintJob || mPrintJob->DonePrintingPages(mPrintObj, NS_OK))) {
|
2017-12-08 14:09:56 +03:00
|
|
|
initNewTimer = false;
|
|
|
|
mDone = true;
|
|
|
|
}
|
2012-08-31 04:24:35 +04:00
|
|
|
}
|
2002-08-21 16:01:05 +04:00
|
|
|
|
2012-08-31 04:24:35 +04:00
|
|
|
// Note that the Stop() destroys this after the print job finishes
|
2017-12-04 20:44:17 +03:00
|
|
|
// (The nsPrintJob stops holding a reference when DonePrintingPages
|
2012-08-31 04:24:35 +04:00
|
|
|
// returns true.)
|
2017-07-06 15:00:35 +03:00
|
|
|
Stop();
|
2012-08-31 04:24:35 +04:00
|
|
|
if (initNewTimer) {
|
|
|
|
++mFiringCount;
|
|
|
|
nsresult result = StartTimer(inRange);
|
|
|
|
if (NS_FAILED(result)) {
|
2012-10-02 21:42:21 +04:00
|
|
|
mDone = true; // had a failure.. we are finished..
|
2017-12-04 20:44:17 +03:00
|
|
|
if (mPrintJob) {
|
|
|
|
mPrintJob->SetIsPrinting(false);
|
2017-01-03 14:10:11 +03:00
|
|
|
}
|
2012-08-31 04:24:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2012-09-05 18:44:24 +04:00
|
|
|
}
|
2002-08-21 16:01:05 +04:00
|
|
|
|
|
|
|
// nsITimerCallback
|
2002-09-07 09:38:16 +04:00
|
|
|
NS_IMETHODIMP
|
2002-08-21 16:01:05 +04:00
|
|
|
nsPagePrintTimer::Notify(nsITimer* timer) {
|
2012-10-02 21:42:21 +04:00
|
|
|
// When finished there may be still pending notifications, which we can just
|
|
|
|
// ignore.
|
|
|
|
if (mDone) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
// There are four things that call Notify with different values for timer:
|
2012-10-02 21:42:21 +04:00
|
|
|
// 1) the delay between pages (timer == mTimer)
|
|
|
|
// 2) canvasPrintState done (timer == null)
|
|
|
|
// 3) the watch dog timer (timer == mWatchDogTimer)
|
2016-01-05 13:08:57 +03:00
|
|
|
// 4) the waiting for remote print "timer" (timer == mWaitingForRemotePrint)
|
|
|
|
if (!timer) {
|
|
|
|
// Reset the counter since a mozPrintCallback has finished.
|
|
|
|
mWatchDogCount = 0;
|
|
|
|
} else if (timer == mTimer) {
|
|
|
|
// Reset the watchdog timer before the start of every page.
|
|
|
|
mWatchDogCount = 0;
|
|
|
|
mTimer = nullptr;
|
|
|
|
} else if (timer == mWaitingForRemotePrint) {
|
|
|
|
mWaitingForRemotePrint = nullptr;
|
|
|
|
|
|
|
|
// If we are still waiting for the page delay timer, don't let the
|
|
|
|
// notification from the remote print job trigger the next page.
|
|
|
|
if (mTimer) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else if (timer == mWatchDogTimer) {
|
2012-10-02 21:42:21 +04:00
|
|
|
mWatchDogCount++;
|
|
|
|
if (mWatchDogCount > WATCH_DOG_MAX_COUNT) {
|
|
|
|
Fail();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:21:53 +03:00
|
|
|
bool donePrePrint = true;
|
|
|
|
// Don't start to pre-print if we're waiting on the parent still.
|
|
|
|
if (mPrintJob && !mWaitingForRemotePrint) {
|
|
|
|
donePrePrint = mPrintJob->PrePrintPage();
|
|
|
|
}
|
2012-08-31 04:24:35 +04:00
|
|
|
|
2018-01-10 01:21:53 +03:00
|
|
|
if (donePrePrint && !mWaitingForRemotePrint) {
|
|
|
|
StopWatchDogTimer();
|
|
|
|
// Pass nullptr here since name already was set in constructor.
|
|
|
|
mDocument->Dispatch(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();
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
2018-01-10 01:21:53 +03:00
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
return NS_OK;
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
void nsPagePrintTimer::WaitForRemotePrint() {
|
2017-10-16 09:13:31 +03:00
|
|
|
mWaitingForRemotePrint = NS_NewTimer();
|
|
|
|
if (!mWaitingForRemotePrint) {
|
2016-01-05 13:08:57 +03:00
|
|
|
NS_WARNING("Failed to wait for remote print, we might time-out.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPagePrintTimer::RemotePrintFinished() {
|
|
|
|
if (!mWaitingForRemotePrint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:28:51 +03:00
|
|
|
// now clean up print or print the next webshell
|
2017-12-04 20:44:17 +03:00
|
|
|
if (mDone && mPrintJob) {
|
|
|
|
mDone = mPrintJob->DonePrintingPages(mPrintObj, NS_OK);
|
2017-11-06 11:28:51 +03:00
|
|
|
}
|
|
|
|
|
2017-03-30 17:49:43 +03:00
|
|
|
mWaitingForRemotePrint->SetTarget(
|
|
|
|
mDocument->EventTargetFor(mozilla::TaskCategory::Other));
|
2016-01-05 13:08:57 +03:00
|
|
|
mozilla::Unused << mWaitingForRemotePrint->InitWithCallback(
|
|
|
|
this, 0, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2006-12-10 11:05:05 +03:00
|
|
|
nsresult nsPagePrintTimer::Start(nsPrintObject* aPO) {
|
|
|
|
mPrintObj = aPO;
|
2012-10-02 21:42:21 +04:00
|
|
|
mDone = false;
|
2011-10-17 18:59:28 +04:00
|
|
|
return StartTimer(false);
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsPagePrintTimer::Stop() {
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
2012-10-02 21:42:21 +04:00
|
|
|
StopWatchDogTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPagePrintTimer::Fail() {
|
2016-12-19 13:12:39 +03:00
|
|
|
NS_WARNING("nsPagePrintTimer::Fail called");
|
|
|
|
|
2012-10-02 21:42:21 +04:00
|
|
|
mDone = true;
|
|
|
|
Stop();
|
2017-12-04 20:44:17 +03:00
|
|
|
if (mPrintJob) {
|
|
|
|
mPrintJob->CleanupOnFailure(NS_OK, false);
|
2012-10-02 21:42:21 +04:00
|
|
|
}
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|