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 | |
| 7 | #include "components/printing/browser/print_manager_utils.h" |
| 8 | #include "components/printing/common/print_messages.h" |
| 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" |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 11 | |
| 12 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwPrintManager); |
| 13 | |
| 14 | namespace android_webview { |
| 15 | |
| 16 | // static |
| 17 | AwPrintManager* AwPrintManager::CreateForWebContents( |
| 18 | content::WebContents* contents, |
| 19 | const printing::PrintSettings& settings, |
| 20 | const base::FileDescriptor& file_descriptor, |
| 21 | const PrintManager::PdfWritingDoneCallback& callback) { |
| 22 | AwPrintManager* print_manager = |
| 23 | new AwPrintManager(contents, settings, file_descriptor, callback); |
| 24 | contents->SetUserData(UserDataKey(), print_manager); |
| 25 | return print_manager; |
| 26 | } |
| 27 | |
| 28 | AwPrintManager::AwPrintManager( |
| 29 | content::WebContents* contents, |
| 30 | const printing::PrintSettings& settings, |
| 31 | const base::FileDescriptor& file_descriptor, |
| 32 | const PdfWritingDoneCallback& callback) |
| 33 | : PrintManager(contents), |
| 34 | settings_(settings) { |
| 35 | set_file_descriptor(file_descriptor); |
| 36 | pdf_writing_done_callback_ = callback; |
| 37 | cookie_ = 1; |
| 38 | } |
| 39 | |
| 40 | AwPrintManager::~AwPrintManager() { |
| 41 | } |
| 42 | |
| 43 | bool AwPrintManager::PrintNow() { |
| 44 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 45 | auto* rfh = web_contents()->GetMainFrame(); |
| 46 | return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 47 | } |
| 48 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 49 | bool AwPrintManager::OnMessageReceived( |
| 50 | const IPC::Message& message, |
| 51 | content::RenderFrameHost* render_frame_host) { |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 52 | bool handled = true; |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 53 | IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(AwPrintManager, message, render_frame_host) |
| 54 | IPC_MESSAGE_HANDLER_WITH_PARAM_DELAY_REPLY( |
| 55 | PrintHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings) |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 56 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 57 | IPC_END_MESSAGE_MAP() |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 58 | return handled ? true |
| 59 | : PrintManager::OnMessageReceived(message, render_frame_host); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 60 | } |
| 61 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 62 | void AwPrintManager::OnGetDefaultPrintSettings( |
| 63 | content::RenderFrameHost* render_frame_host, |
| 64 | IPC::Message* reply_msg) { |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 65 | // Unlike the printing_message_filter, we do process this in UI thread. |
| 66 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 67 | PrintMsg_Print_Params params; |
| 68 | printing::RenderParamsFromPrintSettings(settings_, ¶ms); |
| 69 | params.document_cookie = cookie_; |
| 70 | PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params); |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame^] | 71 | render_frame_host->Send(reply_msg); |
timvolodine | 66cc354b | 2015-07-14 16:13:16 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace android_webview |