printing.patch 28 KB

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