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 int dpi = kPointsPerInch; | |
|
Lei Zhang
2017/04/06 08:08:42
Copy over the comment from GetDpi() to explain why
jzfeng
2017/04/06 09:01:51
Done.
| |
| 32 #else | |
| 33 int dpi = kDefaultPdfDpi; | |
| 34 #endif | |
| 35 gfx::Size physical_size_device_units; | |
| 36 gfx::Rect printable_area_device_units; | |
| 37 double page_width_in_pixel = kLetterWidthInch * dpi; | |
| 38 double page_height_in_pixel = kLetterHeightInch * dpi; | |
| 39 physical_size_device_units.SetSize(static_cast<int>(page_width_in_pixel), | |
| 40 static_cast<int>(page_height_in_pixel)); | |
| 41 printable_area_device_units.SetRect( | |
| 42 static_cast<int>(kLeftMarginInInch * dpi), | |
| 43 static_cast<int>(kTopMarginInInch * dpi), | |
| 44 page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi, | |
| 45 page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi); | |
| 46 | |
| 47 PrintSettings settings; | |
| 48 settings.set_dpi(dpi); | |
| 49 settings.SetPrinterPrintableArea(physical_size_device_units, | |
| 50 printable_area_device_units, true); | |
| 51 settings.set_should_print_backgrounds(true); | |
| 52 return settings; | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 HeadlessPrintManager::HeadlessPrintManager(content::WebContents* web_contents) | |
| 58 : PrintManager(web_contents) { | |
| 59 Reset(); | |
| 60 } | |
| 61 | |
| 62 HeadlessPrintManager::~HeadlessPrintManager() {} | |
| 63 | |
| 64 // static | |
| 65 std::string HeadlessPrintManager::PrintResultToString(PrintResult result) { | |
| 66 switch (result) { | |
| 67 case PRINT_SUCCESS: | |
| 68 return std::string(); // no error message | |
| 69 case PRINTING_FAILED: | |
| 70 return "PrintingFailed"; | |
| 71 case INVALID_PRINTER_SETTINGS: | |
| 72 return "ShowInvalidPrinterSettingsError"; | |
| 73 case INVALID_MEMORY_HANDLE: | |
| 74 return "Invalid memory handle"; | |
|
Lei Zhang
2017/04/06 08:08:42
The ones above this DoNotHaveSpaces.
jzfeng
2017/04/06 09:01:51
Done.
| |
| 75 case METAFILE_MAP_ERROR: | |
| 76 return "Map to SharedMemory error"; | |
| 77 case UNEXPECTED_VALID_MEMORY_HANDLE: | |
| 78 return "Unexpected valide memory handle"; | |
| 79 case METAFILE_INVALID_HEADER: | |
| 80 return "Invalid metafile header"; | |
| 81 case METAFILE_GET_DATA_ERROR: | |
| 82 return "Get data from metafile error"; | |
| 83 case SIMULTANEOUS_PRINT_ACTIVE: | |
| 84 return "The previous printing job hasn't finished"; | |
| 85 default: | |
| 86 NOTREACHED(); | |
| 87 return "Unknown PrintResult"; | |
| 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 auto result = base::MakeUnique<base::DictionaryValue>(); | |
| 97 result->SetString("data", base_64_data); | |
| 98 return result; | |
| 99 } | |
| 100 | |
| 101 void HeadlessPrintManager::GetPDFContents(content::RenderFrameHost* rfh, | |
| 102 const GetPDFCallback& callback) { | |
| 103 DCHECK(callback); | |
| 104 if (!callback_.is_null()) { | |
|
Lei Zhang
2017/04/06 08:08:42
I think you can write: if (callback_) {, just like
jzfeng
2017/04/06 09:01:51
Done.
| |
| 105 callback.Run(SIMULTANEOUS_PRINT_ACTIVE, std::string()); | |
| 106 return; | |
| 107 } | |
| 108 printing_rfh_ = rfh; | |
| 109 callback_ = callback; | |
| 110 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(INVALID_PRINTER_SETTINGS); | |
| 139 } | |
| 140 | |
| 141 void HeadlessPrintManager::OnPrintingFailed(int cookie) { | |
| 142 ReleaseJob(PRINTING_FAILED); | |
| 143 } | |
| 144 | |
| 145 void HeadlessPrintManager::OnDidPrintPage( | |
| 146 const PrintHostMsg_DidPrintPage_Params& params) { | |
|
Lei Zhang
2017/04/06 08:08:42
Add a check to defend against spurious messages:
jzfeng
2017/04/06 09:01:51
Use DCHECK instead, since callback_ should not be
Eric Seckler
2017/04/06 09:14:25
I believe Lei meant that we shouldn't trust the re
jzfeng
2017/04/07 03:24:06
Thanks for the explanation! Done.
| |
| 147 const bool metafile_must_be_valid = expecting_first_page_; | |
| 148 expecting_first_page_ = false; | |
| 149 | |
| 150 // Only used when |metafile_must_be_valid| is true. | |
| 151 std::unique_ptr<base::SharedMemory> shared_buf; | |
| 152 if (metafile_must_be_valid) { | |
| 153 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
| 154 ReleaseJob(INVALID_MEMORY_HANDLE); | |
| 155 return; | |
| 156 } | |
| 157 shared_buf = | |
| 158 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); | |
| 159 if (!shared_buf->Map(params.data_size)) { | |
| 160 ReleaseJob(METAFILE_MAP_ERROR); | |
| 161 return; | |
| 162 } | |
| 163 } else { | |
| 164 if (base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | |
| 165 base::SharedMemory::CloseHandle(params.metafile_data_handle); | |
| 166 ReleaseJob(UNEXPECTED_VALID_MEMORY_HANDLE); | |
| 167 return; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 auto metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); | |
|
Lei Zhang
2017/04/06 08:08:42
You can merge this block of code with the if (meta
jzfeng
2017/04/06 09:01:51
Good catch! Done.
| |
| 172 if (metafile_must_be_valid) { | |
| 173 if (!metafile->InitFromData(shared_buf->memory(), params.data_size)) { | |
| 174 ReleaseJob(METAFILE_INVALID_HEADER); | |
| 175 return; | |
| 176 } | |
| 177 std::vector<char> buffer; | |
| 178 if (!metafile->GetDataAsVector(&buffer)) { | |
| 179 ReleaseJob(METAFILE_GET_DATA_ERROR); | |
| 180 return; | |
| 181 } | |
| 182 data_ = std::string(buffer.data(), buffer.size()); | |
| 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) { | |
|
Lei Zhang
2017/04/06 08:08:42
Add a check to defend against spurious messages he
jzfeng
2017/04/06 09:01:51
Done.
Eric Seckler
2017/04/06 09:14:25
Same here, DCHECK doesn't protect us from spurious
jzfeng
2017/04/07 03:24:06
Done.
| |
| 198 if (result == PRINT_SUCCESS) | |
| 199 callback_.Run(result, std::move(data_)); | |
| 200 else | |
| 201 callback_.Run(result, std::string()); | |
| 202 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), | |
| 203 result == PRINT_SUCCESS)); | |
| 204 Reset(); | |
| 205 } | |
| 206 | |
| 207 } // namespace printing | |
| OLD | NEW |