Commit ee20aa9a authored by sgurun's avatar sgurun Committed by Commit bot

Remove the discrepencies between chrome and android_webview print

messages to enable componentizing printing. Basically, we have
modified two print messages
PrintHostMsg_AllocateTempFileForPrinting
PrintHostMsg_TempFileForPrintingWritten

to a routed message rather then a control message to simplify
implementation. However, this prevents moving the code to components.

BUG=446509

Review URL: https://codereview.chromium.org/854513002

Cr-Commit-Position: refs/heads/master@{#311544}
parent 5a5bf024
......@@ -158,6 +158,8 @@
'browser/aw_login_delegate.h',
'browser/aw_pref_store.cc',
'browser/aw_pref_store.h',
'browser/aw_printing_message_filter.cc',
'browser/aw_printing_message_filter.h',
'browser/aw_quota_manager_bridge.cc',
'browser/aw_quota_manager_bridge.h',
'browser/aw_quota_permission_context.cc',
......
......@@ -11,6 +11,7 @@
#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "android_webview/browser/aw_cookie_access_policy.h"
#include "android_webview/browser/aw_dev_tools_manager_delegate.h"
#include "android_webview/browser/aw_printing_message_filter.h"
#include "android_webview/browser/aw_quota_permission_context.h"
#include "android_webview/browser/aw_web_preferences_populater.h"
#include "android_webview/browser/jni_dependency_factory.h"
......@@ -211,6 +212,7 @@ void AwContentBrowserClient::RenderProcessWillLaunch(
host->AddFilter(new AwContentsMessageFilter(host->GetID()));
host->AddFilter(new cdm::CdmMessageFilterAndroid());
host->AddFilter(new AwPrintingMessageFilter(host->GetID()));
}
net::URLRequestContextGetter* AwContentBrowserClient::CreateRequestContext(
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/aw_printing_message_filter.h"
#include "android_webview/browser/renderer_host/print_manager.h"
#include "android_webview/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
using content::BrowserThread;
using content::WebContents;
namespace android_webview {
namespace {
PrintManager* GetPrintManager(int render_process_id, int render_view_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::RenderViewHost* rvh = content::RenderViewHost::FromID(
render_process_id, render_view_id);
if (rvh == nullptr)
return nullptr;
WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
if (web_contents == nullptr)
return nullptr;
return PrintManager::FromWebContents(web_contents);
}
} // namespace
AwPrintingMessageFilter::AwPrintingMessageFilter(int render_process_id)
: BrowserMessageFilter(PrintMsgStart),
render_process_id_(render_process_id) {
}
AwPrintingMessageFilter::~AwPrintingMessageFilter() {
}
void AwPrintingMessageFilter::OverrideThreadForMessage(
const IPC::Message& message, BrowserThread::ID* thread) {
if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID ||
message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) {
*thread = BrowserThread::UI;
}
}
bool AwPrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(AwPrintingMessageFilter, message)
IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
OnAllocateTempFileForPrinting)
IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
OnTempFileForPrintingWritten)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void AwPrintingMessageFilter::OnAllocateTempFileForPrinting(
int render_view_id,
base::FileDescriptor* temp_file_fd,
int* sequence_number) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
PrintManager* print_manager =
GetPrintManager(render_process_id_, render_view_id);
if (print_manager == nullptr)
return;
print_manager->OnAllocateTempFileForPrinting(temp_file_fd, sequence_number);
}
void AwPrintingMessageFilter::OnTempFileForPrintingWritten(
int render_view_id,
int sequence_number) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
PrintManager* print_manager =
GetPrintManager(render_process_id_, render_view_id);
if (print_manager == nullptr)
return;
print_manager->OnTempFileForPrintingWritten(sequence_number);
}
} // namespace android_webview
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ANDROID_WEBVIEW_BROWSER_PRINTING_MESSAGE_FILTER_H_
#define ANDROID_WEBVIEW_BROWSER_PRINTING_MESSAGE_FILTER_H_
#include "content/public/browser/browser_message_filter.h"
namespace android_webview {
// This class filters out incoming printing related IPC messages for the
// renderer process on the IPC thread.
class AwPrintingMessageFilter : public content::BrowserMessageFilter {
public:
AwPrintingMessageFilter(int render_process_id);
// content::BrowserMessageFilter methods.
void OverrideThreadForMessage(const IPC::Message& message,
content::BrowserThread::ID* thread) override;
bool OnMessageReceived(const IPC::Message& message) override;
private:
~AwPrintingMessageFilter() override;
// Used to ask the browser allocate a temporary file for the renderer
// to fill in resulting PDF in renderer.
void OnAllocateTempFileForPrinting(int render_view_id,
base::FileDescriptor* temp_file_fd,
int* sequence_number);
void OnTempFileForPrintingWritten(int render_view_id, int sequence_number);
const int render_process_id_;
DISALLOW_COPY_AND_ASSIGN(AwPrintingMessageFilter);
};
} // namespace android_webview
#endif // ANDROID_WEBVIEW_BROWSER_PRINTING_MESSAGE_FILTER_H_
......@@ -17,8 +17,22 @@
using content::BrowserThread;
using printing::PrintSettings;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::PrintManager);
namespace android_webview {
// static
PrintManager* PrintManager::CreateForWebContents(
content::WebContents* contents,
PrintSettings* settings,
int fd,
PrintManagerDelegate* delegate) {
PrintManager* print_manager =
new PrintManager(contents, settings, fd, delegate);
contents->SetUserData(UserDataKey(), print_manager);
return print_manager;
}
PrintManager::PrintManager(content::WebContents* contents,
PrintSettings* settings,
int fd,
......@@ -45,10 +59,6 @@ bool PrintManager::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
OnAllocateTempFileForPrinting)
IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
OnTempFileForPrintingWritten)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
......
......@@ -8,6 +8,7 @@
#include "base/callback_forward.h"
#include "base/threading/non_thread_safe.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
class GURL;
......@@ -26,9 +27,6 @@ class PrintManagerDelegate {
virtual ~PrintManagerDelegate() { }
virtual void DidExportPdf(bool success) = 0;
virtual bool IsCancelled() = 0;
private:
// DISALLOW_COPY_AND_ASSIGN(PrintManagerDelegate);
};
// Provides RenderViewHost wrapper functionality for sending WebView-specific
......@@ -38,14 +36,17 @@ class PrintManagerDelegate {
// This class manages the print (an export to PDF file essentially) task for
// a webview. There can be at most one active print task per webview.
class PrintManager : public content::WebContentsObserver,
public content::WebContentsUserData<PrintManager>,
public base::NonThreadSafe {
public:
// To send receive messages to a RenderView we take the WebContents instance,
// as it internally handles RenderViewHost instances changing underneath us.
PrintManager(content::WebContents* contents,
printing::PrintSettings* settings,
int fd,
PrintManagerDelegate* delegate);
// Creates a PrintManager for the provided webcontents. If the printmanager
// already exists, it is destroyed and a new one is created.
static PrintManager* CreateForWebContents(
content::WebContents* contents,
printing::PrintSettings* settings,
int fd,
PrintManagerDelegate* delegate);
virtual ~PrintManager();
// Prints the current document immediately. Since the rendering is
......@@ -56,16 +57,24 @@ class PrintManager : public content::WebContentsObserver,
// already busy printing.
bool PrintNow();
void OnAllocateTempFileForPrinting(base::FileDescriptor* temp_file_fd,
int* sequence_number);
void OnTempFileForPrintingWritten(int sequence_number);
private:
// To send receive messages to a RenderView we take the WebContents instance,
// as it internally handles RenderViewHost instances changing underneath us.
PrintManager(content::WebContents* contents,
printing::PrintSettings* settings,
int fd,
PrintManagerDelegate* delegate);
friend class content::WebContentsUserData<PrintManager>;
virtual bool OnMessageReceived(const IPC::Message& message) override;
void OnDidGetPrintedPagesCount(int cookie, int number_pages);
void OnDidGetDocumentCookie(int cookie);
void OnPrintingFailed(int cookie);
void OnGetDefaultPrintSettingsReply(IPC::Message* reply_msg);
void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
void OnAllocateTempFileForPrinting(base::FileDescriptor* temp_file_fd,
int* sequence_number);
void OnTempFileForPrintingWritten(int sequence_number);
// Print Settings.
printing::PrintSettings* settings_;
......
......@@ -376,11 +376,13 @@ IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_ScriptedPrint,
#if defined(OS_ANDROID)
// Asks the browser to create a temporary file for the renderer to fill
// in resulting PdfMetafileSkia in printing.
IPC_SYNC_MESSAGE_ROUTED0_2(PrintHostMsg_AllocateTempFileForPrinting,
base::FileDescriptor /* temp file fd */,
int /* fd in browser*/)
IPC_MESSAGE_ROUTED1(PrintHostMsg_TempFileForPrintingWritten,
int /* fd in browser */)
IPC_SYNC_MESSAGE_CONTROL1_2(PrintHostMsg_AllocateTempFileForPrinting,
int /* render_view_id */,
base::FileDescriptor /* temp file fd */,
int /* fd in browser*/)
IPC_MESSAGE_CONTROL2(PrintHostMsg_TempFileForPrintingWritten,
int /* render_view_id */,
int /* fd in browser */)
#endif
// Asks the browser to do print preview.
IPC_MESSAGE_ROUTED1(PrintHostMsg_RequestPrintPreview,
......
......@@ -4,7 +4,6 @@
#include "android_webview/native/aw_pdf_exporter.h"
#include "android_webview/browser/renderer_host/print_manager.h"
#include "base/android/jni_android.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
......@@ -48,9 +47,10 @@ void AwPdfExporter::ExportToPdf(JNIEnv* env,
jobject cancel_signal) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CreatePdfSettings(env, obj);
print_manager_.reset(
new PrintManager(web_contents_, print_settings_.get(), fd, this));
if (!print_manager_->PrintNow())
PrintManager* print_manager =
PrintManager::CreateForWebContents(
web_contents_, print_settings_.get(), fd, this);
if (!print_manager->PrintNow())
DidExportPdf(false);
}
......
......@@ -47,7 +47,6 @@ class AwPdfExporter : public PrintManagerDelegate {
JavaObjectWeakGlobalRef java_ref_;
content::WebContents* web_contents_;
scoped_ptr<PrintManager> print_manager_;
scoped_ptr<printing::PrintSettings> print_settings_;
DISALLOW_COPY_AND_ASSIGN(AwPdfExporter);
......
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