From 58fb36a3de3174db41669c2ed0d67e4a18a4a6de Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 1 Feb 2016 10:32:14 -0800 Subject: Merge to XFA: Use JS_ExpandKeywordParams() in app.response() Original Review URL: https://codereview.chromium.org/1654523002 . (cherry picked from commit 3859258ebe9e81e1f766b57e7f78c786ae4ed495) TBR=thestig@chromium.org Review URL: https://codereview.chromium.org/1658753002 . --- fpdfsdk/src/javascript/app.cpp | 87 +++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 57 deletions(-) (limited to 'fpdfsdk/src/javascript') diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp index 057bf7ea58..3a971cf146 100644 --- a/fpdfsdk/src/javascript/app.cpp +++ b/fpdfsdk/src/javascript/app.cpp @@ -690,77 +690,50 @@ FX_BOOL app::response(IJS_Context* cc, const std::vector& params, CJS_Value& vRet, CFX_WideString& sError) { - CFX_WideString swQuestion = L""; - CFX_WideString swLabel = L""; - CFX_WideString swTitle = L"PDF"; - CFX_WideString swDefault = L""; - bool bPassWord = false; - + CJS_Context* pContext = static_cast(cc); CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); - v8::Isolate* isolate = pRuntime->GetIsolate(); - - int iLength = params.size(); - if (iLength > 0 && params[0].GetType() == CJS_Value::VT_object) { - v8::Local pObj = params[0].ToV8Object(); - v8::Local pValue = - FXJS_GetObjectElement(isolate, pObj, L"cQuestion"); - swQuestion = - CJS_Value(pRuntime, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString(); + std::vector newParams = + JS_ExpandKeywordParams(pRuntime, params, 5, L"cQuestion", L"cTitle", + L"cDefault", L"bPassword", L"cLabel"); - pValue = FXJS_GetObjectElement(isolate, pObj, L"cTitle"); - swTitle = - CJS_Value(pRuntime, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString(); + if (newParams[0].GetType() == CJS_Value::VT_unknown) { + sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); + return FALSE; + } + CFX_WideString swQuestion = newParams[0].ToCFXWideString(); - pValue = FXJS_GetObjectElement(isolate, pObj, L"cDefault"); - swDefault = - CJS_Value(pRuntime, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString(); + CFX_WideString swTitle = L"PDF"; + if (newParams[1].GetType() != CJS_Value::VT_unknown) + swTitle = newParams[1].ToCFXWideString(); - pValue = FXJS_GetObjectElement(isolate, pObj, L"cLabel"); - swLabel = - CJS_Value(pRuntime, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString(); + CFX_WideString swDefault; + if (newParams[2].GetType() != CJS_Value::VT_unknown) + swDefault = newParams[2].ToCFXWideString(); - pValue = FXJS_GetObjectElement(isolate, pObj, L"bPassword"); - bPassWord = CJS_Value(pRuntime, pValue, GET_VALUE_TYPE(pValue)).ToBool(); - } else { - switch (iLength) { - case 5: - swLabel = params[4].ToCFXWideString(); - // FALLTHROUGH - case 4: - bPassWord = params[3].ToBool(); - // FALLTHROUGH - case 3: - swDefault = params[2].ToCFXWideString(); - // FALLTHROUGH - case 2: - swTitle = params[1].ToCFXWideString(); - // FALLTHROUGH - case 1: - swQuestion = params[0].ToCFXWideString(); - // FALLTHROUGH - default: - break; - } - } + bool bPassword = false; + if (newParams[3].GetType() != CJS_Value::VT_unknown) + bPassword = newParams[3].ToBool(); - CJS_Context* pContext = (CJS_Context*)cc; - CPDFDoc_Environment* pApp = pContext->GetReaderApp(); + CFX_WideString swLabel; + if (newParams[4].GetType() != CJS_Value::VT_unknown) + swLabel = newParams[4].ToCFXWideString(); const int MAX_INPUT_BYTES = 2048; std::unique_ptr pBuff(new char[MAX_INPUT_BYTES + 2]); memset(pBuff.get(), 0, MAX_INPUT_BYTES + 2); - int nLengthBytes = pApp->JS_appResponse( + + int nLengthBytes = pContext->GetReaderApp()->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(); + bPassword, pBuff.get(), MAX_INPUT_BYTES); + + if (nLengthBytes < 0 || nLengthBytes > MAX_INPUT_BYTES) { + sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG); 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(); + vRet = CFX_WideString::FromUTF16LE(reinterpret_cast(pBuff.get()), + nLengthBytes / sizeof(uint16_t)) + .c_str(); return TRUE; } -- cgit v1.2.3