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/out_of_process_instance.h"
14 #include "ppapi/c/ppp.h"
15 #include "ppapi/cpp/private/internal_module.h"
16 #include "ppapi/cpp/private/pdf.h"
17 #include "v8/include/v8.h"
19 bool g_sdk_initialized_via_pepper
= false;
21 namespace chrome_pdf
{
23 PDFModule::PDFModule() {
26 PDFModule::~PDFModule() {
27 if (g_sdk_initialized_via_pepper
) {
28 chrome_pdf::ShutdownSDK();
29 g_sdk_initialized_via_pepper
= false;
33 bool PDFModule::Init() {
37 pp::Instance
* PDFModule::CreateInstance(PP_Instance instance
) {
38 if (!g_sdk_initialized_via_pepper
) {
39 v8::StartupData natives
;
40 v8::StartupData snapshot
;
41 pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance
),
42 &natives
.data
, &natives
.raw_size
,
43 &snapshot
.data
, &snapshot
.raw_size
);
45 v8::V8::SetNativesDataBlob(&natives
);
46 v8::V8::SetSnapshotDataBlob(&snapshot
);
48 if (!chrome_pdf::InitializeSDK())
50 g_sdk_initialized_via_pepper
= true;
53 return new OutOfProcessInstance(instance
);
57 // Implementation of Global PPP functions ---------------------------------
58 int32_t PPP_InitializeModule(PP_Module module_id
,
59 PPB_GetInterface get_browser_interface
) {
60 PDFModule
* module
= new PDFModule();
61 if (!module
->InternalInit(module_id
, get_browser_interface
)) {
63 return PP_ERROR_FAILED
;
66 pp::InternalSetModuleSingleton(module
);
70 void PPP_ShutdownModule() {
71 delete pp::Module::Get();
72 pp::InternalSetModuleSingleton(NULL
);
75 const void* PPP_GetInterface(const char* interface_name
) {
76 if (!pp::Module::Get())
78 return pp::Module::Get()->GetPluginInterface(interface_name
);
82 bool RenderPDFPageToDC(const void* pdf_buffer
,
92 bool stretch_to_bounds
,
93 bool keep_aspect_ratio
,
94 bool center_in_bounds
,
96 if (!g_sdk_initialized_via_pepper
) {
97 if (!chrome_pdf::InitializeSDK()) {
101 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
102 chrome_pdf::PDFEngineExports::Create());
103 chrome_pdf::PDFEngineExports::RenderingSettings
settings(
104 dpi
, dpi
, pp::Rect(bounds_origin_x
, bounds_origin_y
, bounds_width
,
106 fit_to_bounds
, stretch_to_bounds
, keep_aspect_ratio
, center_in_bounds
,
108 bool ret
= engine_exports
->RenderPDFPageToDC(pdf_buffer
, buffer_size
,
109 page_number
, settings
, dc
);
110 if (!g_sdk_initialized_via_pepper
) {
111 chrome_pdf::ShutdownSDK();
118 bool GetPDFDocInfo(const void* pdf_buffer
,
119 int buffer_size
, int* page_count
,
120 double* max_page_width
) {
121 if (!g_sdk_initialized_via_pepper
) {
122 if (!chrome_pdf::InitializeSDK())
125 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
126 chrome_pdf::PDFEngineExports::Create());
127 bool ret
= engine_exports
->GetPDFDocInfo(
128 pdf_buffer
, buffer_size
, page_count
, max_page_width
);
129 if (!g_sdk_initialized_via_pepper
) {
130 chrome_pdf::ShutdownSDK();
135 bool GetPDFPageSizeByIndex(const void* pdf_buffer
,
136 int pdf_buffer_size
, int page_number
,
137 double* width
, double* height
) {
138 if (!g_sdk_initialized_via_pepper
) {
139 if (!chrome_pdf::InitializeSDK())
142 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
143 chrome_pdf::PDFEngineExports::Create());
144 bool ret
= engine_exports
->GetPDFPageSizeByIndex(
145 pdf_buffer
, pdf_buffer_size
, page_number
, width
, height
);
146 if (!g_sdk_initialized_via_pepper
)
147 chrome_pdf::ShutdownSDK();
151 bool RenderPDFPageToBitmap(const void* pdf_buffer
,
159 if (!g_sdk_initialized_via_pepper
) {
160 if (!chrome_pdf::InitializeSDK())
163 scoped_ptr
<chrome_pdf::PDFEngineExports
> engine_exports(
164 chrome_pdf::PDFEngineExports::Create());
165 chrome_pdf::PDFEngineExports::RenderingSettings
settings(
166 dpi
, dpi
, pp::Rect(bitmap_width
, bitmap_height
), true, false, true, true,
168 bool ret
= engine_exports
->RenderPDFPageToBitmap(
169 pdf_buffer
, pdf_buffer_size
, page_number
, settings
, bitmap_buffer
);
170 if (!g_sdk_initialized_via_pepper
) {
171 chrome_pdf::ShutdownSDK();
176 } // namespace chrome_pdf