Bug 1709347 - Add CanvasRenderingContext2D.reset(). r=lsalzman,webidl,smaug
[gecko.git] / layout / printing / nsPrintData.cpp
blobdb09acae39513fe52990030189b6d4f903d62bc9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsPrintData.h"
9 #include "mozilla/gfx/PrintPromise.h"
10 #include "nsIStringBundle.h"
11 #include "nsIWidget.h"
12 #include "nsPrintObject.h"
13 #include "nsIWebProgressListener.h"
14 #include "mozilla/Services.h"
16 //-----------------------------------------------------
17 // PR LOGGING
18 #include "mozilla/Logging.h"
20 extern mozilla::LazyLogModule gPrintingLog;
22 #define PR_PL(_p1) MOZ_LOG(gPrintingLog, mozilla::LogLevel::Debug, _p1);
24 static void InformListenersOfProgressChange(
25 const nsCOMArray<nsIWebProgressListener>& aListeners, int32_t aProgress,
26 int32_t aMaxProgress, bool aDoStartStop, int32_t aFlag) {
27 size_t numberOfListeners = aListeners.Length();
28 for (size_t i = 0; i < numberOfListeners; ++i) {
29 nsCOMPtr<nsIWebProgressListener> listener = aListeners.SafeElementAt(i);
30 if (NS_WARN_IF(!listener)) {
31 continue;
33 listener->OnProgressChange(nullptr, nullptr, aProgress, aMaxProgress,
34 aProgress, aMaxProgress);
35 if (aDoStartStop) {
36 listener->OnStateChange(nullptr, nullptr, aFlag, NS_OK);
41 static void InformListenersOfEndPrinting(
42 const nsCOMArray<nsIWebProgressListener>& aListeners) {
43 InformListenersOfProgressChange(
44 aListeners, 100, 100, true,
45 nsIWebProgressListener::STATE_STOP |
46 nsIWebProgressListener::STATE_IS_DOCUMENT);
47 InformListenersOfProgressChange(aListeners, 100, 100, true,
48 nsIWebProgressListener::STATE_STOP |
49 nsIWebProgressListener::STATE_IS_NETWORK);
52 //---------------------------------------------------
53 //-- nsPrintData Class Impl
54 //---------------------------------------------------
55 nsPrintData::nsPrintData(ePrintDataType aType)
56 : mType(aType), mOnStartSent(false), mIsAborted(false) {}
58 nsPrintData::~nsPrintData() {
59 // Two things need to be done:
60 // - Inform the listeners
61 // - End/Abort document
62 // Preview requires neither, so return early.
63 if (mType == eIsPrintPreview) {
64 return;
67 if (mPrintDC) {
68 PR_PL(("****************** End Document ************************\n"));
69 PR_PL(("\n"));
70 if (mPrintDC->IsCurrentlyPrintingDocument()) {
71 if (!mIsAborted) {
72 auto promise = mPrintDC->EndDocument();
73 if (mOnStartSent) {
74 promise->Then(mozilla::GetMainThreadSerialEventTarget(), __func__,
75 [listeners = std::move(mPrintProgressListeners)](
76 // We're in dtor, so capture listeners by move.
77 const mozilla::gfx::PrintEndDocumentPromise::
78 ResolveOrRejectValue&) {
79 InformListenersOfEndPrinting(listeners);
80 });
82 // Informing listeners asynchronously, or don't need to inform them, so
83 // return early.
84 return;
86 mPrintDC->AbortDocument();
89 if (mOnStartSent) {
90 // Synchronously notify the listeners.
91 OnEndPrinting();
95 void nsPrintData::OnStartPrinting() {
96 if (!mOnStartSent) {
97 InformListenersOfProgressChange(
98 mPrintProgressListeners, 0, 0, true,
99 nsIWebProgressListener::STATE_START |
100 nsIWebProgressListener::STATE_IS_DOCUMENT |
101 nsIWebProgressListener::STATE_IS_NETWORK);
102 mOnStartSent = true;
106 void nsPrintData::OnEndPrinting() {
107 InformListenersOfEndPrinting(mPrintProgressListeners);
110 void nsPrintData::DoOnProgressChange(int32_t aProgress, int32_t aMaxProgress,
111 bool aDoStartStop, int32_t aFlag) {
112 InformListenersOfProgressChange(mPrintProgressListeners, aProgress,
113 aMaxProgress, aDoStartStop, aFlag);
116 void nsPrintData::DoOnStatusChange(nsresult aStatus) {
117 size_t numberOfListeners = mPrintProgressListeners.Length();
118 for (size_t i = 0; i < numberOfListeners; ++i) {
119 nsCOMPtr<nsIWebProgressListener> listener =
120 mPrintProgressListeners.SafeElementAt(i);
121 if (NS_WARN_IF(!listener)) {
122 continue;
124 listener->OnStatusChange(nullptr, nullptr, aStatus, nullptr);