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 "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_native_library.h" | 10 #include "base/scoped_native_library.h" |
11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
12 #include "chrome/common/chrome_utility_printing_messages.h" | 12 #include "chrome/common/chrome_utility_printing_messages.h" |
13 #include "chrome/utility/cloud_print/bitmap_image.h" | 13 #include "chrome/utility/cloud_print/bitmap_image.h" |
14 #include "chrome/utility/cloud_print/pwg_encoder.h" | 14 #include "chrome/utility/cloud_print/pwg_encoder.h" |
15 #include "content/public/utility/utility_thread.h" | 15 #include "content/public/utility/utility_thread.h" |
16 #include "pdf/pdf.h" | |
17 #include "printing/page_range.h" | 16 #include "printing/page_range.h" |
18 #include "printing/pdf_render_settings.h" | 17 #include "printing/pdf_render_settings.h" |
19 | 18 |
20 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
21 #include "printing/emf_win.h" | 20 #include "printing/emf_win.h" |
22 #include "ui/gfx/gdi_util.h" | 21 #include "ui/gfx/gdi_util.h" |
23 #endif | 22 #endif |
24 | 23 |
25 #if defined(ENABLE_PRINT_PREVIEW) | 24 #if defined(ENABLE_PRINT_PREVIEW) |
26 #include "chrome/common/crash_keys.h" | 25 #include "chrome/common/crash_keys.h" |
27 #include "printing/backend/print_backend.h" | 26 #include "printing/backend/print_backend.h" |
28 #endif | 27 #endif |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 bool Send(IPC::Message* message) { | 31 bool Send(IPC::Message* message) { |
33 return content::UtilityThread::Get()->Send(message); | 32 return content::UtilityThread::Get()->Send(message); |
34 } | 33 } |
35 | 34 |
36 void ReleaseProcessIfNeeded() { | 35 void ReleaseProcessIfNeeded() { |
37 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 36 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
38 } | 37 } |
39 | 38 |
| 39 class PdfFunctionsBase { |
| 40 public: |
| 41 PdfFunctionsBase() : render_pdf_to_bitmap_func_(NULL), |
| 42 get_pdf_doc_info_func_(NULL) {} |
| 43 |
| 44 bool Init() { |
| 45 base::FilePath pdf_module_path; |
| 46 if (!PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path) || |
| 47 !base::PathExists(pdf_module_path)) { |
| 48 return false; |
| 49 } |
| 50 |
| 51 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); |
| 52 if (!pdf_lib_.is_valid()) { |
| 53 LOG(WARNING) << "Couldn't load PDF plugin"; |
| 54 return false; |
| 55 } |
| 56 |
| 57 render_pdf_to_bitmap_func_ = |
| 58 reinterpret_cast<RenderPDFPageToBitmapProc>( |
| 59 pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); |
| 60 LOG_IF(WARNING, !render_pdf_to_bitmap_func_) << |
| 61 "Missing RenderPDFPageToBitmap"; |
| 62 |
| 63 get_pdf_doc_info_func_ = |
| 64 reinterpret_cast<GetPDFDocInfoProc>( |
| 65 pdf_lib_.GetFunctionPointer("GetPDFDocInfo")); |
| 66 LOG_IF(WARNING, !get_pdf_doc_info_func_) << "Missing GetPDFDocInfo"; |
| 67 |
| 68 if (!render_pdf_to_bitmap_func_ || !get_pdf_doc_info_func_ || |
| 69 !PlatformInit(pdf_module_path, pdf_lib_)) { |
| 70 Reset(); |
| 71 } |
| 72 |
| 73 return IsValid(); |
| 74 } |
| 75 |
| 76 bool IsValid() const { |
| 77 return pdf_lib_.is_valid(); |
| 78 } |
| 79 |
| 80 void Reset() { |
| 81 pdf_lib_.Reset(NULL); |
| 82 } |
| 83 |
| 84 bool RenderPDFPageToBitmap(const void* pdf_buffer, |
| 85 int pdf_buffer_size, |
| 86 int page_number, |
| 87 void* bitmap_buffer, |
| 88 int bitmap_width, |
| 89 int bitmap_height, |
| 90 int dpi_x, |
| 91 int dpi_y, |
| 92 bool autorotate) { |
| 93 if (!render_pdf_to_bitmap_func_) |
| 94 return false; |
| 95 return render_pdf_to_bitmap_func_(pdf_buffer, pdf_buffer_size, page_number, |
| 96 bitmap_buffer, bitmap_width, |
| 97 bitmap_height, dpi_x, dpi_y, autorotate); |
| 98 } |
| 99 |
| 100 bool GetPDFDocInfo(const void* pdf_buffer, |
| 101 int buffer_size, |
| 102 int* page_count, |
| 103 double* max_page_width) { |
| 104 if (!get_pdf_doc_info_func_) |
| 105 return false; |
| 106 return get_pdf_doc_info_func_(pdf_buffer, buffer_size, page_count, |
| 107 max_page_width); |
| 108 } |
| 109 |
| 110 protected: |
| 111 virtual bool PlatformInit( |
| 112 const base::FilePath& pdf_module_path, |
| 113 const base::ScopedNativeLibrary& pdf_lib) { |
| 114 return true; |
| 115 } |
| 116 |
| 117 private: |
| 118 // Exported by PDF plugin. |
| 119 typedef bool (*RenderPDFPageToBitmapProc)(const void* pdf_buffer, |
| 120 int pdf_buffer_size, |
| 121 int page_number, |
| 122 void* bitmap_buffer, |
| 123 int bitmap_width, |
| 124 int bitmap_height, |
| 125 int dpi_x, |
| 126 int dpi_y, |
| 127 bool autorotate); |
| 128 typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, |
| 129 int buffer_size, int* page_count, |
| 130 double* max_page_width); |
| 131 |
| 132 RenderPDFPageToBitmapProc render_pdf_to_bitmap_func_; |
| 133 GetPDFDocInfoProc get_pdf_doc_info_func_; |
| 134 |
| 135 base::ScopedNativeLibrary pdf_lib_; |
| 136 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsBase); |
| 137 }; |
| 138 |
| 139 #if defined(OS_WIN) |
| 140 |
| 141 class PdfFunctionsWin : public PdfFunctionsBase { |
| 142 public: |
| 143 PdfFunctionsWin() : render_pdf_to_dc_func_(NULL) { |
| 144 } |
| 145 |
| 146 bool PlatformInit( |
| 147 const base::FilePath& pdf_module_path, |
| 148 const base::ScopedNativeLibrary& pdf_lib) override { |
| 149 render_pdf_to_dc_func_ = |
| 150 reinterpret_cast<RenderPDFPageToDCProc>( |
| 151 pdf_lib.GetFunctionPointer("RenderPDFPageToDC")); |
| 152 LOG_IF(WARNING, !render_pdf_to_dc_func_) << "Missing RenderPDFPageToDC"; |
| 153 |
| 154 return render_pdf_to_dc_func_ != NULL; |
| 155 } |
| 156 |
| 157 bool RenderPDFPageToDC(const void* pdf_buffer, |
| 158 int buffer_size, |
| 159 int page_number, |
| 160 HDC dc, |
| 161 int dpi_x, |
| 162 int dpi_y, |
| 163 int bounds_origin_x, |
| 164 int bounds_origin_y, |
| 165 int bounds_width, |
| 166 int bounds_height, |
| 167 bool fit_to_bounds, |
| 168 bool stretch_to_bounds, |
| 169 bool keep_aspect_ratio, |
| 170 bool center_in_bounds, |
| 171 bool autorotate) { |
| 172 if (!render_pdf_to_dc_func_) |
| 173 return false; |
| 174 return render_pdf_to_dc_func_(pdf_buffer, buffer_size, page_number, |
| 175 dc, dpi_x, dpi_y, bounds_origin_x, |
| 176 bounds_origin_y, bounds_width, bounds_height, |
| 177 fit_to_bounds, stretch_to_bounds, |
| 178 keep_aspect_ratio, center_in_bounds, |
| 179 autorotate); |
| 180 } |
| 181 |
| 182 private: |
| 183 // Exported by PDF plugin. |
| 184 typedef bool (*RenderPDFPageToDCProc)( |
| 185 const void* pdf_buffer, int buffer_size, int page_number, HDC dc, |
| 186 int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, |
| 187 int bounds_width, int bounds_height, bool fit_to_bounds, |
| 188 bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds, |
| 189 bool autorotate); |
| 190 RenderPDFPageToDCProc render_pdf_to_dc_func_; |
| 191 |
| 192 DISALLOW_COPY_AND_ASSIGN(PdfFunctionsWin); |
| 193 }; |
| 194 |
| 195 typedef PdfFunctionsWin PdfFunctions; |
| 196 #else // OS_WIN |
| 197 typedef PdfFunctionsBase PdfFunctions; |
| 198 #endif // OS_WIN |
| 199 |
| 200 base::LazyInstance<PdfFunctions> g_pdf_lib = LAZY_INSTANCE_INITIALIZER; |
| 201 |
40 } // namespace | 202 } // namespace |
41 | 203 |
42 PrintingHandler::PrintingHandler() {} | 204 PrintingHandler::PrintingHandler() {} |
43 | 205 |
44 PrintingHandler::~PrintingHandler() {} | 206 PrintingHandler::~PrintingHandler() {} |
45 | 207 |
| 208 // static |
| 209 void PrintingHandler::PreSandboxStartup() { |
| 210 g_pdf_lib.Get().Init(); |
| 211 } |
| 212 |
46 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { | 213 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { |
47 bool handled = true; | 214 bool handled = true; |
48 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) | 215 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) |
49 #if defined(OS_WIN) | 216 #if defined(OS_WIN) |
50 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, | 217 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, |
51 OnRenderPDFPagesToMetafile) | 218 OnRenderPDFPagesToMetafile) |
52 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, | 219 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, |
53 OnRenderPDFPagesToMetafileGetPage) | 220 OnRenderPDFPagesToMetafileGetPage) |
54 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, | 221 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, |
55 OnRenderPDFPagesToMetafileStop) | 222 OnRenderPDFPagesToMetafileStop) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | 275 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); |
109 } else { | 276 } else { |
110 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | 277 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); |
111 } | 278 } |
112 ReleaseProcessIfNeeded(); | 279 ReleaseProcessIfNeeded(); |
113 } | 280 } |
114 #endif // ENABLE_PRINT_PREVIEW | 281 #endif // ENABLE_PRINT_PREVIEW |
115 | 282 |
116 #if defined(OS_WIN) | 283 #if defined(OS_WIN) |
117 int PrintingHandler::LoadPDF(base::File pdf_file) { | 284 int PrintingHandler::LoadPDF(base::File pdf_file) { |
| 285 if (!g_pdf_lib.Get().IsValid()) |
| 286 return 0; |
| 287 |
118 int64 length64 = pdf_file.GetLength(); | 288 int64 length64 = pdf_file.GetLength(); |
119 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 289 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
120 return 0; | 290 return 0; |
121 int length = static_cast<int>(length64); | 291 int length = static_cast<int>(length64); |
122 | 292 |
123 pdf_data_.resize(length); | 293 pdf_data_.resize(length); |
124 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 294 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
125 return 0; | 295 return 0; |
126 | 296 |
127 int total_page_count = 0; | 297 int total_page_count = 0; |
128 if (!chrome_pdf::GetPDFDocInfo( | 298 if (!g_pdf_lib.Get().GetPDFDocInfo( |
129 &pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) { | 299 &pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) { |
130 return 0; | 300 return 0; |
131 } | 301 } |
132 return total_page_count; | 302 return total_page_count; |
133 } | 303 } |
134 | 304 |
135 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, | 305 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, |
136 base::File output_file, | 306 base::File output_file, |
137 float* scale_factor) { | 307 float* scale_factor) { |
138 printing::Emf metafile; | 308 printing::Emf metafile; |
139 metafile.Init(); | 309 metafile.Init(); |
140 | 310 |
141 // We need to scale down DC to fit an entire page into DC available area. | 311 // We need to scale down DC to fit an entire page into DC available area. |
142 // Current metafile is based on screen DC and have current screen size. | 312 // Current metafile is based on screen DC and have current screen size. |
143 // Writing outside of those boundaries will result in the cut-off output. | 313 // Writing outside of those boundaries will result in the cut-off output. |
144 // On metafiles (this is the case here), scaling down will still record | 314 // On metafiles (this is the case here), scaling down will still record |
145 // original coordinates and we'll be able to print in full resolution. | 315 // original coordinates and we'll be able to print in full resolution. |
146 // Before playback we'll need to counter the scaling up that will happen | 316 // Before playback we'll need to counter the scaling up that will happen |
147 // in the service (print_system_win.cc). | 317 // in the service (print_system_win.cc). |
148 *scale_factor = | 318 *scale_factor = |
149 gfx::CalculatePageScale(metafile.context(), | 319 gfx::CalculatePageScale(metafile.context(), |
150 pdf_rendering_settings_.area().right(), | 320 pdf_rendering_settings_.area().right(), |
151 pdf_rendering_settings_.area().bottom()); | 321 pdf_rendering_settings_.area().bottom()); |
152 gfx::ScaleDC(metafile.context(), *scale_factor); | 322 gfx::ScaleDC(metafile.context(), *scale_factor); |
153 | 323 |
154 // The underlying metafile is of type Emf and ignores the arguments passed | 324 // The underlying metafile is of type Emf and ignores the arguments passed |
155 // to StartPage. | 325 // to StartPage. |
156 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | 326 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
157 if (!chrome_pdf::RenderPDFPageToDC( | 327 if (!g_pdf_lib.Get().RenderPDFPageToDC( |
158 &pdf_data_.front(), | 328 &pdf_data_.front(), |
159 pdf_data_.size(), | 329 pdf_data_.size(), |
160 page_number, | 330 page_number, |
161 metafile.context(), | 331 metafile.context(), |
162 pdf_rendering_settings_.dpi(), | 332 pdf_rendering_settings_.dpi(), |
| 333 pdf_rendering_settings_.dpi(), |
163 pdf_rendering_settings_.area().x(), | 334 pdf_rendering_settings_.area().x(), |
164 pdf_rendering_settings_.area().y(), | 335 pdf_rendering_settings_.area().y(), |
165 pdf_rendering_settings_.area().width(), | 336 pdf_rendering_settings_.area().width(), |
166 pdf_rendering_settings_.area().height(), | 337 pdf_rendering_settings_.area().height(), |
167 true, | 338 true, |
168 false, | 339 false, |
169 true, | 340 true, |
170 true, | 341 true, |
171 pdf_rendering_settings_.autorotate())) { | 342 pdf_rendering_settings_.autorotate())) { |
172 return false; | 343 return false; |
173 } | 344 } |
174 metafile.FinishPage(); | 345 metafile.FinishPage(); |
175 metafile.FinishDocument(); | 346 metafile.FinishDocument(); |
176 return metafile.SaveTo(&output_file); | 347 return metafile.SaveTo(&output_file); |
177 } | 348 } |
178 | 349 |
179 #endif // OS_WIN | 350 #endif // OS_WIN |
180 | 351 |
181 #if defined(ENABLE_PRINT_PREVIEW) | 352 #if defined(ENABLE_PRINT_PREVIEW) |
182 bool PrintingHandler::RenderPDFPagesToPWGRaster( | 353 bool PrintingHandler::RenderPDFPagesToPWGRaster( |
183 base::File pdf_file, | 354 base::File pdf_file, |
184 const printing::PdfRenderSettings& settings, | 355 const printing::PdfRenderSettings& settings, |
185 const printing::PwgRasterSettings& bitmap_settings, | 356 const printing::PwgRasterSettings& bitmap_settings, |
186 base::File bitmap_file) { | 357 base::File bitmap_file) { |
187 bool autoupdate = true; | 358 bool autoupdate = true; |
| 359 if (!g_pdf_lib.Get().IsValid()) |
| 360 return false; |
| 361 |
188 base::File::Info info; | 362 base::File::Info info; |
189 if (!pdf_file.GetInfo(&info) || info.size <= 0 || | 363 if (!pdf_file.GetInfo(&info) || info.size <= 0 || |
190 info.size > std::numeric_limits<int>::max()) | 364 info.size > std::numeric_limits<int>::max()) |
191 return false; | 365 return false; |
192 int data_size = static_cast<int>(info.size); | 366 int data_size = static_cast<int>(info.size); |
193 | 367 |
194 std::string data(data_size, 0); | 368 std::string data(data_size, 0); |
195 if (pdf_file.Read(0, &data[0], data_size) != data_size) | 369 if (pdf_file.Read(0, &data[0], data_size) != data_size) |
196 return false; | 370 return false; |
197 | 371 |
198 int total_page_count = 0; | 372 int total_page_count = 0; |
199 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, | 373 if (!g_pdf_lib.Get().GetPDFDocInfo(data.data(), data_size, |
200 &total_page_count, NULL)) { | 374 &total_page_count, NULL)) { |
201 return false; | 375 return false; |
202 } | 376 } |
203 | 377 |
204 cloud_print::PwgEncoder encoder; | 378 cloud_print::PwgEncoder encoder; |
205 std::string pwg_header; | 379 std::string pwg_header; |
206 encoder.EncodeDocumentHeader(&pwg_header); | 380 encoder.EncodeDocumentHeader(&pwg_header); |
207 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), | 381 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), |
208 pwg_header.size()); | 382 pwg_header.size()); |
209 if (bytes_written != static_cast<int>(pwg_header.size())) | 383 if (bytes_written != static_cast<int>(pwg_header.size())) |
210 return false; | 384 return false; |
211 | 385 |
212 cloud_print::BitmapImage image(settings.area().size(), | 386 cloud_print::BitmapImage image(settings.area().size(), |
213 cloud_print::BitmapImage::BGRA); | 387 cloud_print::BitmapImage::BGRA); |
214 for (int i = 0; i < total_page_count; ++i) { | 388 for (int i = 0; i < total_page_count; ++i) { |
215 int page_number = i; | 389 int page_number = i; |
216 | 390 |
217 if (bitmap_settings.reverse_page_order) { | 391 if (bitmap_settings.reverse_page_order) { |
218 page_number = total_page_count - 1 - page_number; | 392 page_number = total_page_count - 1 - page_number; |
219 } | 393 } |
220 | 394 |
221 if (!chrome_pdf::RenderPDFPageToBitmap(data.data(), | 395 if (!g_pdf_lib.Get().RenderPDFPageToBitmap(data.data(), |
222 data_size, | 396 data_size, |
223 page_number, | 397 page_number, |
224 image.pixel_data(), | 398 image.pixel_data(), |
225 image.size().width(), | 399 image.size().width(), |
226 image.size().height(), | 400 image.size().height(), |
227 settings.dpi(), | 401 settings.dpi(), |
228 autoupdate)) { | 402 settings.dpi(), |
| 403 autoupdate)) { |
229 return false; | 404 return false; |
230 } | 405 } |
231 | 406 |
232 cloud_print::PwgHeaderInfo header_info; | 407 cloud_print::PwgHeaderInfo header_info; |
233 header_info.dpi = settings.dpi(); | 408 header_info.dpi = settings.dpi(); |
234 header_info.total_pages = total_page_count; | 409 header_info.total_pages = total_page_count; |
235 | 410 |
236 // Transform odd pages. | 411 // Transform odd pages. |
237 if (page_number % 2) { | 412 if (page_number % 2) { |
238 switch (bitmap_settings.odd_page_transform) { | 413 switch (bitmap_settings.odd_page_transform) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 &printer_info)) { | 474 &printer_info)) { |
300 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( | 475 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( |
301 printer_name, printer_info)); | 476 printer_name, printer_info)); |
302 } else { | 477 } else { |
303 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 478 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( |
304 printer_name)); | 479 printer_name)); |
305 } | 480 } |
306 ReleaseProcessIfNeeded(); | 481 ReleaseProcessIfNeeded(); |
307 } | 482 } |
308 #endif // ENABLE_PRINT_PREVIEW | 483 #endif // ENABLE_PRINT_PREVIEW |
OLD | NEW |