File: print_view_manager_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1350 lines) | stat: -rw-r--r-- 47,403 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/printing/print_view_manager_base.h"

#include <memory>
#include <string_view>
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/safe_conversions.h"
#include "base/observer_list.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/bad_message.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_compositor_util.h"
#include "chrome/browser/printing/print_error_dialog.h"
#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/device_event_log/device_event_log.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/prefs/pref_service.h"
#include "components/printing/browser/print_composite_client.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print.mojom.h"
#include "components/printing/common/print_params.h"
#include "components/services/print_compositor/public/cpp/print_service_mojo_types.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/system/buffer.h"
#include "printing/buildflags/buildflags.h"
#include "printing/metafile_skia.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"
#include "printing/printed_document.h"
#include "printing/printing_utils.h"
#include "ui/base/l10n/l10n_util.h"

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/print_view_manager.h"
#include "printing/print_settings_conversion.h"
#endif

#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/browser/printing/oop_features.h"
#include "chrome/browser/printing/print_backend_service_manager.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "chrome/browser/printing/xps_features.h"
#endif

#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
#include "chrome/browser/enterprise/data_protection/print_utils.h"
#endif

namespace printing {

namespace {

void OnDidGetDefaultPrintSettings(
    scoped_refptr<PrintQueriesQueue> queue,
    bool want_pdf_settings,
    std::unique_ptr<PrinterQuery> printer_query,
    mojom::PrintManagerHost::GetDefaultPrintSettingsCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (printer_query->last_status() != mojom::ResultCode::kSuccess) {
    if (!want_pdf_settings) {
      ShowPrintErrorDialogForInvalidPrinterError();
    }
    std::move(callback).Run(nullptr);
    return;
  }

  mojom::PrintParamsPtr params = mojom::PrintParams::New();
  RenderParamsFromPrintSettings(printer_query->settings(), params.get());
  params->document_cookie = printer_query->cookie();

  if (!PrintMsgPrintParamsIsValid(*params)) {
    if (!want_pdf_settings) {
      ShowPrintErrorDialogForInvalidPrinterError();
    }
    std::move(callback).Run(nullptr);
    return;
  }

  std::move(callback).Run(std::move(params));
  queue->QueuePrinterQuery(std::move(printer_query));
}

void OnDidScriptedPrint(
    scoped_refptr<PrintQueriesQueue> queue,
    std::unique_ptr<PrinterQuery> printer_query,
    mojom::PrintManagerHost::ScriptedPrintCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (printer_query->last_status() != mojom::ResultCode::kSuccess ||
      !printer_query->settings().dpi()) {
    // Notify user of the error, unless it was explicitly canceled.
    if (printer_query->last_status() != mojom::ResultCode::kCanceled) {
      ShowPrintErrorDialogForGenericError();
    }
    std::move(callback).Run(nullptr);
    return;
  }

  auto params = mojom::PrintPagesParams::New();
  params->params = mojom::PrintParams::New();
  RenderParamsFromPrintSettings(printer_query->settings(),
                                params->params.get());
  params->params->document_cookie = printer_query->cookie();
  if (!PrintMsgPrintParamsIsValid(*params->params)) {
    std::move(callback).Run(nullptr);
    return;
  }

  params->pages = printer_query->settings().ranges();
  std::move(callback).Run(std::move(params));
  queue->QueuePrinterQuery(std::move(printer_query));
}

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
std::string PrintMsgPrintParamsErrorDetails(const mojom::PrintParams& params) {
  std::vector<std::string_view> details;

  if (params.content_size.IsEmpty()) {
    details.push_back("content size is empty");
  }
  if (params.page_size.IsEmpty()) {
    details.push_back("page size is empty");
  }
  if (params.printable_area.IsEmpty()) {
    details.push_back("printable area is empty");
  }
  if (!params.document_cookie) {
    details.push_back("invalid document cookie");
  }
  if (params.dpi.width() <= kMinDpi || params.dpi.height() <= kMinDpi) {
    details.push_back("invalid DPI dimensions");
  }
  if (params.margin_top < 0 || params.margin_left < 0) {
    details.push_back("invalid margins");
  }

  return base::JoinString(details, "; ");
}
#endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)

}  // namespace

PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
    : PrintManager(web_contents),
      queue_(g_browser_process->print_job_manager()->queue()) {
  DCHECK(queue_);
  Profile* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
}

PrintViewManagerBase::~PrintViewManagerBase() {
  ReleasePrinterQuery();
  DisconnectFromCurrentPrintJob();
}

bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  if (!StartPrintCommon(rfh)) {
    return false;
  }

  GetPrintRenderFrame(rfh)->PrintRequestedPages();

  for (auto& observer : GetTestObservers()) {
    observer.OnPrintNow(rfh);
  }
  return true;
}

