printing.patch 27 KB

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