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_Start, | 67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, |
68 OnRenderPDFPagesToMetafileStart) | 68 OnRenderPDFPagesToMetafile) |
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::OnRenderPDFPagesToMetafileStart( | 88 void PrintingHandler::OnRenderPDFPagesToMetafile( |
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; | |
114 ReleaseProcessIfNeeded(); | 112 ReleaseProcessIfNeeded(); |
115 } | 113 } |
116 | 114 |
117 #endif // OS_WIN | 115 #endif // OS_WIN |
118 | 116 |
119 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 117 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
120 void PrintingHandler::OnRenderPDFPagesToPWGRaster( | 118 void PrintingHandler::OnRenderPDFPagesToPWGRaster( |
121 IPC::PlatformFileForTransit pdf_transit, | 119 IPC::PlatformFileForTransit pdf_transit, |
122 const PdfRenderSettings& settings, | 120 const PdfRenderSettings& settings, |
123 const PwgRasterSettings& bitmap_settings, | 121 const PwgRasterSettings& bitmap_settings, |
124 IPC::PlatformFileForTransit bitmap_transit) { | 122 IPC::PlatformFileForTransit bitmap_transit) { |
125 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); | 123 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); |
126 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); | 124 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); |
127 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, | 125 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, |
128 std::move(bitmap))) { | 126 std::move(bitmap))) { |
129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | 127 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); |
130 } else { | 128 } else { |
131 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | 129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); |
132 } | 130 } |
133 ReleaseProcessIfNeeded(); | 131 ReleaseProcessIfNeeded(); |
134 } | 132 } |
135 #endif // ENABLE_PRINT_PREVIEW | 133 #endif // ENABLE_PRINT_PREVIEW |
136 | 134 |
137 #if defined(OS_WIN) | 135 #if defined(OS_WIN) |
138 int PrintingHandler::LoadPDF(base::File pdf_file) { | 136 int PrintingHandler::LoadPDF(base::File pdf_file) { |
139 DCHECK(!pdf_handle_); | |
140 | |
141 int64_t length64 = pdf_file.GetLength(); | 137 int64_t length64 = pdf_file.GetLength(); |
142 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
143 return 0; | 139 return 0; |
144 int length = static_cast<int>(length64); | 140 int length = static_cast<int>(length64); |
145 | 141 |
146 pdf_data_.resize(length); | 142 pdf_data_.resize(length); |
147 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
148 return 0; | 144 return 0; |
149 | 145 |
150 int total_page_count = 0; | 146 int total_page_count = 0; |
151 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), | 147 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), |
152 &total_page_count, nullptr, &pdf_handle_)) { | 148 &total_page_count, nullptr)) { |
153 return 0; | 149 return 0; |
154 } | 150 } |
155 return total_page_count; | 151 return total_page_count; |
156 } | 152 } |
157 | 153 |
158 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, | 154 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, |
159 base::File output_file, | 155 base::File output_file, |
160 float* scale_factor) { | 156 float* scale_factor) { |
161 Emf metafile; | 157 Emf metafile; |
162 metafile.Init(); | 158 metafile.Init(); |
163 | 159 |
164 // We need to scale down DC to fit an entire page into DC available area. | 160 // We need to scale down DC to fit an entire page into DC available area. |
165 // Current metafile is based on screen DC and have current screen size. | 161 // Current metafile is based on screen DC and have current screen size. |
166 // Writing outside of those boundaries will result in the cut-off output. | 162 // Writing outside of those boundaries will result in the cut-off output. |
167 // On metafiles (this is the case here), scaling down will still record | 163 // On metafiles (this is the case here), scaling down will still record |
168 // original coordinates and we'll be able to print in full resolution. | 164 // original coordinates and we'll be able to print in full resolution. |
169 // Before playback we'll need to counter the scaling up that will happen | 165 // Before playback we'll need to counter the scaling up that will happen |
170 // in the service (print_system_win.cc). | 166 // in the service (print_system_win.cc). |
171 *scale_factor = gfx::CalculatePageScale( | 167 *scale_factor = gfx::CalculatePageScale( |
172 metafile.context(), pdf_rendering_settings_.area.right(), | 168 metafile.context(), pdf_rendering_settings_.area.right(), |
173 pdf_rendering_settings_.area.bottom()); | 169 pdf_rendering_settings_.area.bottom()); |
174 gfx::ScaleDC(metafile.context(), *scale_factor); | 170 gfx::ScaleDC(metafile.context(), *scale_factor); |
175 | 171 |
176 // The underlying metafile is of type Emf and ignores the arguments passed | 172 // The underlying metafile is of type Emf and ignores the arguments passed |
177 // to StartPage. | 173 // to StartPage. |
178 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | 174 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
179 if (!chrome_pdf::RenderPDFPageToDC( | 175 if (!chrome_pdf::RenderPDFPageToDC( |
180 pdf_handle_, page_number, metafile.context(), | 176 &pdf_data_.front(), pdf_data_.size(), page_number, metafile.context(), |
181 pdf_rendering_settings_.dpi, pdf_rendering_settings_.area.x(), | 177 pdf_rendering_settings_.dpi, pdf_rendering_settings_.area.x(), |
182 pdf_rendering_settings_.area.y(), | 178 pdf_rendering_settings_.area.y(), |
183 pdf_rendering_settings_.area.width(), | 179 pdf_rendering_settings_.area.width(), |
184 pdf_rendering_settings_.area.height(), true, false, true, true, | 180 pdf_rendering_settings_.area.height(), true, false, true, true, |
185 pdf_rendering_settings_.autorotate)) { | 181 pdf_rendering_settings_.autorotate)) { |
186 return false; | 182 return false; |
187 } | 183 } |
188 metafile.FinishPage(); | 184 metafile.FinishPage(); |
189 metafile.FinishDocument(); | 185 metafile.FinishDocument(); |
190 return metafile.SaveTo(&output_file); | 186 return metafile.SaveTo(&output_file); |
(...skipping 10 matching lines...) Expand all Loading... |
201 if (!pdf_file.GetInfo(&info) || info.size <= 0 || | 197 if (!pdf_file.GetInfo(&info) || info.size <= 0 || |
202 info.size > std::numeric_limits<int>::max()) | 198 info.size > std::numeric_limits<int>::max()) |
203 return false; | 199 return false; |
204 int data_size = static_cast<int>(info.size); | 200 int data_size = static_cast<int>(info.size); |
205 | 201 |
206 std::string data(data_size, 0); | 202 std::string data(data_size, 0); |
207 if (pdf_file.Read(0, &data[0], data_size) != data_size) | 203 if (pdf_file.Read(0, &data[0], data_size) != data_size) |
208 return false; | 204 return false; |
209 | 205 |
210 int total_page_count = 0; | 206 int total_page_count = 0; |
211 void* pdf_handle = nullptr; | |
212 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, &total_page_count, | 207 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, &total_page_count, |
213 nullptr, &pdf_handle)) { | 208 nullptr)) { |
214 return false; | 209 return false; |
215 } | 210 } |
216 | 211 |
217 cloud_print::PwgEncoder encoder; | 212 cloud_print::PwgEncoder encoder; |
218 std::string pwg_header; | 213 std::string pwg_header; |
219 encoder.EncodeDocumentHeader(&pwg_header); | 214 encoder.EncodeDocumentHeader(&pwg_header); |
220 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), | 215 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), |
221 pwg_header.size()); | 216 pwg_header.size()); |
222 if (bytes_written != static_cast<int>(pwg_header.size())) { | 217 if (bytes_written != static_cast<int>(pwg_header.size())) |
223 chrome_pdf::ReleasePDFHandle(pdf_handle); | |
224 return false; | 218 return false; |
225 } | |
226 | 219 |
227 cloud_print::BitmapImage image(settings.area.size(), | 220 cloud_print::BitmapImage image(settings.area.size(), |
228 cloud_print::BitmapImage::BGRA); | 221 cloud_print::BitmapImage::BGRA); |
229 bool ret = true; | |
230 for (int i = 0; i < total_page_count; ++i) { | 222 for (int i = 0; i < total_page_count; ++i) { |
231 int page_number = i; | 223 int page_number = i; |
232 | 224 |
233 if (bitmap_settings.reverse_page_order) { | 225 if (bitmap_settings.reverse_page_order) { |
234 page_number = total_page_count - 1 - page_number; | 226 page_number = total_page_count - 1 - page_number; |
235 } | 227 } |
236 | 228 |
237 if (!chrome_pdf::RenderPDFPageToBitmap( | 229 if (!chrome_pdf::RenderPDFPageToBitmap( |
238 pdf_handle, page_number, image.pixel_data(), image.size().width(), | 230 data.data(), data_size, page_number, image.pixel_data(), |
239 image.size().height(), settings.dpi, settings.autorotate)) { | 231 image.size().width(), image.size().height(), settings.dpi, |
| 232 settings.autorotate)) { |
240 return false; | 233 return false; |
241 } | 234 } |
242 | 235 |
243 cloud_print::PwgHeaderInfo header_info; | 236 cloud_print::PwgHeaderInfo header_info; |
244 header_info.dpi = settings.dpi; | 237 header_info.dpi = settings.dpi; |
245 header_info.total_pages = total_page_count; | 238 header_info.total_pages = total_page_count; |
246 | 239 |
247 // Transform odd pages. | 240 // Transform odd pages. |
248 if (page_number % 2) { | 241 if (page_number % 2) { |
249 switch (bitmap_settings.odd_page_transform) { | 242 switch (bitmap_settings.odd_page_transform) { |
(...skipping 11 matching lines...) Expand all Loading... |
261 break; | 254 break; |
262 } | 255 } |
263 } | 256 } |
264 | 257 |
265 if (bitmap_settings.rotate_all_pages) { | 258 if (bitmap_settings.rotate_all_pages) { |
266 header_info.flipx = !header_info.flipx; | 259 header_info.flipx = !header_info.flipx; |
267 header_info.flipy = !header_info.flipy; | 260 header_info.flipy = !header_info.flipy; |
268 } | 261 } |
269 | 262 |
270 std::string pwg_page; | 263 std::string pwg_page; |
271 if (!encoder.EncodePage(image, header_info, &pwg_page)) { | 264 if (!encoder.EncodePage(image, header_info, &pwg_page)) |
272 ret = false; | 265 return false; |
273 break; | |
274 } | |
275 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), | 266 bytes_written = bitmap_file.WriteAtCurrentPos(pwg_page.data(), |
276 pwg_page.size()); | 267 pwg_page.size()); |
277 if (bytes_written != static_cast<int>(pwg_page.size())) { | 268 if (bytes_written != static_cast<int>(pwg_page.size())) |
278 ret = false; | 269 return false; |
279 break; | |
280 } | |
281 } | 270 } |
282 chrome_pdf::ReleasePDFHandle(pdf_handle); | 271 return true; |
283 return ret; | |
284 } | 272 } |
285 | 273 |
286 void PrintingHandler::OnGetPrinterCapsAndDefaults( | 274 void PrintingHandler::OnGetPrinterCapsAndDefaults( |
287 const std::string& printer_name) { | 275 const std::string& printer_name) { |
288 scoped_refptr<PrintBackend> print_backend = | 276 scoped_refptr<PrintBackend> print_backend = |
289 PrintBackend::CreateInstance(nullptr); | 277 PrintBackend::CreateInstance(nullptr); |
290 PrinterCapsAndDefaults printer_info; | 278 PrinterCapsAndDefaults printer_info; |
291 | 279 |
292 crash_keys::ScopedPrinterInfo crash_key( | 280 crash_keys::ScopedPrinterInfo crash_key( |
293 print_backend->GetPrinterDriverInfo(printer_name)); | 281 print_backend->GetPrinterDriverInfo(printer_name)); |
(...skipping 23 matching lines...) Expand all Loading... |
317 printer_name, printer_info)); | 305 printer_name, printer_info)); |
318 } else { | 306 } else { |
319 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 307 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( |
320 printer_name)); | 308 printer_name)); |
321 } | 309 } |
322 ReleaseProcessIfNeeded(); | 310 ReleaseProcessIfNeeded(); |
323 } | 311 } |
324 #endif // ENABLE_PRINT_PREVIEW | 312 #endif // ENABLE_PRINT_PREVIEW |
325 | 313 |
326 } // namespace printing | 314 } // namespace printing |
OLD | NEW |