blob: 80748093e636e55e070235560d8c138d72f7a966 [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
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
11DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwPrintManager);
12
13namespace android_webview {
14
15// static
16AwPrintManager* 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
27AwPrintManager::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
39AwPrintManager::~AwPrintManager() {
40}
41
42bool AwPrintManager::PrintNow() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 return Send(new PrintMsg_PrintPages(routing_id()));
45}
46
sgurun356feed52016-11-16 16:13:0647bool AwPrintManager::OnMessageReceived(const IPC::Message& message) {
timvolodine66cc354b2015-07-14 16:13:1648 bool handled = true;
sgurun356feed52016-11-16 16:13:0649 IPC_BEGIN_MESSAGE_MAP(AwPrintManager, message)
50 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
51 OnGetDefaultPrintSettings)
timvolodine66cc354b2015-07-14 16:13:1652 IPC_MESSAGE_UNHANDLED(handled = false)
53 IPC_END_MESSAGE_MAP()
sgurun356feed52016-11-16 16:13:0654 return handled ? true : PrintManager::OnMessageReceived(message);
timvolodine66cc354b2015-07-14 16:13:1655}
56
sgurun356feed52016-11-16 16:13:0657void AwPrintManager::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
timvolodine66cc354b2015-07-14 16:13:1658 // 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_, &params);
62 params.document_cookie = cookie_;
63 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params);
sgurun356feed52016-11-16 16:13:0664 Send(reply_msg);
timvolodine66cc354b2015-07-14 16:13:1665}
66
67} // namespace android_webview