• 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 #include <vector>
13 
14 #include "core/fpdfapi/page/cpdf_page.h"
15 #include "core/fpdfapi/parser/cpdf_document.h"
16 #include "core/fpdfdoc/cpdf_occontext.h"
17 #include "core/fxcrt/observable.h"
18 #include "fpdfsdk/cfx_systemhandler.h"
19 #include "fpdfsdk/cpdfsdk_annot.h"
20 #include "fpdfsdk/fsdk_define.h"
21 #include "public/fpdf_formfill.h"
22 #include "public/fpdf_fwlevent.h"
23 
24 class CFFL_InteractiveFormFiller;
25 class CFX_SystemHandler;
26 class CPDFSDK_ActionHandler;
27 class CPDFSDK_AnnotHandlerMgr;
28 class CPDFSDK_InterForm;
29 class CPDFSDK_PageView;
30 class IJS_Runtime;
31 
32 // The CPDFSDK_FormFillEnvironment is "owned" by the embedder across the
33 // C API as a FPDF_FormHandle, and may pop out of existence at any time,
34 // so long as the associated embedder-owned FPDF_Document outlives it.
35 // Pointers from objects in the FPDF_Document ownership hierarchy should
36 // be ObservedPtr<> so as to clear themselves when the embedder "exits"
37 // the form fill environment.  Pointers from objects in this ownership
38 // heirarcy to objects in the FPDF_Document ownership hierarcy should be
39 // UnownedPtr<>, as should pointers from objects in this ownership
40 // hierarcy back to the form fill environment itself, so as to flag any
41 // lingering lifetime issues via the memory tools.
42 
43 class CPDFSDK_FormFillEnvironment
44     : public Observable<CPDFSDK_FormFillEnvironment> {
45  public:
46   CPDFSDK_FormFillEnvironment(UnderlyingDocumentType* pDoc,
47                               FPDF_FORMFILLINFO* pFFinfo);
48   ~CPDFSDK_FormFillEnvironment();
49 
IsSHIFTKeyDown(uint32_t nFlag)50   static bool IsSHIFTKeyDown(uint32_t nFlag) {
51     return !!(nFlag & FWL_EVENTFLAG_ShiftKey);
52   }
IsCTRLKeyDown(uint32_t nFlag)53   static bool IsCTRLKeyDown(uint32_t nFlag) {
54     return !!(nFlag & FWL_EVENTFLAG_ControlKey);
55   }
IsALTKeyDown(uint32_t nFlag)56   static bool IsALTKeyDown(uint32_t nFlag) {
57     return !!(nFlag & FWL_EVENTFLAG_AltKey);
58   }
59 
60   CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage, bool renew);
61   CPDFSDK_PageView* GetPageView(int nIndex);
62   CPDFSDK_PageView* GetCurrentView();
63   void RemovePageView(UnderlyingPageType* pPage);
64   void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
65 
GetFocusAnnot()66   CPDFSDK_Annot* GetFocusAnnot() { return m_pFocusAnnot.Get(); }
67   bool SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot);
68   bool KillFocusAnnot(uint32_t nFlag);
69   void ClearAllFocusedAnnots();
70 
71   bool ExtractPages(const std::vector<uint16_t>& arrExtraPages,
72                     CPDF_Document* pDstDoc);
73   bool InsertPages(int nInsertAt,
74                    const CPDF_Document* pSrcDoc,
75                    const std::vector<uint16_t>& arrSrcPages);
76   bool ReplacePages(int nPage,
77                     const CPDF_Document* pSrcDoc,
78                     const std::vector<uint16_t>& arrSrcPages);
79 
GetPageCount()80   int GetPageCount() const { return m_pUnderlyingDoc->GetPageCount(); }
81   bool GetPermissions(int nFlag) const;
82 
GetChangeMark()83   bool GetChangeMark() const { return m_bChangeMask; }
SetChangeMark()84   void SetChangeMark() { m_bChangeMask = true; }
ClearChangeMark()85   void ClearChangeMark() { m_bChangeMask = false; }
86 
87   void ProcJavascriptFun();
88   bool ProcOpenAction();
89 
90   void Invalidate(UnderlyingPageType* page, const FX_RECT& rect);
91   void OutputSelectedRect(UnderlyingPageType* page, const CFX_FloatRect& rect);
92 
93   void SetCursor(int nCursorType);
94   int SetTimer(int uElapse, TimerCallback lpTimerFunc);
95   void KillTimer(int nTimerID);
96   FX_SYSTEMTIME GetLocalTime() const;
97 
98   void OnChange();
99 
100   FPDF_PAGE GetCurrentPage(UnderlyingDocumentType* document);
101 
102   void ExecuteNamedAction(const char* namedAction);
103   void OnSetFieldInputFocus(FPDF_WIDESTRING focusText,
104                             FPDF_DWORD nTextLen,
105                             bool bFocus);
106   void DoURIAction(const char* bsURI);
107   void DoGoToAction(int nPageIndex,
108                     int zoomMode,
109                     float* fPosArray,
110                     int sizeOfArray);
111 
GetUnderlyingDocument()112   UnderlyingDocumentType* GetUnderlyingDocument() const {
113     return m_pUnderlyingDoc.Get();
114   }
115 
116 #ifdef PDF_ENABLE_XFA
GetPDFDocument()117   CPDF_Document* GetPDFDocument() const {
118     return m_pUnderlyingDoc ? m_pUnderlyingDoc->GetPDFDoc() : nullptr;
119   }
120 
GetXFAContext()121   CPDFXFA_Context* GetXFAContext() const { return m_pUnderlyingDoc.Get(); }
ResetXFADocument()122   void ResetXFADocument() { m_pUnderlyingDoc = nullptr; }
123 
GetPageViewCount()124   int GetPageViewCount() const { return m_PageMap.size(); }
125 
126   void DisplayCaret(CPDFXFA_Page* page,
127                     FPDF_BOOL bVisible,
128                     double left,
129                     double top,
130                     double right,
131                     double bottom);
132   int GetCurrentPageIndex(CPDFXFA_Context* document);
133   void SetCurrentPage(CPDFXFA_Context* document, int iCurPage);
134 
135   // TODO(dsinclair): This should probably change to PDFium?
FFI_GetAppName()136   WideString FFI_GetAppName() const { return WideString(L"Acrobat"); }
137 
138   WideString GetPlatform();
139   void GotoURL(CPDFXFA_Context* document, const WideStringView& wsURL);
140   void GetPageViewRect(CPDFXFA_Page* page, FS_RECTF& dstRect);
141   bool PopupMenu(CPDFXFA_Page* page,
142                  FPDF_WIDGET hWidget,
143                  int menuFlag,
144                  CFX_PointF pt);
145 
146   void Alert(FPDF_WIDESTRING Msg, FPDF_WIDESTRING Title, int Type, int Icon);
147   void EmailTo(FPDF_FILEHANDLER* fileHandler,
148                FPDF_WIDESTRING pTo,
149                FPDF_WIDESTRING pSubject,
150                FPDF_WIDESTRING pCC,
151                FPDF_WIDESTRING pBcc,
152                FPDF_WIDESTRING pMsg);
153   void UploadTo(FPDF_FILEHANDLER* fileHandler,
154                 int fileFlag,
155                 FPDF_WIDESTRING uploadTo);
156   FPDF_FILEHANDLER* OpenFile(int fileType,
157                              FPDF_WIDESTRING wsURL,
158                              const char* mode);
159   RetainPtr<IFX_SeekableReadStream> DownloadFromURL(const wchar_t* url);
160   WideString PostRequestURL(const wchar_t* wsURL,
161                             const wchar_t* wsData,
162                             const wchar_t* wsContentType,
163                             const wchar_t* wsEncode,
164                             const wchar_t* wsHeader);
165   FPDF_BOOL PutRequestURL(const wchar_t* wsURL,
166                           const wchar_t* wsData,
167                           const wchar_t* wsEncode);
168   WideString GetLanguage();
169 
170   void PageEvent(int iPageCount, uint32_t dwEventType) const;
171 #else   // PDF_ENABLE_XFA
GetPDFDocument()172   CPDF_Document* GetPDFDocument() const { return m_pUnderlyingDoc.Get(); }
173 #endif  // PDF_ENABLE_XFA
174 
175   int JS_appAlert(const wchar_t* Msg,
176                   const wchar_t* Title,
177                   uint32_t Type,
178                   uint32_t Icon);
179   int JS_appResponse(const wchar_t* Question,
180                      const wchar_t* Title,
181                      const wchar_t* Default,
182                      const wchar_t* cLabel,
183                      FPDF_BOOL bPassword,
184                      void* response,
185                      int length);
186   void JS_appBeep(int nType);
187   WideString JS_fieldBrowse();
188   WideString JS_docGetFilePath();
189   void JS_docSubmitForm(void* formData, int length, const wchar_t* URL);
190   void JS_docmailForm(void* mailData,
191                       int length,
192                       FPDF_BOOL bUI,
193                       const wchar_t* To,
194                       const wchar_t* Subject,
195                       const wchar_t* CC,
196                       const wchar_t* BCC,
197                       const wchar_t* Msg);
198   void JS_docprint(FPDF_BOOL bUI,
199                    int nStart,
200                    int nEnd,
201                    FPDF_BOOL bSilent,
202                    FPDF_BOOL bShrinkToFit,
203                    FPDF_BOOL bPrintAsImage,
204                    FPDF_BOOL bReverse,
205                    FPDF_BOOL bAnnotations);
206   void JS_docgotoPage(int nPageNum);
207 
IsJSInitiated()208   bool IsJSInitiated() const { return m_pInfo && m_pInfo->m_pJsPlatform; }
GetAppName()209   ByteString GetAppName() const { return ""; }
GetSysHandler()210   CFX_SystemHandler* GetSysHandler() const { return m_pSysHandler.get(); }
GetFormFillInfo()211   FPDF_FORMFILLINFO* GetFormFillInfo() const { return m_pInfo; }
212 
213   // Creates if not present.
214   CFFL_InteractiveFormFiller* GetInteractiveFormFiller();
215   CPDFSDK_AnnotHandlerMgr* GetAnnotHandlerMgr();  // Creates if not present.
216   IJS_Runtime* GetJSRuntime();                    // Creates if not present.
217   CPDFSDK_ActionHandler* GetActionHandler();      // Creates if not present.
218   CPDFSDK_InterForm* GetInterForm();              // Creates if not present.
219 
220  private:
221   UnderlyingPageType* GetPage(int nIndex);
222 
223   FPDF_FORMFILLINFO* const m_pInfo;
224   std::unique_ptr<CPDFSDK_AnnotHandlerMgr> m_pAnnotHandlerMgr;
225   std::unique_ptr<CPDFSDK_ActionHandler> m_pActionHandler;
226   std::unique_ptr<IJS_Runtime> m_pJSRuntime;
227   std::map<UnderlyingPageType*, std::unique_ptr<CPDFSDK_PageView>> m_PageMap;
228   std::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
229   CPDFSDK_Annot::ObservedPtr m_pFocusAnnot;
230   UnownedPtr<UnderlyingDocumentType> m_pUnderlyingDoc;
231   std::unique_ptr<CFFL_InteractiveFormFiller> m_pFormFiller;
232   std::unique_ptr<CFX_SystemHandler> m_pSysHandler;
233   bool m_bChangeMask;
234   bool m_bBeingDestroyed;
235 };
236 
237 #endif  // FPDFSDK_CPDFSDK_FORMFILLENVIRONMENT_H_
238