printing.patch 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 8d40bbf98d4d58704f118cb42039b0956a9f6639..06196c0fa02012a5faa82471bd39fac087918f54 100644
  12. --- a/chrome/browser/printing/print_job.cc
  13. +++ b/chrome/browser/printing/print_job.cc
  14. @@ -89,6 +89,7 @@ bool PrintWithReducedRasterization(PrefService* prefs) {
  15. return base::FeatureList::IsEnabled(features::kPrintWithReducedRasterization);
  16. }
  17. +#if 0
  18. PrefService* GetPrefsForWebContents(content::WebContents* web_contents) {
  19. // TODO(thestig): Figure out why crbug.com/1083911 occurred, which is likely
  20. // because `web_contents` was null. As a result, this section has many more
  21. @@ -97,6 +98,7 @@ PrefService* GetPrefsForWebContents(content::WebContents* web_contents) {
  22. web_contents ? web_contents->GetBrowserContext() : nullptr;
  23. return context ? Profile::FromBrowserContext(context)->GetPrefs() : nullptr;
  24. }
  25. +#endif
  26. #endif // BUILDFLAG(IS_WIN)
  27. @@ -356,8 +358,10 @@ void PrintJob::StartPdfToEmfConversion(
  28. const PrintSettings& settings = document()->settings();
  29. +#if 0
  30. PrefService* prefs = GetPrefsForWebContents(worker_->GetWebContents());
  31. - bool print_with_reduced_rasterization = PrintWithReducedRasterization(prefs);
  32. +#endif
  33. + bool print_with_reduced_rasterization = PrintWithReducedRasterization(nullptr);
  34. using RenderMode = PdfRenderSettings::Mode;
  35. RenderMode mode = print_with_reduced_rasterization
  36. @@ -447,8 +451,10 @@ void PrintJob::StartPdfToPostScriptConversion(
  37. if (ps_level2) {
  38. mode = PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2;
  39. } else {
  40. +#if 0
  41. PrefService* prefs = GetPrefsForWebContents(worker_->GetWebContents());
  42. - mode = PrintWithPostScriptType42Fonts(prefs)
  43. +#endif
  44. + mode = PrintWithPostScriptType42Fonts(nullptr)
  45. ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3_WITH_TYPE42_FONTS
  46. : PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3;
  47. }
  48. diff --git a/chrome/browser/printing/print_job.h b/chrome/browser/printing/print_job.h
  49. index 650c78f16c812170aeda99d75300ff88f47347a0..c33ce445a23f97a744db3a4ac30ef471c359553b 100644
  50. --- a/chrome/browser/printing/print_job.h
  51. +++ b/chrome/browser/printing/print_job.h
  52. @@ -261,6 +261,9 @@ class JobEventDetails : public base::RefCountedThreadSafe<JobEventDetails> {
  53. public:
  54. // Event type.
  55. enum Type {
  56. + // Print... dialog box has been closed with CANCEL button.
  57. + USER_INIT_CANCELED,
  58. +
  59. // A new document started printing.
  60. NEW_DOC,
  61. diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc
  62. index 27305997182f0a669291d2f36dd6b0b98c43f314..cbb83e1f5661852d84468ec9d342af1b2d05ae45 100644
  63. --- a/chrome/browser/printing/print_job_worker.cc
  64. +++ b/chrome/browser/printing/print_job_worker.cc
  65. @@ -20,7 +20,6 @@
  66. #include "build/build_config.h"
  67. #include "chrome/browser/browser_process.h"
  68. #include "chrome/browser/printing/print_job.h"
  69. -#include "chrome/grit/generated_resources.h"
  70. #include "components/crash/core/common/crash_keys.h"
  71. #include "components/device_event_log/device_event_log.h"
  72. #include "content/public/browser/browser_task_traits.h"
  73. @@ -28,6 +27,7 @@
  74. #include "content/public/browser/global_routing_id.h"
  75. #include "content/public/browser/render_frame_host.h"
  76. #include "content/public/browser/web_contents.h"
  77. +#include "electron/grit/electron_resources.h"
  78. #include "printing/backend/print_backend.h"
  79. #include "printing/buildflags/buildflags.h"
  80. #include "printing/mojom/print.mojom.h"
  81. @@ -234,16 +234,21 @@ void PrintJobWorker::UpdatePrintSettings(base::Value new_settings,
  82. #endif // BUILDFLAG(IS_LINUX) && defined(USE_CUPS)
  83. }
  84. - mojom::ResultCode result;
  85. {
  86. #if BUILDFLAG(IS_WIN)
  87. // Blocking is needed here because Windows printer drivers are oftentimes
  88. // not thread-safe and have to be accessed on the UI thread.
  89. base::ScopedAllowBlocking allow_blocking;
  90. #endif
  91. - result = printing_context_->UpdatePrintSettings(std::move(new_settings));
  92. + // Reset settings from previous print job
  93. + printing_context_->ResetSettings();
  94. + mojom::ResultCode get_default_result = printing_context_->UseDefaultSettings();
  95. + if (get_default_result == mojom::ResultCode::kSuccess) {
  96. + mojom::ResultCode update_result =
  97. + printing_context_->UpdatePrintSettings(std::move(new_settings));
  98. + GetSettingsDone(std::move(callback), update_result);
  99. + }
  100. }
  101. - GetSettingsDone(std::move(callback), result);
  102. }
  103. #if BUILDFLAG(IS_CHROMEOS)
  104. diff --git a/chrome/browser/printing/print_job_worker_oop.cc b/chrome/browser/printing/print_job_worker_oop.cc
  105. index 52a13c0c47f7f3f18c4f552806add67291ce8726..765bde402fec094b51faea68e67d3782bbc06564 100644
  106. --- a/chrome/browser/printing/print_job_worker_oop.cc
  107. +++ b/chrome/browser/printing/print_job_worker_oop.cc
  108. @@ -226,7 +226,7 @@ void PrintJobWorkerOop::OnFailure() {
  109. }
  110. void PrintJobWorkerOop::ShowErrorDialog() {
  111. - ShowPrintErrorDialog();
  112. + // [electron]: removed.
  113. }
  114. void PrintJobWorkerOop::UnregisterServiceManagerClient() {
  115. diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
  116. index 1f37c11047d47bb2d65975fa69f33d822206dd08..d13ee6a81cd7edc5be99f595515ca521e01702ac 100644
  117. --- a/chrome/browser/printing/print_view_manager_base.cc
  118. +++ b/chrome/browser/printing/print_view_manager_base.cc
  119. @@ -30,10 +30,10 @@
  120. #include "chrome/browser/printing/print_view_manager_common.h"
  121. #include "chrome/browser/printing/printer_query.h"
  122. #include "chrome/browser/profiles/profile.h"
  123. -#include "chrome/browser/ui/simple_message_box.h"
  124. -#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
  125. #include "chrome/common/pref_names.h"
  126. +#if 0
  127. #include "chrome/grit/generated_resources.h"
  128. +#endif
  129. #include "components/prefs/pref_service.h"
  130. #include "components/printing/browser/print_composite_client.h"
  131. #include "components/printing/browser/print_manager_utils.h"
  132. @@ -49,6 +49,7 @@
  133. #include "content/public/browser/render_process_host.h"
  134. #include "content/public/browser/render_view_host.h"
  135. #include "content/public/browser/web_contents.h"
  136. +#include "electron/grit/electron_resources.h"
  137. #include "mojo/public/cpp/system/buffer.h"
  138. #include "printing/buildflags/buildflags.h"
  139. #include "printing/metafile_skia.h"
  140. @@ -86,6 +87,8 @@ using PrintSettingsCallback =
  141. base::OnceCallback<void(std::unique_ptr<PrinterQuery>)>;
  142. void ShowWarningMessageBox(const std::u16string& message) {
  143. + LOG(ERROR) << "Invalid printer settings " << message;
  144. +#if 0
  145. // Runs always on the UI thread.
  146. static bool is_dialog_shown = false;
  147. if (is_dialog_shown)
  148. @@ -94,6 +97,7 @@ void ShowWarningMessageBox(const std::u16string& message) {
  149. base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
  150. chrome::ShowWarningMessageBox(nullptr, std::u16string(), message);
  151. +#endif
  152. }
  153. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  154. @@ -191,7 +195,9 @@ void UpdatePrintSettingsReplyOnIO(
  155. DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  156. DCHECK(printer_query);
  157. mojom::PrintPagesParamsPtr params = CreateEmptyPrintPagesParamsPtr();
  158. - if (printer_query->last_status() == mojom::ResultCode::kSuccess) {
  159. + // We call update without first printing from defaults,
  160. + // so the last printer status will still be defaulted to PrintingContext::FAILED
  161. + if (printer_query) {
  162. RenderParamsFromPrintSettings(printer_query->settings(),
  163. params->params.get());
  164. params->params->document_cookie = printer_query->cookie();
  165. @@ -244,6 +250,7 @@ void ScriptedPrintReplyOnIO(
  166. mojom::PrintManagerHost::ScriptedPrintCallback callback) {
  167. DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  168. mojom::PrintPagesParamsPtr params = CreateEmptyPrintPagesParamsPtr();
  169. +
  170. if (printer_query->last_status() == mojom::ResultCode::kSuccess &&
  171. printer_query->settings().dpi()) {
  172. RenderParamsFromPrintSettings(printer_query->settings(),
  173. @@ -253,8 +260,9 @@ void ScriptedPrintReplyOnIO(
  174. }
  175. bool has_valid_cookie = params->params->document_cookie;
  176. bool has_dpi = !params->params->dpi.IsEmpty();
  177. + bool canceled = printer_query->last_status() == mojom::ResultCode::kCanceled;
  178. content::GetUIThreadTaskRunner({})->PostTask(
  179. - FROM_HERE, base::BindOnce(std::move(callback), std::move(params)));
  180. + FROM_HERE, base::BindOnce(std::move(callback), std::move(params), canceled));
  181. if (has_dpi && has_valid_cookie) {
  182. queue->QueuePrinterQuery(std::move(printer_query));
  183. @@ -292,12 +300,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
  184. : PrintManager(web_contents),
  185. queue_(g_browser_process->print_job_manager()->queue()) {
  186. DCHECK(queue_);
  187. +#if 0 // Printing is always enabled.
  188. Profile* profile =
  189. Profile::FromBrowserContext(web_contents->GetBrowserContext());
  190. printing_enabled_.Init(
  191. prefs::kPrintingEnabled, profile->GetPrefs(),
  192. base::BindRepeating(&PrintViewManagerBase::UpdatePrintingEnabled,
  193. weak_ptr_factory_.GetWeakPtr()));
  194. +#endif
  195. }
  196. PrintViewManagerBase::~PrintViewManagerBase() {
  197. @@ -305,7 +315,10 @@ PrintViewManagerBase::~PrintViewManagerBase() {
  198. DisconnectFromCurrentPrintJob();
  199. }
  200. -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  201. +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
  202. + bool silent,
  203. + base::Value settings,
  204. + CompletionCallback callback) {
  205. // Remember the ID for `rfh`, to enable checking that the `RenderFrameHost`
  206. // is still valid after a possible inner message loop runs in
  207. // `DisconnectFromCurrentPrintJob()`.
  208. @@ -328,7 +341,9 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  209. // go in `ReleasePrintJob()`.
  210. SetPrintingRFH(rfh);
  211. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  212. + callback_ = std::move(callback);
  213. +
  214. + GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
  215. for (auto& observer : GetObservers())
  216. observer.OnPrintNow(rfh);
  217. @@ -471,7 +486,8 @@ void PrintViewManagerBase::GetDefaultPrintSettingsReply(
  218. void PrintViewManagerBase::ScriptedPrintReply(
  219. ScriptedPrintCallback callback,
  220. int process_id,
  221. - mojom::PrintPagesParamsPtr params) {
  222. + mojom::PrintPagesParamsPtr params,
  223. + bool canceled) {
  224. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  225. if (!content::RenderProcessHost::FromID(process_id)) {
  226. @@ -479,16 +495,19 @@ void PrintViewManagerBase::ScriptedPrintReply(
  227. return;
  228. }
  229. + if (canceled)
  230. + UserInitCanceled();
  231. +
  232. set_cookie(params->params->document_cookie);
  233. - std::move(callback).Run(std::move(params));
  234. + std::move(callback).Run(std::move(params), canceled);
  235. }
  236. void PrintViewManagerBase::UpdatePrintingEnabled() {
  237. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  238. // The Unretained() is safe because ForEachRenderFrameHost() is synchronous.
  239. - web_contents()->GetMainFrame()->ForEachRenderFrameHost(base::BindRepeating(
  240. - &PrintViewManagerBase::SendPrintingEnabled, base::Unretained(this),
  241. - printing_enabled_.GetValue()));
  242. + web_contents()->GetMainFrame()->ForEachRenderFrameHost(
  243. + base::BindRepeating(&PrintViewManagerBase::SendPrintingEnabled,
  244. + base::Unretained(this), true));
  245. }
  246. void PrintViewManagerBase::NavigationStopped() {
  247. @@ -602,12 +621,13 @@ void PrintViewManagerBase::DidPrintDocument(
  248. void PrintViewManagerBase::GetDefaultPrintSettings(
  249. GetDefaultPrintSettingsCallback callback) {
  250. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  251. +#if 0 // Printing is always enabled.
  252. if (!printing_enabled_.GetValue()) {
  253. GetDefaultPrintSettingsReply(std::move(callback),
  254. mojom::PrintParams::New());
  255. return;
  256. }
  257. -
  258. +#endif
  259. content::RenderFrameHost* render_frame_host = GetCurrentTargetFrame();
  260. auto callback_wrapper =
  261. base::BindOnce(&PrintViewManagerBase::GetDefaultPrintSettingsReply,
  262. @@ -624,18 +644,20 @@ void PrintViewManagerBase::UpdatePrintSettings(
  263. base::Value job_settings,
  264. UpdatePrintSettingsCallback callback) {
  265. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  266. +#if 0 // Printing is always enabled.
  267. if (!printing_enabled_.GetValue()) {
  268. UpdatePrintSettingsReply(std::move(callback),
  269. CreateEmptyPrintPagesParamsPtr(), false);
  270. return;
  271. }
  272. -
  273. +#endif
  274. if (!job_settings.FindIntKey(kSettingPrinterType)) {
  275. UpdatePrintSettingsReply(std::move(callback),
  276. CreateEmptyPrintPagesParamsPtr(), false);
  277. return;
  278. }
  279. +#if 0
  280. content::BrowserContext* context =
  281. web_contents() ? web_contents()->GetBrowserContext() : nullptr;
  282. PrefService* prefs =
  283. @@ -645,6 +667,7 @@ void PrintViewManagerBase::UpdatePrintSettings(
  284. if (value > 0)
  285. job_settings.SetIntKey(kSettingRasterizePdfDpi, value);
  286. }
  287. +#endif
  288. auto callback_wrapper =
  289. base::BindOnce(&PrintViewManagerBase::UpdatePrintSettingsReply,
  290. @@ -670,7 +693,7 @@ void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
  291. // didn't happen for some reason.
  292. bad_message::ReceivedBadMessage(
  293. render_process_host, bad_message::PVMB_SCRIPTED_PRINT_FENCED_FRAME);
  294. - std::move(callback).Run(CreateEmptyPrintPagesParamsPtr());
  295. + std::move(callback).Run(CreateEmptyPrintPagesParamsPtr(), false);
  296. return;
  297. }
  298. auto callback_wrapper = base::BindOnce(
  299. @@ -691,7 +714,6 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie) {
  300. PrintManager::PrintingFailed(cookie);
  301. #if !BUILDFLAG(IS_ANDROID) // Android does not implement this function.
  302. - ShowPrintErrorDialog();
  303. #endif
  304. ReleasePrinterQuery();
  305. @@ -706,6 +728,11 @@ void PrintViewManagerBase::RemoveObserver(Observer& observer) {
  306. }
  307. void PrintViewManagerBase::ShowInvalidPrinterSettingsError() {
  308. + if (!callback_.is_null()) {
  309. + std::string cb_str = "Invalid printer settings";
  310. + std::move(callback_).Run(printing_succeeded_, cb_str);
  311. + }
  312. +
  313. base::ThreadTaskRunnerHandle::Get()->PostTask(
  314. FROM_HERE, base::BindOnce(&ShowWarningMessageBox,
  315. l10n_util::GetStringUTF16(
  316. @@ -716,10 +743,12 @@ void PrintViewManagerBase::RenderFrameHostStateChanged(
  317. content::RenderFrameHost* render_frame_host,
  318. content::RenderFrameHost::LifecycleState /*old_state*/,
  319. content::RenderFrameHost::LifecycleState new_state) {
  320. +#if 0
  321. if (new_state == content::RenderFrameHost::LifecycleState::kActive &&
  322. render_frame_host->GetProcess()->IsPdf()) {
  323. SendPrintingEnabled(printing_enabled_.GetValue(), render_frame_host);
  324. }
  325. +#endif
  326. }
  327. void PrintViewManagerBase::DidStartLoading() {
  328. @@ -779,6 +808,11 @@ void PrintViewManagerBase::OnJobDone() {
  329. ReleasePrintJob();
  330. }
  331. +void PrintViewManagerBase::UserInitCanceled() {
  332. + printing_canceled_ = true;
  333. + ReleasePrintJob();
  334. +}
  335. +
  336. void PrintViewManagerBase::OnFailed() {
  337. TerminatePrintJob(true);
  338. }
  339. @@ -840,7 +874,10 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  340. // Disconnect the current |print_job_|.
  341. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  342. - DisconnectFromCurrentPrintJob();
  343. + if (callback_.is_null()) {
  344. + // Disconnect the current |print_job_| only when calling window.print()
  345. + DisconnectFromCurrentPrintJob();
  346. + }
  347. if (!weak_this)
  348. return false;
  349. @@ -915,6 +952,13 @@ void PrintViewManagerBase::ReleasePrintJob() {
  350. content::RenderFrameHost* rfh = printing_rfh_;
  351. printing_rfh_ = nullptr;
  352. + if (!callback_.is_null()) {
  353. + std::string cb_str = "";
  354. + if (!printing_succeeded_)
  355. + cb_str = printing_canceled_ ? "canceled" : "failed";
  356. + std::move(callback_).Run(printing_succeeded_, cb_str);
  357. + }
  358. +
  359. if (!print_job_)
  360. return;
  361. @@ -964,7 +1008,7 @@ bool PrintViewManagerBase::RunInnerMessageLoop() {
  362. }
  363. bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  364. - if (print_job_)
  365. + if (print_job_ && print_job_->document())
  366. return true;
  367. if (!cookie) {
  368. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  369. index 2661776307f773ac8f2c62529ec86349b045ee8f..cb41b271adbb02517a5e1ad222d0320000437dfb 100644
  370. --- a/chrome/browser/printing/print_view_manager_base.h
  371. +++ b/chrome/browser/printing/print_view_manager_base.h
  372. @@ -37,6 +37,8 @@ namespace printing {
  373. class PrintQueriesQueue;
  374. class PrinterQuery;
  375. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  376. +
  377. // Base class for managing the print commands for a WebContents.
  378. class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  379. public:
  380. @@ -58,7 +60,10 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  381. // Prints the current document immediately. Since the rendering is
  382. // asynchronous, the actual printing will not be completed on the return of
  383. // this function. Returns false if printing is impossible at the moment.
  384. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  385. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  386. + bool silent = true,
  387. + base::Value settings = {},
  388. + CompletionCallback callback = {});
  389. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  390. // Prints the document in `print_data` with settings specified in
  391. @@ -106,6 +111,7 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  392. ScriptedPrintCallback callback) override;
  393. void ShowInvalidPrinterSettingsError() override;
  394. void PrintingFailed(int32_t cookie) override;
  395. + void UserInitCanceled();
  396. // Adds and removes observers for `PrintViewManagerBase` events. The order in
  397. // which notifications are sent to observers is undefined. Observers must be
  398. @@ -193,7 +199,8 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  399. // Runs `callback` with `params` to reply to ScriptedPrint().
  400. void ScriptedPrintReply(ScriptedPrintCallback callback,
  401. int process_id,
  402. - mojom::PrintPagesParamsPtr params);
  403. + mojom::PrintPagesParamsPtr params,
  404. + bool canceled);
  405. // Requests the RenderView to render all the missing pages for the print job.
  406. // No-op if no print job is pending. Returns true if at least one page has
  407. @@ -248,9 +255,15 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  408. // The current RFH that is printing with a system printing dialog.
  409. raw_ptr<content::RenderFrameHost> printing_rfh_ = nullptr;
  410. + // Respond with success of the print job.
  411. + CompletionCallback callback_;
  412. +
  413. // Indication of success of the print job.
  414. bool printing_succeeded_ = false;
  415. + // Indication of whether the print job was manually canceled
  416. + bool printing_canceled_ = false;
  417. +
  418. // Set while running an inner message loop inside RenderAllMissingPagesNow().
  419. // This means we are _blocking_ until all the necessary pages have been
  420. // rendered or the print settings are being loaded.
  421. diff --git a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  422. index 879004c790d57b28e7a816ebf560971876c17168..334509d3ab45af4bb7877f656ca5aca7ee1bce00 100644
  423. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  424. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  425. @@ -20,7 +20,7 @@ FakePrintRenderFrame::FakePrintRenderFrame(
  426. FakePrintRenderFrame::~FakePrintRenderFrame() = default;
  427. -void FakePrintRenderFrame::PrintRequestedPages() {}
  428. +void FakePrintRenderFrame::PrintRequestedPages(bool /*silent*/, ::base::Value /*settings*/) {}
  429. void FakePrintRenderFrame::PrintForSystemDialog() {}
  430. diff --git a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  431. index 10f46664d8337d6be2fac24d9a6933429f3b2c2b..6de833f2da3ae85cf0752284146974f2026ab174 100644
  432. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  433. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  434. @@ -24,7 +24,7 @@ class FakePrintRenderFrame : public mojom::PrintRenderFrame {
  435. private:
  436. // printing::mojom::PrintRenderFrame:
  437. - void PrintRequestedPages() override;
  438. + void PrintRequestedPages(bool silent, ::base::Value settings) override;
  439. void PrintForSystemDialog() override;
  440. void SetPrintPreviewUI(
  441. mojo::PendingAssociatedRemote<mojom::PrintPreviewUI> preview) override;
  442. diff --git a/components/printing/browser/print_to_pdf/pdf_print_manager.cc b/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  443. index 66810a2a5f0c77ba107c71d2abaef8692bda0fea..cd6103af4571f82f11652a3c7ecf0e534428dc49 100644
  444. --- a/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  445. +++ b/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  446. @@ -116,7 +116,8 @@ void PdfPrintManager::PrintToPdf(
  447. set_cookie(print_pages_params_->params->document_cookie);
  448. callback_ = std::move(callback);
  449. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  450. + // TODO(electron-maintainers): do something with job_settings here?
  451. + GetPrintRenderFrame(rfh)->PrintRequestedPages(true/*silent*/, base::Value{}/*job_settings*/);
  452. }
  453. void PdfPrintManager::GetDefaultPrintSettings(
  454. @@ -138,14 +139,14 @@ void PdfPrintManager::ScriptedPrint(
  455. if (!printing_rfh_) {
  456. DLOG(ERROR) << "Unexpected message received before PrintToPdf is "
  457. "called: ScriptedPrint";
  458. - std::move(callback).Run(std::move(default_param));
  459. + std::move(callback).Run(std::move(default_param), true/*canceled*/);
  460. return;
  461. }
  462. if (params->is_scripted &&
  463. GetCurrentTargetFrame()->IsNestedWithinFencedFrame()) {
  464. DLOG(ERROR) << "Unexpected message received. Script Print is not allowed"
  465. " in a fenced frame.";
  466. - std::move(callback).Run(std::move(default_param));
  467. + std::move(callback).Run(std::move(default_param), true/*canceled*/);
  468. return;
  469. }
  470. absl::variant<printing::PageRanges, PageRangeError> page_ranges =
  471. @@ -162,7 +163,7 @@ void PdfPrintManager::ScriptedPrint(
  472. break;
  473. }
  474. ReleaseJob(print_result);
  475. - std::move(callback).Run(std::move(default_param));
  476. + std::move(callback).Run(std::move(default_param), true/*canceled*/);
  477. return;
  478. }
  479. @@ -170,7 +171,7 @@ void PdfPrintManager::ScriptedPrint(
  480. print_pages_params_->pages = printing::PageRange::GetPages(
  481. absl::get<printing::PageRanges>(page_ranges));
  482. - std::move(callback).Run(print_pages_params_->Clone());
  483. + std::move(callback).Run(print_pages_params_->Clone(), false/*canceled*/);
  484. }
  485. void PdfPrintManager::ShowInvalidPrinterSettingsError() {
  486. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  487. index 5afad24754e12554368a6619466ca025edc26180..e2e786692bd877d3b8bf7c31829496afa99ed539 100644
  488. --- a/components/printing/common/print.mojom
  489. +++ b/components/printing/common/print.mojom
  490. @@ -274,7 +274,7 @@ interface PrintPreviewUI {
  491. interface PrintRenderFrame {
  492. // Tells the RenderFrame to switch the CSS to print media type, render every
  493. // requested page, and then switch back the CSS to display media type.
  494. - PrintRequestedPages();
  495. + PrintRequestedPages(bool silent, mojo_base.mojom.DeprecatedDictionaryValue settings);
  496. // Tells the RenderFrame to switch the CSS to print media type, render every
  497. // requested page using the print preview document's frame/node, and then
  498. @@ -341,7 +341,7 @@ interface PrintManagerHost {
  499. // Request the print settings from the user. This step is about showing
  500. // UI to the user to select the final print settings.
  501. [Sync]
  502. - ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams settings);
  503. + ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams settings, bool canceled);
  504. // Tells the browser that there are invalid printer settings.
  505. ShowInvalidPrinterSettingsError();
  506. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  507. index 066521576d021cbd3e68057f68199c23a8a30437..72777428d2456191875806bc3c57d0801b43a8ea 100644
  508. --- a/components/printing/renderer/print_render_frame_helper.cc
  509. +++ b/components/printing/renderer/print_render_frame_helper.cc
  510. @@ -40,6 +40,7 @@
  511. #include "printing/metafile_skia.h"
  512. #include "printing/mojom/print.mojom.h"
  513. #include "printing/print_job_constants.h"
  514. +#include "printing/print_settings.h"
  515. #include "printing/units.h"
  516. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  517. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  518. @@ -1263,7 +1264,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  519. if (!weak_this)
  520. return;
  521. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  522. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  523. + false /* silent */, base::DictionaryValue() /* new_settings */);
  524. if (!weak_this)
  525. return;
  526. @@ -1294,7 +1296,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  527. receivers_.Add(this, std::move(receiver));
  528. }
  529. -void PrintRenderFrameHelper::PrintRequestedPages() {
  530. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) {
  531. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  532. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  533. return;
  534. @@ -1309,7 +1311,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  535. // plugin node and print that instead.
  536. auto plugin = delegate_->GetPdfElement(frame);
  537. - Print(frame, plugin, PrintRequestType::kRegular);
  538. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  539. if (!render_frame_gone_)
  540. frame->DispatchAfterPrintEvent();
  541. @@ -1340,7 +1342,8 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  542. }
  543. Print(frame, print_preview_context_.source_node(),
  544. - PrintRequestType::kRegular);
  545. + PrintRequestType::kRegular, false,
  546. + base::DictionaryValue());
  547. if (!render_frame_gone_)
  548. print_preview_context_.DispatchAfterPrintEvent();
  549. // WARNING: |this| may be gone at this point. Do not do any more work here and
  550. @@ -1387,6 +1390,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) {
  551. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  552. return;
  553. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  554. + print_preview_context_.InitWithFrame(frame);
  555. print_preview_context_.OnPrintPreview();
  556. if (print_preview_context_.IsForArc()) {
  557. @@ -1924,7 +1929,8 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  558. return;
  559. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  560. - PrintRequestType::kRegular);
  561. + PrintRequestType::kRegular, false /* silent */,
  562. + base::DictionaryValue() /* new_settings */);
  563. // Check if |this| is still valid.
  564. if (!weak_this)
  565. return;
  566. @@ -1939,7 +1945,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  567. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  568. const blink::WebNode& node,
  569. - PrintRequestType print_request_type) {
  570. + PrintRequestType print_request_type,
  571. + bool silent,
  572. + base::Value settings) {
  573. // If still not finished with earlier print request simply ignore.
  574. if (prep_frame_view_)
  575. return;
  576. @@ -1947,7 +1955,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  577. FrameReference frame_ref(frame);
  578. uint32_t expected_page_count = 0;
  579. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  580. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) {
  581. DidFinishPrinting(FAIL_PRINT_INIT);
  582. return; // Failed to init print page settings.
  583. }
  584. @@ -1966,8 +1974,15 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  585. print_pages_params_->params->print_scaling_option;
  586. auto self = weak_ptr_factory_.GetWeakPtr();
  587. - mojom::PrintPagesParamsPtr print_settings = GetPrintSettingsFromUser(
  588. + mojom::PrintPagesParamsPtr print_settings;
  589. +
  590. + if (silent) {
  591. + print_settings = mojom::PrintPagesParams::New();
  592. + print_settings->params = print_pages_params_->params->Clone();
  593. + } else {
  594. + print_settings = GetPrintSettingsFromUser(
  595. frame_ref.GetFrame(), node, expected_page_count, print_request_type);
  596. + }
  597. // Check if |this| is still valid.
  598. if (!self)
  599. return;
  600. @@ -2215,36 +2230,51 @@ void PrintRenderFrameHelper::IPCProcessed() {
  601. }
  602. }
  603. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  604. - mojom::PrintPagesParams settings;
  605. - settings.params = mojom::PrintParams::New();
  606. - GetPrintManagerHost()->GetDefaultPrintSettings(&settings.params);
  607. +bool PrintRenderFrameHelper::InitPrintSettings(
  608. + bool fit_to_paper_size,
  609. + const base::DictionaryValue& new_settings) {
  610. + mojom::PrintPagesParamsPtr settings;
  611. +
  612. + if (new_settings.DictEmpty()) {
  613. + settings = mojom::PrintPagesParams::New();
  614. + settings->params = mojom::PrintParams::New();
  615. + GetPrintManagerHost()->GetDefaultPrintSettings(&settings->params);
  616. + } else {
  617. + bool canceled = false;
  618. + int cookie =
  619. + print_pages_params_ ? print_pages_params_->params->document_cookie : 0;
  620. + GetPrintManagerHost()->UpdatePrintSettings(cookie, new_settings.Clone(), &settings, &canceled);
  621. + if (canceled)
  622. + return false;
  623. + }
  624. // Check if the printer returned any settings, if the settings is empty, we
  625. // can safely assume there are no printer drivers configured. So we safely
  626. // terminate.
  627. bool result = true;
  628. - if (!PrintMsg_Print_Params_IsValid(*settings.params))
  629. + if (!PrintMsg_Print_Params_IsValid(*settings->params))
  630. result = false;
  631. // Reset to default values.
  632. ignore_css_margins_ = false;
  633. - settings.pages.clear();
  634. + settings->pages.clear();
  635. - settings.params->print_scaling_option =
  636. + settings->params->print_scaling_option =
  637. fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea
  638. : mojom::PrintScalingOption::kSourceSize;
  639. - SetPrintPagesParams(settings);
  640. + SetPrintPagesParams(*settings);
  641. return result;
  642. }
  643. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  644. - const blink::WebNode& node,
  645. - uint32_t* number_of_pages) {
  646. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  647. + blink::WebLocalFrame* frame,
  648. + const blink::WebNode& node,
  649. + uint32_t* number_of_pages,
  650. + const base::DictionaryValue& settings) {
  651. DCHECK(frame);
  652. bool fit_to_paper_size = !IsPrintingNodeOrPdfFrame(frame, node);
  653. - if (!InitPrintSettings(fit_to_paper_size)) {
  654. + if (!InitPrintSettings(fit_to_paper_size, settings)) {
  655. notify_browser_of_print_failure_ = false;
  656. GetPrintManagerHost()->ShowInvalidPrinterSettingsError();
  657. return false;
  658. @@ -2389,7 +2419,7 @@ mojom::PrintPagesParamsPtr PrintRenderFrameHelper::GetPrintSettingsFromUser(
  659. std::move(params),
  660. base::BindOnce(
  661. [](base::OnceClosure quit_closure, mojom::PrintPagesParamsPtr* output,
  662. - mojom::PrintPagesParamsPtr input) {
  663. + mojom::PrintPagesParamsPtr input, bool canceled) {
  664. *output = std::move(input);
  665. std::move(quit_closure).Run();
  666. },
  667. @@ -2634,18 +2664,7 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type,
  668. }
  669. bool PrintRenderFrameHelper::CheckForCancel() {
  670. - const mojom::PrintParams& print_params = *print_pages_params_->params;
  671. - bool cancel = false;
  672. -
  673. - if (!GetPrintManagerHost()->CheckForCancel(print_params.preview_ui_id,
  674. - print_params.preview_request_id,
  675. - &cancel)) {
  676. - cancel = true;
  677. - }
  678. -
  679. - if (cancel)
  680. - notify_browser_of_print_failure_ = false;
  681. - return cancel;
  682. + return false;
  683. }
  684. bool PrintRenderFrameHelper::PreviewPageRendered(
  685. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  686. index 2b703118bf94a82262adc293368dcfcdb67807ff..a07f307ff48f3ce5409354a5ba8d54b43325da73 100644
  687. --- a/components/printing/renderer/print_render_frame_helper.h
  688. +++ b/components/printing/renderer/print_render_frame_helper.h
  689. @@ -254,7 +254,7 @@ class PrintRenderFrameHelper
  690. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  691. // printing::mojom::PrintRenderFrame:
  692. - void PrintRequestedPages() override;
  693. + void PrintRequestedPages(bool silent, base::Value settings) override;
  694. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  695. void PrintForSystemDialog() override;
  696. void SetPrintPreviewUI(
  697. @@ -321,7 +321,9 @@ class PrintRenderFrameHelper
  698. // WARNING: |this| may be gone after this method returns.
  699. void Print(blink::WebLocalFrame* frame,
  700. const blink::WebNode& node,
  701. - PrintRequestType print_request_type);
  702. + PrintRequestType print_request_type,
  703. + bool silent,
  704. + base::Value settings);
  705. // Notification when printing is done - signal tear-down/free resources.
  706. void DidFinishPrinting(PrintingResult result);
  707. @@ -330,12 +332,14 @@ class PrintRenderFrameHelper
  708. // Initialize print page settings with default settings.
  709. // Used only for native printing workflow.
  710. - bool InitPrintSettings(bool fit_to_paper_size);
  711. + bool InitPrintSettings(bool fit_to_paper_size,
  712. + const base::DictionaryValue& settings);
  713. // Calculate number of pages in source document.
  714. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  715. const blink::WebNode& node,
  716. - uint32_t* number_of_pages);
  717. + uint32_t* number_of_pages,
  718. + const base::DictionaryValue& settings);
  719. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  720. // Set options for print preset from source PDF document.
  721. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  722. index 3d8281c8af9a4339bdd492c67edafc4ec6efb09d..6f8b9d42e051579cf1d0774afa771a7e45d31ff2 100644
  723. --- a/printing/printing_context.cc
  724. +++ b/printing/printing_context.cc
  725. @@ -120,7 +120,6 @@ mojom::ResultCode PrintingContext::UsePdfSettings() {
  726. mojom::ResultCode PrintingContext::UpdatePrintSettings(
  727. base::Value job_settings) {
  728. - ResetSettings();
  729. {
  730. std::unique_ptr<PrintSettings> settings =
  731. PrintSettingsFromJobSettings(job_settings);
  732. diff --git a/printing/printing_context.h b/printing/printing_context.h
  733. index 3f36303105b7979a1a771bf26b42596abe5b3cce..52f740bb832db4a8d76431d9bc77cab10bb7e0c7 100644
  734. --- a/printing/printing_context.h
  735. +++ b/printing/printing_context.h
  736. @@ -170,6 +170,9 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  737. bool PrintingAborted() const { return abort_printing_; }
  738. + // Reinitializes the settings for object reuse.
  739. + void ResetSettings();
  740. +
  741. int job_id() const { return job_id_; }
  742. protected:
  743. @@ -180,9 +183,6 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  744. static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
  745. bool skip_system_calls);
  746. - // Reinitializes the settings for object reuse.
  747. - void ResetSettings();
  748. -
  749. // Determine if system calls should be skipped by this instance.
  750. bool skip_system_calls() const {
  751. #if BUILDFLAG(ENABLE_OOP_PRINTING)