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; | |
groby-ooo-7-16
2016/12/14 17:28:29
I have no idea what the threading implications aro
tibell
2016/12/14 23:12:19
Addressed the first paragraph.
I don't know the c
| |
66 } | |
62 | 67 |
63 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { | 68 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { |
groby-ooo-7-16
2016/12/14 17:28:29
Do we still need this? Technically, this is not a
tibell
2016/12/14 23:12:19
Done.
| |
64 bool handled = true; | 69 return false; |
65 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) | 70 } |
71 | |
72 void PrintingHandler::PrintingHandlerFactory::MakePrinting( | |
73 mojom::PrintingRequest request, | |
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 void PrintingHandler::RenderPDFPagesToMetafiles( | |
groby-ooo-7-16
2016/12/14 17:28:29
This seems kind of odd from a non-mojo-educated po
tibell
2016/12/14 23:12:19
I will bring this up during the Mojo meeting today
tibell
2016/12/21 03:52:31
We've now discussed this (https://groups.google.co
| |
89 base::File pdf_file, | |
90 const PdfRenderSettings& settings, | |
91 bool print_text_with_gdi, | |
92 const RenderPDFPagesToMetafilesCallback& callback) { | |
66 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, | 94 pdf_rendering_settings_ = settings; |
68 OnRenderPDFPagesToMetafile) | 95 chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi); |
69 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, | 96 int page_count = LoadPDF(std::move(pdf_file)); |
70 OnRenderPDFPagesToMetafileGetPage) | 97 callback.Run(page_count); |
71 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, | 98 #else |
72 OnRenderPDFPagesToMetafileStop) | 99 NOTREACHED(); |
73 #endif // OS_WIN | 100 #endif |
101 } | |
102 | |
103 void PrintingHandler::RenderPDFPagesToMetafilesGetPage( | |
104 int page_number, | |
105 base::File emf_file, | |
106 const RenderPDFPagesToMetafilesGetPageCallback& callback) { | |
107 #if defined(OS_WIN) | |
108 float scale_factor = 1.0f; | |
109 bool success = | |
110 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); | |
111 callback.Run(success, scale_factor); | |
112 #else | |
113 NOTREACHED(); | |
114 #endif | |
115 } | |
116 | |
117 void PrintingHandler::OnConnectionClosed() { | |
118 #if defined(OS_WIN) | |
119 ReleaseProcessIfNeeded(); | |
120 #else | |
121 NOTREACHED(); | |
122 #endif | |
123 } | |
124 | |
125 void PrintingHandler::RenderPDFPagesToPWGRaster( | |
126 base::File pdf, | |
127 const PdfRenderSettings& settings, | |
128 const PwgRasterSettings& bitmap_settings, | |
129 base::File bitmap, | |
130 const RenderPDFPagesToPWGRasterCallback& callback) { | |
74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 131 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
75 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, | 132 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, |
76 OnRenderPDFPagesToPWGRaster) | 133 std::move(bitmap))) { |
77 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | 134 callback.Run(true); |
78 OnGetPrinterCapsAndDefaults) | 135 } else { |
79 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, | 136 callback.Run(false); |
80 OnGetPrinterSemanticCapsAndDefaults) | 137 } |
138 ReleaseProcessIfNeeded(); | |
139 #else | |
140 NOTREACHED(); | |
81 #endif // ENABLE_PRINT_PREVIEW | 141 #endif // ENABLE_PRINT_PREVIEW |
82 IPC_MESSAGE_UNHANDLED(handled = false) | |
83 IPC_END_MESSAGE_MAP() | |
84 return handled; | |
85 } | 142 } |
86 | 143 |
87 #if defined(OS_WIN) | 144 #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) { | 145 int PrintingHandler::LoadPDF(base::File pdf_file) { |
137 int64_t length64 = pdf_file.GetLength(); | 146 int64_t length64 = pdf_file.GetLength(); |
138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 147 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
139 return 0; | 148 return 0; |
140 int length = static_cast<int>(length64); | 149 int length = static_cast<int>(length64); |
141 | 150 |
142 pdf_data_.resize(length); | 151 pdf_data_.resize(length); |
143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 152 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
144 return 0; | 153 return 0; |
145 | 154 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 std::string pwg_page; | 272 std::string pwg_page; |
264 if (!encoder.EncodePage(image, header_info, &pwg_page)) | 273 if (!encoder.EncodePage(image, header_info, &pwg_page)) |
265 return false; | 274 return false; |
266 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), | 275 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), |
267 pwg_page.size()); | 276 pwg_page.size()); |
268 if (bytes_written != static_cast<int>(pwg_page.size())) | 277 if (bytes_written != static_cast<int>(pwg_page.size())) |
269 return false; | 278 return false; |
270 } | 279 } |
271 return true; | 280 return true; |
272 } | 281 } |
282 #endif // ENABLE_PRINT_PREVIEW | |
273 | 283 |
274 void PrintingHandler::OnGetPrinterCapsAndDefaults( | 284 void PrintingHandler::GetPrinterCapsAndDefaults( |
275 const std::string& printer_name) { | 285 const std::string& printer_name, |
286 const GetPrinterCapsAndDefaultsCallback& callback) { | |
287 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | |
276 scoped_refptr<PrintBackend> print_backend = | 288 scoped_refptr<PrintBackend> print_backend = |
277 PrintBackend::CreateInstance(nullptr); | 289 PrintBackend::CreateInstance(nullptr); |
278 PrinterCapsAndDefaults printer_info; | 290 PrinterCapsAndDefaults printer_info; |
279 | 291 |
280 crash_keys::ScopedPrinterInfo crash_key( | 292 crash_keys::ScopedPrinterInfo crash_key( |
281 print_backend->GetPrinterDriverInfo(printer_name)); | 293 print_backend->GetPrinterDriverInfo(printer_name)); |
282 | 294 |
283 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { | 295 if (print_backend->GetPrinterCapsAndDefaults(printer_name, &printer_info)) { |
284 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded( | 296 callback.Run(printer_info); |
285 printer_name, printer_info)); | |
286 } else { | 297 } else { |
287 Send(new ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed( | 298 callback.Run(base::nullopt); |
288 printer_name)); | |
289 } | 299 } |
290 ReleaseProcessIfNeeded(); | 300 ReleaseProcessIfNeeded(); |
301 #else | |
302 NOTREACHED(); | |
303 #endif // ENABLE_PRINT_PREVIEW | |
291 } | 304 } |
292 | 305 |
293 void PrintingHandler::OnGetPrinterSemanticCapsAndDefaults( | 306 void PrintingHandler::GetPrinterSemanticCapsAndDefaults( |
groby-ooo-7-16
2016/12/14 17:28:29
Same comment as above - I'd be much happier if the
tibell
2016/12/14 23:12:19
If print preview does make sense as a separate int
| |
294 const std::string& printer_name) { | 307 const std::string& printer_name, |
308 const GetPrinterSemanticCapsAndDefaultsCallback& callback) { | |
309 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | |
295 scoped_refptr<PrintBackend> print_backend = | 310 scoped_refptr<PrintBackend> print_backend = |
296 PrintBackend::CreateInstance(nullptr); | 311 PrintBackend::CreateInstance(nullptr); |
297 PrinterSemanticCapsAndDefaults printer_info; | 312 PrinterSemanticCapsAndDefaults printer_info; |
298 | 313 |
299 crash_keys::ScopedPrinterInfo crash_key( | 314 crash_keys::ScopedPrinterInfo crash_key( |
300 print_backend->GetPrinterDriverInfo(printer_name)); | 315 print_backend->GetPrinterDriverInfo(printer_name)); |
301 | 316 |
302 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, | 317 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, |
303 &printer_info)) { | 318 &printer_info)) { |
304 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( | 319 callback.Run(printer_info); |
305 printer_name, printer_info)); | |
306 } else { | 320 } else { |
307 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 321 callback.Run(base::nullopt); |
308 printer_name)); | |
309 } | 322 } |
310 ReleaseProcessIfNeeded(); | 323 ReleaseProcessIfNeeded(); |
324 #else | |
325 NOTREACHED(); | |
326 #endif // ENABLE_PRINT_PREVIEW | |
311 } | 327 } |
312 #endif // ENABLE_PRINT_PREVIEW | |
313 | 328 |
314 } // namespace printing | 329 } // namespace printing |
OLD | NEW |