OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 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 "headless/lib/browser/headless_print_manager.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/base64.h" | |
11 #include "components/printing/browser/print_manager_utils.h" | |
12 #include "components/printing/common/print_messages.h" | |
13 #include "printing/pdf_metafile_skia.h" | |
14 #include "printing/print_settings.h" | |
15 #include "printing/units.h" | |
16 | |
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::HeadlessPrintManager); | |
18 | |
19 namespace printing { | |
20 | |
21 namespace { | |
22 | |
23 // TODO(jzfeng): let the print settings to be configurable. | |
24 const double kTopMarginInInch = 0.25; | |
25 const double kBottomMarginInInch = 0.56; | |
26 const double kLeftMarginInInch = 0.25; | |
27 const double kRightMarginInInch = 0.25; | |
28 | |
29 PrintSettings GetDefaultPDFPrinterSettings() { | |
30 int dpi = printing::kDefaultPdfDpi; | |
Lei Zhang
2017/03/29 01:38:03
No need for printing:: if you decide to keep this
jzfeng
2017/03/29 03:50:13
Done.
I feel it is ok to be under printing like Pr
Eric Seckler
2017/03/29 11:21:19
FWIW, either namespace is fine with me :)
| |
31 gfx::Size physical_size_device_units; | |
32 gfx::Rect printable_area_device_units; | |
33 double page_width_in_pixel = printing::kLetterWidthInch * dpi; | |
34 double page_height_in_pixel = printing::kLetterHeightInch * dpi; | |
35 physical_size_device_units.SetSize(static_cast<int>(page_width_in_pixel), | |
36 static_cast<int>(page_height_in_pixel)); | |
37 printable_area_device_units.SetRect( | |
38 static_cast<int>(kLeftMarginInInch * dpi), | |
39 static_cast<int>(kTopMarginInInch * dpi), | |
40 page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi, | |
41 page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi); | |
42 | |
43 PrintSettings settings; | |
44 settings.set_dpi(dpi); | |
45 settings.SetPrinterPrintableArea(physical_size_device_units, | |
46 printable_area_device_units, true); | |
47 settings.set_should_print_backgrounds(true); | |
48 return settings; | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 HeadlessPrintManager::HeadlessPrintManager(content::WebContents* web_contents) | |
54 : PrintManager(web_contents), | |
55 printing_rfh_(nullptr), | |
Lei Zhang
2017/03/29 01:38:03
Maybe implement a Reset() method and call it both
jzfeng
2017/03/29 03:50:13
Done.
| |
56 is_active_(false), | |
57 #if !defined(OS_MACOSX) | |
58 expecting_first_page_(true) | |
59 #endif | |
60 { | |
61 } | |
62 | |
63 HeadlessPrintManager::~HeadlessPrintManager() {} | |
64 | |
65 // static | |
66 std::string HeadlessPrintManager::PrintResultToErrMsg(PrintResult result) { | |
67 switch (result) { | |
68 case kPrintSuccess: | |
69 return ""; // no error message | |
70 case kPrintingFailed: | |
71 return "PrintingFailed"; | |
72 case kInvalidPrinterSettings: | |
73 return "ShowInvalidPrinterSettingsError"; | |
74 case kInvalidMemoryHandle: | |
75 return "Invalid memory handle"; | |
76 case kMetafileMapError: | |
77 return "Map to SharedMemory error"; | |
78 case kUnexpectedValidMemoryHandle: | |
79 return "Unexpected valide memory handle"; | |
80 case kMetafileInvalidHeader: | |
81 return "Invalid metafile header"; | |
82 case kMetafileGetDataError: | |
83 return "Get data from metafile error"; | |
84 case kDuplicatedPrintingError: | |
85 return "The previous printing job hasn't finished"; | |
86 default: | |
87 return "Unknown PrintResult"; | |
Lei Zhang
2017/03/29 01:38:03
Probably needs a NOTREACHED();
jzfeng
2017/03/29 03:50:13
Done.
| |
88 } | |
89 } | |
90 | |
91 // static | |
92 std::unique_ptr<base::DictionaryValue> | |
93 HeadlessPrintManager::PDFContentsToDictionaryValue(const std::string& data) { | |
94 std::string base_64_data; | |
95 base::Base64Encode(data, &base_64_data); | |
96 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
Lei Zhang
2017/03/29 01:38:03
auto result = base::MakeUnique<base::DictionaryVal
jzfeng
2017/03/29 03:50:13
Done.
| |
97 result->SetString("data", base_64_data); | |
98 return result; | |
99 } | |
100 | |
101 bool HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, | |
102 GetPDFCallback callback) { | |
103 if (is_active_) { | |
104 callback.Run(kDuplicatedPrintingError, ""); | |
105 return true; | |
Lei Zhang
2017/03/29 01:38:03
Shouldn't this return false?
jzfeng
2017/03/29 03:50:13
I was thinking something else. Changed to just ret
| |
106 } | |
107 is_active_ = true; | |
108 printing_rfh_ = rfh; | |
109 callback_ = callback; | |
110 return rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); | |
111 } | |
112 | |
113 bool HeadlessPrintManager::OnMessageReceived( | |
114 const IPC::Message& message, | |
115 content::RenderFrameHost* render_frame_host) { | |
116 bool handled = true; | |
117 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) | |
118 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, | |
119 OnShowInvalidPrinterSettingsError) | |
120 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | |
121 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, | |
122 OnGetDefaultPrintSettings) | |
123 IPC_MESSAGE_UNHANDLED(handled = false) | |
124 IPC_END_MESSAGE_MAP() | |
125 return handled || PrintManager::OnMessageReceived(message, render_frame_host); | |
126 } | |
127 | |
128 void HeadlessPrintManager::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { | |
129 PrintMsg_Print_Params print_params; | |
130 RenderParamsFromPrintSettings(GetDefaultPDFPrinterSettings(), &print_params); | |
131 print_params.document_cookie = PrintSettings::NewCookie(); | |
132 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, | |
133 print_params); | |
134 printing_rfh_->Send(reply_msg); | |
135 } | |
136 | |
137 void HeadlessPrintManager::OnShowInvalidPrinterSettingsError() { | |
138 ReleaseJob(kInvalidPrinterSettings); | |
139 } | |
140 | |
141 void HeadlessPrintManager::OnPrintingFailed(int cookie) { | |
142 ReleaseJob(kPrintingFailed); | |
143 } | |
144 | |
145 void HeadlessPrintManager::OnDidPrintPage( | |
146 const PrintHostMsg_DidPrintPage_Params& params) { | |
147 #if defined(OS_MACOSX) | |
148 const bool metafile_must_be_valid = true; | |
149 #else | |
150 const bool metafile_must_be_valid = expecting_first_page_; | |
151 expecting_first_page_ = false; | |
152 #endif | |
153 | |
154 // Only used when |metafile_must_be_valid| is true. | |
155 std::unique_ptr<base::SharedMemory> shared_buf; | |
156 if (metafile_must_be_valid) { | |
157 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
158 ReleaseJob(kInvalidMemoryHandle); | |
159 return; | |
160 } | |
161 shared_buf = | |
162 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); | |
163 if (!shared_buf->Map(params.data_size)) { | |
164 ReleaseJob(kMetafileMapError); | |
165 return; | |
166 } | |
167 } else { | |
168 if (base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
169 base::SharedMemory::CloseHandle(params.metafile_data_handle); | |
170 ReleaseJob(kUnexpectedValidMemoryHandle); | |
171 return; | |
172 } | |
173 } | |
174 | |
175 std::unique_ptr<PdfMetafileSkia> metafile( | |
176 new PdfMetafileSkia(PDF_SKIA_DOCUMENT_TYPE)); | |
Lei Zhang
2017/03/29 01:38:03
More base::MakeUnique. Basically try to avoid usin
jzfeng
2017/03/29 03:50:13
Cool. Also change some other places that use "new"
| |
177 if (metafile_must_be_valid) { | |
178 if (!metafile->InitFromData(shared_buf->memory(), params.data_size)) { | |
179 ReleaseJob(kMetafileInvalidHeader); | |
180 return; | |
181 } | |
182 std::vector<char> buffer; | |
183 if (!metafile->GetDataAsVector(&buffer)) { | |
184 ReleaseJob(kMetafileGetDataError); | |
185 return; | |
186 } | |
187 data_ = std::string(buffer.data(), buffer.size()); | |
188 } | |
189 | |
190 if (--number_pages_ == 0) | |
191 ReleaseJob(kPrintSuccess); | |
192 } | |
193 | |
194 void HeadlessPrintManager::ReleaseJob(PrintResult result) { | |
195 if (result == kPrintSuccess) | |
196 callback_.Run(result, std::move(data_)); | |
197 else | |
198 callback_.Run(result, ""); | |
199 data_.clear(); | |
200 number_pages_ = 0; | |
201 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), | |
202 result == kPrintSuccess)); | |
203 printing_rfh_ = nullptr; | |
204 is_active_ = false; | |
205 #if !defined(OS_MACOSX) | |
206 expecting_first_page_ = true; | |
207 #endif | |
208 } | |
209 | |
210 } // namespace printing | |
OLD | NEW |