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

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

Issue 799643004: Combine PDF plugin into the Chromium binary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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 "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"
16 #include "printing/page_range.h" 17 #include "printing/page_range.h"
17 #include "printing/pdf_render_settings.h" 18 #include "printing/pdf_render_settings.h"
18 19
19 #if defined(OS_WIN) 20 #if defined(OS_WIN)
20 #include "printing/emf_win.h" 21 #include "printing/emf_win.h"
21 #include "ui/gfx/gdi_util.h" 22 #include "ui/gfx/gdi_util.h"
22 #endif 23 #endif
23 24
24 #if defined(ENABLE_PRINT_PREVIEW) 25 #if defined(ENABLE_PRINT_PREVIEW)
25 #include "chrome/common/crash_keys.h" 26 #include "chrome/common/crash_keys.h"
26 #include "printing/backend/print_backend.h" 27 #include "printing/backend/print_backend.h"
27 #endif 28 #endif
28 29
29 namespace { 30 namespace {
30 31
31 bool Send(IPC::Message* message) { 32 bool Send(IPC::Message* message) {
32 return content::UtilityThread::Get()->Send(message); 33 return content::UtilityThread::Get()->Send(message);
33 } 34 }
34 35
35 void ReleaseProcessIfNeeded() { 36 void ReleaseProcessIfNeeded() {
36 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); 37 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
37 } 38 }
38 39
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
202 } // namespace 40 } // namespace
203 41
204 PrintingHandler::PrintingHandler() {} 42 PrintingHandler::PrintingHandler() {}
205 43
206 PrintingHandler::~PrintingHandler() {} 44 PrintingHandler::~PrintingHandler() {}
207 45
208 // static
209 void PrintingHandler::PreSandboxStartup() {
210 g_pdf_lib.Get().Init();
211 }
212
213 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { 46 bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
214 bool handled = true; 47 bool handled = true;
215 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message) 48 IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message)
216 #if defined(OS_WIN) 49 #if defined(OS_WIN)
217 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles, 50 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
218 OnRenderPDFPagesToMetafile) 51 OnRenderPDFPagesToMetafile)
219 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage, 52 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
220 OnRenderPDFPagesToMetafileGetPage) 53 OnRenderPDFPagesToMetafileGetPage)
221 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop, 54 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
222 OnRenderPDFPagesToMetafileStop) 55 OnRenderPDFPagesToMetafileStop)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); 108 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded());
276 } else { 109 } else {
277 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); 110 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed());
278 } 111 }
279 ReleaseProcessIfNeeded(); 112 ReleaseProcessIfNeeded();
280 } 113 }
281 #endif // ENABLE_PRINT_PREVIEW 114 #endif // ENABLE_PRINT_PREVIEW
282 115
283 #if defined(OS_WIN) 116 #if defined(OS_WIN)
284 int PrintingHandler::LoadPDF(base::File pdf_file) { 117 int PrintingHandler::LoadPDF(base::File pdf_file) {
285 if (!g_pdf_lib.Get().IsValid())
286 return 0;
287
288 int64 length64 = pdf_file.GetLength(); 118 int64 length64 = pdf_file.GetLength();
289 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) 119 if (length64 <= 0 || length64 > std::numeric_limits<int>::max())
290 return 0; 120 return 0;
291 int length = static_cast<int>(length64); 121 int length = static_cast<int>(length64);
292 122
293 pdf_data_.resize(length); 123 pdf_data_.resize(length);
294 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) 124 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size()))
295 return 0; 125 return 0;
296 126
297 int total_page_count = 0; 127 int total_page_count = 0;
298 if (!g_pdf_lib.Get().GetPDFDocInfo( 128 if (!chrome_pdf::GetPDFDocInfo(
299 &pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) { 129 &pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) {
300 return 0; 130 return 0;
301 } 131 }
302 return total_page_count; 132 return total_page_count;
303 } 133 }
304 134
305 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, 135 bool PrintingHandler::RenderPdfPageToMetafile(int page_number,
306 base::File output_file, 136 base::File output_file,
307 float* scale_factor) { 137 float* scale_factor) {
308 printing::Emf metafile; 138 printing::Emf metafile;
309 metafile.Init(); 139 metafile.Init();
310 140
311 // We need to scale down DC to fit an entire page into DC available area. 141 // We need to scale down DC to fit an entire page into DC available area.
312 // Current metafile is based on screen DC and have current screen size. 142 // Current metafile is based on screen DC and have current screen size.
313 // Writing outside of those boundaries will result in the cut-off output. 143 // Writing outside of those boundaries will result in the cut-off output.
314 // On metafiles (this is the case here), scaling down will still record 144 // On metafiles (this is the case here), scaling down will still record
315 // original coordinates and we'll be able to print in full resolution. 145 // original coordinates and we'll be able to print in full resolution.
316 // Before playback we'll need to counter the scaling up that will happen 146 // Before playback we'll need to counter the scaling up that will happen
317 // in the service (print_system_win.cc). 147 // in the service (print_system_win.cc).
318 *scale_factor = 148 *scale_factor =
319 gfx::CalculatePageScale(metafile.context(), 149 gfx::CalculatePageScale(metafile.context(),
320 pdf_rendering_settings_.area().right(), 150 pdf_rendering_settings_.area().right(),
321 pdf_rendering_settings_.area().bottom()); 151 pdf_rendering_settings_.area().bottom());
322 gfx::ScaleDC(metafile.context(), *scale_factor); 152 gfx::ScaleDC(metafile.context(), *scale_factor);
323 153
324 // The underlying metafile is of type Emf and ignores the arguments passed 154 // The underlying metafile is of type Emf and ignores the arguments passed
325 // to StartPage. 155 // to StartPage.
326 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); 156 metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
327 if (!g_pdf_lib.Get().RenderPDFPageToDC( 157 if (!chrome_pdf::RenderPDFPageToDC(
328 &pdf_data_.front(), 158 &pdf_data_.front(),
329 pdf_data_.size(), 159 pdf_data_.size(),
330 page_number, 160 page_number,
331 metafile.context(), 161 metafile.context(),
332 pdf_rendering_settings_.dpi(), 162 pdf_rendering_settings_.dpi(),
333 pdf_rendering_settings_.dpi(),
334 pdf_rendering_settings_.area().x(), 163 pdf_rendering_settings_.area().x(),
335 pdf_rendering_settings_.area().y(), 164 pdf_rendering_settings_.area().y(),
336 pdf_rendering_settings_.area().width(), 165 pdf_rendering_settings_.area().width(),
337 pdf_rendering_settings_.area().height(), 166 pdf_rendering_settings_.area().height(),
338 true, 167 true,
339 false, 168 false,
340 true, 169 true,
341 true, 170 true,
342 pdf_rendering_settings_.autorotate())) { 171 pdf_rendering_settings_.autorotate())) {
343 return false; 172 return false;
344 } 173 }
345 metafile.FinishPage(); 174 metafile.FinishPage();
346 metafile.FinishDocument(); 175 metafile.FinishDocument();
347 return metafile.SaveTo(&output_file); 176 return metafile.SaveTo(&output_file);
348 } 177 }
349 178
350 #endif // OS_WIN 179 #endif // OS_WIN
351 180
352 #if defined(ENABLE_PRINT_PREVIEW) 181 #if defined(ENABLE_PRINT_PREVIEW)
353 bool PrintingHandler::RenderPDFPagesToPWGRaster( 182 bool PrintingHandler::RenderPDFPagesToPWGRaster(
354 base::File pdf_file, 183 base::File pdf_file,
355 const printing::PdfRenderSettings& settings, 184 const printing::PdfRenderSettings& settings,
356 const printing::PwgRasterSettings& bitmap_settings, 185 const printing::PwgRasterSettings& bitmap_settings,
357 base::File bitmap_file) { 186 base::File bitmap_file) {
358 bool autoupdate = true; 187 bool autoupdate = true;
359 if (!g_pdf_lib.Get().IsValid())
360 return false;
361
362 base::File::Info info; 188 base::File::Info info;
363 if (!pdf_file.GetInfo(&info) || info.size <= 0 || 189 if (!pdf_file.GetInfo(&info) || info.size <= 0 ||
364 info.size > std::numeric_limits<int>::max()) 190 info.size > std::numeric_limits<int>::max())
365 return false; 191 return false;
366 int data_size = static_cast<int>(info.size); 192 int data_size = static_cast<int>(info.size);
367 193
368 std::string data(data_size, 0); 194 std::string data(data_size, 0);
369 if (pdf_file.Read(0, &data[0], data_size) != data_size) 195 if (pdf_file.Read(0, &data[0], data_size) != data_size)
370 return false; 196 return false;
371 197
372 int total_page_count = 0; 198 int total_page_count = 0;
373 if (!g_pdf_lib.Get().GetPDFDocInfo(data.data(), data_size, 199 if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size,
374 &total_page_count, NULL)) { 200 &total_page_count, NULL)) {
375 return false; 201 return false;
376 } 202 }
377 203
378 cloud_print::PwgEncoder encoder; 204 cloud_print::PwgEncoder encoder;
379 std::string pwg_header; 205 std::string pwg_header;
380 encoder.EncodeDocumentHeader(&pwg_header); 206 encoder.EncodeDocumentHeader(&pwg_header);
381 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(), 207 int bytes_written = bitmap_file.WriteAtCurrentPos(pwg_header.data(),
382 pwg_header.size()); 208 pwg_header.size());
383 if (bytes_written != static_cast<int>(pwg_header.size())) 209 if (bytes_written != static_cast<int>(pwg_header.size()))
384 return false; 210 return false;
385 211
386 cloud_print::BitmapImage image(settings.area().size(), 212 cloud_print::BitmapImage image(settings.area().size(),
387 cloud_print::BitmapImage::BGRA); 213 cloud_print::BitmapImage::BGRA);
388 for (int i = 0; i < total_page_count; ++i) { 214 for (int i = 0; i < total_page_count; ++i) {
389 int page_number = i; 215 int page_number = i;
390 216
391 if (bitmap_settings.reverse_page_order) { 217 if (bitmap_settings.reverse_page_order) {
392 page_number = total_page_count - 1 - page_number; 218 page_number = total_page_count - 1 - page_number;
393 } 219 }
394 220
395 if (!g_pdf_lib.Get().RenderPDFPageToBitmap(data.data(), 221 if (!chrome_pdf::RenderPDFPageToBitmap(data.data(),
396 data_size, 222 data_size,
397 page_number, 223 page_number,
398 image.pixel_data(), 224 image.pixel_data(),
399 image.size().width(), 225 image.size().width(),
400 image.size().height(), 226 image.size().height(),
401 settings.dpi(), 227 settings.dpi(),
402 settings.dpi(), 228 autoupdate)) {
403 autoupdate)) {
404 return false; 229 return false;
405 } 230 }
406 231
407 cloud_print::PwgHeaderInfo header_info; 232 cloud_print::PwgHeaderInfo header_info;
408 header_info.dpi = settings.dpi(); 233 header_info.dpi = settings.dpi();
409 header_info.total_pages = total_page_count; 234 header_info.total_pages = total_page_count;
410 235
411 // Transform odd pages. 236 // Transform odd pages.
412 if (page_number % 2) { 237 if (page_number % 2) {
413 switch (bitmap_settings.odd_page_transform) { 238 switch (bitmap_settings.odd_page_transform) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 &printer_info)) { 299 &printer_info)) {
475 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( 300 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded(
476 printer_name, printer_info)); 301 printer_name, printer_info));
477 } else { 302 } else {
478 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( 303 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed(
479 printer_name)); 304 printer_name));
480 } 305 }
481 ReleaseProcessIfNeeded(); 306 ReleaseProcessIfNeeded();
482 } 307 }
483 #endif // ENABLE_PRINT_PREVIEW 308 #endif // ENABLE_PRINT_PREVIEW
OLDNEW
« no previous file with comments | « chrome/utility/printing_handler.h ('k') | pdf/BUILD.gn » ('j') | pdf/pdf.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698