Adds support for mojo apps to live in their own directory
[chromium-blink-merge.git] / pdf / pdf.cc
blob7aba41229d376848c64ea8eda2a70e025592079d
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 "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() {
35 return true;
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);
45 if (natives.data) {
46 v8::V8::SetNativesDataBlob(&natives);
47 v8::V8::SetSnapshotDataBlob(&snapshot);
49 if (!chrome_pdf::InitializeSDK())
50 return NULL;
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)) {
65 delete module;
66 return PP_ERROR_FAILED;
69 pp::InternalSetModuleSingleton(module);
70 return PP_OK;
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())
80 return NULL;
81 return pp::Module::Get()->GetPluginInterface(interface_name);
84 #if defined(OS_WIN)
85 bool RenderPDFPageToDC(const void* pdf_buffer,
86 int buffer_size,
87 int page_number,
88 HDC dc,
89 int dpi,
90 int bounds_origin_x,
91 int bounds_origin_y,
92 int bounds_width,
93 int bounds_height,
94 bool fit_to_bounds,
95 bool stretch_to_bounds,
96 bool keep_aspect_ratio,
97 bool center_in_bounds,
98 bool autorotate) {
99 if (!g_sdk_initialized_via_pepper) {
100 if (!chrome_pdf::InitializeSDK()) {
101 return false;
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,
108 bounds_height),
109 fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
110 autorotate);
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();
116 return ret;
119 #endif // OS_WIN
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())
126 return false;
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();
135 return ret;
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())
143 return false;
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();
151 return ret;
154 bool RenderPDFPageToBitmap(const void* pdf_buffer,
155 int pdf_buffer_size,
156 int page_number,
157 void* bitmap_buffer,
158 int bitmap_width,
159 int bitmap_height,
160 int dpi,
161 bool autorotate) {
162 if (!g_sdk_initialized_via_pepper) {
163 if (!chrome_pdf::InitializeSDK())
164 return false;
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,
170 autorotate);
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();
176 return ret;
179 } // namespace chrome_pdf