-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathnsPrintJob.cpp
2414 lines (2055 loc) · 82.3 KB
/
nsPrintJob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "nsPrintJob.h"
#include "nsDebug.h"
#include "nsDocShell.h"
#include "nsReadableUtils.h"
#include "nsQueryObject.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/ComputedStyleInlines.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/PBrowser.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/dom/ShadowRoot.h"
#include "mozilla/dom/CustomEvent.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/DocumentTimeline.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "mozilla/StaticPrefs_print.h"
#include "mozilla/glean/PrintingMetrics.h"
#include "mozilla/Try.h"
#include "nsIBrowserChild.h"
#include "nsIOService.h"
#include "nsIScriptGlobalObject.h"
#include "nsIStringBundle.h"
#include "nsPIDOMWindow.h"
#include "nsPrintData.h"
#include "nsPrintObject.h"
#include "nsIDocShell.h"
#include "nsIURI.h"
#include "nsITextToSubURI.h"
#include "nsError.h"
#include "nsView.h"
#include <algorithm>
// Print Options
#include "nsIPrintSettings.h"
#include "nsIPrintSettingsService.h"
#include "nsGkAtoms.h"
#include "nsXPCOM.h"
static const char sPrintSettingsServiceContractID[] =
"@mozilla.org/gfx/printsettings-service;1";
// Printing Timer
#include "nsPagePrintTimer.h"
// FrameSet
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
// Misc
#include "gfxContext.h"
#include "mozilla/gfx/DrawEventRecorder.h"
#include "mozilla/layout/RemotePrintJobChild.h"
#include "nsISupportsUtils.h"
#include "nsIScriptContext.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "Text.h"
#include "nsIDeviceContextSpec.h"
#include "nsDeviceContextSpecProxy.h"
#include "nsViewManager.h"
#include "nsPageSequenceFrame.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIWebBrowserChrome.h"
#include "mozilla/ReflowInput.h"
#include "nsIDocumentViewer.h"
#include "nsIDocumentViewerPrint.h"
#include "nsFocusManager.h"
#include "nsRange.h"
#include "mozilla/Components.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLFrameElement.h"
#include "mozilla/ServoStyleSet.h"
using namespace mozilla;
using namespace mozilla::dom;
//-----------------------------------------------------
// PR LOGGING
#include "mozilla/Logging.h"
#ifdef DEBUG
// PR_LOGGING is force to always be on (even in release builds)
// but we only want some of it on,
// #define EXTENDED_DEBUG_PRINTING
#endif
// this log level turns on the dumping of each document's layout info
#define DUMP_LAYOUT_LEVEL (static_cast<mozilla::LogLevel>(9))
#ifndef PR_PL
static mozilla::LazyLogModule gPrintingLog("printing");
# define PR_PL(_p1) MOZ_LOG(gPrintingLog, mozilla::LogLevel::Debug, _p1);
#endif
#ifdef EXTENDED_DEBUG_PRINTING
static uint32_t gDumpFileNameCnt = 0;
static uint32_t gDumpLOFileNameCnt = 0;
#endif
#define PRT_YESNO(_p) ((_p) ? "YES" : "NO")
inline const char* LoggableTypeOfPO(const nsPrintObject* aPO) {
// TODO(dholbert): These strings used to represent actual enum values, but
// the enum type has been removed. We could replace them with more
// descriptive names, if anyone uses this logging and cares to do so.
return aPO->mParent ? "eIFrame" : "eDoc";
}
inline const char* ShortLoggableTypeOfPO(const nsPrintObject* aPO) {
return aPO->mParent ? "IF" : "DC";
}
// This processes the selection on aOrigDoc and creates an inverted selection on
// aDoc, which it then deletes. If the start or end of the inverted selection
// ranges occur in text nodes then an ellipsis is added.
static nsresult DeleteNonSelectedNodes(Document& aDoc);
#ifdef EXTENDED_DEBUG_PRINTING
// Forward Declarations
static void DumpPrintObjectsListStart(const char* aStr,
const nsTArray<nsPrintObject*>& aDocList);
static void DumpPrintObjectsTree(nsPrintObject* aPO, int aLevel = 0,
FILE* aFD = nullptr);
static void DumpPrintObjectsTreeLayout(const UniquePtr<nsPrintObject>& aPO,
nsDeviceContext* aDC, int aLevel = 0,
FILE* aFD = nullptr);
# define DUMP_DOC_LIST(_title) \
DumpPrintObjectsListStart((_title), mPrintDocList);
# define DUMP_DOC_TREE DumpPrintObjectsTree(mPrintObject.get());
# define DUMP_DOC_TREELAYOUT \
DumpPrintObjectsTreeLayout(mPrintObject, mPrt->mPrintDC);
#else
# define DUMP_DOC_LIST(_title)
# define DUMP_DOC_TREE
# define DUMP_DOC_TREELAYOUT
#endif
// -------------------------------------------------------
// Helpers
// -------------------------------------------------------
/**
* Build a tree of nsPrintObjects under aPO. It also appends a (depth first)
* flat list of all the nsPrintObjects created to mPrintDocList. If
* one of the nsPrintObject's document is the focused document, then the print
* object is set as mSelectionRoot.
* @param aParentPO The parent nsPrintObject to populate, must not be null.
*/
void nsPrintJob::BuildNestedPrintObjects(
const UniquePtr<nsPrintObject>& aParentPO) {
MOZ_ASSERT(aParentPO);
// If aParentPO is for an iframe and its original document was the document
// that had focus then always set as the selection root.
if (aParentPO->mParent &&
aParentPO->mDocument->GetProperty(nsGkAtoms::printisfocuseddoc)) {
mSelectionRoot = aParentPO.get();
} else if (!mSelectionRoot && aParentPO->HasSelection()) {
// If there is no focused iframe but there is a selection in one or more
// frames then we want to set the root nsPrintObject as the focus root so
// that later EnablePrintingSelectionOnly can search for and enable all
// nsPrintObjects containing selections.
mSelectionRoot = mPrintObject.get();
}
for (auto& bc : aParentPO->mDocShell->GetBrowsingContext()->Children()) {
nsCOMPtr<nsIDocShell> docShell = bc->GetDocShell();
if (!docShell) {
if (auto* cc = dom::ContentChild::GetSingleton()) {
nsCOMPtr<nsIPrintSettingsService> printSettingsService =
do_GetService(sPrintSettingsServiceContractID);
embedding::PrintData printData;
printSettingsService->SerializeToPrintData(mPrintSettings, &printData);
Unused << cc->SendUpdateRemotePrintSettings(bc, printData);
}
continue;
}
RefPtr<Document> doc = docShell->GetDocument();
MOZ_DIAGNOSTIC_ASSERT(doc);
// We might find non-static documents here if the fission remoteness change
// hasn't happened / finished yet. In that case, just skip them, the same
// way we do for remote frames above.
MOZ_DIAGNOSTIC_ASSERT(doc->IsStaticDocument() || doc->IsInitialDocument());
if (!doc || !doc->IsStaticDocument()) {
continue;
}
// Note: docShell and doc are known-non-null at this point; they've been
// null-checked above (with null leading to 'continue' statements).
auto childPO = MakeUnique<nsPrintObject>(*docShell, *doc, aParentPO.get());
mPrintDocList.AppendElement(childPO.get());
BuildNestedPrintObjects(childPO);
aParentPO->mKids.AppendElement(std::move(childPO));
}
}
static nsresult GetDefaultPrintSettings(nsIPrintSettings** aSettings) {
*aSettings = nullptr;
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIPrintSettingsService> printSettingsService =
do_GetService(sPrintSettingsServiceContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return printSettingsService->GetDefaultPrintSettingsForPrinting(aSettings);
}
//-------------------------------------------------------
NS_IMPL_ISUPPORTS(nsPrintJob, nsIWebProgressListener, nsISupportsWeakReference)
//-------------------------------------------------------
nsPrintJob::~nsPrintJob() {
Destroy(); // for insurance
DisconnectPagePrintTimer();
}
bool nsPrintJob::CheckBeforeDestroy() const { return mPreparingForPrint; }
//-------------------------------------------------------
void nsPrintJob::Destroy() {
if (mIsDestroying) {
return;
}
mIsDestroying = true;
DestroyPrintingData();
mDocViewerPrint = nullptr;
}
//-------------------------------------------------------
void nsPrintJob::DestroyPrintingData() {
mPrintObject = nullptr;
mPrt = nullptr;
}
nsPrintJob::nsPrintJob(nsIDocumentViewerPrint& aDocViewerPrint,
nsIDocShell& aDocShell, Document& aOriginalDoc,
float aScreenDPI)
: mDocViewerPrint(&aDocViewerPrint),
mDocShell(do_GetWeakReference(&aDocShell)),
mScreenDPI(aScreenDPI) {
// Any state that we need from aOriginalDoc must be fetched and stored
// here, since the document that the user selected to print may mutate
// across consecutive PrintPreview() calls.
Element* root = aOriginalDoc.GetRootElement();
mDisallowSelectionPrint =
root && root->HasAttr(nsGkAtoms::mozdisallowselectionprint);
}
//-----------------------------------------------------------------
std::tuple<nsPageSequenceFrame*, int32_t>
nsPrintJob::GetSeqFrameAndCountSheets() const {
if (NS_WARN_IF(!mPrt)) {
return {nullptr, 0};
}
const nsPrintObject* po = mPrintObject.get();
if (NS_WARN_IF(!po)) {
return {nullptr, 0};
}
// This is sometimes incorrectly called before the pres shell has been created
// (bug 1141756). MOZ_DIAGNOSTIC_ASSERT so we'll still see the crash in
// Nightly/Aurora in case the other patch fixes this.
if (!po->mPresShell) {
MOZ_DIAGNOSTIC_ASSERT(
false, "GetSeqFrameAndCountSheets needs a non-null pres shell");
return {nullptr, 0};
}
nsPageSequenceFrame* seqFrame = po->mPresShell->GetPageSequenceFrame();
if (!seqFrame) {
return {nullptr, 0};
}
// count the total number of sheets
return {seqFrame, seqFrame->PrincipalChildList().GetLength()};
}
// Foward decl for Debug Helper Functions
#ifdef EXTENDED_DEBUG_PRINTING
# ifdef XP_WIN
static int RemoveFilesInDir(const char* aDir);
# endif
static void GetDocTitleAndURL(const UniquePtr<nsPrintObject>& aPO,
nsACString& aDocStr, nsACString& aURLStr);
static void DumpPrintObjectsTree(nsPrintObject* aPO, int aLevel, FILE* aFD);
static void DumpPrintObjectsList(const nsTArray<nsPrintObject*>& aDocList);
static void RootFrameList(nsPresContext* aPresContext, FILE* out,
const char* aPrefix);
static void DumpViews(nsIDocShell* aDocShell, FILE* out);
static void DumpLayoutData(const char* aTitleStr, const char* aURLStr,
nsPresContext* aPresContext, nsDeviceContext* aDC,
nsIFrame* aRootFrame, nsIDocShell* aDocShell,
FILE* aFD);
#endif
//--------------------------------------------------------------------------------
nsresult nsPrintJob::CommonPrint(bool aIsPrintPreview,
nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener,
Document& aSourceDoc) {
// Callers must hold a strong reference to |this| to ensure that we stay
// alive for the duration of this method, because our main owning reference
// (on nsDocumentViewer) might be cleared during this function (if we cause
// script to run and it cancels the print operation).
nsresult rv = DoCommonPrint(aIsPrintPreview, aPrintSettings,
aWebProgressListener, aSourceDoc);
if (NS_FAILED(rv)) {
if (aIsPrintPreview) {
mIsCreatingPrintPreview = false;
SetIsPrintPreview(false);
} else {
SetIsPrinting(false);
}
if (rv != NS_ERROR_ABORT && rv != NS_ERROR_OUT_OF_MEMORY) {
FirePrintingErrorEvent(rv);
}
DestroyPrintingData();
}
return rv;
}
nsresult nsPrintJob::DoCommonPrint(bool aIsPrintPreview,
nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener,
Document& aDoc) {
MOZ_ASSERT(aDoc.IsStaticDocument());
nsresult rv;
// Grab the new instance with local variable to guarantee that it won't be
// deleted during this method.
// Note: Methods we call early below rely on mPrt being set.
mPrt = new nsPrintData(aIsPrintPreview ? nsPrintData::eIsPrintPreview
: nsPrintData::eIsPrinting);
RefPtr<nsPrintData> printData = mPrt;
if (aIsPrintPreview) {
mIsCreatingPrintPreview = true;
SetIsPrintPreview(true);
} else {
SetIsPrinting(true);
}
if (aWebProgressListener) {
printData->mPrintProgressListeners.AppendObject(aWebProgressListener);
}
if (mRemotePrintJob) {
// If we have a RemotePrintJob add it to the print progress listeners,
// so it can forward to the parent.
printData->mPrintProgressListeners.AppendElement(mRemotePrintJob);
}
// Get the docshell for this documentviewer
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// if they don't pass in a PrintSettings, then get the Global PS
mPrintSettings = aPrintSettings;
if (!mPrintSettings) {
MOZ_TRY(GetDefaultPrintSettings(getter_AddRefs(mPrintSettings)));
}
{
nsAutoScriptBlocker scriptBlocker;
// Note: docShell is implicitly non-null via do_QueryReferent necessarily
// having succeeded (if we got here).
mPrintObject = MakeUnique<nsPrintObject>(*docShell, aDoc);
mPrintDocList.AppendElement(mPrintObject.get());
BuildNestedPrintObjects(mPrintObject);
}
// The nsAutoScriptBlocker above will now have been destroyed, which may
// cause our print/print-preview operation to finish. In this case, we
// should immediately return an error code so that the root caller knows
// it shouldn't continue to do anything with this instance.
if (mIsDestroying) {
return NS_ERROR_FAILURE;
}
// XXX This isn't really correct...
if (!mPrintObject->mDocument || !mPrintObject->mDocument->GetRootElement()) {
return NS_ERROR_GFX_PRINTER_STARTDOC;
}
mPrintSettings->GetShrinkToFit(&mShrinkToFit);
nsCOMPtr<nsIDeviceContextSpec> devspec;
if (XRE_IsContentProcess()) {
devspec = new nsDeviceContextSpecProxy(mRemotePrintJob);
} else {
devspec = do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
bool printSilently = false;
mPrintSettings->GetPrintSilent(&printSilently);
if (StaticPrefs::print_always_print_silent()) {
printSilently = true;
}
if (mIsDoingPrinting && printSilently) {
glean::printing::silent_print.Add(1);
}
MOZ_TRY(devspec->Init(mPrintSettings, mIsCreatingPrintPreview));
printData->mPrintDC = new nsDeviceContext();
MOZ_TRY(printData->mPrintDC->InitForPrinting(devspec));
MOZ_TRY(EnablePOsForPrinting());
if (!mIsCreatingPrintPreview) {
printData->OnStartPrinting();
}
InitPrintDocConstruction(false);
return NS_OK;
}
//---------------------------------------------------------------------------------
nsresult nsPrintJob::Print(Document& aDoc, nsIPrintSettings* aPrintSettings,
RemotePrintJobChild* aRemotePrintJob,
nsIWebProgressListener* aWebProgressListener) {
mRemotePrintJob = aRemotePrintJob;
return CommonPrint(false, aPrintSettings, aWebProgressListener, aDoc);
}
nsresult nsPrintJob::PrintPreview(Document& aDoc,
nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener,
PrintPreviewResolver&& aCallback) {
// Take ownership of aCallback, otherwise a function further up the call
// stack will call it to signal failure (by passing zero).
mPrintPreviewCallback = std::move(aCallback);
nsresult rv = CommonPrint(true, aPrintSettings, aWebProgressListener, aDoc);
if (NS_FAILED(rv)) {
if (mPrintPreviewCallback) {
// signal error
mPrintPreviewCallback(
PrintPreviewResultInfo(0, 0, false, false, false, {}, {}, {}));
mPrintPreviewCallback = nullptr;
}
}
return rv;
}
int32_t nsPrintJob::GetRawNumPages() const {
auto [seqFrame, numSheets] = GetSeqFrameAndCountSheets();
Unused << numSheets;
return seqFrame ? seqFrame->GetRawNumPages() : 0;
}
bool nsPrintJob::GetIsEmpty() const {
auto [seqFrame, numSheets] = GetSeqFrameAndCountSheets();
if (!seqFrame) {
return true;
}
if (numSheets > 1) {
return false;
}
return !seqFrame->GetPagesInFirstSheet();
}
int32_t nsPrintJob::GetPrintPreviewNumSheets() const {
auto [seqFrame, numSheets] = GetSeqFrameAndCountSheets();
Unused << seqFrame;
return numSheets;
}
//-----------------------------------------------------------------
//-- Section: Pre-Reflow Methods
//-----------------------------------------------------------------
// static
void nsPrintJob::GetDisplayTitleAndURL(Document& aDoc,
nsIPrintSettings* aSettings,
DocTitleDefault aTitleDefault,
nsAString& aTitle, nsAString& aURLStr) {
aTitle.Truncate();
aURLStr.Truncate();
if (aSettings) {
aSettings->GetTitle(aTitle);
aSettings->GetDocURL(aURLStr);
}
if (aTitle.IsEmpty()) {
aDoc.GetTitle(aTitle);
if (aTitle.IsEmpty()) {
if (!aURLStr.IsEmpty() &&
aTitleDefault == DocTitleDefault::eDocURLElseFallback) {
aTitle = aURLStr;
} else {
nsCOMPtr<nsIStringBundle> brandBundle;
nsCOMPtr<nsIStringBundleService> svc =
mozilla::components::StringBundle::Service();
if (svc) {
svc->CreateBundle("chrome://branding/locale/brand.properties",
getter_AddRefs(brandBundle));
if (brandBundle) {
brandBundle->GetStringFromName("brandShortName", aTitle);
}
}
if (aTitle.IsEmpty()) {
aTitle.AssignLiteral(u"Mozilla Document");
}
}
}
}
if (aURLStr.IsEmpty()) {
nsIURI* url = aDoc.GetDocumentURI();
if (!url) {
return;
}
nsCOMPtr<nsIURI> exposableURI = net::nsIOService::CreateExposableURI(url);
nsAutoCString urlCStr;
nsresult rv = exposableURI->GetSpec(urlCStr);
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsITextToSubURI> textToSubURI =
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}
textToSubURI->UnEscapeURIForUI(urlCStr, aURLStr);
}
}
//---------------------------------------------------------------------
nsresult nsPrintJob::DocumentReadyForPrinting() {
// Send the document to the printer...
nsresult rv = SetupToPrintContent();
if (NS_FAILED(rv)) {
// The print job was canceled or there was a problem
// So remove all other documents from the print list
DonePrintingSheets(nullptr, rv);
}
return rv;
}
/** ---------------------------------------------------
* Cleans up when an error occurred
*/
nsresult nsPrintJob::CleanupOnFailure(nsresult aResult, bool aIsPrinting) {
PR_PL(("**** Failed %s - rv 0x%" PRIX32,
aIsPrinting ? "Printing" : "Print Preview",
static_cast<uint32_t>(aResult)));
PROFILER_MARKER_TEXT("PrintJob", LAYOUT_Printing, MarkerStack::Capture(),
"nsPrintJob::CleanupOnFailure"_ns);
/* cleanup... */
if (mPagePrintTimer) {
mPagePrintTimer->Stop();
DisconnectPagePrintTimer();
}
if (aIsPrinting) {
SetIsPrinting(false);
} else {
SetIsPrintPreview(false);
mIsCreatingPrintPreview = false;
}
/* cleanup done, let's fire-up an error dialog to notify the user
* what went wrong...
*
* When rv == NS_ERROR_ABORT, it means we want out of the
* print job without displaying any error messages
*/
if (aResult != NS_ERROR_ABORT) {
FirePrintingErrorEvent(aResult);
}
FirePrintCompletionEvent();
return aResult;
}
//---------------------------------------------------------------------
void nsPrintJob::FirePrintingErrorEvent(nsresult aPrintError) {
if (mPrintPreviewCallback) {
// signal error
mPrintPreviewCallback(
PrintPreviewResultInfo(0, 0, false, false, false, {}, {}, {}));
mPrintPreviewCallback = nullptr;
}
nsCOMPtr<nsIDocumentViewer> viewer = do_QueryInterface(mDocViewerPrint);
if (NS_WARN_IF(!viewer)) {
return;
}
const RefPtr<Document> doc = viewer->GetDocument();
const RefPtr<CustomEvent> event = NS_NewDOMCustomEvent(doc, nullptr, nullptr);
MOZ_ASSERT(event);
AutoJSAPI jsapi;
if (!jsapi.Init(event->GetParentObject())) {
return;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JS::Value> detail(
cx, JS::NumberValue(static_cast<double>(aPrintError)));
event->InitCustomEvent(cx, u"PrintingError"_ns, false, false, detail);
event->SetTrusted(true);
// Event listeners in chrome shouldn't delete this.
AsyncEventDispatcher::RunDOMEventWhenSafe(*doc, *event,
ChromeOnlyDispatch::eYes);
// Inform any progress listeners of the Error.
if (mPrt) {
// Note that nsPrintData::DoOnStatusChange() will call some listeners.
// So, mPrt can be cleared or recreated.
RefPtr<nsPrintData> printData = mPrt;
printData->DoOnStatusChange(aPrintError);
}
}
//-----------------------------------------------------------------
//-- Section: Reflow Methods
//-----------------------------------------------------------------
nsresult nsPrintJob::ReconstructAndReflow() {
if (NS_WARN_IF(!mPrt)) {
return NS_ERROR_FAILURE;
}
#if defined(XP_WIN) && defined(EXTENDED_DEBUG_PRINTING)
// We need to clear all the output files here
// because they will be re-created with second reflow of the docs
if (MOZ_LOG_TEST(gPrintingLog, DUMP_LAYOUT_LEVEL)) {
RemoveFilesInDir(".\\");
gDumpFileNameCnt = 0;
gDumpLOFileNameCnt = 0;
}
#endif
// In this loop, it's conceivable that one of our helpers might clear mPrt,
// while we're using it & its members! So we capture it in an owning local
// reference & use that instead of using mPrt directly.
RefPtr<nsPrintData> printData = mPrt;
for (nsPrintObject* po : mPrintDocList) {
if (!po->PrintingIsEnabled() || po->mInvisible) {
continue;
}
// When the print object has been marked as "print the document" (i.e,
// po->PrintingIsEnabled() is true), mPresContext and mPresShell should be
// non-nullptr (i.e., should've been created for the print) since they
// are necessary to print the document.
MOZ_ASSERT(po->mPresContext && po->mPresShell,
"mPresContext and mPresShell shouldn't be nullptr when the "
"print object "
"has been marked as \"print the document\"");
UpdateZoomRatio(po);
po->mPresContext->SetPageScale(po->mZoomRatio);
// Calculate scale factor from printer to screen
float printDPI = float(AppUnitsPerCSSInch()) /
float(printData->mPrintDC->AppUnitsPerDevPixel());
po->mPresContext->SetPrintPreviewScale(mScreenDPI / printDPI);
RefPtr<PresShell> presShell(po->mPresShell);
if (NS_WARN_IF(presShell->IsDestroying())) {
return NS_ERROR_FAILURE;
}
presShell->ReconstructFrames();
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
// For all views except the first one, setup the root view.
// ??? Can there be multiple po for the top-level-document?
bool documentIsTopLevel = true;
if (po->mParent) {
nsSize adjSize;
bool doReturn;
nsresult rv = SetRootView(po, doReturn, documentIsTopLevel, adjSize);
MOZ_ASSERT(!documentIsTopLevel, "How could this happen?");
if (NS_FAILED(rv) || doReturn) {
return rv;
}
}
presShell->FlushPendingNotifications(FlushType::Layout);
if (NS_WARN_IF(presShell->IsDestroying())) {
return NS_ERROR_FAILURE;
}
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
po->mDocument->UpdateRemoteFrameEffects();
MOZ_TRY(UpdateSelectionAndShrinkPrintObject(po, documentIsTopLevel));
}
return NS_OK;
}
//-------------------------------------------------------
nsresult nsPrintJob::SetupToPrintContent() {
// This method may be called while DoCommonPrint() initializes the instance
// when its script blocker goes out of scope. In such case, this cannot do
// its job as expected because some objects in mPrt have not been initialized
// yet but they are necessary.
// Note: it shouldn't be possible for mPrintObject to be null; we check
// it for good measure (after we check its owner) before we start
// dereferencing it below.
if (NS_WARN_IF(!mPrt) || NS_WARN_IF(!mPrintObject)) {
return NS_ERROR_FAILURE;
}
// If this is creating print preview, mPrintObject->mPresContext and
// mPrintObject->mPresShell need to be non-nullptr because this cannot
// initialize page sequence frame without them at end of this method since
// page sequence frame has already been destroyed or not been created yet.
if (mIsCreatingPrintPreview && (NS_WARN_IF(!mPrintObject->mPresContext) ||
NS_WARN_IF(!mPrintObject->mPresShell))) {
return NS_ERROR_FAILURE;
}
// If this is printing some documents (not print-previewing the documents),
// mPrintObject->mPresContext and mPrintObject->mPresShell can be
// nullptr only when mPrintObject->PrintingIsEnabled() is false. E.g.,
// if the document has a <frameset> element and it's printing only content in
// a <frame> element or all <frame> elements separately.
MOZ_ASSERT(
(!mIsCreatingPrintPreview && !mPrintObject->PrintingIsEnabled()) ||
(mPrintObject->mPresContext && mPrintObject->mPresShell),
"mPresContext and mPresShell shouldn't be nullptr when printing the "
"document or creating print-preview");
bool didReconstruction = false;
// This method works with mPrintObject. So, we need to guarantee that
// it won't be deleted in this method. We achieve this by holding a strong
// local reference to mPrt, which in turn keeps mPrintObject alive.
RefPtr<nsPrintData> printData = mPrt;
// If some new content got loaded since the initial reflow rebuild
// everything.
if (mDidLoadDataForPrinting) {
nsresult rv = ReconstructAndReflow();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
didReconstruction = true;
}
// Here is where we figure out if extra reflow for shrinking the content
// is required.
if (mShrinkToFit) {
mShrinkToFitFactor = mPrintObject->mShrinkRatio;
if (mShrinkToFitFactor < 0.998f) {
nsresult rv = ReconstructAndReflow();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
didReconstruction = true;
}
if (MOZ_LOG_TEST(gPrintingLog, LogLevel::Debug)) {
float calcRatio = mPrintObject->mShrinkRatio;
PR_PL(
("*******************************************************************"
"*******\n"));
PR_PL(("STF Ratio is: %8.5f Effective Ratio: %8.5f Diff: %8.5f\n",
mShrinkToFitFactor, calcRatio, mShrinkToFitFactor - calcRatio));
PR_PL(
("*******************************************************************"
"*******\n"));
}
}
// If the frames got reconstructed and reflowed the number of pages might
// has changed.
if (didReconstruction) {
FirePrintPreviewUpdateEvent();
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
}
DUMP_DOC_LIST(("\nAfter Reflow------------------------------------------"));
PR_PL(("\n"));
PR_PL(("-------------------------------------------------------\n"));
PR_PL(("\n"));
CalcNumPrintablePages(mNumPrintablePages);
PR_PL(("--- Printing %d pages\n", mNumPrintablePages));
DUMP_DOC_TREELAYOUT;
// Print listener setup...
printData->OnStartPrinting();
// If the printing was canceled or restarted with different data,
// let's stop doing this printing.
if (NS_WARN_IF(mPrt != printData)) {
return NS_ERROR_FAILURE;
}
nsAutoString fileNameStr;
// check to see if we are printing to a file
if (mPrintSettings->GetOutputDestination() ==
nsIPrintSettings::kOutputDestinationFile) {
// On some platforms the BeginDocument needs to know the name of the file.
mPrintSettings->GetToFileName(fileNameStr);
}
nsAutoString docTitleStr;
nsAutoString docURLStr;
GetDisplayTitleAndURL(*mPrintObject->mDocument, mPrintSettings,
DocTitleDefault::eDocURLElseFallback, docTitleStr,
docURLStr);
int32_t startPage = 1;
int32_t endPage = mNumPrintablePages;
nsTArray<int32_t> ranges;
mPrintSettings->GetPageRanges(ranges);
for (size_t i = 0; i < ranges.Length(); i += 2) {
startPage = std::max(1, std::min(startPage, ranges[i]));
endPage = std::min(mNumPrintablePages, std::max(endPage, ranges[i + 1]));
}
nsresult rv = NS_OK;
// BeginDocument may pass back a FAILURE code
// i.e. On Windows, if you are printing to a file and hit "Cancel"
// to the "File Name" dialog, this comes back as an error
// Don't start printing when regression test are executed
if (mIsDoingPrinting) {
rv = printData->mPrintDC->BeginDocument(docTitleStr, fileNameStr, startPage,
endPage);
}
if (mIsCreatingPrintPreview) {
// Copy docTitleStr and docURLStr to the pageSequenceFrame, to be displayed
// in the header
nsPageSequenceFrame* seqFrame =
mPrintObject->mPresShell->GetPageSequenceFrame();
if (seqFrame) {
seqFrame->StartPrint(mPrintObject->mPresContext, mPrintSettings,
docTitleStr, docURLStr);
}
}
PR_PL(("****************** Begin Document ************************\n"));
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_ABORT,
"Failed to begin document for printing");
return rv;
}
// This will print the docshell document
// when it completes asynchronously in the DonePrintingSheets method
// it will check to see if there are more docshells to be printed and
// then PrintDocContent will be called again.
if (mIsDoingPrinting) {
// Double-check that mPrintObject is non-null, because it could have
// gotten cleared while running script since we last checked it.
if (NS_WARN_IF(!mPrintObject)) {
return NS_ERROR_FAILURE;
}
PrintDocContent(mPrintObject, rv); // ignore return value
}
return rv;
}
//-------------------------------------------------------
// Recursively reflow each sub-doc and then calc
// all the frame locations of the sub-docs
nsresult nsPrintJob::ReflowDocList(const UniquePtr<nsPrintObject>& aPO) {
NS_ENSURE_ARG_POINTER(aPO);
// Check to see if the subdocument's element has been hidden by the parent
// document
if (aPO->mParent && aPO->mParent->mPresShell) {
nsIFrame* frame =
aPO->mContent ? aPO->mContent->GetPrimaryFrame() : nullptr;
if (!frame || !frame->StyleVisibility()->IsVisible()) {
aPO->EnablePrinting(false);
aPO->mInvisible = true;
return NS_OK;
}
}
UpdateZoomRatio(aPO.get());
// Reflow the PO
MOZ_TRY(ReflowPrintObject(aPO));
for (const UniquePtr<nsPrintObject>& kid : aPO->mKids) {
MOZ_TRY(ReflowDocList(kid));
}
return NS_OK;
}
void nsPrintJob::FirePrintPreviewUpdateEvent() {
// Dispatch the event only while in PrintPreview. When printing, there is no
// listener bound to this event and therefore no need to dispatch it.
if (mCreatedForPrintPreview && !mIsDoingPrinting) {
nsCOMPtr<nsIDocumentViewer> viewer = do_QueryInterface(mDocViewerPrint);
if (Document* document = viewer->GetDocument()) {
AsyncEventDispatcher::RunDOMEventWhenSafe(
*document, u"printPreviewUpdate"_ns, CanBubble::eYes,
ChromeOnlyDispatch::eYes);
}
}
}
nsresult nsPrintJob::InitPrintDocConstruction(bool aHandleError) {
// Guarantee that mPrintObject won't be deleted.
RefPtr<nsPrintData> printData = mPrt;
if (NS_WARN_IF(!printData)) {
return NS_ERROR_FAILURE;
}
// Attach progressListener to catch network requests.
mDidLoadDataForPrinting = false;
{
AutoRestore<bool> restore{mDoingInitialReflow};
mDoingInitialReflow = true;
nsCOMPtr<nsIWebProgress> webProgress =
do_QueryInterface(mPrintObject->mDocShell);
webProgress->AddProgressListener(static_cast<nsIWebProgressListener*>(this),
nsIWebProgress::NOTIFY_STATE_REQUEST);