void PrintViewManagerBase::PrintNodeUnderContextMenu(
    content::RenderFrameHost* rfh) {
  if (!StartPrintCommon(rfh)) {
    return;
  }

  GetPrintRenderFrame(rfh)->PrintNodeUnderContextMenu();

  for (auto& observer : GetTestObservers()) {
    observer.OnPrintNow(rfh);
  }
}

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::PrintForPrintPreview(
    base::Value::Dict job_settings,
    scoped_refptr<base::RefCountedMemory> print_data,
    content::RenderFrameHost* rfh,
    PrinterHandler::PrintCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

#if BUILDFLAG(ENABLE_OOP_PRINTING) || BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  bool show_system_dialog =
      job_settings.FindBool(kSettingShowSystemDialog).value_or(false);
#endif

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (show_system_dialog && ShouldPrintJobOop()) {
    if (!RegisterSystemPrintClient()) {
      // Platform unable to support system print dialog at this time, treat
      // this as a cancel.
      std::move(callback).Run(
          base::Value("Concurrent system print not allowed"));
      return;
    }
  }
#endif

  std::unique_ptr<printing::PrinterQuery> printer_query =
      queue()->CreatePrinterQuery(rfh->GetGlobalId());
  auto* printer_query_ptr = printer_query.get();
  const int page_count = job_settings.FindInt(kSettingPreviewPageCount).value();

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (query_with_ui_client_id().has_value()) {
    printer_query->SetClientId(query_with_ui_client_id().value());
  }
#endif
  printer_query_ptr->SetSettings(
      std::move(job_settings),
      base::BindOnce(&PrintViewManagerBase::OnPrintSettingsDone,
                     weak_ptr_factory_.GetWeakPtr(), print_data, page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
                     show_system_dialog,
#endif
                     std::move(callback), std::move(printer_query)));
}
#endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)

void PrintViewManagerBase::PrintToPdf(
    content::RenderFrameHost* rfh,
    const std::string& page_ranges,
    mojom::PrintPagesParamsPtr print_pages_params,
    print_to_pdf::PdfPrintJob::PrintToPdfCallback callback) {
  print_to_pdf::PdfPrintJob::StartJob(
      web_contents(), rfh, GetPrintRenderFrame(rfh), page_ranges,
      std::move(print_pages_params), std::move(callback));
}

void PrintViewManagerBase::PrintDocument(
    scoped_refptr<base::RefCountedMemory> print_data,
    const gfx::Size& page_size,
    const gfx::Rect& content_area,
    const gfx::Point& offsets) {
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  if (content_analysis_before_printing_document_) {
    std::move(content_analysis_before_printing_document_)
        .Run(print_data, page_size, content_area, offsets);
    return;
  }
#endif

#if BUILDFLAG(IS_WIN)
  const bool source_is_pdf =
      !print_job_->document()->settings().is_modifiable();
  if (!ShouldPrintUsingXps(source_is_pdf)) {
    // Print using GDI, which first requires conversion to EMF.
    print_job_->StartConversionToNativeFormat(
        print_data, page_size, content_area, offsets,
        web_contents()->GetLastCommittedURL());
    return;
  }
#endif

  std::unique_ptr<MetafileSkia> metafile = std::make_unique<MetafileSkia>();
  CHECK(metafile->InitFromData(*print_data));

  // Update the rendered document. It will send notifications to the listener.
  PrintedDocument* document = print_job_->document();
  document->SetDocument(std::move(metafile));
  ShouldQuitFromInnerMessageLoop();
}

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#if BUILDFLAG(IS_WIN)
void PrintViewManagerBase::OnDidUpdatePrintableArea(
    std::unique_ptr<PrinterQuery> printer_query,
    base::Value::Dict job_settings,
    std::unique_ptr<PrintSettings> print_settings,
    UpdatePrintSettingsCallback callback,
    bool success) {
  if (!success) {
    PRINTER_LOG(ERROR) << "Unable to update printable area for "
                       << base::UTF16ToUTF8(print_settings->device_name())
                       << " (paper vendor id "
                       << print_settings->requested_media().vendor_id << ")";
    std::move(callback).Run(nullptr);
    return;
  }
  PRINTER_LOG(EVENT) << "Paper printable area updated for vendor id "
                     << print_settings->requested_media().vendor_id;
  CompleteUpdatePrintSettings(std::move(job_settings),
                              std::move(print_settings), std::move(callback));
}
#endif

void PrintViewManagerBase::CompleteUpdatePrintSettings(
    base::Value::Dict job_settings,
    std::unique_ptr<PrintSettings> print_settings,
    UpdatePrintSettingsCallback callback) {
  mojom::PrintPagesParamsPtr settings = mojom::PrintPagesParams::New();
  settings->pages = GetPageRangesFromJobSettings(job_settings);
  settings->params = mojom::PrintParams::New();
  RenderParamsFromPrintSettings(*print_settings, settings->params.get());
  settings->params->document_cookie = PrintSettings::NewCookie();
  if (!PrintMsgPrintParamsIsValid(*settings->params)) {
    mojom::PrinterType printer_type = static_cast<mojom::PrinterType>(
        *job_settings.FindInt(kSettingPrinterType));
    PRINTER_LOG(ERROR) << "Printer settings invalid for "
                       << base::UTF16ToUTF8(print_settings->device_name())
                       << " (destination type " << printer_type << "): "
                       << PrintMsgPrintParamsErrorDetails(*settings->params);
    std::move(callback).Run(nullptr);
    return;
  }

  set_cookie(settings->params->document_cookie);
  std::move(callback).Run(std::move(settings));
}

void PrintViewManagerBase::OnPrintSettingsDone(
    scoped_refptr<base::RefCountedMemory> print_data,
    uint32_t page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
    bool show_system_dialog,
#endif
    PrinterHandler::PrintCallback callback,
    std::unique_ptr<printing::PrinterQuery> printer_query) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(printer_query);

  // Check if the job was cancelled.  With out-of-process printing, this could
  // happen if we detect that another system print dialog is already being
  // displayed.  Otherwise this should only happen on Windows when the system
  // dialog is cancelled.
  if (printer_query->last_status() == mojom::ResultCode::kCanceled) {
#if BUILDFLAG(ENABLE_OOP_PRINTING)
    if (ShouldPrintJobOop()) {
      UnregisterSystemPrintClient();
    }
#endif
#if BUILDFLAG(IS_WIN)
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE, base::BindOnce(&PrintViewManagerBase::SystemDialogCancelled,
                                  weak_ptr_factory_.GetWeakPtr()));
