Commit e5c558f2 authored by Jesse Schettler's avatar Jesse Schettler Committed by Commit Bot

printing: Mojofy PrintMsg_* in PrintViewManager

Replace PrintMsg_ClosePrintPreviewDialog and
PrintMsg_PrintForSystemDialog IPC messages with new Mojo methods.

Bug: 1008939
Test: Print a webpage and PDF
Change-Id: I59dd3f50f01c04debe3cb0ebd51062bc6cf597be
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829467Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#705747}
parent 93621ffc
......@@ -26,7 +26,6 @@
#include "content/public/browser/web_contents.h"
#include "content/public/common/webplugininfo.h"
#include "ipc/ipc_message_macros.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
......@@ -90,9 +89,13 @@ bool PrintViewManager::PrintForSystemDialogNow(
is_switching_to_system_dialog_ = true;
SetPrintingRFH(print_preview_rfh_);
int32_t id = print_preview_rfh_->GetRoutingID();
return PrintNowInternal(print_preview_rfh_,
std::make_unique<PrintMsg_PrintForSystemDialog>(id));
// Don't print / print preview interstitials or crashed tabs.
if (IsInterstitialOrCrashed())
return false;
GetPrintRenderFrame(print_preview_rfh_)->PrintForSystemDialog();
return true;
}
bool PrintViewManager::BasicPrint(content::RenderFrameHost* rfh) {
......@@ -143,22 +146,20 @@ void PrintViewManager::PrintPreviewDone() {
if (print_preview_state_ == NOT_PREVIEWING)
return;
// Send ClosePrintPreview message for 'afterprint' event.
// Send OnPrintPreviewDialogClosed message for 'afterprint' event.
#if defined(OS_WIN)
// On Windows, we always send ClosePrintPreviewDialog. It's ok to dispatch
// On Windows, we always send OnPrintPreviewDialogClosed. It's ok to dispatch
// 'afterprint' at this timing because system dialog printing on
// Windows doesn't need the original frame.
bool send_message = true;
#else
// On non-Windows, we don't need to send ClosePrintPreviewDialog when we are
// switching to system dialog. PrintRenderFrameHelper is responsible to
// On non-Windows, we don't need to send OnPrintPreviewDialogClosed when we
// are switching to system dialog. PrintRenderFrameHelper is responsible to
// dispatch 'afterprint' event.
bool send_message = !is_switching_to_system_dialog_;
#endif
if (send_message) {
print_preview_rfh_->Send(new PrintMsg_ClosePrintPreviewDialog(
print_preview_rfh_->GetRoutingID()));
}
if (send_message)
GetPrintRenderFrame(print_preview_rfh_)->OnPrintPreviewDialogClosed();
is_switching_to_system_dialog_ = false;
if (print_preview_state_ == SCRIPTED_PREVIEW) {
......@@ -193,6 +194,21 @@ 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,
......@@ -207,10 +223,8 @@ bool PrintViewManager::PrintPreview(
if (IsInterstitialOrCrashed())
return false;
mojo::AssociatedRemote<mojom::PrintRenderFrame> print_render_frame;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame);
print_render_frame->InitiatePrintPreview(std::move(print_renderer),
has_selection);
GetPrintRenderFrame(rfh)->InitiatePrintPreview(std::move(print_renderer),
has_selection);
DCHECK(!print_preview_rfh_);
print_preview_rfh_ = rfh;
......
......@@ -9,6 +9,7 @@
#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 {
......@@ -79,6 +80,15 @@ 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
......@@ -117,6 +127,10 @@ 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);
......
......@@ -3,6 +3,7 @@
# found in the LICENSE file.
import("//mojo/public/tools/bindings/mojom.gni")
import("//printing/buildflags/buildflags.gni")
static_library("common") {
sources = [
......@@ -35,4 +36,8 @@ mojom("mojo_interfaces") {
public_deps = [
"//mojo/public/mojom/base",
]
if (enable_print_preview) {
enabled_features = [ "enable_print_preview" ]
}
}
......@@ -14,8 +14,18 @@ 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 initiate print preview for the entire
// document. Optionally provides a |print_renderer| to render print documents.
// 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.
PrintForSystemDialog();
// Tells the RenderFrame to initiate print preview for the entire document.
// Optionally provides a |print_renderer| to render print documents.
[EnableIf=enable_print_preview]
InitiatePrintPreview(associated PrintRenderer? print_renderer,
bool has_selection);
// Tells the RenderFrame that the print preview dialog was closed.
[EnableIf=enable_print_preview]
OnPrintPreviewDialogClosed();
};
......@@ -366,9 +366,6 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
// 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)
// Like PrintMsg_PrintPages, but using the print preview document's frame/node.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintForSystemDialog)
#endif
// Print content of an out-of-process subframe.
......@@ -387,9 +384,6 @@ IPC_MESSAGE_ROUTED1(PrintMsg_SetPrintingEnabled, bool /* enabled */)
// called multiple times as the user updates settings.
IPC_MESSAGE_ROUTED1(PrintMsg_PrintPreview,
base::DictionaryValue /* settings */)
// Tells the RenderFrame that print preview dialog was closed.
IPC_MESSAGE_ROUTED0(PrintMsg_ClosePrintPreviewDialog)
#endif
// Messages sent from the renderer to the browser.
......
......@@ -1155,12 +1155,9 @@ bool PrintRenderFrameHelper::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintRenderFrameHelper, message)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
IPC_MESSAGE_HANDLER(PrintMsg_ClosePrintPreviewDialog,
OnClosePrintPreviewDialog)
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_PrintFrameContent, OnPrintFrameContent)
IPC_MESSAGE_HANDLER(PrintMsg_SetPrintingEnabled, OnSetPrintingEnabled)
......@@ -1180,15 +1177,32 @@ void PrintRenderFrameHelper::OnDestruct() {
void PrintRenderFrameHelper::BindPrintRenderFrameReceiver(
mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver) {
print_render_frame_receiver_.Bind(std::move(receiver));
receivers_.Add(this, std::move(receiver));
}
void PrintRenderFrameHelper::PrintForSystemDialog() {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = print_preview_context_.source_frame();
if (!frame) {
NOTREACHED();
return;
}
auto weak_this = weak_ptr_factory_.GetWeakPtr();
Print(frame, print_preview_context_.source_node(),
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.
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::InitiatePrintPreview(
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
if (ipc_nesting_level_ > 1)
return;
......@@ -1208,9 +1222,14 @@ void PrintRenderFrameHelper::InitiatePrintPreview(
RequestPrintPreview(has_selection
? PRINT_PREVIEW_USER_INITIATED_SELECTION
: PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
}
void PrintRenderFrameHelper::OnPrintPreviewDialogClosed() {
ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr());
print_preview_context_.source_frame()->DispatchAfterPrintEvent();
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::OnPrintPages() {
if (ipc_nesting_level_ > 1)
return;
......@@ -1231,23 +1250,6 @@ void PrintRenderFrameHelper::OnPrintPages() {
// just return.
}
void PrintRenderFrameHelper::OnPrintForSystemDialog() {
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = print_preview_context_.source_frame();
if (!frame) {
NOTREACHED();
return;
}
auto weak_this = weak_ptr_factory_.GetWeakPtr();
Print(frame, print_preview_context_.source_node(),
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::GetPageSizeAndContentAreaFromPageLayout(
const PageSizeMargins& page_layout_in_points,
gfx::Size* page_size,
......@@ -1549,12 +1551,6 @@ void PrintRenderFrameHelper::OnSetPrintingEnabled(bool enabled) {
is_printing_enabled_ = enabled;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::OnClosePrintPreviewDialog() {
print_preview_context_.source_frame()->DispatchAfterPrintEvent();
}
#endif
void PrintRenderFrameHelper::OnPrintFrameContent(
const PrintMsg_PrintFrame_Params& params) {
if (ipc_nesting_level_ > 1)
......
......@@ -19,7 +19,7 @@
#include "components/printing/common/print.mojom.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "printing/buildflags/buildflags.h"
#include "printing/common/metafile_utils.h"
......@@ -212,16 +212,18 @@ class PrintRenderFrameHelper
mojo::PendingAssociatedReceiver<mojom::PrintRenderFrame> receiver);
// printing::mojom::PrintRenderFrame:
void PrintForSystemDialog() override;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void InitiatePrintPreview(
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection) override;
void OnPrintPreviewDialogClosed() override;
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Message handlers ---------------------------------------------------------
void OnPrintPages();
void OnPrintForSystemDialog();
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnPrintPreview(const base::DictionaryValue& settings);
void OnClosePrintPreviewDialog();
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnPrintFrameContent(const PrintMsg_PrintFrame_Params& params);
void OnPrintingDone(bool success);
......@@ -426,8 +428,7 @@ class PrintRenderFrameHelper
// etc.).
mojom::PrintRendererAssociatedPtr print_renderer_;
mojo::AssociatedReceiver<mojom::PrintRenderFrame>
print_render_frame_receiver_{this};
mojo::AssociatedReceiverSet<mojom::PrintRenderFrame> receivers_;
// Keeps track of the state of print preview between messages.
// TODO(vitalybuka): Create PrintPreviewContext when needed and delete after
......
......@@ -271,7 +271,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
}
void OnClosePrintPreviewDialog() {
GetPrintRenderFrameHelper()->OnClosePrintPreviewDialog();
GetPrintRenderFrameHelper()->OnPrintPreviewDialogClosed();
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
......
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