Commit 55bcc073 authored by Jesse Schettler's avatar Jesse Schettler Committed by Commit Bot

printing: Mojofy PrintMsg_InitiatePrintPreview

Add a new mojo interface (printing::mojom::PrintRenderFrameHelper) and
use it to replace the PrintMsg_InitiatePrintPreview IPC message. This
also enables using the new mojo method to pass a PrintRendererPtr to the
PrintRenderFrameHelper, which will be implemented in a separate change.

Bug: b:140576300
Test: ./unit_tests --gtest_filter=PrintViewManagerTest.*
Test: Verify print preview still works
Change-Id: Icda449d9a72166d1162b95b5e08303752b2fa8f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1788233Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696633}
parent c789d28b
......@@ -4317,6 +4317,8 @@ jumbo_split_static_library("browser") {
"printing/pwg_raster_converter.h",
]
deps += [ "//components/printing/common:mojo_interfaces" ]
if (is_posix || is_fuchsia) {
sources += [ "printing/print_dialog_cloud_posix.cc" ]
}
......
......@@ -16,6 +16,7 @@
#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"
......@@ -26,6 +27,7 @@
#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;
......@@ -114,11 +116,14 @@ bool PrintViewManager::PrintPreviewNow(content::RenderFrameHost* rfh,
if (print_preview_state_ != NOT_PREVIEWING)
return false;
auto message = std::make_unique<PrintMsg_InitiatePrintPreview>(
rfh->GetRoutingID(), has_selection);
if (!PrintNowInternal(rfh, std::move(message)))
// Don't print / print preview interstitials or crashed tabs.
if (IsInterstitialOrCrashed())
return false;
mojom::PrintRenderFrameAssociatedPtr print_render_frame;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame);
print_render_frame->InitiatePrintPreview(has_selection);
DCHECK(!print_preview_rfh_);
print_preview_rfh_ = rfh;
print_preview_state_ = USER_INITIATED_PREVIEW;
......
......@@ -682,11 +682,16 @@ bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
return true;
}
bool PrintViewManagerBase::IsInterstitialOrCrashed() {
return web_contents()->ShowingInterstitialPage() ||
web_contents()->IsCrashed();
}
bool PrintViewManagerBase::PrintNowInternal(
content::RenderFrameHost* rfh,
std::unique_ptr<IPC::Message> message) {
// Don't print / print preview interstitials or crashed tabs.
if (web_contents()->ShowingInterstitialPage() || web_contents()->IsCrashed())
if (IsInterstitialOrCrashed())
return false;
return rfh->Send(message.release());
}
......
......@@ -73,6 +73,10 @@ class PrintViewManagerBase : public content::NotificationObserver,
protected:
explicit PrintViewManagerBase(content::WebContents* web_contents);
// Helper method for checking whether the WebContents is showing an
// interstitial page or is crashed.
bool IsInterstitialOrCrashed();
// Helper method for Print*Now().
bool PrintNowInternal(content::RenderFrameHost* rfh,
std::unique_ptr<IPC::Message> message);
......
......@@ -18,8 +18,9 @@
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "components/printing/common/print_messages.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/test/test_renderer_host.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
namespace printing {
......@@ -40,9 +41,14 @@ class TestPrintViewManager : public PrintViewManagerBase {
// Mostly copied from PrintViewManager::PrintPreviewNow(). We can't override
// PrintViewManager since it is a user data class.
bool PrintPreviewNow(content::RenderFrameHost* rfh, bool has_selection) {
auto msg = std::make_unique<PrintMsg_InitiatePrintPreview>(
rfh->GetRoutingID(), has_selection);
return PrintNowInternal(rfh, std::move(msg));
// Don't print / print preview interstitials or crashed tabs.
if (IsInterstitialOrCrashed())
return false;
mojom::PrintRenderFrameAssociatedPtr print_render_frame;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame);
print_render_frame->InitiatePrintPreview(has_selection);
return true;
}
// Getters for validating arguments to StartPdf...Conversion functions
......
......@@ -10,3 +10,11 @@ interface PrintRenderer {
// TODO(jschettler): Add methods to render a print document and signal the
// close of Chrome print preview.
};
// 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.
InitiatePrintPreview(bool has_selection);
};
......@@ -358,11 +358,6 @@ IPC_STRUCT_END()
// Messages sent from the browser to the renderer.
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Tells the RenderFrame to initiate print preview for the entire document.
IPC_MESSAGE_ROUTED1(PrintMsg_InitiatePrintPreview, bool /* has_selection */)
#endif
// Tells the RenderFrame to initiate printing or print preview for a particular
// node, depending on which mode the RenderFrame is in.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
......
......@@ -12,6 +12,7 @@ static_library("renderer") {
deps = [
"//base",
"//components/printing/common",
"//components/printing/common:mojo_interfaces",
"//components/resources",
"//content/public/common",
"//content/public/renderer",
......
......@@ -39,6 +39,7 @@
#include "printing/buildflags/buildflags.h"
#include "printing/metafile_skia.h"
#include "printing/units.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
#include "third_party/blink/public/common/frame/sandbox_flags.h"
#include "third_party/blink/public/mojom/frame/document_interface_broker.mojom.h"
......@@ -1072,9 +1073,14 @@ PrintRenderFrameHelper::PrintRenderFrameHelper(
std::unique_ptr<Delegate> delegate)
: content::RenderFrameObserver(render_frame),
content::RenderFrameObserverTracker<PrintRenderFrameHelper>(render_frame),
delegate_(std::move(delegate)) {
delegate_(std::move(delegate)),
print_render_frame_binding_(this) {
if (!delegate_->IsPrintPreviewEnabled())
DisablePreview();
render_frame->GetAssociatedInterfaceRegistry()->AddInterface(
base::BindRepeating(&PrintRenderFrameHelper::OnPrintRenderFrameRequest,
weak_ptr_factory_.GetWeakPtr()));
}
PrintRenderFrameHelper::~PrintRenderFrameHelper() {}
......@@ -1164,7 +1170,6 @@ bool PrintRenderFrameHelper::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
IPC_MESSAGE_HANDLER(PrintMsg_ClosePrintPreviewDialog,
......@@ -1189,6 +1194,32 @@ void PrintRenderFrameHelper::OnDestruct() {
delete this;
}
void PrintRenderFrameHelper::OnPrintRenderFrameRequest(
mojom::PrintRenderFrameAssociatedRequest request) {
print_render_frame_binding_.Bind(std::move(request));
}
void PrintRenderFrameHelper::InitiatePrintPreview(bool has_selection) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
if (!plugin.IsNull()) {
PrintNode(plugin);
return;
}
print_preview_context_.InitWithFrame(frame);
RequestPrintPreview(has_selection
? PRINT_PREVIEW_USER_INITIATED_SELECTION
: PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
}
void PrintRenderFrameHelper::OnPrintPages() {
if (ipc_nesting_level_ > 1)
return;
......@@ -1529,25 +1560,6 @@ void PrintRenderFrameHelper::OnSetPrintingEnabled(bool enabled) {
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintRenderFrameHelper::OnInitiatePrintPreview(bool has_selection) {
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
if (!plugin.IsNull()) {
PrintNode(plugin);
return;
}
print_preview_context_.InitWithFrame(frame);
RequestPrintPreview(has_selection
? PRINT_PREVIEW_USER_INITIATED_SELECTION
: PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
}
void PrintRenderFrameHelper::OnClosePrintPreviewDialog() {
print_preview_context_.source_frame()->DispatchAfterPrintEvent();
}
......
......@@ -16,8 +16,10 @@
#include "base/time/time.h"
#include "build/build_config.h"
#include "cc/paint/paint_canvas.h"
#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_binding.h"
#include "printing/buildflags/buildflags.h"
#include "printing/common/metafile_utils.h"
#include "third_party/blink/public/web/web_node.h"
......@@ -86,7 +88,8 @@ class FrameReference {
// copying the DOM of the document and creating a new WebView with the contents.
class PrintRenderFrameHelper
: public content::RenderFrameObserver,
public content::RenderFrameObserverTracker<PrintRenderFrameHelper> {
public content::RenderFrameObserverTracker<PrintRenderFrameHelper>,
public mojom::PrintRenderFrame {
public:
class Delegate {
public:
......@@ -192,11 +195,16 @@ class PrintRenderFrameHelper
void ScriptedPrint(bool user_initiated) override;
bool OnMessageReceived(const IPC::Message& message) override;
void OnPrintRenderFrameRequest(
mojom::PrintRenderFrameAssociatedRequest request);
// printing::mojom::PrintRenderFrame:
void InitiatePrintPreview(bool has_selection) override;
// Message handlers ---------------------------------------------------------
void OnPrintPages();
void OnPrintForSystemDialog();
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnInitiatePrintPreview(bool has_selection);
void OnPrintPreview(const base::DictionaryValue& settings);
void OnClosePrintPreviewDialog();
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
......@@ -393,6 +401,8 @@ class PrintRenderFrameHelper
// Used to check the prerendering status.
const std::unique_ptr<Delegate> delegate_;
mojo::AssociatedBinding<mojom::PrintRenderFrame> print_render_frame_binding_;
// Keeps track of the state of print preview between messages.
// TODO(vitalybuka): Create PrintPreviewContext when needed and delete after
// use. Now it's interaction with various messages is confusing.
......
......@@ -261,7 +261,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
void OnPrintPreview(const base::DictionaryValue& dict) {
PrintRenderFrameHelper* print_render_frame_helper =
GetPrintRenderFrameHelper();
print_render_frame_helper->OnInitiatePrintPreview(false);
print_render_frame_helper->InitiatePrintPreview(false);
base::RunLoop run_loop;
DidPreviewPageListener filter(&run_loop);
render_thread_->sink().AddFilter(&filter);
......
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