| 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 int dpi = kDefaultPdfDpi; |
| 31 gfx::Size physical_size_device_units; |
| 32 gfx::Rect printable_area_device_units; |
| 33 double page_width_in_pixel = kLetterWidthInch * dpi; |
| 34 double page_height_in_pixel = 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 Reset(); |
| 56 } |
| 57 |
| 58 HeadlessPrintManager::~HeadlessPrintManager() {} |
| 59 |
| 60 // static |
| 61 std::string HeadlessPrintManager::PrintResultToString(PrintResult result) { |
| 62 switch (result) { |
| 63 case PRINT_SUCCESS: |
| 64 return std::string(); // no error message |
| 65 case PRINTING_FAILED: |
| 66 return "PrintingFailed"; |
| 67 case INVALID_PRINTER_SETTINGS: |
| 68 return "ShowInvalidPrinterSettingsError"; |
| 69 case INVALID_MEMORY_HANDLE: |
| 70 return "Invalid memory handle"; |
| 71 case METAFILE_MAP_ERROR: |
| 72 return "Map to SharedMemory error"; |
| 73 case UNEXPECTED_VALID_MEMORY_HANDLE: |
| 74 return "Unexpected valide memory handle"; |
| 75 case METAFILE_INVALID_HEADER: |
| 76 return "Invalid metafile header"; |
| 77 case METAFILE_GET_DATA_ERROR: |
| 78 return "Get data from metafile error"; |
| 79 case SIMULTANEOUS_PRINT_ACTIVE: |
| 80 return "The previous printing job hasn't finished"; |
| 81 default: |
| 82 NOTREACHED(); |
| 83 return "Unknown PrintResult"; |
| 84 } |
| 85 } |
| 86 |
| 87 // static |
| 88 std::unique_ptr<base::DictionaryValue> |
| 89 HeadlessPrintManager::PDFContentsToDictionaryValue(const std::string& data) { |
| 90 std::string base_64_data; |
| 91 base::Base64Encode(data, &base_64_data); |
| 92 auto result = base::MakeUnique<base::DictionaryValue>(); |
| 93 result->SetString("data", base_64_data); |
| 94 return result; |
| 95 } |
| 96 |
| 97 void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, |
| 98 const GetPDFCallback& callback) { |
| 99 DCHECK(callback); |
| 100 if (!callback_.is_null()) { |
| 101 callback.Run(SIMULTANEOUS_PRINT_ACTIVE, std::string()); |
| 102 return; |
| 103 } |
| 104 printing_rfh_ = rfh; |
| 105 callback_ = callback; |
| 106 rfh->Send(new PrintMsg_PrintPages(rfh->GetRoutingID())); |
| 107 } |
| 108 |
| 109 bool HeadlessPrintManager::OnMessageReceived( |
| 110 const IPC::Message& message, |
| 111 content::RenderFrameHost* render_frame_host) { |
| 112 bool handled = true; |
| 113 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) |
| 114 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, |
| 115 OnShowInvalidPrinterSettingsError) |
| 116 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) |
| 117 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, |
| 118 OnGetDefaultPrintSettings) |
| 119 IPC_MESSAGE_UNHANDLED(handled = false) |
| 120 IPC_END_MESSAGE_MAP() |
| 121 return handled || PrintManager::OnMessageReceived(message, render_frame_host); |
| 122 } |
| 123 |
| 124 void HeadlessPrintManager::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { |
| 125 PrintMsg_Print_Params print_params; |
| 126 RenderParamsFromPrintSettings(GetDefaultPDFPrinterSettings(), &print_params); |
| 127 print_params.document_cookie = PrintSettings::NewCookie(); |
| 128 PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, |
| 129 print_params); |
| 130 printing_rfh_->Send(reply_msg); |
| 131 } |
| 132 |
| 133 void HeadlessPrintManager::OnShowInvalidPrinterSettingsError() { |
| 134 ReleaseJob(INVALID_PRINTER_SETTINGS); |
| 135 } |
| 136 |
| 137 void HeadlessPrintManager::OnPrintingFailed(int cookie) { |
| 138 ReleaseJob(PRINTING_FAILED); |
| 139 } |
| 140 |
| 141 void HeadlessPrintManager::OnDidPrintPage( |
| 142 const PrintHostMsg_DidPrintPage_Params& params) { |
| 143 const bool metafile_must_be_valid = expecting_first_page_; |
| 144 expecting_first_page_ = false; |
| 145 |
| 146 // Only used when |metafile_must_be_valid| is true. |
| 147 std::unique_ptr<base::SharedMemory> shared_buf; |
| 148 if (metafile_must_be_valid) { |
| 149 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { |
| 150 ReleaseJob(INVALID_MEMORY_HANDLE); |
| 151 return; |
| 152 } |
| 153 shared_buf = |
| 154 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); |
| 155 if (!shared_buf->Map(params.data_size)) { |
| 156 ReleaseJob(METAFILE_MAP_ERROR); |
| 157 return; |
| 158 } |
| 159 } else { |
| 160 if (base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { |
| 161 base::SharedMemory::CloseHandle(params.metafile_data_handle); |
| 162 ReleaseJob(UNEXPECTED_VALID_MEMORY_HANDLE); |
| 163 return; |
| 164 } |
| 165 } |
| 166 |
| 167 auto metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); |
| 168 if (metafile_must_be_valid) { |
| 169 if (!metafile->InitFromData(shared_buf->memory(), params.data_size)) { |
| 170 ReleaseJob(METAFILE_INVALID_HEADER); |
| 171 return; |
| 172 } |
| 173 std::vector<char> buffer; |
| 174 if (!metafile->GetDataAsVector(&buffer)) { |
| 175 ReleaseJob(METAFILE_GET_DATA_ERROR); |
| 176 return; |
| 177 } |
| 178 data_ = std::string(buffer.data(), buffer.size()); |
| 179 } |
| 180 |
| 181 if (--number_pages_ == 0) |
| 182 ReleaseJob(PRINT_SUCCESS); |
| 183 } |
| 184 |
| 185 void HeadlessPrintManager::Reset() { |
| 186 printing_rfh_ = nullptr; |
| 187 callback_.Reset(); |
| 188 data_.clear(); |
| 189 expecting_first_page_ = true; |
| 190 number_pages_ = 0; |
| 191 } |
| 192 |
| 193 void HeadlessPrintManager::ReleaseJob(PrintResult result) { |
| 194 if (result == PRINT_SUCCESS) |
| 195 callback_.Run(result, std::move(data_)); |
| 196 else |
| 197 callback_.Run(result, std::string()); |
| 198 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), |
| 199 result == PRINT_SUCCESS)); |
| 200 Reset(); |
| 201 } |
| 202 |
| 203 } // namespace printing |
| OLD | NEW |