Roll src/third_party/WebKit 86dee58:318ad17 (svn 202526:202527)
[chromium-blink-merge.git] / pdf / pdf.cc
blob1f580e4f1532a7d543314d2e3f1f87f0d1851f30
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "pdf/pdf.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "gin/array_buffer.h"
14 #include "gin/public/isolate_holder.h"
15 #include "pdf/out_of_process_instance.h"
16 #include "ppapi/c/ppp.h"
17 #include "ppapi/cpp/private/internal_module.h"
18 #include "ppapi/cpp/private/pdf.h"
19 #include "v8/include/v8.h"
21 namespace chrome_pdf {
23 namespace {
25 bool g_sdk_initialized_via_pepper = false;
27 gin::IsolateHolder* g_isolate_holder = nullptr;
29 void TearDownV8() {
30 g_isolate_holder->isolate()->Exit();
31 delete g_isolate_holder;
32 g_isolate_holder = nullptr;
35 } // namespace
37 PDFModule::PDFModule() {
40 PDFModule::~PDFModule() {
41 if (g_sdk_initialized_via_pepper) {
42 TearDownV8();
43 chrome_pdf::ShutdownSDK();
44 g_sdk_initialized_via_pepper = false;
48 bool PDFModule::Init() {
49 return true;
52 pp::Instance* PDFModule::CreateInstance(PP_Instance instance) {
53 if (!g_sdk_initialized_via_pepper) {
54 v8::StartupData natives;
55 v8::StartupData snapshot;
56 pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance),
57 &natives.data, &natives.raw_size,
58 &snapshot.data, &snapshot.raw_size);
59 if (natives.data) {
60 v8::V8::SetNativesDataBlob(&natives);
61 v8::V8::SetSnapshotDataBlob(&snapshot);
63 gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
64 gin::ArrayBufferAllocator::SharedInstance());
65 g_isolate_holder =
66 new gin::IsolateHolder(gin::IsolateHolder::kSingleThread);
67 g_isolate_holder->isolate()->Enter();
68 if (!chrome_pdf::InitializeSDK()) {
69 TearDownV8();
70 return NULL;
72 g_sdk_initialized_via_pepper = true;
75 return new OutOfProcessInstance(instance);
79 // Implementation of Global PPP functions ---------------------------------
80 int32_t PPP_InitializeModule(PP_Module module_id,
81 PPB_GetInterface get_browser_interface) {
82 PDFModule* module = new PDFModule();
83 if (!module->InternalInit(module_id, get_browser_interface)) {
84 delete module;
85 return PP_ERROR_FAILED;
88 pp::InternalSetModuleSingleton(module);
89 return PP_OK;
92 void PPP_ShutdownModule() {
93 delete pp::Module::Get();
94 pp::InternalSetModuleSingleton(NULL);
97 const void* PPP_GetInterface(const char* interface_name) {
98 if (!pp::Module::Get())
99 return NULL;
100 return pp::Module::Get()->GetPluginInterface(interface_name);
103 #if defined(OS_WIN)
104 bool RenderPDFPageToDC(const void* pdf_buffer,
105 int buffer_size,
106 int page_number,
107 HDC dc,
108 int dpi,
109 int bounds_origin_x,
110 int bounds_origin_y,
111 int bounds_width,
112 int bounds_height,
113 bool fit_to_bounds,
114 bool stretch_to_bounds,
115 bool keep_aspect_ratio,
116 bool center_in_bounds,
117 bool autorotate) {
118 if (!g_sdk_initialized_via_pepper) {
119 if (!chrome_pdf::InitializeSDK()) {
120 return false;
123 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
124 chrome_pdf::PDFEngineExports::Create());
125 chrome_pdf::PDFEngineExports::RenderingSettings settings(
126 dpi, dpi, pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width,
127 bounds_height),
128 fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
129 autorotate);
130 bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
131 page_number, settings, dc);
132 if (!g_sdk_initialized_via_pepper) {
133 chrome_pdf::ShutdownSDK();
135 return ret;
138 #endif // OS_WIN
140 bool GetPDFDocInfo(const void* pdf_buffer,
141 int buffer_size, int* page_count,
142 double* max_page_width) {
143 if (!g_sdk_initialized_via_pepper) {
144 if (!chrome_pdf::InitializeSDK())
145 return false;
147 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
148 chrome_pdf::PDFEngineExports::Create());
149 bool ret = engine_exports->GetPDFDocInfo(
150 pdf_buffer, buffer_size, page_count, max_page_width);
151 if (!g_sdk_initialized_via_pepper) {
152 chrome_pdf::ShutdownSDK();
154 return ret;
157 bool GetPDFPageSizeByIndex(const void* pdf_buffer,
158 int pdf_buffer_size, int page_number,
159 double* width, double* height) {
160 if (!g_sdk_initialized_via_pepper) {
161 if (!chrome_pdf::InitializeSDK())
162 return false;
164 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
165 chrome_pdf::PDFEngineExports::Create());
166 bool ret = engine_exports->GetPDFPageSizeByIndex(
167 pdf_buffer, pdf_buffer_size, page_number, width, height);
168 if (!g_sdk_initialized_via_pepper)
169 chrome_pdf::ShutdownSDK();
170 return ret;
173 bool RenderPDFPageToBitmap(const void* pdf_buffer,
174 int pdf_buffer_size,
175 int page_number,
176 void* bitmap_buffer,
177 int bitmap_width,
178 int bitmap_height,
179 int dpi,
180 bool autorotate) {
181 if (!g_sdk_initialized_via_pepper) {
182 if (!chrome_pdf::InitializeSDK())
183 return false;
185 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
186 chrome_pdf::PDFEngineExports::Create());
187 chrome_pdf::PDFEngineExports::RenderingSettings settings(
188 dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
189 autorotate);
190 bool ret = engine_exports->RenderPDFPageToBitmap(
191 pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
192 if (!g_sdk_initialized_via_pepper) {
193 chrome_pdf::ShutdownSDK();
195 return ret;
198 } // namespace chrome_pdf