Avoid some string -> ptr -> string duplicate allocations in FF Environment

Change-Id: I4bd89b64cd77a4e2fe0ffc2dcc415cc8fe34667a
Reviewed-on: https://pdfium-review.googlesource.com/30871
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 262a56e..b3375d4 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -58,25 +58,25 @@
     m_pInfo->Release(m_pInfo);
 }
 
-int CPDFSDK_FormFillEnvironment::JS_appAlert(const wchar_t* Msg,
-                                             const wchar_t* Title,
+int CPDFSDK_FormFillEnvironment::JS_appAlert(const WideString& Msg,
+                                             const WideString& Title,
                                              uint32_t Type,
                                              uint32_t Icon) {
   if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
       !m_pInfo->m_pJsPlatform->app_alert) {
     return -1;
   }
-  ByteString bsMsg = WideString(Msg).UTF16LE_Encode();
-  ByteString bsTitle = WideString(Title).UTF16LE_Encode();
+  ByteString bsMsg = Msg.UTF16LE_Encode();
+  ByteString bsTitle = Title.UTF16LE_Encode();
   return m_pInfo->m_pJsPlatform->app_alert(
       m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
       AsFPDFWideString(&bsTitle), Type, Icon);
 }
 
-int CPDFSDK_FormFillEnvironment::JS_appResponse(const wchar_t* Question,
-                                                const wchar_t* Title,
-                                                const wchar_t* Default,
-                                                const wchar_t* cLabel,
+int CPDFSDK_FormFillEnvironment::JS_appResponse(const WideString& Question,
+                                                const WideString& Title,
+                                                const WideString& Default,
+                                                const WideString& Label,
                                                 FPDF_BOOL bPassword,
                                                 void* response,
                                                 int length) {
@@ -84,10 +84,10 @@
       !m_pInfo->m_pJsPlatform->app_response) {
     return -1;
   }
-  ByteString bsQuestion = WideString(Question).UTF16LE_Encode();
-  ByteString bsTitle = WideString(Title).UTF16LE_Encode();
-  ByteString bsDefault = WideString(Default).UTF16LE_Encode();
-  ByteString bsLabel = WideString(cLabel).UTF16LE_Encode();
+  ByteString bsQuestion = Question.UTF16LE_Encode();
+  ByteString bsTitle = Title.UTF16LE_Encode();
+  ByteString bsDefault = Default.UTF16LE_Encode();
+  ByteString bsLabel = Label.UTF16LE_Encode();
   return m_pInfo->m_pJsPlatform->app_response(
       m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
       AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
@@ -144,34 +144,33 @@
 
 void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
                                                    int length,
-                                                   const wchar_t* URL) {
+                                                   const WideString& URL) {
   if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
       !m_pInfo->m_pJsPlatform->Doc_submitForm) {
     return;
   }
-  ByteString bsDestination = WideString(URL).UTF16LE_Encode();
+  ByteString bsUrl = URL.UTF16LE_Encode();
   m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
-                                         length,
-                                         AsFPDFWideString(&bsDestination));
+                                         length, AsFPDFWideString(&bsUrl));
 }
 
 void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
                                                  int length,
                                                  FPDF_BOOL bUI,
-                                                 const wchar_t* To,
-                                                 const wchar_t* Subject,
-                                                 const wchar_t* CC,
-                                                 const wchar_t* BCC,
-                                                 const wchar_t* Msg) {
+                                                 const WideString& To,
+                                                 const WideString& Subject,
+                                                 const WideString& CC,
+                                                 const WideString& BCC,
+                                                 const WideString& Msg) {
   if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
       !m_pInfo->m_pJsPlatform->Doc_mail) {
     return;
   }
-  ByteString bsTo = WideString(To).UTF16LE_Encode();
-  ByteString bsSubject = WideString(Subject).UTF16LE_Encode();
-  ByteString bsCC = WideString(CC).UTF16LE_Encode();
-  ByteString bsBcc = WideString(BCC).UTF16LE_Encode();
-  ByteString bsMsg = WideString(Msg).UTF16LE_Encode();
+  ByteString bsTo = To.UTF16LE_Encode();
+  ByteString bsSubject = Subject.UTF16LE_Encode();
+  ByteString bsCC = CC.UTF16LE_Encode();
+  ByteString bsBcc = BCC.UTF16LE_Encode();
+  ByteString bsMsg = Msg.UTF16LE_Encode();
   m_pInfo->m_pJsPlatform->Doc_mail(
       m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
       AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
@@ -435,11 +434,11 @@
 }
 
 RetainPtr<IFX_SeekableReadStream> CPDFSDK_FormFillEnvironment::DownloadFromURL(
-    const wchar_t* url) {
+    const WideString& url) {
   if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
     return nullptr;
 
-  ByteString bstrURL = WideString(url).UTF16LE_Encode();
+  ByteString bstrURL = url.UTF16LE_Encode();
   FPDF_LPFILEHANDLER fileHandler =
       m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL));
 
@@ -447,19 +446,19 @@
 }
 
 WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
-    const wchar_t* wsURL,
-    const wchar_t* wsData,
-    const wchar_t* wsContentType,
-    const wchar_t* wsEncode,
-    const wchar_t* wsHeader) {
+    const WideString& wsURL,
+    const WideString& wsData,
+    const WideString& wsContentType,
+    const WideString& wsEncode,
+    const WideString& wsHeader) {
   if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
     return L"";
 
-  ByteString bsURL = WideString(wsURL).UTF16LE_Encode();
-  ByteString bsData = WideString(wsData).UTF16LE_Encode();
-  ByteString bsContentType = WideString(wsContentType).UTF16LE_Encode();
-  ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode();
-  ByteString bsHeader = WideString(wsHeader).UTF16LE_Encode();
+  ByteString bsURL = wsURL.UTF16LE_Encode();
+  ByteString bsData = wsData.UTF16LE_Encode();
+  ByteString bsContentType = wsContentType.UTF16LE_Encode();
+  ByteString bsEncode = wsEncode.UTF16LE_Encode();
+  ByteString bsHeader = wsHeader.UTF16LE_Encode();
 
   FPDF_BSTR response;
   FPDF_BStr_Init(&response);
@@ -476,15 +475,16 @@
   return wsRet;
 }
 
-FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const wchar_t* wsURL,
-                                                     const wchar_t* wsData,
-                                                     const wchar_t* wsEncode) {
+FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(
+    const WideString& wsURL,
+    const WideString& wsData,
+    const WideString& wsEncode) {
   if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
     return false;
 
-  ByteString bsURL = WideString(wsURL).UTF16LE_Encode();
-  ByteString bsData = WideString(wsData).UTF16LE_Encode();
-  ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode();
+  ByteString bsURL = wsURL.UTF16LE_Encode();
+  ByteString bsData = wsData.UTF16LE_Encode();
+  ByteString bsEncode = wsEncode.UTF16LE_Encode();
 
   return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL),
                                     AsFPDFWideString(&bsData),
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.h b/fpdfsdk/cpdfsdk_formfillenvironment.h
index 18626cf..d60f2ec 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.h
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.h
@@ -160,15 +160,15 @@
   FPDF_FILEHANDLER* OpenFile(int fileType,
                              FPDF_WIDESTRING wsURL,
                              const char* mode);
-  RetainPtr<IFX_SeekableReadStream> DownloadFromURL(const wchar_t* url);
-  WideString PostRequestURL(const wchar_t* wsURL,
-                            const wchar_t* wsData,
-                            const wchar_t* wsContentType,
-                            const wchar_t* wsEncode,
-                            const wchar_t* wsHeader);
-  FPDF_BOOL PutRequestURL(const wchar_t* wsURL,
-                          const wchar_t* wsData,
-                          const wchar_t* wsEncode);
+  RetainPtr<IFX_SeekableReadStream> DownloadFromURL(const WideString& url);
+  WideString PostRequestURL(const WideString& wsURL,
+                            const WideString& wsData,
+                            const WideString& wsContentType,
+                            const WideString& wsEncode,
+                            const WideString& wsHeader);
+  FPDF_BOOL PutRequestURL(const WideString& wsURL,
+                          const WideString& wsData,
+                          const WideString& wsEncode);
   WideString GetLanguage();
 
   void PageEvent(int iPageCount, uint32_t dwEventType) const;
@@ -176,29 +176,29 @@
   CPDF_Document* GetPDFDocument() const { return m_pUnderlyingDoc.Get(); }
 #endif  // PDF_ENABLE_XFA
 
-  int JS_appAlert(const wchar_t* Msg,
-                  const wchar_t* Title,
+  int JS_appAlert(const WideString& Msg,
+                  const WideString& Title,
                   uint32_t Type,
                   uint32_t Icon);
-  int JS_appResponse(const wchar_t* Question,
-                     const wchar_t* Title,
-                     const wchar_t* Default,
-                     const wchar_t* cLabel,
+  int JS_appResponse(const WideString& Question,
+                     const WideString& Title,
+                     const WideString& Default,
+                     const WideString& cLabel,
                      FPDF_BOOL bPassword,
                      void* response,
                      int length);
   void JS_appBeep(int nType);
   WideString JS_fieldBrowse();
   WideString JS_docGetFilePath();
-  void JS_docSubmitForm(void* formData, int length, const wchar_t* URL);
+  void JS_docSubmitForm(void* formData, int length, const WideString& URL);
   void JS_docmailForm(void* mailData,
                       int length,
                       FPDF_BOOL bUI,
-                      const wchar_t* To,
-                      const wchar_t* Subject,
-                      const wchar_t* CC,
-                      const wchar_t* BCC,
-                      const wchar_t* Msg);
+                      const WideString& To,
+                      const WideString& Subject,
+                      const WideString& CC,
+                      const WideString& BCC,
+                      const WideString& Msg);
   void JS_docprint(FPDF_BOOL bUI,
                    int nStart,
                    int nEnd,
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index e83e599..5ebf683 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -472,7 +472,6 @@
                                      bool bIncludeOrExclude,
                                      bool bUrlEncoded) {
   ByteString textBuf = ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude);
-
   size_t nBufSize = textBuf.GetLength();
   if (nBufSize == 0)
     return false;
@@ -486,7 +485,7 @@
     return false;
   }
 
-  m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
+  m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination);
 
   if (pBuffer != pLocalBuffer)
     FX_Free(pBuffer);
@@ -564,15 +563,15 @@
 
   uint8_t* pLocalBuffer = FX_Alloc(uint8_t, fdfBuffer.GetLength());
   memcpy(pLocalBuffer, fdfBuffer.c_str(), fdfBuffer.GetLength());
-  uint8_t* pBuffer = pLocalBuffer;
 
+  uint8_t* pBuffer = pLocalBuffer;
   size_t nBufSize = fdfBuffer.GetLength();
   if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) {
     FX_Free(pLocalBuffer);
     return false;
   }
 
-  m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
+  m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination);
 
   if (pBuffer != pLocalBuffer)
     FX_Free(pBuffer);
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
index e1721a3..949d886 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -258,8 +258,9 @@
       iButtonType |= 3;
       break;
   }
-  int32_t iRet = m_pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
-                                             iButtonType, iconType);
+  int32_t iRet =
+      m_pFormFillEnv->JS_appAlert(wsMessage, wsTitle, iButtonType, iconType);
+
   switch (iRet) {
     case 1:
       return XFA_IDOK;
@@ -282,9 +283,9 @@
 
   int nLength = 2048;
   std::vector<uint8_t> pBuff(nLength);
-  nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
-                                           wsDefaultAnswer.c_str(), nullptr,
-                                           bMark, pBuff.data(), nLength);
+  nLength = m_pFormFillEnv->JS_appResponse(wsQuestion, wsTitle, wsDefaultAnswer,
+                                           WideString(), bMark, pBuff.data(),
+                                           nLength);
   if (nLength <= 0)
     return WideString();
 
