printing.patch 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 e8c8a8450ff8325905a76addc262a6fd68f037be..e703690f0f0952d7f97cde04e7c2c786ab1747ec 100644
  12. --- a/BUILD.gn
  13. +++ b/BUILD.gn
  14. @@ -973,7 +973,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. @@ -982,6 +981,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 819ea8a1902bd4cbf52f6e52622203b34a096aae..c7111b8814cc9260de2201febdcaeb2f588e5d6a 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. @@ -387,8 +387,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. @@ -480,8 +482,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 666ee7afbd6425bab3da68d2367db99121268758..4553693807136a7bc580e35b85c5445468cbda20 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. @@ -189,9 +210,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. @@ -214,12 +237,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. @@ -343,12 +374,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. @@ -356,7 +388,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. @@ -368,6 +401,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. @@ -503,7 +540,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. @@ -518,12 +556,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. @@ -651,10 +692,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. @@ -706,10 +749,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. @@ -720,6 +765,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. @@ -739,6 +785,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. @@ -758,7 +805,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. @@ -780,8 +841,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. @@ -797,7 +856,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. @@ -813,14 +872,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. @@ -862,6 +921,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. @@ -871,7 +931,7 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie,
  347. print_job_->document()->cookie() == cookie) {
  348. ShowPrintErrorDialogForGenericError();
  349. }
  350. -
  351. +#endif
  352. ReleasePrinterQuery();
  353. }
  354. @@ -883,15 +943,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. @@ -943,7 +1012,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. @@ -952,9 +1026,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. @@ -964,7 +1039,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. @@ -1017,7 +1092,10 @@ bool PrintViewManagerBase::SetupNewPrintJob(
  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. @@ -1037,7 +1115,7 @@ bool PrintViewManagerBase::SetupNewPrintJob(
  418. #endif
  419. print_job_->AddObserver(*this);
  420. - printing_succeeded_ = false;
  421. + printing_status_ = PrintStatus::kFailed;
  422. return true;
  423. }
  424. @@ -1105,6 +1183,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. @@ -1112,7 +1195,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. @@ -1154,7 +1237,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. @@ -1300,7 +1383,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. @@ -1350,7 +1433,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 ae2339d1ab82bd7cfa971ed03a188cfc26d6e1c0..186414ee72c6b431b87b02623abd4d1903de812b 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. @@ -281,6 +296,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. @@ -310,7 +326,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. @@ -391,8 +408,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 3f9a514fb41d72c5d06de6ac989f9d7c0513a4e7..0e7ada9df962808dad7caf074a08ebdedbd45604 100644
  612. --- a/components/printing/common/print.mojom
  613. +++ b/components/printing/common/print.mojom
  614. @@ -300,7 +300,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. @@ -394,7 +394,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 5a58210461222ed431624bf161b32770c5ae97e6..aec69c525cefd73e50f50883ac0dc8bee94e78c0 100644
  635. --- a/components/printing/renderer/print_render_frame_helper.cc
  636. +++ b/components/printing/renderer/print_render_frame_helper.cc
  637. @@ -45,6 +45,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. @@ -1237,14 +1238,14 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  646. }
  647. print_in_progress_ = true;
  648. -
  649. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  650. web_frame->DispatchBeforePrintEvent(/*print_client=*/nullptr);
  651. if (!weak_this) {
  652. return;
  653. }
  654. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  655. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  656. + false /* silent */, base::Value::Dict() /* new_settings */);
  657. if (!weak_this) {
  658. return;
  659. }
  660. @@ -1275,7 +1276,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  661. receivers_.Add(this, std::move(receiver));
  662. }
  663. -void PrintRenderFrameHelper::PrintRequestedPages() {
  664. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value::Dict settings) {
  665. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  666. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  667. return;
  668. @@ -1290,7 +1291,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  669. // plugin node and print that instead.
  670. auto plugin = delegate_->GetPdfElement(frame);
  671. - Print(frame, plugin, PrintRequestType::kRegular);
  672. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  673. if (render_frame_gone_) {
  674. return;
  675. @@ -1377,7 +1378,8 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  676. }
  677. Print(frame, print_preview_context_.source_node(),
  678. - PrintRequestType::kRegular);
  679. + PrintRequestType::kRegular, false,
  680. + base::Value::Dict());
  681. if (render_frame_gone_) {
  682. return;
  683. }
  684. @@ -1440,6 +1442,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value::Dict settings) {
  685. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  686. return;
  687. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  688. + print_preview_context_.InitWithFrame(frame);
  689. print_preview_context_.OnPrintPreview();
  690. #if BUILDFLAG(IS_CHROMEOS_ASH)
  691. @@ -2065,7 +2069,8 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  692. }
  693. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  694. - PrintRequestType::kRegular);
  695. + PrintRequestType::kRegular, false /* silent */,
  696. + base::Value::Dict() /* new_settings */);
  697. // Check if `this` is still valid.
  698. if (!weak_this) {
  699. return;
  700. @@ -2081,7 +2086,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  701. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  702. const blink::WebNode& node,
  703. - PrintRequestType print_request_type) {
  704. + PrintRequestType print_request_type,
  705. + bool silent,
  706. + base::Value::Dict settings) {
  707. // If still not finished with earlier print request simply ignore.
  708. if (prep_frame_view_)
  709. return;
  710. @@ -2089,7 +2096,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  711. FrameReference frame_ref(frame);
  712. uint32_t expected_page_count = 0;
  713. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  714. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, std::move(settings))) {
  715. DidFinishPrinting(PrintingResult::kFailPrintInit);
  716. return; // Failed to init print page settings.
  717. }
  718. @@ -2108,8 +2115,15 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  719. print_pages_params_->params->print_scaling_option;
  720. auto self = weak_ptr_factory_.GetWeakPtr();
  721. - mojom::PrintPagesParamsPtr print_settings = GetPrintSettingsFromUser(
  722. + mojom::PrintPagesParamsPtr print_settings;
  723. +
  724. + if (silent) {
  725. + print_settings = mojom::PrintPagesParams::New();
  726. + print_settings->params = print_pages_params_->params->Clone();
  727. + } else {
  728. + print_settings = GetPrintSettingsFromUser(
  729. frame_ref.GetFrame(), node, expected_page_count, print_request_type);
  730. + }
  731. // Check if `this` is still valid.
  732. if (!self)
  733. return;
  734. @@ -2349,35 +2363,47 @@ void PrintRenderFrameHelper::IPCProcessed() {
  735. }
  736. }
  737. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  738. +bool PrintRenderFrameHelper::InitPrintSettings(
  739. + bool fit_to_paper_size,
  740. + base::Value::Dict new_settings) {
  741. // Reset to default values.
  742. ignore_css_margins_ = false;
  743. - mojom::PrintPagesParams settings;
  744. - GetPrintManagerHost()->GetDefaultPrintSettings(&settings.params);
  745. + mojom::PrintPagesParamsPtr settings;
  746. + if (new_settings.empty()) {
  747. + settings = mojom::PrintPagesParams::New();
  748. + settings->params = mojom::PrintParams::New();
  749. + GetPrintManagerHost()->GetDefaultPrintSettings(&settings->params);
  750. + } else {
  751. + GetPrintManagerHost()->UpdatePrintSettings(
  752. + std::move(new_settings), &settings);
  753. + }
  754. // Check if the printer returned any settings, if the settings are null,
  755. // assume there are no printer drivers configured. So safely terminate.
  756. - if (!settings.params) {
  757. + if (!settings || !settings->params) {
  758. // Caller will reset `print_pages_params_`.
  759. return false;
  760. }
  761. - settings.params->print_scaling_option =
  762. + settings->params->print_scaling_option =
  763. fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea
  764. : mojom::PrintScalingOption::kSourceSize;
  765. - SetPrintPagesParams(settings);
  766. + SetPrintPagesParams(*settings);
  767. return true;
  768. }
  769. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  770. - const blink::WebNode& node,
  771. - uint32_t* number_of_pages) {
  772. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  773. + blink::WebLocalFrame* frame,
  774. + const blink::WebNode& node,
  775. + uint32_t* number_of_pages,
  776. + base::Value::Dict settings) {
  777. DCHECK(frame);
  778. bool fit_to_paper_size = !IsPrintingPdfFrame(frame, node);
  779. - if (!InitPrintSettings(fit_to_paper_size)) {
  780. + if (!InitPrintSettings(fit_to_paper_size, std::move(settings))) {
  781. // Browser triggered this code path. It already knows about the failure.
  782. notify_browser_of_print_failure_ = false;
  783. + GetPrintManagerHost()->ShowInvalidPrinterSettingsError();
  784. return false;
  785. }
  786. @@ -2482,7 +2508,7 @@ mojom::PrintPagesParamsPtr PrintRenderFrameHelper::GetPrintSettingsFromUser(
  787. std::move(params),
  788. base::BindOnce(
  789. [](base::OnceClosure quit_closure, mojom::PrintPagesParamsPtr* output,
  790. - mojom::PrintPagesParamsPtr input) {
  791. + mojom::PrintPagesParamsPtr input, bool canceled) {
  792. *output = std::move(input);
  793. std::move(quit_closure).Run();
  794. },
  795. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  796. index 5cbb2940f83af329ea38efca5bf3216056269654..8cf783d37589fdca88592eeb9b8cc91b6ae60203 100644
  797. --- a/components/printing/renderer/print_render_frame_helper.h
  798. +++ b/components/printing/renderer/print_render_frame_helper.h
  799. @@ -247,7 +247,7 @@ class PrintRenderFrameHelper
  800. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  801. // printing::mojom::PrintRenderFrame:
  802. - void PrintRequestedPages() override;
  803. + void PrintRequestedPages(bool silent, base::Value::Dict settings) override;
  804. void PrintWithParams(mojom::PrintPagesParamsPtr params,
  805. PrintWithParamsCallback callback) override;
  806. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  807. @@ -317,7 +317,9 @@ class PrintRenderFrameHelper
  808. // WARNING: |this| may be gone after this method returns.
  809. void Print(blink::WebLocalFrame* frame,
  810. const blink::WebNode& node,
  811. - PrintRequestType print_request_type);
  812. + PrintRequestType print_request_type,
  813. + bool silent,
  814. + base::Value::Dict settings);
  815. // Notification when printing is done - signal tear-down/free resources.
  816. void DidFinishPrinting(PrintingResult result);
  817. @@ -326,12 +328,14 @@ class PrintRenderFrameHelper
  818. // Initialize print page settings with default settings.
  819. // Used only for native printing workflow.
  820. - bool InitPrintSettings(bool fit_to_paper_size);
  821. + bool InitPrintSettings(bool fit_to_paper_size,
  822. + base::Value::Dict new_settings);
  823. // Calculate number of pages in source document.
  824. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  825. const blink::WebNode& node,
  826. - uint32_t* number_of_pages);
  827. + uint32_t* number_of_pages,
  828. + base::Value::Dict settings);
  829. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  830. // Set options for print preset from source PDF document.
  831. diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
  832. index 755bfcc4ed84f44d0d8bb53615637ccd69a858e7..368cf7aaffeef5f9796f78a72f13eddb6c44dec9 100644
  833. --- a/content/browser/BUILD.gn
  834. +++ b/content/browser/BUILD.gn
  835. @@ -2955,8 +2955,9 @@ source_set("browser") {
  836. "//ppapi/shared_impl",
  837. ]
  838. - assert(enable_printing)
  839. - deps += [ "//printing" ]
  840. + if (enable_printing) {
  841. + deps += [ "//printing" ]
  842. + }
  843. if (is_chromeos) {
  844. sources += [
  845. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  846. index 6aae8700c36346edfb44db0cec569efb3702cafe..75a5c89b8e0c916239b5f4fc1f062df4cd22733b 100644
  847. --- a/printing/printing_context.cc
  848. +++ b/printing/printing_context.cc
  849. @@ -143,7 +143,6 @@ void PrintingContext::UsePdfSettings() {
  850. mojom::ResultCode PrintingContext::UpdatePrintSettings(
  851. base::Value::Dict job_settings) {
  852. - ResetSettings();
  853. {
  854. std::unique_ptr<PrintSettings> settings =
  855. PrintSettingsFromJobSettings(job_settings);
  856. diff --git a/printing/printing_context.h b/printing/printing_context.h
  857. index c753f04d8b73a51cb7e05dcdb86afc0e981e7a71..7ec1729e08bf42a51f80885b3838eee95c8bc953 100644
  858. --- a/printing/printing_context.h
  859. +++ b/printing/printing_context.h
  860. @@ -174,6 +174,9 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  861. bool PrintingAborted() const { return abort_printing_; }
  862. + // Reinitializes the settings for object reuse.
  863. + void ResetSettings();
  864. +
  865. int job_id() const { return job_id_; }
  866. protected:
  867. @@ -184,9 +187,6 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  868. static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
  869. bool skip_system_calls);
  870. - // Reinitializes the settings for object reuse.
  871. - void ResetSettings();
  872. -
  873. // Determine if system calls should be skipped by this instance.
  874. bool skip_system_calls() const {
  875. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  876. diff --git a/sandbox/policy/mac/sandbox_mac.mm b/sandbox/policy/mac/sandbox_mac.mm
  877. index 10096f5a3824c71e6cb13b86ef7bdd02488f76c8..f7de11c89efed78f0280ada29f55f3bbd119ee88 100644
  878. --- a/sandbox/policy/mac/sandbox_mac.mm
  879. +++ b/sandbox/policy/mac/sandbox_mac.mm
  880. @@ -37,6 +37,10 @@
  881. #include "sandbox/policy/mac/utility.sb.h"
  882. #include "sandbox/policy/mojom/sandbox.mojom.h"
  883. +#if BUILDFLAG(ENABLE_PRINTING)
  884. +#include "sandbox/policy/mac/print_backend.sb.h"
  885. +#endif
  886. +
  887. namespace sandbox::policy {
  888. base::FilePath GetCanonicalPath(const base::FilePath& path) {