Skip to content

Commit

Permalink
fix: refactor printing for mojo (#21059)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Nov 13, 2019
1 parent 59cb78e commit 8d67f16
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 76 deletions.
88 changes: 49 additions & 39 deletions patches/chromium/printing.patch
Expand Up @@ -63,7 +63,7 @@ index ab32d5475a0e269d32f6ab71f7dc82aa9e02035a..9ada750416f02d23c2f9faae0c41e4a7
}

diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb07588684d1ea05a 100644
index 81cb954f02363c829947dde830f340a761c80a77..98043d6ec0cb2c55493ef7446a6de0c4eeed184e 100644
--- a/chrome/browser/printing/print_view_manager_base.cc
+++ b/chrome/browser/printing/print_view_manager_base.cc
@@ -27,10 +27,7 @@
Expand Down Expand Up @@ -117,28 +117,35 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
}

PrintViewManagerBase::~PrintViewManagerBase() {
@@ -123,7 +126,8 @@ PrintViewManagerBase::~PrintViewManagerBase() {
@@ -123,7 +126,10 @@ PrintViewManagerBase::~PrintViewManagerBase() {
DisconnectFromCurrentPrintJob();
}

-bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
+bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
+ CompletionCallback callback) {
+ bool silent,
+ base::Value settings,
+ CompletionCallback callback) {
DisconnectFromCurrentPrintJob();

// Don't print / print preview interstitials or crashed tabs.
@@ -131,7 +135,9 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
@@ -131,7 +136,14 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
return false;

SetPrintingRFH(rfh);
- GetPrintRenderFrame(rfh)->PrintRequestedPages();
+ callback_ = std::move(callback);
+ // FIXME: pass 'silent' through from the caller..?
+ GetPrintRenderFrame(rfh)->PrintRequestedPages(false /* silent */);
+
+ if (!callback_.is_null()) {
+ registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
+ content::NotificationService::AllSources());
+ }
+
+ GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings));
return true;
}

