blob: 6aa43f476a3e90271c618d2beeccfcb42cd2d979 [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"
thestigcb959ce2016-11-17 05:56:3210#include "content/public/browser/render_frame_host.h"
timvolodine66cc354b2015-07-14 16:13:1611
12DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwPrintManager);
13
14namespace android_webview {
15
16// static
17AwPrintManager* 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
28AwPrintManager::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
40AwPrintManager::~AwPrintManager() {
41}
42
43bool AwPrintManager::PrintNow() {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
thestigcb959ce2016-11-17 05:56:3245 auto* rfh = web_contents()->GetMainFrame();
46 return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID()));
timvolodine66cc354b2015-07-14 16:13:1647}
48
thestigcb959ce2016-11-17 05:56:3249bool AwPrintManager::OnMessageReceived(
50 const IPC::Message& message,
51 content::RenderFrameHost* render_frame_host) {
timvolodine66cc354b2015-07-14 16:13:1652 bool handled = true;
thestigcb959ce2016-11-17 05:56:3253 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(AwPrintManager, message, render_frame_host)
54 IPC_MESSAGE_HANDLER_WITH_PARAM_DELAY_REPLY(
55 PrintHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings)
timvolodine66cc354b2015-07-14 16:13:1656 IPC_MESSAGE_UNHANDLED(handled = false)
57 IPC_END_MESSAGE_MAP()
thestigcb959ce2016-11-17 05:56:3258 return handled ? true
59 : PrintManager::OnMessageReceived(message, render_frame_host);
timvolodine66cc354b2015-07-14 16:13:1660}
61
thestigcb959ce2016-11-17 05:56:3262void AwPrintManager::OnGetDefaultPrintSettings(
63 content::RenderFrameHost* render_frame_host,
64 IPC::Message* reply_msg) {
timvolodine66cc354b2015-07-14 16:13:1665 // 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_, &params);
69 params.document_cookie = cookie_;
70 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params);
thestigcb959ce2016-11-17 05:56:3271 render_frame_host->Send(reply_msg);
timvolodine66cc354b2015-07-14 16:13:1672}
73
74} // namespace android_webview