printing.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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/chrome/browser/printing/print_job.cc b/chrome/browser/printing/print_job.cc
  11. index 0cfd89165ad179230662a6fdcb9e1bd929f63188..dd573471a452ae2b10ed60a641bcc5d463804cba 100644
  12. --- a/chrome/browser/printing/print_job.cc
  13. +++ b/chrome/browser/printing/print_job.cc
  14. @@ -317,7 +317,12 @@ void PrintJob::StartPdfToEmfConversion(
  15. // seems to work with the fix for this bug applied.
  16. const PrintSettings& settings = document()->settings();
  17. bool print_text_with_gdi =
  18. - settings.print_text_with_gdi() && !settings.printer_is_xps() &&
  19. +#if defined(OS_WIN)
  20. + settings.is_modifiable()
  21. +#else
  22. + settings.print_text_with_gdi()
  23. +#endif
  24. + && !settings.printer_is_xps() &&
  25. base::FeatureList::IsEnabled(::features::kGdiTextPrinting);
  26. PdfRenderSettings render_settings(
  27. content_area, gfx::Point(0, 0), settings.dpi_size(),
  28. diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc
  29. index 33e17f0df3563726767d912fb828ab959c8ec252..780967949746cbe957cd7b3487507892b3df607c 100644
  30. --- a/chrome/browser/printing/print_job_worker.cc
  31. +++ b/chrome/browser/printing/print_job_worker.cc
  32. @@ -21,7 +21,6 @@
  33. #include "chrome/browser/browser_process.h"
  34. #include "chrome/browser/chrome_notification_types.h"
  35. #include "chrome/browser/printing/print_job.h"
  36. -#include "chrome/grit/generated_resources.h"
  37. #include "components/crash/core/common/crash_keys.h"
  38. #include "content/public/browser/browser_task_traits.h"
  39. #include "content/public/browser/browser_thread.h"
  40. @@ -29,6 +28,7 @@
  41. #include "content/public/browser/render_frame_host.h"
  42. #include "content/public/browser/web_contents.h"
  43. #include "printing/backend/print_backend.h"
  44. +#include "electron/grit/electron_resources.h"
  45. #include "printing/print_job_constants.h"
  46. #include "printing/printed_document.h"
  47. #include "printing/printing_utils.h"
  48. @@ -222,9 +222,14 @@ void PrintJobWorker::UpdatePrintSettings(base::Value new_settings,
  49. print_backend->GetPrinterDriverInfo(printer_name));
  50. }
  51. - PrintingContext::Result result =
  52. - printing_context_->UpdatePrintSettings(std::move(new_settings));
  53. - GetSettingsDone(std::move(callback), result);
  54. + // Reset settings from previous print job
  55. + printing_context_->ResetSettings();
  56. + PrintingContext::Result get_default_result = printing_context_->UseDefaultSettings();
  57. + if (get_default_result == PrintingContext::Result::OK) {
  58. + PrintingContext::Result update_result =
  59. + printing_context_->UpdatePrintSettings(std::move(new_settings));
  60. + GetSettingsDone(std::move(callback), update_result);
  61. + }
  62. }
  63. #if defined(OS_CHROMEOS)
  64. @@ -240,6 +245,13 @@ void PrintJobWorker::UpdatePrintSettingsFromPOD(
  65. void PrintJobWorker::GetSettingsDone(SettingsCallback callback,
  66. PrintingContext::Result result) {
  67. + if (result == PrintingContext::CANCEL) {
  68. + print_job_->PostTask(
  69. + FROM_HERE,
  70. + base::BindOnce(&NotificationCallback, base::RetainedRef(print_job_),
  71. + JobEventDetails::USER_INIT_CANCELED, 0,
  72. + base::RetainedRef(document_)));
  73. + }
  74. std::move(callback).Run(printing_context_->TakeAndResetSettings(), result);
  75. }
  76. diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
  77. index 3378f34bd8e3b1cf8156e86d0e9bea97120c101e..6c6d5c885f41412e25ddfc3b884b9c39fabc4f10 100644
  78. --- a/chrome/browser/printing/print_view_manager_base.cc
  79. +++ b/chrome/browser/printing/print_view_manager_base.cc
  80. @@ -27,10 +27,7 @@
  81. #include "chrome/browser/printing/print_view_manager_common.h"
  82. #include "chrome/browser/printing/printer_query.h"
  83. #include "chrome/browser/profiles/profile.h"
  84. -#include "chrome/browser/ui/simple_message_box.h"
  85. -#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
  86. #include "chrome/common/pref_names.h"
  87. -#include "chrome/grit/generated_resources.h"
  88. #include "components/prefs/pref_service.h"
  89. #include "components/printing/browser/print_composite_client.h"
  90. #include "components/printing/browser/print_manager_utils.h"
  91. @@ -45,6 +42,7 @@
  92. #include "content/public/browser/render_process_host.h"
  93. #include "content/public/browser/render_view_host.h"
  94. #include "content/public/browser/web_contents.h"
  95. +#include "electron/grit/electron_resources.h"
  96. #include "mojo/public/cpp/system/buffer.h"
  97. #include "printing/buildflags/buildflags.h"
  98. #include "printing/metafile_skia.h"
  99. @@ -68,6 +66,8 @@ using PrintSettingsCallback =
  100. base::OnceCallback<void(std::unique_ptr<PrinterQuery>)>;
  101. void ShowWarningMessageBox(const base::string16& message) {
  102. + LOG(ERROR) << "Invalid printer settings " << message;
  103. +#if 0
  104. // Runs always on the UI thread.
  105. static bool is_dialog_shown = false;
  106. if (is_dialog_shown)
  107. @@ -76,6 +76,7 @@ void ShowWarningMessageBox(const base::string16& message) {
  108. base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
  109. chrome::ShowWarningMessageBox(nullptr, base::string16(), message);
  110. +#endif
  111. }
  112. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  113. @@ -114,12 +115,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
  114. printing_succeeded_(false),
  115. queue_(g_browser_process->print_job_manager()->queue()) {
  116. DCHECK(queue_);
  117. +#if 0
  118. Profile* profile =
  119. Profile::FromBrowserContext(web_contents->GetBrowserContext());
  120. printing_enabled_.Init(
  121. prefs::kPrintingEnabled, profile->GetPrefs(),
  122. base::BindRepeating(&PrintViewManagerBase::UpdatePrintingEnabled,
  123. weak_ptr_factory_.GetWeakPtr()));
  124. +#endif
  125. }
  126. PrintViewManagerBase::~PrintViewManagerBase() {
  127. @@ -127,7 +130,10 @@ PrintViewManagerBase::~PrintViewManagerBase() {
  128. DisconnectFromCurrentPrintJob();
  129. }
  130. -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  131. +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
  132. + bool silent,
  133. + base::Value settings,
  134. + CompletionCallback callback) {
  135. DisconnectFromCurrentPrintJob();
  136. // Don't print / print preview interstitials or crashed tabs.
  137. @@ -135,7 +141,14 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  138. return false;
  139. SetPrintingRFH(rfh);
  140. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  141. + callback_ = std::move(callback);
  142. +
  143. + if (!callback_.is_null()) {
  144. + registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  145. + content::NotificationService::AllSources());
  146. + }
  147. +
  148. + GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
  149. return true;
  150. }
  151. @@ -256,9 +269,9 @@ void PrintViewManagerBase::StartLocalPrintJob(
  152. void PrintViewManagerBase::UpdatePrintingEnabled() {
  153. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  154. // The Unretained() is safe because ForEachFrame() is synchronous.
  155. - web_contents()->ForEachFrame(base::BindRepeating(
  156. - &PrintViewManagerBase::SendPrintingEnabled, base::Unretained(this),
  157. - printing_enabled_.GetValue()));
  158. + web_contents()->ForEachFrame(
  159. + base::BindRepeating(&PrintViewManagerBase::SendPrintingEnabled,
  160. + base::Unretained(this), true));
  161. }
  162. void PrintViewManagerBase::NavigationStopped() {
  163. @@ -361,7 +374,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) {
  164. PrintManager::OnPrintingFailed(cookie);
  165. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  166. - ShowPrintErrorDialog();
  167. + // ShowPrintErrorDialog();
  168. #endif
  169. ReleasePrinterQuery();
  170. @@ -380,6 +393,10 @@ void PrintViewManagerBase::OnScriptedPrint(
  171. }
  172. void PrintViewManagerBase::OnShowInvalidPrinterSettingsError() {
  173. + if (!callback_.is_null()) {
  174. + std::string cb_str = "Invalid printer settings";
  175. + std::move(callback_).Run(printing_succeeded_, cb_str);
  176. + }
  177. base::ThreadTaskRunnerHandle::Get()->PostTask(
  178. FROM_HERE, base::BindOnce(&ShowWarningMessageBox,
  179. l10n_util::GetStringUTF16(
  180. @@ -461,9 +478,13 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent(
  181. content::NotificationService::NoDetails());
  182. break;
  183. }
  184. - case JobEventDetails::USER_INIT_DONE:
  185. - case JobEventDetails::DEFAULT_INIT_DONE:
  186. case JobEventDetails::USER_INIT_CANCELED: {
  187. + printing_cancelled_ = true;
  188. + ReleasePrintJob();
  189. + break;
  190. + }
  191. + case JobEventDetails::USER_INIT_DONE:
  192. + case JobEventDetails::DEFAULT_INIT_DONE: {
  193. NOTREACHED();
  194. break;
  195. }
  196. @@ -558,8 +579,10 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  197. DCHECK(!quit_inner_loop_);
  198. DCHECK(query);
  199. - // Disconnect the current |print_job_|.
  200. - DisconnectFromCurrentPrintJob();
  201. + if (callback_.is_null()) {
  202. + // Disconnect the current |print_job_| only when calling window.print()
  203. + DisconnectFromCurrentPrintJob();
  204. + }
  205. // We can't print if there is no renderer.
  206. if (!web_contents()->GetRenderViewHost() ||
  207. @@ -574,8 +597,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  208. print_job_->SetSource(PrintJob::Source::PRINT_PREVIEW, /*source_id=*/"");
  209. #endif // defined(OS_CHROMEOS)
  210. - registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  211. - content::Source<PrintJob>(print_job_.get()));
  212. printing_succeeded_ = false;
  213. return true;
  214. }
  215. @@ -624,14 +645,22 @@ void PrintViewManagerBase::ReleasePrintJob() {
  216. content::RenderFrameHost* rfh = printing_rfh_;
  217. printing_rfh_ = nullptr;
  218. + if (!callback_.is_null()) {
  219. + std::string cb_str = "";
  220. + if (!printing_succeeded_)
  221. + cb_str = printing_cancelled_ ? "cancelled" : "failed";
  222. + std::move(callback_).Run(printing_succeeded_, cb_str);
  223. + }
  224. +
  225. if (!print_job_)
  226. return;
  227. if (rfh)
  228. GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  229. - registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  230. - content::Source<PrintJob>(print_job_.get()));
  231. + if (!callback_.is_null())
  232. + registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  233. + content::NotificationService::AllSources());
  234. // Don't close the worker thread.
  235. print_job_ = nullptr;
  236. }
  237. @@ -667,7 +696,7 @@ bool PrintViewManagerBase::RunInnerMessageLoop() {
  238. }
  239. bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  240. - if (print_job_)
  241. + if (print_job_ && print_job_->document())
  242. return true;
  243. if (!cookie) {
  244. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  245. index 3d434244927aca5e5dbec8d7c1cd212c5c260c71..9d62c903bf08fa8cfa8c425d430c5e995a04f7fc 100644
  246. --- a/chrome/browser/printing/print_view_manager_base.h
  247. +++ b/chrome/browser/printing/print_view_manager_base.h
  248. @@ -33,6 +33,8 @@ class PrintJob;
  249. class PrintQueriesQueue;
  250. class PrinterQuery;
  251. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  252. +
  253. // Base class for managing the print commands for a WebContents.
  254. class PrintViewManagerBase : public content::NotificationObserver,
  255. public PrintManager {
  256. @@ -42,7 +44,10 @@ class PrintViewManagerBase : public content::NotificationObserver,
  257. // Prints the current document immediately. Since the rendering is
  258. // asynchronous, the actual printing will not be completed on the return of
  259. // this function. Returns false if printing is impossible at the moment.
  260. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  261. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  262. + bool silent,
  263. + base::Value settings,
  264. + CompletionCallback callback);
  265. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  266. // Prints the document in |print_data| with settings specified in
  267. @@ -206,9 +211,15 @@ class PrintViewManagerBase : public content::NotificationObserver,
  268. // The current RFH that is printing with a system printing dialog.
  269. content::RenderFrameHost* printing_rfh_;
  270. + // Respond with success of the print job.
  271. + CompletionCallback callback_;
  272. +
  273. // Indication of success of the print job.
  274. bool printing_succeeded_;
  275. + // Indication of whether the print job was manually cancelled
  276. + bool printing_cancelled_ = false;
  277. +
  278. // Set while running an inner message loop inside RenderAllMissingPagesNow().
  279. // This means we are _blocking_ until all the necessary pages have been
  280. // rendered or the print settings are being loaded.
  281. diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc
  282. index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2eb48262ce 100644
  283. --- a/chrome/browser/printing/printing_message_filter.cc
  284. +++ b/chrome/browser/printing/printing_message_filter.cc
  285. @@ -22,6 +22,7 @@
  286. #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
  287. #include "components/printing/browser/print_manager_utils.h"
  288. #include "components/printing/common/print_messages.h"
  289. +#include "content/public/browser/browser_context.h"
  290. #include "content/public/browser/browser_task_traits.h"
  291. #include "content/public/browser/render_frame_host.h"
  292. #include "content/public/browser/web_contents.h"
  293. @@ -90,20 +91,23 @@ void PrintingMessageFilter::SetDelegateForTesting(TestDelegate* delegate) {
  294. g_test_delegate = delegate;
  295. }
  296. -PrintingMessageFilter::PrintingMessageFilter(int render_process_id,
  297. - Profile* profile)
  298. +PrintingMessageFilter::PrintingMessageFilter(
  299. + int render_process_id,
  300. + content::BrowserContext* browser_context)
  301. : BrowserMessageFilter(PrintMsgStart),
  302. render_process_id_(render_process_id),
  303. queue_(g_browser_process->print_job_manager()->queue()) {
  304. DCHECK(queue_.get());
  305. printing_shutdown_notifier_ =
  306. PrintingMessageFilterShutdownNotifierFactory::GetInstance()
  307. - ->Get(profile)
  308. + ->Get(browser_context)
  309. ->Subscribe(base::Bind(&PrintingMessageFilter::ShutdownOnUIThread,
  310. base::Unretained(this)));
  311. + #if 0
  312. is_printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
  313. is_printing_enabled_.MoveToSequence(
  314. base::CreateSingleThreadTaskRunner({BrowserThread::IO}));
  315. + #endif
  316. }
  317. PrintingMessageFilter::~PrintingMessageFilter() {
  318. @@ -138,11 +142,13 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
  319. void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
  320. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  321. +#if 0
  322. if (!is_printing_enabled_.GetValue()) {
  323. // Reply with NULL query.
  324. OnGetDefaultPrintSettingsReply(nullptr, reply_msg);
  325. return;
  326. }
  327. +#endif
  328. std::unique_ptr<PrinterQuery> printer_query = queue_->PopPrinterQuery(0);
  329. if (!printer_query) {
  330. printer_query =
  331. @@ -228,11 +234,13 @@ void PrintingMessageFilter::OnScriptedPrintReply(
  332. void PrintingMessageFilter::OnUpdatePrintSettings(int document_cookie,
  333. base::Value job_settings,
  334. IPC::Message* reply_msg) {
  335. +#if 0
  336. if (!is_printing_enabled_.GetValue()) {
  337. // Reply with NULL query.
  338. OnUpdatePrintSettingsReply(nullptr, reply_msg);
  339. return;
  340. }
  341. +#endif
  342. std::unique_ptr<PrinterQuery> printer_query =
  343. queue_->PopPrinterQuery(document_cookie);
  344. if (!printer_query) {
  345. @@ -258,7 +266,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
  346. std::unique_ptr<PrinterQuery> printer_query,
  347. IPC::Message* reply_msg) {
  348. PrintMsg_PrintPages_Params params;
  349. - if (!printer_query || printer_query->last_status() != PrintingContext::OK) {
  350. + // We call update without first printing from defaults,
  351. + // so the last printer status will still be defaulted to PrintingContext::FAILED
  352. + if (!printer_query) {
  353. params.Reset();
  354. } else {
  355. RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
  356. @@ -296,7 +306,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
  357. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  358. void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids,
  359. bool* cancel) {
  360. - *cancel = PrintPreviewUI::ShouldCancelRequest(ids);
  361. + *cancel = false;
  362. }
  363. #endif
  364. diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h
  365. index 9fbea6d0a2dbe55b1d600fbc217dee5aa8ae8cd5..de9bd267e408c02fd4da7d903523c0e6305088d5 100644
  366. --- a/chrome/browser/printing/printing_message_filter.h
  367. +++ b/chrome/browser/printing/printing_message_filter.h
  368. @@ -24,6 +24,10 @@ struct PrintHostMsg_ScriptedPrint_Params;
  369. struct PrintMsg_Print_Params;
  370. class Profile;
  371. +namespace content {
  372. +class BrowserContext;
  373. +}
  374. +
  375. namespace printing {
  376. class PrintQueriesQueue;
  377. @@ -44,7 +48,8 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
  378. static void SetDelegateForTesting(TestDelegate* delegate);
  379. - PrintingMessageFilter(int render_process_id, Profile* profile);
  380. + PrintingMessageFilter(int render_process_id,
  381. + content::BrowserContext* browser_context);
  382. // content::BrowserMessageFilter:
  383. bool OnMessageReceived(const IPC::Message& message) override;
  384. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  385. index 3695656560c54b5aa1fb08fb5e7c17d54989c597..85ffa5704d8dea809e80b1993c7c852fc545c7a6 100644
  386. --- a/components/printing/common/print.mojom
  387. +++ b/components/printing/common/print.mojom
  388. @@ -35,7 +35,7 @@ interface PrintRenderer {
  389. interface PrintRenderFrame {
  390. // Tells the RenderFrame to switch the CSS to print media type, render every
  391. // requested page, and then switch back the CSS to display media type.
  392. - PrintRequestedPages();
  393. + PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);
  394. // Tells the RenderFrame to switch the CSS to print media type, render every
  395. // requested page using the print preview document's frame/node, and then
  396. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  397. index 3eb237c833300fdf8707a610616989a805133be4..8350af471355b9c620d0c337545ce614974b5a6b 100644
  398. --- a/components/printing/renderer/print_render_frame_helper.cc
  399. +++ b/components/printing/renderer/print_render_frame_helper.cc
  400. @@ -40,6 +40,7 @@
  401. #include "printing/buildflags/buildflags.h"
  402. #include "printing/metafile_skia.h"
  403. #include "printing/printing_features.h"
  404. +#include "printing/print_settings.h"
  405. #include "printing/units.h"
  406. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  407. #include "third_party/blink/public/common/frame/frame_owner_element_type.h"
  408. @@ -1145,7 +1146,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  409. web_frame->DispatchBeforePrintEvent();
  410. if (!weak_this)
  411. return;
  412. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  413. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  414. + false /* silent */, base::DictionaryValue() /* new_settings */);
  415. if (weak_this)
  416. web_frame->DispatchAfterPrintEvent();
  417. }
  418. @@ -1166,7 +1168,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  419. receivers_.Add(this, std::move(receiver));
  420. }
  421. -void PrintRenderFrameHelper::PrintRequestedPages() {
  422. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) {
  423. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  424. if (ipc_nesting_level_ > 1)
  425. return;
  426. @@ -1180,7 +1182,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  427. // If we are printing a PDF extension frame, find the plugin node and print
  428. // that instead.
  429. auto plugin = delegate_->GetPdfElement(frame);
  430. - Print(frame, plugin, PrintRequestType::kRegular);
  431. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  432. if (!render_frame_gone_)
  433. frame->DispatchAfterPrintEvent();
  434. // WARNING: |this| may be gone at this point. Do not do any more work here and
  435. @@ -1197,7 +1199,7 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  436. return;
  437. }
  438. Print(frame, print_preview_context_.source_node(),
  439. - PrintRequestType::kRegular);
  440. + PrintRequestType::kRegular, false, base::DictionaryValue());
  441. if (!render_frame_gone_)
  442. frame->DispatchAfterPrintEvent();
  443. // WARNING: |this| may be gone at this point. Do not do any more work here and
  444. @@ -1237,6 +1239,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) {
  445. if (ipc_nesting_level_ > 1)
  446. return;
  447. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  448. + print_preview_context_.InitWithFrame(frame);
  449. print_preview_context_.OnPrintPreview();
  450. base::UmaHistogramEnumeration(print_preview_context_.IsForArc()
  451. @@ -1736,7 +1740,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  452. auto self = weak_ptr_factory_.GetWeakPtr();
  453. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  454. - PrintRequestType::kRegular);
  455. + PrintRequestType::kRegular,
  456. + false /* silent */,
  457. + base::DictionaryValue() /* new_settings */);
  458. // Check if |this| is still valid.
  459. if (!self)
  460. return;
  461. @@ -1747,7 +1753,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  462. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  463. const blink::WebNode& node,
  464. - PrintRequestType print_request_type) {
  465. + PrintRequestType print_request_type,
  466. + bool silent,
  467. + base::Value settings) {
  468. // If still not finished with earlier print request simply ignore.
  469. if (prep_frame_view_)
  470. return;
  471. @@ -1755,7 +1763,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  472. FrameReference frame_ref(frame);
  473. int expected_page_count = 0;
  474. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  475. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) {
  476. DidFinishPrinting(FAIL_PRINT_INIT);
  477. return; // Failed to init print page settings.
  478. }
  479. @@ -1775,8 +1783,11 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  480. PrintMsg_PrintPages_Params print_settings;
  481. auto self = weak_ptr_factory_.GetWeakPtr();
  482. - GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
  483. - print_request_type, &print_settings);
  484. + if (silent)
  485. + print_settings = *print_pages_params_.get();
  486. + else
  487. + GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
  488. + print_request_type, &print_settings);
  489. // Check if |this| is still valid.
  490. if (!self)
  491. return;
  492. @@ -2011,10 +2022,23 @@ void PrintRenderFrameHelper::IPCProcessed() {
  493. base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
  494. }
  495. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  496. +bool PrintRenderFrameHelper::InitPrintSettings(
  497. + bool fit_to_paper_size,
  498. + const base::DictionaryValue& new_settings) {
  499. PrintMsg_PrintPages_Params settings;
  500. - Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
  501. - &settings.params));
  502. + if (new_settings.empty()) {
  503. + // Send the default IPC message if caller is window.print()
  504. + Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
  505. + &settings.params));
  506. + } else {
  507. + // Send the update IPC message if caller is webContents.print()
  508. + bool canceled = false;
  509. + Send(new PrintHostMsg_UpdatePrintSettings(
  510. + routing_id(), 0, new_settings, &settings, &canceled));
  511. + if (canceled)
  512. + return false;
  513. + }
  514. +
  515. // Check if the printer returned any settings, if the settings is empty, we
  516. // can safely assume there are no printer drivers configured. So we safely
  517. // terminate.
  518. @@ -2034,12 +2058,14 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  519. return result;
  520. }
  521. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  522. - const blink::WebNode& node,
  523. - int* number_of_pages) {
  524. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  525. + blink::WebLocalFrame* frame,
  526. + const blink::WebNode& node,
  527. + int* number_of_pages,
  528. + const base::DictionaryValue& settings) {
  529. DCHECK(frame);
  530. bool fit_to_paper_size = !IsPrintingNodeOrPdfFrame(frame, node);
  531. - if (!InitPrintSettings(fit_to_paper_size)) {
  532. + if (!InitPrintSettings(fit_to_paper_size, settings)) {
  533. notify_browser_of_print_failure_ = false;
  534. Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
  535. return false;
  536. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  537. index 92ad8475b8268db89a5a7f22ad22547686388374..81df566a5908f1b35b9da5c2aad35ea7dec1dc3b 100644
  538. --- a/components/printing/renderer/print_render_frame_helper.h
  539. +++ b/components/printing/renderer/print_render_frame_helper.h
  540. @@ -226,7 +226,7 @@ class PrintRenderFrameHelper
  541. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  542. // printing::mojom::PrintRenderFrame:
  543. - void PrintRequestedPages() override;
  544. + void PrintRequestedPages(bool silent, base::Value settings) override;
  545. void PrintForSystemDialog() override;
  546. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  547. void InitiatePrintPreview(
  548. @@ -290,7 +290,9 @@ class PrintRenderFrameHelper
  549. // WARNING: |this| may be gone after this method returns.
  550. void Print(blink::WebLocalFrame* frame,
  551. const blink::WebNode& node,
  552. - PrintRequestType print_request_type);
  553. + PrintRequestType print_request_type,
  554. + bool silent,
  555. + base::Value settings);
  556. // Notification when printing is done - signal tear-down/free resources.
  557. void DidFinishPrinting(PrintingResult result);
  558. @@ -299,12 +301,14 @@ class PrintRenderFrameHelper
  559. // Initialize print page settings with default settings.
  560. // Used only for native printing workflow.
  561. - bool InitPrintSettings(bool fit_to_paper_size);
  562. + bool InitPrintSettings(bool fit_to_paper_size,
  563. + const base::DictionaryValue& settings);
  564. // Calculate number of pages in source document.
  565. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  566. const blink::WebNode& node,
  567. - int* number_of_pages);
  568. + int* number_of_pages,
  569. + const base::DictionaryValue& settings);
  570. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  571. // Set options for print preset from source PDF document.
  572. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  573. index 73940192472b1576a701cad3abbb92f2d72aa77e..bc0d39ccd113306691ae532e9fbc5b64c9aa0a33 100644
  574. --- a/printing/printing_context.cc
  575. +++ b/printing/printing_context.cc
  576. @@ -90,8 +90,6 @@ PrintingContext::Result PrintingContext::UsePdfSettings() {
  577. PrintingContext::Result PrintingContext::UpdatePrintSettings(
  578. base::Value job_settings) {
  579. - ResetSettings();
  580. -
  581. if (!PrintSettingsFromJobSettings(job_settings, settings_.get())) {
  582. NOTREACHED();
  583. return OnError();
  584. diff --git a/printing/printing_context.h b/printing/printing_context.h
  585. index 6a5a7c90ef5ba82837095c7bb934881b108797f7..a033c58076ff229ae45ed7c454fc60a57e5707b7 100644
  586. --- a/printing/printing_context.h
  587. +++ b/printing/printing_context.h
  588. @@ -131,12 +131,12 @@ class PRINTING_EXPORT PrintingContext {
  589. int job_id() const { return job_id_; }
  590. - protected:
  591. - explicit PrintingContext(Delegate* delegate);
  592. -
  593. // Reinitializes the settings for object reuse.
  594. void ResetSettings();
  595. + protected:
  596. + explicit PrintingContext(Delegate* delegate);
  597. +
  598. // Does bookkeeping when an error occurs.
  599. PrintingContext::Result OnError();