From 3d8131535e6b127c7ededdbd2e76662688997272 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 21 Jun 2018 17:28:24 +0000 Subject: Pass output parameters as pointers in ijs_event_context It is hard to tell if there is some unsavory stuff going on with references otherwise. Avoids some const_casts in the process. Add some UnownedPtrs along the way to check caller's storage duration. Change-Id: Ic8d85802083f0b27e07993ea25f8f1c15fca1712 Reviewed-on: https://pdfium-review.googlesource.com/35750 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- fpdfsdk/cpdfsdk_actionhandler.cpp | 18 ++++++------ fpdfsdk/cpdfsdk_interform.cpp | 4 +-- fxjs/cjs_event_context.cpp | 28 +++++++++---------- fxjs/cjs_event_context.h | 26 +++++++++--------- fxjs/cjs_event_context_stub.h | 26 +++++++++--------- fxjs/cjs_eventhandler.cpp | 58 +++++++++++++++++++++------------------ fxjs/cjs_eventhandler.h | 33 +++++++++++----------- fxjs/ijs_event_context.h | 27 +++++++++--------- 8 files changed, 113 insertions(+), 107 deletions(-) diff --git a/fpdfsdk/cpdfsdk_actionhandler.cpp b/fpdfsdk/cpdfsdk_actionhandler.cpp index 5b72082104..64d82b93ba 100644 --- a/fpdfsdk/cpdfsdk_actionhandler.cpp +++ b/fpdfsdk/cpdfsdk_actionhandler.cpp @@ -422,22 +422,24 @@ void CPDFSDK_ActionHandler::RunFieldJavaScript( break; case CPDF_AAction::GetFocus: context->OnField_Focus(data->bModifier, data->bShift, pFormField, - data->sValue); + &data->sValue); break; case CPDF_AAction::LoseFocus: context->OnField_Blur(data->bModifier, data->bShift, pFormField, - data->sValue); + &data->sValue); break; case CPDF_AAction::KeyStroke: context->OnField_Keystroke( - data->sChange, data->sChangeEx, data->bKeyDown, data->bModifier, - data->nSelEnd, data->nSelStart, data->bShift, pFormField, - data->sValue, data->bWillCommit, data->bFieldFull, data->bRC); + &data->sChange, data->sChangeEx, data->bKeyDown, + data->bModifier, &data->nSelEnd, &data->nSelStart, data->bShift, + pFormField, &data->sValue, data->bWillCommit, data->bFieldFull, + &data->bRC); break; case CPDF_AAction::Validate: - context->OnField_Validate( - data->sChange, data->sChangeEx, data->bKeyDown, data->bModifier, - data->bShift, pFormField, data->sValue, data->bRC); + context->OnField_Validate(&data->sChange, data->sChangeEx, + data->bKeyDown, data->bModifier, + data->bShift, pFormField, &data->sValue, + &data->bRC); break; default: NOTREACHED(); diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 751a889b68..9d646b6574 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -290,7 +290,7 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { WideString sValue = sOldValue; bool bRC = true; IJS_Runtime::ScopedEventContext pContext(pRuntime); - pContext->OnField_Calculate(pFormField, pField, sValue, bRC); + pContext->OnField_Calculate(pFormField, pField, &sValue, &bRC); Optional err = pContext->RunScript(csJS); if (!err && bRC && sValue.Compare(sOldValue) != 0) @@ -324,7 +324,7 @@ WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, if (!script.IsEmpty()) { WideString Value = sValue; IJS_Runtime::ScopedEventContext pContext(pRuntime); - pContext->OnField_Format(pFormField, Value, true); + pContext->OnField_Format(pFormField, &Value, true); Optional err = pContext->RunScript(script); if (!err) { sValue = std::move(Value); diff --git a/fxjs/cjs_event_context.cpp b/fxjs/cjs_event_context.cpp index 70f06771d1..3d40c9dc5a 100644 --- a/fxjs/cjs_event_context.cpp +++ b/fxjs/cjs_event_context.cpp @@ -136,55 +136,55 @@ void CJS_EventContext::OnField_MouseUp(bool bModifier, void CJS_EventContext::OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) { + WideString* Value) { m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value); } void CJS_EventContext::OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) { + WideString* Value) { m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value); } void CJS_EventContext::OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) { - m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc); + WideString* pValue, + bool* pRc) { + m_pEventHandler->OnField_Calculate(pSource, pTarget, pValue, pRc); } void CJS_EventContext::OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit) { m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit); } -void CJS_EventContext::OnField_Keystroke(WideString& strChange, +void CJS_EventContext::OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* nSelEnd, + int* nSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit, bool bFieldFull, - bool& bRc) { + bool* bRc) { m_pEventHandler->OnField_Keystroke( strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift, pTarget, Value, bWillCommit, bFieldFull, bRc); } -void CJS_EventContext::OnField_Validate(WideString& strChange, +void CJS_EventContext::OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) { + WideString* Value, + bool* bRc) { m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier, bShift, pTarget, Value, bRc); } diff --git a/fxjs/cjs_event_context.h b/fxjs/cjs_event_context.h index 3cfc6da775..29bbd5a853 100644 --- a/fxjs/cjs_event_context.h +++ b/fxjs/cjs_event_context.h @@ -52,38 +52,38 @@ class CJS_EventContext : public IJS_EventContext { void OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) override; + WideString* Value) override; void OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) override; + WideString* Value) override; void OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) override; + WideString* pValue, + bool* pRc) override; void OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit) override; - void OnField_Keystroke(WideString& strChange, + void OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* nSelEnd, + int* nSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit, bool bFieldFull, - bool& bRc) override; - void OnField_Validate(WideString& strChange, + bool* bRc) override; + void OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) override; + WideString* Value, + bool* bRc) override; void OnScreen_Focus(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen) override; diff --git a/fxjs/cjs_event_context_stub.h b/fxjs/cjs_event_context_stub.h index c8c5e33ac3..341b9959f9 100644 --- a/fxjs/cjs_event_context_stub.h +++ b/fxjs/cjs_event_context_stub.h @@ -44,38 +44,38 @@ class CJS_EventContextStub final : public IJS_EventContext { void OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) override {} + WideString* Value) override {} void OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) override {} + WideString* Value) override {} void OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) override {} + WideString* pValue, + bool* pRc) override {} void OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit) override {} - void OnField_Keystroke(WideString& strChange, + void OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool KeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* nSelEnd, + int* nSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit, bool bFieldFull, - bool& bRc) override {} - void OnField_Validate(WideString& strChange, + bool* bRc) override {} + void OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) override {} + WideString* Value, + bool* bRc) override {} void OnScreen_Focus(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen) override {} diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp index 43605d985b..884651a77b 100644 --- a/fxjs/cjs_eventhandler.cpp +++ b/fxjs/cjs_eventhandler.cpp @@ -127,97 +127,103 @@ void CJS_EventHandler::OnField_MouseUp(bool bModifier, void CJS_EventHandler::OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) { + WideString* pValue) { + ASSERT(pValue); Initialize(JET_FIELD_FOCUS); m_bModifier = bModifier; m_bShift = bShift; m_strTargetName = pTarget->GetFullName(); - m_pValue = const_cast(&Value); + m_pValue = pValue; } void CJS_EventHandler::OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) { + WideString* pValue) { + ASSERT(pValue); Initialize(JET_FIELD_BLUR); m_bModifier = bModifier; m_bShift = bShift; m_strTargetName = pTarget->GetFullName(); - m_pValue = const_cast(&Value); + m_pValue = pValue; } -void CJS_EventHandler::OnField_Keystroke(WideString& strChange, +void CJS_EventHandler::OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool KeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* pSelEnd, + int* pSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* pValue, bool bWillCommit, bool bFieldFull, - bool& bRc) { + bool* pbRc) { + ASSERT(pValue && pbRc && pSelEnd && pSelStart); Initialize(JET_FIELD_KEYSTROKE); m_nCommitKey = 0; - m_pWideStrChange = &strChange; + m_pWideStrChange = strChange; m_WideStrChangeEx = strChangeEx; m_bKeyDown = KeyDown; m_bModifier = bModifier; - m_pISelEnd = &nSelEnd; - m_pISelStart = &nSelStart; + m_pISelEnd = pSelEnd; + m_pISelStart = pSelStart; m_bShift = bShift; m_strTargetName = pTarget->GetFullName(); - m_pValue = &Value; + m_pValue = pValue; m_bWillCommit = bWillCommit; - m_pbRc = &bRc; + m_pbRc = pbRc; m_bFieldFull = bFieldFull; } -void CJS_EventHandler::OnField_Validate(WideString& strChange, +void CJS_EventHandler::OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) { + WideString* pValue, + bool* pbRc) { + ASSERT(pValue && pbRc); Initialize(JET_FIELD_VALIDATE); - m_pWideStrChange = &strChange; + m_pWideStrChange = strChange; m_WideStrChangeEx = strChangeEx; m_bKeyDown = bKeyDown; m_bModifier = bModifier; m_bShift = bShift; m_strTargetName = pTarget->GetFullName(); - m_pValue = &Value; - m_pbRc = &bRc; + m_pValue = pValue; + m_pbRc = pbRc; } void CJS_EventHandler::OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) { + WideString* pValue, + bool* pRc) { + ASSERT(pValue && pRc); Initialize(JET_FIELD_CALCULATE); if (pSource) m_strSourceName = pSource->GetFullName(); m_strTargetName = pTarget->GetFullName(); - m_pValue = &Value; - m_pbRc = &bRc; + m_pValue = pValue; + m_pbRc = pRc; } void CJS_EventHandler::OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* pValue, bool bWillCommit) { + ASSERT(pValue); Initialize(JET_FIELD_FORMAT); m_nCommitKey = 0; m_strTargetName = pTarget->GetFullName(); - m_pValue = &Value; + m_pValue = pValue; m_bWillCommit = bWillCommit; } diff --git a/fxjs/cjs_eventhandler.h b/fxjs/cjs_eventhandler.h index 7c33ed739e..b38d3ff158 100644 --- a/fxjs/cjs_eventhandler.h +++ b/fxjs/cjs_eventhandler.h @@ -81,32 +81,31 @@ class CJS_EventHandler { void OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc); + WideString* Value, + bool* pbRc); void OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit); - void OnField_Keystroke(WideString& strChange, + void OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool KeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* nSelEnd, + int* nSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit, bool bFieldFull, - bool& bRc); - void OnField_Validate(WideString& strChange, + bool* bRc); + void OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc); - + WideString* Value, + bool* bRc); void OnField_MouseDown(bool bModifier, bool bShift, CPDF_FormField* pTarget); void OnField_MouseEnter(bool bModifier, bool bShift, CPDF_FormField* pTarget); void OnField_MouseExit(bool bModifier, bool bShift, CPDF_FormField* pTarget); @@ -114,11 +113,11 @@ class CJS_EventHandler { void OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value); + WideString* Value); void OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value); + WideString* Value); void OnScreen_Focus(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); void OnScreen_Blur(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen); @@ -190,13 +189,13 @@ class CJS_EventHandler { bool m_bKeyDown = false; bool m_bModifier = false; bool m_bShift = false; - int* m_pISelEnd = nullptr; + UnownedPtr m_pISelEnd; int m_nSelEndDu = 0; - int* m_pISelStart = nullptr; + UnownedPtr m_pISelStart; int m_nSelStartDu = 0; bool m_bWillCommit = false; bool m_bFieldFull = false; - bool* m_pbRc = nullptr; + UnownedPtr m_pbRc; bool m_bRcDu = false; UnownedPtr m_pTargetBookMark; diff --git a/fxjs/ijs_event_context.h b/fxjs/ijs_event_context.h index 8317bc29be..a81f9fb5cc 100644 --- a/fxjs/ijs_event_context.h +++ b/fxjs/ijs_event_context.h @@ -57,39 +57,38 @@ class IJS_EventContext { virtual void OnField_Focus(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) = 0; + WideString* Value) = 0; virtual void OnField_Blur(bool bModifier, bool bShift, CPDF_FormField* pTarget, - const WideString& Value) = 0; - + WideString* Value) = 0; virtual void OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) = 0; + WideString* Value, + bool* bRc) = 0; virtual void OnField_Format(CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit) = 0; - virtual void OnField_Keystroke(WideString& strChange, + virtual void OnField_Keystroke(WideString* strChange, const WideString& strChangeEx, bool KeyDown, bool bModifier, - int& nSelEnd, - int& nSelStart, + int* nSelEnd, + int* nSelStart, bool bShift, CPDF_FormField* pTarget, - WideString& Value, + WideString* Value, bool bWillCommit, bool bFieldFull, - bool& bRc) = 0; - virtual void OnField_Validate(WideString& strChange, + bool* bRc) = 0; + virtual void OnField_Validate(WideString* strChange, const WideString& strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField* pTarget, - WideString& Value, - bool& bRc) = 0; + WideString* Value, + bool* bRc) = 0; virtual void OnScreen_Focus(bool bModifier, bool bShift, -- cgit v1.2.3