diff options
author | Tom Sepez <tsepez@chromium.org> | 2016-02-01 15:38:39 -0800 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2016-02-01 15:38:39 -0800 |
commit | 1654972e0c81f1841653f803903cc48b93f1738e (patch) | |
tree | 4e2cd623b0a1bc50dbc30556edcede984dfb3a56 /fpdfsdk/src/fsdk_mgr.cpp | |
parent | a620372c9e1d0ff825bcbe6620fe7e1b63c769c2 (diff) | |
download | pdfium-1654972e0c81f1841653f803903cc48b93f1738e.tar.xz |
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 .
Diffstat (limited to 'fpdfsdk/src/fsdk_mgr.cpp')
-rw-r--r-- | fpdfsdk/src/fsdk_mgr.cpp | 187 |
1 files changed, 100 insertions, 87 deletions
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 <ctime> #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<FPDF_WIDESTRING>( + 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<char[]> 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<char[]> 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() { |