Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 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 #if defined(OS_MACOSX) | |
| 31 // On the Mac, the printable area is in points, use kPointsPerInch to compute | |
| 32 // its bounds. | |
| 33 int dpi = kPointsPerInch; | |
| 34 #else | |
| 35 int dpi = kDefaultPdfDpi; | |
| 36 #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 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 HeadlessPrintManager::HeadlessPrintManager(content::WebContents* web_contents) | |
| 60 : PrintManager(web_contents) { | |
| 61 Reset(); | |
| 62 } | |
| 63 | |
| 64 HeadlessPrintManager::~HeadlessPrintManager() {} | |
| 65 | |
| 66 // static | |
| 67 std::string HeadlessPrintManager::PrintResultToString(PrintResult result) { | |
| 68 switch (result) { | |
| 69 case PRINT_SUCCESS: | |
| 70 return std::string(); // no error message | |
| 71 case PRINTING_FAILED: | |
| 72 return "Printing failed"; | |
| 73 case INVALID_PRINTER_SETTINGS: | |
| 74 return "Show invalid printer settings error"; | |
| 75 case INVALID_MEMORY_HANDLE: | |
| 76 return "Invalid memory handle"; | |
| 77 case METAFILE_MAP_ERROR: | |
| 78 return "Map to shared memory error"; | |
| 79 case UNEXPECTED_VALID_MEMORY_HANDLE: | |
| 80 return "Unexpected valide memory handle"; | |
| 81 case METAFILE_INVALID_HEADER: | |
| 82 return "Invalid metafile header"; | |
| 83 case METAFILE_GET_DATA_ERROR: | |
| 84 return "Get data from metafile error"; | |
| 85 case SIMULTANEOUS_PRINT_ACTIVE: | |
| 86 return "The previous printing job hasn't finished"; | |
| 87 default: | |
| 88 NOTREACHED(); | |
| 89 return "Unknown PrintResult"; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // static | |
| 94 std::unique_ptr<base::DictionaryValue> | |
| 95 HeadlessPrintManager::PDFContentsToDictionaryValue(const std::string& data) { | |
| 96 std::string base_64_data; | |
| 97 base::Base64Encode(data, &base_64_data); | |
| 98 auto result = base::MakeUnique<base::DictionaryValue>(); | |
| 99 result->SetString("data", base_64_data); | |
| 100 return result; | |
| 101 } | |
| 102 | |
| 103 void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, | |
| 104 const GetPDFCallback& callback) { | |
| 105 DCHECK(callback); | |
|
Lei Zhang
2017/04/06 10:23:00
DCHECK and then handling the DCHECK failure is not
Eric Seckler
2017/04/06 10:28:55
This is actually fine, I think, because the DCHECK
Lei Zhang
2017/04/06 10:46:40
*blink* *blink*
| |
| 106 | |
| 107 if (callback_) { | |
| 108 callback.Run(SIMULTANEOUS_PRINT_ACTIVE, std::string()); | |
| 109 return; | |
| 110 } | |
| 111 printing_rfh_ = rfh; | |
| 112 callback_ = callback; | |
| 113 rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); | |
| 114 } | |
| 115 | |
| 116 bool HeadlessPrintManager::OnMessageReceived( | |
| 117 const IPC::Message& message, | |
| 118 content::RenderFrameHost* render_frame_host) { | |
| 119 bool handled = true; | |
| 120 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) | |
| 121 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, | |
| 122 OnShowInvalidPrinterSettingsError) | |
| 123 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | |
| 124 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, | |
| 125 OnGetDefaultPrintSettings) | |
| 126 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 127 IPC_END_MESSAGE_MAP() | |
| 128 return handled || PrintManager::OnMessageReceived(message, render_frame_host); | |
| 129 } | |
| 130 | |
| 131 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, | |
| 136 print_params); | |
| 137 printing_rfh_->Send(reply_msg); | |
| 138 } | |
| 139 | |
| 140 void HeadlessPrintManager::OnShowInvalidPrinterSettingsError() { | |
| 141 ReleaseJob(INVALID_PRINTER_SETTINGS); | |
| 142 } | |
| 143 | |
| 144 void HeadlessPrintManager::OnPrintingFailed(int cookie) { | |
| 145 ReleaseJob(PRINTING_FAILED); | |
| 146 } | |
| 147 | |
| 148 void HeadlessPrintManager::OnDidPrintPage( | |
| 149 const PrintHostMsg_DidPrintPage_Params& params) { | |
| 150 DCHECK(callback_); | |
|
Lei Zhang
2017/04/06 10:23:00
Yes, actually handle this. Don't fully trust rende
| |
| 151 | |
| 152 const bool metafile_must_be_valid = expecting_first_page_; | |
| 153 expecting_first_page_ = false; | |
| 154 | |
| 155 if (metafile_must_be_valid) { | |
| 156 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
| 157 ReleaseJob(INVALID_MEMORY_HANDLE); | |
| 158 return; | |
| 159 } | |
| 160 auto shared_buf = | |
| 161 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); | |
| 162 if (!shared_buf->Map(params.data_size)) { | |
| 163 ReleaseJob(METAFILE_MAP_ERROR); | |
| 164 return; | |
| 165 } | |
| 166 auto metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); | |
| 167 if (!metafile->InitFromData(shared_buf->memory(), params.data_size)) { | |
| 168 ReleaseJob(METAFILE_INVALID_HEADER); | |
| 169 return; | |
| 170 } | |
| 171 std::vector<char> buffer; | |
| 172 if (!metafile->GetDataAsVector(&buffer)) { | |
| 173 ReleaseJob(METAFILE_GET_DATA_ERROR); | |
| 174 return; | |
| 175 } | |
| 176 data_ = std::string(buffer.data(), buffer.size()); | |
| 177 } else { | |
| 178 if (base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
| 179 base::SharedMemory::CloseHandle(params.metafile_data_handle); | |
| 180 ReleaseJob(UNEXPECTED_VALID_MEMORY_HANDLE); | |
| 181 return; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 if (--number_pages_ == 0) | |
| 186 ReleaseJob(PRINT_SUCCESS); | |
| 187 } | |
| 188 | |
| 189 void HeadlessPrintManager::Reset() { | |
| 190 printing_rfh_ = nullptr; | |
| 191 callback_.Reset(); | |
| 192 data_.clear(); | |
| 193 expecting_first_page_ = true; | |
| 194 number_pages_ = 0; | |
| 195 } | |
| 196 | |
| 197 void HeadlessPrintManager::ReleaseJob(PrintResult result) { | |
| 198 DCHECK(callback_); | |
| 199 | |
| 200 if (result == PRINT_SUCCESS) | |
| 201 callback_.Run(result, std::move(data_)); | |
| 202 else | |
| 203 callback_.Run(result, std::string()); | |
| 204 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), | |
| 205 result == PRINT_SUCCESS)); | |
| 206 Reset(); | |
| 207 } | |
| 208 | |
| 209 } // namespace printing | |
| OLD | NEW |