Index: chrome/utility/printing_handler.cc |
diff --git a/chrome/utility/printing_handler.cc b/chrome/utility/printing_handler.cc |
index 9e282adb467725a4549b0e24ca1d5c11502f8432..3dd8e923b6c6e4fa76d70e70666c375d1cf1eef4 100644 |
--- a/chrome/utility/printing_handler.cc |
+++ b/chrome/utility/printing_handler.cc |
@@ -13,7 +13,6 @@ |
#include "chrome/utility/cloud_print/bitmap_image.h" |
#include "chrome/utility/cloud_print/pwg_encoder.h" |
#include "content/public/utility/utility_thread.h" |
-#include "pdf/pdf.h" |
#include "printing/page_range.h" |
#include "printing/pdf_render_settings.h" |
@@ -37,11 +36,179 @@ |
content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
} |
+class PdfFunctionsBase { |
+ public: |
+ PdfFunctionsBase() : render_pdf_to_bitmap_func_(NULL), |
+ get_pdf_doc_info_func_(NULL) {} |
+ |
+ bool Init() { |
+ base::FilePath pdf_module_path; |
+ if (!PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path) || |
+ !base::PathExists(pdf_module_path)) { |
+ return false; |
+ } |
+ |
+ pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL)); |
+ if (!pdf_lib_.is_valid()) { |
+ LOG(WARNING) << "Couldn't load PDF plugin"; |
+ return false; |
+ } |
+ |
+ render_pdf_to_bitmap_func_ = |
+ reinterpret_cast<RenderPDFPageToBitmapProc>( |
+ pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap")); |
+ LOG_IF(WARNING, !render_pdf_to_bitmap_func_) << |
+ "Missing RenderPDFPageToBitmap"; |
+ |
+ get_pdf_doc_info_func_ = |
+ reinterpret_cast<GetPDFDocInfoProc>( |
+ pdf_lib_.GetFunctionPointer("GetPDFDocInfo")); |
+ LOG_IF(WARNING, !get_pdf_doc_info_func_) << "Missing GetPDFDocInfo"; |
+ |
+ if (!render_pdf_to_bitmap_func_ || !get_pdf_doc_info_func_ || |
+ !PlatformInit(pdf_module_path, pdf_lib_)) { |
+ Reset(); |
+ } |
+ |
+ return IsValid(); |
+ } |
+ |
+ bool IsValid() const { |
+ return pdf_lib_.is_valid(); |
+ } |
+ |
+ void Reset() { |
+ pdf_lib_.Reset(NULL); |
+ } |
+ |
+ bool RenderPDFPageToBitmap(const void* pdf_buffer, |
+ int pdf_buffer_size, |
+ int page_number, |
+ void* bitmap_buffer, |
+ int bitmap_width, |
+ int bitmap_height, |
+ int dpi_x, |
+ int dpi_y, |
+ bool autorotate) { |
+ if (!render_pdf_to_bitmap_func_) |
+ return false; |
+ return render_pdf_to_bitmap_func_(pdf_buffer, pdf_buffer_size, page_number, |
+ bitmap_buffer, bitmap_width, |
+ bitmap_height, dpi_x, dpi_y, autorotate); |
+ } |
+ |
+ bool GetPDFDocInfo(const void* pdf_buffer, |
+ int buffer_size, |
+ int* page_count, |
+ double* max_page_width) { |
+ if (!get_pdf_doc_info_func_) |
+ return false; |
+ return get_pdf_doc_info_func_(pdf_buffer, buffer_size, page_count, |
+ max_page_width); |
+ } |
+ |
+ protected: |
+ virtual bool PlatformInit( |
+ const base::FilePath& pdf_module_path, |
+ const base::ScopedNativeLibrary& pdf_lib) { |
+ return true; |
+ } |
+ |
+ private: |
+ // Exported by PDF plugin. |
+ typedef bool (*RenderPDFPageToBitmapProc)(const void* pdf_buffer, |
+ int pdf_buffer_size, |
+ int page_number, |
+ void* bitmap_buffer, |
+ int bitmap_width, |
+ int bitmap_height, |
+ int dpi_x, |
+ int dpi_y, |
+ bool autorotate); |
+ typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer, |
+ int buffer_size, int* page_count, |
+ double* max_page_width); |
+ |
+ RenderPDFPageToBitmapProc render_pdf_to_bitmap_func_; |
+ GetPDFDocInfoProc get_pdf_doc_info_func_; |
+ |
+ base::ScopedNativeLibrary pdf_lib_; |
+ DISALLOW_COPY_AND_ASSIGN(PdfFunctionsBase); |
+}; |
+ |
+#if defined(OS_WIN) |
+ |
+class PdfFunctionsWin : public PdfFunctionsBase { |
+ public: |
+ PdfFunctionsWin() : render_pdf_to_dc_func_(NULL) { |
+ } |
+ |
+ bool PlatformInit( |
+ const base::FilePath& pdf_module_path, |
+ const base::ScopedNativeLibrary& pdf_lib) override { |
+ render_pdf_to_dc_func_ = |
+ reinterpret_cast<RenderPDFPageToDCProc>( |
+ pdf_lib.GetFunctionPointer("RenderPDFPageToDC")); |
+ LOG_IF(WARNING, !render_pdf_to_dc_func_) << "Missing RenderPDFPageToDC"; |
+ |
+ return render_pdf_to_dc_func_ != NULL; |
+ } |
+ |
+ bool RenderPDFPageToDC(const void* pdf_buffer, |
+ int buffer_size, |
+ int page_number, |
+ HDC dc, |
+ int dpi_x, |
+ int dpi_y, |
+ int bounds_origin_x, |
+ int bounds_origin_y, |
+ int bounds_width, |
+ int bounds_height, |
+ bool fit_to_bounds, |
+ bool stretch_to_bounds, |
+ bool keep_aspect_ratio, |
+ bool center_in_bounds, |
+ bool autorotate) { |
+ if (!render_pdf_to_dc_func_) |
+ return false; |
+ return render_pdf_to_dc_func_(pdf_buffer, buffer_size, page_number, |
+ dc, dpi_x, dpi_y, bounds_origin_x, |
+ bounds_origin_y, bounds_width, bounds_height, |
+ fit_to_bounds, stretch_to_bounds, |
+ keep_aspect_ratio, center_in_bounds, |
+ autorotate); |
+ } |
+ |
+ private: |
+ // Exported by PDF plugin. |
+ typedef bool (*RenderPDFPageToDCProc)( |
+ const void* pdf_buffer, int buffer_size, int page_number, HDC dc, |
+ int dpi_x, int dpi_y, int bounds_origin_x, int bounds_origin_y, |
+ int bounds_width, int bounds_height, bool fit_to_bounds, |
+ bool stretch_to_bounds, bool keep_aspect_ratio, bool center_in_bounds, |
+ bool autorotate); |
+ RenderPDFPageToDCProc render_pdf_to_dc_func_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PdfFunctionsWin); |
+}; |
+ |
+typedef PdfFunctionsWin PdfFunctions; |
+#else // OS_WIN |
+typedef PdfFunctionsBase PdfFunctions; |
+#endif // OS_WIN |
+ |
+base::LazyInstance<PdfFunctions> g_pdf_lib = LAZY_INSTANCE_INITIALIZER; |
+ |
} // namespace |
PrintingHandler::PrintingHandler() {} |
PrintingHandler::~PrintingHandler() {} |
+ |
+// static |
+void PrintingHandler::PreSandboxStartup() { |
+ g_pdf_lib.Get().Init(); |
+} |
bool PrintingHandler::OnMessageReceived(const IPC::Message& message) { |
bool handled = true; |
@@ -115,6 +282,9 @@ |
#if defined(OS_WIN) |
int PrintingHandler::LoadPDF(base::File pdf_file) { |
+ if (!g_pdf_lib.Get().IsValid()) |
+ return 0; |
+ |
int64 length64 = pdf_file.GetLength(); |
if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
return 0; |
@@ -125,7 +295,7 @@ |
return 0; |
int total_page_count = 0; |
- if (!chrome_pdf::GetPDFDocInfo( |
+ if (!g_pdf_lib.Get().GetPDFDocInfo( |
&pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) { |
return 0; |
} |
@@ -154,11 +324,12 @@ |
// The underlying metafile is of type Emf and ignores the arguments passed |
// to StartPage. |
metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
- if (!chrome_pdf::RenderPDFPageToDC( |
+ if (!g_pdf_lib.Get().RenderPDFPageToDC( |
&pdf_data_.front(), |
pdf_data_.size(), |
page_number, |
metafile.context(), |
+ pdf_rendering_settings_.dpi(), |
pdf_rendering_settings_.dpi(), |
pdf_rendering_settings_.area().x(), |
pdf_rendering_settings_.area().y(), |
@@ -185,6 +356,9 @@ |
const printing::PwgRasterSettings& bitmap_settings, |
base::File bitmap_file) { |
bool autoupdate = true; |
+ if (!g_pdf_lib.Get().IsValid()) |
+ return false; |
+ |
base::File::Info info; |
if (!pdf_file.GetInfo(&info) || info.size <= 0 || |
info.size > std::numeric_limits<int>::max()) |
@@ -196,8 +370,8 @@ |
return false; |
int total_page_count = 0; |
- if (!chrome_pdf::GetPDFDocInfo(data.data(), data_size, |
- &total_page_count, NULL)) { |
+ if (!g_pdf_lib.Get().GetPDFDocInfo(data.data(), data_size, |
+ &total_page_count, NULL)) { |
return false; |
} |
@@ -218,14 +392,15 @@ |
page_number = total_page_count - 1 - page_number; |
} |
- if (!chrome_pdf::RenderPDFPageToBitmap(data.data(), |
- data_size, |
- page_number, |
- image.pixel_data(), |
- image.size().width(), |
- image.size().height(), |
- settings.dpi(), |
- autoupdate)) { |
+ if (!g_pdf_lib.Get().RenderPDFPageToBitmap(data.data(), |
+ data_size, |
+ page_number, |
+ image.pixel_data(), |
+ image.size().width(), |
+ image.size().height(), |
+ settings.dpi(), |
+ settings.dpi(), |
+ autoupdate)) { |
return false; |
} |