1// Copyright (c) 2013 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.
4
5#include "base/logging.h"
6#include "ppapi/c/pp_errors.h"
7#include "ppapi/c/private/ppb_pdf.h"
8#include "ppapi/thunk/enter.h"
9#include "ppapi/thunk/ppb_flash_font_file_api.h"
10#include "ppapi/thunk/ppb_pdf_api.h"
11#include "ppapi/thunk/resource_creation_api.h"
12#include "ppapi/thunk/thunk.h"
13
14namespace ppapi {
15namespace thunk {
16
17namespace {
18
19PP_Var GetLocalizedString(PP_Instance instance, PP_ResourceString string_id) {
20  EnterInstanceAPI<PPB_PDF_API> enter(instance);
21  if (enter.failed())
22    return PP_MakeUndefined();
23  return enter.functions()->GetLocalizedString(string_id);
24}
25
26PP_Resource GetResourceImage(PP_Instance instance,
27                             PP_ResourceImage image_id) {
28  EnterInstanceAPI<PPB_PDF_API> enter(instance);
29  if (enter.failed())
30    return 0;
31  return enter.functions()->GetResourceImage(image_id);
32}
33
34PP_Resource GetFontFileWithFallback(
35    PP_Instance instance,
36    const PP_BrowserFont_Trusted_Description* description,
37    PP_PrivateFontCharset charset) {
38  // TODO(raymes): Eventually we should replace the use of this function with
39  // either PPB_Flash_Font_File or PPB_TrueType_Font directly in the PDF code.
40  // For now just call into PPB_Flash_Font_File which has the exact same API.
41  EnterResourceCreation enter(instance);
42  if (enter.failed())
43    return 0;
44  return enter.functions()->CreateFlashFontFile(instance, description, charset);
45}
46
47bool GetFontTableForPrivateFontFile(PP_Resource font_file,
48                                    uint32_t table,
49                                    void* output,
50                                    uint32_t* output_length) {
51  // TODO(raymes): Eventually we should replace the use of this function with
52  // either PPB_Flash_Font_File or PPB_TrueType_Font directly in the PDF code.
53  // For now just call into PPB_Flash_Font_File which has the exact same API.
54  EnterResource<PPB_Flash_FontFile_API> enter(font_file, true);
55  if (enter.failed())
56    return PP_FALSE;
57  return PP_ToBool(enter.object()->GetFontTable(table, output, output_length));
58}
59
60void SearchString(PP_Instance instance,
61                  const unsigned short* string,
62                  const unsigned short* term,
63                  bool case_sensitive,
64                  PP_PrivateFindResult** results,
65                  int* count) {
66  EnterInstanceAPI<PPB_PDF_API> enter(instance);
67  if (enter.failed())
68    return;
69  enter.functions()->SearchString(string, term, case_sensitive, results, count);
70}
71
72void DidStartLoading(PP_Instance instance) {
73  EnterInstanceAPI<PPB_PDF_API> enter(instance);
74  if (enter.succeeded())
75    enter.functions()->DidStartLoading();
76}
77
78void DidStopLoading(PP_Instance instance) {
79  EnterInstanceAPI<PPB_PDF_API> enter(instance);
80  if (enter.succeeded())
81    enter.functions()->DidStopLoading();
82}
83
84void SetContentRestriction(PP_Instance instance, int restrictions) {
85  EnterInstanceAPI<PPB_PDF_API> enter(instance);
86  if (enter.succeeded())
87    enter.functions()->SetContentRestriction(restrictions);
88}
89
90void HistogramPDFPageCount(PP_Instance instance, int count) {
91  EnterInstanceAPI<PPB_PDF_API> enter(instance);
92  if (enter.succeeded())
93    enter.functions()->HistogramPDFPageCount(count);
94}
95
96void UserMetricsRecordAction(PP_Instance instance, PP_Var action) {
97  EnterInstanceAPI<PPB_PDF_API> enter(instance);
98  if (enter.succeeded())
99    enter.functions()->UserMetricsRecordAction(action);
100}
101
102void HasUnsupportedFeature(PP_Instance instance) {
103  EnterInstanceAPI<PPB_PDF_API> enter(instance);
104  if (enter.succeeded())
105    enter.functions()->HasUnsupportedFeature();
106}
107
108void SaveAs(PP_Instance instance) {
109  EnterInstanceAPI<PPB_PDF_API> enter(instance);
110  if (enter.succeeded())
111    enter.functions()->SaveAs();
112}
113
114void Print(PP_Instance instance) {
115  EnterInstanceAPI<PPB_PDF_API> enter(instance);
116  if (enter.succeeded())
117    enter.functions()->Print();
118}
119
120PP_Bool IsFeatureEnabled(PP_Instance instance, PP_PDFFeature feature) {
121  EnterInstanceAPI<PPB_PDF_API> enter(instance);
122  if (enter.failed())
123    return PP_FALSE;
124  return enter.functions()->IsFeatureEnabled(feature);
125}
126
127PP_Resource GetResourceImageForScale(PP_Instance instance,
128                                     PP_ResourceImage image_id,
129                                     float scale) {
130  EnterInstanceAPI<PPB_PDF_API> enter(instance);
131  if (enter.failed())
132    return 0;
133  return enter.functions()->GetResourceImageForScale(image_id, scale);
134}
135
136PP_Var ModalPromptForPassword(PP_Instance instance_id,
137                              PP_Var message) {
138  // TODO(raymes): Implement or remove this function.
139  NOTIMPLEMENTED();
140  return PP_MakeUndefined();
141}
142
143PP_Bool IsOutOfProcess(PP_Instance instance) {
144  EnterInstanceAPI<PPB_PDF_API> enter(instance);
145  if (enter.failed())
146    return PP_FALSE;
147  return enter.functions()->IsOutOfProcess();
148}
149
150void SetSelectedText(PP_Instance instance,
151                     const char* selected_text) {
152  EnterInstanceAPI<PPB_PDF_API> enter(instance);
153  if (enter.succeeded())
154    enter.functions()->SetSelectedText(selected_text);
155}
156
157void SetLinkUnderCursor(PP_Instance instance, const char* url) {
158  EnterInstanceAPI<PPB_PDF_API> enter(instance);
159  if (enter.failed())
160    return;
161  enter.functions()->SetLinkUnderCursor(url);
162}
163
164const PPB_PDF g_ppb_pdf_thunk = {
165  &GetLocalizedString,
166  &GetResourceImage,
167  &GetFontFileWithFallback,
168  &GetFontTableForPrivateFontFile,
169  &SearchString,
170  &DidStartLoading,
171  &DidStopLoading,
172  &SetContentRestriction,
173  &HistogramPDFPageCount,
174  &UserMetricsRecordAction,
175  &HasUnsupportedFeature,
176  &SaveAs,
177  &Print,
178  &IsFeatureEnabled,
179  &GetResourceImageForScale,
180  &ModalPromptForPassword,
181  &IsOutOfProcess,
182  &SetSelectedText,
183  &SetLinkUnderCursor,
184};
185
186}  // namespace
187
188const PPB_PDF* GetPPB_PDF_Thunk() {
189  return &g_ppb_pdf_thunk;
190}
191
192}  // namespace thunk
193}  // namespace ppapi
194