OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fpdfsdk/include/cpdfdoc_environment.h" | 7 #include "fpdfsdk/include/cpdfsdk_environment.h" |
8 | 8 |
9 #include "fpdfsdk/formfiller/cffl_iformfiller.h" | 9 #include "fpdfsdk/formfiller/cffl_iformfiller.h" |
10 #include "fpdfsdk/include/cpdfsdk_annothandlermgr.h" | 10 #include "fpdfsdk/include/cpdfsdk_annothandlermgr.h" |
11 #include "fpdfsdk/include/fsdk_actionhandler.h" | 11 #include "fpdfsdk/include/fsdk_actionhandler.h" |
12 #include "fpdfsdk/javascript/ijs_runtime.h" | 12 #include "fpdfsdk/javascript/ijs_runtime.h" |
13 | 13 |
14 #ifdef PDF_ENABLE_XFA | 14 #ifdef PDF_ENABLE_XFA |
15 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" | 15 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" |
16 #endif // PDF_ENABLE_XFA | 16 #endif // PDF_ENABLE_XFA |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken | 20 // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken |
21 // since modifying the result would impact |bsUTF16LE|. | 21 // since modifying the result would impact |bsUTF16LE|. |
22 FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { | 22 FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { |
23 return reinterpret_cast<FPDF_WIDESTRING>( | 23 return reinterpret_cast<FPDF_WIDESTRING>( |
24 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); | 24 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); |
25 } | 25 } |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc, | 29 CPDFSDK_Environment::CPDFSDK_Environment(UnderlyingDocumentType* pDoc, |
30 FPDF_FORMFILLINFO* pFFinfo) | 30 FPDF_FORMFILLINFO* pFFinfo) |
31 : m_pInfo(pFFinfo), m_pSDKDoc(nullptr), m_pUnderlyingDoc(pDoc) { | 31 : m_pInfo(pFFinfo), m_pSDKDoc(nullptr), m_pUnderlyingDoc(pDoc) { |
32 m_pSysHandler.reset(new CFX_SystemHandler(this)); | 32 m_pSysHandler.reset(new CFX_SystemHandler(this)); |
33 } | 33 } |
34 | 34 |
35 CPDFDoc_Environment::~CPDFDoc_Environment() { | 35 CPDFSDK_Environment::~CPDFSDK_Environment() { |
36 #ifdef PDF_ENABLE_XFA | 36 #ifdef PDF_ENABLE_XFA |
37 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); | 37 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); |
38 if (pProvider->m_pEnvList.GetSize() == 0) | 38 if (pProvider->m_pEnvList.GetSize() == 0) |
39 pProvider->SetJavaScriptInitialized(FALSE); | 39 pProvider->SetJavaScriptInitialized(FALSE); |
40 #endif // PDF_ENABLE_XFA | 40 #endif // PDF_ENABLE_XFA |
41 if (m_pInfo && m_pInfo->Release) | 41 if (m_pInfo && m_pInfo->Release) |
42 m_pInfo->Release(m_pInfo); | 42 m_pInfo->Release(m_pInfo); |
43 } | 43 } |
44 | 44 |
45 int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg, | 45 int CPDFSDK_Environment::JS_appAlert(const FX_WCHAR* Msg, |
46 const FX_WCHAR* Title, | 46 const FX_WCHAR* Title, |
47 FX_UINT Type, | 47 FX_UINT Type, |
48 FX_UINT Icon) { | 48 FX_UINT Icon) { |
49 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 49 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
50 !m_pInfo->m_pJsPlatform->app_alert) { | 50 !m_pInfo->m_pJsPlatform->app_alert) { |
51 return -1; | 51 return -1; |
52 } | 52 } |
53 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); | 53 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
54 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); | 54 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
55 return m_pInfo->m_pJsPlatform->app_alert( | 55 return m_pInfo->m_pJsPlatform->app_alert( |
56 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), | 56 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
57 AsFPDFWideString(&bsTitle), Type, Icon); | 57 AsFPDFWideString(&bsTitle), Type, Icon); |
58 } | 58 } |
59 | 59 |
60 int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, | 60 int CPDFSDK_Environment::JS_appResponse(const FX_WCHAR* Question, |
61 const FX_WCHAR* Title, | 61 const FX_WCHAR* Title, |
62 const FX_WCHAR* Default, | 62 const FX_WCHAR* Default, |
63 const FX_WCHAR* cLabel, | 63 const FX_WCHAR* cLabel, |
64 FPDF_BOOL bPassword, | 64 FPDF_BOOL bPassword, |
65 void* response, | 65 void* response, |
66 int length) { | 66 int length) { |
67 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 67 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
68 !m_pInfo->m_pJsPlatform->app_response) { | 68 !m_pInfo->m_pJsPlatform->app_response) { |
69 return -1; | 69 return -1; |
70 } | 70 } |
71 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); | 71 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); |
72 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); | 72 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
73 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); | 73 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); |
74 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); | 74 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); |
75 return m_pInfo->m_pJsPlatform->app_response( | 75 return m_pInfo->m_pJsPlatform->app_response( |
76 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), | 76 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), |
77 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), | 77 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), |
78 AsFPDFWideString(&bsLabel), bPassword, response, length); | 78 AsFPDFWideString(&bsLabel), bPassword, response, length); |
79 } | 79 } |
80 | 80 |
81 void CPDFDoc_Environment::JS_appBeep(int nType) { | 81 void CPDFSDK_Environment::JS_appBeep(int nType) { |
82 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 82 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
83 !m_pInfo->m_pJsPlatform->app_beep) { | 83 !m_pInfo->m_pJsPlatform->app_beep) { |
84 return; | 84 return; |
85 } | 85 } |
86 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); | 86 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); |
87 } | 87 } |
88 | 88 |
89 CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { | 89 CFX_WideString CPDFSDK_Environment::JS_fieldBrowse() { |
90 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 90 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
91 !m_pInfo->m_pJsPlatform->Field_browse) { | 91 !m_pInfo->m_pJsPlatform->Field_browse) { |
92 return CFX_WideString(); | 92 return CFX_WideString(); |
93 } | 93 } |
94 const int nRequiredLen = | 94 const int nRequiredLen = |
95 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); | 95 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
96 if (nRequiredLen <= 0) | 96 if (nRequiredLen <= 0) |
97 return CFX_WideString(); | 97 return CFX_WideString(); |
98 | 98 |
99 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); | 99 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
100 memset(pBuff.get(), 0, nRequiredLen); | 100 memset(pBuff.get(), 0, nRequiredLen); |
101 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( | 101 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
102 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); | 102 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
103 if (nActualLen <= 0 || nActualLen > nRequiredLen) | 103 if (nActualLen <= 0 || nActualLen > nRequiredLen) |
104 return CFX_WideString(); | 104 return CFX_WideString(); |
105 | 105 |
106 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); | 106 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
107 } | 107 } |
108 | 108 |
109 CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { | 109 CFX_WideString CPDFSDK_Environment::JS_docGetFilePath() { |
110 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 110 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
111 !m_pInfo->m_pJsPlatform->Doc_getFilePath) { | 111 !m_pInfo->m_pJsPlatform->Doc_getFilePath) { |
112 return CFX_WideString(); | 112 return CFX_WideString(); |
113 } | 113 } |
114 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( | 114 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
115 m_pInfo->m_pJsPlatform, nullptr, 0); | 115 m_pInfo->m_pJsPlatform, nullptr, 0); |
116 if (nRequiredLen <= 0) | 116 if (nRequiredLen <= 0) |
117 return CFX_WideString(); | 117 return CFX_WideString(); |
118 | 118 |
119 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); | 119 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
120 memset(pBuff.get(), 0, nRequiredLen); | 120 memset(pBuff.get(), 0, nRequiredLen); |
121 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( | 121 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
122 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); | 122 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
123 if (nActualLen <= 0 || nActualLen > nRequiredLen) | 123 if (nActualLen <= 0 || nActualLen > nRequiredLen) |
124 return CFX_WideString(); | 124 return CFX_WideString(); |
125 | 125 |
126 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); | 126 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
127 } | 127 } |
128 | 128 |
129 void CPDFDoc_Environment::JS_docSubmitForm(void* formData, | 129 void CPDFSDK_Environment::JS_docSubmitForm(void* formData, |
130 int length, | 130 int length, |
131 const FX_WCHAR* URL) { | 131 const FX_WCHAR* URL) { |
132 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 132 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
133 !m_pInfo->m_pJsPlatform->Doc_submitForm) { | 133 !m_pInfo->m_pJsPlatform->Doc_submitForm) { |
134 return; | 134 return; |
135 } | 135 } |
136 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); | 136 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); |
137 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, | 137 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, |
138 length, | 138 length, |
139 AsFPDFWideString(&bsDestination)); | 139 AsFPDFWideString(&bsDestination)); |
140 } | 140 } |
141 | 141 |
142 void CPDFDoc_Environment::JS_docmailForm(void* mailData, | 142 void CPDFSDK_Environment::JS_docmailForm(void* mailData, |
143 int length, | 143 int length, |
144 FPDF_BOOL bUI, | 144 FPDF_BOOL bUI, |
145 const FX_WCHAR* To, | 145 const FX_WCHAR* To, |
146 const FX_WCHAR* Subject, | 146 const FX_WCHAR* Subject, |
147 const FX_WCHAR* CC, | 147 const FX_WCHAR* CC, |
148 const FX_WCHAR* BCC, | 148 const FX_WCHAR* BCC, |
149 const FX_WCHAR* Msg) { | 149 const FX_WCHAR* Msg) { |
150 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 150 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
151 !m_pInfo->m_pJsPlatform->Doc_mail) { | 151 !m_pInfo->m_pJsPlatform->Doc_mail) { |
152 return; | 152 return; |
153 } | 153 } |
154 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); | 154 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); |
155 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); | 155 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); |
156 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); | 156 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); |
157 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); | 157 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); |
158 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); | 158 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
159 m_pInfo->m_pJsPlatform->Doc_mail( | 159 m_pInfo->m_pJsPlatform->Doc_mail( |
160 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), | 160 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), |
161 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), | 161 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), |
162 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); | 162 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); |
163 } | 163 } |
164 | 164 |
165 void CPDFDoc_Environment::JS_docprint(FPDF_BOOL bUI, | 165 void CPDFSDK_Environment::JS_docprint(FPDF_BOOL bUI, |
166 int nStart, | 166 int nStart, |
167 int nEnd, | 167 int nEnd, |
168 FPDF_BOOL bSilent, | 168 FPDF_BOOL bSilent, |
169 FPDF_BOOL bShrinkToFit, | 169 FPDF_BOOL bShrinkToFit, |
170 FPDF_BOOL bPrintAsImage, | 170 FPDF_BOOL bPrintAsImage, |
171 FPDF_BOOL bReverse, | 171 FPDF_BOOL bReverse, |
172 FPDF_BOOL bAnnotations) { | 172 FPDF_BOOL bAnnotations) { |
173 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 173 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
174 !m_pInfo->m_pJsPlatform->Doc_print) { | 174 !m_pInfo->m_pJsPlatform->Doc_print) { |
175 return; | 175 return; |
176 } | 176 } |
177 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, | 177 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, |
178 bSilent, bShrinkToFit, bPrintAsImage, | 178 bSilent, bShrinkToFit, bPrintAsImage, |
179 bReverse, bAnnotations); | 179 bReverse, bAnnotations); |
180 } | 180 } |
181 | 181 |
182 void CPDFDoc_Environment::JS_docgotoPage(int nPageNum) { | 182 void CPDFSDK_Environment::JS_docgotoPage(int nPageNum) { |
183 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | 183 if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
184 !m_pInfo->m_pJsPlatform->Doc_gotoPage) { | 184 !m_pInfo->m_pJsPlatform->Doc_gotoPage) { |
185 return; | 185 return; |
186 } | 186 } |
187 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); | 187 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); |
188 } | 188 } |
189 | 189 |
190 IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { | 190 IJS_Runtime* CPDFSDK_Environment::GetJSRuntime() { |
191 if (!IsJSInitiated()) | 191 if (!IsJSInitiated()) |
192 return nullptr; | 192 return nullptr; |
193 if (!m_pJSRuntime) | 193 if (!m_pJSRuntime) |
194 m_pJSRuntime.reset(IJS_Runtime::Create(this)); | 194 m_pJSRuntime.reset(IJS_Runtime::Create(this)); |
195 return m_pJSRuntime.get(); | 195 return m_pJSRuntime.get(); |
196 } | 196 } |
197 | 197 |
198 CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() { | 198 CPDFSDK_AnnotHandlerMgr* CPDFSDK_Environment::GetAnnotHandlerMgr() { |
199 if (!m_pAnnotHandlerMgr) | 199 if (!m_pAnnotHandlerMgr) |
200 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this)); | 200 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this)); |
201 return m_pAnnotHandlerMgr.get(); | 201 return m_pAnnotHandlerMgr.get(); |
202 } | 202 } |
203 | 203 |
204 CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() { | 204 CPDFSDK_ActionHandler* CPDFSDK_Environment::GetActionHander() { |
205 if (!m_pActionHandler) | 205 if (!m_pActionHandler) |
206 m_pActionHandler.reset(new CPDFSDK_ActionHandler()); | 206 m_pActionHandler.reset(new CPDFSDK_ActionHandler()); |
207 return m_pActionHandler.get(); | 207 return m_pActionHandler.get(); |
208 } | 208 } |
209 | 209 |
210 CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() { | 210 CFFL_IFormFiller* CPDFSDK_Environment::GetIFormFiller() { |
211 if (!m_pIFormFiller) | 211 if (!m_pIFormFiller) |
212 m_pIFormFiller.reset(new CFFL_IFormFiller(this)); | 212 m_pIFormFiller.reset(new CFFL_IFormFiller(this)); |
213 return m_pIFormFiller.get(); | 213 return m_pIFormFiller.get(); |
214 } | 214 } |
OLD | NEW |