#endif
    std::move(callback).Run(base::Value());
    return;
  }

  if (!printer_query->cookie() || !printer_query->settings().dpi()) {
    PRINTER_LOG(ERROR) << "Unable to update print settings";
#if BUILDFLAG(ENABLE_OOP_PRINTING)
    if (ShouldPrintJobOop()) {
      UnregisterSystemPrintClient();
    }
#endif
    ShowPrintErrorDialogForGenericError();
    std::move(callback).Run(base::Value("Update settings failed"));
    return;
  }

  // Post task so that the query has time to reset the callback before calling
  // DidGetPrintedPagesCount().
  int cookie = printer_query->cookie();
  queue()->QueuePrinterQuery(std::move(printer_query));
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(&PrintViewManagerBase::StartLocalPrintJob,
                     weak_ptr_factory_.GetWeakPtr(), print_data, page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
                     show_system_dialog,
#endif
                     cookie, std::move(callback)));
}

void PrintViewManagerBase::StartLocalPrintJob(
    scoped_refptr<base::RefCountedMemory> print_data,
    uint32_t page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
    bool show_system_dialog,
#endif
    int cookie,
    PrinterHandler::PrintCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  // Populating `content_analysis_before_printing_document_` if needed should be
  // done first in this function's workflow, this way other code can check if
  // content analysis is going to happen and delay starting `print_job_` to
  // avoid needlessly prompting the user.
  using enterprise_data_protection::PrintScanningContext;
  auto context = show_system_dialog
                     ? PrintScanningContext::kSystemPrintBeforePrintDocument
                     : PrintScanningContext::kNormalPrintBeforePrintDocument;

  std::optional<enterprise_connectors::ContentAnalysisDelegate::Data>
      scanning_data = enterprise_data_protection::GetPrintAnalysisData(
          web_contents(), context);

  if (scanning_data) {
    set_content_analysis_before_printing_document(base::BindOnce(
        &PrintViewManagerBase::ContentAnalysisBeforePrintingDocument,
        weak_ptr_factory_.GetWeakPtr(), std::move(*scanning_data)));
  }
#endif  // BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)

  set_cookie(cookie);
  DidGetPrintedPagesCount(cookie, page_count);

  if (!PrintJobHasDocument(cookie)) {
    std::move(callback).Run(base::Value("Failed to print"));
    return;
  }

#if BUILDFLAG(IS_WIN)
  print_job_->ResetPageMapping();
#endif

  const printing::PrintSettings& settings = print_job_->settings();
  gfx::Size page_size = settings.page_setup_device_units().physical_size();
  gfx::Rect content_area =
      gfx::Rect(0, 0, page_size.width(), page_size.height());

  PrintDocument(print_data, page_size, content_area,
                settings.page_setup_device_units().printable_area().origin());
  std::move(callback).Run(base::Value());
}
#endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)

void PrintViewManagerBase::GetDefaultPrintSettingsReply(
    GetDefaultPrintSettingsCallback callback,
    mojom::PrintParamsPtr params) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop() && !params) {
    // The attempt to use the default settings failed.  There should be no
    // subsequent call to get settings from the user that would normally be
    // shared as part of this client registration.  Immediately notify the
    // service manager that this client is no longer needed.
    UnregisterSystemPrintClient();
  }
#endif
  if (params) {
    set_cookie(params->document_cookie);
    std::move(callback).Run(std::move(params));
  } else {
    set_cookie(PrintSettings::NewInvalidCookie());
    std::move(callback).Run(nullptr);
  }
}

void PrintViewManagerBase::ScriptedPrintReply(
    ScriptedPrintCallback callback,
    int process_id,
    mojom::PrintPagesParamsPtr params) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop()) {
    // Finished getting all settings (defaults and from user), no further need
    // to be registered as a system print client.
    UnregisterSystemPrintClient();
  }
#endif  // BUILDFLAG(ENABLE_OOP_PRINTING)
  if (!content::RenderProcessHost::FromID(process_id)) {
    // Early return if the renderer is not alive.
    return;
  }

  if (params) {
    set_cookie(params->params->document_cookie);
    std::move(callback).Run(std::move(params));
  } else {
    set_cookie(PrintSettings::NewInvalidCookie());
    std::move(callback).Run(nullptr);
  }
}

void PrintViewManagerBase::NavigationStopped() {
  // Cancel the current job, wait for the worker to finish.
  TerminatePrintJob(true);
}

std::u16string PrintViewManagerBase::RenderSourceName() {
  std::u16string name(web_contents()->GetTitle());
  if (name.empty())
    name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
  return name;
}

void PrintViewManagerBase::DidGetPrintedPagesCount(int32_t cookie,
                                                   uint32_t number_pages) {
  PrintManager::DidGetPrintedPagesCount(cookie, number_pages);
  OpportunisticallyCreatePrintJob(cookie);
}

bool PrintViewManagerBase::PrintJobHasDocument(int cookie) {
  if (!OpportunisticallyCreatePrintJob(cookie))
    return false;

  // These checks may fail since we are completely asynchronous. Old spurious
  // messages can be received if one of the processes is overloaded.
  PrintedDocument* document = print_job_->document();
  return document && document->cookie() == cookie;
}

