Commit 10d377be authored by Jesse Schettler's avatar Jesse Schettler Committed by Commit Bot

printing: Mojofy PrintMsg_* in PrintViewManagerBase

Replace PrintMsg_PrintingDone, PrintMsg_PrintPages, and
PrintMsg_SetPrintingEnabled IPC messages with new Mojo methods. Update
AwPrintManager and HeadlessPrintManager to also use these new methods.

Bug: 1008939
Test: Print a webpage and PDF
Change-Id: I3a403dd47ffc9c4debe819d26b00eccbd24b1180
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1876473
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarChangwan Ryu <changwan@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710576}
parent b87db175
......@@ -214,6 +214,7 @@ source_set("browser") {
"//components/prefs",
"//components/printing/browser",
"//components/printing/common",
"//components/printing/common:mojo_interfaces",
"//components/safe_browsing",
"//components/safe_browsing:features",
"//components/safe_browsing:ping_manager",
......
......@@ -72,7 +72,8 @@ void AwPrintManager::PdfWritingDone(int page_count) {
bool AwPrintManager::PrintNow() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* rfh = web_contents()->GetMainFrame();
return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID()));
GetPrintRenderFrame(rfh)->PrintRequestedPages();
return true;
}
void AwPrintManager::OnGetDefaultPrintSettings(
......
......@@ -16,7 +16,6 @@
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#include "chrome/common/chrome_content_client.h"
#include "components/printing/common/print.mojom.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/plugin_service.h"
......@@ -27,7 +26,6 @@
#include "content/public/common/webplugininfo.h"
#include "ipc/ipc_message_macros.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
using content::BrowserThread;
......@@ -194,21 +192,6 @@ void PrintViewManager::RenderFrameDeleted(
PrintViewManagerBase::RenderFrameDeleted(render_frame_host);
}
const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>&
PrintViewManager::GetPrintRenderFrame(content::RenderFrameHost* rfh) {
if (!print_render_frame_.is_bound()) {
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame_);
print_render_frame_.set_disconnect_handler(
base::BindOnce(&PrintViewManager::OnPrintRenderFrameDisconnected,
base::Unretained(this)));
}
return print_render_frame_;
}
void PrintViewManager::OnPrintRenderFrameDisconnected() {
print_render_frame_.reset();
}
bool PrintViewManager::PrintPreview(
content::RenderFrameHost* rfh,
mojom::PrintRendererAssociatedPtrInfo print_renderer,
......
......@@ -9,7 +9,6 @@
#include "chrome/browser/printing/print_view_manager_base.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "printing/buildflags/buildflags.h"
namespace content {
......@@ -80,15 +79,6 @@ class PrintViewManager : public PrintViewManagerBase,
struct FrameDispatchHelper;
// Helper method to fetch the PrintRenderFrame associated remote interface
// pointer.
const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>&
GetPrintRenderFrame(content::RenderFrameHost* rfh);
// Resets the PrintRenderFrame associated remote when it's disconnected from
// its receiver.
void OnPrintRenderFrameDisconnected();
// Helper method for PrintPreviewNow() and PrintPreviewWithRenderer().
// Initiate print preview of the current document by first notifying the
// renderer. Since this happens asynchronously, the print preview dialog
......@@ -127,10 +117,6 @@ class PrintViewManager : public PrintViewManagerBase,
// flag is true between PrintForSystemDialogNow() and PrintPreviewDone().
bool is_switching_to_system_dialog_ = false;
// Used to transmit mojom interface method calls to the PrintRenderFrame
// associated remote.
mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> print_render_frame_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(PrintViewManager);
......
......@@ -126,9 +126,13 @@ PrintViewManagerBase::~PrintViewManagerBase() {
bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
DisconnectFromCurrentPrintJob();
// Don't print / print preview interstitials or crashed tabs.
if (IsInterstitialOrCrashed())
return false;
SetPrintingRFH(rfh);
int32_t id = rfh->GetRoutingID();
return PrintNowInternal(rfh, std::make_unique<PrintMsg_PrintPages>(id));
GetPrintRenderFrame(rfh)->PrintRequestedPages();
return true;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
......@@ -611,11 +615,8 @@ void PrintViewManagerBase::ReleasePrintJob() {
if (!print_job_)
return;
if (rfh) {
auto msg = std::make_unique<PrintMsg_PrintingDone>(rfh->GetRoutingID(),
printing_succeeded_);
rfh->Send(msg.release());
}
if (rfh)
GetPrintRenderFrame(rfh)->PrintingDone(printing_succeeded_);
registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
content::Source<PrintJob>(print_job_.get()));
......@@ -723,7 +724,7 @@ void PrintViewManagerBase::ReleasePrinterQuery() {
void PrintViewManagerBase::SendPrintingEnabled(bool enabled,
content::RenderFrameHost* rfh) {
rfh->Send(new PrintMsg_SetPrintingEnabled(rfh->GetRoutingID(), enabled));
GetPrintRenderFrame(rfh)->SetPrintingEnabled(enabled);
}
} // namespace printing
......@@ -39,6 +39,7 @@ static_library("browser") {
"//components/crash/core/common",
"//components/discardable_memory/service",
"//components/printing/common",
"//components/printing/common:mojo_interfaces",
"//components/services/pdf_compositor/public/cpp",
"//components/services/pdf_compositor/public/mojom",
"//components/strings:components_strings_grit",
......
......@@ -4,9 +4,11 @@
#include "components/printing/browser/print_manager.h"
#include "base/bind.h"
#include "build/build_config.h"
#include "components/printing/common/print_messages.h"
#include "content/public/browser/render_frame_host.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
namespace printing {
......@@ -110,6 +112,19 @@ void PrintManager::OnPrintingFailed(int cookie) {
#endif
}
const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>&
PrintManager::GetPrintRenderFrame(content::RenderFrameHost* rfh) {
// When print preview is closed, the remote is disconnected from the receiver.
// Reset a disconnected remote before using it again.
if (print_render_frame_.is_bound() && !print_render_frame_.is_connected())
print_render_frame_.reset();
if (!print_render_frame_.is_bound())
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame_);
return print_render_frame_;
}
void PrintManager::PrintingRenderFrameDeleted() {
#if defined(OS_ANDROID)
PdfWritingDone(0);
......
......@@ -9,7 +9,9 @@
#include "base/macros.h"
#include "build/build_config.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/browser/web_contents_observer.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#if defined(OS_ANDROID)
#include "base/callback.h"
......@@ -39,6 +41,11 @@ class PrintManager : public content::WebContentsObserver {
protected:
explicit PrintManager(content::WebContents* contents);
// Helper method to fetch the PrintRenderFrame associated remote interface
// pointer.
const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>&
GetPrintRenderFrame(content::RenderFrameHost* rfh);
// Terminates or cancels the print job if one was pending.
void PrintingRenderFrameDeleted();
......@@ -96,6 +103,10 @@ class PrintManager : public content::WebContentsObserver {
private:
void OnDidGetDocumentCookie(int cookie);
// Used to transmit mojom interface method calls to the PrintRenderFrame
// associated remote.
mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> print_render_frame_;
DISALLOW_COPY_AND_ASSIGN(PrintManager);
};
......
......@@ -14,6 +14,10 @@ interface PrintRenderer {
// Render process interface exposed to the browser to handle most of the
// printing grunt work for RenderView.
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();
// 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
// switch the CSS back to display media type.
......@@ -28,4 +32,10 @@ interface PrintRenderFrame {
// Tells the RenderFrame that the print preview dialog was closed.
[EnableIf=enable_print_preview]
OnPrintPreviewDialogClosed();
// Tells the RenderFrame whether printing is enabled or not.
SetPrintingEnabled(bool enabled);
// Tells the RenderFrame that printing is done so it can clean up.
PrintingDone(bool success);
};
......@@ -362,22 +362,9 @@ IPC_STRUCT_END()
// node, depending on which mode the RenderFrame is in.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
#if BUILDFLAG(ENABLE_PRINTING)
// Tells the RenderFrame to switch the CSS to print media type, renders every
// requested pages and switch back the CSS to display media type.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
#endif
// Print content of an out-of-process subframe.
IPC_MESSAGE_ROUTED1(PrintMsg_PrintFrameContent, PrintMsg_PrintFrame_Params)
// Tells the RenderFrame that printing is done so it can clean up.
IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone,
bool /* success */)
// Tells the RenderFrame whether printing is enabled or not.
IPC_MESSAGE_ROUTED1(PrintMsg_SetPrintingEnabled, bool /* enabled */)
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Tells the RenderFrame to switch the CSS to print media type, renders every
// requested pages for print preview using the given |settings|. This gets
......
......@@ -1174,13 +1174,10 @@ bool PrintRenderFrameHelper::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintRenderFrameHelper, message)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_PrintFrameContent, OnPrintFrameContent)
IPC_MESSAGE_HANDLER(PrintMsg_SetPrintingEnabled, OnSetPrintingEnabled)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
......@@ -1200,6 +1197,27 @@ void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
receivers_.Add(this, std::move(receiver));
}
void PrintRenderFrameHelper::PrintRequestedPages() {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
frame->DispatchBeforePrintEvent();
// Don't print if the RenderFrame is gone.
if (render_frame_gone_)
return;
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
Print(frame, plugin, PrintRequestType::kRegular);
if (!render_frame_gone_)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
}
void PrintRenderFrameHelper::PrintForSystemDialog() {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
......@@ -1209,10 +1227,9 @@ void PrintRenderFrameHelper::PrintForSystemDialog() {
NOTREACHED();
return;
}
auto weak_this = weak_ptr_factory_.GetWeakPtr();
Print(frame, print_preview_context_.source_node(),
PrintRequestType::kRegular);
if (weak_this)
if (!render_frame_gone_)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
......@@ -1250,24 +1267,17 @@ void PrintRenderFrameHelper::OnPrintPreviewDialogClosed() {
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::OnPrintPages() {
void PrintRenderFrameHelper::PrintingDone(bool success) {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
return;
notify_browser_of_print_failure_ = false;
DidFinishPrinting(success ? OK : FAIL_PRINT);
}
auto weak_this = weak_ptr_factory_.GetWeakPtr();
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
frame->DispatchBeforePrintEvent();
if (!weak_this)
return;
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
Print(frame, plugin, PrintRequestType::kRegular);
if (weak_this)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
void PrintRenderFrameHelper::SetPrintingEnabled(bool enabled) {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
is_printing_enabled_ = enabled;
}
void PrintRenderFrameHelper::GetPageSizeAndContentAreaFromPageLayout(
......@@ -1558,19 +1568,6 @@ int PrintRenderFrameHelper::GetFitToPageScaleFactor(
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::OnPrintingDone(bool success) {
if (ipc_nesting_level_ > 1)
return;
notify_browser_of_print_failure_ = false;
if (!success)
LOG(ERROR) << "Failure in OnPrintingDone";
DidFinishPrinting(success ? OK : FAIL_PRINT);
}
void PrintRenderFrameHelper::OnSetPrintingEnabled(bool enabled) {
is_printing_enabled_ = enabled;
}
void PrintRenderFrameHelper::OnPrintFrameContent(
const PrintMsg_PrintFrame_Params& params) {
if (ipc_nesting_level_ > 1)
......
......@@ -139,7 +139,8 @@ class PrintRenderFrameHelper
friend class PrintRenderFrameHelperTestBase;
FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintRenderFrameHelperPreviewTest,
BlockScriptInitiatedPrinting);
FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintRenderFrameHelperTest, OnPrintPages);
FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintRenderFrameHelperTest,
PrintRequestedPages);
FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintRenderFrameHelperTest,
BlockScriptInitiatedPrinting);
FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintRenderFrameHelperTest,
......@@ -211,6 +212,7 @@ class PrintRenderFrameHelper
mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
// printing::mojom::PrintRenderFrame:
void PrintRequestedPages() override;
void PrintForSystemDialog() override;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void InitiatePrintPreview(
......@@ -218,14 +220,14 @@ class PrintRenderFrameHelper
bool has_selection) override;
void OnPrintPreviewDialogClosed() override;
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintingDone(bool success) override;
void SetPrintingEnabled(bool enabled) override;
// Message handlers ---------------------------------------------------------
void OnPrintPages();
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnPrintPreview(const base::DictionaryValue& settings);
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnPrintFrameContent(const PrintMsg_PrintFrame_Params& params);
void OnPrintingDone(bool success);
// Get |page_size| and |content_area| information from
// |page_layout_in_points|.
......@@ -258,9 +260,6 @@ class PrintRenderFrameHelper
int GetFitToPageScaleFactor(const gfx::Rect& printable_area_in_points);
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Enable/Disable printing.
void OnSetPrintingEnabled(bool enabled);
// Main printing code -------------------------------------------------------
// Print with the system dialog.
......
......@@ -237,7 +237,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
}
void OnPrintPages() {
GetPrintRenderFrameHelper()->OnPrintPages();
GetPrintRenderFrameHelper()->PrintRequestedPages();
base::RunLoop().RunUntilIdle();
}
......@@ -245,7 +245,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
PrintRenderFrameHelper* helper =
GetPrintRenderFrameHelperForFrame(frame_name);
ASSERT_TRUE(helper);
helper->OnPrintPages();
helper->PrintRequestedPages();
base::RunLoop().RunUntilIdle();
}
......@@ -741,11 +741,11 @@ TEST_F(MAYBE_PrintRenderFrameHelperPreviewTest, BlockScriptInitiatedPrinting) {
LoadHTML(kHelloWorldHTML);
PrintRenderFrameHelper* print_render_frame_helper =
GetPrintRenderFrameHelper();
print_render_frame_helper->OnSetPrintingEnabled(false);
print_render_frame_helper->SetPrintingEnabled(false);
PrintWithJavaScript();
VerifyPreviewRequest(false);
print_render_frame_helper->OnSetPrintingEnabled(true);
print_render_frame_helper->SetPrintingEnabled(true);
PrintWithJavaScript();
VerifyPreviewRequest(true);
}
......
......@@ -139,7 +139,7 @@ void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh,
print_params_ = GetPrintParamsFromSettings(settings);
page_ranges_text_ = settings.page_ranges;
ignore_invalid_page_ranges_ = settings.ignore_invalid_page_ranges;
rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID()));
GetPrintRenderFrame(rfh)->PrintRequestedPages();
}
std::unique_ptr<PrintMsg_PrintPages_Params>
......@@ -307,8 +307,7 @@ void HeadlessPrintManager::ReleaseJob(PrintResult result) {
std::move(callback_).Run(result,
base::MakeRefCounted<base::RefCountedString>());
}
printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(),
result == PRINT_SUCCESS));
GetPrintRenderFrame(printing_rfh_)->PrintingDone(result == PRINT_SUCCESS);
Reset();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment