• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 PDFium 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 "public/fpdf_javascript.h"
6 
7 #include <memory>
8 
9 #include "core/fpdfapi/parser/cpdf_dictionary.h"
10 #include "core/fpdfapi/parser/cpdf_document.h"
11 #include "core/fpdfdoc/cpdf_action.h"
12 #include "core/fpdfdoc/cpdf_nametree.h"
13 #include "fpdfsdk/cpdfsdk_helpers.h"
14 #include "third_party/base/ptr_util.h"
15 
16 struct CPDF_JavaScript {
17   WideString name;
18   WideString script;
19 };
20 
21 FPDF_EXPORT int FPDF_CALLCONV
FPDFDoc_GetJavaScriptActionCount(FPDF_DOCUMENT document)22 FPDFDoc_GetJavaScriptActionCount(FPDF_DOCUMENT document) {
23   CPDF_Document* doc = CPDFDocumentFromFPDFDocument(document);
24   return doc ? CPDF_NameTree(doc, "JavaScript").GetCount() : -1;
25 }
26 
27 FPDF_EXPORT FPDF_JAVASCRIPT_ACTION FPDF_CALLCONV
FPDFDoc_GetJavaScriptAction(FPDF_DOCUMENT document,int index)28 FPDFDoc_GetJavaScriptAction(FPDF_DOCUMENT document, int index) {
29   CPDF_Document* doc = CPDFDocumentFromFPDFDocument(document);
30   if (!doc || index < 0)
31     return nullptr;
32 
33   CPDF_NameTree name_tree(doc, "JavaScript");
34   if (static_cast<size_t>(index) >= name_tree.GetCount())
35     return nullptr;
36 
37   WideString name;
38   CPDF_Dictionary* obj =
39       ToDictionary(name_tree.LookupValueAndName(index, &name));
40   if (!obj)
41     return nullptr;
42 
43   // Validate |obj|. Type is optional, but must be valid if present.
44   CPDF_Action action(obj);
45   if (action.GetType() != CPDF_Action::JavaScript)
46     return nullptr;
47 
48   Optional<WideString> script = action.MaybeGetJavaScript();
49   if (!script.has_value())
50     return nullptr;
51 
52   auto js = pdfium::MakeUnique<CPDF_JavaScript>();
53   js->name = name;
54   js->script = script.value();
55   return FPDFJavaScriptActionFromCPDFJavaScriptAction(js.release());
56 }
57 
58 FPDF_EXPORT void FPDF_CALLCONV
FPDFDoc_CloseJavaScriptAction(FPDF_JAVASCRIPT_ACTION javascript)59 FPDFDoc_CloseJavaScriptAction(FPDF_JAVASCRIPT_ACTION javascript) {
60   // Take object back across API and destroy it.
61   std::unique_ptr<CPDF_JavaScript>(
62       CPDFJavaScriptActionFromFPDFJavaScriptAction(javascript));
63 }
64 
65 FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFJavaScriptAction_GetName(FPDF_JAVASCRIPT_ACTION javascript,FPDF_WCHAR * buffer,unsigned long buflen)66 FPDFJavaScriptAction_GetName(FPDF_JAVASCRIPT_ACTION javascript,
67                              FPDF_WCHAR* buffer,
68                              unsigned long buflen) {
69   CPDF_JavaScript* js =
70       CPDFJavaScriptActionFromFPDFJavaScriptAction(javascript);
71   if (!js)
72     return 0;
73   return Utf16EncodeMaybeCopyAndReturnLength(js->name, buffer, buflen);
74 }
75 
76 FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFJavaScriptAction_GetScript(FPDF_JAVASCRIPT_ACTION javascript,FPDF_WCHAR * buffer,unsigned long buflen)77 FPDFJavaScriptAction_GetScript(FPDF_JAVASCRIPT_ACTION javascript,
78                                FPDF_WCHAR* buffer,
79                                unsigned long buflen) {
80   CPDF_JavaScript* js =
81       CPDFJavaScriptActionFromFPDFJavaScriptAction(javascript);
82   if (!js)
83     return 0;
84   return Utf16EncodeMaybeCopyAndReturnLength(js->script, buffer, buflen);
85 }
86