bool PrintViewManagerBase::OnComposePdfDoneImpl(
    int document_cookie,
    const gfx::Size& page_size,
    const gfx::Rect& content_area,
    const gfx::Point& physical_offsets,
    mojom::PrintCompositor::Status status,
    base::ReadOnlySharedMemoryRegion region) {
  if (status != mojom::PrintCompositor::Status::kSuccess) {
    DLOG(ERROR) << "Compositing pdf failed with error " << status;
    return false;
  }

  if (!print_job_ || !print_job_->document() ||
      print_job_->document()->cookie() != document_cookie) {
    return false;
  }

  DCHECK(region.IsValid());
  DCHECK(LooksLikePdf(region.Map().GetMemoryAsSpan<const uint8_t>()));
  scoped_refptr<base::RefCountedSharedMemoryMapping> data =
      base::RefCountedSharedMemoryMapping::CreateFromWholeRegion(region);
  if (!data)
    return false;

  PrintDocument(data, page_size, content_area, physical_offsets);
  return true;
}

void PrintViewManagerBase::OnComposeDocumentDone(
    int document_cookie,
    const gfx::Size& page_size,
    const gfx::Rect& content_area,
    const gfx::Point& physical_offsets,
    DidPrintDocumentCallback callback,
    mojom::PrintCompositor::Status status,
    base::ReadOnlySharedMemoryRegion region) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  bool success =
      OnComposePdfDoneImpl(document_cookie, page_size, content_area,
                           physical_offsets, status, std::move(region));
  OnDidPrintDocument(std::move(callback), success);
}

void PrintViewManagerBase::OnDidPrintDocument(DidPrintDocumentCallback callback,
                                              bool succeeded) {
  std::move(callback).Run(succeeded);
  for (auto& observer : GetTestObservers()) {
    observer.OnDidPrintDocument();
  }
}

void PrintViewManagerBase::DidPrintDocument(
    mojom::DidPrintDocumentParamsPtr params,
    DidPrintDocumentCallback callback) {
  if (!PrintJobHasDocument(params->document_cookie)) {
    OnDidPrintDocument(std::move(callback), /*succeeded=*/false);
    return;
  }

  const mojom::DidPrintContentParams& content = *params->content;
  if (!content.metafile_data_region.IsValid()) {
    NOTREACHED() << "invalid memory handle";
  }

  if (IsOopifEnabled() && print_job_->document()->settings().is_modifiable()) {
    auto* client = PrintCompositeClient::FromWebContents(web_contents());
    client->CompositeDocument(
        params->document_cookie, GetCurrentTargetFrame(), content,
        ui::AXTreeUpdate(), mojom::GenerateDocumentOutline::kNone,
        GetCompositorDocumentType(),
        base::BindOnce(&PrintViewManagerBase::OnComposeDocumentDone,
                       weak_ptr_factory_.GetWeakPtr(), params->document_cookie,
                       params->page_size, params->content_area,
                       params->physical_offsets, std::move(callback)));
    return;
  }
  auto data = base::RefCountedSharedMemoryMapping::CreateFromWholeRegion(
      content.metafile_data_region);
  if (!data) {
    NOTREACHED() << "couldn't map";
  }

  PrintDocument(data, params->page_size, params->content_area,
                params->physical_offsets);
  OnDidPrintDocument(std::move(callback), /*succeeded=*/true);
}

void PrintViewManagerBase::GetDefaultPrintSettings(
    GetDefaultPrintSettingsCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!GetPrintingEnabledBooleanPref()) {
    GetDefaultPrintSettingsReply(std::move(callback), nullptr);
    return;
  }

  content::RenderFrameHost* render_frame_host = GetCurrentTargetFrame();
  if (!render_frame_host->IsActive()) {
    // Only active RFHs should show UI elements.
    GetDefaultPrintSettingsReply(std::move(callback), nullptr);
    return;
  }

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop() &&
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
      !analyzing_content_ &&
#endif
      !query_with_ui_client_id().has_value()) {
    // Script initiated print, this is first signal of start of printing.
    RegisterSystemPrintClient();
  }
#endif

  content::RenderProcessHost* render_process_host =
      render_frame_host->GetProcess();
  auto callback_wrapper =
      base::BindOnce(&PrintViewManagerBase::GetDefaultPrintSettingsReply,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback));
  std::unique_ptr<PrinterQuery> printer_query =
      queue()->PopPrinterQuery(PrintSettings::NewInvalidCookie());
  if (!printer_query) {
    printer_query =
        queue()->CreatePrinterQuery(render_frame_host->GetGlobalId());
#if BUILDFLAG(ENABLE_OOP_PRINTING)
    if (query_with_ui_client_id().has_value()) {
      printer_query->SetClientId(query_with_ui_client_id().value());
    }
#endif
  }

  // Sometimes it is desired to get the PDF settings as opposed to the settings
  // of the default system print driver.
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  bool want_pdf_settings = analyzing_content_;
#else
  bool want_pdf_settings = false;
