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

arc: Send PrintRendererAssociatedPtr to PrintRenderFrameHelper

Send a PrintRendererAssociatedPtr to PrintRenderFrameHelper. When
present, the associated interface pointer will be used to render print
documents. An additional change will be made to perform the rendering.

        PrintViewManagerTest.*:PrintPreviewHandlerTest.*: \
        PrintPreviewUIUnitTest.*

Bug: b:140576300
Test: browser_tests --gtest_filter=PrintBrowserTest.*
Test: unit_tests --gtest_filter=PrintPreviewDialogControllerUnitTest.*: \
Change-Id: Id344d85cc1113f40a93a25c756290246905c4d3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1789537Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarYusuke Sato <yusukes@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696986}
parent d1ccba88
......@@ -25,15 +25,15 @@
namespace arc {
// static
arc::mojom::PrintSessionHostPtr PrintSessionImpl::Create(
mojom::PrintSessionHostPtr PrintSessionImpl::Create(
std::unique_ptr<content::WebContents> web_contents,
std::unique_ptr<ash::ArcCustomTab> custom_tab,
arc::mojom::PrintSessionInstancePtr instance) {
mojom::PrintSessionInstancePtr instance) {
if (!custom_tab || !instance)
return nullptr;
// This object will be deleted when the mojo connection is closed.
arc::mojom::PrintSessionHostPtr ptr;
mojom::PrintSessionHostPtr ptr;
new PrintSessionImpl(std::move(web_contents), std::move(custom_tab),
std::move(instance), mojo::MakeRequest(&ptr));
return ptr;
......@@ -42,13 +42,14 @@ arc::mojom::PrintSessionHostPtr PrintSessionImpl::Create(
PrintSessionImpl::PrintSessionImpl(
std::unique_ptr<content::WebContents> web_contents,
std::unique_ptr<ash::ArcCustomTab> custom_tab,
arc::mojom::PrintSessionInstancePtr instance,
arc::mojom::PrintSessionHostRequest request)
mojom::PrintSessionInstancePtr instance,
mojom::PrintSessionHostRequest request)
: ArcCustomTabModalDialogHost(std::move(custom_tab),
std::move(web_contents)),
binding_(this, std::move(request)),
instance_(std::move(instance)) {
binding_.set_connection_error_handler(
instance_(std::move(instance)),
print_renderer_binding_(this),
session_binding_(this, std::move(request)) {
session_binding_.set_connection_error_handler(
base::BindOnce(&PrintSessionImpl::Close, weak_ptr_factory_.GetWeakPtr()));
web_contents_->SetUserData(UserDataKey(), base::WrapUnique(this));
......@@ -86,7 +87,10 @@ void PrintSessionImpl::OnPrintPreviewClosed() {
}
void PrintSessionImpl::StartPrintAfterDelay() {
printing::StartPrint(web_contents_.get(), false, false);
printing::mojom::PrintRendererAssociatedPtrInfo print_renderer_ptr_info;
print_renderer_binding_.Bind(mojo::MakeRequest(&print_renderer_ptr_info));
printing::StartPrint(web_contents_.get(), std::move(print_renderer_ptr_info),
false, false);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintSessionImpl)
......
......@@ -13,6 +13,7 @@
#include "components/arc/mojom/print_spooler.mojom.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "mojo/public/cpp/bindings/binding.h"
namespace ash {
......@@ -27,15 +28,15 @@ namespace arc {
// Implementation of PrintSessionHost interface. Also used by other classes to
// send print-related messages to ARC.
class PrintSessionImpl : public arc::mojom::PrintSessionHost,
class PrintSessionImpl : public mojom::PrintSessionHost,
public ArcCustomTabModalDialogHost,
public content::WebContentsUserData<PrintSessionImpl>,
public printing::mojom::PrintRenderer {
public:
static arc::mojom::PrintSessionHostPtr Create(
static mojom::PrintSessionHostPtr Create(
std::unique_ptr<content::WebContents> web_contents,
std::unique_ptr<ash::ArcCustomTab> custom_tab,
arc::mojom::PrintSessionInstancePtr instance);
mojom::PrintSessionInstancePtr instance);
~PrintSessionImpl() override;
......@@ -45,8 +46,8 @@ class PrintSessionImpl : public arc::mojom::PrintSessionHost,
private:
PrintSessionImpl(std::unique_ptr<content::WebContents> web_contents,
std::unique_ptr<ash::ArcCustomTab> custom_tab,
arc::mojom::PrintSessionInstancePtr instance,
arc::mojom::PrintSessionHostRequest request);
mojom::PrintSessionInstancePtr instance,
mojom::PrintSessionHostRequest request);
friend class content::WebContentsUserData<PrintSessionImpl>;
// Used to close the ARC Custom Tab used for printing. If the remote end
......@@ -57,12 +58,16 @@ class PrintSessionImpl : public arc::mojom::PrintSessionHost,
// Opens Chrome print preview after waiting for the PDF plugin to load.
void StartPrintAfterDelay();
// Used to send messages to ARC and request a new print document.
mojom::PrintSessionInstancePtr instance_;
// Binding for PrintRenderer.
mojo::AssociatedBinding<printing::mojom::PrintRenderer>
print_renderer_binding_;
// Used to bind the PrintSessionHost interface implementation to a message
// pipe.
mojo::Binding<arc::mojom::PrintSessionHost> binding_;
// Used to send messages to ARC and request a new print document.
arc::mojom::PrintSessionInstancePtr instance_;
mojo::Binding<mojom::PrintSessionHost> session_binding_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
......
......@@ -195,6 +195,7 @@ class PrintBrowserTest : public InProcessBrowserTest {
PrintPreviewObserver print_preview_observer;
StartPrint(browser()->tab_strip_model()->GetActiveWebContents(),
/*print_renderer=*/nullptr,
/*print_preview_disabled=*/false, print_only_selection);
print_preview_observer.WaitUntilPreviewIsReady();
......@@ -281,6 +282,7 @@ class PrintExtensionBrowserTest : public extensions::ExtensionBrowserTest {
PrintPreviewObserver print_preview_observer;
StartPrint(browser()->tab_strip_model()->GetActiveWebContents(),
/*print_renderer=*/nullptr,
/*print_preview_disabled=*/false, print_only_selection);
print_preview_observer.WaitUntilPreviewIsReady();
......
......@@ -110,24 +110,13 @@ bool PrintViewManager::BasicPrint(content::RenderFrameHost* rfh) {
bool PrintViewManager::PrintPreviewNow(content::RenderFrameHost* rfh,
bool has_selection) {
// Users can send print commands all they want and it is beyond
// PrintViewManager's control. Just ignore the extra commands.
// See http://crbug.com/136842 for example.
if (print_preview_state_ != NOT_PREVIEWING)
return false;
// 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 PrintPreview(rfh, nullptr, has_selection);
}
DCHECK(!print_preview_rfh_);
print_preview_rfh_ = rfh;
print_preview_state_ = USER_INITIATED_PREVIEW;
return true;
bool PrintViewManager::PrintPreviewWithPrintRenderer(
content::RenderFrameHost* rfh,
mojom::PrintRendererAssociatedPtrInfo print_renderer) {
return PrintPreview(rfh, std::move(print_renderer), false);
}
void PrintViewManager::PrintPreviewForWebNode(content::RenderFrameHost* rfh) {
......@@ -203,6 +192,31 @@ void PrintViewManager::RenderFrameDeleted(
PrintViewManagerBase::RenderFrameDeleted(render_frame_host);
}
bool PrintViewManager::PrintPreview(
content::RenderFrameHost* rfh,
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection) {
// Users can send print commands all they want and it is beyond
// PrintViewManager's control. Just ignore the extra commands.
// See http://crbug.com/136842 for example.
if (print_preview_state_ != NOT_PREVIEWING)
return false;
// 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(std::move(print_renderer),
has_selection);
DCHECK(!print_preview_rfh_);
print_preview_rfh_ = rfh;
print_preview_state_ = USER_INITIATED_PREVIEW;
return true;
}
void PrintViewManager::OnDidShowPrintDialog(content::RenderFrameHost* rfh) {
if (rfh != print_preview_rfh_)
return;
......
......@@ -7,6 +7,7 @@
#include "base/macros.h"
#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 "printing/buildflags/buildflags.h"
......@@ -33,12 +34,17 @@ class PrintViewManager : public PrintViewManagerBase,
// preview dialog.
bool BasicPrint(content::RenderFrameHost* rfh);
// Initiate print preview of the current document by first notifying the
// renderer. Since this happens asynchronous, the print preview dialog
// creation will not be completed on the return of this function. Returns
// false if print preview is impossible at the moment.
// Initiate print preview of the current document and specify whether a
// selection or the entire frame is being printed.
bool PrintPreviewNow(content::RenderFrameHost* rfh, bool has_selection);
// Initiate print preview of the current document and provide the renderer
// a printing::mojom::PrintRenderer to perform the actual rendering of
// the print document.
bool PrintPreviewWithPrintRenderer(
content::RenderFrameHost* rfh,
mojom::PrintRendererAssociatedPtrInfo print_renderer);
// Notify PrintViewManager that print preview is starting in the renderer for
// a particular WebNode.
void PrintPreviewForWebNode(content::RenderFrameHost* rfh);
......@@ -73,6 +79,15 @@ class PrintViewManager : public PrintViewManagerBase,
struct FrameDispatchHelper;
// 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
// creation will not be completed on the return of this function. Returns
// false if print preview is impossible at the moment.
bool PrintPreview(content::RenderFrameHost* rfh,
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection);
// IPC Message handlers.
void OnDidShowPrintDialog(content::RenderFrameHost* rfh);
void OnSetupScriptedPrintPreview(content::RenderFrameHost* rfh,
......
......@@ -50,6 +50,7 @@ content::RenderFrameHost* GetRenderFrameHostToUse(
} // namespace
void StartPrint(content::WebContents* contents,
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool print_preview_disabled,
bool has_selection) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
......@@ -71,7 +72,12 @@ void StartPrint(content::WebContents* contents,
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (!print_preview_disabled) {
print_view_manager->PrintPreviewNow(rfh_to_use, has_selection);
if (print_renderer) {
print_view_manager->PrintPreviewWithPrintRenderer(
rfh_to_use, std::move(print_renderer));
} else {
print_view_manager->PrintPreviewNow(rfh_to_use, has_selection);
}
return;
}
#endif // ENABLE_PRINT_PREVIEW
......
......@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_COMMON_H_
#define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_COMMON_H_
#include "components/printing/common/print.mojom.h"
#include "printing/buildflags/buildflags.h"
namespace content {
......@@ -15,7 +16,10 @@ class WebContents;
namespace printing {
// Start printing using the appropriate PrintViewManagerBase subclass.
// Optionally provide a printing::mojom::PrintRenderer to render print
// documents.
void StartPrint(content::WebContents* web_contents,
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool print_preview_disabled,
bool has_selection);
......
......@@ -47,7 +47,7 @@ class TestPrintViewManager : public PrintViewManagerBase {
mojom::PrintRenderFrameAssociatedPtr print_render_frame;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&print_render_frame);
print_render_frame->InitiatePrintPreview(has_selection);
print_render_frame->InitiatePrintPreview(nullptr, has_selection);
return true;
}
......
......@@ -2934,7 +2934,7 @@ void RenderViewContextMenu::ExecPrint() {
}
printing::StartPrint(
source_web_contents_,
source_web_contents_, nullptr,
GetPrefs(browser_context_)->GetBoolean(prefs::kPrintPreviewDisabled),
!params_.selection_text.empty());
#endif // BUILDFLAG(ENABLE_PRINTING)
......
......@@ -971,7 +971,7 @@ void Print(Browser* browser) {
#if BUILDFLAG(ENABLE_PRINTING)
auto* web_contents = browser->tab_strip_model()->GetActiveWebContents();
printing::StartPrint(
web_contents,
web_contents, nullptr /* print_renderer */,
browser->profile()->GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled),
false /* has_selection? */);
#endif
......
......@@ -15,6 +15,7 @@ interface PrintRenderer {
// printing grunt work for RenderView.
interface PrintRenderFrame {
// Tells the RenderFrame to initiate print preview for the entire
// document.
InitiatePrintPreview(bool has_selection);
// document. Optionally provides a |print_renderer| to render print documents.
InitiatePrintPreview(associated PrintRenderer? print_renderer,
bool has_selection);
};
......@@ -1198,11 +1198,16 @@ void PrintRenderFrameHelper::OnPrintRenderFrameRequest(
print_render_frame_binding_.Bind(std::move(request));
}
void PrintRenderFrameHelper::InitiatePrintPreview(bool has_selection) {
void PrintRenderFrameHelper::InitiatePrintPreview(
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (ipc_nesting_level_ > 1)
return;
if (print_renderer)
print_renderer_.Bind(std::move(print_renderer));
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
// If we are printing a PDF extension frame, find the plugin node and print
......
......@@ -199,7 +199,9 @@ class PrintRenderFrameHelper
mojom::PrintRenderFrameAssociatedRequest request);
// printing::mojom::PrintRenderFrame:
void InitiatePrintPreview(bool has_selection) override;
void InitiatePrintPreview(
mojom::PrintRendererAssociatedPtrInfo print_renderer,
bool has_selection) override;
// Message handlers ---------------------------------------------------------
void OnPrintPages();
......@@ -401,6 +403,10 @@ class PrintRenderFrameHelper
// Used to check the prerendering status.
const std::unique_ptr<Delegate> delegate_;
// Used to render print documents from an external source (ARC, Crostini,
// etc.).
mojom::PrintRendererAssociatedPtr print_renderer_;
mojo::AssociatedBinding<mojom::PrintRenderFrame> print_render_frame_binding_;
// Keeps track of the state of print preview between messages.
......
......@@ -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->InitiatePrintPreview(false);
print_render_frame_helper->InitiatePrintPreview(nullptr, 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