printing.patch 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Fri, 7 Jun 2019 13:59:37 -0700
  4. Subject: printing.patch
  5. Add changeset that was previously applied to sources in chromium_src. The
  6. majority of changes originally come from these PRs:
  7. * https://github.com/electron/electron/pull/1835
  8. * https://github.com/electron/electron/pull/8596
  9. This patch also fixes callback for manual user cancellation and success.
  10. diff --git a/BUILD.gn b/BUILD.gn
  11. index dfcf0ad8238688f9e25d09cc3842cd8a42cf7316..e7b0bd77727f9c352a287a3100f35f7672d4b4a8 100644
  12. --- a/BUILD.gn
  13. +++ b/BUILD.gn
  14. @@ -974,7 +974,6 @@ if (is_win) {
  15. "//media:media_unittests",
  16. "//media/midi:midi_unittests",
  17. "//net:net_unittests",
  18. - "//printing:printing_unittests",
  19. "//sql:sql_unittests",
  20. "//third_party/breakpad:symupload($host_toolchain)",
  21. "//ui/base:ui_base_unittests",
  22. @@ -983,6 +982,10 @@ if (is_win) {
  23. "//ui/views:views_unittests",
  24. "//url:url_unittests",
  25. ]
  26. +
  27. + if (enable_printing) {
  28. + deps += [ "//printing:printing_unittests" ]
  29. + }
  30. }
  31. }
  32. diff --git a/chrome/browser/printing/print_job.cc b/chrome/browser/printing/print_job.cc
  33. index c27e2d50aa6cd45484c222221c6a8dda50baf8ff..43c48b75806bb08f3a4ea45689fe2469368cca4a 100644
  34. --- a/chrome/browser/printing/print_job.cc
  35. +++ b/chrome/browser/printing/print_job.cc
  36. @@ -93,6 +93,7 @@ bool PrintWithReducedRasterization(PrefService* prefs) {
  37. return base::FeatureList::IsEnabled(features::kPrintWithReducedRasterization);
  38. }
  39. +#if 0
  40. PrefService* GetPrefsForWebContents(content::WebContents* web_contents) {
  41. // TODO(thestig): Figure out why crbug.com/1083911 occurred, which is likely
  42. // because `web_contents` was null. As a result, this section has many more
  43. @@ -107,6 +108,7 @@ content::WebContents* GetWebContents(content::GlobalRenderFrameHostId rfh_id) {
  44. auto* rfh = content::RenderFrameHost::FromID(rfh_id);
  45. return rfh ? content::WebContents::FromRenderFrameHost(rfh) : nullptr;
  46. }
  47. +#endif
  48. #endif // BUILDFLAG(IS_WIN)
  49. @@ -147,10 +149,8 @@ void PrintJob::Initialize(std::unique_ptr<PrinterQuery> query,
  50. #if BUILDFLAG(IS_WIN)
  51. pdf_page_mapping_ = PageNumber::GetPages(settings->ranges(), page_count);
  52. - PrefService* prefs = GetPrefsForWebContents(GetWebContents(rfh_id_));
  53. - if (prefs && prefs->IsManagedPreference(prefs::kPdfUseSkiaRendererEnabled)) {
  54. - use_skia_ = prefs->GetBoolean(prefs::kPdfUseSkiaRendererEnabled);
  55. - }
  56. + // TODO(codebytere): should we enable this later?
  57. + use_skia_ = false;
  58. #endif
  59. auto new_doc = base::MakeRefCounted<PrintedDocument>(std::move(settings),
  60. @@ -381,8 +381,10 @@ void PrintJob::StartPdfToEmfConversion(
  61. const PrintSettings& settings = document()->settings();
  62. +#if 0
  63. PrefService* prefs = GetPrefsForWebContents(GetWebContents(rfh_id_));
  64. - bool print_with_reduced_rasterization = PrintWithReducedRasterization(prefs);
  65. +#endif
  66. + bool print_with_reduced_rasterization = PrintWithReducedRasterization(nullptr);
  67. using RenderMode = PdfRenderSettings::Mode;
  68. RenderMode mode = print_with_reduced_rasterization
  69. @@ -472,8 +474,10 @@ void PrintJob::StartPdfToPostScriptConversion(
  70. if (ps_level2) {
  71. mode = PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2;
  72. } else {
  73. +#if 0
  74. PrefService* prefs = GetPrefsForWebContents(GetWebContents(rfh_id_));
  75. - mode = PrintWithPostScriptType42Fonts(prefs)
  76. +#endif
  77. + mode = PrintWithPostScriptType42Fonts(nullptr)
  78. ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3_WITH_TYPE42_FONTS
  79. : PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3;
  80. }
  81. diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
  82. index 6e5f0ae32837f5b08ff366b698a262b041b2bb96..bf31824cbeb49ccbe52b81a91edb3c884d5d8351 100644
  83. --- a/chrome/browser/printing/print_view_manager_base.cc
  84. +++ b/chrome/browser/printing/print_view_manager_base.cc
  85. @@ -23,7 +23,9 @@
  86. #include "chrome/browser/bad_message.h"
  87. #include "chrome/browser/browser_process.h"
  88. #include "chrome/browser/chrome_notification_types.h"
  89. +#if 0 // Electron does not use Chrome error dialogs
  90. #include "chrome/browser/printing/print_error_dialog.h"
  91. +#endif
  92. #include "chrome/browser/printing/print_job.h"
  93. #include "chrome/browser/printing/print_job_manager.h"
  94. #include "chrome/browser/printing/print_view_manager_common.h"
  95. @@ -83,6 +85,20 @@ namespace printing {
  96. namespace {
  97. +std::string PrintReasonFromPrintStatus(PrintViewManager::PrintStatus status) {
  98. + if (status == PrintViewManager::PrintStatus::kInvalid) {
  99. + return "Invalid printer settings";
  100. + } else if (status == PrintViewManager::PrintStatus::kCanceled) {
  101. + return "Print job canceled";
  102. + } else if (status == PrintViewManager::PrintStatus::kFailed) {
  103. + return "Print job failed";
  104. + }
  105. + return "";
  106. +}
  107. +
  108. +using PrintSettingsCallback =
  109. + base::OnceCallback<void(std::unique_ptr<PrinterQuery>)>;
  110. +
  111. void OnDidGetDefaultPrintSettings(
  112. scoped_refptr<PrintQueriesQueue> queue,
  113. bool want_pdf_settings,
  114. @@ -91,9 +107,11 @@ void OnDidGetDefaultPrintSettings(
  115. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  116. if (printer_query->last_status() != mojom::ResultCode::kSuccess) {
  117. +#if 0 // Electron does not use Chrome error dialogs
  118. if (!want_pdf_settings) {
  119. ShowPrintErrorDialogForInvalidPrinterError();
  120. }
  121. +#endif
  122. std::move(callback).Run(nullptr);
  123. return;
  124. }
  125. @@ -103,9 +121,11 @@ void OnDidGetDefaultPrintSettings(
  126. params->document_cookie = printer_query->cookie();
  127. if (!PrintMsgPrintParamsIsValid(*params)) {
  128. +#if 0 // Electron does not use Chrome error dialogs
  129. if (!want_pdf_settings) {
  130. ShowPrintErrorDialogForInvalidPrinterError();
  131. }
  132. +#endif
  133. std::move(callback).Run(nullptr);
  134. return;
  135. }
  136. @@ -122,7 +142,8 @@ void OnDidScriptedPrint(
  137. if (printer_query->last_status() != mojom::ResultCode::kSuccess ||
  138. !printer_query->settings().dpi()) {
  139. - std::move(callback).Run(nullptr);
  140. + bool canceled = printer_query->last_status() == mojom::ResultCode::kCanceled;
  141. + std::move(callback).Run(nullptr, canceled);
  142. return;
  143. }
  144. @@ -132,12 +153,12 @@ void OnDidScriptedPrint(
  145. params->params.get());
  146. params->params->document_cookie = printer_query->cookie();
  147. if (!PrintMsgPrintParamsIsValid(*params->params)) {
  148. - std::move(callback).Run(nullptr);
  149. + std::move(callback).Run(nullptr, false);
  150. return;
  151. }
  152. params->pages = printer_query->settings().ranges();
  153. - std::move(callback).Run(std::move(params));
  154. + std::move(callback).Run(std::move(params), false);
  155. queue->QueuePrinterQuery(std::move(printer_query));
  156. }
  157. @@ -174,9 +195,11 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
  158. : PrintManager(web_contents),
  159. queue_(g_browser_process->print_job_manager()->queue()) {
  160. DCHECK(queue_);
  161. +#if 0 // Printing is always enabled.
  162. Profile* profile =
  163. Profile::FromBrowserContext(web_contents->GetBrowserContext());
  164. - printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
  165. + printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
  166. +#endif
  167. }
  168. PrintViewManagerBase::~PrintViewManagerBase() {
  169. @@ -199,12 +222,20 @@ void PrintViewManagerBase::DisableThirdPartyBlocking() {
  170. }
  171. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
  172. -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  173. +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
  174. + bool silent,
  175. + base::Value::Dict settings,
  176. + CompletionCallback callback) {
  177. if (!StartPrintCommon(rfh)) {
  178. return false;
  179. }
  180. +#if 0
  181. CompletePrintNow(rfh);
  182. +#endif
  183. + callback_ = std::move(callback);
  184. +
  185. + GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
  186. return true;
  187. }
  188. @@ -327,12 +358,13 @@ void PrintViewManagerBase::OnDidUpdatePrintableArea(
  189. }
  190. PRINTER_LOG(EVENT) << "Paper printable area updated for vendor id "
  191. << print_settings->requested_media().vendor_id;
  192. - CompleteUpdatePrintSettings(std::move(job_settings),
  193. + CompleteUpdatePrintSettings(nullptr /* printer_query */, std::move(job_settings),
  194. std::move(print_settings), std::move(callback));
  195. }
  196. #endif
  197. void PrintViewManagerBase::CompleteUpdatePrintSettings(
  198. + std::unique_ptr<PrinterQuery> printer_query,
  199. base::Value::Dict job_settings,
  200. std::unique_ptr<PrintSettings> print_settings,
  201. UpdatePrintSettingsCallback callback) {
  202. @@ -340,7 +372,8 @@ void PrintViewManagerBase::CompleteUpdatePrintSettings(
  203. settings->pages = GetPageRangesFromJobSettings(job_settings);
  204. settings->params = mojom::PrintParams::New();
  205. RenderParamsFromPrintSettings(*print_settings, settings->params.get());
  206. - settings->params->document_cookie = PrintSettings::NewCookie();
  207. + settings->params->document_cookie = printer_query ? printer_query->cookie()
  208. + : PrintSettings::NewCookie();
  209. if (!PrintMsgPrintParamsIsValid(*settings->params)) {
  210. mojom::PrinterType printer_type = static_cast<mojom::PrinterType>(
  211. *job_settings.FindInt(kSettingPrinterType));
  212. @@ -352,6 +385,10 @@ void PrintViewManagerBase::CompleteUpdatePrintSettings(
  213. return;
  214. }
  215. + if (printer_query && printer_query->cookie() && printer_query->settings().dpi()) {
  216. + queue_->QueuePrinterQuery(std::move(printer_query));
  217. + }
  218. +
  219. set_cookie(settings->params->document_cookie);
  220. std::move(callback).Run(std::move(settings));
  221. }
  222. @@ -487,7 +524,8 @@ void PrintViewManagerBase::GetDefaultPrintSettingsReply(
  223. void PrintViewManagerBase::ScriptedPrintReply(
  224. ScriptedPrintCallback callback,
  225. int process_id,
  226. - mojom::PrintPagesParamsPtr params) {
  227. + mojom::PrintPagesParamsPtr params,
  228. + bool canceled) {
  229. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  230. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  231. @@ -502,12 +540,15 @@ void PrintViewManagerBase::ScriptedPrintReply(
  232. return;
  233. }
  234. + if (canceled)
  235. + UserInitCanceled();
  236. +
  237. if (params) {
  238. set_cookie(params->params->document_cookie);
  239. - std::move(callback).Run(std::move(params));
  240. + std::move(callback).Run(std::move(params), canceled);
  241. } else {
  242. set_cookie(PrintSettings::NewInvalidCookie());
  243. - std::move(callback).Run(nullptr);
  244. + std::move(callback).Run(nullptr, false);
  245. }
  246. }
  247. @@ -635,10 +676,12 @@ void PrintViewManagerBase::DidPrintDocument(
  248. void PrintViewManagerBase::GetDefaultPrintSettings(
  249. GetDefaultPrintSettingsCallback callback) {
  250. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  251. +#if 0 // Printing is always enabled.
  252. if (!printing_enabled_.GetValue()) {
  253. GetDefaultPrintSettingsReply(std::move(callback), nullptr);
  254. return;
  255. }
  256. +#endif
  257. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  258. if (printing::features::kEnableOopPrintDriversJobPrint.Get() &&
  259. #if BUILDFLAG(ENABLE_PRINT_CONTENT_ANALYSIS)
  260. @@ -690,10 +733,12 @@ void PrintViewManagerBase::UpdatePrintSettings(
  261. base::Value::Dict job_settings,
  262. UpdatePrintSettingsCallback callback) {
  263. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  264. +#if 0 // Printing is always enabled.
  265. if (!printing_enabled_.GetValue()) {
  266. std::move(callback).Run(nullptr);
  267. return;
  268. }
  269. +#endif // Printing is always enabled.
  270. absl::optional<int> printer_type_value =
  271. job_settings.FindInt(kSettingPrinterType);
  272. @@ -704,6 +749,7 @@ void PrintViewManagerBase::UpdatePrintSettings(
  273. mojom::PrinterType printer_type =
  274. static_cast<mojom::PrinterType>(*printer_type_value);
  275. +#if 0 // Printing is always enabled.
  276. if (printer_type != mojom::PrinterType::kExtension &&
  277. printer_type != mojom::PrinterType::kPdf &&
  278. printer_type != mojom::PrinterType::kLocal) {
  279. @@ -723,6 +769,7 @@ void PrintViewManagerBase::UpdatePrintSettings(
  280. if (value > 0)
  281. job_settings.Set(kSettingRasterizePdfDpi, value);
  282. }
  283. +#endif // Printing is always enabled.
  284. std::unique_ptr<PrintSettings> print_settings =
  285. PrintSettingsFromJobSettings(job_settings);
  286. @@ -742,7 +789,21 @@ void PrintViewManagerBase::UpdatePrintSettings(
  287. }
  288. }
  289. -#if BUILDFLAG(IS_WIN)
  290. + std::unique_ptr<PrinterQuery> query =
  291. + queue_->CreatePrinterQuery(GetCurrentTargetFrame()->GetGlobalId());
  292. + auto* query_ptr = query.get();
  293. + // We need to clone this before calling SetSettings because some environments
  294. + // evaluate job_settings.Clone() first, and some std::move(job_settings) first,
  295. + // for the former things work correctly but for the latter the cloned value is null.
  296. + auto job_settings_copy = job_settings.Clone();
  297. + query_ptr->SetSettings(
  298. + std::move(job_settings_copy),
  299. + base::BindOnce(&PrintViewManagerBase::CompleteUpdatePrintSettings,
  300. + weak_ptr_factory_.GetWeakPtr(), std::move(query),
  301. + std::move(job_settings), std::move(print_settings),
  302. + std::move(callback)));
  303. +
  304. +#if 0 // See https://chromium-review.googlesource.com/412367
  305. // TODO(crbug.com/1424368): Remove this if the printable areas can be made
  306. // fully available from `PrintBackend::GetPrinterSemanticCapsAndDefaults()`
  307. // for in-browser queries.
  308. @@ -764,8 +825,6 @@ void PrintViewManagerBase::UpdatePrintSettings(
  309. }
  310. #endif
  311. - CompleteUpdatePrintSettings(std::move(job_settings),
  312. - std::move(print_settings), std::move(callback));
  313. }
  314. void PrintViewManagerBase::SetAccessibilityTree(
  315. @@ -781,7 +840,7 @@ void PrintViewManagerBase::SetAccessibilityTree(
  316. void PrintViewManagerBase::IsPrintingEnabled(
  317. IsPrintingEnabledCallback callback) {
  318. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  319. - std::move(callback).Run(printing_enabled_.GetValue());
  320. + std::move(callback).Run(true);
  321. }
  322. void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
  323. @@ -797,14 +856,14 @@ void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
  324. // didn't happen for some reason.
  325. bad_message::ReceivedBadMessage(
  326. render_process_host, bad_message::PVMB_SCRIPTED_PRINT_FENCED_FRAME);
  327. - std::move(callback).Run(nullptr);
  328. + std::move(callback).Run(nullptr, false);
  329. return;
  330. }
  331. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  332. if (printing::features::kEnableOopPrintDriversJobPrint.Get() &&
  333. !query_with_ui_client_id_.has_value()) {
  334. // Renderer process has requested settings outside of the expected setup.
  335. - std::move(callback).Run(nullptr);
  336. + std::move(callback).Run(nullptr, false);
  337. return;
  338. }
  339. #endif
  340. @@ -850,6 +909,7 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie,
  341. PrintManager::PrintingFailed(cookie, reason);
  342. +#if 0 // Electron does not use Chromium error dialogs
  343. // `PrintingFailed()` can occur because asynchronous compositing results
  344. // don't complete until after a print job has already failed and been
  345. // destroyed. In such cases the error notification to the user will
  346. @@ -859,7 +919,7 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie,
  347. print_job_->document()->cookie() == cookie) {
  348. ShowPrintErrorDialogForGenericError();
  349. }
  350. -
  351. +#endif
  352. ReleasePrinterQuery();
  353. }
  354. @@ -871,15 +931,24 @@ void PrintViewManagerBase::RemoveTestObserver(TestObserver& observer) {
  355. test_observers_.RemoveObserver(&observer);
  356. }
  357. +void PrintViewManagerBase::ShowInvalidPrinterSettingsError() {
  358. + if (!callback_.is_null()) {
  359. + printing_status_ = PrintStatus::kInvalid;
  360. + TerminatePrintJob(true);
  361. + }
  362. +}
  363. +
  364. void PrintViewManagerBase::RenderFrameHostStateChanged(
  365. content::RenderFrameHost* render_frame_host,
  366. content::RenderFrameHost::LifecycleState /*old_state*/,
  367. content::RenderFrameHost::LifecycleState new_state) {
  368. +#if 0
  369. if (new_state == content::RenderFrameHost::LifecycleState::kActive &&
  370. render_frame_host->GetProcess()->IsPdf() &&
  371. !render_frame_host->GetMainFrame()->GetParentOrOuterDocument()) {
  372. GetPrintRenderFrame(render_frame_host)->ConnectToPdfRenderer();
  373. }
  374. +#endif
  375. }
  376. void PrintViewManagerBase::RenderFrameDeleted(
  377. @@ -931,7 +1000,12 @@ void PrintViewManagerBase::OnJobDone() {
  378. // Printing is done, we don't need it anymore.
  379. // print_job_->is_job_pending() may still be true, depending on the order
  380. // of object registration.
  381. - printing_succeeded_ = true;
  382. + printing_status_ = PrintStatus::kSucceeded;
  383. + ReleasePrintJob();
  384. +}
  385. +
  386. +void PrintViewManagerBase::UserInitCanceled() {
  387. + printing_status_ = PrintStatus::kCanceled;
  388. ReleasePrintJob();
  389. }
  390. @@ -940,9 +1014,10 @@ void PrintViewManagerBase::OnCanceling() {
  391. }
  392. void PrintViewManagerBase::OnFailed() {
  393. +#if 0 // Electron does not use Chromium error dialogs
  394. if (!canceling_job_)
  395. ShowPrintErrorDialogForGenericError();
  396. -
  397. +#endif
  398. TerminatePrintJob(true);
  399. }
  400. @@ -952,7 +1027,7 @@ bool PrintViewManagerBase::RenderAllMissingPagesNow() {
  401. // Is the document already complete?
  402. if (print_job_->document() && print_job_->document()->IsComplete()) {
  403. - printing_succeeded_ = true;
  404. + printing_status_ = PrintStatus::kSucceeded;
  405. return true;
  406. }
  407. @@ -1000,7 +1075,10 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  408. // Disconnect the current `print_job_`.
  409. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  410. - DisconnectFromCurrentPrintJob();
  411. + if (callback_.is_null()) {
  412. + // Disconnect the current |print_job_| only when calling window.print()
  413. + DisconnectFromCurrentPrintJob();
  414. + }
  415. if (!weak_this)
  416. return false;
  417. @@ -1021,7 +1099,7 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  418. #endif
  419. print_job_->AddObserver(*this);
  420. - printing_succeeded_ = false;
  421. + printing_status_ = PrintStatus::kFailed;
  422. return true;
  423. }
  424. @@ -1089,6 +1167,11 @@ void PrintViewManagerBase::ReleasePrintJob() {
  425. }
  426. #endif
  427. + if (!callback_.is_null()) {
  428. + bool success = printing_status_ == PrintStatus::kSucceeded;
  429. + std::move(callback_).Run(success, PrintReasonFromPrintStatus(printing_status_));
  430. + }
  431. +
  432. if (!print_job_)
  433. return;
  434. @@ -1096,7 +1179,7 @@ void PrintViewManagerBase::ReleasePrintJob() {
  435. // printing_rfh_ should only ever point to a RenderFrameHost with a live
  436. // RenderFrame.
  437. DCHECK(rfh->IsRenderFrameLive());
  438. - GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  439. + GetPrintRenderFrame(rfh)->PrintingDone(printing_status_ == PrintStatus::kSucceeded);
  440. }
  441. print_job_->RemoveObserver(*this);
  442. @@ -1138,7 +1221,7 @@ bool PrintViewManagerBase::RunInnerMessageLoop() {
  443. }
  444. bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  445. - if (print_job_)
  446. + if (print_job_ && print_job_->document())
  447. return true;
  448. if (!cookie) {
  449. @@ -1284,7 +1367,7 @@ void PrintViewManagerBase::ReleasePrinterQuery() {
  450. }
  451. void PrintViewManagerBase::CompletePrintNow(content::RenderFrameHost* rfh) {
  452. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  453. + GetPrintRenderFrame(rfh)->PrintRequestedPages(/*silent=*/true, /*job_settings=*/base::Value::Dict());
  454. for (auto& observer : GetTestObservers()) {
  455. observer.OnPrintNow(rfh);
  456. @@ -1334,7 +1417,7 @@ void PrintViewManagerBase::CompleteScriptedPrintAfterContentAnalysis(
  457. set_analyzing_content(/*analyzing=*/false);
  458. if (!allowed || !printing_rfh_ || IsCrashed() ||
  459. !printing_rfh_->IsRenderFrameLive()) {
  460. - std::move(callback).Run(nullptr);
  461. + std::move(callback).Run(nullptr, false);
  462. return;
  463. }
  464. CompleteScriptedPrint(printing_rfh_, std::move(params), std::move(callback));
  465. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  466. index eab82d64b186ca820d7c28a1a1060e95fc67fff8..1292c0050f67c09be87f8fd06026412c816b505d 100644
  467. --- a/chrome/browser/printing/print_view_manager_base.h
  468. +++ b/chrome/browser/printing/print_view_manager_base.h
  469. @@ -44,6 +44,8 @@ namespace printing {
  470. class PrintQueriesQueue;
  471. class PrinterQuery;
  472. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  473. +
  474. // Base class for managing the print commands for a WebContents.
  475. class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  476. public:
  477. @@ -77,7 +79,10 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  478. // Prints the current document immediately. Since the rendering is
  479. // asynchronous, the actual printing will not be completed on the return of
  480. // this function. Returns false if printing is impossible at the moment.
  481. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  482. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  483. + bool silent = true,
  484. + base::Value::Dict settings = {},
  485. + CompletionCallback callback = {});
  486. // Like PrintNow(), but for the node under the context menu, instead of the
  487. // entire frame.
  488. @@ -131,8 +136,10 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  489. void IsPrintingEnabled(IsPrintingEnabledCallback callback) override;
  490. void ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
  491. ScriptedPrintCallback callback) override;
  492. + void ShowInvalidPrinterSettingsError() override;
  493. void PrintingFailed(int32_t cookie,
  494. mojom::PrintFailureReason reason) override;
  495. + void UserInitCanceled();
  496. // Adds and removes observers for `PrintViewManagerBase` events. The order in
  497. // which notifications are sent to observers is undefined. Observers must be
  498. @@ -140,6 +147,14 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  499. void AddTestObserver(TestObserver& observer);
  500. void RemoveTestObserver(TestObserver& observer);
  501. + enum class PrintStatus {
  502. + kSucceeded,
  503. + kCanceled,
  504. + kFailed,
  505. + kInvalid,
  506. + kUnknown
  507. + };
  508. +
  509. protected:
  510. explicit PrintViewManagerBase(content::WebContents* web_contents);
  511. @@ -277,6 +292,7 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  512. bool success);
  513. #endif
  514. void CompleteUpdatePrintSettings(
  515. + std::unique_ptr<PrinterQuery> printer_query,
  516. base::Value::Dict job_settings,
  517. std::unique_ptr<PrintSettings> print_settings,
  518. UpdatePrintSettingsCallback callback);
  519. @@ -312,7 +328,8 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  520. // Runs `callback` with `params` to reply to ScriptedPrint().
  521. void ScriptedPrintReply(ScriptedPrintCallback callback,
  522. int process_id,
  523. - mojom::PrintPagesParamsPtr params);
  524. + mojom::PrintPagesParamsPtr params,
  525. + bool canceled);
  526. // Requests the RenderView to render all the missing pages for the print job.
  527. // No-op if no print job is pending. Returns true if at least one page has
  528. @@ -393,8 +410,11 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  529. // The current RFH that is printing with a system printing dialog.
  530. raw_ptr<content::RenderFrameHost> printing_rfh_ = nullptr;
  531. + // Respond with success of the print job.
  532. + CompletionCallback callback_;
  533. +
  534. // Indication of success of the print job.
  535. - bool printing_succeeded_ = false;
  536. + PrintStatus printing_status_ = PrintStatus::kUnknown;
  537. // Indication that the job is getting canceled.
  538. bool canceling_job_ = false;
  539. diff --git a/chrome/browser/printing/printer_query.cc b/chrome/browser/printing/printer_query.cc
  540. index 83bce57ef52afa2094e294ae6bbe35e5e9e7797c..a32fc474b7db1bfa07443900eac49184036136da 100644
  541. --- a/chrome/browser/printing/printer_query.cc
  542. +++ b/chrome/browser/printing/printer_query.cc
  543. @@ -355,17 +355,19 @@ void PrinterQuery::UpdatePrintSettings(base::Value::Dict new_settings,
  544. #endif // BUILDFLAG(IS_LINUX) && BUILDFLAG(USE_CUPS)
  545. }
  546. - mojom::ResultCode result;
  547. {
  548. #if BUILDFLAG(IS_WIN)
  549. // Blocking is needed here because Windows printer drivers are oftentimes
  550. // not thread-safe and have to be accessed on the UI thread.
  551. base::ScopedAllowBlocking allow_blocking;
  552. #endif
  553. - result = printing_context_->UpdatePrintSettings(std::move(new_settings));
  554. + // Reset settings from previous print job
  555. + printing_context_->ResetSettings();
  556. + mojom::ResultCode result_code = printing_context_->UseDefaultSettings();
  557. + if (result_code == mojom::ResultCode::kSuccess)
  558. + result_code = printing_context_->UpdatePrintSettings(std::move(new_settings));
  559. + InvokeSettingsCallback(std::move(callback), result_code);
  560. }
  561. -
  562. - InvokeSettingsCallback(std::move(callback), result);
  563. }
  564. #if BUILDFLAG(IS_CHROMEOS)
  565. diff --git a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  566. index e83cf407beebcec5ccf7eaa991f43d4d3713833b..5e770a6a840b48e07ff056fe038aad54e526429e 100644
  567. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  568. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  569. @@ -21,7 +21,7 @@ FakePrintRenderFrame::FakePrintRenderFrame(
  570. FakePrintRenderFrame::~FakePrintRenderFrame() = default;
  571. -void FakePrintRenderFrame::PrintRequestedPages() {}
  572. +void FakePrintRenderFrame::PrintRequestedPages(bool /*silent*/, ::base::Value::Dict /*settings*/) {}
  573. void FakePrintRenderFrame::PrintWithParams(mojom::PrintPagesParamsPtr params,
  574. PrintWithParamsCallback callback) {
  575. diff --git a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  576. index 32403bb077dcbbffe6a3a862feff619e980c5f93..af773c93ab969a5dc483cc63384851ff62cf51ec 100644
  577. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  578. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  579. @@ -25,7 +25,7 @@ class FakePrintRenderFrame : public mojom::PrintRenderFrame {
  580. private:
  581. // printing::mojom::PrintRenderFrame:
  582. - void PrintRequestedPages() override;
  583. + void PrintRequestedPages(bool silent, ::base::Value::Dict settings) override;
  584. void PrintWithParams(mojom::PrintPagesParamsPtr params,
  585. PrintWithParamsCallback callback) override;
  586. void PrintForSystemDialog() override;
  587. diff --git a/components/printing/browser/print_manager.cc b/components/printing/browser/print_manager.cc
  588. index 21c81377d32ae8d4185598a7eba88ed1d2063ef0..0767f4e9369e926b1cea99178c1a1975941f1765 100644
  589. --- a/components/printing/browser/print_manager.cc
  590. +++ b/components/printing/browser/print_manager.cc
  591. @@ -47,6 +47,8 @@ void PrintManager::IsPrintingEnabled(IsPrintingEnabledCallback callback) {
  592. std::move(callback).Run(true);
  593. }
  594. +void PrintManager::ShowInvalidPrinterSettingsError() {}
  595. +
  596. void PrintManager::PrintingFailed(int32_t cookie,
  597. mojom::PrintFailureReason reason) {
  598. // Note: Not redundant with cookie checks in the same method in other parts of
  599. diff --git a/components/printing/browser/print_manager.h b/components/printing/browser/print_manager.h
  600. index ca71560874a0189068dd11fbc039f5673bf6bd96..a8551d95e64da2afbc1685b2df8f1fc377c7117b 100644
  601. --- a/components/printing/browser/print_manager.h
  602. +++ b/components/printing/browser/print_manager.h
  603. @@ -48,6 +48,7 @@ class PrintManager : public content::WebContentsObserver,
  604. DidPrintDocumentCallback callback) override;
  605. void IsPrintingEnabled(IsPrintingEnabledCallback callback) override;
  606. void DidShowPrintDialog() override;
  607. + void ShowInvalidPrinterSettingsError() override;
  608. void PrintingFailed(int32_t cookie,
  609. mojom::PrintFailureReason reason) override;
  610. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  611. index 0f535d5dd1db2e538382a91a0cf06f37d6b4961f..8156c74ad9ffeabfc0d87570a2d2ae1304f1ffa7 100644
  612. --- a/components/printing/common/print.mojom
  613. +++ b/components/printing/common/print.mojom
  614. @@ -302,7 +302,7 @@ union PrintWithParamsResult {
  615. interface PrintRenderFrame {
  616. // Tells the RenderFrame to switch the CSS to print media type, render every
  617. // requested page, and then switch back the CSS to display media type.
  618. - PrintRequestedPages();
  619. + PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);
  620. // Requests the frame to be printed with specified parameters. This is used
  621. // to programmatically produce PDF by request from the browser (e.g. over
  622. @@ -396,7 +396,10 @@ interface PrintManagerHost {
  623. // UI to the user to select the final print settings. If the user cancels or
  624. // an error occurs, return null.
  625. [Sync]
  626. - ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams? settings);
  627. + ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams? settings, bool canceled);
  628. +
  629. + // Tells the browser that there are invalid printer settings.
  630. + ShowInvalidPrinterSettingsError();
  631. // Tells the browser printing failed.
  632. PrintingFailed(int32 cookie, PrintFailureReason reason);
  633. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  634. index 33835f1bd82a2aa2d1efa2574ec52e182d479421..546c2e189b1a304a32ee9b10ef8d515e7820f1de 100644
  635. --- a/components/printing/renderer/print_render_frame_helper.cc
  636. +++ b/components/printing/renderer/print_render_frame_helper.cc
  637. @@ -46,6 +46,7 @@
  638. #include "printing/mojom/print.mojom.h"
  639. #include "printing/page_number.h"
  640. #include "printing/print_job_constants.h"
  641. +#include "printing/print_settings.h"
  642. #include "printing/units.h"
  643. #include "services/metrics/public/cpp/ukm_source_id.h"
  644. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  645. @@ -1351,7 +1352,6 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  646. }
  647. print_in_progress_ = true;
  648. -
  649. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  650. RecordBeforeAfterPrintEventForDebugging(__LINE__);
  651. web_frame->DispatchBeforePrintEvent(/*print_client=*/nullptr);
  652. @@ -1359,7 +1359,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  653. return;
  654. }
  655. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  656. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  657. + false /* silent */, base::Value::Dict() /* new_settings */);
  658. if (!weak_this) {
  659. return;
  660. }
  661. @@ -1391,7 +1392,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  662. receivers_.Add(this, std::move(receiver));
  663. }
  664. -void PrintRenderFrameHelper::PrintRequestedPages() {
  665. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value::Dict settings) {
  666. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  667. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  668. return;
  669. @@ -1407,7 +1408,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  670. // plugin node and print that instead.
  671. auto plugin = delegate_->GetPdfElement(frame);
  672. - Print(frame, plugin, PrintRequestType::kRegular);
  673. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  674. if (!render_frame_gone_) {
  675. RecordBeforeAfterPrintEventForDebugging(__LINE__);
  676. @@ -1491,7 +1492,8 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  677. }
  678. Print(frame, print_preview_context_.source_node(),
  679. - PrintRequestType::kRegular);
  680. + PrintRequestType::kRegular, false,
  681. + base::Value::Dict());
  682. if (render_frame_gone_) {
  683. return;
  684. }
  685. @@ -1555,6 +1557,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value::Dict settings) {
  686. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  687. return;
  688. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  689. + print_preview_context_.InitWithFrame(frame);
  690. print_preview_context_.OnPrintPreview();
  691. #if BUILDFLAG(IS_CHROMEOS_ASH)
  692. @@ -2189,7 +2193,8 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  693. }
  694. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  695. - PrintRequestType::kRegular);
  696. + PrintRequestType::kRegular, false /* silent */,
  697. + base::Value::Dict() /* new_settings */);
  698. // Check if |this| is still valid.
  699. if (!weak_this) {
  700. return;
  701. @@ -2206,7 +2211,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  702. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  703. const blink::WebNode& node,
  704. - PrintRequestType print_request_type) {
  705. + PrintRequestType print_request_type,
  706. + bool silent,
  707. + base::Value::Dict settings) {
  708. // If still not finished with earlier print request simply ignore.
  709. if (prep_frame_view_)
  710. return;
  711. @@ -2214,7 +2221,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  712. FrameReference frame_ref(frame);
  713. uint32_t expected_page_count = 0;
  714. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  715. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, std::move(settings))) {
  716. DidFinishPrinting(PrintingResult::kFailPrintInit);
  717. return; // Failed to init print page settings.
  718. }
  719. @@ -2233,8 +2240,15 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  720. print_pages_params_->params->print_scaling_option;
  721. auto self = weak_ptr_factory_.GetWeakPtr();
  722. - mojom::PrintPagesParamsPtr print_settings = GetPrintSettingsFromUser(
  723. + mojom::PrintPagesParamsPtr print_settings;
  724. +
  725. + if (silent) {
  726. + print_settings = mojom::PrintPagesParams::New();
  727. + print_settings->params = print_pages_params_->params->Clone();
  728. + } else {
  729. + print_settings = GetPrintSettingsFromUser(
  730. frame_ref.GetFrame(), node, expected_page_count, print_request_type);
  731. + }
  732. // Check if |this| is still valid.
  733. if (!self)
  734. return;
  735. @@ -2478,35 +2492,47 @@ void PrintRenderFrameHelper::IPCProcessed() {
  736. }
  737. }
  738. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  739. +bool PrintRenderFrameHelper::InitPrintSettings(
  740. + bool fit_to_paper_size,
  741. + base::Value::Dict new_settings) {
  742. // Reset to default values.
  743. ignore_css_margins_ = false;
  744. - mojom::PrintPagesParams settings;
  745. - GetPrintManagerHost()->GetDefaultPrintSettings(&settings.params);
  746. + mojom::PrintPagesParamsPtr settings;
  747. + if (new_settings.empty()) {
  748. + settings = mojom::PrintPagesParams::New();
  749. + settings->params = mojom::PrintParams::New();
  750. + GetPrintManagerHost()->GetDefaultPrintSettings(&settings->params);
  751. + } else {
  752. + GetPrintManagerHost()->UpdatePrintSettings(
  753. + std::move(new_settings), &settings);
  754. + }
  755. // Check if the printer returned any settings, if the settings are null,
  756. // assume there are no printer drivers configured. So safely terminate.
  757. - if (!settings.params) {
  758. + if (!settings || !settings->params) {
  759. // Caller will reset `print_pages_params_`.
  760. return false;
  761. }
  762. - settings.params->print_scaling_option =
  763. + settings->params->print_scaling_option =
  764. fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea
  765. : mojom::PrintScalingOption::kSourceSize;
  766. - SetPrintPagesParams(settings);
  767. + SetPrintPagesParams(*settings);
  768. return true;
  769. }
  770. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  771. - const blink::WebNode& node,
  772. - uint32_t* number_of_pages) {
  773. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  774. + blink::WebLocalFrame* frame,
  775. + const blink::WebNode& node,
  776. + uint32_t* number_of_pages,
  777. + base::Value::Dict settings) {
  778. DCHECK(frame);
  779. bool fit_to_paper_size = !IsPrintingPdfFrame(frame, node);
  780. - if (!InitPrintSettings(fit_to_paper_size)) {
  781. + if (!InitPrintSettings(fit_to_paper_size, std::move(settings))) {
  782. // Browser triggered this code path. It already knows about the failure.
  783. notify_browser_of_print_failure_ = false;
  784. + GetPrintManagerHost()->ShowInvalidPrinterSettingsError();
  785. return false;
  786. }
  787. @@ -2611,7 +2637,7 @@ mojom::PrintPagesParamsPtr PrintRenderFrameHelper::GetPrintSettingsFromUser(
  788. std::move(params),
  789. base::BindOnce(
  790. [](base::OnceClosure quit_closure, mojom::PrintPagesParamsPtr* output,
  791. - mojom::PrintPagesParamsPtr input) {
  792. + mojom::PrintPagesParamsPtr input, bool canceled) {
  793. *output = std::move(input);
  794. std::move(quit_closure).Run();
  795. },
  796. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  797. index 731b0fa4c0e1b3a5b4ce73d5bf2322767c70f9be..58921e7fdb9c33e21694186f436f409f74c81c3a 100644
  798. --- a/components/printing/renderer/print_render_frame_helper.h
  799. +++ b/components/printing/renderer/print_render_frame_helper.h
  800. @@ -247,7 +247,7 @@ class PrintRenderFrameHelper
  801. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  802. // printing::mojom::PrintRenderFrame:
  803. - void PrintRequestedPages() override;
  804. + void PrintRequestedPages(bool silent, base::Value::Dict settings) override;
  805. void PrintWithParams(mojom::PrintPagesParamsPtr params,
  806. PrintWithParamsCallback callback) override;
  807. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  808. @@ -317,7 +317,9 @@ class PrintRenderFrameHelper
  809. // WARNING: |this| may be gone after this method returns.
  810. void Print(blink::WebLocalFrame* frame,
  811. const blink::WebNode& node,
  812. - PrintRequestType print_request_type);
  813. + PrintRequestType print_request_type,
  814. + bool silent,
  815. + base::Value::Dict settings);
  816. // Notification when printing is done - signal tear-down/free resources.
  817. void DidFinishPrinting(PrintingResult result);
  818. @@ -326,12 +328,14 @@ class PrintRenderFrameHelper
  819. // Initialize print page settings with default settings.
  820. // Used only for native printing workflow.
  821. - bool InitPrintSettings(bool fit_to_paper_size);
  822. + bool InitPrintSettings(bool fit_to_paper_size,
  823. + base::Value::Dict new_settings);
  824. // Calculate number of pages in source document.
  825. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  826. const blink::WebNode& node,
  827. - uint32_t* number_of_pages);
  828. + uint32_t* number_of_pages,
  829. + base::Value::Dict settings);
  830. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  831. // Set options for print preset from source PDF document.
  832. diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
  833. index 7ed78907d28c0b79914eae27dac3b3b5184f1540..224a6884ef5070efe1a51fdf1c24662388288432 100644
  834. --- a/content/browser/BUILD.gn
  835. +++ b/content/browser/BUILD.gn
  836. @@ -2916,8 +2916,9 @@ source_set("browser") {
  837. "//ppapi/shared_impl",
  838. ]
  839. - assert(enable_printing)
  840. - deps += [ "//printing" ]
  841. + if (enable_printing) {
  842. + deps += [ "//printing" ]
  843. + }
  844. if (is_chromeos) {
  845. sources += [
  846. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  847. index 370dedfb4b4649f85a02b3a50ee16f525f57f44a..256666b64d8ffb5b50c9424c40ae1bb4050da6fb 100644
  848. --- a/printing/printing_context.cc
  849. +++ b/printing/printing_context.cc
  850. @@ -143,7 +143,6 @@ void PrintingContext::UsePdfSettings() {
  851. mojom::ResultCode PrintingContext::UpdatePrintSettings(
  852. base::Value::Dict job_settings) {
  853. - ResetSettings();
  854. {
  855. std::unique_ptr<PrintSettings> settings =
  856. PrintSettingsFromJobSettings(job_settings);
  857. diff --git a/printing/printing_context.h b/printing/printing_context.h
  858. index 2fb3aef0797f204f08c20e48987cd8c2703185de..75a70e331f8b539027748dad3252912cb85e8797 100644
  859. --- a/printing/printing_context.h
  860. +++ b/printing/printing_context.h
  861. @@ -178,6 +178,9 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  862. bool PrintingAborted() const { return abort_printing_; }
  863. + // Reinitializes the settings for object reuse.
  864. + void ResetSettings();
  865. +
  866. int job_id() const { return job_id_; }
  867. protected:
  868. @@ -188,9 +191,6 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  869. static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
  870. bool skip_system_calls);
  871. - // Reinitializes the settings for object reuse.
  872. - void ResetSettings();
  873. -
  874. // Determine if system calls should be skipped by this instance.
  875. bool skip_system_calls() const {
  876. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  877. diff --git a/sandbox/policy/mac/sandbox_mac.mm b/sandbox/policy/mac/sandbox_mac.mm
  878. index 10096f5a3824c71e6cb13b86ef7bdd02488f76c8..f7de11c89efed78f0280ada29f55f3bbd119ee88 100644
  879. --- a/sandbox/policy/mac/sandbox_mac.mm
  880. +++ b/sandbox/policy/mac/sandbox_mac.mm
  881. @@ -37,6 +37,10 @@
  882. #include "sandbox/policy/mac/utility.sb.h"
  883. #include "sandbox/policy/mojom/sandbox.mojom.h"
  884. +#if BUILDFLAG(ENABLE_PRINTING)
  885. +#include "sandbox/policy/mac/print_backend.sb.h"
  886. +#endif
  887. +
  888. namespace sandbox::policy {
  889. base::FilePath GetCanonicalPath(const base::FilePath& path) {