#endif

  // Loads default settings. This is asynchronous, only the mojo message sender
  // will hang until the settings are retrieved.
  auto* printer_query_ptr = printer_query.get();
  printer_query_ptr->GetDefaultSettings(
      base::BindOnce(&OnDidGetDefaultPrintSettings, queue_, want_pdf_settings,
                     std::move(printer_query), std::move(callback_wrapper)),
      !render_process_host->IsPdf(), want_pdf_settings);
}

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::UpdatePrintSettings(
    base::Value::Dict job_settings,
    UpdatePrintSettingsCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!GetPrintingEnabledBooleanPref()) {
    std::move(callback).Run(nullptr);
    return;
  }

  std::optional<int> printer_type_value =
      job_settings.FindInt(kSettingPrinterType);
  if (!printer_type_value) {
    std::move(callback).Run(nullptr);
    return;
  }

  mojom::PrinterType printer_type =
      static_cast<mojom::PrinterType>(*printer_type_value);
  if (printer_type != mojom::PrinterType::kExtension &&
      printer_type != mojom::PrinterType::kPdf &&
      printer_type != mojom::PrinterType::kLocal) {
    std::move(callback).Run(nullptr);
    return;
  }

  // `job_settings` does not yet contain the rasterized PDF dpi, so if the user
  // has the print preference set, fetch it for use in
  // `PrintSettingsFromJobSettings()`.
  content::BrowserContext* context =
      web_contents() ? web_contents()->GetBrowserContext() : nullptr;
  PrefService* prefs =
      context ? Profile::FromBrowserContext(context)->GetPrefs() : nullptr;
  if (prefs && prefs->HasPrefPath(prefs::kPrintRasterizePdfDpi)) {
    int value = prefs->GetInteger(prefs::kPrintRasterizePdfDpi);
    if (value > 0)
      job_settings.Set(kSettingRasterizePdfDpi, value);
  }

  std::unique_ptr<PrintSettings> print_settings =
      PrintSettingsFromJobSettings(job_settings);
  if (!print_settings) {
    std::move(callback).Run(nullptr);
    return;
  }

  bool open_in_external_preview =
      job_settings.contains(kSettingOpenPDFInPreview);
  if (!open_in_external_preview &&
      (printer_type == mojom::PrinterType::kPdf ||
       printer_type == mojom::PrinterType::kExtension)) {
    if (print_settings->page_setup_device_units().printable_area().IsEmpty()) {
      PrinterQuery::ApplyDefaultPrintableAreaToVirtualPrinterPrintSettings(
          *print_settings);
    }
  }

#if BUILDFLAG(IS_WIN)
  // TODO(crbug.com/40260379):  Remove this if the printable areas can be made
  // fully available from `PrintBackend::GetPrinterSemanticCapsAndDefaults()`
  // for in-browser queries.
  if (printer_type == mojom::PrinterType::kLocal) {
    // Without a document cookie to find a previous query, must generate a
    // fresh printer query each time, even if the paper size didn't change.
    std::unique_ptr<PrinterQuery> printer_query =
        queue()->CreatePrinterQuery(GetCurrentTargetFrame()->GetGlobalId());

    auto* printer_query_ptr = printer_query.get();
    auto* print_settings_ptr = print_settings.get();
    printer_query_ptr->UpdatePrintableArea(
        print_settings_ptr,
        base::BindOnce(&PrintViewManagerBase::OnDidUpdatePrintableArea,
                       weak_ptr_factory_.GetWeakPtr(), std::move(printer_query),
                       std::move(job_settings), std::move(print_settings),
                       std::move(callback)));
    return;
  }
#endif

  CompleteUpdatePrintSettings(std::move(job_settings),
                              std::move(print_settings), std::move(callback));
}

void PrintViewManagerBase::SetAccessibilityTree(
    int32_t cookie,
    const ui::AXTreeUpdate& accessibility_tree) {
  auto* client = PrintCompositeClient::FromWebContents(web_contents());
  if (client) {
    client->SetAccessibilityTree(cookie, accessibility_tree);
  }
}
#endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)

void PrintViewManagerBase::IsPrintingEnabled(
    IsPrintingEnabledCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  std::move(callback).Run(GetPrintingEnabledBooleanPref());
}

void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
                                         ScriptedPrintCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  content::RenderFrameHost* render_frame_host = GetCurrentTargetFrame();
  content::RenderProcessHost* render_process_host =
      render_frame_host->GetProcess();
  if (params->is_scripted && render_frame_host->IsNestedWithinFencedFrame()) {
    // The renderer should have checked and disallowed the request for fenced
    // frames in ChromeClient. Ignore the request and mark it as bad if it
    // didn't happen for some reason.
    bad_message::ReceivedBadMessage(
        render_process_host, bad_message::PVMB_SCRIPTED_PRINT_FENCED_FRAME);
    std::move(callback).Run(nullptr);
    return;
  }
#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop() && !query_with_ui_client_id().has_value()) {
    // Renderer process has requested settings outside of the expected setup.
    std::move(callback).Run(nullptr);
    return;
  }
#endif
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  std::optional<enterprise_connectors::ContentAnalysisDelegate::Data>
      scanning_data = enterprise_data_protection::GetPrintAnalysisData(
          web_contents(), enterprise_data_protection::PrintScanningContext::
                              kBeforeSystemDialog);
  if (scanning_data) {
    set_content_analysis_before_printing_document(base::BindOnce(
        &PrintViewManagerBase::ContentAnalysisBeforePrintingDocument,
        weak_ptr_factory_.GetWeakPtr(), std::move(*scanning_data)));
  }
#endif  // BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)

  CompleteScriptedPrint(render_frame_host, std::move(params),
                        std::move(callback));
}

void PrintViewManagerBase::PrintingFailed(int32_t cookie,
                                          mojom::PrintFailureReason reason) {
  // Note: Not redundant with cookie checks in the same method in other parts of
  // the class hierarchy.
  if (!IsValidCookie(cookie))
    return;

  PrintManager::PrintingFailed(cookie, reason);

  // `PrintingFailed()` can occur because asynchronous compositing results
  // don't complete until after a print job has already failed and been
  // destroyed.  In such cases the error notification to the user will
  // have already been displayed, and a second message should not be
  // shown.
  if (print_job_ && print_job_->document() &&
      print_job_->document()->cookie() == cookie) {
    ShowPrintErrorDialogForGenericError();
  }

  ReleasePrinterQuery();
}

void PrintViewManagerBase::AddTestObserver(TestObserver& observer) {
  test_observers_.AddObserver(&observer);
}

void PrintViewManagerBase::RemoveTestObserver(TestObserver& observer) {
  test_observers_.RemoveObserver(&observer);
}

