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 "pdf/instance.h"
14 #include "pdf/out_of_process_instance.h"
15 #include "ppapi/c/ppp.h"
16 #include "ppapi/cpp/private/internal_module.h"
17 #include "ppapi/cpp/private/pdf.h"
18 #include "v8/include/v8.h"
20 bool g_sdk_initialized_via_pepper
= false;
22 namespace chrome_pdf
{
24 PDFModule::PDFModule() {
27 PDFModule::~PDFModule() {
28 if (g_sdk_initialized_via_pepper
) {
29 chrome_pdf::ShutdownSDK();
30 g_sdk_initialized_via_pepper
= false;
34 bool PDFModule::Init() {
38 pp::Instance
* PDFModule::CreateInstance(PP_Instance instance
) {
39 if (!g_sdk_initialized_via_pepper
) {
40 v8::StartupData natives
;
41 v8::StartupData snapshot
;
42 pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance
),
43 &natives
.data
, &natives
.raw_size
,
44 &snapshot
.data
, &snapshot
.raw_size
);
46 v8::V8::SetNativesDataBlob(&natives
);
47 v8::V8::SetSnapshotDataBlob(&snapshot
);
49 if (!chrome_pdf::InitializeSDK())
51 g_sdk_initialized_via_pepper
= true;
54 if (pp::PDF::IsOutOfProcess(pp::InstanceHandle(instance
)))
55 return new OutOfProcessInstance(instance
);
56 return new Instance(instance
);
60 // Implementation of Global PPP functions ---------------------------------
61 int32_t PPP_InitializeModule(PP_Module module_id
,
62 PPB_GetInterface get_browser_interface
) {
63 PDFModule
* module
= new PDFModule();
64 if (!module
->InternalInit(module_id
, get_browser_interface
)) {
66 return PP_ERROR_FAILED
;
69 pp::InternalSetModuleSingleton(module
);
73 void PPP_ShutdownModule() {
74 delete pp::Module::Get();
75 pp::InternalSetModuleSingleton(NULL
);
78 const void* PPP_GetInterface(const char* interface_name
) {
79 if (!pp::Module::Get())
81 return pp::Module::Get()->GetPluginInterface(interface_name
);
85 bool RenderPDFPageToDC(const void* pdf_buffer
,
95 bool stretch_to_bounds
,
96 bool keep_aspect_ratio
,
97 bool center_in_bounds
,
99 if (!g_sdk_initialized_via_pepper
) {
100 if (!chrome_pdf::InitializeSDK()) {
104 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
105 chrome_pdf::PDFEngineExports::Create());
106 chrome_pdf::PDFEngineExports::RenderingSettings
settings(
107 dpi
, dpi
, pp::Rect(bounds_origin_x
, bounds_origin_y
, bounds_width
,
109 fit_to_bounds
, stretch_to_bounds
, keep_aspect_ratio
, center_in_bounds
,
111 bool ret
= engine_exports
->RenderPDFPageToDC(pdf_buffer
, buffer_size
,
112 page_number
, settings
, dc
);
113 if (!g_sdk_initialized_via_pepper
) {
114 chrome_pdf::ShutdownSDK();
121 bool GetPDFDocInfo(const void* pdf_buffer
,
122 int buffer_size
, int* page_count
,
123 double* max_page_width
) {
124 if (!g_sdk_initialized_via_pepper
) {
125 if (!chrome_pdf::InitializeSDK())
128 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
129 chrome_pdf::PDFEngineExports::Create());
130 bool ret
= engine_exports
->GetPDFDocInfo(
131 pdf_buffer
, buffer_size
, page_count
, max_page_width
);
132 if (!g_sdk_initialized_via_pepper
) {
133 chrome_pdf::ShutdownSDK();
138 bool GetPDFPageSizeByIndex(const void* pdf_buffer
,
139 int pdf_buffer_size
, int page_number
,
140 double* width
, double* height
) {
141 if (!g_sdk_initialized_via_pepper
) {
142 if (!chrome_pdf::InitializeSDK())
145 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
146 chrome_pdf::PDFEngineExports::Create());
147 bool ret
= engine_exports
->GetPDFPageSizeByIndex(
148 pdf_buffer
, pdf_buffer_size
, page_number
, width
, height
);
149 if (!g_sdk_initialized_via_pepper
)
150 chrome_pdf::ShutdownSDK();
154 bool RenderPDFPageToBitmap(const void* pdf_buffer
,
162 if (!g_sdk_initialized_via_pepper
) {
163 if (!chrome_pdf::InitializeSDK())
166 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
167 chrome_pdf::PDFEngineExports::Create());
168 chrome_pdf::PDFEngineExports::RenderingSettings
settings(
169 dpi
, dpi
, pp::Rect(bitmap_width
, bitmap_height
), true, false, true, true,
171 bool ret
= engine_exports
->RenderPDFPageToBitmap(
172 pdf_buffer
, pdf_buffer_size
, page_number
, settings
, bitmap_buffer
);
173 if (!g_sdk_initialized_via_pepper
) {
174 chrome_pdf::ShutdownSDK();
179 } // namespace chrome_pdf