printing.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 ebffdb08d080eceb0ea9e96ee3ca7db627cfaf5e..bc00a7a00626cee17e5f5ea976f8248f71e06505 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 155d86fe79b99989c3f604aeaab13782f8e6631c..b60263e722b2604ed35a1ed10469ac91e22aaa33 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. @@ -250,9 +263,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. @@ -355,7 +368,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. @@ -455,9 +468,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. @@ -552,8 +569,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  169. DCHECK(!quit_inner_loop_);
  170. DCHECK(query);
  171. - // Disconnect the current |print_job_|.
  172. - DisconnectFromCurrentPrintJob();
  173. // We can't print if there is no renderer.
  174. if (!web_contents()->GetRenderViewHost() ||
  175. @@ -568,8 +583,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  176. print_job_->SetSource(PrintJob::Source::PRINT_PREVIEW, /*source_id=*/"");
  177. #endif // defined(OS_CHROMEOS)
  178. - registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  179. - content::Source<PrintJob>(print_job_.get()));
  180. printing_succeeded_ = false;
  181. return true;
  182. }
  183. @@ -618,14 +631,24 @@ void PrintViewManagerBase::ReleasePrintJob() {
  184. content::RenderFrameHost* rfh = printing_rfh_;
  185. printing_rfh_ = nullptr;
  186. + if (!callback_.is_null()) {
  187. + std::string cb_str = "";
  188. + if (!printing_succeeded_)
  189. + cb_str = printing_cancelled_ ? "cancelled" : "failed";
  190. + std::move(callback_).Run(printing_succeeded_, cb_str);
  191. + }
  192. +
  193. + TerminatePrintJob(true);
  194. +
  195. if (!print_job_)
  196. return;
  197. if (rfh)
  198. GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  199. - registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  200. - content::Source<PrintJob>(print_job_.get()));
  201. + if (!callback_.is_null())
  202. + registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  203. + content::NotificationService::AllSources());
  204. // Don't close the worker thread.
  205. print_job_ = nullptr;
  206. }
  207. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  208. index 3d434244927aca5e5dbec8d7c1cd212c5c260c71..9d62c903bf08fa8cfa8c425d430c5e995a04f7fc 100644
  209. --- a/chrome/browser/printing/print_view_manager_base.h
  210. +++ b/chrome/browser/printing/print_view_manager_base.h
  211. @@ -33,6 +33,8 @@ class PrintJob;
  212. class PrintQueriesQueue;
  213. class PrinterQuery;
  214. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  215. +
  216. // Base class for managing the print commands for a WebContents.
  217. class PrintViewManagerBase : public content::NotificationObserver,
  218. public PrintManager {
  219. @@ -42,7 +44,10 @@ class PrintViewManagerBase : public content::NotificationObserver,
  220. // Prints the current document immediately. Since the rendering is
  221. // asynchronous, the actual printing will not be completed on the return of
  222. // this function. Returns false if printing is impossible at the moment.
  223. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  224. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  225. + bool silent,
  226. + base::Value settings,
  227. + CompletionCallback callback);
  228. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  229. // Prints the document in |print_data| with settings specified in
  230. @@ -206,9 +211,15 @@ class PrintViewManagerBase : public content::NotificationObserver,
  231. // The current RFH that is printing with a system printing dialog.
  232. content::RenderFrameHost* printing_rfh_;
  233. + // Respond with success of the print job.
  234. + CompletionCallback callback_;
  235. +
  236. // Indication of success of the print job.
  237. bool printing_succeeded_;
  238. + // Indication of whether the print job was manually cancelled
  239. + bool printing_cancelled_ = false;
  240. +
  241. // Set while running an inner message loop inside RenderAllMissingPagesNow().
  242. // This means we are _blocking_ until all the necessary pages have been
  243. // rendered or the print settings are being loaded.
  244. diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc
  245. index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2eb48262ce 100644
  246. --- a/chrome/browser/printing/printing_message_filter.cc
  247. +++ b/chrome/browser/printing/printing_message_filter.cc
  248. @@ -22,6 +22,7 @@
  249. #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
  250. #include "components/printing/browser/print_manager_utils.h"
  251. #include "components/printing/common/print_messages.h"
  252. +#include "content/public/browser/browser_context.h"
  253. #include "content/public/browser/browser_task_traits.h"
  254. #include "content/public/browser/render_frame_host.h"
  255. #include "content/public/browser/web_contents.h"
  256. @@ -90,20 +91,23 @@ void PrintingMessageFilter::SetDelegateForTesting(TestDelegate* delegate) {
  257. g_test_delegate = delegate;
  258. }
  259. -PrintingMessageFilter::PrintingMessageFilter(int render_process_id,
  260. - Profile* profile)
  261. +PrintingMessageFilter::PrintingMessageFilter(
  262. + int render_process_id,
  263. + content::BrowserContext* browser_context)
  264. : BrowserMessageFilter(PrintMsgStart),
  265. render_process_id_(render_process_id),
  266. queue_(g_browser_process->print_job_manager()->queue()) {
  267. DCHECK(queue_.get());
  268. printing_shutdown_notifier_ =
  269. PrintingMessageFilterShutdownNotifierFactory::GetInstance()
  270. - ->Get(profile)
  271. + ->Get(browser_context)
  272. ->Subscribe(base::Bind(&PrintingMessageFilter::ShutdownOnUIThread,
  273. base::Unretained(this)));
  274. + #if 0
  275. is_printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
  276. is_printing_enabled_.MoveToSequence(
  277. base::CreateSingleThreadTaskRunner({BrowserThread::IO}));
  278. + #endif
  279. }
  280. PrintingMessageFilter::~PrintingMessageFilter() {
  281. @@ -138,11 +142,13 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
  282. void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
  283. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  284. +#if 0
  285. if (!is_printing_enabled_.GetValue()) {
  286. // Reply with NULL query.
  287. OnGetDefaultPrintSettingsReply(nullptr, reply_msg);
  288. return;
  289. }
  290. +#endif
  291. std::unique_ptr<PrinterQuery> printer_query = queue_->PopPrinterQuery(0);
  292. if (!printer_query) {
  293. printer_query =
  294. @@ -228,11 +234,13 @@ void PrintingMessageFilter::OnScriptedPrintReply(
  295. void PrintingMessageFilter::OnUpdatePrintSettings(int document_cookie,
  296. base::Value job_settings,
  297. IPC::Message* reply_msg) {
  298. +#if 0
  299. if (!is_printing_enabled_.GetValue()) {
  300. // Reply with NULL query.
  301. OnUpdatePrintSettingsReply(nullptr, reply_msg);
  302. return;
  303. }
  304. +#endif
  305. std::unique_ptr<PrinterQuery> printer_query =
  306. queue_->PopPrinterQuery(document_cookie);
  307. if (!printer_query) {
  308. @@ -258,7 +266,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
  309. std::unique_ptr<PrinterQuery> printer_query,
  310. IPC::Message* reply_msg) {
  311. PrintMsg_PrintPages_Params params;
  312. - if (!printer_query || printer_query->last_status() != PrintingContext::OK) {
  313. + // We call update without first printing from defaults,
  314. + // so the last printer status will still be defaulted to PrintingContext::FAILED
  315. + if (!printer_query) {
  316. params.Reset();
  317. } else {
  318. RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
  319. @@ -296,7 +306,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
  320. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  321. void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids,
  322. bool* cancel) {
  323. - *cancel = PrintPreviewUI::ShouldCancelRequest(ids);
  324. + *cancel = false;
  325. }
  326. #endif
  327. diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h
  328. index 9fbea6d0a2dbe55b1d600fbc217dee5aa8ae8cd5..de9bd267e408c02fd4da7d903523c0e6305088d5 100644
  329. --- a/chrome/browser/printing/printing_message_filter.h
  330. +++ b/chrome/browser/printing/printing_message_filter.h
  331. @@ -24,6 +24,10 @@ struct PrintHostMsg_ScriptedPrint_Params;
  332. struct PrintMsg_Print_Params;
  333. class Profile;
  334. +namespace content {
  335. +class BrowserContext;
  336. +}
  337. +
  338. namespace printing {
  339. class PrintQueriesQueue;
  340. @@ -44,7 +48,8 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
  341. static void SetDelegateForTesting(TestDelegate* delegate);
  342. - PrintingMessageFilter(int render_process_id, Profile* profile);
  343. + PrintingMessageFilter(int render_process_id,
  344. + content::BrowserContext* browser_context);
  345. // content::BrowserMessageFilter:
  346. bool OnMessageReceived(const IPC::Message& message) override;
  347. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  348. index c4707f4680f1522689c77d3e8bbc57cb62787381..2909205539b2fcfc86e610cfdff0ca418b73d934 100644
  349. --- a/components/printing/common/print.mojom
  350. +++ b/components/printing/common/print.mojom
  351. @@ -35,7 +35,7 @@ interface PrintRenderer {
  352. interface PrintRenderFrame {
  353. // Tells the RenderFrame to switch the CSS to print media type, render every
  354. // requested page, and then switch back the CSS to display media type.
  355. - PrintRequestedPages();
  356. + PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);
  357. // Tells the RenderFrame to switch the CSS to print media type, render every
  358. // requested page using the print preview document's frame/node, and then
  359. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  360. index 1af8977aaa94dc9cb8b6587bff84884d5f0cd20f..819c9416a30aac7f10fd788c477a0c8e9f7e7cca 100644
  361. --- a/components/printing/renderer/print_render_frame_helper.cc
  362. +++ b/components/printing/renderer/print_render_frame_helper.cc
  363. @@ -38,6 +38,7 @@
  364. #include "net/base/escape.h"
  365. #include "printing/buildflags/buildflags.h"
  366. #include "printing/metafile_skia.h"
  367. +#include "printing/print_settings.h"
  368. #include "printing/units.h"
  369. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  370. #include "third_party/blink/public/common/frame/frame_owner_element_type.h"
  371. @@ -1120,7 +1121,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  372. web_frame->DispatchBeforePrintEvent();
  373. if (!weak_this)
  374. return;
  375. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  376. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  377. + false /* silent */, base::DictionaryValue() /* new_settings */);
  378. if (weak_this)
  379. web_frame->DispatchAfterPrintEvent();
  380. }
  381. @@ -1141,7 +1143,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  382. receivers_.Add(this, std::move(receiver));
  383. }
  384. -void PrintRenderFrameHelper::PrintRequestedPages() {
  385. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) {
  386. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  387. if (ipc_nesting_level_ > 1)
  388. return;
  389. @@ -1155,7 +1157,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  390. // If we are printing a PDF extension frame, find the plugin node and print
  391. // that instead.
  392. auto plugin = delegate_->GetPdfElement(frame);
  393. - Print(frame, plugin, PrintRequestType::kRegular);
  394. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  395. if (!render_frame_gone_)
  396. frame->DispatchAfterPrintEvent();
  397. // WARNING: |this| may be gone at this point. Do not do any more work here and
  398. @@ -1172,7 +1174,7 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  399. return;
  400. }
  401. Print(frame, print_preview_context_.source_node(),
  402. - PrintRequestType::kRegular);
  403. + PrintRequestType::kRegular, false, base::DictionaryValue());
  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. @@ -1212,6 +1214,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) {
  408. if (ipc_nesting_level_ > 1)
  409. return;
  410. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  411. + print_preview_context_.InitWithFrame(frame);
  412. print_preview_context_.OnPrintPreview();
  413. base::UmaHistogramEnumeration(print_preview_context_.IsForArc()
  414. @@ -1564,13 +1568,13 @@ bool PrintRenderFrameHelper::FinalizePrintReadyDocument() {
  415. print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_CAPTURE_FAILED);
  416. return false;
  417. }
  418. - } else {
  419. - if (!CopyMetafileDataToReadOnlySharedMem(*metafile,
  420. - &preview_params.content)) {
  421. - LOG(ERROR) << "CopyMetafileDataToReadOnlySharedMem failed";
  422. - print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
  423. - return false;
  424. - }
  425. + }
  426. +
  427. + if (!CopyMetafileDataToReadOnlySharedMem(*metafile,
  428. + &preview_params.content)) {
  429. + LOG(ERROR) << "CopyMetafileDataToReadOnlySharedMem failed";
  430. + print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
  431. + return false;
  432. }
  433. preview_params.document_cookie = print_pages_params_->params.document_cookie;
  434. @@ -1705,7 +1709,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  435. auto self = weak_ptr_factory_.GetWeakPtr();
  436. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  437. - PrintRequestType::kRegular);
  438. + PrintRequestType::kRegular,
  439. + false /* silent */,
  440. + base::DictionaryValue() /* new_settings */);
  441. // Check if |this| is still valid.
  442. if (!self)
  443. return;
  444. @@ -1716,7 +1722,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  445. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  446. const blink::WebNode& node,
  447. - PrintRequestType print_request_type) {
  448. + PrintRequestType print_request_type,
  449. + bool silent,
  450. + base::Value settings) {
  451. // If still not finished with earlier print request simply ignore.
  452. if (prep_frame_view_)
  453. return;
  454. @@ -1724,7 +1732,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  455. FrameReference frame_ref(frame);
  456. int expected_page_count = 0;
  457. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  458. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) {
  459. DidFinishPrinting(FAIL_PRINT_INIT);
  460. return; // Failed to init print page settings.
  461. }
  462. @@ -1744,8 +1752,11 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  463. PrintMsg_PrintPages_Params print_settings;
  464. auto self = weak_ptr_factory_.GetWeakPtr();
  465. - GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
  466. - print_request_type, &print_settings);
  467. + if (silent)
  468. + print_settings = *print_pages_params_.get();
  469. + else
  470. + GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
  471. + print_request_type, &print_settings);
  472. // Check if |this| is still valid.
  473. if (!self)
  474. return;
  475. @@ -1979,10 +1990,23 @@ void PrintRenderFrameHelper::IPCProcessed() {
  476. base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
  477. }
  478. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  479. +bool PrintRenderFrameHelper::InitPrintSettings(
  480. + bool fit_to_paper_size,
  481. + const base::DictionaryValue& new_settings) {
  482. PrintMsg_PrintPages_Params settings;
  483. - Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
  484. - &settings.params));
  485. + if (new_settings.empty()) {
  486. + // Send the default IPC message if caller is window.print()
  487. + Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
  488. + &settings.params));
  489. + } else {
  490. + // Send the update IPC message if caller is webContents.print()
  491. + bool canceled = false;
  492. + Send(new PrintHostMsg_UpdatePrintSettings(
  493. + routing_id(), 0, new_settings, &settings, &canceled));
  494. + if (canceled)
  495. + return false;
  496. + }
  497. +
  498. // Check if the printer returned any settings, if the settings is empty, we
  499. // can safely assume there are no printer drivers configured. So we safely
  500. // terminate.
  501. @@ -2002,12 +2026,14 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  502. return result;
  503. }
  504. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  505. - const blink::WebNode& node,
  506. - int* number_of_pages) {
  507. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  508. + blink::WebLocalFrame* frame,
  509. + const blink::WebNode& node,
  510. + int* number_of_pages,
  511. + const base::DictionaryValue& settings) {
  512. DCHECK(frame);
  513. bool fit_to_paper_size = !IsPrintingNodeOrPdfFrame(frame, node);
  514. - if (!InitPrintSettings(fit_to_paper_size)) {
  515. + if (!InitPrintSettings(fit_to_paper_size, settings)) {
  516. notify_browser_of_print_failure_ = false;
  517. Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
  518. return false;
  519. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  520. index 0778deefa7af69bb747719ab1c05c6de6c201be5..9d1872d79b4bf88a2d8db8b24eebfdc63c2ad2a8 100644
  521. --- a/components/printing/renderer/print_render_frame_helper.h
  522. +++ b/components/printing/renderer/print_render_frame_helper.h
  523. @@ -222,7 +222,7 @@ class PrintRenderFrameHelper
  524. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  525. // printing::mojom::PrintRenderFrame:
  526. - void PrintRequestedPages() override;
  527. + void PrintRequestedPages(bool silent, base::Value settings) override;
  528. void PrintForSystemDialog() override;
  529. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  530. void InitiatePrintPreview(
  531. @@ -285,7 +285,9 @@ class PrintRenderFrameHelper
  532. // WARNING: |this| may be gone after this method returns.
  533. void Print(blink::WebLocalFrame* frame,
  534. const blink::WebNode& node,
  535. - PrintRequestType print_request_type);
  536. + PrintRequestType print_request_type,
  537. + bool silent,
  538. + base::Value settings);
  539. // Notification when printing is done - signal tear-down/free resources.
  540. void DidFinishPrinting(PrintingResult result);
  541. @@ -294,12 +296,14 @@ class PrintRenderFrameHelper
  542. // Initialize print page settings with default settings.
  543. // Used only for native printing workflow.
  544. - bool InitPrintSettings(bool fit_to_paper_size);
  545. + bool InitPrintSettings(bool fit_to_paper_size,
  546. + const base::DictionaryValue& settings);
  547. // Calculate number of pages in source document.
  548. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  549. const blink::WebNode& node,
  550. - int* number_of_pages);
  551. + int* number_of_pages,
  552. + const base::DictionaryValue& settings);
  553. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  554. // Set options for print preset from source PDF document.
  555. diff --git a/printing/print_settings_conversion.cc b/printing/print_settings_conversion.cc
  556. index e01d195261a4c993dddb77d7a355481bf02067cc..2e533a6981c86387f892f8a3aaed99478f6079ef 100644
  557. --- a/printing/print_settings_conversion.cc
  558. +++ b/printing/print_settings_conversion.cc
  559. @@ -186,12 +186,14 @@ bool PrintSettingsFromJobSettings(const base::Value& job_settings,
  560. settings->set_dpi_xy(dpi_horizontal.value(), dpi_vertical.value());
  561. #endif
  562. + const std::string* device_name =
  563. + job_settings.FindStringKey(kSettingDeviceName);
  564. + if (!device_name->empty())
  565. + settings->set_device_name(base::UTF8ToUTF16(*device_name));
  566. settings->set_collate(collate.value());
  567. settings->set_copies(copies.value());
  568. settings->SetOrientation(landscape.value());
  569. - settings->set_device_name(
  570. - base::UTF8ToUTF16(*job_settings.FindStringKey(kSettingDeviceName)));
  571. settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode.value()));
  572. settings->set_color(static_cast<ColorModel>(color.value()));
  573. settings->set_scale_factor(static_cast<double>(scale_factor.value()) / 100.0);
  574. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  575. index 73940192472b1576a701cad3abbb92f2d72aa77e..bc0d39ccd113306691ae532e9fbc5b64c9aa0a33 100644
  576. --- a/printing/printing_context.cc
  577. +++ b/printing/printing_context.cc
  578. @@ -90,8 +90,6 @@ PrintingContext::Result PrintingContext::UsePdfSettings() {
  579. PrintingContext::Result PrintingContext::UpdatePrintSettings(
  580. base::Value job_settings) {
  581. - ResetSettings();
  582. -
  583. if (!PrintSettingsFromJobSettings(job_settings, settings_.get())) {
  584. NOTREACHED();
  585. return OnError();
  586. diff --git a/printing/printing_context.h b/printing/printing_context.h
  587. index 6a5a7c90ef5ba82837095c7bb934881b108797f7..a033c58076ff229ae45ed7c454fc60a57e5707b7 100644
  588. --- a/printing/printing_context.h
  589. +++ b/printing/printing_context.h
  590. @@ -131,12 +131,12 @@ class PRINTING_EXPORT PrintingContext {
  591. int job_id() const { return job_id_; }
  592. - protected:
  593. - explicit PrintingContext(Delegate* delegate);
  594. -
  595. // Reinitializes the settings for object reuse.
  596. void ResetSettings();
  597. + protected:
  598. + explicit PrintingContext(Delegate* delegate);
  599. +
  600. // Does bookkeeping when an error occurs.
  601. PrintingContext::Result OnError();