@@ -297,8 +298,7 @@
 
 RetainPtr<IFX_SeekableReadStream> CPDFXFA_Context::DownloadURL(
     const WideString& wsURL) {
-  return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL.c_str())
-                        : nullptr;
+  return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL) : nullptr;
 }
 
 bool CPDFXFA_Context::PostRequestURL(const WideString& wsURL,
@@ -310,9 +310,8 @@
   if (!m_pFormFillEnv)
     return false;
 
-  wsResponse = m_pFormFillEnv->PostRequestURL(
-      wsURL.c_str(), wsData.c_str(), wsContentType.c_str(), wsEncode.c_str(),
-      wsHeader.c_str());
+  wsResponse = m_pFormFillEnv->PostRequestURL(wsURL, wsData, wsContentType,
+                                              wsEncode, wsHeader);
   return true;
 }
 
@@ -320,8 +319,7 @@
                                     const WideString& wsData,
                                     const WideString& wsEncode) {
   return m_pFormFillEnv &&
-         m_pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(),
-                                       wsEncode.c_str());
+         m_pFormFillEnv->PutRequestURL(wsURL, wsData, wsEncode);
 }
 
 IFWL_AdapterTimerMgr* CPDFXFA_Context::GetTimerMgr() {
diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp
index 5bd41e8..35bdc32 100644
--- a/fxjs/cjs_app.cpp
+++ b/fxjs/cjs_app.cpp
@@ -263,9 +263,8 @@
 
   pRuntime->BeginBlock();
   pFormFillEnv->KillFocusAnnot(0);
-
   v8::Local<v8::Value> ret = pRuntime->NewNumber(
-      pFormFillEnv->JS_appAlert(swMsg.c_str(), swTitle.c_str(), iType, iIcon));
+      pFormFillEnv->JS_appAlert(swMsg, swTitle, iType, iIcon));
   pRuntime->EndBlock();
 
   return CJS_Return(ret);
@@ -466,9 +465,8 @@
     cMsg = pRuntime->ToWideString(newParams[5]);
 
   pRuntime->BeginBlock();
-  pRuntime->GetFormFillEnv()->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(),
-                                             cSubject.c_str(), cCc.c_str(),
-                                             cBcc.c_str(), cMsg.c_str());
+  pRuntime->GetFormFillEnv()->JS_docmailForm(nullptr, 0, bUI, cTo, cSubject,
+                                             cCc, cBcc, cMsg);
   pRuntime->EndBlock();
   return CJS_Return(true);
 }
@@ -558,8 +556,8 @@
   const int MAX_INPUT_BYTES = 2048;
   std::vector<uint8_t> pBuff(MAX_INPUT_BYTES + 2);
   int nLengthBytes = pRuntime->GetFormFillEnv()->JS_appResponse(
-      swQuestion.c_str(), swTitle.c_str(), swDefault.c_str(), swLabel.c_str(),
-      bPassword, pBuff.data(), MAX_INPUT_BYTES);
+      swQuestion, swTitle, swDefault, swLabel, bPassword, pBuff.data(),
+      MAX_INPUT_BYTES);
 
   if (nLengthBytes < 0 || nLengthBytes > MAX_INPUT_BYTES)
     return CJS_Return(JSGetStringFromID(JSMessage::kParamTooLongError));
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 2a36b61..248116b 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -338,9 +338,8 @@
 
   pRuntime->BeginBlock();
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
-  pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo.c_str(),
-                               cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
-                               cMsg.c_str());
+  pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo, cSubject, cCc,
+                               cBcc, cMsg);
   pRuntime->EndBlock();
   FX_Free(pMutableBuf);
   return CJS_Return(true);
@@ -639,8 +638,7 @@
 
   pRuntime->BeginBlock();
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
-  pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
-                               cCc.c_str(), cBcc.c_str(), cMsg.c_str());
+  pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo, cSubject, cCc, cBcc, cMsg);
   pRuntime->EndBlock();
   return CJS_Return(true);
 }
diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp
index 8dc69c4..6f49940 100644
--- a/fxjs/cjs_publicmethods.cpp
+++ b/fxjs/cjs_publicmethods.cpp
@@ -81,7 +81,7 @@
 void AlertIfPossible(CJS_EventContext* pContext, const wchar_t* swMsg) {
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
   if (pFormFillEnv)
-    pFormFillEnv->JS_appAlert(swMsg, nullptr, 0, 3);
+    pFormFillEnv->JS_appAlert(swMsg, WideString(), 0, 3);
 }
 
 #if _FX_OS_ != _FX_OS_ANDROID_