timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 1 | // 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 | |
avi | ad7848339 | 2017-04-28 16:56:12 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 8 | #include "components/printing/browser/print_manager_utils.h" |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 9 | #include "content/public/browser/browser_thread.h" |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 10 | #include "content/public/browser/render_frame_host.h" |
Lukasz Anforowicz | f4357ca | 2017-09-07 01:43:32 | [diff] [blame] | 11 | #include "content/public/browser/render_view_host.h" |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 12 | |
| 13 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwPrintManager); |
| 14 | |
| 15 | namespace android_webview { |
| 16 | |
Lukasz Anforowicz | f4357ca | 2017-09-07 01:43:32 | [diff] [blame] | 17 | struct 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 | |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 33 | // static |
| 34 | AwPrintManager* AwPrintManager::CreateForWebContents( |
| 35 | content::WebContents* contents, |
| 36 | const printing::PrintSettings& settings, |
| 37 | const base::FileDescriptor& file_descriptor, |
tzik | b57fcc4 | 2018-03-23 02:56:50 | [diff] [blame] | 38 | PrintManager::PdfWritingDoneCallback callback) { |
| 39 | AwPrintManager* print_manager = new AwPrintManager( |
| 40 | contents, settings, file_descriptor, std::move(callback)); |
avi | ad7848339 | 2017-04-28 16:56:12 | [diff] [blame] | 41 | contents->SetUserData(UserDataKey(), base::WrapUnique(print_manager)); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 42 | return print_manager; |
| 43 | } |
| 44 | |
tzik | b57fcc4 | 2018-03-23 02:56:50 | [diff] [blame] | 45 | AwPrintManager::AwPrintManager(content::WebContents* contents, |
| 46 | const printing::PrintSettings& settings, |
| 47 | const base::FileDescriptor& file_descriptor, |
| 48 | PdfWritingDoneCallback callback) |
| 49 | : PrintManager(contents), settings_(settings) { |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 50 | set_file_descriptor(file_descriptor); |
tzik | b57fcc4 | 2018-03-23 02:56:50 | [diff] [blame] | 51 | pdf_writing_done_callback_ = std::move(callback); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 52 | cookie_ = 1; |
| 53 | } |
| 54 | |
| 55 | AwPrintManager::~AwPrintManager() { |
| 56 | } |
| 57 | |
| 58 | bool AwPrintManager::PrintNow() { |
| 59 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 60 | auto* rfh = web_contents()->GetMainFrame(); |
| 61 | return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 62 | } |
| 63 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 64 | bool AwPrintManager::OnMessageReceived( |
| 65 | const IPC::Message& message, |
| 66 | content::RenderFrameHost* render_frame_host) { |
Lukasz Anforowicz | f4357ca | 2017-09-07 01:43:32 | [diff] [blame] | 67 | FrameDispatchHelper helper = {this, render_frame_host}; |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 68 | bool handled = true; |
Lukasz Anforowicz | f4357ca | 2017-09-07 01:43:32 | [diff] [blame] | 69 | 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) |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 75 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 76 | IPC_END_MESSAGE_MAP() |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 77 | return handled ? true |
| 78 | : PrintManager::OnMessageReceived(message, render_frame_host); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 79 | } |
| 80 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 81 | void AwPrintManager::OnGetDefaultPrintSettings( |
| 82 | content::RenderFrameHost* render_frame_host, |
| 83 | IPC::Message* reply_msg) { |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 84 | // 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_, ¶ms); |
| 88 | params.document_cookie = cookie_; |
| 89 | PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params); |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 90 | render_frame_host->Send(reply_msg); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 91 | } |
| 92 | |
ctzsm | d20a4bb | 2017-03-24 21:52:18 | [diff] [blame] | 93 | void 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_, ¶ms.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 | |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 106 | } // namespace android_webview |