| 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" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/chrome_utility_printing_messages.h" | |
| 14 #include "chrome/utility/cloud_print/bitmap_image.h" | 13 #include "chrome/utility/cloud_print/bitmap_image.h" |
| 15 #include "chrome/utility/cloud_print/pwg_encoder.h" | 14 #include "chrome/utility/cloud_print/pwg_encoder.h" |
| 16 #include "content/public/utility/utility_thread.h" | 15 #include "content/public/utility/utility_thread.h" |
| 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 17 #include "mojo/public/cpp/system/platform_handle.h" |
| 17 #include "pdf/pdf.h" | 18 #include "pdf/pdf.h" |
| 18 #include "printing/features/features.h" | 19 #include "printing/features/features.h" |
| 19 #include "printing/page_range.h" | 20 #include "printing/page_range.h" |
| 20 #include "printing/pdf_render_settings.h" | 21 #include "printing/pdf_render_settings.h" |
| 21 | 22 |
| 22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 23 #include "printing/emf_win.h" | 24 #include "printing/emf_win.h" |
| 24 #include "ui/gfx/gdi_util.h" | 25 #include "ui/gfx/gdi_util.h" |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 28 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 28 #include "chrome/common/crash_keys.h" | 29 #include "chrome/common/crash_keys.h" |
| 29 #include "printing/backend/print_backend.h" | 30 #include "printing/backend/print_backend.h" |
| 30 #endif | 31 #endif |
| 31 | 32 |
| 32 namespace printing { | 33 namespace printing { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 bool Send(IPC::Message* message) { | |
| 37 return content::UtilityThread::Get()->Send(message); | |
| 38 } | |
| 39 | |
| 40 void ReleaseProcessIfNeeded() { | 37 void ReleaseProcessIfNeeded() { |
| 41 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 38 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 42 } | 39 } |
| 43 | 40 |
| 41 static mojom::FontPreCaching* g_font_pre_caching; |
| 42 |
| 44 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 45 void PreCacheFontCharacters(const LOGFONT* logfont, | 44 void PreCacheFontCharacters(const LOGFONT* logfont, |
| 46 const wchar_t* text, | 45 const wchar_t* text, |
| 47 size_t text_length) { | 46 size_t text_length) { |
| 48 Send(new ChromeUtilityHostMsg_PreCacheFontCharacters( | 47 if (g_font_pre_caching) { |
| 49 *logfont, base::string16(text, text_length))); | 48 g_font_pre_caching->PreCacheFontCharacters( |
| 49 *logfont, base::string16(text, text_length)); |
| 50 } |
| 50 } | 51 } |
| 51 #endif | 52 #endif |
| 52 | 53 |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 PrintingHandler::PrintingHandler() { | 56 PrintingHandler::PrintingHandler(mojom::FontPreCachingPtr font_pre_caching) |
| 57 : font_pre_caching_(std::move(font_pre_caching)) { |
| 58 g_font_pre_caching = font_pre_caching_.get(); |
| 56 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
| 57 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters); | 60 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters); |
| 58 #endif | 61 #endif |
| 59 } | 62 } |
| 60 | 63 |
| 61 PrintingHandler::~PrintingHandler() {} | 64 PrintingHandler::~PrintingHandler() { |
| 65 g_font_pre_caching = nullptr; |
| 66 } |
| 62 | 67 |
| 63 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { | 68 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { |
| 64 bool handled = true; | 69 return false; |
| 65 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) | 70 } |
| 71 |
| 72 namespace { |
| 73 class PrintingHandlerFactory : public mojom::PrintingFactory { |
| 74 public: |
| 75 // mojom::PrintingFactory: |
| 76 void MakePrinting(mojom::PrintingRequest request, |
| 77 mojom::FontPreCachingPtr font_pre_caching) override { |
| 78 mojo::MakeStrongBinding( |
| 79 base::MakeUnique<PrintingHandler>(std::move(font_pre_caching)), |
| 80 std::move(request)); |
| 81 } |
| 82 }; |
| 83 } // namespace |
| 84 |
| 85 // static |
| 86 void PrintingHandler::Create(mojom::PrintingFactoryRequest request) { |
| 87 mojo::MakeStrongBinding(base::MakeUnique<PrintingHandlerFactory>(), |
| 88 std::move(request)); |
| 89 } |
| 90 |
| 91 void PrintingHandler::RenderPDFPagesToMetafiles( |
| 92 mojo::ScopedHandle pdf_transit, |
| 93 const PdfRenderSettings& settings, |
| 94 bool print_text_with_gdi, |
| 95 const RenderPDFPagesToMetafilesCallback& callback) { |
| 66 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
| 67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, | 97 pdf_rendering_settings_ = settings; |
| 68 OnRenderPDFPagesToMetafile) | 98 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi); |
| 69 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, | 99 base::PlatformFile platform_file; |
| 70 OnRenderPDFPagesToMetafileGetPage) | 100 CHECK_EQ(mojo::UnwrapPlatformFile(std::move(pdf_transit), &platform_file), |
| 71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, | 101 MOJO_RESULT_OK); |
| 72 OnRenderPDFPagesToMetafileStop) | 102 base::File pdf_file(platform_file); |
| 73 #endif // OS_WIN | 103 int page_count = LoadPDF(std::move(pdf_file)); |
| 104 callback.Run(page_count); |
| 105 #else |
| 106 NOTREACHED(); |
| 107 #endif |
| 108 } |
| 109 |
| 110 void PrintingHandler::RenderPDFPagesToMetafilesGetPage( |
| 111 int page_number, |
| 112 mojo::ScopedHandle output_file, |
| 113 const RenderPDFPagesToMetafilesGetPageCallback& callback) { |
| 114 #if defined(OS_WIN) |
| 115 base::PlatformFile platform_file; |
| 116 CHECK_EQ(mojo::UnwrapPlatformFile(std::move(output_file), &platform_file), |
| 117 MOJO_RESULT_OK); |
| 118 base::File emf_file(platform_file); |
| 119 float scale_factor = 1.0f; |
| 120 bool success = |
| 121 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); |
| 122 callback.Run(success, scale_factor); |
| 123 #else |
| 124 NOTREACHED(); |
| 125 #endif |
| 126 } |
| 127 |
| 128 void PrintingHandler::RenderPDFPagesToMetafilesStop() { |
| 129 #if defined(OS_WIN) |
| 130 ReleaseProcessIfNeeded(); |
| 131 #else |
| 132 NOTREACHED(); |
| 133 #endif |
| 134 } |
| 135 |
| 136 void PrintingHandler::RenderPDFPagesToPWGRaster( |
| 137 mojo::ScopedHandle pdf_transit, |
| 138 const PdfRenderSettings& settings, |
| 139 const PwgRasterSettings& bitmap_settings, |
| 140 mojo::ScopedHandle bitmap_transit, |
| 141 const RenderPDFPagesToPWGRasterCallback& callback) { |
| 74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 142 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, | 143 base::PlatformFile pdf_platform_file; |
| 76 OnRenderPDFPagesToPWGRaster) | 144 CHECK_EQ(mojo::UnwrapPlatformFile(std::move(pdf_transit), &pdf_platform_file), |
| 77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | 145 MOJO_RESULT_OK); |
| 78 OnGetPrinterCapsAndDefaults) | 146 base::File pdf(pdf_platform_file); |
| 79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, | 147 base::PlatformFile bitmap_platform_file; |
| 80 OnGetPrinterSemanticCapsAndDefaults) | 148 CHECK_EQ(mojo::UnwrapPlatformFile(std::move(bitmap_transit), |
| 149 &bitmap_platform_file), |
| 150 MOJO_RESULT_OK); |
| 151 base::File bitmap(bitmap_platform_file); |
| 152 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, |
| 153 std::move(bitmap))) { |
| 154 callback.Run(true); |
| 155 } else { |
| 156 callback.Run(false); |
| 157 } |
| 158 ReleaseProcessIfNeeded(); |
| 159 #else |
| 160 NOTREACHED(); |
| 81 #endif // ENABLE_PRINT_PREVIEW | 161 #endif // ENABLE_PRINT_PREVIEW |
| 82 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 83 IPC_END_MESSAGE_MAP() | |
| 84 return handled; | |
| 85 } | 162 } |
| 86 | 163 |
| 87 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
| 88 void PrintingHandler::OnRenderPDFPagesToMetafile( | |
| 89 IPC::PlatformFileForTransit pdf_transit, | |
| 90 const PdfRenderSettings& settings, | |
| 91 bool print_text_with_gdi) { | |
| 92 pdf_rendering_settings_ = settings; | |
| 93 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi); | |
| 94 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); | |
| 95 int page_count = LoadPDF(std::move(pdf_file)); | |
| 96 Send( | |
| 97 new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count)); | |
| 98 } | |
| 99 | |
| 100 void PrintingHandler::OnRenderPDFPagesToMetafileGetPage( | |
| 101 int page_number, | |
| 102 IPC::PlatformFileForTransit output_file) { | |
| 103 base::File emf_file = IPC::PlatformFileForTransitToFile(output_file); | |
| 104 float scale_factor = 1.0f; | |
| 105 bool success = | |
| 106 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); | |
| 107 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone( | |
| 108 success, scale_factor)); | |
| 109 } | |
| 110 | |
| 111 void PrintingHandler::OnRenderPDFPagesToMetafileStop() { | |
| 112 ReleaseProcessIfNeeded(); | |
| 113 } | |
| 114 | |
| 115 #endif // OS_WIN | |
| 116 | |
| 117 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | |
| 118 void PrintingHandler::OnRenderPDFPagesToPWGRaster( | |
| 119 IPC::PlatformFileForTransit pdf_transit, | |
| 120 const PdfRenderSettings& settings, | |
| 121 const PwgRasterSettings& bitmap_settings, | |
| 122 IPC::PlatformFileForTransit bitmap_transit) { | |
| 123 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); | |
| 124 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); | |
| 125 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, | |
| 126 std::move(bitmap))) { | |
| 127 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | |
| 128 } else { | |
| 129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | |
| 130 } | |
| 131 ReleaseProcessIfNeeded(); | |
| 132 } | |
| 133 #endif // ENABLE_PRINT_PREVIEW | |
| 134 | |
| 135 #if defined(OS_WIN) | |
| 136 int PrintingHandler::LoadPDF(base::File pdf_file) { | 165 int PrintingHandler::LoadPDF(base::File pdf_file) { |
| 137 int64_t length64 = pdf_file.GetLength(); | 166 int64_t length64 = pdf_file.GetLength(); |
| 138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 167 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
| 139 return 0; | 168 return 0; |
| 140 int length = static_cast<int>(length64); | 169 int length = static_cast<int>(length64); |
| 141 | 170 |
| 142 pdf_data_.resize(length); | 171 pdf_data_.resize(length); |
| 143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 172 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
| 144 return 0; | 173 return 0; |
| 145 | 174 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 std::string pwg_page; | 292 std::string pwg_page; |
| 264 if (!encoder.EncodePage(image, header_info, &pwg_page)) | 293 if (!encoder.EncodePage(image, header_info, &pwg_page)) |
| 265 return false; | 294 return false; |
| 266 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), | 295 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), |
| 267 pwg_page.size()); | 296 pwg_page.size()); |
| 268 if (bytes_written != static_cast<int>(pwg_page.size())) | 297 if (bytes_written != static_cast<int>(pwg_page.size())) |
| 269 return false; | 298 return false; |
| 270 } | 299 } |
| 271 return true; | 300 return true; |
| 272 } | 301 } |
| 302 #endif // ENABLE_PRINT_PREVIEW |
| 273 | 303 |
| 274 void PrintingHandler::OnGetPrinterCapsAndDefaults( | 304 void PrintingHandler::GetPrinterCapsAndDefaults( |
| 275 const std::string& printer_name) { | 305 const std::string& printer_name, |
| 306 const GetPrinterCapsAndDefaultsCallback& callback) { |
| 307 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 276 scoped_refptr<PrintBackend> print_backend = | 308 scoped_refptr<PrintBackend> print_backend = |
| 277 PrintBackend::CreateInstance(nullptr); | 309 PrintBackend::CreateInstance(nullptr); |
| 278 PrinterCapsAndDefaults printer_info; | 310 PrinterCapsAndDefaults printer_info; |
| 279 | 311 |
| 280 crash_keys::ScopedPrinterInfo crash_key( | 312 crash_keys::ScopedPrinterInfo crash_key( |
| 281 print_backend->GetPrinterDriverInfo(printer_name)); | 313 print_backend->GetPrinterDriverInfo(printer_name)); |
| 282 | 314 |
| 283 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { | 315 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { |
| 284 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded( | 316 callback.Run(printer_info); |
| 285 printer_name, printer_info)); | |
| 286 } else { | 317 } else { |
| 287 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( | 318 callback.Run(base::nullopt); |
| 288 printer_name)); | |
| 289 } | 319 } |
| 290 ReleaseProcessIfNeeded(); | 320 ReleaseProcessIfNeeded(); |
| 321 #else |
| 322 NOTREACHED(); |
| 323 #endif // ENABLE_PRINT_PREVIEW |
| 291 } | 324 } |
| 292 | 325 |
| 293 void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults( | 326 void PrintingHandler::GetPrinterSemanticCapsAndDefaults( |
| 294 const std::string& printer_name) { | 327 const std::string& printer_name, |
| 328 const GetPrinterSemanticCapsAndDefaultsCallback& callback) { |
| 329 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 295 scoped_refptr<PrintBackend> print_backend = | 330 scoped_refptr<PrintBackend> print_backend = |
| 296 PrintBackend::CreateInstance(nullptr); | 331 PrintBackend::CreateInstance(nullptr); |
| 297 PrinterSemanticCapsAndDefaults printer_info; | 332 PrinterSemanticCapsAndDefaults printer_info; |
| 298 | 333 |
| 299 crash_keys::ScopedPrinterInfo crash_key( | 334 crash_keys::ScopedPrinterInfo crash_key( |
| 300 print_backend->GetPrinterDriverInfo(printer_name)); | 335 print_backend->GetPrinterDriverInfo(printer_name)); |
| 301 | 336 |
| 302 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, | 337 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, |
| 303 &printer_info)) { | 338 &printer_info)) { |
| 304 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( | 339 callback.Run(printer_info); |
| 305 printer_name, printer_info)); | |
| 306 } else { | 340 } else { |
| 307 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 341 callback.Run(base::nullopt); |
| 308 printer_name)); | |
| 309 } | 342 } |
| 310 ReleaseProcessIfNeeded(); | 343 ReleaseProcessIfNeeded(); |
| 344 #else |
| 345 NOTREACHED(); |
| 346 #endif // ENABLE_PRINT_PREVIEW |
| 311 } | 347 } |
| 312 #endif // ENABLE_PRINT_PREVIEW | |
| 313 | 348 |
| 314 } // namespace printing | 349 } // namespace printing |
| OLD | NEW |