printing.patch 37 KB

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