void PrintViewManagerBase::RenderFrameHostStateChanged(
    content::RenderFrameHost* render_frame_host,
    content::RenderFrameHost::LifecycleState /*old_state*/,
    content::RenderFrameHost::LifecycleState new_state) {
  if (new_state == content::RenderFrameHost::LifecycleState::kActive &&
      render_frame_host->GetProcess()->IsPdf() &&
      !render_frame_host->GetMainFrame()->GetParentOrOuterDocument()) {
    GetPrintRenderFrame(render_frame_host)->ConnectToPdfRenderer();
  }
}

void PrintViewManagerBase::RenderFrameDeleted(
    content::RenderFrameHost* render_frame_host) {
  PrintManager::RenderFrameDeleted(render_frame_host);

  // Terminates or cancels the print job if one was pending.
  if (render_frame_host != printing_rfh_)
    return;

  for (auto& observer : GetTestObservers()) {
    observer.OnRenderFrameDeleted();
  }

  printing_rfh_ = nullptr;

  PrintManager::PrintingRenderFrameDeleted();
  ReleasePrinterQuery();
#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop()) {
    UnregisterSystemPrintClient();
  }
#endif

  if (!print_job_)
    return;

  scoped_refptr<PrintedDocument> document(print_job_->document());
  if (document) {
    // If IsComplete() returns false, the document isn't completely rendered.
    // Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
    // the print job may finish without problem.
    TerminatePrintJob(!document->IsComplete());
  }
}

#if BUILDFLAG(IS_WIN) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::SystemDialogCancelled() {
  // System dialog was cancelled. Clean up the print job and notify the
  // BackgroundPrintingManager.
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  ReleasePrinterQuery();
  TerminatePrintJob(true);
}
#endif

bool PrintViewManagerBase::GetPrintingEnabledBooleanPref() const {
  return printing_enabled_.GetValue();
}

void PrintViewManagerBase::OnDocDone(int job_id, PrintedDocument* document) {
#if BUILDFLAG(IS_ANDROID)
  DCHECK_LE(number_pages(), kMaxPageCount);
  PdfWritingDone(base::checked_cast<int>(number_pages()));
#endif
}

void PrintViewManagerBase::OnJobDone() {
  // Printing is done, we don't need it anymore.
  // print_job_->is_job_pending() may still be true, depending on the order
  // of object registration.
  printing_succeeded_ = true;
  ReleasePrintJob();
}

void PrintViewManagerBase::OnCanceling() {
  canceling_job_ = true;
}

void PrintViewManagerBase::OnFailed() {
  if (!canceling_job_)
    ShowPrintErrorDialogForGenericError();

  TerminatePrintJob(true);
}

bool PrintViewManagerBase::RenderAllMissingPagesNow() {
  if (!print_job_ || !print_job_->is_job_pending())
    return false;

  // Is the document already complete?
  if (print_job_->document() && print_job_->document()->IsComplete()) {
    printing_succeeded_ = true;
    return true;
  }

  // We can't print if there is no renderer.
  if (!web_contents() ||
      !web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive()) {
    return false;
  }

  // WebContents is either dying or a second consecutive request to print
  // happened before the first had time to finish. We need to render all the
  // pages in an hurry if a print_job_ is still pending. No need to wait for it
  // to actually spool the pages, only to have the renderer generate them. Run
  // a message loop until we get our signal that the print job is satisfied.
  // `quit_inner_loop_` will be called as soon as
  // print_job_->document()->IsComplete() is true in DidPrintDocument(). The
  // check is done in ShouldQuitFromInnerMessageLoop().
  // BLOCKS until all the pages are received. (Need to enable recursive task)
  // WARNING: Do not do any work after RunInnerMessageLoop() returns, as `this`
  // may have gone away.
  if (!RunInnerMessageLoop()) {
    // This function is always called from DisconnectFromCurrentPrintJob() so we
    // know that the job will be stopped/canceled in any case.
    return false;
  }
  return true;
}

void PrintViewManagerBase::ShouldQuitFromInnerMessageLoop() {
  // Look at the reason.
  DCHECK(print_job_->document());
  if (print_job_->document() && print_job_->document()->IsComplete() &&
      quit_inner_loop_) {
    // We are in a message loop created by RenderAllMissingPagesNow. Quit from
    // it.
    std::move(quit_inner_loop_).Run();
  }
}

scoped_refptr<PrintJob> PrintViewManagerBase::CreatePrintJob(
    PrintJobManager* print_job_manager) {
  return base::MakeRefCounted<PrintJob>(print_job_manager);
}

bool PrintViewManagerBase::SetupNewPrintJob(
    std::unique_ptr<PrinterQuery> query) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!quit_inner_loop_);
  DCHECK(query);

  // Disconnect the current `print_job_`.
  auto weak_this = weak_ptr_factory_.GetWeakPtr();
  DisconnectFromCurrentPrintJob();
  if (!weak_this)
    return false;

  // We can't print if there is no renderer.
  if (!web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive()) {
    return false;
  }

  DCHECK(!print_job_);
  print_job_ = CreatePrintJob(g_browser_process->print_job_manager());
  print_job_->Initialize(std::move(query), RenderSourceName(), number_pages());
#if BUILDFLAG(IS_CHROMEOS)
  print_job_->SetSource(web_contents()->GetBrowserContext()->IsOffTheRecord()
                            ? PrintJob::Source::kPrintPreviewIncognito
                            : PrintJob::Source::kPrintPreview,
                        /*source_id=*/"");
#endif
  print_job_->AddObserver(*this);

  printing_succeeded_ = false;
  return true;
}

