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