| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/utility/printing_handler.h" | 5 #include "chrome/utility/printing_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters); | 57 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters); |
| 58 #endif | 58 #endif |
| 59 } | 59 } |
| 60 | 60 |
| 61 PrintingHandler::~PrintingHandler() {} | 61 PrintingHandler::~PrintingHandler() {} |
| 62 | 62 |
| 63 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { | 63 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { |
| 64 bool handled = true; | 64 bool handled = true; |
| 65 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) | 65 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) |
| 66 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
| 67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, | 67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Start, |
| 68 OnRenderPDFPagesToMetafile) | 68 OnRenderPDFPagesToMetafileStart) |
| 69 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, | 69 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, |
| 70 OnRenderPDFPagesToMetafileGetPage) | 70 OnRenderPDFPagesToMetafileGetPage) |
| 71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, | 71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, |
| 72 OnRenderPDFPagesToMetafileStop) | 72 OnRenderPDFPagesToMetafileStop) |
| 73 #endif // OS_WIN | 73 #endif // OS_WIN |
| 74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, | 75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, |
| 76 OnRenderPDFPagesToPWGRaster) | 76 OnRenderPDFPagesToPWGRaster) |
| 77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | 77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, |
| 78 OnGetPrinterCapsAndDefaults) | 78 OnGetPrinterCapsAndDefaults) |
| 79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, | 79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, |
| 80 OnGetPrinterSemanticCapsAndDefaults) | 80 OnGetPrinterSemanticCapsAndDefaults) |
| 81 #endif // ENABLE_PRINT_PREVIEW | 81 #endif // ENABLE_PRINT_PREVIEW |
| 82 IPC_MESSAGE_UNHANDLED(handled = false) | 82 IPC_MESSAGE_UNHANDLED(handled = false) |
| 83 IPC_END_MESSAGE_MAP() | 83 IPC_END_MESSAGE_MAP() |
| 84 return handled; | 84 return handled; |
| 85 } | 85 } |
| 86 | 86 |
| 87 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
| 88 void PrintingHandler::OnRenderPDFPagesToMetafile( | 88 void PrintingHandler::OnRenderPDFPagesToMetafileStart( |
| 89 IPC::PlatformFileForTransit pdf_transit, | 89 IPC::PlatformFileForTransit pdf_transit, |
| 90 const PdfRenderSettings& settings, | 90 const PdfRenderSettings& settings, |
| 91 bool print_text_with_gdi) { | 91 bool print_text_with_gdi) { |
| 92 pdf_rendering_settings_ = settings; | 92 pdf_rendering_settings_ = settings; |
| 93 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi); | 93 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi); |
| 94 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); | 94 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); |
| 95 int page_count = LoadPDF(std::move(pdf_file)); | 95 int page_count = LoadPDF(std::move(pdf_file)); |
| 96 Send( | 96 Send( |
| 97 new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count)); | 97 new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count)); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void PrintingHandler::OnRenderPDFPagesToMetafileGetPage( | 100 void PrintingHandler::OnRenderPDFPagesToMetafileGetPage( |
| 101 int page_number, | 101 int page_number, |
| 102 IPC::PlatformFileForTransit output_file) { | 102 IPC::PlatformFileForTransit output_file) { |
| 103 base::File emf_file = IPC::PlatformFileForTransitToFile(output_file); | 103 base::File emf_file = IPC::PlatformFileForTransitToFile(output_file); |
| 104 float scale_factor = 1.0f; | 104 float scale_factor = 1.0f; |
| 105 bool success = | 105 bool success = |
| 106 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); | 106 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); |
| 107 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone( | 107 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone( |
| 108 success, scale_factor)); | 108 success, scale_factor)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void PrintingHandler::OnRenderPDFPagesToMetafileStop() { | 111 void PrintingHandler::OnRenderPDFPagesToMetafileStop() { |
| 112 chrome_pdf::ReleasePDFHandle(pdf_handle_); |
| 113 pdf_handle_ = nullptr; |
| 112 ReleaseProcessIfNeeded(); | 114 ReleaseProcessIfNeeded(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 #endif // OS_WIN | 117 #endif // OS_WIN |
| 116 | 118 |
| 117 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 119 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 118 void PrintingHandler::OnRenderPDFPagesToPWGRaster( | 120 void PrintingHandler::OnRenderPDFPagesToPWGRaster( |
| 119 IPC::PlatformFileForTransit pdf_transit, | 121 IPC::PlatformFileForTransit pdf_transit, |
| 120 const PdfRenderSettings& settings, | 122 const PdfRenderSettings& settings, |
| 121 const PwgRasterSettings& bitmap_settings, | 123 const PwgRasterSettings& bitmap_settings, |
| 122 IPC::PlatformFileForTransit bitmap_transit) { | 124 IPC::PlatformFileForTransit bitmap_transit) { |
| 123 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); | 125 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); |
| 124 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); | 126 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); |
| 125 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, | 127 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, |
| 126 std::move(bitmap))) { | 128 std::move(bitmap))) { |
| 127 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | 129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); |
| 128 } else { | 130 } else { |
| 129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | 131 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); |
| 130 } | 132 } |
| 131 ReleaseProcessIfNeeded(); | 133 ReleaseProcessIfNeeded(); |
| 132 } | 134 } |
| 133 #endif // ENABLE_PRINT_PREVIEW | 135 #endif // ENABLE_PRINT_PREVIEW |
| 134 | 136 |
| 135 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
| 136 int PrintingHandler::LoadPDF(base::File pdf_file) { | 138 int PrintingHandler::LoadPDF(base::File pdf_file) { |
| 139 DCHECK(!pdf_handle_); |
| 140 |
| 137 int64_t length64 = pdf_file.GetLength(); | 141 int64_t length64 = pdf_file.GetLength(); |
| 138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 142 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
| 139 return 0; | 143 return 0; |
| 140 int length = static_cast<int>(length64); | 144 int length = static_cast<int>(length64); |
| 141 | 145 |
| 142 pdf_data_.resize(length); | 146 pdf_data_.resize(length); |
| 143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 147 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
| 144 return 0; | 148 return 0; |
| 145 | 149 |
| 146 int total_page_count = 0; | 150 int total_page_count = 0; |
| 147 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), | 151 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), |
| 148 &total_page_count, nullptr)) { | 152 &total_page_count, nullptr, &pdf_handle_)) { |
| 149 return 0; | 153 return 0; |
| 150 } | 154 } |
| 151 return total_page_count; | 155 return total_page_count; |
| 152 } | 156 } |
| 153 | 157 |
| 154 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, | 158 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, |
| 155 base::File output_file, | 159 base::File output_file, |
| 156 float* scale_factor) { | 160 float* scale_factor) { |
| 157 Emf metafile; | 161 Emf metafile; |
| 158 metafile.Init(); | 162 metafile.Init(); |
| 159 | 163 |
| 160 // We need to scale down DC to fit an entire page into DC available area. | 164 // We need to scale down DC to fit an entire page into DC available area. |
| 161 // Current metafile is based on screen DC and have current screen size. | 165 // Current metafile is based on screen DC and have current screen size. |
| 162 // Writing outside of those boundaries will result in the cut-off output. | 166 // Writing outside of those boundaries will result in the cut-off output. |
| 163 // On metafiles (this is the case here), scaling down will still record | 167 // On metafiles (this is the case here), scaling down will still record |
| 164 // original coordinates and we'll be able to print in full resolution. | 168 // original coordinates and we'll be able to print in full resolution. |
| 165 // Before playback we'll need to counter the scaling up that will happen | 169 // Before playback we'll need to counter the scaling up that will happen |
| 166 // in the service (print_system_win.cc). | 170 // in the service (print_system_win.cc). |
| 167 *scale_factor = | 171 *scale_factor = |
| 168 gfx::CalculatePageScale(metafile.context(), | 172 gfx::CalculatePageScale(metafile.context(), |
| 169 pdf_rendering_settings_.area().right(), | 173 pdf_rendering_settings_.area().right(), |
| 170 pdf_rendering_settings_.area().bottom()); | 174 pdf_rendering_settings_.area().bottom()); |
| 171 gfx::ScaleDC(metafile.context(), *scale_factor); | 175 gfx::ScaleDC(metafile.context(), *scale_factor); |
| 172 | 176 |
| 173 // The underlying metafile is of type Emf and ignores the arguments passed | 177 // The underlying metafile is of type Emf and ignores the arguments passed |
| 174 // to StartPage. | 178 // to StartPage. |
| 175 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | 179 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
| 176 if (!chrome_pdf::RenderPDFPageToDC( | 180 if (!chrome_pdf::RenderPDFPageToDC( |
| 177 &pdf_data_.front(), | 181 pdf_handle_, page_number, metafile.context(), |
| 178 pdf_data_.size(), | 182 pdf_rendering_settings_.dpi(), pdf_rendering_settings_.area().x(), |
| 179 page_number, | |
| 180 metafile.context(), | |
| 181 pdf_rendering_settings_.dpi(), | |
| 182 pdf_rendering_settings_.area().x(), | |
| 183 pdf_rendering_settings_.area().y(), | 183 pdf_rendering_settings_.area().y(), |
| 184 pdf_rendering_settings_.area().width(), | 184 pdf_rendering_settings_.area().width(), |
| 185 pdf_rendering_settings_.area().height(), | 185 pdf_rendering_settings_.area().height(), true, false, true, true, |
| 186 true, | |
| 187 false, | |
| 188 true, | |
| 189 true, | |
| 190 pdf_rendering_settings_.autorotate())) { | 186 pdf_rendering_settings_.autorotate())) { |
| 191 return false; | 187 return false; |
| 192 } | 188 } |
| 193 metafile.FinishPage(); | 189 metafile.FinishPage(); |
| 194 metafile.FinishDocument(); | 190 metafile.FinishDocument(); |
| 195 return metafile.SaveTo(&output_file); | 191 return metafile.SaveTo(&output_file); |
| 196 } | 192 } |
| 197 | 193 |
| 198 #endif // OS_WIN | 194 #endif // OS_WIN |
| 199 | 195 |
| 200 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 196 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 201 bool PrintingHandler::RenderPDFPagesToPWGRaster( | 197 bool PrintingHandler::RenderPDFPagesToPWGRaster( |
| 202 base::File pdf_file, | 198 base::File pdf_file, |
| 203 const PdfRenderSettings& settings, | 199 const PdfRenderSettings& settings, |
| 204 const PwgRasterSettings& bitmap_settings, | 200 const PwgRasterSettings& bitmap_settings, |
| 205 base::File bitmap_file) { | 201 base::File bitmap_file) { |
| 206 bool autoupdate = true; | 202 bool autoupdate = true; |
| 207 base::File::Info info; | 203 base::File::Info info; |
| 208 if (!pdf_file.GetInfo(&info) || info.size <= 0 || | 204 if (!pdf_file.GetInfo(&info) || info.size <= 0 || |
| 209 info.size > std::numeric_limits<int>::max()) | 205 info.size > std::numeric_limits<int>::max()) |
| 210 return false; | 206 return false; |
| 211 int data_size = static_cast<int>(info.size); | 207 int data_size = static_cast<int>(info.size); |
| 212 | 208 |
| 213 std::string data(data_size, 0); | 209 std::string data(data_size, 0); |
| 214 if (pdf_file.Read(0, &data[0], data_size) != data_size) | 210 if (pdf_file.Read(0, &data[0], data_size) != data_size) |
| 215 return false; | 211 return false; |
| 216 | 212 |
| 217 int total_page_count = 0; | 213 int total_page_count = 0; |
| 214 void* pdf_handle = nullptr; |
| 218 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, &total_page_count, | 215 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, &total_page_count, |
| 219 nullptr)) { | 216 nullptr, &pdf_handle)) { |
| 220 return false; | 217 return false; |
| 221 } | 218 } |
| 222 | 219 |
| 223 cloud_print::PwgEncoder encoder; | 220 cloud_print::PwgEncoder encoder; |
| 224 std::string pwg_header; | 221 std::string pwg_header; |
| 225 encoder.EncodeDocumentHeader(&pwg_header); | 222 encoder.EncodeDocumentHeader(&pwg_header); |
| 226 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), | 223 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), |
| 227 pwg_header.size()); | 224 pwg_header.size()); |
| 228 if (bytes_written != static_cast<int>(pwg_header.size())) | 225 if (bytes_written != static_cast<int>(pwg_header.size())) { |
| 226 chrome_pdf::ReleasePDFHandle(pdf_handle); |
| 229 return false; | 227 return false; |
| 228 } |
| 230 | 229 |
| 231 cloud_print::BitmapImage image(settings.area().size(), | 230 cloud_print::BitmapImage image(settings.area().size(), |
| 232 cloud_print::BitmapImage::BGRA); | 231 cloud_print::BitmapImage::BGRA); |
| 232 bool ret = true; |
| 233 for (int i = 0; i < total_page_count; ++i) { | 233 for (int i = 0; i < total_page_count; ++i) { |
| 234 int page_number = i; | 234 int page_number = i; |
| 235 | 235 |
| 236 if (bitmap_settings.reverse_page_order) { | 236 if (bitmap_settings.reverse_page_order) { |
| 237 page_number = total_page_count - 1 - page_number; | 237 page_number = total_page_count - 1 - page_number; |
| 238 } | 238 } |
| 239 | 239 |
| 240 if (!chrome_pdf::RenderPDFPageToBitmap(data.data(), | 240 if (!chrome_pdf::RenderPDFPageToBitmap( |
| 241 data_size, | 241 pdf_handle, page_number, image.pixel_data(), image.size().width(), |
| 242 page_number, | 242 image.size().height(), settings.dpi(), autoupdate)) { |
| 243 image.pixel_data(), | 243 ret = false; |
| 244 image.size().width(), | 244 break; |
| 245 image.size().height(), | |
| 246 settings.dpi(), | |
| 247 autoupdate)) { | |
| 248 return false; | |
| 249 } | 245 } |
| 250 | 246 |
| 251 cloud_print::PwgHeaderInfo header_info; | 247 cloud_print::PwgHeaderInfo header_info; |
| 252 header_info.dpi = settings.dpi(); | 248 header_info.dpi = settings.dpi(); |
| 253 header_info.total_pages = total_page_count; | 249 header_info.total_pages = total_page_count; |
| 254 | 250 |
| 255 // Transform odd pages. | 251 // Transform odd pages. |
| 256 if (page_number % 2) { | 252 if (page_number % 2) { |
| 257 switch (bitmap_settings.odd_page_transform) { | 253 switch (bitmap_settings.odd_page_transform) { |
| 258 case TRANSFORM_NORMAL: | 254 case TRANSFORM_NORMAL: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 269 break; | 265 break; |
| 270 } | 266 } |
| 271 } | 267 } |
| 272 | 268 |
| 273 if (bitmap_settings.rotate_all_pages) { | 269 if (bitmap_settings.rotate_all_pages) { |
| 274 header_info.flipx = !header_info.flipx; | 270 header_info.flipx = !header_info.flipx; |
| 275 header_info.flipy = !header_info.flipy; | 271 header_info.flipy = !header_info.flipy; |
| 276 } | 272 } |
| 277 | 273 |
| 278 std::string pwg_page; | 274 std::string pwg_page; |
| 279 if (!encoder.EncodePage(image, header_info, &pwg_page)) | 275 if (!encoder.EncodePage(image, header_info, &pwg_page)) { |
| 280 return false; | 276 ret = false; |
| 277 break; |
| 278 } |
| 281 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), | 279 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), |
| 282 pwg_page.size()); | 280 pwg_page.size()); |
| 283 if (bytes_written != static_cast<int>(pwg_page.size())) | 281 if (bytes_written != static_cast<int>(pwg_page.size())) { |
| 284 return false; | 282 ret = false; |
| 283 break; |
| 284 } |
| 285 } | 285 } |
| 286 return true; | 286 chrome_pdf::ReleasePDFHandle(pdf_handle); |
| 287 return ret; |
| 287 } | 288 } |
| 288 | 289 |
| 289 void PrintingHandler::OnGetPrinterCapsAndDefaults( | 290 void PrintingHandler::OnGetPrinterCapsAndDefaults( |
| 290 const std::string& printer_name) { | 291 const std::string& printer_name) { |
| 291 scoped_refptr<PrintBackend> print_backend = | 292 scoped_refptr<PrintBackend> print_backend = |
| 292 PrintBackend::CreateInstance(nullptr); | 293 PrintBackend::CreateInstance(nullptr); |
| 293 PrinterCapsAndDefaults printer_info; | 294 PrinterCapsAndDefaults printer_info; |
| 294 | 295 |
| 295 crash_keys::ScopedPrinterInfo crash_key( | 296 crash_keys::ScopedPrinterInfo crash_key( |
| 296 print_backend->GetPrinterDriverInfo(printer_name)); | 297 print_backend->GetPrinterDriverInfo(printer_name)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 320 printer_name, printer_info)); | 321 printer_name, printer_info)); |
| 321 } else { | 322 } else { |
| 322 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 323 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( |
| 323 printer_name)); | 324 printer_name)); |
| 324 } | 325 } |
| 325 ReleaseProcessIfNeeded(); | 326 ReleaseProcessIfNeeded(); |
| 326 } | 327 } |
| 327 #endif // ENABLE_PRINT_PREVIEW | 328 #endif // ENABLE_PRINT_PREVIEW |
| 328 | 329 |
| 329 } // namespace printing | 330 } // namespace printing |
| OLD | NEW |