Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: chrome/utility/printing_handler.cc

Issue 2477283002: Convert printing IPCs to Mojo
Patch Set: Fix more Windows compile errors Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message.h"
17 #include "mojo/public/cpp/bindings/strong_binding.h"
18 #include "mojo/public/cpp/system/platform_handle.h"
17 #include "pdf/pdf.h" 19 #include "pdf/pdf.h"
18 #include "printing/features/features.h" 20 #include "printing/features/features.h"
19 #include "printing/page_range.h" 21 #include "printing/page_range.h"
20 #include "printing/pdf_render_settings.h" 22 #include "printing/pdf_render_settings.h"
21 23
22 #if defined(OS_WIN) 24 #if defined(OS_WIN)
23 #include "printing/emf_win.h" 25 #include "printing/emf_win.h"
24 #include "ui/gfx/gdi_util.h" 26 #include "ui/gfx/gdi_util.h"
25 #endif 27 #endif
26 28
27 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) 29 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
28 #include "chrome/common/crash_keys.h" 30 #include "chrome/common/crash_keys.h"
29 #include "printing/backend/print_backend.h" 31 #include "printing/backend/print_backend.h"
30 #endif 32 #endif
31 33
32 namespace printing { 34 namespace printing {
33 35
34 namespace { 36 namespace {
35 37
36 bool Send(IPC::Message* message) {
37 return content::UtilityThread::Get()->Send(message);
38 }
39
40 void ReleaseProcessIfNeeded() { 38 void ReleaseProcessIfNeeded() {
41 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); 39 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
42 } 40 }
43 41
42 static mojom::FontPreCaching* g_font_pre_caching;
43
44 #if defined(OS_WIN) 44 #if defined(OS_WIN)
45 void PreCacheFontCharacters(const LOGFONT* logfont, 45 void PreCacheFontCharacters(const LOGFONT* logfont,
46 const wchar_t* text, 46 const wchar_t* text,
47 size_t text_length) { 47 size_t text_length) {
48 Send(new ChromeUtilityHostMsg_PreCacheFontCharacters( 48 if (g_font_pre_caching) {
49 *logfont, base::string16(text, text_length))); 49 g_font_pre_caching->PreCacheFontCharacters(
50 *logfont, base::string16(text, text_length));
51 }
50 } 52 }
51 #endif 53 #endif
52 54
53 } // namespace 55 } // namespace
54 56
55 PrintingHandler::PrintingHandler() { 57 PrintingHandler::PrintingHandler(mojom::FontPreCachingPtr font_pre_caching)
58 : font_pre_caching_(std::move(font_pre_caching)) {
59 g_font_pre_caching = font_pre_caching_.get();
56 #if defined(OS_WIN) 60 #if defined(OS_WIN)
57 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters); 61 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters);
58 #endif 62 #endif
59 } 63 }
60 64
61 PrintingHandler::~PrintingHandler() {} 65 PrintingHandler::~PrintingHandler() {
66 #if defined(OS_WIN)
67 chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(nullptr);
68 #endif
69 g_font_pre_caching = nullptr;
70 }
62 71
63 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { 72 void PrintingHandler::PrintingHandlerFactory::MakePrinting(
64 bool handled = true; 73 mojom::PrintingRequest request,
65 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) 74 mojom::FontPreCachingPtr font_pre_caching) {
75 auto impl = base::MakeUnique<PrintingHandler>(std::move(font_pre_caching));
76 base::Closure error_handler = base::Bind(&PrintingHandler::OnConnectionClosed,
77 base::Unretained(impl.get()));
78 auto binding = mojo::MakeStrongBinding(std::move(impl), std::move(request));
79 binding->set_connection_error_handler(error_handler);
80 }
81
82 // static
83 void PrintingHandler::Create(mojom::PrintingFactoryRequest request) {
84 mojo::MakeStrongBinding(base::MakeUnique<PrintingHandlerFactory>(),
85 std::move(request));
86 }
87
88 // TODO(crbug.com/676224): Conditionally define some messages instead of
89 // rejecting them at runtime.
90 void PrintingHandler::RenderPDFPagesToMetafiles(
91 base::File pdf_file,
92 const PdfRenderSettings& settings,
93 bool print_text_with_gdi,
94 const RenderPDFPagesToMetafilesCallback& callback) {
66 #if defined(OS_WIN) 95 #if defined(OS_WIN)
67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, 96 pdf_rendering_settings_ = settings;
68 OnRenderPDFPagesToMetafile) 97 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi);
69 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, 98 int page_count = LoadPDF(std::move(pdf_file));
70 OnRenderPDFPagesToMetafileGetPage) 99 callback.Run(page_count);
71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, 100 #else
72 OnRenderPDFPagesToMetafileStop) 101 mojo::ReportBadMessage("RenderPDFPagesToMetafiles is Windows only");
73 #endif // OS_WIN 102 #endif
103 }
104
105 void PrintingHandler::RenderPDFPagesToMetafilesGetPage(
106 int page_number,
107 base::File emf_file,
108 const RenderPDFPagesToMetafilesGetPageCallback& callback) {
109 #if defined(OS_WIN)
110 float scale_factor = 1.0f;
111 bool success =
112 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor);
113 callback.Run(success, scale_factor);
114 #else
115 mojo::ReportBadMessage("RenderPDFPagesToMetafilesGetPage is Windows only");
116 #endif
117 }
118
119 void PrintingHandler::OnConnectionClosed() {
120 #if defined(OS_WIN)
121 ReleaseProcessIfNeeded();
122 #else
123 NOTREACHED();
124 #endif
125 }
126
127 void PrintingHandler::RenderPDFPagesToPWGRaster(
128 base::File pdf,
129 const PdfRenderSettings& settings,
130 const PwgRasterSettings& bitmap_settings,
131 base::File bitmap,
132 const RenderPDFPagesToPWGRasterCallback& callback) {
74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) 133 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, 134 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings,
76 OnRenderPDFPagesToPWGRaster) 135 std::move(bitmap))) {
77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, 136 callback.Run(true);
78 OnGetPrinterCapsAndDefaults) 137 } else {
79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, 138 callback.Run(false);
80 OnGetPrinterSemanticCapsAndDefaults) 139 }
140 ReleaseProcessIfNeeded();
141 #else
142 mojo::ReportBadMessage(
143 "RenderPDFPagesToPWGRaster requires print preview to be enabled");
81 #endif // ENABLE_PRINT_PREVIEW 144 #endif // ENABLE_PRINT_PREVIEW
82 IPC_MESSAGE_UNHANDLED(handled = false)
83 IPC_END_MESSAGE_MAP()
84 return handled;
85 } 145 }
86 146
87 #if defined(OS_WIN) 147 #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) { 148 int PrintingHandler::LoadPDF(base::File pdf_file) {
137 int64_t length64 = pdf_file.GetLength(); 149 int64_t length64 = pdf_file.GetLength();
138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) 150 if (length64 <= 0 || length64 > std::numeric_limits<int>::max())
139 return 0; 151 return 0;
140 int length = static_cast<int>(length64); 152 int length = static_cast<int>(length64);
141 153
142 pdf_data_.resize(length); 154 pdf_data_.resize(length);
143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) 155 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size()))
144 return 0; 156 return 0;
145 157
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 std::string pwg_page; 275 std::string pwg_page;
264 if (!encoder.EncodePage(image, header_info, &pwg_page)) 276 if (!encoder.EncodePage(image, header_info, &pwg_page))
265 return false; 277 return false;
266 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), 278 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(),
267 pwg_page.size()); 279 pwg_page.size());
268 if (bytes_written != static_cast<int>(pwg_page.size())) 280 if (bytes_written != static_cast<int>(pwg_page.size()))
269 return false; 281 return false;
270 } 282 }
271 return true; 283 return true;
272 } 284 }
285 #endif // ENABLE_PRINT_PREVIEW
273 286
274 void PrintingHandler::OnGetPrinterCapsAndDefaults( 287 void PrintingHandler::GetPrinterCapsAndDefaults(
275 const std::string& printer_name) { 288 const std::string& printer_name,
289 const GetPrinterCapsAndDefaultsCallback& callback) {
290 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
276 scoped_refptr<PrintBackend> print_backend = 291 scoped_refptr<PrintBackend> print_backend =
277 PrintBackend::CreateInstance(nullptr); 292 PrintBackend::CreateInstance(nullptr);
278 PrinterCapsAndDefaults printer_info; 293 PrinterCapsAndDefaults printer_info;
279 294
280 crash_keys::ScopedPrinterInfo crash_key( 295 crash_keys::ScopedPrinterInfo crash_key(
281 print_backend->GetPrinterDriverInfo(printer_name)); 296 print_backend->GetPrinterDriverInfo(printer_name));
282 297
283 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { 298 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) {
284 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded( 299 callback.Run(printer_info);
285 printer_name, printer_info));
286 } else { 300 } else {
287 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( 301 callback.Run(base::nullopt);
288 printer_name));
289 } 302 }
290 ReleaseProcessIfNeeded(); 303 ReleaseProcessIfNeeded();
304 #else
305 mojo::ReportBadMessage(
306 "GetPrinterCapsAndDefaults requires print preview to be enabled");
307 #endif // ENABLE_PRINT_PREVIEW
291 } 308 }
292 309
293 void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults( 310 void PrintingHandler::GetPrinterSemanticCapsAndDefaults(
294 const std::string& printer_name) { 311 const std::string& printer_name,
312 const GetPrinterSemanticCapsAndDefaultsCallback& callback) {
313 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
295 scoped_refptr<PrintBackend> print_backend = 314 scoped_refptr<PrintBackend> print_backend =
296 PrintBackend::CreateInstance(nullptr); 315 PrintBackend::CreateInstance(nullptr);
297 PrinterSemanticCapsAndDefaults printer_info; 316 PrinterSemanticCapsAndDefaults printer_info;
298 317
299 crash_keys::ScopedPrinterInfo crash_key( 318 crash_keys::ScopedPrinterInfo crash_key(
300 print_backend->GetPrinterDriverInfo(printer_name)); 319 print_backend->GetPrinterDriverInfo(printer_name));
301 320
302 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, 321 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name,
303 &printer_info)) { 322 &printer_info)) {
304 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( 323 callback.Run(printer_info);
305 printer_name, printer_info));
306 } else { 324 } else {
307 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( 325 callback.Run(base::nullopt);
308 printer_name));
309 } 326 }
310 ReleaseProcessIfNeeded(); 327 ReleaseProcessIfNeeded();
328 #else
329 mojo::ReportBadMessage(
330 "GetPrinterSemanticCapsAndDefaults requires print preview to be enabled");
331 #endif // ENABLE_PRINT_PREVIEW
311 } 332 }
312 #endif // ENABLE_PRINT_PREVIEW
313 333
314 } // namespace printing 334 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698