Commit ac7fed6f authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Chromium LUCI CQ

[printing] Convert PrintHostMsg_SetupScriptedPrintPreview to Mojo

This CL converts PrintHostMsg_SetupScriptedPrintPreview message to
SetupScriptedPrintPreview() in mojom::PrintManagerHost.

Bug: 1008939
Change-Id: I52a9a299f975e56f33cdfef65c8be8bfc65bb9dd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2607979
Commit-Queue: Julie Kim <jkim@igalia.com>
Reviewed-by: default avatarDave Tapuska <dtapuska@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846003}
parent 6964b8a4
......@@ -71,17 +71,6 @@ content::WebContents* GetPrintPreviewDialog(
} // namespace
struct PrintViewManager::FrameDispatchHelper {
PrintViewManager* manager;
content::RenderFrameHost* render_frame_host;
bool Send(IPC::Message* msg) { return render_frame_host->Send(msg); }
void OnSetupScriptedPrintPreview(IPC::Message* reply_msg) {
manager->OnSetupScriptedPrintPreview(render_frame_host, reply_msg);
}
};
PrintViewManager::PrintViewManager(content::WebContents* web_contents)
: PrintViewManagerBase(web_contents) {
if (PrintPreviewDialogController::IsPrintPreviewURL(web_contents->GetURL())) {
......@@ -262,43 +251,46 @@ void PrintViewManager::DidShowPrintDialog() {
std::move(on_print_dialog_shown_callback_).Run();
}
void PrintViewManager::OnSetupScriptedPrintPreview(
content::RenderFrameHost* rfh,
IPC::Message* reply_msg) {
void PrintViewManager::SetupScriptedPrintPreview(
SetupScriptedPrintPreviewCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto& map = g_scripted_print_preview_closure_map.Get();
content::RenderFrameHost* rfh =
print_manager_host_receivers_.GetCurrentTargetFrame();
content::RenderProcessHost* rph = rfh->GetProcess();
if (base::Contains(map, rph)) {
// Renderer already handling window.print(). Abort this attempt to prevent
// the renderer from having multiple nested loops. If multiple nested loops
// existed, then they have to exit in the right order and that is messy.
rfh->Send(reply_msg);
std::move(callback).Run();
return;
}
if (print_preview_state_ != NOT_PREVIEWING) {
// If a print dialog is already open for this tab, ignore the scripted print
// message.
rfh->Send(reply_msg);
std::move(callback).Run();
return;
}
PrintPreviewDialogController* dialog_controller =
PrintPreviewDialogController::GetInstance();
if (!dialog_controller) {
rfh->Send(reply_msg);
std::move(callback).Run();
return;
}
if (RejectPrintPreviewRequestIfRestricted(rfh))
if (RejectPrintPreviewRequestIfRestricted(rfh)) {
std::move(callback).Run();
return;
}
DCHECK(!print_preview_rfh_);
print_preview_rfh_ = rfh;
print_preview_state_ = SCRIPTED_PREVIEW;
map[rph] = base::BindOnce(&PrintViewManager::OnScriptedPrintPreviewReply,
base::Unretained(this), reply_msg);
base::Unretained(this), std::move(callback));
scripted_print_preview_rph_ = rph;
DCHECK(!scripted_print_preview_rph_set_blocked_);
if (!scripted_print_preview_rph_->IsBlocked()) {
......@@ -358,25 +350,10 @@ void PrintViewManager::CheckForCancel(int32_t preview_ui_id,
PrintPreviewUI::ShouldCancelRequest(preview_ui_id, request_id));
}
void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) {
void PrintViewManager::OnScriptedPrintPreviewReply(
SetupScriptedPrintPreviewCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
print_preview_rfh_->Send(reply_msg);
}
bool PrintViewManager::OnMessageReceived(
const IPC::Message& message,
content::RenderFrameHost* render_frame_host) {
FrameDispatchHelper helper = {this, render_frame_host};
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManager, message, render_frame_host)
IPC_MESSAGE_FORWARD_DELAY_REPLY(
PrintHostMsg_SetupScriptedPrintPreview, &helper,
FrameDispatchHelper::OnSetupScriptedPrintPreview)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled ||
PrintViewManagerBase::OnMessageReceived(message, render_frame_host);
std::move(callback).Run();
}
void PrintViewManager::MaybeUnblockScriptedPreviewRPH() {
......
......@@ -60,6 +60,8 @@ class PrintViewManager : public PrintViewManagerBase,
// mojom::PrintManagerHost:
void DidShowPrintDialog() override;
void SetupScriptedPrintPreview(
SetupScriptedPrintPreviewCallback callback) override;
void ShowScriptedPrintPreview(bool source_is_modifiable) override;
void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override;
void CheckForCancel(int32_t preview_ui_id,
......@@ -69,8 +71,6 @@ class PrintViewManager : public PrintViewManagerBase,
// content::WebContentsObserver implementation.
void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
bool OnMessageReceived(const IPC::Message& message,
content::RenderFrameHost* render_frame_host) override;
content::RenderFrameHost* print_preview_rfh() { return print_preview_rfh_; }
......@@ -86,8 +86,6 @@ class PrintViewManager : public PrintViewManagerBase,
SCRIPTED_PREVIEW,
};
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
......@@ -98,10 +96,7 @@ class PrintViewManager : public PrintViewManagerBase,
mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer,
bool has_selection);
// IPC Message handlers.
void OnSetupScriptedPrintPreview(content::RenderFrameHost* rfh,
IPC::Message* reply_msg);
void OnScriptedPrintPreviewReply(IPC::Message* reply_msg);
void OnScriptedPrintPreviewReply(SetupScriptedPrintPreviewCallback callback);
void MaybeUnblockScriptedPreviewRPH();
......
......@@ -67,6 +67,11 @@ void PrintManager::PrintingFailed(int32_t cookie) {
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintManager::SetupScriptedPrintPreview(
SetupScriptedPrintPreviewCallback callback) {
std::move(callback).Run();
}
void PrintManager::ShowScriptedPrintPreview(bool source_is_modifiable) {}
void PrintManager::RequestPrintPreview(
......
......@@ -57,6 +57,8 @@ class PrintManager : public content::WebContentsObserver,
void ShowInvalidPrinterSettingsError() override;
void PrintingFailed(int32_t cookie) override;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void SetupScriptedPrintPreview(
SetupScriptedPrintPreviewCallback callback) override;
void ShowScriptedPrintPreview(bool source_is_modifiable) override;
void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override;
void CheckForCancel(int32_t preview_ui_id,
......
......@@ -330,6 +330,12 @@ interface PrintManagerHost {
// Tells the browser printing failed.
PrintingFailed(int32 cookie);
// Tells the browser to set up the print preview requested by the script. It
// runs a nested run loop in the renderer until print preview for
// window.print() finishes.
[EnableIf=enable_print_preview, Sync]
SetupScriptedPrintPreview() => ();
// Tells the browser to show the print preview, when the document is
// sufficiently loaded such that the renderer can determine whether it is
// modifiable or not.
......
......@@ -265,9 +265,6 @@ IPC_MESSAGE_ROUTED2(PrintHostMsg_MetafileReadyForPrinting,
printing::mojom::DidPreviewDocumentParams /* params */,
printing::mojom::PreviewIds /* ids */)
// Run a nested run loop in the renderer until print preview for
// window.print() finishes.
IPC_SYNC_MESSAGE_ROUTED0_0(PrintHostMsg_SetupScriptedPrintPreview)
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
#endif // COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
......@@ -2400,8 +2400,8 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) {
switch (type) {
case PRINT_PREVIEW_SCRIPTED: {
// Shows scripted print preview in two stages.
// 1. PrintHostMsg_SetupScriptedPrintPreview blocks this call and JS by
// pumping messages here.
// 1. SetupScriptedPrintPreview() blocks this call and JS by running a
// nested run loop.
// 2. ShowScriptedPrintPreview() shows preview once the document has been
// loaded.
is_scripted_preview_delayed_ = true;
......@@ -2418,11 +2418,11 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) {
base::BindOnce(&PrintRenderFrameHelper::ShowScriptedPrintPreview,
weak_ptr_factory_.GetWeakPtr()));
}
auto msg = std::make_unique<PrintHostMsg_SetupScriptedPrintPreview>(
routing_id());
msg->EnableMessagePumping();
auto self = weak_ptr_factory_.GetWeakPtr();
Send(msg.release());
base::RunLoop loop{base::RunLoop::Type::kNestableTasksAllowed};
GetPrintManagerHost()->SetupScriptedPrintPreview(loop.QuitClosure());
loop.Run();
// Check if |this| is still valid.
if (self)
is_scripted_preview_delayed_ = false;
......
......@@ -229,7 +229,7 @@ class TestPrintManagerHost
}
~TestPrintManagerHost() override = default;
// mojom::PrintManagerInterceptorForTesting
// mojom::PrintManagerHostInterceptorForTesting
mojom::PrintManagerHost* GetForwardingInterface() override { return nullptr; }
void DidGetPrintedPagesCount(int32_t cookie, uint32_t number_pages) override {
if (number_pages_ > 0)
......@@ -348,6 +348,11 @@ class TestPrintManagerHost
std::move(callback).Run(std::move(settings));
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void SetupScriptedPrintPreview(
SetupScriptedPrintPreviewCallback callback) override {
is_setup_scripted_print_preview_ = true;
std::move(callback).Run();
}
void ShowScriptedPrintPreview(bool source_is_modifiable) override {}
void RequestPrintPreview(
mojom::RequestPrintPreviewParamsPtr params) override {}
......@@ -358,6 +363,12 @@ class TestPrintManagerHost
}
#endif
bool IsSetupScriptedPrintPreview() {
return is_setup_scripted_print_preview_;
}
void ResetSetupScriptedPrintPreview() {
is_setup_scripted_print_preview_ = false;
}
bool IsPrinted() { return is_printed_; }
void SetExpectedPagesCount(uint32_t number_pages) {
number_pages_ = number_pages;
......@@ -394,6 +405,7 @@ class TestPrintManagerHost
}
uint32_t number_pages_ = 0;
bool is_setup_scripted_print_preview_ = false;
bool is_printed_ = false;
MockPrinter* printer_;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
......@@ -452,6 +464,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
void ClearPrintManagerHost() { frame_to_print_manager_map_.clear(); }
void PrintWithJavaScript() {
print_manager()->ResetSetupScriptedPrintPreview();
ExecuteJavaScriptForTests("window.print();");
base::RunLoop().RunUntilIdle();
}
......@@ -509,11 +522,7 @@ class PrintRenderFrameHelperTestBase : public content::RenderViewTest {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void VerifyPreviewRequest(bool expect_request) {
const IPC::Message* print_msg =
render_thread_->sink().GetUniqueMessageMatching(
PrintHostMsg_SetupScriptedPrintPreview::ID);
bool got_preview_request = !!print_msg;
EXPECT_EQ(expect_request, got_preview_request);
EXPECT_EQ(expect_request, print_manager()->IsSetupScriptedPrintPreview());
}
void OnPrintPreview(const base::DictionaryValue& dict) {
......
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