From 5ab3fc60fc44aca144607416d18e7efdc1203fe6 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 22 Jun 2015 15:13:36 -0700 Subject: Cleanup: Remove more checks for new returning NULL. R=jam@chromium.org Review URL: https://codereview.chromium.org/1198903002. --- fpdfsdk/include/fsdk_mgr.h | 54 ++---------------------------- fpdfsdk/src/fpdfformfill.cpp | 74 +++++++++++++++++------------------------- fpdfsdk/src/fsdk_mgr.cpp | 50 ++++++++++++++++++++++++++++ fpdfsdk/src/javascript/app.cpp | 42 ++++++++++++------------ 4 files changed, 103 insertions(+), 117 deletions(-) diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h index 4f2a87d974..167d0d6fcd 100644 --- a/fpdfsdk/include/fsdk_mgr.h +++ b/fpdfsdk/include/fsdk_mgr.h @@ -170,59 +170,9 @@ public: } } - CFX_WideString JS_fieldBrowse() - { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Field_browse) - { - int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, NULL, 0); - if (nRequiredLen <= 0) - return L""; - - char* pbuff = new char[nRequiredLen]; - if (!pbuff) - return L""; - - memset(pbuff, 0, nRequiredLen); - int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen); - if (nActualLen <= 0 || nActualLen > nRequiredLen) - { - delete[] pbuff; - return L""; - } - CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); - CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); - delete[] pbuff; - return wsRet; - } - return L""; - } + CFX_WideString JS_fieldBrowse(); - CFX_WideString JS_docGetFilePath() - { - if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_getFilePath) - { - int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, NULL, 0); - if (nRequiredLen <= 0) - return L""; - - char* pbuff = new char[nRequiredLen]; - if (!pbuff) - return L""; - - memset(pbuff, 0, nRequiredLen); - int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen); - if (nActualLen <= 0 || nActualLen > nRequiredLen) - { - delete[] pbuff; - return L""; - } - CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); - CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); - delete[] pbuff; - return wsRet; - } - return L""; - } + CFX_WideString JS_docGetFilePath(); void JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL) { diff --git a/fpdfsdk/src/fpdfformfill.cpp b/fpdfsdk/src/fpdfformfill.cpp index 9358c9e194..021a78aace 100644 --- a/fpdfsdk/src/fpdfformfill.cpp +++ b/fpdfsdk/src/fpdfformfill.cpp @@ -6,59 +6,43 @@ #include "../../public/fpdf_formfill.h" #include "../../public/fpdfview.h" +#include "../../third_party/base/nonstd_unique_ptr.h" #include "../include/fsdk_define.h" #include "../include/fsdk_mgr.h" - - #include "../include/javascript/IJavaScript.h" - -DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, FPDF_PAGE page,double page_x, double page_y) +DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint( + FPDF_FORMHANDLE hHandle, FPDF_PAGE page, double page_x, double page_y) { - if(!page || !hHandle) - return -1; - CPDF_Page * pPage = (CPDF_Page*) page; - - CPDF_InterForm * pInterForm = NULL; - pInterForm = new CPDF_InterForm(pPage->m_pDocument,FALSE); - if (!pInterForm) - return -1; - CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y); - if(!pFormCtrl) - { - delete pInterForm; - return -1; - } - CPDF_FormField* pFormField = pFormCtrl->GetField(); - if(!pFormField) - { - delete pInterForm; - return -1; - } - - int nType = pFormField->GetFieldType(); - delete pInterForm; - return nType; + if (!page || !hHandle) + return -1; + CPDF_Page* pPage = (CPDF_Page*) page; + + nonstd::unique_ptr pInterForm( + new CPDF_InterForm(pPage->m_pDocument, FALSE)); + CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint( + pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y); + if (!pFormCtrl) + return -1; + + CPDF_FormField* pFormField = pFormCtrl->GetField(); + if(!pFormField) + return -1; + + return pFormField->GetFieldType(); } -DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo) +DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment( + FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo) { - if(!document || !formInfo || formInfo->version!=1) - return NULL; - CPDF_Document * pDocument = (CPDF_Document*) document; - CPDFDoc_Environment * pEnv = NULL; - pEnv = new CPDFDoc_Environment(pDocument); - if (!pEnv) - return NULL; - pEnv->RegAppHandle(formInfo); - - if(pEnv->GetPDFDocument()) - { - CPDFSDK_Document* pSDKDoc = new CPDFSDK_Document(pEnv->GetPDFDocument(), pEnv); - if(pSDKDoc) - pEnv->SetCurrentDoc(pSDKDoc); - } - return pEnv; + if (!document || !formInfo || formInfo->version != 1) + return nullptr; + CPDF_Document * pDocument = (CPDF_Document*) document; + CPDFDoc_Environment * pEnv = new CPDFDoc_Environment(pDocument); + pEnv->RegAppHandle(formInfo); + if (CPDF_Document* pEnvDocument = pEnv->GetPDFDocument()) + pEnv->SetCurrentDoc(new CPDFSDK_Document(pEnvDocument, pEnv)); + return pEnv; } DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp index ad27aeb92f..527538cd09 100644 --- a/fpdfsdk/src/fsdk_mgr.cpp +++ b/fpdfsdk/src/fsdk_mgr.cpp @@ -5,6 +5,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com #include "../../public/fpdf_ext.h" +#include "../../third_party/base/nonstd_unique_ptr.h" #include "../include/fsdk_define.h" #include "../include/fsdk_mgr.h" #include "../include/formfiller/FFL_FormFiller.h" @@ -254,6 +255,55 @@ CPDFDoc_Environment::~CPDFDoc_Environment() m_pActionHandler = NULL; } +CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() +{ + if (!m_pInfo || + !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Field_browse) { + return L""; + } + + const int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse( + m_pInfo->m_pJsPlatform, nullptr, 0); + if (nRequiredLen <= 0) + return L""; + + nonstd::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""; + + CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); + CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); + return wsRet; +} + +CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() +{ + if (!m_pInfo || + !m_pInfo->m_pJsPlatform || + !m_pInfo->m_pJsPlatform->Doc_getFilePath) { + return L""; + } + + const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( + m_pInfo->m_pJsPlatform, nullptr, 0); + if (nRequiredLen <= 0) + return L""; + + nonstd::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""; + + CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); + CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); + return wsRet; +} IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp index 9d992d0189..31e313179e 100644 --- a/fpdfsdk/src/javascript/app.cpp +++ b/fpdfsdk/src/javascript/app.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "../../../third_party/base/nonstd_unique_ptr.h" #include "../../include/javascript/JavaScript.h" #include "../../include/javascript/IJavaScript.h" #include "../../include/javascript/JS_Define.h" @@ -894,26 +895,27 @@ FX_BOOL app::response(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value CPDFDoc_Environment* pApp = pContext->GetReaderApp(); ASSERT(pApp != NULL); - const int MAX_INPUT_BYTES = 2048; - char* pBuff = new char[MAX_INPUT_BYTES + 2]; - if (!pBuff) - return FALSE; - - memset(pBuff, 0, MAX_INPUT_BYTES + 2); - int nLengthBytes = pApp->JS_appResponse(swQuestion.c_str(), swTitle.c_str(), swDefault.c_str(), - swLabel.c_str(), bPassWord, pBuff, MAX_INPUT_BYTES); - if (nLengthBytes <= 0) - { - vRet.SetNull(); - delete[] pBuff; - return FALSE; - } - if (nLengthBytes > MAX_INPUT_BYTES) - nLengthBytes = MAX_INPUT_BYTES; - - vRet = CFX_WideString::FromUTF16LE((unsigned short*)pBuff, nLengthBytes / sizeof(unsigned short)).c_str(); - delete[] pBuff; - return TRUE; + const int MAX_INPUT_BYTES = 2048; + nonstd::unique_ptr pBuff(new char[MAX_INPUT_BYTES + 2]); + memset(pBuff.get(), 0, MAX_INPUT_BYTES + 2); + int nLengthBytes = pApp->JS_appResponse(swQuestion.c_str(), + swTitle.c_str(), + swDefault.c_str(), + swLabel.c_str(), + bPassWord, + pBuff.get(), + MAX_INPUT_BYTES); + if (nLengthBytes <= 0) { + vRet.SetNull(); + return FALSE; + } + nLengthBytes = std::min(nLengthBytes, MAX_INPUT_BYTES); + + CFX_WideString ret_string = + CFX_WideString::FromUTF16LE((unsigned short*)pBuff.get(), + nLengthBytes / sizeof(unsigned short)); + vRet = ret_string.c_str(); + return TRUE; } FX_BOOL app::media(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) -- cgit v1.2.3