Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: fpdfsdk/src/fsdk_mgr.cpp

Issue 1658853002: Tidy up CPDF_DocEnvironment::JS_* methods (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Missing ! Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/include/fsdk_mgr.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 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 <algorithm> 7 #include <algorithm>
8 #include <memory> 8 #include <memory>
9 9
10 #include "fpdfsdk/include/fsdk_mgr.h" 10 #include "fpdfsdk/include/fsdk_mgr.h"
11 11
12 #include "fpdfsdk/include/formfiller/FFL_FormFiller.h" 12 #include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
13 #include "fpdfsdk/include/fsdk_define.h" 13 #include "fpdfsdk/include/fsdk_define.h"
14 #include "fpdfsdk/include/javascript/IJavaScript.h" 14 #include "fpdfsdk/include/javascript/IJavaScript.h"
15 #include "public/fpdf_ext.h" 15 #include "public/fpdf_ext.h"
16 #include "third_party/base/stl_util.h" 16 #include "third_party/base/stl_util.h"
17 17
18 #if _FX_OS_ == _FX_ANDROID_ 18 #if _FX_OS_ == _FX_ANDROID_
19 #include "time.h" 19 #include "time.h"
20 #else 20 #else
21 #include <ctime> 21 #include <ctime>
22 #endif 22 #endif
23 23
24 namespace {
25
26 int CharSet2CP(int charset) {
27 if (charset == 128)
28 return 932;
29 if (charset == 134)
30 return 936;
31 if (charset == 129)
32 return 949;
33 if (charset == 136)
34 return 950;
35 return 0;
36 }
37
38 } // namespace
39
40 FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) {
41 return reinterpret_cast<FPDF_WIDESTRING>(
42 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()));
43 }
44
24 class CFX_SystemHandler : public IFX_SystemHandler { 45 class CFX_SystemHandler : public IFX_SystemHandler {
25 public: 46 public:
26 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv) 47 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
27 : m_pEnv(pEnv), m_nCharSet(-1) {} 48 : m_pEnv(pEnv), m_nCharSet(-1) {}
28 ~CFX_SystemHandler() override {} 49 ~CFX_SystemHandler() override {}
29 50
30 public: 51 public:
31 // IFX_SystemHandler 52 // IFX_SystemHandler
32 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override; 53 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
33 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override; 54 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 176 }
156 177
157 for (int i = 0; i < nSize; ++i) { 178 for (int i = 0; i < nSize; ++i) {
158 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName)) 179 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
159 return TRUE; 180 return TRUE;
160 } 181 }
161 182
162 return FALSE; 183 return FALSE;
163 } 184 }
164 185
165 static int CharSet2CP(int charset) {
166 if (charset == 128)
167 return 932;
168 if (charset == 134)
169 return 936;
170 if (charset == 129)
171 return 949;
172 if (charset == 136)
173 return 950;
174 return 0;
175 }
176 CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF( 186 CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
177 CPDF_Document* pDoc, 187 CPDF_Document* pDoc,
178 CFX_ByteString sFontFaceName, 188 CFX_ByteString sFontFaceName,
179 uint8_t nCharset) { 189 uint8_t nCharset) {
180 if (pDoc) { 190 if (pDoc) {
181 CFX_Font* pFXFont = new CFX_Font(); 191 CFX_Font* pFXFont = new CFX_Font();
182 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset), 192 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
183 FALSE); 193 FALSE);
184 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE); 194 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
185 delete pFXFont; 195 delete pFXFont;
(...skipping 21 matching lines...) Expand all
207 m_pSysHandler.reset(new CFX_SystemHandler(this)); 217 m_pSysHandler.reset(new CFX_SystemHandler(this));
208 } 218 }
209 219
210 CPDFDoc_Environment::~CPDFDoc_Environment() { 220 CPDFDoc_Environment::~CPDFDoc_Environment() {
211 } 221 }
212 222
213 int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg, 223 int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
214 const FX_WCHAR* Title, 224 const FX_WCHAR* Title,
215 FX_UINT Type, 225 FX_UINT Type,
216 FX_UINT Icon) { 226 FX_UINT Icon) {
217 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { 227 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
218 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); 228 !m_pInfo->m_pJsPlatform->app_alert) {
219 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); 229 return -1;
220 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
221 FPDF_WIDESTRING pTitle =
222 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
223 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
224 pTitle, Type, Icon);
225 bsMsg.ReleaseBuffer();
226 bsTitle.ReleaseBuffer();
227 return ret;
228 } 230 }
229 return -1; 231 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
232 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
233 return m_pInfo->m_pJsPlatform->app_alert(
234 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
235 AsFPDFWideString(&bsTitle), Type, Icon);
230 } 236 }
231 237
232 int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, 238 int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
233 const FX_WCHAR* Title, 239 const FX_WCHAR* Title,
234 const FX_WCHAR* Default, 240 const FX_WCHAR* Default,
235 const FX_WCHAR* cLabel, 241 const FX_WCHAR* cLabel,
236 FPDF_BOOL bPassword, 242 FPDF_BOOL bPassword,
237 void* response, 243 void* response,
238 int length) { 244 int length) {
239 if (m_pInfo && m_pInfo->m_pJsPlatform && 245 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
240 m_pInfo->m_pJsPlatform->app_response) { 246 !m_pInfo->m_pJsPlatform->app_response) {
241 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); 247 return -1;
242 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
243 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
244 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
245 FPDF_WIDESTRING pQuestion =
246 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
247 FPDF_WIDESTRING pTitle =
248 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
249 FPDF_WIDESTRING pDefault =
250 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
251 FPDF_WIDESTRING pLabel =
252 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
253 int ret = m_pInfo->m_pJsPlatform->app_response(
254 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
255 response, length);
256 bsQuestion.ReleaseBuffer();
257 bsTitle.ReleaseBuffer();
258 bsDefault.ReleaseBuffer();
259 bsLabel.ReleaseBuffer();
260 return ret;
261 } 248 }
262 return -1; 249 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
250 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
251 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
252 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
253 return m_pInfo->m_pJsPlatform->app_response(
254 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
255 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
256 AsFPDFWideString(&bsLabel), bPassword, response, length);
257 }
258
259 void CPDFDoc_Environment::JS_appBeep(int nType) {
260 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
261 !m_pInfo->m_pJsPlatform->app_beep) {
262 return;
263 }
264 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
263 } 265 }
264 266
265 CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { 267 CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
266 if (!m_pInfo || !m_pInfo->m_pJsPlatform || 268 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
267 !m_pInfo->m_pJsPlatform->Field_browse) { 269 !m_pInfo->m_pJsPlatform->Field_browse) {
268 return L""; 270 return CFX_WideString();
269 } 271 }
270
271 const int nRequiredLen = 272 const int nRequiredLen =
272 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); 273 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
273 if (nRequiredLen <= 0) 274 if (nRequiredLen <= 0)
274 return L""; 275 return CFX_WideString();
275 276
276 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); 277 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
277 memset(pBuff.get(), 0, nRequiredLen); 278 memset(pBuff.get(), 0, nRequiredLen);
278 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( 279 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
279 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); 280 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
280 if (nActualLen <= 0 || nActualLen > nRequiredLen) 281 if (nActualLen <= 0 || nActualLen > nRequiredLen)
281 return L""; 282 return CFX_WideString();
282 283
283 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); 284 return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen));
284 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
285 return wsRet;
286 } 285 }
287 286
288 CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { 287 CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
289 if (!m_pInfo || !m_pInfo->m_pJsPlatform || 288 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
290 !m_pInfo->m_pJsPlatform->Doc_getFilePath) { 289 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
291 return L""; 290 return CFX_WideString();
292 } 291 }
293
294 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( 292 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
295 m_pInfo->m_pJsPlatform, nullptr, 0); 293 m_pInfo->m_pJsPlatform, nullptr, 0);
296 if (nRequiredLen <= 0) 294 if (nRequiredLen <= 0)
297 return L""; 295 return CFX_WideString();
298 296
299 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); 297 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
300 memset(pBuff.get(), 0, nRequiredLen); 298 memset(pBuff.get(), 0, nRequiredLen);
301 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( 299 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
302 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); 300 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
303 if (nActualLen <= 0 || nActualLen > nRequiredLen) 301 if (nActualLen <= 0 || nActualLen > nRequiredLen)
304 return L""; 302 return CFX_WideString();
305 303
306 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); 304 return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen));
307 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
308 return wsRet;
309 } 305 }
310 306
311 void CPDFDoc_Environment::JS_docSubmitForm(void* formData, 307 void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
312 int length, 308 int length,
313 const FX_WCHAR* URL) { 309 const FX_WCHAR* URL) {
314 if (m_pInfo && m_pInfo->m_pJsPlatform && 310 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
315 m_pInfo->m_pJsPlatform->Doc_submitForm) { 311 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
316 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); 312 return;
317 FPDF_WIDESTRING pDestination =
318 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
319 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
320 length, pDestination);
321 bsDestination.ReleaseBuffer();
322 } 313 }
314 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
315 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
316 length,
317 AsFPDFWideString(&bsDestination));
323 } 318 }
324 319
325 void CPDFDoc_Environment::JS_docmailForm(void* mailData, 320 void CPDFDoc_Environment::JS_docmailForm(void* mailData,
326 int length, 321 int length,
327 FPDF_BOOL bUI, 322 FPDF_BOOL bUI,
328 const FX_WCHAR* To, 323 const FX_WCHAR* To,
329 const FX_WCHAR* Subject, 324 const FX_WCHAR* Subject,
330 const FX_WCHAR* CC, 325 const FX_WCHAR* CC,
331 const FX_WCHAR* BCC, 326 const FX_WCHAR* BCC,
332 const FX_WCHAR* Msg) { 327 const FX_WCHAR* Msg) {
333 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) { 328 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
334 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); 329 !m_pInfo->m_pJsPlatform->Doc_mail) {
335 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); 330 return;
336 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
337 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
338 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
339 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
340 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
341 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
342 FPDF_WIDESTRING pSubject =
343 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
344 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
345 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
346 bUI, pTo, pSubject, pCC, pBcc, pMsg);
347 bsTo.ReleaseBuffer();
348 bsCC.ReleaseBuffer();
349 bsBcc.ReleaseBuffer();
350 bsSubject.ReleaseBuffer();
351 bsMsg.ReleaseBuffer();
352 } 331 }
332 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
333 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
334 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
335 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
336 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
337 m_pInfo->m_pJsPlatform->Doc_mail(
338 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
339 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
340 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
341 }
342
343 void CPDFDoc_Environment::JS_docprint(FPDF_BOOL bUI,
344 int nStart,
345 int nEnd,
346 FPDF_BOOL bSilent,
347 FPDF_BOOL bShrinkToFit,
348 FPDF_BOOL bPrintAsImage,
349 FPDF_BOOL bReverse,
350 FPDF_BOOL bAnnotations) {
351 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
352 !m_pInfo->m_pJsPlatform->Doc_print) {
353 return;
354 }
355 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
356 bSilent, bShrinkToFit, bPrintAsImage,
357 bReverse, bAnnotations);
358 }
359
360 void CPDFDoc_Environment::JS_docgotoPage(int nPageNum) {
361 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
362 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
363 return;
364 }
365 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
353 } 366 }
354 367
355 IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { 368 IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
356 if (!IsJSInitiated()) 369 if (!IsJSInitiated())
357 return NULL; 370 return NULL;
358 if (!m_pJSRuntime) 371 if (!m_pJSRuntime)
359 m_pJSRuntime.reset(IJS_Runtime::Create(this)); 372 m_pJSRuntime.reset(IJS_Runtime::Create(this));
360 return m_pJSRuntime.get(); 373 return m_pJSRuntime.get();
361 } 374 }
362 375
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot(); 937 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
925 if (!pFocusAnnot) 938 if (!pFocusAnnot)
926 return nullptr; 939 return nullptr;
927 940
928 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { 941 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
929 if (pAnnot == pFocusAnnot) 942 if (pAnnot == pFocusAnnot)
930 return pAnnot; 943 return pAnnot;
931 } 944 }
932 return nullptr; 945 return nullptr;
933 } 946 }
OLDNEW
« no previous file with comments | « fpdfsdk/include/fsdk_mgr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698