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.
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
{
25 bool g_sdk_initialized_via_pepper
= false;
27 gin::IsolateHolder
* g_isolate_holder
= nullptr;
30 g_isolate_holder
->isolate()->Exit();
31 delete g_isolate_holder
;
32 g_isolate_holder
= nullptr;
37 PDFModule::PDFModule() {
40 PDFModule::~PDFModule() {
41 if (g_sdk_initialized_via_pepper
) {
43 chrome_pdf::ShutdownSDK();
44 g_sdk_initialized_via_pepper
= false;
48 bool PDFModule::Init() {
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
);
60 v8::V8::SetNativesDataBlob(&natives
);
61 v8::V8::SetSnapshotDataBlob(&snapshot
);
63 gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode
,
64 gin::ArrayBufferAllocator::SharedInstance());
66 new gin::IsolateHolder(gin::IsolateHolder::kSingleThread
);
67 g_isolate_holder
->isolate()->Enter();
68 if (!chrome_pdf::InitializeSDK()) {
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
)) {
85 return PP_ERROR_FAILED
;
88 pp::InternalSetModuleSingleton(module
);
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())
100 return pp::Module::Get()->GetPluginInterface(interface_name
);
104 bool RenderPDFPageToDC(const void* pdf_buffer
,
114 bool stretch_to_bounds
,
115 bool keep_aspect_ratio
,
116 bool center_in_bounds
,
118 if (!g_sdk_initialized_via_pepper
) {
119 if (!chrome_pdf::InitializeSDK()) {
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
,
128 fit_to_bounds
, stretch_to_bounds
, keep_aspect_ratio
, center_in_bounds
,
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();
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())
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();
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())
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();
173 bool RenderPDFPageToBitmap(const void* pdf_buffer
,
181 if (!g_sdk_initialized_via_pepper
) {
182 if (!chrome_pdf::InitializeSDK())
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,
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();
198 } // namespace chrome_pdf