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