Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "headless/lib/browser/headless_print_manager.h" | 5 #include "headless/lib/browser/headless_print_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "components/printing/browser/print_manager_utils.h" | 13 #include "components/printing/browser/print_manager_utils.h" |
| 12 #include "components/printing/common/print_messages.h" | 14 #include "components/printing/common/print_messages.h" |
| 13 #include "printing/pdf_metafile_skia.h" | 15 #include "printing/pdf_metafile_skia.h" |
| 14 #include "printing/print_settings.h" | 16 #include "printing/print_job_constants.h" |
| 15 #include "printing/units.h" | 17 #include "printing/units.h" |
| 18 #include "ui/gfx/geometry/size_f.h" | |
| 16 | 19 |
| 17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::HeadlessPrintManager); | 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::HeadlessPrintManager); |
| 18 | 21 |
| 19 namespace printing { | 22 namespace printing { |
| 20 | 23 |
| 21 namespace { | 24 HeadlessPrintSettings::HeadlessPrintSettings() |
| 25 : dpi(kDefaultPdfDpi), | |
| 26 paper_type(LETTER), | |
| 27 margin_type(DEFAULT_MARGINS), | |
| 28 landscape(false), | |
| 29 display_header_footer(false), | |
| 30 should_print_backgrounds(false), | |
| 31 scale(1) {} | |
| 22 | 32 |
| 23 // TODO(jzfeng): let the print settings to be configurable. | 33 HeadlessPrintSettings::HeadlessPrintSettings(const HeadlessPrintSettings& obj) = |
| 24 const double kTopMarginInInch = 0.25; | 34 default; |
| 25 const double kBottomMarginInInch = 0.56; | 35 HeadlessPrintSettings::~HeadlessPrintSettings() = default; |
| 26 const double kLeftMarginInInch = 0.25; | |
| 27 const double kRightMarginInInch = 0.25; | |
| 28 | 36 |
| 29 PrintSettings GetDefaultPDFPrinterSettings() { | 37 int HeadlessPrintSettings::GetDPI() { |
|
Eric Seckler
2017/04/20 09:15:28
nit: this doesn't always return the DPI, so should
jzfeng
2017/04/27 06:56:06
Removed this method since setting different dpi do
| |
| 30 #if defined(OS_MACOSX) | 38 #if defined(OS_MACOSX) |
| 31 // On the Mac, the printable area is in points, use kPointsPerInch to compute | 39 // On the Mac, the printable area is in points, use kPointsPerInch to compute |
| 32 // its bounds. | 40 // its bounds. |
| 33 int dpi = kPointsPerInch; | 41 return kPointsPerInch; |
| 34 #else | 42 #else |
| 35 int dpi = kDefaultPdfDpi; | 43 return dpi; |
| 36 #endif | 44 #endif |
| 37 gfx::Size physical_size_device_units; | |
| 38 gfx::Rect printable_area_device_units; | |
| 39 double page_width_in_pixel = kLetterWidthInch * dpi; | |
| 40 double page_height_in_pixel = kLetterHeightInch * dpi; | |
| 41 physical_size_device_units.SetSize(static_cast<int>(page_width_in_pixel), | |
| 42 static_cast<int>(page_height_in_pixel)); | |
| 43 printable_area_device_units.SetRect( | |
| 44 static_cast<int>(kLeftMarginInInch * dpi), | |
| 45 static_cast<int>(kTopMarginInInch * dpi), | |
| 46 page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi, | |
| 47 page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi); | |
| 48 | |
| 49 PrintSettings settings; | |
| 50 settings.set_dpi(dpi); | |
| 51 settings.SetPrinterPrintableArea(physical_size_device_units, | |
| 52 printable_area_device_units, true); | |
| 53 settings.set_should_print_backgrounds(true); | |
| 54 return settings; | |
| 55 } | 45 } |
| 56 | 46 |
| 57 } // namespace | 47 gfx::Size HeadlessPrintSettings::PaperSizeInPixel() { |
|
Eric Seckler
2017/04/20 09:15:28
nit: PaperSizeInPixelsOrPoints
jzfeng
2017/04/27 06:56:06
Change to PaperSizeInPoints.
| |
| 48 gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch); | |
| 49 switch (paper_type) { | |
| 50 case LEGAL: | |
| 51 paper_size.SetSize(kLegalWidthInch, kLegalHeightInch); | |
| 52 break; | |
| 53 case A4: | |
| 54 paper_size.SetSize(kA4WidthInch, kA4HeightInch); | |
| 55 break; | |
| 56 case A3: | |
| 57 paper_size.SetSize(kA3WidthInch, kA3HeightInch); | |
| 58 break; | |
| 59 default: // LETTER is used for default fallback. | |
| 60 break; | |
| 61 } | |
| 62 return gfx::Size(paper_size.width() * GetDPI(), | |
| 63 paper_size.height() * GetDPI()); | |
| 64 } | |
| 65 | |
| 66 PageMargins HeadlessPrintSettings::MarginInPoints() { | |
| 67 PageMargins margins_in_points; | |
| 68 margins_in_points.top = margin_top_in_inch * kPointsPerInch; | |
| 69 margins_in_points.bottom = margin_bottom_in_inch * kPointsPerInch; | |
| 70 margins_in_points.left = margin_left_in_inch * kPointsPerInch; | |
| 71 margins_in_points.right = margin_right_in_inch * kPointsPerInch; | |
| 72 return margins_in_points; | |
| 73 } | |
| 58 | 74 |
| 59 HeadlessPrintManager::HeadlessPrintManager(content::WebContents* web_contents) | 75 HeadlessPrintManager::HeadlessPrintManager(content::WebContents* web_contents) |
| 60 : PrintManager(web_contents) { | 76 : PrintManager(web_contents) { |
| 61 Reset(); | 77 Reset(); |
| 62 } | 78 } |
| 63 | 79 |
| 64 HeadlessPrintManager::~HeadlessPrintManager() {} | 80 HeadlessPrintManager::~HeadlessPrintManager() {} |
| 65 | 81 |
| 66 // static | 82 // static |
| 67 std::string HeadlessPrintManager::PrintResultToString(PrintResult result) { | 83 std::string HeadlessPrintManager::PrintResultToString(PrintResult result) { |
| 68 switch (result) { | 84 switch (result) { |
| 69 case PRINT_SUCCESS: | 85 case PRINT_SUCCESS: |
| 70 return std::string(); // no error message | 86 return std::string(); // no error message |
| 71 case PRINTING_FAILED: | 87 case PRINTING_FAILED: |
| 72 return "Printing failed"; | 88 return "Printing failed"; |
| 73 case INVALID_PRINTER_SETTINGS: | 89 case INVALID_PRINTER_SETTINGS: |
| 74 return "Show invalid printer settings error"; | 90 return "Show invalid printer settings error"; |
| 75 case INVALID_MEMORY_HANDLE: | 91 case INVALID_MEMORY_HANDLE: |
| 76 return "Invalid memory handle"; | 92 return "Invalid memory handle"; |
| 77 case METAFILE_MAP_ERROR: | 93 case METAFILE_MAP_ERROR: |
| 78 return "Map to shared memory error"; | 94 return "Map to shared memory error"; |
| 79 case UNEXPECTED_VALID_MEMORY_HANDLE: | 95 case UNEXPECTED_VALID_MEMORY_HANDLE: |
| 80 return "Unexpected valide memory handle"; | 96 return "Unexpected valide memory handle"; |
| 81 case METAFILE_INVALID_HEADER: | 97 case METAFILE_INVALID_HEADER: |
| 82 return "Invalid metafile header"; | 98 return "Invalid metafile header"; |
| 83 case METAFILE_GET_DATA_ERROR: | 99 case METAFILE_GET_DATA_ERROR: |
| 84 return "Get data from metafile error"; | 100 return "Get data from metafile error"; |
| 85 case SIMULTANEOUS_PRINT_ACTIVE: | 101 case SIMULTANEOUS_PRINT_ACTIVE: |
| 86 return "The previous printing job hasn't finished"; | 102 return "The previous printing job hasn't finished"; |
| 103 case EXCEED_PAGE_LIMIT: | |
| 104 return "Page range exceed page count"; | |
|
Eric Seckler
2017/04/20 09:15:28
nit: exceeds
jzfeng
2017/04/27 06:56:06
Done.
| |
| 87 default: | 105 default: |
| 88 NOTREACHED(); | 106 NOTREACHED(); |
| 89 return "Unknown PrintResult"; | 107 return "Unknown PrintResult"; |
| 90 } | 108 } |
| 91 } | 109 } |
| 92 | 110 |
| 93 // static | 111 // static |
| 94 std::unique_ptr<base::DictionaryValue> | 112 std::unique_ptr<base::DictionaryValue> |
| 95 HeadlessPrintManager::PDFContentsToDictionaryValue(const std::string& data) { | 113 HeadlessPrintManager::PDFContentsToDictionaryValue(const std::string& data) { |
| 96 std::string base_64_data; | 114 std::string base_64_data; |
| 97 base::Base64Encode(data, &base_64_data); | 115 base::Base64Encode(data, &base_64_data); |
| 98 auto result = base::MakeUnique<base::DictionaryValue>(); | 116 auto result = base::MakeUnique<base::DictionaryValue>(); |
| 99 result->SetString("data", base_64_data); | 117 result->SetString("data", base_64_data); |
| 100 return result; | 118 return result; |
| 101 } | 119 } |
| 102 | 120 |
| 103 void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, | 121 void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, |
| 122 HeadlessPrintSettings settings, | |
| 104 const GetPDFCallback& callback) { | 123 const GetPDFCallback& callback) { |
| 105 DCHECK(callback); | 124 DCHECK(callback); |
| 106 | 125 |
| 107 if (callback_) { | 126 if (callback_) { |
| 108 callback.Run(SIMULTANEOUS_PRINT_ACTIVE, std::string()); | 127 callback.Run(SIMULTANEOUS_PRINT_ACTIVE, std::string()); |
| 109 return; | 128 return; |
| 110 } | 129 } |
| 111 printing_rfh_ = rfh; | 130 printing_rfh_ = rfh; |
| 112 callback_ = callback; | 131 callback_ = callback; |
| 132 print_params_ = PrintParams(settings); | |
| 113 rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); | 133 rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); |
| 114 } | 134 } |
| 115 | 135 |
| 136 std::unique_ptr<PrintMsg_PrintPages_Params> HeadlessPrintManager::PrintParams( | |
| 137 HeadlessPrintSettings settings) { | |
| 138 PrintSettings print_settings; | |
| 139 print_settings.set_dpi(settings.GetDPI()); | |
| 140 print_settings.set_should_print_backgrounds( | |
| 141 settings.should_print_backgrounds); | |
| 142 print_settings.set_scale_factor(settings.scale); | |
| 143 print_settings.SetOrientation(settings.landscape); | |
| 144 print_settings.set_ranges(settings.page_ranges); | |
| 145 | |
| 146 print_settings.set_display_header_footer(settings.display_header_footer); | |
| 147 if (print_settings.display_header_footer()) { | |
| 148 print_settings.set_url( | |
|
Lei Zhang
2017/04/20 08:35:58
Do you need to set the page title as well? Sanitiz
jzfeng
2017/04/27 06:56:06
Setting page title doesn't work in most case, sinc
| |
| 149 base::UTF8ToUTF16(printing_rfh_->GetLastCommittedURL().spec())); | |
| 150 } | |
| 151 | |
| 152 print_settings.set_margin_type(settings.margin_type); | |
| 153 if (settings.margin_type == CUSTOM_MARGINS) | |
| 154 print_settings.SetCustomMargins(settings.MarginInPoints()); | |
| 155 | |
| 156 gfx::Size physical_size_device_units = settings.PaperSizeInPixel(); | |
| 157 gfx::Rect printable_area_device_units(physical_size_device_units); | |
| 158 print_settings.SetPrinterPrintableArea(physical_size_device_units, | |
| 159 printable_area_device_units, true); | |
| 160 | |
| 161 auto print_params = base::MakeUnique<PrintMsg_PrintPages_Params>(); | |
| 162 RenderParamsFromPrintSettings(print_settings, &print_params->params); | |
| 163 print_params->params.document_cookie = PrintSettings::NewCookie(); | |
| 164 print_params->pages = PageRange::GetPages(settings.page_ranges); | |
| 165 return print_params; | |
| 166 } | |
| 167 | |
| 116 bool HeadlessPrintManager::OnMessageReceived( | 168 bool HeadlessPrintManager::OnMessageReceived( |
| 117 const IPC::Message& message, | 169 const IPC::Message& message, |
| 118 content::RenderFrameHost* render_frame_host) { | 170 content::RenderFrameHost* render_frame_host) { |
| 119 bool handled = true; | 171 bool handled = true; |
| 120 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) | 172 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) |
| 121 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, | 173 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, |
| 122 OnShowInvalidPrinterSettingsError) | 174 OnShowInvalidPrinterSettingsError) |
| 123 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | 175 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) |
| 124 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, | 176 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, |
| 125 OnGetDefaultPrintSettings) | 177 OnGetDefaultPrintSettings) |
| 178 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) | |
| 126 IPC_MESSAGE_UNHANDLED(handled = false) | 179 IPC_MESSAGE_UNHANDLED(handled = false) |
| 127 IPC_END_MESSAGE_MAP() | 180 IPC_END_MESSAGE_MAP() |
| 128 return handled || PrintManager::OnMessageReceived(message, render_frame_host); | 181 return handled || PrintManager::OnMessageReceived(message, render_frame_host); |
| 129 } | 182 } |
| 130 | 183 |
| 131 void HeadlessPrintManager::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { | 184 void HeadlessPrintManager::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { |
| 132 PrintMsg_Print_Params print_params; | |
| 133 RenderParamsFromPrintSettings(GetDefaultPDFPrinterSettings(), &print_params); | |
| 134 print_params.document_cookie = PrintSettings::NewCookie(); | |
| 135 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, | 185 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, |
| 136 print_params); | 186 print_params_->params); |
| 137 printing_rfh_->Send(reply_msg); | 187 printing_rfh_->Send(reply_msg); |
| 138 } | 188 } |
| 139 | 189 |
| 190 void HeadlessPrintManager::OnScriptedPrint( | |
| 191 const PrintHostMsg_ScriptedPrint_Params& params, | |
| 192 IPC::Message* reply_msg) { | |
| 193 if (!print_params_->pages.empty() && | |
| 194 print_params_->pages.back() > params.expected_pages_count) { | |
|
Lei Zhang
2017/04/20 08:35:58
Is the vector of pages sorted? If the range input
jzfeng
2017/04/27 06:56:06
Yes, it is sorted. PageRange::GetPages use a set t
| |
| 195 printing_rfh_->Send(reply_msg); | |
| 196 ReleaseJob(EXCEED_PAGE_LIMIT); | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, *print_params_); | |
| 201 printing_rfh_->Send(reply_msg); | |
| 202 } | |
| 203 | |
| 140 void HeadlessPrintManager::OnShowInvalidPrinterSettingsError() { | 204 void HeadlessPrintManager::OnShowInvalidPrinterSettingsError() { |
| 141 ReleaseJob(INVALID_PRINTER_SETTINGS); | 205 ReleaseJob(INVALID_PRINTER_SETTINGS); |
| 142 } | 206 } |
| 143 | 207 |
| 144 void HeadlessPrintManager::OnPrintingFailed(int cookie) { | 208 void HeadlessPrintManager::OnPrintingFailed(int cookie) { |
| 145 ReleaseJob(PRINTING_FAILED); | 209 ReleaseJob(PRINTING_FAILED); |
| 146 } | 210 } |
| 147 | 211 |
| 212 void HeadlessPrintManager::OnDidGetPrintedPagesCount(int cookie, | |
| 213 int number_pages) { | |
| 214 PrintManager::OnDidGetPrintedPagesCount(cookie, number_pages); | |
| 215 if (!print_params_->pages.empty()) | |
| 216 number_pages_ = print_params_->pages.size(); | |
|
Lei Zhang
2017/04/20 08:35:58
Why do we need to do this?
jzfeng
2017/04/27 06:56:06
number_pages returned from the renderer is always
Lei Zhang
2017/04/27 22:17:46
Is this the issue described in https://crbug.com/1
jzfeng
2017/05/02 07:50:55
The bug you found is right. The reason why chrome
| |
| 217 } | |
| 218 | |
| 148 void HeadlessPrintManager::OnDidPrintPage( | 219 void HeadlessPrintManager::OnDidPrintPage( |
| 149 const PrintHostMsg_DidPrintPage_Params& params) { | 220 const PrintHostMsg_DidPrintPage_Params& params) { |
| 150 if (!callback_) { | 221 if (!callback_) { |
| 151 DLOG(ERROR) | 222 DLOG(ERROR) |
| 152 << "Unexpected PrintHostMsg_DidPrintPage message from the renderer"; | 223 << "Unexpected PrintHostMsg_DidPrintPage message from the renderer"; |
| 153 return; | 224 return; |
| 154 } | 225 } |
| 155 | 226 |
| 156 const bool metafile_must_be_valid = expecting_first_page_; | 227 const bool metafile_must_be_valid = expecting_first_page_; |
| 157 expecting_first_page_ = false; | 228 expecting_first_page_ = false; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 186 } | 257 } |
| 187 } | 258 } |
| 188 | 259 |
| 189 if (--number_pages_ == 0) | 260 if (--number_pages_ == 0) |
| 190 ReleaseJob(PRINT_SUCCESS); | 261 ReleaseJob(PRINT_SUCCESS); |
| 191 } | 262 } |
| 192 | 263 |
| 193 void HeadlessPrintManager::Reset() { | 264 void HeadlessPrintManager::Reset() { |
| 194 printing_rfh_ = nullptr; | 265 printing_rfh_ = nullptr; |
| 195 callback_.Reset(); | 266 callback_.Reset(); |
| 267 print_params_.reset(nullptr); | |
|
Lei Zhang
2017/04/20 08:35:58
There's no need to pass nullptr to std::unique_ptr
jzfeng
2017/04/27 06:56:06
Done.
| |
| 196 data_.clear(); | 268 data_.clear(); |
| 197 expecting_first_page_ = true; | 269 expecting_first_page_ = true; |
| 198 number_pages_ = 0; | 270 number_pages_ = 0; |
| 199 } | 271 } |
| 200 | 272 |
| 201 void HeadlessPrintManager::ReleaseJob(PrintResult result) { | 273 void HeadlessPrintManager::ReleaseJob(PrintResult result) { |
| 202 if (!callback_) { | 274 if (!callback_) { |
| 203 DLOG(ERROR) << "ReleaseJob is called when callback_ is null. Check whether " | 275 DLOG(ERROR) << "ReleaseJob is called when callback_ is null. Check whether " |
| 204 "ReleaseJob is called more than once."; | 276 "ReleaseJob is called more than once."; |
| 205 return; | 277 return; |
| 206 } | 278 } |
| 207 | 279 |
| 208 if (result == PRINT_SUCCESS) | 280 if (result == PRINT_SUCCESS) |
| 209 callback_.Run(result, std::move(data_)); | 281 callback_.Run(result, std::move(data_)); |
| 210 else | 282 else |
| 211 callback_.Run(result, std::string()); | 283 callback_.Run(result, std::string()); |
| 212 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), | 284 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), |
| 213 result == PRINT_SUCCESS)); | 285 result == PRINT_SUCCESS)); |
| 214 Reset(); | 286 Reset(); |
| 215 } | 287 } |
| 216 | 288 |
| 217 } // namespace printing | 289 } // namespace printing |
| OLD | NEW |