@@ -246,9 +252,9 @@ void PrintViewManagerBase::StartLocalPrintJob(
@@ -246,9 +258,9 @@ void PrintViewManagerBase::StartLocalPrintJob(
void PrintViewManagerBase::UpdatePrintingEnabled() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// The Unretained() is safe because ForEachFrame() is synchronous.
Expand All @@ -151,7 +158,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
}

void PrintViewManagerBase::NavigationStopped() {
@@ -351,7 +357,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) {
@@ -351,7 +363,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) {
PrintManager::OnPrintingFailed(cookie);

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
Expand All @@ -160,7 +167,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
#endif

ReleasePrinterQuery();
@@ -449,9 +455,13 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent(
@@ -449,9 +461,13 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent(
content::NotificationService::NoDetails());
break;
}
Expand All @@ -176,7 +183,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
NOTREACHED();
break;
}
@@ -546,8 +556,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
@@ -546,8 +562,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
DCHECK(!quit_inner_loop_);
DCHECK(query);

Expand All @@ -185,7 +192,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868

// We can't print if there is no renderer.
if (!web_contents()->GetRenderViewHost() ||
@@ -562,8 +570,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
@@ -562,8 +576,6 @@ bool PrintViewManagerBase::CreateNewPrintJob(
print_job_->SetSource(PrintJob::Source::PRINT_PREVIEW, /*source_id=*/"");
#endif // defined(OS_CHROMEOS)

Expand All @@ -194,7 +201,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
printing_succeeded_ = false;
return true;
}
@@ -612,14 +618,22 @@ void PrintViewManagerBase::ReleasePrintJob() {
@@ -612,14 +624,24 @@ void PrintViewManagerBase::ReleasePrintJob() {
content::RenderFrameHost* rfh = printing_rfh_;
printing_rfh_ = nullptr;

Expand All @@ -204,6 +211,8 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
+ cb_str = printing_cancelled_ ? "cancelled" : "failed";
+ std::move(callback_).Run(printing_succeeded_, cb_str);
+ }
+
+ TerminatePrintJob(true);
+
if (!print_job_)
return;
Expand All @@ -219,18 +228,8 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868
// Don't close the worker thread.
print_job_ = nullptr;
}
@@ -694,6 +708,9 @@ bool PrintViewManagerBase::PrintNowInternal(
// Don't print / print preview interstitials or crashed tabs.
if (IsInterstitialOrCrashed())
return false;
+
+ registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
+ content::NotificationService::AllSources());
return rfh->Send(message.release());
}

diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a9e5342ff 100644
index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..b99b5d0a4fce3b15484495948462ee2aa2e321c3 100644
--- a/chrome/browser/printing/print_view_manager_base.h
+++ b/chrome/browser/printing/print_view_manager_base.h
@@ -33,6 +33,8 @@ class PrintJob;
Expand All @@ -242,17 +241,19 @@ index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a
// Base class for managing the print commands for a WebContents.
class PrintViewManagerBase : public content::NotificationObserver,
public PrintManager {
@@ -42,7 +44,8 @@ class PrintViewManagerBase : public content::NotificationObserver,
@@ -42,7 +44,10 @@ class PrintViewManagerBase : public content::NotificationObserver,
// Prints the current document immediately. Since the rendering is
// asynchronous, the actual printing will not be completed on the return of
// this function. Returns false if printing is impossible at the moment.
- virtual bool PrintNow(content::RenderFrameHost* rfh);
+ virtual bool PrintNow(content::RenderFrameHost* rfh,
+ bool silent,
+ base::Value settings,
+ CompletionCallback callback);

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Prints the document in |print_data| with settings specified in
@@ -206,9 +209,15 @@ class PrintViewManagerBase : public content::NotificationObserver,
@@ -206,9 +210,15 @@ class PrintViewManagerBase : public content::NotificationObserver,
// The current RFH that is printing with a system printing dialog.
content::RenderFrameHost* printing_rfh_;

Expand All @@ -269,7 +270,7 @@ index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a
// This means we are _blocking_ until all the necessary pages have been
// rendered or the print settings are being loaded.
diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc
index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2eb48262ce 100644
index 40762a36024bc48dfe5259520161dc203197bfd0..8f2d0b808df1b68699bb946436b219315f1ea6d3 100644
--- a/chrome/browser/printing/printing_message_filter.cc
+++ b/chrome/browser/printing/printing_message_filter.cc
@@ -22,6 +22,7 @@
Expand Down Expand Up @@ -335,7 +336,7 @@ index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2e
std::unique_ptr<PrinterQuery> printer_query =
queue_->PopPrinterQuery(document_cookie);
if (!printer_query) {
@@ -258,7 +266,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
@@ -258,7 +267,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
std::unique_ptr<PrinterQuery> printer_query,
IPC::Message* reply_msg) {
PrintMsg_PrintPages_Params params;
Expand All @@ -346,7 +347,7 @@ index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2e
params.Reset();
} else {
RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
@@ -296,7 +306,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
@@ -296,7 +307,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids,
bool* cancel) {
Expand Down Expand Up @@ -381,20 +382,29 @@ index 9fbea6d0a2dbe55b1d600fbc217dee5aa8ae8cd5..de9bd267e408c02fd4da7d903523c0e6
// content::BrowserMessageFilter:
bool OnMessageReceived(const IPC::Message& message) override;
diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom
index d5ff612320fb3f324b58c91b0c05e0eac689beef..9fafde963ecce596c9514384d2aa11a7540e0d87 100644
index d5ff612320fb3f324b58c91b0c05e0eac689beef..254810ca174c456b6bf6966954e4e188c2460f15 100644
--- a/components/printing/common/print.mojom
+++ b/components/printing/common/print.mojom
@@ -16,7 +16,7 @@ interface PrintRenderer {
@@ -4,6 +4,8 @@

module printing.mojom;

+import "mojo/public/mojom/base/values.mojom";
+
// Interface implemented by a class that desires to render print documents for
// Chrome print preview.
interface PrintRenderer {
@@ -16,7 +18,7 @@ interface PrintRenderer {
interface PrintRenderFrame {
// Tells the RenderFrame to switch the CSS to print media type, render every
// requested page, and then switch back the CSS to display media type.
- PrintRequestedPages();
+ PrintRequestedPages(bool silent);
+ PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings);

// Tells the RenderFrame to switch the CSS to print media type, render every
// requested page using the print preview document's frame/node, and then
diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f7546550644a1d0 100644
index 95bf92929983110de4489ee18f52783ddf178bcf..43c6b504f60a44daedd66f7dcf7d8d0b2cf9c6e9 100644
--- a/components/printing/renderer/print_render_frame_helper.cc
+++ b/components/printing/renderer/print_render_frame_helper.cc
@@ -38,6 +38,7 @@
Expand All @@ -420,7 +430,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655
}

-void PrintRenderFrameHelper::PrintRequestedPages() {
+void PrintRenderFrameHelper::PrintRequestedPages(bool silent) {
+void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
return;
Expand All @@ -429,7 +439,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
- Print(frame, plugin, PrintRequestType::kRegular);
+ Print(frame, plugin, PrintRequestType::kRegular, silent, base::DictionaryValue());
+ Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings));
if (!render_frame_gone_)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
Expand Down Expand Up @@ -469,7 +479,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655
- PrintRequestType print_request_type) {
+ PrintRequestType print_request_type,
+ bool silent,
+ const base::DictionaryValue& settings) {
+ base::Value settings) {
// If still not finished with earlier print request simply ignore.
if (prep_frame_view_)
return;
Expand All @@ -478,7 +488,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655

int expected_page_count = 0;
- if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
+ if (!CalculateNumberOfPages(frame, node, &expected_page_count, settings)) {
+ if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) {
DidFinishPrinting(FAIL_PRINT_INIT);
return; // Failed to init print page settings.
}
Expand Down Expand Up @@ -543,15 +553,15 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655
Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
return false;
diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
index dcb0da469d5aab19bc568deb07d3619f8defa76e..9d5049cc42a8a13055fadc94e02c9feca7fe8955 100644
index dcb0da469d5aab19bc568deb07d3619f8defa76e..6ade4fcb0d9e758e69016f1b032d7d37e1a635ab 100644
--- a/components/printing/renderer/print_render_frame_helper.h
+++ b/components/printing/renderer/print_render_frame_helper.h
@@ -212,7 +212,7 @@ class PrintRenderFrameHelper
mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);

// printing::mojom::PrintRenderFrame:
- void PrintRequestedPages() override;
+ void PrintRequestedPages(bool silent) override;
+ void PrintRequestedPages(bool silent, base::Value settings) override;
void PrintForSystemDialog() override;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void InitiatePrintPreview(
Expand All @@ -562,7 +572,7 @@ index dcb0da469d5aab19bc568deb07d3619f8defa76e..9d5049cc42a8a13055fadc94e02c9fec
- PrintRequestType print_request_type);
+ PrintRequestType print_request_type,
+ bool silent,
+ const base::DictionaryValue& settings);
+ base::Value settings);

// Notification when printing is done - signal tear-down/free resources.
void DidFinishPrinting(PrintingResult result);
Expand Down

0 comments on commit 8d67f16

Please sign in to comment.