printing.patch 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 331a084371402b5a2440b5d60feac8f0189e84b9..6755d1f497cef4deea6b83df1d8720dcf54817e9 100644
  12. --- a/chrome/browser/printing/print_job.cc
  13. +++ b/chrome/browser/printing/print_job.cc
  14. @@ -90,6 +90,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. @@ -98,6 +99,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. @@ -351,8 +353,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. @@ -442,8 +446,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_worker.cc b/chrome/browser/printing/print_job_worker.cc
  49. index d9ae000f1348529ab849349d7562dbb04fe9fd16..fcfeffd86bd897467b12bf1aba4aaac09986cfd9 100644
  50. --- a/chrome/browser/printing/print_job_worker.cc
  51. +++ b/chrome/browser/printing/print_job_worker.cc
  52. @@ -20,7 +20,6 @@
  53. #include "build/build_config.h"
  54. #include "chrome/browser/browser_process.h"
  55. #include "chrome/browser/printing/print_job.h"
  56. -#include "chrome/grit/generated_resources.h"
  57. #include "components/crash/core/common/crash_keys.h"
  58. #include "components/device_event_log/device_event_log.h"
  59. #include "content/public/browser/browser_task_traits.h"
  60. @@ -28,6 +27,7 @@
  61. #include "content/public/browser/global_routing_id.h"
  62. #include "content/public/browser/render_frame_host.h"
  63. #include "content/public/browser/web_contents.h"
  64. +#include "chrome/grit/generated_resources.h"
  65. #include "printing/backend/print_backend.h"
  66. #include "printing/buildflags/buildflags.h"
  67. #include "printing/mojom/print.mojom.h"
  68. @@ -222,16 +222,19 @@ void PrintJobWorker::UpdatePrintSettings(base::Value::Dict new_settings,
  69. #endif // BUILDFLAG(IS_LINUX) && defined(USE_CUPS)
  70. }
  71. - mojom::ResultCode result;
  72. {
  73. #if BUILDFLAG(IS_WIN)
  74. // Blocking is needed here because Windows printer drivers are oftentimes
  75. // not thread-safe and have to be accessed on the UI thread.
  76. base::ScopedAllowBlocking allow_blocking;
  77. #endif
  78. - result = printing_context_->UpdatePrintSettings(std::move(new_settings));
  79. + // Reset settings from previous print job
  80. + printing_context_->ResetSettings();
  81. + mojom::ResultCode result_code = printing_context_->UseDefaultSettings();
  82. + if (result_code == mojom::ResultCode::kSuccess)
  83. + result_code = printing_context_->UpdatePrintSettings(std::move(new_settings));
  84. + GetSettingsDone(std::move(callback), result_code);
  85. }
  86. - GetSettingsDone(std::move(callback), result);
  87. }
  88. #if BUILDFLAG(IS_CHROMEOS)
  89. diff --git a/chrome/browser/printing/print_job_worker_oop.cc b/chrome/browser/printing/print_job_worker_oop.cc
  90. index 1e158ecd686e775f656d5a05a9d916ce8f075fa8..20613012d1e6f435c3211d78ec311cf06d4852f5 100644
  91. --- a/chrome/browser/printing/print_job_worker_oop.cc
  92. +++ b/chrome/browser/printing/print_job_worker_oop.cc
  93. @@ -362,7 +362,7 @@ void PrintJobWorkerOop::OnFailure() {
  94. }
  95. void PrintJobWorkerOop::ShowErrorDialog() {
  96. - ShowPrintErrorDialog();
  97. + // [electron]: removed.
  98. }
  99. void PrintJobWorkerOop::UnregisterServiceManagerClient() {
  100. diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
  101. index 4bd871e5cadc693aea6e8c71b3fe3296b743d9ec..471440d0c1a23d028d0fb476f2c6315354305b0b 100644
  102. --- a/chrome/browser/printing/print_view_manager_base.cc
  103. +++ b/chrome/browser/printing/print_view_manager_base.cc
  104. @@ -30,10 +30,10 @@
  105. #include "chrome/browser/printing/print_view_manager_common.h"
  106. #include "chrome/browser/printing/printer_query.h"
  107. #include "chrome/browser/profiles/profile.h"
  108. -#include "chrome/browser/ui/simple_message_box.h"
  109. -#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
  110. #include "chrome/common/pref_names.h"
  111. +#if 0
  112. #include "chrome/grit/generated_resources.h"
  113. +#endif
  114. #include "components/prefs/pref_service.h"
  115. #include "components/printing/browser/print_composite_client.h"
  116. #include "components/printing/browser/print_manager_utils.h"
  117. @@ -48,6 +48,7 @@
  118. #include "content/public/browser/render_frame_host.h"
  119. #include "content/public/browser/render_process_host.h"
  120. #include "content/public/browser/web_contents.h"
  121. +#include "chrome/grit/generated_resources.h"
  122. #include "mojo/public/cpp/system/buffer.h"
  123. #include "printing/buildflags/buildflags.h"
  124. #include "printing/metafile_skia.h"
  125. @@ -83,10 +84,23 @@ namespace printing {
  126. namespace {
  127. +std::string PrintReasonFromPrintStatus(PrintViewManager::PrintStatus status) {
  128. + if (status == PrintViewManager::PrintStatus::kInvalid) {
  129. + return "Invalid printer settings";
  130. + } else if (status == PrintViewManager::PrintStatus::kCanceled) {
  131. + return "Print job canceled";
  132. + } else if (status == PrintViewManager::PrintStatus::kFailed) {
  133. + return "Print job failed";
  134. + }
  135. + return "";
  136. +}
  137. +
  138. using PrintSettingsCallback =
  139. base::OnceCallback<void(std::unique_ptr<PrinterQuery>)>;
  140. void ShowWarningMessageBox(const std::u16string& message) {
  141. + LOG(ERROR) << "Invalid printer settings " << message;
  142. +#if 0
  143. // Runs always on the UI thread.
  144. static bool is_dialog_shown = false;
  145. if (is_dialog_shown)
  146. @@ -95,6 +109,7 @@ void ShowWarningMessageBox(const std::u16string& message) {
  147. base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
  148. chrome::ShowWarningMessageBox(nullptr, std::u16string(), message);
  149. +#endif
  150. }
  151. void OnDidGetDefaultPrintSettings(
  152. @@ -144,7 +159,9 @@ void OnDidUpdatePrintSettings(
  153. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  154. DCHECK(printer_query);
  155. mojom::PrintPagesParamsPtr params = CreateEmptyPrintPagesParamsPtr();
  156. - if (printer_query->last_status() == mojom::ResultCode::kSuccess) {
  157. + // We call update without first printing from defaults,
  158. + // so the last printer status will still be defaulted to PrintingContext::FAILED
  159. + if (printer_query) {
  160. RenderParamsFromPrintSettings(printer_query->settings(),
  161. params->params.get());
  162. params->params->document_cookie = printer_query->cookie();
  163. @@ -172,6 +189,7 @@ void OnDidScriptedPrint(
  164. mojom::PrintManagerHost::ScriptedPrintCallback callback) {
  165. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  166. mojom::PrintPagesParamsPtr params = CreateEmptyPrintPagesParamsPtr();
  167. +
  168. if (printer_query->last_status() == mojom::ResultCode::kSuccess &&
  169. printer_query->settings().dpi()) {
  170. RenderParamsFromPrintSettings(printer_query->settings(),
  171. @@ -181,7 +199,8 @@ void OnDidScriptedPrint(
  172. }
  173. bool has_valid_cookie = params->params->document_cookie;
  174. bool has_dpi = !params->params->dpi.IsEmpty();
  175. - std::move(callback).Run(std::move(params));
  176. + bool canceled = printer_query->last_status() == mojom::ResultCode::kCanceled;
  177. + std::move(callback).Run(std::move(params), canceled);
  178. if (has_dpi && has_valid_cookie) {
  179. queue->QueuePrinterQuery(std::move(printer_query));
  180. @@ -196,12 +215,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
  181. : PrintManager(web_contents),
  182. queue_(g_browser_process->print_job_manager()->queue()) {
  183. DCHECK(queue_);
  184. +#if 0 // Printing is always enabled.
  185. Profile* profile =
  186. Profile::FromBrowserContext(web_contents->GetBrowserContext());
  187. printing_enabled_.Init(
  188. prefs::kPrintingEnabled, profile->GetPrefs(),
  189. base::BindRepeating(&PrintViewManagerBase::UpdatePrintingEnabled,
  190. weak_ptr_factory_.GetWeakPtr()));
  191. +#endif
  192. }
  193. PrintViewManagerBase::~PrintViewManagerBase() {
  194. @@ -209,7 +230,10 @@ PrintViewManagerBase::~PrintViewManagerBase() {
  195. DisconnectFromCurrentPrintJob();
  196. }
  197. -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  198. +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
  199. + bool silent,
  200. + base::Value::Dict settings,
  201. + CompletionCallback callback) {
  202. // Remember the ID for `rfh`, to enable checking that the `RenderFrameHost`
  203. // is still valid after a possible inner message loop runs in
  204. // `DisconnectFromCurrentPrintJob()`.
  205. @@ -237,6 +261,9 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
  206. #endif
  207. SetPrintingRFH(rfh);
  208. + callback_ = std::move(callback);
  209. +
  210. + GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
  211. #if BUILDFLAG(ENABLE_PRINT_CONTENT_ANALYSIS)
  212. enterprise_connectors::ContentAnalysisDelegate::Data scanning_data;
  213. @@ -405,7 +432,8 @@ void PrintViewManagerBase::GetDefaultPrintSettingsReply(
  214. void PrintViewManagerBase::ScriptedPrintReply(
  215. ScriptedPrintCallback callback,
  216. int process_id,
  217. - mojom::PrintPagesParamsPtr params) {
  218. + mojom::PrintPagesParamsPtr params,
  219. + bool canceled) {
  220. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  221. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  222. @@ -420,8 +448,11 @@ void PrintViewManagerBase::ScriptedPrintReply(
  223. return;
  224. }
  225. + if (canceled)
  226. + UserInitCanceled();
  227. +
  228. set_cookie(params->params->document_cookie);
  229. - std::move(callback).Run(std::move(params));
  230. + std::move(callback).Run(std::move(params), canceled);
  231. }
  232. void PrintViewManagerBase::UpdatePrintingEnabled() {
  233. @@ -429,8 +460,7 @@ void PrintViewManagerBase::UpdatePrintingEnabled() {
  234. // The Unretained() is safe because ForEachRenderFrameHost() is synchronous.
  235. web_contents()->GetPrimaryMainFrame()->ForEachRenderFrameHost(
  236. base::BindRepeating(&PrintViewManagerBase::SendPrintingEnabled,
  237. - base::Unretained(this),
  238. - printing_enabled_.GetValue()));
  239. + base::Unretained(this), true));
  240. }
  241. void PrintViewManagerBase::NavigationStopped() {
  242. @@ -546,11 +576,14 @@ void PrintViewManagerBase::DidPrintDocument(
  243. void PrintViewManagerBase::GetDefaultPrintSettings(
  244. GetDefaultPrintSettingsCallback callback) {
  245. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  246. +#if 0 // Printing is always enabled.
  247. +
  248. if (!printing_enabled_.GetValue()) {
  249. GetDefaultPrintSettingsReply(std::move(callback),
  250. mojom::PrintParams::New());
  251. return;
  252. }
  253. +#endif
  254. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  255. if (printing::features::kEnableOopPrintDriversJobPrint.Get() &&
  256. !service_manager_client_id_.has_value()) {
  257. @@ -588,18 +621,20 @@ void PrintViewManagerBase::UpdatePrintSettings(
  258. base::Value::Dict job_settings,
  259. UpdatePrintSettingsCallback callback) {
  260. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  261. +#if 0 // Printing is always enabled.
  262. if (!printing_enabled_.GetValue()) {
  263. UpdatePrintSettingsReply(std::move(callback),
  264. CreateEmptyPrintPagesParamsPtr(), false);
  265. return;
  266. }
  267. -
  268. +#endif
  269. if (!job_settings.FindInt(kSettingPrinterType)) {
  270. UpdatePrintSettingsReply(std::move(callback),
  271. CreateEmptyPrintPagesParamsPtr(), false);
  272. return;
  273. }
  274. +#if 0
  275. content::BrowserContext* context =
  276. web_contents() ? web_contents()->GetBrowserContext() : nullptr;
  277. PrefService* prefs =
  278. @@ -609,6 +644,7 @@ void PrintViewManagerBase::UpdatePrintSettings(
  279. if (value > 0)
  280. job_settings.Set(kSettingRasterizePdfDpi, value);
  281. }
  282. +#endif
  283. auto callback_wrapper =
  284. base::BindOnce(&PrintViewManagerBase::UpdatePrintSettingsReply,
  285. @@ -640,14 +676,14 @@ void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
  286. // didn't happen for some reason.
  287. bad_message::ReceivedBadMessage(
  288. render_process_host, bad_message::PVMB_SCRIPTED_PRINT_FENCED_FRAME);
  289. - std::move(callback).Run(CreateEmptyPrintPagesParamsPtr());
  290. + std::move(callback).Run(CreateEmptyPrintPagesParamsPtr(), false);
  291. return;
  292. }
  293. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  294. if (printing::features::kEnableOopPrintDriversJobPrint.Get() &&
  295. !service_manager_client_id_.has_value()) {
  296. // Renderer process has requested settings outside of the expected setup.
  297. - std::move(callback).Run(CreateEmptyPrintPagesParamsPtr());
  298. + std::move(callback).Run(CreateEmptyPrintPagesParamsPtr(), false);
  299. return;
  300. }
  301. #endif
  302. @@ -685,7 +721,6 @@ void PrintViewManagerBase::PrintingFailed(int32_t cookie,
  303. PrintManager::PrintingFailed(cookie, reason);
  304. #if !BUILDFLAG(IS_ANDROID) // Android does not implement this function.
  305. - ShowPrintErrorDialog();
  306. #endif
  307. ReleasePrinterQuery();
  308. @@ -700,6 +735,11 @@ void PrintViewManagerBase::RemoveObserver(Observer& observer) {
  309. }
  310. void PrintViewManagerBase::ShowInvalidPrinterSettingsError() {
  311. + if (!callback_.is_null()) {
  312. + printing_status_ = PrintStatus::kInvalid;
  313. + TerminatePrintJob(true);
  314. + }
  315. +
  316. base::ThreadTaskRunnerHandle::Get()->PostTask(
  317. FROM_HERE, base::BindOnce(&ShowWarningMessageBox,
  318. l10n_util::GetStringUTF16(
  319. @@ -710,10 +750,12 @@ void PrintViewManagerBase::RenderFrameHostStateChanged(
  320. content::RenderFrameHost* render_frame_host,
  321. content::RenderFrameHost::LifecycleState /*old_state*/,
  322. content::RenderFrameHost::LifecycleState new_state) {
  323. +#if 0
  324. if (new_state == content::RenderFrameHost::LifecycleState::kActive &&
  325. render_frame_host->GetProcess()->IsPdf()) {
  326. SendPrintingEnabled(printing_enabled_.GetValue(), render_frame_host);
  327. }
  328. +#endif
  329. }
  330. void PrintViewManagerBase::DidStartLoading() {
  331. @@ -769,7 +811,12 @@ void PrintViewManagerBase::OnJobDone() {
  332. // Printing is done, we don't need it anymore.
  333. // print_job_->is_job_pending() may still be true, depending on the order
  334. // of object registration.
  335. - printing_succeeded_ = true;
  336. + printing_status_ = PrintStatus::kSucceeded;
  337. + ReleasePrintJob();
  338. +}
  339. +
  340. +void PrintViewManagerBase::UserInitCanceled() {
  341. + printing_status_ = PrintStatus::kCanceled;
  342. ReleasePrintJob();
  343. }
  344. @@ -783,7 +830,7 @@ bool PrintViewManagerBase::RenderAllMissingPagesNow() {
  345. // Is the document already complete?
  346. if (print_job_->document() && print_job_->document()->IsComplete()) {
  347. - printing_succeeded_ = true;
  348. + printing_status_ = PrintStatus::kSucceeded;
  349. return true;
  350. }
  351. @@ -831,7 +878,10 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  352. // Disconnect the current `print_job_`.
  353. auto weak_this = weak_ptr_factory_.GetWeakPtr();
  354. - DisconnectFromCurrentPrintJob();
  355. + if (callback_.is_null()) {
  356. + // Disconnect the current |print_job_| only when calling window.print()
  357. + DisconnectFromCurrentPrintJob();
  358. + }
  359. if (!weak_this)
  360. return false;
  361. @@ -852,7 +902,7 @@ bool PrintViewManagerBase::CreateNewPrintJob(
  362. #endif
  363. print_job_->AddObserver(*this);
  364. - printing_succeeded_ = false;
  365. + printing_status_ = PrintStatus::kFailed;
  366. return true;
  367. }
  368. @@ -912,6 +962,11 @@ void PrintViewManagerBase::ReleasePrintJob() {
  369. }
  370. #endif
  371. + if (!callback_.is_null()) {
  372. + bool success = printing_status_ == PrintStatus::kSucceeded;
  373. + std::move(callback_).Run(success, PrintReasonFromPrintStatus(printing_status_));
  374. + }
  375. +
  376. if (!print_job_)
  377. return;
  378. @@ -919,7 +974,7 @@ void PrintViewManagerBase::ReleasePrintJob() {
  379. // printing_rfh_ should only ever point to a RenderFrameHost with a live
  380. // RenderFrame.
  381. DCHECK(rfh->IsRenderFrameLive());
  382. - GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
  383. + GetPrintRenderFrame(rfh)->PrintingDone(printing_status_ == PrintStatus::kSucceeded);
  384. }
  385. print_job_->RemoveObserver(*this);
  386. @@ -961,7 +1016,7 @@ bool PrintViewManagerBase::RunInnerMessageLoop() {
  387. }
  388. bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
  389. - if (print_job_)
  390. + if (print_job_ && print_job_->document())
  391. return true;
  392. if (!cookie) {
  393. @@ -1069,7 +1124,7 @@ void PrintViewManagerBase::SendPrintingEnabled(bool enabled,
  394. }
  395. void PrintViewManagerBase::CompletePrintNow(content::RenderFrameHost* rfh) {
  396. - GetPrintRenderFrame(rfh)->PrintRequestedPages();
  397. + GetPrintRenderFrame(rfh)->PrintRequestedPages(/*silent=*/true, /*job_settings=*/base::Value::Dict());
  398. for (auto& observer : GetObservers())
  399. observer.OnPrintNow(rfh);
  400. diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
  401. index 746df417a23f7760818ba265d4a7d589dec8bf34..0d3e4491826be629c7251a69afc7ebde990e10e1 100644
  402. --- a/chrome/browser/printing/print_view_manager_base.h
  403. +++ b/chrome/browser/printing/print_view_manager_base.h
  404. @@ -41,6 +41,8 @@ namespace printing {
  405. class PrintQueriesQueue;
  406. class PrinterQuery;
  407. +using CompletionCallback = base::OnceCallback<void(bool, const std::string&)>;
  408. +
  409. // Base class for managing the print commands for a WebContents.
  410. class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  411. public:
  412. @@ -64,7 +66,10 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  413. // Prints the current document immediately. Since the rendering is
  414. // asynchronous, the actual printing will not be completed on the return of
  415. // this function. Returns false if printing is impossible at the moment.
  416. - virtual bool PrintNow(content::RenderFrameHost* rfh);
  417. + virtual bool PrintNow(content::RenderFrameHost* rfh,
  418. + bool silent = true,
  419. + base::Value::Dict settings = {},
  420. + CompletionCallback callback = {});
  421. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  422. // Prints the document in `print_data` with settings specified in
  423. @@ -113,6 +118,7 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  424. void ShowInvalidPrinterSettingsError() override;
  425. void PrintingFailed(int32_t cookie,
  426. mojom::PrintFailureReason reason) override;
  427. + void UserInitCanceled();
  428. // Adds and removes observers for `PrintViewManagerBase` events. The order in
  429. // which notifications are sent to observers is undefined. Observers must be
  430. @@ -120,6 +126,14 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  431. void AddObserver(Observer& observer);
  432. void RemoveObserver(Observer& observer);
  433. + enum class PrintStatus {
  434. + kSucceeded,
  435. + kCanceled,
  436. + kFailed,
  437. + kInvalid,
  438. + kUnknown
  439. + };
  440. +
  441. protected:
  442. explicit PrintViewManagerBase(content::WebContents* web_contents);
  443. @@ -241,7 +255,8 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  444. // Runs `callback` with `params` to reply to ScriptedPrint().
  445. void ScriptedPrintReply(ScriptedPrintCallback callback,
  446. int process_id,
  447. - mojom::PrintPagesParamsPtr params);
  448. + mojom::PrintPagesParamsPtr params,
  449. + bool canceled);
  450. // Requests the RenderView to render all the missing pages for the print job.
  451. // No-op if no print job is pending. Returns true if at least one page has
  452. @@ -314,8 +329,11 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer {
  453. // The current RFH that is printing with a system printing dialog.
  454. raw_ptr<content::RenderFrameHost> printing_rfh_ = nullptr;
  455. + // Respond with success of the print job.
  456. + CompletionCallback callback_;
  457. +
  458. // Indication of success of the print job.
  459. - bool printing_succeeded_ = false;
  460. + PrintStatus printing_status_ = PrintStatus::kUnknown;
  461. // Set while running an inner message loop inside RenderAllMissingPagesNow().
  462. // This means we are _blocking_ until all the necessary pages have been
  463. 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
  464. index b2bd74f28f70bc601ec47820030ad965b19cf068..027e4c0b78d44b69504d248755bf7f25ff423361 100644
  465. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  466. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.cc
  467. @@ -21,7 +21,7 @@ FakePrintRenderFrame::FakePrintRenderFrame(
  468. FakePrintRenderFrame::~FakePrintRenderFrame() = default;
  469. -void FakePrintRenderFrame::PrintRequestedPages() {}
  470. +void FakePrintRenderFrame::PrintRequestedPages(bool /*silent*/, ::base::Value::Dict /*settings*/) {}
  471. void FakePrintRenderFrame::PrintWithParams(mojom::PrintPagesParamsPtr params) {
  472. NOTREACHED();
  473. 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
  474. index 42f9f296e6ef65a934167c2d4773e504020378bc..3ac69d39e4eb380f97cb779be1e9ad8706ce8243 100644
  475. --- a/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  476. +++ b/chrome/browser/ui/webui/print_preview/fake_print_render_frame.h
  477. @@ -25,7 +25,7 @@ class FakePrintRenderFrame : public mojom::PrintRenderFrame {
  478. private:
  479. // printing::mojom::PrintRenderFrame:
  480. - void PrintRequestedPages() override;
  481. + void PrintRequestedPages(bool silent, ::base::Value::Dict settings) override;
  482. void PrintWithParams(mojom::PrintPagesParamsPtr params) override;
  483. void PrintForSystemDialog() override;
  484. void SetPrintPreviewUI(
  485. diff --git a/components/printing/browser/print_to_pdf/pdf_print_manager.cc b/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  486. index 82591f8c2abbc1a180ef62f7264a68ca279e9b9c..ad27a15ba3028af1046482192dec789df5dda7b2 100644
  487. --- a/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  488. +++ b/components/printing/browser/print_to_pdf/pdf_print_manager.cc
  489. @@ -132,7 +132,8 @@ void PdfPrintManager::PrintToPdf(
  490. set_cookie(print_pages_params->params->document_cookie);
  491. callback_ = std::move(callback);
  492. - GetPrintRenderFrame(rfh)->PrintWithParams(std::move(print_pages_params));
  493. + // TODO(electron-maintainers): do something with job_settings here?
  494. + GetPrintRenderFrame(rfh)->PrintRequestedPages(true/*silent*/, base::Value{}/*job_settings*/);
  495. }
  496. void PdfPrintManager::GetDefaultPrintSettings(
  497. @@ -147,7 +148,7 @@ void PdfPrintManager::ScriptedPrint(
  498. auto default_param = printing::mojom::PrintPagesParams::New();
  499. default_param->params = printing::mojom::PrintParams::New();
  500. DLOG(ERROR) << "Scripted print is not supported";
  501. - std::move(callback).Run(std::move(default_param));
  502. + std::move(callback).Run(std::move(default_param), true/*canceled*/);
  503. }
  504. void PdfPrintManager::ShowInvalidPrinterSettingsError() {
  505. diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
  506. index f2c17a8fecc36ea18de71598b582e206661763c5..599b34690da042b57fcd78d0c0557d183ce10c0f 100644
  507. --- a/components/printing/common/print.mojom
  508. +++ b/components/printing/common/print.mojom
  509. @@ -280,7 +280,7 @@ enum PrintFailureReason {
  510. interface PrintRenderFrame {
  511. // Tells the RenderFrame to switch the CSS to print media type, render every
  512. // requested page, and then switch back the CSS to display media type.
  513. - PrintRequestedPages();
  514. + PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);
  515. // Requests the frame to be printed with specified parameters. This is used
  516. // to programmatically produce PDF by request from the browser (e.g. over
  517. @@ -362,7 +362,7 @@ interface PrintManagerHost {
  518. // Request the print settings from the user. This step is about showing
  519. // UI to the user to select the final print settings.
  520. [Sync]
  521. - ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams settings);
  522. + ScriptedPrint(ScriptedPrintParams params) => (PrintPagesParams settings, bool canceled);
  523. // Tells the browser that there are invalid printer settings.
  524. ShowInvalidPrinterSettingsError();
  525. diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
  526. index 66ac411e4a70610cbf457e0bbbb0439a6e0f7c59..8b5e9f822615223bb55dba288e8f131d095950c7 100644
  527. --- a/components/printing/renderer/print_render_frame_helper.cc
  528. +++ b/components/printing/renderer/print_render_frame_helper.cc
  529. @@ -42,6 +42,7 @@
  530. #include "printing/mojom/print.mojom.h"
  531. #include "printing/page_number.h"
  532. #include "printing/print_job_constants.h"
  533. +#include "printing/print_settings.h"
  534. #include "printing/units.h"
  535. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  536. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  537. @@ -1279,7 +1280,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
  538. if (!weak_this)
  539. return;
  540. - Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
  541. + Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
  542. + false /* silent */, base::Value::Dict() /* new_settings */);
  543. if (!weak_this)
  544. return;
  545. @@ -1310,7 +1312,7 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
  546. receivers_.Add(this, std::move(receiver));
  547. }
  548. -void PrintRenderFrameHelper::PrintRequestedPages() {
  549. +void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value::Dict settings) {
  550. ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
  551. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  552. return;
  553. @@ -1325,7 +1327,7 @@ void PrintRenderFrameHelper::PrintRequestedPages() {
  554. // plugin node and print that instead.
  555. auto plugin = delegate_->GetPdfElement(frame);
  556. - Print(frame, plugin, PrintRequestType::kRegular);
  557. + Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
  558. if (!render_frame_gone_)
  559. frame->DispatchAfterPrintEvent();
  560. @@ -1391,7 +1393,8 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
  561. }
  562. Print(frame, print_preview_context_.source_node(),
  563. - PrintRequestType::kRegular);
  564. + PrintRequestType::kRegular, false,
  565. + base::Value::Dict());
  566. if (!render_frame_gone_)
  567. print_preview_context_.DispatchAfterPrintEvent();
  568. // WARNING: |this| may be gone at this point. Do not do any more work here and
  569. @@ -1440,6 +1443,8 @@ void PrintRenderFrameHelper::PrintPreview(base::Value::Dict settings) {
  570. if (ipc_nesting_level_ > kAllowedIpcDepthForPrint)
  571. return;
  572. + blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  573. + print_preview_context_.InitWithFrame(frame);
  574. print_preview_context_.OnPrintPreview();
  575. #if BUILDFLAG(IS_CHROMEOS_ASH)
  576. @@ -2051,7 +2056,8 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  577. return;
  578. Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
  579. - PrintRequestType::kRegular);
  580. + PrintRequestType::kRegular, false /* silent */,
  581. + base::Value::Dict() /* new_settings */);
  582. // Check if |this| is still valid.
  583. if (!weak_this)
  584. return;
  585. @@ -2066,7 +2072,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
  586. void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  587. const blink::WebNode& node,
  588. - PrintRequestType print_request_type) {
  589. + PrintRequestType print_request_type,
  590. + bool silent,
  591. + base::Value::Dict settings) {
  592. // If still not finished with earlier print request simply ignore.
  593. if (prep_frame_view_)
  594. return;
  595. @@ -2074,7 +2082,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  596. FrameReference frame_ref(frame);
  597. uint32_t expected_page_count = 0;
  598. - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
  599. + if (!CalculateNumberOfPages(frame, node, &expected_page_count, std::move(settings))) {
  600. DidFinishPrinting(FAIL_PRINT_INIT);
  601. return; // Failed to init print page settings.
  602. }
  603. @@ -2093,8 +2101,15 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
  604. print_pages_params_->params->print_scaling_option;
  605. auto self = weak_ptr_factory_.GetWeakPtr();
  606. - mojom::PrintPagesParamsPtr print_settings = GetPrintSettingsFromUser(
  607. + mojom::PrintPagesParamsPtr print_settings;
  608. +
  609. + if (silent) {
  610. + print_settings = mojom::PrintPagesParams::New();
  611. + print_settings->params = print_pages_params_->params->Clone();
  612. + } else {
  613. + print_settings = GetPrintSettingsFromUser(
  614. frame_ref.GetFrame(), node, expected_page_count, print_request_type);
  615. + }
  616. // Check if |this| is still valid.
  617. if (!self)
  618. return;
  619. @@ -2327,36 +2342,52 @@ void PrintRenderFrameHelper::IPCProcessed() {
  620. }
  621. }
  622. -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
  623. - mojom::PrintPagesParams settings;
  624. - settings.params = mojom::PrintParams::New();
  625. - GetPrintManagerHost()->GetDefaultPrintSettings(&settings.params);
  626. +bool PrintRenderFrameHelper::InitPrintSettings(
  627. + bool fit_to_paper_size,
  628. + base::Value::Dict new_settings) {
  629. + mojom::PrintPagesParamsPtr settings;
  630. +
  631. + if (new_settings.empty()) {
  632. + settings = mojom::PrintPagesParams::New();
  633. + settings->params = mojom::PrintParams::New();
  634. + GetPrintManagerHost()->GetDefaultPrintSettings(&settings->params);
  635. + } else {
  636. + bool canceled = false;
  637. + int cookie =
  638. + print_pages_params_ ? print_pages_params_->params->document_cookie : 0;
  639. + GetPrintManagerHost()->UpdatePrintSettings(
  640. + cookie, std::move(new_settings), &settings, &canceled);
  641. + if (canceled)
  642. + return false;
  643. + }
  644. // Check if the printer returned any settings, if the settings is empty, we
  645. // can safely assume there are no printer drivers configured. So we safely
  646. // terminate.
  647. bool result = true;
  648. - if (!PrintMsg_Print_Params_IsValid(*settings.params))
  649. + if (!PrintMsg_Print_Params_IsValid(*settings->params))
  650. result = false;
  651. // Reset to default values.
  652. ignore_css_margins_ = false;
  653. - settings.pages.clear();
  654. + settings->pages.clear();
  655. - settings.params->print_scaling_option =
  656. + settings->params->print_scaling_option =
  657. fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea
  658. : mojom::PrintScalingOption::kSourceSize;
  659. - SetPrintPagesParams(settings);
  660. + SetPrintPagesParams(*settings);
  661. return result;
  662. }
  663. -bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
  664. - const blink::WebNode& node,
  665. - uint32_t* number_of_pages) {
  666. +bool PrintRenderFrameHelper::CalculateNumberOfPages(
  667. + blink::WebLocalFrame* frame,
  668. + const blink::WebNode& node,
  669. + uint32_t* number_of_pages,
  670. + base::Value::Dict settings) {
  671. DCHECK(frame);
  672. bool fit_to_paper_size = !IsPrintingPdfFrame(frame, node);
  673. - if (!InitPrintSettings(fit_to_paper_size)) {
  674. + if (!InitPrintSettings(fit_to_paper_size, std::move(settings))) {
  675. notify_browser_of_print_failure_ = false;
  676. GetPrintManagerHost()->ShowInvalidPrinterSettingsError();
  677. return false;
  678. @@ -2481,7 +2512,7 @@ mojom::PrintPagesParamsPtr PrintRenderFrameHelper::GetPrintSettingsFromUser(
  679. std::move(params),
  680. base::BindOnce(
  681. [](base::OnceClosure quit_closure, mojom::PrintPagesParamsPtr* output,
  682. - mojom::PrintPagesParamsPtr input) {
  683. + mojom::PrintPagesParamsPtr input, bool canceled) {
  684. *output = std::move(input);
  685. std::move(quit_closure).Run();
  686. },
  687. @@ -2725,18 +2756,7 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type,
  688. }
  689. bool PrintRenderFrameHelper::CheckForCancel() {
  690. - const mojom::PrintParams& print_params = *print_pages_params_->params;
  691. - bool cancel = false;
  692. -
  693. - if (!GetPrintManagerHost()->CheckForCancel(print_params.preview_ui_id,
  694. - print_params.preview_request_id,
  695. - &cancel)) {
  696. - cancel = true;
  697. - }
  698. -
  699. - if (cancel)
  700. - notify_browser_of_print_failure_ = false;
  701. - return cancel;
  702. + return false;
  703. }
  704. bool PrintRenderFrameHelper::PreviewPageRendered(
  705. diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
  706. index f118cae62de1cebb78c8193365bafcaba3b863a8..6c8bb35f6ea00b8366134a3f00d1a43fa22d81b5 100644
  707. --- a/components/printing/renderer/print_render_frame_helper.h
  708. +++ b/components/printing/renderer/print_render_frame_helper.h
  709. @@ -255,7 +255,7 @@ class PrintRenderFrameHelper
  710. mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
  711. // printing::mojom::PrintRenderFrame:
  712. - void PrintRequestedPages() override;
  713. + void PrintRequestedPages(bool silent, base::Value::Dict settings) override;
  714. void PrintWithParams(mojom::PrintPagesParamsPtr params) override;
  715. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  716. void PrintForSystemDialog() override;
  717. @@ -327,7 +327,9 @@ class PrintRenderFrameHelper
  718. // WARNING: |this| may be gone after this method returns.
  719. void Print(blink::WebLocalFrame* frame,
  720. const blink::WebNode& node,
  721. - PrintRequestType print_request_type);
  722. + PrintRequestType print_request_type,
  723. + bool silent,
  724. + base::Value::Dict settings);
  725. // Notification when printing is done - signal tear-down/free resources.
  726. void DidFinishPrinting(PrintingResult result);
  727. @@ -336,12 +338,14 @@ class PrintRenderFrameHelper
  728. // Initialize print page settings with default settings.
  729. // Used only for native printing workflow.
  730. - bool InitPrintSettings(bool fit_to_paper_size);
  731. + bool InitPrintSettings(bool fit_to_paper_size,
  732. + base::Value::Dict new_settings);
  733. // Calculate number of pages in source document.
  734. bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
  735. const blink::WebNode& node,
  736. - uint32_t* number_of_pages);
  737. + uint32_t* number_of_pages,
  738. + base::Value::Dict settings);
  739. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  740. // Set options for print preset from source PDF document.
  741. diff --git a/printing/printing_context.cc b/printing/printing_context.cc
  742. index 93db1a80a360702a36f2d3113c9a83105bf7fffe..c3e012ec8d9a1c19434240d27553e486c0729d43 100644
  743. --- a/printing/printing_context.cc
  744. +++ b/printing/printing_context.cc
  745. @@ -128,7 +128,6 @@ void PrintingContext::UsePdfSettings() {
  746. mojom::ResultCode PrintingContext::UpdatePrintSettings(
  747. base::Value::Dict job_settings) {
  748. - ResetSettings();
  749. {
  750. std::unique_ptr<PrintSettings> settings =
  751. PrintSettingsFromJobSettings(job_settings);
  752. diff --git a/printing/printing_context.h b/printing/printing_context.h
  753. index 58fcf619add5093bd99fd9c561e8686b060a01c6..76db2a2438cef84fcb6dfd4a67d2e171428e4be0 100644
  754. --- a/printing/printing_context.h
  755. +++ b/printing/printing_context.h
  756. @@ -171,6 +171,9 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  757. bool PrintingAborted() const { return abort_printing_; }
  758. + // Reinitializes the settings for object reuse.
  759. + void ResetSettings();
  760. +
  761. int job_id() const { return job_id_; }
  762. protected:
  763. @@ -181,9 +184,6 @@ class COMPONENT_EXPORT(PRINTING) PrintingContext {
  764. static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
  765. bool skip_system_calls);
  766. - // Reinitializes the settings for object reuse.
  767. - void ResetSettings();
  768. -
  769. // Determine if system calls should be skipped by this instance.
  770. bool skip_system_calls() const {
  771. #if BUILDFLAG(ENABLE_OOP_PRINTING)