• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_CPDFSDK_FORMFILLENVIRONMENT_H_
8 #define FPDFSDK_CPDFSDK_FORMFILLENVIRONMENT_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "core/fpdfapi/page/cpdf_occontext.h"
14 #include "core/fpdfapi/page/cpdf_page.h"
15 #include "core/fpdfapi/parser/cpdf_document.h"
16 #include "core/fxcrt/observed_ptr.h"
17 #include "core/fxcrt/timerhandler_iface.h"
18 #include "fpdfsdk/cpdfsdk_annot.h"
19 #include "fpdfsdk/pwl/ipwl_systemhandler.h"
20 #include "public/fpdf_formfill.h"
21 
22 class CFFL_InteractiveFormFiller;
23 class CPDFSDK_ActionHandler;
24 class CPDFSDK_AnnotHandlerMgr;
25 class CPDFSDK_InteractiveForm;
26 class CPDFSDK_PageView;
27 class IJS_Runtime;
28 
29 // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken
30 // since modifying the result would impact |bsUTF16LE|.
31 FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE);
32 
33 // The CPDFSDK_FormFillEnvironment is "owned" by the embedder across the
34 // C API as a FPDF_FormHandle, and may pop out of existence at any time,
35 // so long as the associated embedder-owned FPDF_Document outlives it.
36 // Pointers from objects in the FPDF_Document ownership hierarchy should
37 // be ObservedPtr<> so as to clear themselves when the embedder "exits"
38 // the form fill environment.  Pointers from objects in this ownership
39 // heirarcy to objects in the FPDF_Document ownership hierarcy should be
40 // UnownedPtr<>, as should pointers from objects in this ownership
41 // hierarcy back to the form fill environment itself, so as to flag any
42 // lingering lifetime issues via the memory tools.
43 
44 class CPDFSDK_FormFillEnvironment final : public Observable,
45                                           public TimerHandlerIface,
46                                           public IPWL_SystemHandler {
47  public:
48   CPDFSDK_FormFillEnvironment(
49       CPDF_Document* pDoc,
50       FPDF_FORMFILLINFO* pFFinfo,
51       std::unique_ptr<CPDFSDK_AnnotHandlerMgr> pHandlerMgr);
52 
53   ~CPDFSDK_FormFillEnvironment() override;
54 
55   // TimerHandlerIface:
56   int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
57   void KillTimer(int32_t nTimerID) override;
58 
59   // IPWL_SystemHandler:
60   void InvalidateRect(PerWindowData* pWidgetData,
61                       const CFX_FloatRect& rect) override;
62   void OutputSelectedRect(CFFL_FormFiller* pFormFiller,
63                           const CFX_FloatRect& rect) override;
64   bool IsSelectionImplemented() const override;
65   void SetCursor(int32_t nCursorType) override;
66 
67   CPDFSDK_PageView* GetPageView(IPDF_Page* pUnderlyingPage, bool renew);
68   CPDFSDK_PageView* GetPageView(int nIndex);
69 
70   void RemovePageView(IPDF_Page* pUnderlyingPage);
71   void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
72 
GetFocusAnnot()73   CPDFSDK_Annot* GetFocusAnnot() const { return m_pFocusAnnot.Get(); }
74   bool SetFocusAnnot(ObservedPtr<CPDFSDK_Annot>* pAnnot);
75   bool KillFocusAnnot(uint32_t nFlag);
76   void ClearAllFocusedAnnots();
77 
78   int GetPageCount() const;
79   bool GetPermissions(int nFlag) const;
80 
GetChangeMark()81   bool GetChangeMark() const { return m_bChangeMask; }
SetChangeMark()82   void SetChangeMark() { m_bChangeMask = true; }
ClearChangeMark()83   void ClearChangeMark() { m_bChangeMask = false; }
84 
85   void ProcJavascriptAction();
86   bool ProcOpenAction();
87   void Invalidate(IPDF_Page* page, const FX_RECT& rect);
88 
89   void OnChange();
90   void ExecuteNamedAction(const char* namedAction);
91   void OnSetFieldInputFocus(FPDF_WIDESTRING focusText,
92                             FPDF_DWORD nTextLen,
93                             bool bFocus);
94   void DoURIAction(const char* bsURI);
95   void DoGoToAction(int nPageIndex,
96                     int zoomMode,
97                     float* fPosArray,
98                     int sizeOfArray);
99 
GetPDFDocument()100   CPDF_Document* GetPDFDocument() const { return m_pCPDFDoc.Get(); }
GetDocExtension()101   CPDF_Document::Extension* GetDocExtension() const {
102     return m_pCPDFDoc->GetExtension();
103   }
104 
IsJSPlatformPresent()105   bool IsJSPlatformPresent() const { return m_pInfo && m_pInfo->m_pJsPlatform; }
106 
107 #ifdef PDF_ENABLE_V8
108   CPDFSDK_PageView* GetCurrentView();
109   FPDF_PAGE GetCurrentPage() const;
110 
111   WideString GetLanguage();
112   WideString GetPlatform();
113 
114   int JS_appAlert(const WideString& Msg,
115                   const WideString& Title,
116                   int Type,
117                   int Icon);
118   int JS_appResponse(const WideString& Question,
119                      const WideString& Title,
120                      const WideString& Default,
121                      const WideString& cLabel,
122                      FPDF_BOOL bPassword,
123                      void* response,
124                      int length);
125   void JS_appBeep(int nType);
126   WideString JS_fieldBrowse();
127   void JS_docmailForm(void* mailData,
128                       int length,
129                       FPDF_BOOL bUI,
130                       const WideString& To,
131                       const WideString& Subject,
132                       const WideString& CC,
133                       const WideString& BCC,
134                       const WideString& Msg);
135   void JS_docprint(FPDF_BOOL bUI,
136                    int nStart,
137                    int nEnd,
138                    FPDF_BOOL bSilent,
139                    FPDF_BOOL bShrinkToFit,
140                    FPDF_BOOL bPrintAsImage,
141                    FPDF_BOOL bReverse,
142                    FPDF_BOOL bAnnotations);
143   void JS_docgotoPage(int nPageNum);
144   WideString JS_docGetFilePath();
145 
146 #ifdef PDF_ENABLE_XFA
147   int GetPageViewCount() const;
148   void DisplayCaret(IPDF_Page* page,
149                     FPDF_BOOL bVisible,
150                     double left,
151                     double top,
152                     double right,
153                     double bottom);
154   int GetCurrentPageIndex() const;
155   void SetCurrentPage(int iCurPage);
156 
157   // TODO(dsinclair): This should probably change to PDFium?
FFI_GetAppName()158   WideString FFI_GetAppName() const { return WideString(L"Acrobat"); }
159 
160   void GotoURL(const WideString& wsURL);
161   FS_RECTF GetPageViewRect(IPDF_Page* page);
162   bool PopupMenu(IPDF_Page* page,
163                  FPDF_WIDGET hWidget,
164                  int menuFlag,
165                  const CFX_PointF& pt);
166   void EmailTo(FPDF_FILEHANDLER* fileHandler,
167                FPDF_WIDESTRING pTo,
168                FPDF_WIDESTRING pSubject,
169                FPDF_WIDESTRING pCC,
170                FPDF_WIDESTRING pBcc,
171                FPDF_WIDESTRING pMsg);
172   void UploadTo(FPDF_FILEHANDLER* fileHandler,
173                 int fileFlag,
174                 FPDF_WIDESTRING uploadTo);
175   FPDF_FILEHANDLER* OpenFile(int fileType,
176                              FPDF_WIDESTRING wsURL,
177                              const char* mode);
178   RetainPtr<IFX_SeekableReadStream> DownloadFromURL(const WideString& url);
179   WideString PostRequestURL(const WideString& wsURL,
180                             const WideString& wsData,
181                             const WideString& wsContentType,
182                             const WideString& wsEncode,
183                             const WideString& wsHeader);
184   FPDF_BOOL PutRequestURL(const WideString& wsURL,
185                           const WideString& wsData,
186                           const WideString& wsEncode);
187 
188   void PageEvent(int iPageCount, uint32_t dwEventType) const;
189 #endif  // PDF_ENABLE_XFA
190 #endif  // PDF_ENABLE_V8
191 
192   WideString GetFilePath() const;
GetAppName()193   ByteString GetAppName() const { return ByteString(); }
GetTimerHandler()194   TimerHandlerIface* GetTimerHandler() { return this; }
GetSysHandler()195   IPWL_SystemHandler* GetSysHandler() { return this; }
GetFormFillInfo()196   FPDF_FORMFILLINFO* GetFormFillInfo() const { return m_pInfo; }
197   void SubmitForm(pdfium::span<uint8_t> form_data, const WideString& URL);
198 
199   CPDFSDK_AnnotHandlerMgr* GetAnnotHandlerMgr();  // Always present.
200 
201   // Creates if not present.
202   CFFL_InteractiveFormFiller* GetInteractiveFormFiller();
203   IJS_Runtime* GetIJSRuntime();                   // Creates if not present.
204   CPDFSDK_ActionHandler* GetActionHandler();      // Creates if not present.
205   CPDFSDK_InteractiveForm* GetInteractiveForm();  // Creates if not present.
206 
207  private:
208   IPDF_Page* GetPage(int nIndex);
209 
210   FPDF_FORMFILLINFO* const m_pInfo;
211   std::unique_ptr<CPDFSDK_ActionHandler> m_pActionHandler;
212   std::unique_ptr<IJS_Runtime> m_pIJSRuntime;
213   std::map<IPDF_Page*, std::unique_ptr<CPDFSDK_PageView>> m_PageMap;
214   std::unique_ptr<CPDFSDK_InteractiveForm> m_pInteractiveForm;
215   ObservedPtr<CPDFSDK_Annot> m_pFocusAnnot;
216   UnownedPtr<CPDF_Document> const m_pCPDFDoc;
217   std::unique_ptr<CPDFSDK_AnnotHandlerMgr> m_pAnnotHandlerMgr;
218   std::unique_ptr<CFFL_InteractiveFormFiller> m_pFormFiller;
219   bool m_bChangeMask = false;
220   bool m_bBeingDestroyed = false;
221 };
222 
223 #endif  // FPDFSDK_CPDFSDK_FORMFILLENVIRONMENT_H_
224