1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/browser/renderer_host/print_manager.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/common/render_view_messages.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/callback.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "components/printing/common/print_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "printing/print_settings.h"
17 using content::BrowserThread
;
18 using printing::PrintSettings
;
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::PrintManager
);
22 namespace android_webview
{
25 PrintManager
* PrintManager::CreateForWebContents(
26 content::WebContents
* contents
,
27 PrintSettings
* settings
,
29 PrintManagerDelegate
* delegate
) {
30 PrintManager
* print_manager
=
31 new PrintManager(contents
, settings
, fd
, delegate
);
32 contents
->SetUserData(UserDataKey(), print_manager
);
36 PrintManager::PrintManager(content::WebContents
* contents
,
37 PrintSettings
* settings
,
39 PrintManagerDelegate
* delegate
)
40 : content::WebContentsObserver(contents
),
50 PrintManager::~PrintManager() {}
52 bool PrintManager::OnMessageReceived(const IPC::Message
& message
) {
54 IPC_BEGIN_MESSAGE_MAP(PrintManager
, message
)
55 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount
,
56 OnDidGetPrintedPagesCount
)
57 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie
,
58 OnDidGetDocumentCookie
)
59 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed
, OnPrintingFailed
)
60 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings
,
61 OnGetDefaultPrintSettings
)
62 IPC_MESSAGE_UNHANDLED(handled
= false)
65 return handled
? true : WebContentsObserver::OnMessageReceived(message
);
68 void PrintManager::OnAllocateTempFileForPrinting(
69 base::FileDescriptor
* temp_file_fd
,
70 int* sequence_number
) {
71 // we don't really use the sequence number.
73 temp_file_fd
->fd
= fd_
;
74 temp_file_fd
->auto_close
= false;
77 void PrintManager::OnTempFileForPrintingWritten(int sequence_number
) {
78 delegate_
->DidExportPdf(true);
81 void PrintManager::OnDidGetPrintedPagesCount(int cookie
,
84 DCHECK_GT(number_pages
, 0);
85 number_pages_
= number_pages
;
88 void PrintManager::OnDidGetDocumentCookie(int cookie
) {
92 void PrintManager::OnGetDefaultPrintSettings(IPC::Message
* reply_msg
) {
93 // Unlike the printing_message_filter, we do process this in ui thread
94 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
95 OnGetDefaultPrintSettingsReply(reply_msg
);
98 void RenderParamsFromPrintSettings(const printing::PrintSettings
& settings
,
99 PrintMsg_Print_Params
* params
) {
100 params
->page_size
= settings
.page_setup_device_units().physical_size();
101 params
->content_size
.SetSize(
102 settings
.page_setup_device_units().content_area().width(),
103 settings
.page_setup_device_units().content_area().height());
104 params
->printable_area
.SetRect(
105 settings
.page_setup_device_units().printable_area().x(),
106 settings
.page_setup_device_units().printable_area().y(),
107 settings
.page_setup_device_units().printable_area().width(),
108 settings
.page_setup_device_units().printable_area().height());
109 params
->margin_top
= settings
.page_setup_device_units().content_area().y();
110 params
->margin_left
= settings
.page_setup_device_units().content_area().x();
111 params
->dpi
= settings
.dpi();
112 // Currently hardcoded at 1.25. See PrintSettings' constructor.
113 params
->min_shrink
= settings
.min_shrink();
114 // Currently hardcoded at 2.0. See PrintSettings' constructor.
115 params
->max_shrink
= settings
.max_shrink();
116 // Currently hardcoded at 72dpi. See PrintSettings' constructor.
117 params
->desired_dpi
= settings
.desired_dpi();
118 // Always use an invalid cookie.
119 params
->document_cookie
= 0;
120 params
->selection_only
= settings
.selection_only();
121 params
->supports_alpha_blend
= settings
.supports_alpha_blend();
122 params
->should_print_backgrounds
= settings
.should_print_backgrounds();
123 params
->display_header_footer
= settings
.display_header_footer();
124 params
->title
= settings
.title();
125 params
->url
= settings
.url();
128 void PrintManager::OnGetDefaultPrintSettingsReply(IPC::Message
* reply_msg
) {
129 PrintMsg_Print_Params params
;
130 RenderParamsFromPrintSettings(*settings_
, ¶ms
);
131 params
.document_cookie
= cookie_
;
132 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg
, params
);
136 void PrintManager::OnPrintingFailed(int cookie
) {
137 if (cookie
!= cookie_
) {
141 delegate_
->DidExportPdf(false);
144 bool PrintManager::PrintNow() {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
150 return Send(new PrintMsg_PrintPages(routing_id()));
153 } // namespace android_webview