1 // Copyright 2014 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef FPDFSDK_JAVASCRIPT_DOCUMENT_H_
8 #define FPDFSDK_JAVASCRIPT_DOCUMENT_H_
9 
10 #include <list>
11 #include <memory>
12 #include <vector>
13 
14 #include "core/fpdfapi/page/cpdf_pageobject.h"
15 #include "core/fpdfapi/page/cpdf_textobject.h"
16 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
17 #include "fpdfsdk/javascript/JS_Define.h"
18 
19 class PrintParamsObj : public CJS_EmbedObj {
20  public:
21   explicit PrintParamsObj(CJS_Object* pJSObject);
~PrintParamsObj()22   ~PrintParamsObj() override {}
23 
24  public:
25   bool bUI;
26   int nStart;
27   int nEnd;
28   bool bSilent;
29   bool bShrinkToFit;
30   bool bPrintAsImage;
31   bool bReverse;
32   bool bAnnotations;
33 };
34 
35 class CJS_PrintParamsObj : public CJS_Object {
36  public:
CJS_PrintParamsObj(v8::Local<v8::Object> pObject)37   explicit CJS_PrintParamsObj(v8::Local<v8::Object> pObject)
38       : CJS_Object(pObject) {}
~CJS_PrintParamsObj()39   ~CJS_PrintParamsObj() override {}
40 
41   DECLARE_JS_CLASS();
42 };
43 
44 struct CJS_AnnotObj;
45 struct CJS_DelayAnnot;
46 struct CJS_DelayData;
47 
48 class Document : public CJS_EmbedObj {
49  public:
50   explicit Document(CJS_Object* pJSObject);
51   ~Document() override;
52 
53   bool ADBE(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
54   bool author(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
55   bool baseURL(CJS_Runtime* pRuntime,
56                CJS_PropValue& vp,
57                CFX_WideString& sError);
58   bool bookmarkRoot(CJS_Runtime* pRuntime,
59                     CJS_PropValue& vp,
60                     CFX_WideString& sError);
61   bool calculate(CJS_Runtime* pRuntime,
62                  CJS_PropValue& vp,
63                  CFX_WideString& sError);
64   bool Collab(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
65   bool creationDate(CJS_Runtime* pRuntime,
66                     CJS_PropValue& vp,
67                     CFX_WideString& sError);
68   bool creator(CJS_Runtime* pRuntime,
69                CJS_PropValue& vp,
70                CFX_WideString& sError);
71   bool delay(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
72   bool dirty(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
73   bool documentFileName(CJS_Runtime* pRuntime,
74                         CJS_PropValue& vp,
75                         CFX_WideString& sError);
76   bool external(CJS_Runtime* pRuntime,
77                 CJS_PropValue& vp,
78                 CFX_WideString& sError);
79   bool filesize(CJS_Runtime* pRuntime,
80                 CJS_PropValue& vp,
81                 CFX_WideString& sError);
82   bool icons(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
83   bool info(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
84   bool keywords(CJS_Runtime* pRuntime,
85                 CJS_PropValue& vp,
86                 CFX_WideString& sError);
87   bool layout(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
88   bool media(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
89   bool modDate(CJS_Runtime* pRuntime,
90                CJS_PropValue& vp,
91                CFX_WideString& sError);
92   bool mouseX(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
93   bool mouseY(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
94   bool numFields(CJS_Runtime* pRuntime,
95                  CJS_PropValue& vp,
96                  CFX_WideString& sError);
97   bool numPages(CJS_Runtime* pRuntime,
98                 CJS_PropValue& vp,
99                 CFX_WideString& sError);
100   bool pageNum(CJS_Runtime* pRuntime,
101                CJS_PropValue& vp,
102                CFX_WideString& sError);
103   bool pageWindowRect(CJS_Runtime* pRuntime,
104                       CJS_PropValue& vp,
105                       CFX_WideString& sError);
106   bool path(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
107   bool producer(CJS_Runtime* pRuntime,
108                 CJS_PropValue& vp,
109                 CFX_WideString& sError);
110   bool subject(CJS_Runtime* pRuntime,
111                CJS_PropValue& vp,
112                CFX_WideString& sError);
113   bool title(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
114   bool zoom(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
115   bool zoomType(CJS_Runtime* pRuntime,
116                 CJS_PropValue& vp,
117                 CFX_WideString& sError);
118 
119   bool addAnnot(CJS_Runtime* pRuntime,
120                 const std::vector<CJS_Value>& params,
121                 CJS_Value& vRet,
122                 CFX_WideString& sError);
123   bool addField(CJS_Runtime* pRuntime,
124                 const std::vector<CJS_Value>& params,
125                 CJS_Value& vRet,
126                 CFX_WideString& sError);
127   bool addLink(CJS_Runtime* pRuntime,
128                const std::vector<CJS_Value>& params,
129                CJS_Value& vRet,
130                CFX_WideString& sError);
131   bool addIcon(CJS_Runtime* pRuntime,
132                const std::vector<CJS_Value>& params,
133                CJS_Value& vRet,
134                CFX_WideString& sError);
135   bool calculateNow(CJS_Runtime* pRuntime,
136                     const std::vector<CJS_Value>& params,
137                     CJS_Value& vRet,
138                     CFX_WideString& sError);
139   bool closeDoc(CJS_Runtime* pRuntime,
140                 const std::vector<CJS_Value>& params,
141                 CJS_Value& vRet,
142                 CFX_WideString& sError);
143   bool createDataObject(CJS_Runtime* pRuntime,
144                         const std::vector<CJS_Value>& params,
145                         CJS_Value& vRet,
146                         CFX_WideString& sError);
147   bool deletePages(CJS_Runtime* pRuntime,
148                    const std::vector<CJS_Value>& params,
149                    CJS_Value& vRet,
150                    CFX_WideString& sError);
151   bool exportAsText(CJS_Runtime* pRuntime,
152                     const std::vector<CJS_Value>& params,
153                     CJS_Value& vRet,
154                     CFX_WideString& sError);
155   bool exportAsFDF(CJS_Runtime* pRuntime,
156                    const std::vector<CJS_Value>& params,
157                    CJS_Value& vRet,
158                    CFX_WideString& sError);
159   bool exportAsXFDF(CJS_Runtime* pRuntime,
160                     const std::vector<CJS_Value>& params,
161                     CJS_Value& vRet,
162                     CFX_WideString& sError);
163   bool extractPages(CJS_Runtime* pRuntime,
164                     const std::vector<CJS_Value>& params,
165                     CJS_Value& vRet,
166                     CFX_WideString& sError);
167   bool getAnnot(CJS_Runtime* pRuntime,
168                 const std::vector<CJS_Value>& params,
169                 CJS_Value& vRet,
170                 CFX_WideString& sError);
171   bool getAnnots(CJS_Runtime* pRuntime,
172                  const std::vector<CJS_Value>& params,
173                  CJS_Value& vRet,
174                  CFX_WideString& sError);
175   bool getAnnot3D(CJS_Runtime* pRuntime,
176                   const std::vector<CJS_Value>& params,
177                   CJS_Value& vRet,
178                   CFX_WideString& sError);
179   bool getAnnots3D(CJS_Runtime* pRuntime,
180                    const std::vector<CJS_Value>& params,
181                    CJS_Value& vRet,
182                    CFX_WideString& sError);
183   bool getField(CJS_Runtime* pRuntime,
184                 const std::vector<CJS_Value>& params,
185                 CJS_Value& vRet,
186                 CFX_WideString& sError);
187   bool getIcon(CJS_Runtime* pRuntime,
188                const std::vector<CJS_Value>& params,
189                CJS_Value& vRet,
190                CFX_WideString& sError);
191   bool getLinks(CJS_Runtime* pRuntime,
192                 const std::vector<CJS_Value>& params,
193                 CJS_Value& vRet,
194                 CFX_WideString& sError);
195   bool getNthFieldName(CJS_Runtime* pRuntime,
196                        const std::vector<CJS_Value>& params,
197                        CJS_Value& vRet,
198                        CFX_WideString& sError);
199   bool getOCGs(CJS_Runtime* pRuntime,
200                const std::vector<CJS_Value>& params,
201                CJS_Value& vRet,
202                CFX_WideString& sError);
203   bool getPageBox(CJS_Runtime* pRuntime,
204                   const std::vector<CJS_Value>& params,
205                   CJS_Value& vRet,
206                   CFX_WideString& sError);
207   bool getPageNthWord(CJS_Runtime* pRuntime,
208                       const std::vector<CJS_Value>& params,
209                       CJS_Value& vRet,
210                       CFX_WideString& sError);
211   bool getPageNthWordQuads(CJS_Runtime* pRuntime,
212                            const std::vector<CJS_Value>& params,
213                            CJS_Value& vRet,
214                            CFX_WideString& sError);
215   bool getPageNumWords(CJS_Runtime* pRuntime,
216                        const std::vector<CJS_Value>& params,
217                        CJS_Value& vRet,
218                        CFX_WideString& sError);
219   bool getPrintParams(CJS_Runtime* pRuntime,
220                       const std::vector<CJS_Value>& params,
221                       CJS_Value& vRet,
222                       CFX_WideString& sError);
223   bool getURL(CJS_Runtime* pRuntime,
224               const std::vector<CJS_Value>& params,
225               CJS_Value& vRet,
226               CFX_WideString& sError);
227   bool gotoNamedDest(CJS_Runtime* pRuntime,
228                      const std::vector<CJS_Value>& params,
229                      CJS_Value& vRet,
230                      CFX_WideString& sError);
231   bool importAnFDF(CJS_Runtime* pRuntime,
232                    const std::vector<CJS_Value>& params,
233                    CJS_Value& vRet,
234                    CFX_WideString& sError);
235   bool importAnXFDF(CJS_Runtime* pRuntime,
236                     const std::vector<CJS_Value>& params,
237                     CJS_Value& vRet,
238                     CFX_WideString& sError);
239   bool importTextData(CJS_Runtime* pRuntime,
240                       const std::vector<CJS_Value>& params,
241                       CJS_Value& vRet,
242                       CFX_WideString& sError);
243   bool insertPages(CJS_Runtime* pRuntime,
244                    const std::vector<CJS_Value>& params,
245                    CJS_Value& vRet,
246                    CFX_WideString& sError);
247   bool mailForm(CJS_Runtime* pRuntime,
248                 const std::vector<CJS_Value>& params,
249                 CJS_Value& vRet,
250                 CFX_WideString& sError);
251   bool print(CJS_Runtime* pRuntime,
252              const std::vector<CJS_Value>& params,
253              CJS_Value& vRet,
254              CFX_WideString& sError);
255   bool removeField(CJS_Runtime* pRuntime,
256                    const std::vector<CJS_Value>& params,
257                    CJS_Value& vRet,
258                    CFX_WideString& sError);
259   bool replacePages(CJS_Runtime* pRuntime,
260                     const std::vector<CJS_Value>& params,
261                     CJS_Value& vRet,
262                     CFX_WideString& sError);
263   bool resetForm(CJS_Runtime* pRuntime,
264                  const std::vector<CJS_Value>& params,
265                  CJS_Value& vRet,
266                  CFX_WideString& sError);
267   bool saveAs(CJS_Runtime* pRuntime,
268               const std::vector<CJS_Value>& params,
269               CJS_Value& vRet,
270               CFX_WideString& sError);
271   bool submitForm(CJS_Runtime* pRuntime,
272                   const std::vector<CJS_Value>& params,
273                   CJS_Value& vRet,
274                   CFX_WideString& sError);
275   bool syncAnnotScan(CJS_Runtime* pRuntime,
276                      const std::vector<CJS_Value>& params,
277                      CJS_Value& vRet,
278                      CFX_WideString& sError);
279   bool mailDoc(CJS_Runtime* pRuntime,
280                const std::vector<CJS_Value>& params,
281                CJS_Value& vRet,
282                CFX_WideString& sError);
283   bool removeIcon(CJS_Runtime* pRuntime,
284                   const std::vector<CJS_Value>& params,
285                   CJS_Value& vRet,
286                   CFX_WideString& sError);
287   bool URL(CJS_Runtime* pRuntime, CJS_PropValue& vp, CFX_WideString& sError);
288 
289   void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv);
GetFormFillEnv()290   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const {
291     return m_pFormFillEnv.Get();
292   }
293   void AddDelayData(CJS_DelayData* pData);
294   void DoFieldDelay(const CFX_WideString& sFieldName, int nControlIndex);
295   CJS_Document* GetCJSDoc() const;
296 
297  private:
298   bool IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect);
299   int CountWords(CPDF_TextObject* pTextObj);
300   CFX_WideString GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex);
301 
302   bool getPropertyInternal(CJS_Runtime* pRuntime,
303                            CJS_PropValue& vp,
304                            const CFX_ByteString& propName,
305                            CFX_WideString& sError);
306 
307   CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
308   CFX_WideString m_cwBaseURL;
309   std::list<std::unique_ptr<CJS_DelayData>> m_DelayData;
310   // Needs to be a std::list for iterator stability.
311   std::list<CFX_WideString> m_IconNames;
312   bool m_bDelay;
313 };
314 
315 class CJS_Document : public CJS_Object {
316  public:
CJS_Document(v8::Local<v8::Object> pObject)317   explicit CJS_Document(v8::Local<v8::Object> pObject) : CJS_Object(pObject) {}
~CJS_Document()318   ~CJS_Document() override {}
319 
320   // CJS_Object
321   void InitInstance(IJS_Runtime* pIRuntime) override;
322 
323   DECLARE_JS_CLASS();
324 
325   JS_STATIC_PROP(ADBE, Document);
326   JS_STATIC_PROP(author, Document);
327   JS_STATIC_PROP(baseURL, Document);
328   JS_STATIC_PROP(bookmarkRoot, Document);
329   JS_STATIC_PROP(calculate, Document);
330   JS_STATIC_PROP(Collab, Document);
331   JS_STATIC_PROP(creationDate, Document);
332   JS_STATIC_PROP(creator, Document);
333   JS_STATIC_PROP(delay, Document);
334   JS_STATIC_PROP(dirty, Document);
335   JS_STATIC_PROP(documentFileName, Document);
336   JS_STATIC_PROP(external, Document);
337   JS_STATIC_PROP(filesize, Document);
338   JS_STATIC_PROP(icons, Document);
339   JS_STATIC_PROP(info, Document);
340   JS_STATIC_PROP(keywords, Document);
341   JS_STATIC_PROP(layout, Document);
342   JS_STATIC_PROP(media, Document);
343   JS_STATIC_PROP(modDate, Document);
344   JS_STATIC_PROP(mouseX, Document);
345   JS_STATIC_PROP(mouseY, Document);
346   JS_STATIC_PROP(numFields, Document);
347   JS_STATIC_PROP(numPages, Document);
348   JS_STATIC_PROP(pageNum, Document);
349   JS_STATIC_PROP(pageWindowRect, Document);
350   JS_STATIC_PROP(path, Document);
351   JS_STATIC_PROP(producer, Document);
352   JS_STATIC_PROP(subject, Document);
353   JS_STATIC_PROP(title, Document);
354   JS_STATIC_PROP(URL, Document);
355   JS_STATIC_PROP(zoom, Document);
356   JS_STATIC_PROP(zoomType, Document);
357 
358   JS_STATIC_METHOD(addAnnot, Document);
359   JS_STATIC_METHOD(addField, Document);
360   JS_STATIC_METHOD(addLink, Document);
361   JS_STATIC_METHOD(addIcon, Document);
362   JS_STATIC_METHOD(calculateNow, Document);
363   JS_STATIC_METHOD(closeDoc, Document);
364   JS_STATIC_METHOD(createDataObject, Document);
365   JS_STATIC_METHOD(deletePages, Document);
366   JS_STATIC_METHOD(exportAsText, Document);
367   JS_STATIC_METHOD(exportAsFDF, Document);
368   JS_STATIC_METHOD(exportAsXFDF, Document);
369   JS_STATIC_METHOD(extractPages, Document);
370   JS_STATIC_METHOD(getAnnot, Document);
371   JS_STATIC_METHOD(getAnnots, Document);
372   JS_STATIC_METHOD(getAnnot3D, Document);
373   JS_STATIC_METHOD(getAnnots3D, Document);
374   JS_STATIC_METHOD(getField, Document);
375   JS_STATIC_METHOD(getIcon, Document);
376   JS_STATIC_METHOD(getLinks, Document);
377   JS_STATIC_METHOD(getNthFieldName, Document);
378   JS_STATIC_METHOD(getOCGs, Document);
379   JS_STATIC_METHOD(getPageBox, Document);
380   JS_STATIC_METHOD(getPageNthWord, Document);
381   JS_STATIC_METHOD(getPageNthWordQuads, Document);
382   JS_STATIC_METHOD(getPageNumWords, Document);
383   JS_STATIC_METHOD(getPrintParams, Document);
384   JS_STATIC_METHOD(getURL, Document);
385   JS_STATIC_METHOD(gotoNamedDest, Document);
386   JS_STATIC_METHOD(importAnFDF, Document);
387   JS_STATIC_METHOD(importAnXFDF, Document);
388   JS_STATIC_METHOD(importTextData, Document);
389   JS_STATIC_METHOD(insertPages, Document);
390   JS_STATIC_METHOD(mailForm, Document);
391   JS_STATIC_METHOD(print, Document);
392   JS_STATIC_METHOD(removeField, Document);
393   JS_STATIC_METHOD(replacePages, Document);
394   JS_STATIC_METHOD(removeIcon, Document);
395   JS_STATIC_METHOD(resetForm, Document);
396   JS_STATIC_METHOD(saveAs, Document);
397   JS_STATIC_METHOD(submitForm, Document);
398   JS_STATIC_METHOD(syncAnnotScan, Document);
399   JS_STATIC_METHOD(mailDoc, Document);
400 };
401 
402 #endif  // FPDFSDK_JAVASCRIPT_DOCUMENT_H_
403