blob: 736771c5f7d19de7ce9a61525b7acccd031d83e5 [file] [log] [blame]
timvolodine66cc354b2015-07-14 16:13:161// Copyright 2015 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.
4
5#include "android_webview/browser/aw_print_manager.h"
6
aviad78483392017-04-28 16:56:127#include "base/memory/ptr_util.h"
timvolodine66cc354b2015-07-14 16:13:168#include "components/printing/browser/print_manager_utils.h"
timvolodine66cc354b2015-07-14 16:13:169#include "content/public/browser/browser_thread.h"
thestigcb959ce2016-11-17 05:56:3210#include "content/public/browser/render_frame_host.h"
Lukasz Anforowiczf4357ca2017-09-07 01:43:3211#include "content/public/browser/render_view_host.h"
timvolodine66cc354b2015-07-14 16:13:1612
13DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwPrintManager);
14
15namespace android_webview {
16
Lukasz Anforowiczf4357ca2017-09-07 01:43:3217struct AwPrintManager::FrameDispatchHelper {
18 AwPrintManager* manager;
19 content::RenderFrameHost* render_frame_host;
20
21 bool Send(IPC::Message* msg) { return render_frame_host->Send(msg); }
22
23 void OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
24 manager->OnGetDefaultPrintSettings(render_frame_host, reply_msg);
25 }
26
27 void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& scripted_params,
28 IPC::Message* reply_msg) {
29 manager->OnScriptedPrint(render_frame_host, scripted_params, reply_msg);
30 }
31};
32
timvolodine66cc354b2015-07-14 16:13:1633// static
34AwPrintManager* AwPrintManager::CreateForWebContents(
35 content::WebContents* contents,
36 const printing::PrintSettings& settings,
37 const base::FileDescriptor& file_descriptor,
tzikb57fcc42018-03-23 02:56:5038 PrintManager::PdfWritingDoneCallback callback) {
39 AwPrintManager* print_manager = new AwPrintManager(
40 contents, settings, file_descriptor, std::move(callback));
aviad78483392017-04-28 16:56:1241 contents->SetUserData(UserDataKey(), base::WrapUnique(print_manager));
timvolodine66cc354b2015-07-14 16:13:1642 return print_manager;
43}
44
tzikb57fcc42018-03-23 02:56:5045AwPrintManager::AwPrintManager(content::WebContents* contents,
46 const printing::PrintSettings& settings,
47 const base::FileDescriptor& file_descriptor,
48 PdfWritingDoneCallback callback)
49 : PrintManager(contents), settings_(settings) {
timvolodine66cc354b2015-07-14 16:13:1650 set_file_descriptor(file_descriptor);
tzikb57fcc42018-03-23 02:56:5051 pdf_writing_done_callback_ = std::move(callback);
timvolodine66cc354b2015-07-14 16:13:1652 cookie_ = 1;
53}
54
55AwPrintManager::~AwPrintManager() {
56}
57
58bool AwPrintManager::PrintNow() {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
thestigcb959ce2016-11-17 05:56:3260 auto* rfh = web_contents()->GetMainFrame();
61 return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID()));
timvolodine66cc354b2015-07-14 16:13:1662}
63
thestigcb959ce2016-11-17 05:56:3264bool AwPrintManager::OnMessageReceived(
65 const IPC::Message& message,
66 content::RenderFrameHost* render_frame_host) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:3267 FrameDispatchHelper helper = {this, render_frame_host};
timvolodine66cc354b2015-07-14 16:13:1668 bool handled = true;
Lukasz Anforowiczf4357ca2017-09-07 01:43:3269 IPC_BEGIN_MESSAGE_MAP(AwPrintManager, message)
70 IPC_MESSAGE_FORWARD_DELAY_REPLY(
71 PrintHostMsg_GetDefaultPrintSettings, &helper,
72 FrameDispatchHelper::OnGetDefaultPrintSettings)
73 IPC_MESSAGE_FORWARD_DELAY_REPLY(PrintHostMsg_ScriptedPrint, &helper,
74 FrameDispatchHelper::OnScriptedPrint)
timvolodine66cc354b2015-07-14 16:13:1675 IPC_MESSAGE_UNHANDLED(handled = false)
76 IPC_END_MESSAGE_MAP()
thestigcb959ce2016-11-17 05:56:3277 return handled ? true
78 : PrintManager::OnMessageReceived(message, render_frame_host);
timvolodine66cc354b2015-07-14 16:13:1679}
80
thestigcb959ce2016-11-17 05:56:3281void AwPrintManager::OnGetDefaultPrintSettings(
82 content::RenderFrameHost* render_frame_host,
83 IPC::Message* reply_msg) {
timvolodine66cc354b2015-07-14 16:13:1684 // Unlike the printing_message_filter, we do process this in UI thread.
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
86 PrintMsg_Print_Params params;
87 printing::RenderParamsFromPrintSettings(settings_, &params);
88 params.document_cookie = cookie_;
89 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params);
thestigcb959ce2016-11-17 05:56:3290 render_frame_host->Send(reply_msg);
timvolodine66cc354b2015-07-14 16:13:1691}
92
ctzsmd20a4bb2017-03-24 21:52:1893void AwPrintManager::OnScriptedPrint(
94 content::RenderFrameHost* render_frame_host,
95 const PrintHostMsg_ScriptedPrint_Params& scripted_params,
96 IPC::Message* reply_msg) {
97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
98 PrintMsg_PrintPages_Params params;
99 printing::RenderParamsFromPrintSettings(settings_, &params.params);
100 params.params.document_cookie = scripted_params.cookie;
101 params.pages = printing::PageRange::GetPages(settings_.ranges());
102 PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, params);
103 render_frame_host->Send(reply_msg);
104}
105
timvolodine66cc354b2015-07-14 16:13:16106} // namespace android_webview