printing.patch 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 bca04dcd90921e508fb0e5c8201265532e42e88f..345547f382c8984ea29483b84475c120aa58e19b 100644
  12. --- a/chrome/browser/printing/print_job.cc
  13. +++ b/chrome/browser/printing/print_job.cc
  14. @@ -350,18 +350,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_language_is_xps() &&
  19. +#if defined(OS_WIN)
  20. + settings.is_modifiable()
  21. +#else
  22. + settings.print_text_with_gdi()
  23. +#endif
  24. + && !settings.printer_language_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. @@ -499,6 +506,10 @@ void PrintJob::OnNotifyPrintJobEvent(const JobEventDetails& event_details) {
  41. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  42. switch (event_details.type()) {
  43. + case JobEventDetails::USER_INIT_CANCELED: {
  44. + DCHECK_EQ(event_details.document(), document_.get());
  45. + break;
  46. + }
  47. case JobEventDetails::FAILED:
  48. // No need to cancel since the worker already canceled itself.
  49. Stop();
  50. diff --git a/chrome/browser/printing/print_job.h b/chrome/browser/printing/print_job.h
  51. index b8b83529be4c7da661032d3b941984c02b978047..98ef6cfad5dc46d9dcb65a0ce506db3729410239 100644
  52. --- a/chrome/browser/printing/print_job.h
  53. +++ b/chrome/browser/printing/print_job.h
  54. @@ -242,6 +242,9 @@ class JobEventDetails : public base::RefCountedThreadSafe<JobEventDetails> {
  55. public:
  56. // Event type.
  57. enum Type {
  58. + // Print... dialog box has been closed with CANCEL button.
  59. + USER_INIT_CANCELED,
  60. +
  61. // A new document started printing.
  62. NEW_DOC,
  63. diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc
  64. index 2824b97e715a493082734d40f62860c8cafa5f34..584d4ef2b73a0f89458224eb134a8d8a2b439995 100644
  65. --- a/chrome/browser/printing/print_job_worker.cc
  66. +++ b/chrome/browser/printing/print_job_worker.cc
  67. @@ -21,13 +21,13 @@
  68. #include "chrome/browser/browser_process.h"
  69. #include "chrome/browser/chrome_notification_types.h"
  70. #include "chrome/browser/printing/print_job.h"
  71. -#include "chrome/grit/generated_resources.h"
  72. #include "components/crash/core/common/crash_keys.h"
  73. #include "content/public/browser/browser_task_traits.h"
  74. #include "content/public/browser/browser_thread.h"
  75. #include "content/public/browser/notification_service.h"
  76. #include "content/public/browser/render_frame_host.h"
  77. #include "content/public/browser/web_contents.h"
  78. +#include "electron/grit/electron_resources.h"
  79. #include "printing/backend/print_backend.h"
  80. #include "printing/mojom/print.mojom.h"
  81. #include "printing/print_job_constants.h"
  82. @@ -238,16 +238,21 @@ void PrintJobWorker::UpdatePrintSettings(base::Value new_settings,
  83. #endif // defined(OS_LINUX) && defined(USE_CUPS)
  84. }
  85. - PrintingContext::Result result;
  86. {
  87. #if defined(OS_WIN)
  88. // Blocking is needed here because Windows printer drivers are oftentimes
  89. // not thread-safe and have to be accessed on the UI thread.
  90. base::ScopedAllowBlocking allow_blocking;
  91. #endif
  92. - result = printing_context_->UpdatePrintSettings(std::move(new_settings));
  93. + // Reset settings from previous print job
  94. + printing_context_->ResetSettings();
  95. + PrintingContext::Result get_default_result = printing_context_->UseDefaultSettings();
  96. + if (get_default_result == PrintingContext::Result::OK) {
  97. + PrintingContext::Result update_result =
  98. + printing_context_->UpdatePrintSettings(std::move(new_settings));
  99. + GetSettingsDone(std::move(callback), update_result);
  100. + }
  101. }
  102. - GetSettingsDone(std::move(callback), result);
  103. }
  104. #if defined(OS_CHROMEOS)
  105. @@ -263,6 +268,13 @@ void PrintJobWorker::UpdatePrintSettingsFromPOD(
  106. void PrintJobWorker::GetSettingsDone(SettingsCallback callback,
  107. PrintingContext::Result result) {
  108. + if (result == PrintingContext::CANCEL) {
  109. + print_job_->PostTask(
  110. + FROM_HERE,
  111. + base::BindOnce(&NotificationCallback, base::RetainedRef(print_job_),
  112. + JobEventDetails::USER_INIT_CANCELED, 0,
  113. + base::RetainedRef(document_)));
  114. + }
  115. std::move(callback).Run(printing_context_->TakeAndResetSettings(), result);
  116. }
  117. diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
  118. index e345c611cac05d7e802b1f910031e8d799196e31..562800bf194e1f07f58b912b54a740b7c7dcabb6 100644
  119. --- a/chrome/browser/printing/print_view_manager_base.cc
  120. +++ b/chrome/browser/printing/print_view_manager_base.cc
  121. @@ -28,10 +28,10 @@
  122. #include "chrome/browser/printing/print_view_manager_common.h"
  123. #include "chrome/browser/printing/printer_query.h"
  124. #include "chrome/browser/profiles/profile.h"
  125. -#include "chrome/browser/ui/simple_message_box.h"
  126. -#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
  127. #include "chrome/common/pref_names.h"
  128. +#if 0
  129. #include "chrome/grit/generated_resources.h"
  130. +#endif
  131. #include "components/prefs/pref_service.h"
  132. #include "components/printing/browser/print_composite_client.h"
  133. #include "components/printing/browser/print_manager_utils.h"
  134. @@ -46,6 +46,7 @@
  135. #include "content/public/browser/render_process_host.h"
  136. #include "content/public/browser/render_view_host.h"
  137. #include "content/public/browser/web_contents.h"
  138. +#include "electron/grit/electron_resources.h"
  139. #include "mojo/public/cpp/system/buffer.h"
  140. #include "printing/buildflags/buildflags.h"
  141. #include "printing/metafile_skia.h"
  142. @@ -110,6 +111,8 @@ crosapi::mojom::PrintJobPtr PrintJobToMojom(
  143. #endif
  144. void ShowWarningMessageBox(const std::u16string& message) {
  145. + LOG(ERROR) << "Invalid printer settings " << message;
  146. +#if 0
  147. // Runs always on the UI thread.
  148. static bool is_dialog_shown = false;
  149. if (is_dialog_shown)
  150. @@ -118,6 +121,7 @@ void ShowWarningMessageBox(const std::u16string& message) {
  151. base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
  152. chrome::ShowWarningMessageBox(nullptr, std::u16string(), message);
  153. +#endif
  154. }
  155. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  156. @@ -236,7 +240,9 @@ void UpdatePrintSettingsReplyOnIO(
  157. DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  158. DCHECK(printer_query);
  159. mojom::PrintPagesParamsPtr params = CreateEmptyPrintPagesParamsPtr();
  160. - if (printer_query->last_status() == PrintingContext::OK) {
  161. + // We call update without first printing from defaults,
  162. + // so the last printer status will still be defaulted to PrintingContext::FAILED
  163. + if (printer_query) {
  164. RenderParamsFromPrintSettings(printer_query->settings(),
  165. params->params.get());
  166. params->params->document_cookie = printer_query->cookie();
  167. @@ -339,12 +345,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
  168. : PrintManager(web_contents),
  169. queue_(g_browser_process->print_job_manager()->queue()) {
  170. DCHECK(queue_);
  171. +#if 0 // Printing is always enabled.
  172. Profile* profile =
  173. Profile::FromBrowserContext(web_contents->GetBrowserContext());
  174. printing_enabled_.Init(
  175. prefs::kPrintingEnabled, profile->GetPrefs(),
  176. base::BindRepeating(&PrintViewManagerBase::UpdatePrintingEnabled,
  177. weak_ptr_factory_.GetWeakPtr()));
  178. +#endif
  179. }
  180. PrintViewManagerBase::~PrintViewManagerBase() {
  181. @@ -352,7 +360,10 @@ PrintViewManagerBase::~PrintViewManagerBase() {
  182. DisconnectFromCurrentPrintJob();
  183. }
  184. -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  185. +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
  186. + bool silent,
  187. + base::Value settings,
  188. + CompletionCallback callback) {
  189. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  190. DisconnectFromCurrentPrintJob();
  191. if (!weak_this)
  192. @@ -367,7 +378,14 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  193. // go in `ReleasePrintJob()`.
  194. SetPrintingRFH(rfh);
  195. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  196. + callback_ = std::move(callback);
  197. +
  198. + if (!callback_.is_null()) {
  199. + registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  200. + content::NotificationService::AllSources());
  201. + }
  202. +
  203. + GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
  204. return true;
  205. }
  206. @@ -522,9 +540,9 @@ void PrintViewManagerBase::ScriptedPrintReply(
  207. void PrintViewManagerBase::UpdatePrintingEnabled() {
  208. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  209. // The Unretained() is safe because ForEachFrame() is synchronous.
  210. - web_contents()->ForEachFrame(base::BindRepeating(
  211. - &PrintViewManagerBase::SendPrintingEnabled, base::Unretained(this),
  212. - printing_enabled_.GetValue()));
  213. + web_contents()->ForEachFrame(
  214. + base::BindRepeating(&PrintViewManagerBase::SendPrintingEnabled,
  215. + base::Unretained(this), true));
  216. }
  217. void PrintViewManagerBase::NavigationStopped() {
  218. @@ -638,12 +656,13 @@ void PrintViewManagerBase::DidPrintDocument(
  219. void PrintViewManagerBase::GetDefaultPrintSettings(
  220. GetDefaultPrintSettingsCallback callback) {
  221. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  222. +#if 0 // Printing is always enabled.
  223. if (!printing_enabled_.GetValue()) {
  224. GetDefaultPrintSettingsReply(std::move(callback),
  225. mojom::PrintParams::New());
  226. return;
  227. }
  228. -
  229. +#endif
  230. content::RenderFrameHost* render_frame_host = GetCurrentTargetFrame();
  231. auto callback_wrapper =
  232. base::BindOnce(&PrintViewManagerBase::GetDefaultPrintSettingsReply,
  233. @@ -661,12 +680,13 @@ void PrintViewManagerBase::UpdatePrintSettings(
  234. base::Value job_settings,
  235. UpdatePrintSettingsCallback callback) {
  236. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  237. +#if 0 // Printing is always enabled.
  238. if (!printing_enabled_.GetValue()) {
  239. UpdatePrintSettingsReply(std::move(callback),
  240. CreateEmptyPrintPagesParamsPtr(), false);
  241. return;
  242. }
  243. -
  244. +#endif
  245. if (!job_settings.FindIntKey(kSettingPrinterType)) {
  246. UpdatePrintSettingsReply(std::move(callback),
  247. CreateEmptyPrintPagesParamsPtr(), false);
  248. @@ -711,13 +731,18 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie) {
  249. PrintManager::PrintingFailed(cookie);
  250. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  251. - ShowPrintErrorDialog();
  252. + // ShowPrintErrorDialog();
  253. #endif
  254. ReleasePrinterQuery();
  255. }
  256. void PrintViewManagerBase::ShowInvalidPrinterSettingsError() {
  257. + if (!callback_.is_null()) {
  258. + std::string cb_str = "Invalid printer settings";
  259. + std::move(callback_).Run(printing_succeeded_, cb_str);
  260. + }
  261. +
  262. base::ThreadTaskRunnerHandle::Get()->PostTask(
  263. FROM_HERE, base::BindOnce(&ShowWarningMessageBox,
  264. l10n_util::GetStringUTF16(
  265. @@ -797,6 +822,11 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent(
  266. #endif
  267. break;
  268. }
  269. + case JobEventDetails::USER_INIT_CANCELED: {
  270. + printing_cancelled_ = true;
  271. + ReleasePrintJob();
  272. + break;
  273. + }
  274. case JobEventDetails::JOB_DONE:
  275. // Printing is done, we don't need it anymore.
  276. // print_job_->is_job_pending() may still be true, depending on the order
  277. @@ -864,9 +894,11 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  278. DCHECK(!quit_inner_loop_);
  279. DCHECK(query);
  280. - // Disconnect the current |print_job_|.
  281. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  282. - DisconnectFromCurrentPrintJob();
  283. + if (callback_.is_null()) {
  284. + // Disconnect the current |print_job_| only when calling window.print()
  285. + DisconnectFromCurrentPrintJob();
  286. + }
  287. if (!weak_this)
  288. return false;
  289. @@ -889,8 +921,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  290. /*source_id=*/"");
  291. #endif
  292. - registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  293. - content::Source<PrintJob>(print_job_.get()));
  294. printing_succeeded_ = false;
  295. return true;
  296. }
  297. @@ -942,14 +972,22 @@ void PrintViewManagerBase::ReleasePrintJob() {
  298. content::RenderFrameHost* rfh = printing_rfh_;
  299. printing_rfh_ = nullptr;
  300. + if (!callback_.is_null()) {
  301. + registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  302. + content::NotificationService::AllSources());
  303. +
  304. + std::string cb_str = "";
  305. + if (!printing_succeeded_)
  306. + cb_str = printing_cancelled_ ? "cancelled" : "failed";
  307. + std::move(callback_).Run(printing_succeeded_, cb_str);
  308. + }
  309. +
  310. if (!print_job_)
  311. return;
  312. if (rfh)
  313. GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  314. - registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
  315. - content::Source<PrintJob>(print_job_.get()));
  316. // Don't close the worker thread.
  317. print_job_ = nullptr;
  318. }
  319. @@ -988,7 +1026,7 @@ bool PrintViewManagerBase::RunInnerMessageLoop() {
  320. }
  321. bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  322. - if (print_job_)
  323. + if (print_job_ && print_job_->document())
  324. return true;
  325. if (!cookie) {
  326. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  327. index eaa0e162a339ba68d42c920fdd30869d259b4f27..8381f5171970aa89fc5c406d57d2665e16c6678b 100644
  328. --- a/chrome/browser/printing/print_view_manager_base.h
  329. +++ b/chrome/browser/printing/print_view_manager_base.h
  330. @@ -38,6 +38,8 @@ class PrintJob;
  331. class PrintQueriesQueue;
  332. class PrinterQuery;
  333. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  334. +
  335. // Base class for managing the print commands for a WebContents.
  336. class PrintViewManagerBase : public content::NotificationObserver,
  337. public PrintManager {
  338. @@ -47,7 +49,10 @@ class PrintViewManagerBase : public content::NotificationObserver,
  339. // Prints the current document immediately. Since the rendering is
  340. // asynchronous, the actual printing will not be completed on the return of
  341. // this function. Returns false if printing is impossible at the moment.
  342. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  343. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  344. + bool silent,
  345. + base::Value settings,
  346. + CompletionCallback callback);
  347. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  348. // Prints the document in |print_data| with settings specified in
  349. @@ -234,9 +239,15 @@ class PrintViewManagerBase : public content::NotificationObserver,
  350. // The current RFH that is printing with a system printing dialog.
  351. content::RenderFrameHost* printing_rfh_ = nullptr;
  352. + // Respond with success of the print job.
  353. + CompletionCallback callback_;
  354. +
  355. // Indication of success of the print job.
  356. bool printing_succeeded_ = false;
  357. + // Indication of whether the print job was manually cancelled
  358. + bool printing_cancelled_ = false;
  359. +
  360. // Set while running an inner message loop inside RenderAllMissingPagesNow().
  361. // This means we are _blocking_ until all the necessary pages have been
  362. // rendered or the print settings are being loaded.
  363. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  364. index 623659a3c78ce069cbcc83eeccfbc7265437ff01..f02cb6bced9f8382c84f560b5b40c9247db790ce 100644
  365. --- a/components/printing/common/print.mojom
  366. +++ b/components/printing/common/print.mojom
  367. @@ -270,7 +270,7 @@ interface PrintPreviewUI {
  368. interface PrintRenderFrame {
  369. // Tells the RenderFrame to switch the CSS to print media type, render every
  370. // requested page, and then switch back the CSS to display media type.
  371. - PrintRequestedPages();
  372. + PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);
  373. // Tells the RenderFrame to switch the CSS to print media type, render every
  374. // requested page using the print preview document's frame/node, and then
  375. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  376. index 868f1706b1b8db28b995cc2dc9fcfefec62b6574..5f98a820f4452328ab38b2e918a2d9a6258cb658 100644
  377. --- a/components/printing/renderer/print_render_frame_helper.cc
  378. +++ b/components/printing/renderer/print_render_frame_helper.cc
  379. @@ -38,6 +38,7 @@
  380. #include "printing/metafile_skia.h"
  381. #include "printing/mojom/print.mojom.h"
  382. #include "printing/print_job_constants.h"
  383. +#include "printing/print_settings.h"
  384. #include "printing/units.h"
  385. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  386. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  387. @@ -1218,7 +1219,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  388. if (!weak_this)
  389. return;
  390. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  391. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  392. + false /* silent */, base::DictionaryValue() /* new_settings */);
  393. if (!weak_this)
  394. return;
  395. @@ -1249,7 +1251,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  396. receivers_.Add(this, std::move(receiver));
  397. }
  398. -void PrintRenderFrameHelper::PrintRequestedPages() {
  399. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) {
  400. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  401. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  402. return;
  403. @@ -1264,7 +1266,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  404. // that instead.
  405. auto plugin = delegate_->GetPdfElement(frame);
  406. - Print(frame, plugin, PrintRequestType::kRegular);
  407. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  408. if (!render_frame_gone_)
  409. frame->DispatchAfterPrintEvent();
  410. @@ -1295,7 +1297,8 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  411. }
  412. Print(frame, print_preview_context_.source_node(),
  413. - PrintRequestType::kRegular);
  414. + PrintRequestType::kRegular, false,
  415. + base::DictionaryValue());
  416. if (!render_frame_gone_)
  417. print_preview_context_.DispatchAfterPrintEvent();
  418. // WARNING: |this| may be gone at this point. Do not do any more work here and
  419. @@ -1342,6 +1345,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) {
  420. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  421. return;
  422. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  423. + print_preview_context_.InitWithFrame(frame);
  424. print_preview_context_.OnPrintPreview();
  425. if (print_preview_context_.IsForArc()) {
  426. @@ -1878,7 +1883,8 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  427. return;
  428. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  429. - PrintRequestType::kRegular);
  430. + PrintRequestType::kRegular, false /* silent */,
  431. + base::DictionaryValue() /* new_settings */);
  432. // Check if |this| is still valid.
  433. if (!weak_this)
  434. return;
  435. @@ -1893,7 +1899,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  436. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  437. const blink::WebNode& node,
  438. - PrintRequestType print_request_type) {
  439. + PrintRequestType print_request_type,
  440. + bool silent,
  441. + base::Value settings) {
  442. // If still not finished with earlier print request simply ignore.
  443. if (prep_frame_view_)
  444. return;
  445. @@ -1901,7 +1909,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  446. FrameReference frame_ref(frame);
  447. uint32_t expected_page_count = 0;
  448. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  449. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) {
  450. DidFinishPrinting(FAIL_PRINT_INIT);
  451. return; // Failed to init print page settings.
  452. }
  453. @@ -1920,8 +1928,41 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  454. print_pages_params_->params->print_scaling_option;
  455. auto self = weak_ptr_factory_.GetWeakPtr();
  456. - mojom::PrintPagesParamsPtr print_settings = GetPrintSettingsFromUser(
  457. + mojom::PrintPagesParamsPtr print_settings;
  458. +
  459. + if (silent) {
  460. + print_settings = mojom::PrintPagesParams::New();
  461. + print_settings->params = mojom::PrintParams::New(
  462. + print_pages_params_->params->page_size,
  463. + print_pages_params_->params->content_size,
  464. + print_pages_params_->params->printable_area,
  465. + print_pages_params_->params->margin_top,
  466. + print_pages_params_->params->margin_left,
  467. + print_pages_params_->params->page_orientation,
  468. + print_pages_params_->params->dpi,
  469. + print_pages_params_->params->scale_factor,
  470. + print_pages_params_->params->document_cookie,
  471. + print_pages_params_->params->selection_only,
  472. + print_pages_params_->params->supports_alpha_blend,
  473. + print_pages_params_->params->preview_ui_id,
  474. + print_pages_params_->params->preview_request_id,
  475. + print_pages_params_->params->is_first_request,
  476. + print_pages_params_->params->print_scaling_option,
  477. + print_pages_params_->params->print_to_pdf,
  478. + print_pages_params_->params->display_header_footer,
  479. + print_pages_params_->params->title,
  480. + print_pages_params_->params->url,
  481. + print_pages_params_->params->header_template,
  482. + print_pages_params_->params->footer_template,
  483. + print_pages_params_->params->rasterize_pdf,
  484. + print_pages_params_->params->should_print_backgrounds,
  485. + print_pages_params_->params->printed_doc_type,
  486. + print_pages_params_->params->prefer_css_page_size,
  487. + print_pages_params_->params->pages_per_sheet);
  488. + } else {
  489. + print_settings = GetPrintSettingsFromUser(
  490. frame_ref.GetFrame(), node, expected_page_count, print_request_type);
  491. + }
  492. // Check if |this| is still valid.
  493. if (!self)
  494. return;
  495. @@ -2169,36 +2210,51 @@ void PrintRenderFrameHelper::IPCProcessed() {
  496. }
  497. }
  498. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  499. - mojom::PrintPagesParams settings;
  500. - settings.params = mojom::PrintParams::New();
  501. - GetPrintManagerHost()->GetDefaultPrintSettings(&settings.params);
  502. +bool PrintRenderFrameHelper::InitPrintSettings(
  503. + bool fit_to_paper_size,
  504. + const base::DictionaryValue& new_settings) {
  505. + mojom::PrintPagesParamsPtr settings;
  506. +
  507. + if (new_settings.DictEmpty()) {
  508. + settings = mojom::PrintPagesParams::New();
  509. + settings->params = mojom::PrintParams::New();
  510. + GetPrintManagerHost()->GetDefaultPrintSettings(&settings->params);
  511. + } else {
  512. + bool canceled = false;
  513. + int cookie =
  514. + print_pages_params_ ? print_pages_params_->params->document_cookie : 0;
  515. + GetPrintManagerHost()->UpdatePrintSettings(cookie, new_settings.Clone(), &settings, &canceled);
  516. + if (canceled)
  517. + return false;
  518. + }
  519. // Check if the printer returned any settings, if the settings is empty, we
  520. // can safely assume there are no printer drivers configured. So we safely
  521. // terminate.
  522. bool result = true;
  523. - if (!PrintMsg_Print_Params_IsValid(*settings.params))
  524. + if (!PrintMsg_Print_Params_IsValid(*settings->params))
  525. result = false;
  526. // Reset to default values.
  527. ignore_css_margins_ = false;
  528. - settings.pages.clear();
  529. + settings->pages.clear();
  530. - settings.params->print_scaling_option =
  531. + settings->params->print_scaling_option =
  532. fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea
  533. : mojom::PrintScalingOption::kSourceSize;
  534. - SetPrintPagesParams(settings);
  535. + SetPrintPagesParams(*settings);
  536. return result;
  537. }
  538. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  539. - const blink::WebNode& node,
  540. - uint32_t* number_of_pages) {
  541. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  542. + blink::WebLocalFrame* frame,
  543. + const blink::WebNode& node,
  544. + uint32_t* number_of_pages,
  545. + const base::DictionaryValue& settings) {
  546. DCHECK(frame);
  547. bool fit_to_paper_size = !IsPrintingNodeOrPdfFrame(frame, node);
  548. - if (!InitPrintSettings(fit_to_paper_size)) {
  549. + if (!InitPrintSettings(fit_to_paper_size, settings)) {
  550. notify_browser_of_print_failure_ = false;
  551. GetPrintManagerHost()->ShowInvalidPrinterSettingsError();
  552. return false;
  553. @@ -2569,18 +2625,7 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) {
  554. }
  555. bool PrintRenderFrameHelper::CheckForCancel() {
  556. - const mojom::PrintParams& print_params = *print_pages_params_->params;
  557. - bool cancel = false;
  558. -
  559. - if (!GetPrintManagerHost()->CheckForCancel(print_params.preview_ui_id,
  560. - print_params.preview_request_id,
  561. - &cancel)) {
  562. - cancel = true;
  563. - }
  564. -
  565. - if (cancel)
  566. - notify_browser_of_print_failure_ = false;
  567. - return cancel;
  568. + return false;
  569. }
  570. bool PrintRenderFrameHelper::PreviewPageRendered(
  571. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  572. index 90236920457c931c86426049c6cbc30b592b597f..353178863eba37b9112e784ffa4b3519076e91b9 100644
  573. --- a/components/printing/renderer/print_render_frame_helper.h
  574. +++ b/components/printing/renderer/print_render_frame_helper.h
  575. @@ -256,7 +256,7 @@ class PrintRenderFrameHelper
  576. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  577. // printing::mojom::PrintRenderFrame:
  578. - void PrintRequestedPages() override;
  579. + void PrintRequestedPages(bool silent, base::Value settings) override;
  580. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  581. void PrintForSystemDialog() override;
  582. void SetPrintPreviewUI(
  583. @@ -323,7 +323,9 @@ class PrintRenderFrameHelper
  584. // WARNING: |this| may be gone after this method returns.
  585. void Print(blink::WebLocalFrame* frame,
  586. const blink::WebNode& node,
  587. - PrintRequestType print_request_type);
  588. + PrintRequestType print_request_type,
  589. + bool silent,
  590. + base::Value settings);
  591. // Notification when printing is done - signal tear-down/free resources.
  592. void DidFinishPrinting(PrintingResult result);
  593. @@ -332,12 +334,14 @@ class PrintRenderFrameHelper
  594. // Initialize print page settings with default settings.
  595. // Used only for native printing workflow.
  596. - bool InitPrintSettings(bool fit_to_paper_size);
  597. + bool InitPrintSettings(bool fit_to_paper_size,
  598. + const base::DictionaryValue& settings);
  599. // Calculate number of pages in source document.
  600. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  601. const blink::WebNode& node,
  602. - uint32_t* number_of_pages);
  603. + uint32_t* number_of_pages,
  604. + const base::DictionaryValue& settings);
  605. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  606. // Set options for print preset from source PDF document.
  607. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  608. index 24cf547a45b76705ec4094bec219c53785781059..b81e7bcee4139dbfb4c5211f4164c53b864768bd 100644
  609. --- a/printing/printing_context.cc
  610. +++ b/printing/printing_context.cc
  611. @@ -97,7 +97,6 @@ PrintingContext::Result PrintingContext::UsePdfSettings() {
  612. PrintingContext::Result PrintingContext::UpdatePrintSettings(
  613. base::Value job_settings) {
  614. - ResetSettings();
  615. {
  616. std::unique_ptr<PrintSettings> settings =
  617. PrintSettingsFromJobSettings(job_settings);
  618. diff --git a/printing/printing_context.h b/printing/printing_context.h
  619. index 5c300fa414fbb8688674aed114543e602cdb96db..ba6d0bbce4edcb1a6e05108e4e83fc6ba5894bb4 100644
  620. --- a/printing/printing_context.h
  621. +++ b/printing/printing_context.h
  622. @@ -133,12 +133,12 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  623. int job_id() const { return job_id_; }
  624. - protected:
  625. - explicit PrintingContext(Delegate* delegate);
  626. -
  627. // Reinitializes the settings for object reuse.
  628. void ResetSettings();
  629. + protected:
  630. + explicit PrintingContext(Delegate* delegate);
  631. +
  632. // Does bookkeeping when an error occurs.
  633. PrintingContext::Result OnError();