void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
  // Make sure all the necessary rendered page are done. Don't bother with the
  // return value.
  auto weak_this = weak_ptr_factory_.GetWeakPtr();
  bool result = RenderAllMissingPagesNow();
  if (!weak_this)
    return;

  // Verify that assertion.
  if (print_job_ && print_job_->document() &&
      !print_job_->document()->IsComplete()) {
    DCHECK(!result);
    // That failed.
    TerminatePrintJob(true);
  } else {
    // DO NOT wait for the job to finish.
    ReleasePrintJob();
  }
}

void PrintViewManagerBase::TerminatePrintJob(bool cancel) {
  if (!print_job_)
    return;

  if (cancel) {
    canceling_job_ = true;

    // We don't need the metafile data anymore because the printing is canceled.
    print_job_->Cancel();
    quit_inner_loop_.Reset();
#if BUILDFLAG(IS_ANDROID)
    PdfWritingDone(0);
#endif
  } else {
    DCHECK(!quit_inner_loop_);
    DCHECK(!print_job_->document() || print_job_->document()->IsComplete());

    // WebContents is either dying or navigating elsewhere. We need to render
    // all the pages in an hurry if a print job is still pending. This does the
    // trick since it runs a blocking message loop:
    print_job_->Stop();
  }
  ReleasePrintJob();
}

void PrintViewManagerBase::ReleasePrintJob() {
  content::RenderFrameHost* rfh = printing_rfh_;
  printing_rfh_ = nullptr;

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop()) {
    // Ensure that any residual registration of printing client is released.
    // This might be necessary in some abnormal cases, such as the associated
    // render process having terminated.
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
    if (!analyzing_content_) {
      UnregisterSystemPrintClient();
    }
#else
    UnregisterSystemPrintClient();
#endif
  }
#endif

  if (!print_job_)
    return;

  if (rfh) {
    // printing_rfh_ should only ever point to a RenderFrameHost with a live
    // RenderFrame.
    DCHECK(rfh->IsRenderFrameLive());
    GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  }

  print_job_->RemoveObserver(*this);

  // Don't close the worker thread.
  print_job_ = nullptr;
}

bool PrintViewManagerBase::RunInnerMessageLoop() {
  // This value may actually be too low:
  //
  // - If we're looping because of printer settings initialization, the premise
  // here is that some poor users have their print server away on a VPN over a
  // slow connection. In this situation, the simple fact of opening the printer
  // can be dead slow. On the other side, we don't want to die infinitely for a
  // real network error. Give the printer 60 seconds to comply.
  //
  // - If we're looping because of renderer page generation, the renderer could
  // be CPU bound, the page overly complex/large or the system just
  // memory-bound.
  static constexpr base::TimeDelta kPrinterSettingsTimeout = base::Seconds(60);
  base::OneShotTimer quit_timer;
  base::RunLoop run_loop{base::RunLoop::Type::kNestableTasksAllowed};
  quit_timer.Start(FROM_HERE, kPrinterSettingsTimeout,
                   run_loop.QuitWhenIdleClosure());

  quit_inner_loop_ = run_loop.QuitClosure();

  auto weak_this = weak_ptr_factory_.GetWeakPtr();
  run_loop.Run();
  if (!weak_this)
    return false;

  // If the inner-loop quit closure is still set then we timed out.
  bool success = !quit_inner_loop_;
  quit_inner_loop_.Reset();

  return success;
}

bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  if (print_job_)
    return true;

  if (!cookie) {
    // Out of sync. It may happen since we are completely asynchronous. Old
    // spurious message can happen if one of the processes is overloaded.
    return false;
  }

  // The job was initiated by a script. Time to get the corresponding worker
  // thread.
  std::unique_ptr<PrinterQuery> queued_query = queue()->PopPrinterQuery(cookie);
  if (!queued_query) {
    // Out of sync.  It may happen since we are completely asynchronous, when
    // an error occurs during the first setup of a print job.
    return false;
  }

  if (!SetupNewPrintJob(std::move(queued_query))) {
    // Don't kill anything.
    return false;
  }

#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
  // Don't start printing if enterprise checks are being performed to check if
  // printing is allowed, or if content analysis is going to take place right
  // before starting `print_job_`.
  if (analyzing_content_ || content_analysis_before_printing_document_) {
    return true;
  }
#endif

  // Settings are already loaded. Go ahead. This will set
  // print_job_->is_job_pending() to true.
  print_job_->StartPrinting();
  return true;
}

bool PrintViewManagerBase::IsCrashed() {
  return web_contents()->IsCrashed();
}

void PrintViewManagerBase::SetPrintingRFH(content::RenderFrameHost* rfh) {
  // Do not allow any print operation during prerendering.
  if (rfh->GetLifecycleState() ==
      content::RenderFrameHost::LifecycleState::kPrerendering) {
    // If we come here during prerendering, it's because either:
    // 1) Renderer did something unexpected (indicates a compromised renderer),
    // or 2) Some plumbing in the browser side is wrong (wrong code).
    // mojo::ReportBadMessage() below will let the renderer crash for 1), or
    // will hit DCHECK for 2).
    mojo::ReportBadMessage(
        "The print's message shouldn't reach here during prerendering.");
    return;
  }
  DCHECK(!printing_rfh_);
  // Protect against future unsafety, since printing_rfh_ is cleared by
  // RenderFrameDeleted(), which will not be called if the render frame is not
  // live.
  CHECK(rfh->IsRenderFrameLive());
  printing_rfh_ = rfh;
}

