From 1654972e0c81f1841653f803903cc48b93f1738e Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 1 Feb 2016 15:38:39 -0800 Subject: Tidy up CPDF_DocEnvironment::JS_* methods Remove code from header. Put related declarations together. Add convenience function for widestrings. Avoid pointless releases. Use early returns in all cases for consistency. R=thestig@chromium.org Review URL: https://codereview.chromium.org/1658853002 . --- fpdfsdk/include/fsdk_mgr.h | 87 +++++++++------------ fpdfsdk/src/fsdk_mgr.cpp | 187 ++++++++++++++++++++++++--------------------- 2 files changed, 137 insertions(+), 137 deletions(-) diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h index 323b07a46d..130abc8436 100644 --- a/fpdfsdk/include/fsdk_mgr.h +++ b/fpdfsdk/include/fsdk_mgr.h @@ -31,6 +31,10 @@ class CPDFSDK_PageView; class CPDFSDK_Widget; class IFX_SystemHandler; +// NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken +// since modifying the result would impact |bsUTF16LE|. +FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE); + class CPDFDoc_Environment final { public: CPDFDoc_Environment(UnderlyingDocumentType* pDoc, FPDF_FORMFILLINFO* pFFinfo); @@ -105,56 +109,6 @@ class CPDFDoc_Environment final { FX_BOOL FFI_IsINSERTKeyDown(FX_DWORD nFlag) const { return FALSE; } - int JS_appAlert(const FX_WCHAR* Msg, - const FX_WCHAR* Title, - FX_UINT Type, - FX_UINT Icon); - int JS_appResponse(const FX_WCHAR* Question, - const FX_WCHAR* Title, - const FX_WCHAR* Default, - const FX_WCHAR* cLabel, - FPDF_BOOL bPassword, - void* response, - int length); - - void JS_appBeep(int nType) { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_beep) - m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); - } - - CFX_WideString JS_fieldBrowse(); - CFX_WideString JS_docGetFilePath(); - - void JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL); - void JS_docmailForm(void* mailData, - int length, - FPDF_BOOL bUI, - const FX_WCHAR* To, - const FX_WCHAR* Subject, - const FX_WCHAR* CC, - const FX_WCHAR* BCC, - const FX_WCHAR* Msg); - - void JS_docprint(FPDF_BOOL bUI, - int nStart, - int nEnd, - FPDF_BOOL bSilent, - FPDF_BOOL bShrinkToFit, - FPDF_BOOL bPrintAsImage, - FPDF_BOOL bReverse, - FPDF_BOOL bAnnotations) { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_print) - m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, - nEnd, bSilent, bShrinkToFit, - bPrintAsImage, bReverse, bAnnotations); - } - - void JS_docgotoPage(int nPageNum) { - if (m_pInfo && m_pInfo->m_pJsPlatform && - m_pInfo->m_pJsPlatform->Doc_gotoPage) - m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); - } - FPDF_PAGE FFI_GetPage(FPDF_DOCUMENT document, int nPageIndex) { if (m_pInfo && m_pInfo->FFI_GetPage) return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex); @@ -200,6 +154,39 @@ class CPDFDoc_Environment final { sizeOfArray); } + int JS_appAlert(const FX_WCHAR* Msg, + const FX_WCHAR* Title, + FX_UINT Type, + FX_UINT Icon); + int JS_appResponse(const FX_WCHAR* Question, + const FX_WCHAR* Title, + const FX_WCHAR* Default, + const FX_WCHAR* cLabel, + FPDF_BOOL bPassword, + void* response, + int length); + void JS_appBeep(int nType); + CFX_WideString JS_fieldBrowse(); + CFX_WideString JS_docGetFilePath(); + void JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL); + void JS_docmailForm(void* mailData, + int length, + FPDF_BOOL bUI, + const FX_WCHAR* To, + const FX_WCHAR* Subject, + const FX_WCHAR* CC, + const FX_WCHAR* BCC, + const FX_WCHAR* Msg); + void JS_docprint(FPDF_BOOL bUI, + int nStart, + int nEnd, + FPDF_BOOL bSilent, + FPDF_BOOL bShrinkToFit, + FPDF_BOOL bPrintAsImage, + FPDF_BOOL bReverse, + FPDF_BOOL bAnnotations); + void JS_docgotoPage(int nPageNum); + FX_BOOL IsJSInitiated() const { return m_pInfo && m_pInfo->m_pJsPlatform; } void SetSDKDocument(CPDFSDK_Document* pFXDoc) { m_pSDKDoc = pFXDoc; } CPDFSDK_Document* GetSDKDocument() const { return m_pSDKDoc; } diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp index 0a9fcd34f2..980ffb4534 100644 --- a/fpdfsdk/src/fsdk_mgr.cpp +++ b/fpdfsdk/src/fsdk_mgr.cpp @@ -21,6 +21,27 @@ #include #endif +namespace { + +int CharSet2CP(int charset) { + if (charset == 128) + return 932; + if (charset == 134) + return 936; + if (charset == 129) + return 949; + if (charset == 136) + return 950; + return 0; +} + +} // namespace + +FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { + return reinterpret_cast( + bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); +} + class CFX_SystemHandler : public IFX_SystemHandler { public: explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv) @@ -162,17 +183,6 @@ FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont( return FALSE; } -static int CharSet2CP(int charset) { - if (charset == 128) - return 932; - if (charset == 134) - return 936; - if (charset == 129) - return 949; - if (charset == 136) - return 950; - return 0; -} CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF( CPDF_Document* pDoc, CFX_ByteString sFontFaceName, @@ -214,19 +224,15 @@ int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg, const FX_WCHAR* Title, FX_UINT Type, FX_UINT Icon) { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { - CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); - CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); - FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength()); - FPDF_WIDESTRING pTitle = - (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength()); - int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg, - pTitle, Type, Icon); - bsMsg.ReleaseBuffer(); - bsTitle.ReleaseBuffer(); - return ret; + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->app_alert) { + return -1; } - return -1; + CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); + CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); + return m_pInfo->m_pJsPlatform->app_alert( + m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), + AsFPDFWideString(&bsTitle), Type, Icon); } int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, @@ -236,90 +242,79 @@ int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, FPDF_BOOL bPassword, void* response, int length) { - if (m_pInfo && m_pInfo->m_pJsPlatform && - m_pInfo->m_pJsPlatform->app_response) { - CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); - CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); - CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); - CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); - FPDF_WIDESTRING pQuestion = - (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength()); - FPDF_WIDESTRING pTitle = - (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength()); - FPDF_WIDESTRING pDefault = - (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength()); - FPDF_WIDESTRING pLabel = - (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength()); - int ret = m_pInfo->m_pJsPlatform->app_response( - m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword, - response, length); - bsQuestion.ReleaseBuffer(); - bsTitle.ReleaseBuffer(); - bsDefault.ReleaseBuffer(); - bsLabel.ReleaseBuffer(); - return ret; + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->app_response) { + return -1; } - return -1; + CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); + CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); + CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); + CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); + return m_pInfo->m_pJsPlatform->app_response( + m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), + AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), + AsFPDFWideString(&bsLabel), bPassword, response, length); +} + +void CPDFDoc_Environment::JS_appBeep(int nType) { + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->app_beep) { + return; + } + m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); } CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { if (!m_pInfo || !m_pInfo->m_pJsPlatform || !m_pInfo->m_pJsPlatform->Field_browse) { - return L""; + return CFX_WideString(); } - const int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); if (nRequiredLen <= 0) - return L""; + return CFX_WideString(); std::unique_ptr pBuff(new char[nRequiredLen]); memset(pBuff.get(), 0, nRequiredLen); const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); if (nActualLen <= 0 || nActualLen > nRequiredLen) - return L""; + return CFX_WideString(); - CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); - CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); - return wsRet; + return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen)); } CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { if (!m_pInfo || !m_pInfo->m_pJsPlatform || !m_pInfo->m_pJsPlatform->Doc_getFilePath) { - return L""; + return CFX_WideString(); } - const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( m_pInfo->m_pJsPlatform, nullptr, 0); if (nRequiredLen <= 0) - return L""; + return CFX_WideString(); std::unique_ptr pBuff(new char[nRequiredLen]); memset(pBuff.get(), 0, nRequiredLen); const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); if (nActualLen <= 0 || nActualLen > nRequiredLen) - return L""; + return CFX_WideString(); - CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); - CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); - return wsRet; + return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen)); } void CPDFDoc_Environment::JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL) { - if (m_pInfo && m_pInfo->m_pJsPlatform && - m_pInfo->m_pJsPlatform->Doc_submitForm) { - CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); - FPDF_WIDESTRING pDestination = - (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength()); - m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, - length, pDestination); - bsDestination.ReleaseBuffer(); + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Doc_submitForm) { + return; } + CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); + m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, + length, + AsFPDFWideString(&bsDestination)); } void CPDFDoc_Environment::JS_docmailForm(void* mailData, @@ -330,26 +325,44 @@ void CPDFDoc_Environment::JS_docmailForm(void* mailData, const FX_WCHAR* CC, const FX_WCHAR* BCC, const FX_WCHAR* Msg) { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) { - CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); - CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); - CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); - CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); - CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); - FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength()); - FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength()); - FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength()); - FPDF_WIDESTRING pSubject = - (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength()); - FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength()); - m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length, - bUI, pTo, pSubject, pCC, pBcc, pMsg); - bsTo.ReleaseBuffer(); - bsCC.ReleaseBuffer(); - bsBcc.ReleaseBuffer(); - bsSubject.ReleaseBuffer(); - bsMsg.ReleaseBuffer(); + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Doc_mail) { + return; + } + CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); + CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); + CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); + CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); + CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); + m_pInfo->m_pJsPlatform->Doc_mail( + m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), + AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), + AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); +} + +void CPDFDoc_Environment::JS_docprint(FPDF_BOOL bUI, + int nStart, + int nEnd, + FPDF_BOOL bSilent, + FPDF_BOOL bShrinkToFit, + FPDF_BOOL bPrintAsImage, + FPDF_BOOL bReverse, + FPDF_BOOL bAnnotations) { + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Doc_print) { + return; + } + m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, + bSilent, bShrinkToFit, bPrintAsImage, + bReverse, bAnnotations); +} + +void CPDFDoc_Environment::JS_docgotoPage(int nPageNum) { + if (!m_pInfo || !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Doc_gotoPage) { + return; } + m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); } IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { -- cgit v1.2.3