printing.patch 29 KB

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