bool PrintViewManagerBase::StartPrintCommon(content::RenderFrameHost* rfh) {
  // Remember the ID for `rfh`, to enable checking that the `RenderFrameHost`
  // is still valid after a possible inner message loop runs in
  // `DisconnectFromCurrentPrintJob()`.
  content::GlobalRenderFrameHostId rfh_id = rfh->GetGlobalId();
  auto weak_this = weak_ptr_factory_.GetWeakPtr();
  DisconnectFromCurrentPrintJob();
  if (!weak_this) {
    return false;
  }

  // Don't print / print preview crashed tabs.
  if (IsCrashed()) {
    return false;
  }

  // Don't print if `rfh` is no longer live.
  if (!content::RenderFrameHost::FromID(rfh_id) || !rfh->IsRenderFrameLive()) {
    return false;
  }

#if BUILDFLAG(ENABLE_OOP_PRINTING)
  if (ShouldPrintJobOop()) {
    // Register this worker so that the service persists as long as the user
    // keeps the system print dialog UI displayed.
    if (!RegisterSystemPrintClient()) {
      return false;
    }
  }
#endif

  SetPrintingRFH(rfh);
  return true;
}

#if BUILDFLAG(ENABLE_OOP_PRINTING)
bool PrintViewManagerBase::RegisterSystemPrintClient() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(ShouldPrintJobOop());
  DCHECK(!query_with_ui_client_id().has_value());
  query_with_ui_client_id_ =
      PrintBackendServiceManager::GetInstance().RegisterQueryWithUiClient();
  bool registered = query_with_ui_client_id().has_value();
  if (!registered) {
    PRINTER_LOG(DEBUG) << "Unable to initiate a concurrent system print dialog";
  }
  for (auto& observer : GetTestObservers()) {
    observer.OnRegisterSystemPrintClient(registered);
  }
  return registered;
}

void PrintViewManagerBase::UnregisterSystemPrintClient() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(ShouldPrintJobOop());
  if (!query_with_ui_client_id().has_value()) {
    return;
  }

  PrintBackendServiceManager::GetInstance().UnregisterClient(
      query_with_ui_client_id().value());
  query_with_ui_client_id_.reset();
}
#endif  // BUILDFLAG(ENABLE_OOP_PRINTING)

void PrintViewManagerBase::ReleasePrinterQuery() {
  int current_cookie = cookie();
  if (!current_cookie)
    return;

  set_cookie(PrintSettings::NewInvalidCookie());

  PrintJobManager* print_job_manager = g_browser_process->print_job_manager();
  // May be NULL in tests.
  if (!print_job_manager)
    return;

  // Let `printer_query` go out of scope to release it.
  std::unique_ptr<PrinterQuery> printer_query =
      queue()->PopPrinterQuery(current_cookie);
}

void PrintViewManagerBase::CompleteScriptedPrint(
    content::RenderFrameHost* rfh,
    mojom::ScriptedPrintParamsPtr params,
    ScriptedPrintCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  content::RenderProcessHost* render_process_host = rfh->GetProcess();
  auto callback_wrapper = base::BindOnce(
      &PrintViewManagerBase::ScriptedPrintReply, weak_ptr_factory_.GetWeakPtr(),
      std::move(callback), render_process_host->GetDeprecatedID());
  std::unique_ptr<PrinterQuery> printer_query =
      queue()->PopPrinterQuery(params->cookie);
  if (!printer_query)
    printer_query = queue()->CreatePrinterQuery(rfh->GetGlobalId());

  auto* printer_query_ptr = printer_query.get();
  printer_query_ptr->GetSettingsFromUser(
      params->expected_pages_count, params->has_selection, params->margin_type,
      params->is_scripted, !render_process_host->IsPdf(),
      base::BindOnce(&OnDidScriptedPrint, queue_, std::move(printer_query),
                     std::move(callback_wrapper)));
}

#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
void PrintViewManagerBase::CompletePrintDocumentAfterContentAnalysis(
    scoped_refptr<base::RefCountedMemory> print_data,
    const gfx::Size& page_size,
    const gfx::Rect& content_area,
    const gfx::Point& offsets,
    bool allowed) {
  if (!allowed || IsCrashed()) {
    ReleasePrinterQuery();
    print_job_->CleanupAfterContentAnalysisDenial();
    TerminatePrintJob(/*cancel=*/true);
    return;
  }
  print_job_->StartPrinting();
  PrintDocument(print_data, page_size, content_area, offsets);
}

void PrintViewManagerBase::ContentAnalysisBeforePrintingDocument(
    enterprise_connectors::ContentAnalysisDelegate::Data scanning_data,
    scoped_refptr<base::RefCountedMemory> print_data,
    const gfx::Size& page_size,
    const gfx::Rect& content_area,
    const gfx::Point& offsets) {
  scanning_data.printer_name =
      base::UTF16ToUTF8(print_job_->document()->settings().device_name());

  auto on_verdict = base::BindOnce(
      &PrintViewManagerBase::CompletePrintDocumentAfterContentAnalysis,
      weak_ptr_factory_.GetWeakPtr(), print_data, page_size, content_area,
      offsets);

  enterprise_data_protection::PrintIfAllowedByPolicy(
      print_data, web_contents()->GetOutermostWebContents(),
      std::move(scanning_data), std::move(on_verdict));
}

void PrintViewManagerBase::set_analyzing_content(bool analyzing) {
  PRINTER_LOG(EVENT) << (analyzing ? "Starting" : "Completed")
                     << " content analysis";
  analyzing_content_ = analyzing;
}

void PrintViewManagerBase::set_content_analysis_before_printing_document(
    PrintDocumentCallback callback) {
  content_analysis_before_printing_document_ = std::move(callback);
}
#endif  // BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)

}  // namespace printing