summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-10-10 18:44:45 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-10-10 18:44:45 +0000
commit4f261fffddddd97602e88fe462fef1a18a0b4782 (patch)
treee3a93a859d189a5fe46791d7954396b2b394cbd4 /fpdfsdk
parentb353f8c32b2d17cbea5ace6eb5f7edb8663e37be (diff)
downloadpdfium-4f261fffddddd97602e88fe462fef1a18a0b4782.tar.xz
Get rid of non-const ref parameter in CPDFSDK_InterForm::OnFormat().
Return Optional<WideString> instead. Change the call stack to accept Optional<WideString>. Change-Id: I020589fac8a1de4974070268a19b4acad64c4092 Reviewed-on: https://pdfium-review.googlesource.com/c/43601 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/cpdfsdk_interform.cpp32
-rw-r--r--fpdfsdk/cpdfsdk_interform.h5
-rw-r--r--fpdfsdk/cpdfsdk_widget.cpp17
-rw-r--r--fpdfsdk/cpdfsdk_widget.h4
-rw-r--r--fpdfsdk/cpdfsdk_widgethandler.cpp12
-rw-r--r--fpdfsdk/formfiller/cffl_interactiveformfiller.cpp7
-rw-r--r--fpdfsdk/pwl/cpwl_appstream.cpp21
-rw-r--r--fpdfsdk/pwl/cpwl_appstream.h9
8 files changed, 46 insertions, 61 deletions
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index 549caafb51..2cc20f9d98 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -323,14 +323,11 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
}
}
-WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
- bool& bFormatted) {
- WideString sValue = pFormField->GetValue();
- if (!m_pFormFillEnv->IsJSPlatformPresent()) {
- bFormatted = false;
- return sValue;
- }
+Optional<WideString> CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField) {
+ if (!m_pFormFillEnv->IsJSPlatformPresent())
+ return {};
+ WideString sValue = pFormField->GetValue();
IJS_Runtime* pRuntime = m_pFormFillEnv->GetIJSRuntime();
if (pFormField->GetFieldType() == FormFieldType::kComboBox &&
pFormField->CountSelectedItems() > 0) {
@@ -339,30 +336,25 @@ WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
sValue = pFormField->GetOptionLabel(index);
}
- bFormatted = false;
-
CPDF_AAction aAction = pFormField->GetAdditionalAction();
if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
if (action.GetDict()) {
WideString script = action.GetJavaScript();
if (!script.IsEmpty()) {
- WideString Value = sValue;
IJS_Runtime::ScopedEventContext pContext(pRuntime);
- pContext->OnField_Format(pFormField, &Value, true);
+ pContext->OnField_Format(pFormField, &sValue, true);
Optional<IJS_Runtime::JS_Error> err = pContext->RunScript(script);
- if (!err) {
- sValue = std::move(Value);
- bFormatted = true;
- }
+ if (!err)
+ return sValue;
}
}
}
- return sValue;
+ return {};
}
void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
- const WideString* sValue,
+ Optional<WideString> sValue,
bool bValueChanged) {
for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
@@ -591,9 +583,7 @@ void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
return;
OnCalculate(pField);
- bool bFormatted = false;
- WideString sValue = OnFormat(pField, bFormatted);
- ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, true);
+ ResetFieldAppearance(pField, OnFormat(pField), true);
UpdateField(pField);
}
@@ -611,7 +601,7 @@ void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
return;
OnCalculate(pField);
- ResetFieldAppearance(pField, nullptr, true);
+ ResetFieldAppearance(pField, pdfium::nullopt, true);
UpdateField(pField);
}
diff --git a/fpdfsdk/cpdfsdk_interform.h b/fpdfsdk/cpdfsdk_interform.h
index 1862fdbe2c..2a34c23748 100644
--- a/fpdfsdk/cpdfsdk_interform.h
+++ b/fpdfsdk/cpdfsdk_interform.h
@@ -16,6 +16,7 @@
#include "core/fxcrt/unowned_ptr.h"
#include "core/fxge/fx_dib.h"
#include "fpdfsdk/cpdfsdk_widget.h"
+#include "third_party/base/optional.h"
class CPDF_Dictionary;
class CPDF_FormControl;
@@ -66,10 +67,10 @@ class CPDFSDK_InterForm final : public IPDF_FormNotify {
bool OnKeyStrokeCommit(CPDF_FormField* pFormField, const WideString& csValue);
bool OnValidate(CPDF_FormField* pFormField, const WideString& csValue);
void OnCalculate(CPDF_FormField* pFormField);
- WideString OnFormat(CPDF_FormField* pFormField, bool& bFormatted);
+ Optional<WideString> OnFormat(CPDF_FormField* pFormField);
void ResetFieldAppearance(CPDF_FormField* pFormField,
- const WideString* sValue,
+ Optional<WideString> sValue,
bool bValueChanged);
void UpdateField(CPDF_FormField* pFormField);
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index c2a4754edc..fc848d5014 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -585,19 +585,17 @@ void CPDFSDK_Widget::ResetAppearance(bool bValueChanged) {
switch (GetFieldType()) {
case FormFieldType::kTextField:
case FormFieldType::kComboBox: {
- bool bFormatted = false;
- WideString sValue = OnFormat(bFormatted);
- ResetAppearance(bFormatted ? &sValue : nullptr, true);
+ ResetAppearance(OnFormat(), true);
break;
}
default:
- ResetAppearance(nullptr, false);
+ ResetAppearance(pdfium::nullopt, false);
break;
}
}
#endif // PDF_ENABLE_XFA
-void CPDFSDK_Widget::ResetAppearance(const WideString* sValue,
+void CPDFSDK_Widget::ResetAppearance(Optional<WideString> sValue,
bool bValueChanged) {
SetAppModified();
@@ -632,16 +630,17 @@ void CPDFSDK_Widget::ResetAppearance(const WideString* sValue,
m_pAnnot->ClearCachedAP();
}
-WideString CPDFSDK_Widget::OnFormat(bool& bFormatted) {
+Optional<WideString> CPDFSDK_Widget::OnFormat() {
CPDF_FormField* pFormField = GetFormField();
ASSERT(pFormField);
- return m_pInterForm->OnFormat(pFormField, bFormatted);
+ return m_pInterForm->OnFormat(pFormField);
}
void CPDFSDK_Widget::ResetFieldAppearance(bool bValueChanged) {
CPDF_FormField* pFormField = GetFormField();
ASSERT(pFormField);
- m_pInterForm->ResetFieldAppearance(pFormField, nullptr, bValueChanged);
+ m_pInterForm->ResetFieldAppearance(pFormField, pdfium::nullopt,
+ bValueChanged);
}
void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
@@ -700,7 +699,7 @@ void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
CFX_FloatRect rcWindow = GetRotatedRect();
- float fBorderWidth = (float)GetBorderWidth();
+ float fBorderWidth = GetBorderWidth();
switch (GetBorderStyle()) {
case BorderStyle::BEVELED:
case BorderStyle::INSET:
diff --git a/fpdfsdk/cpdfsdk_widget.h b/fpdfsdk/cpdfsdk_widget.h
index fb83ceb555..e3f9204eb8 100644
--- a/fpdfsdk/cpdfsdk_widget.h
+++ b/fpdfsdk/cpdfsdk_widget.h
@@ -89,10 +89,10 @@ class CPDFSDK_Widget final : public CPDFSDK_BAAnnot {
#ifdef PDF_ENABLE_XFA
void ResetAppearance(bool bValueChanged);
#endif // PDF_ENABLE_XFA
- void ResetAppearance(const WideString* sValue, bool bValueChanged);
+ void ResetAppearance(Optional<WideString> sValue, bool bValueChanged);
void ResetFieldAppearance(bool bValueChanged);
void UpdateField();
- WideString OnFormat(bool& bFormatted);
+ Optional<WideString> OnFormat();
bool OnAAction(CPDF_AAction::AActionType type,
CPDFSDK_FieldAction* data,
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index b67a92d29f..c63d91564b 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -63,8 +63,7 @@ CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
pInterForm->AddMap(pCtrl, pWidget);
CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
if (pPDFInterForm->NeedConstructAP())
- pWidget->ResetAppearance(nullptr, false);
-
+ pWidget->ResetAppearance(pdfium::nullopt, false);
return pWidget;
}
@@ -223,19 +222,18 @@ void CPDFSDK_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
if (!pWidget->IsAppearanceValid())
- pWidget->ResetAppearance(nullptr, false);
+ pWidget->ResetAppearance(pdfium::nullopt, false);
FormFieldType fieldType = pWidget->GetFieldType();
if (fieldType == FormFieldType::kTextField ||
fieldType == FormFieldType::kComboBox) {
- bool bFormatted = false;
CPDFSDK_Annot::ObservedPtr pObserved(pWidget);
- WideString sValue = pWidget->OnFormat(bFormatted);
+ Optional<WideString> sValue = pWidget->OnFormat();
if (!pObserved)
return;
- if (bFormatted && fieldType == FormFieldType::kComboBox)
- pWidget->ResetAppearance(&sValue, false);
+ if (sValue.has_value() && fieldType == FormFieldType::kComboBox)
+ pWidget->ResetAppearance(sValue, false);
}
#ifdef PDF_ENABLE_XFA
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index 329224d2b2..cb36de732e 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -718,13 +718,12 @@ void CFFL_InteractiveFormFiller::OnFormat(CPDFSDK_Annot::ObservedPtr* pAnnot,
ASSERT(pWidget);
CPDFSDK_InterForm* pInterForm = pPageView->GetFormFillEnv()->GetInterForm();
- bool bFormatted = false;
- WideString sValue = pInterForm->OnFormat(pWidget->GetFormField(), bFormatted);
+ Optional<WideString> sValue = pInterForm->OnFormat(pWidget->GetFormField());
if (!(*pAnnot))
return;
- if (bFormatted) {
- pInterForm->ResetFieldAppearance(pWidget->GetFormField(), &sValue, true);
+ if (sValue.has_value()) {
+ pInterForm->ResetFieldAppearance(pWidget->GetFormField(), sValue, true);
pInterForm->UpdateField(pWidget->GetFormField());
}
diff --git a/fpdfsdk/pwl/cpwl_appstream.cpp b/fpdfsdk/pwl/cpwl_appstream.cpp
index 8ced563161..d2ffd2a9b1 100644
--- a/fpdfsdk/pwl/cpwl_appstream.cpp
+++ b/fpdfsdk/pwl/cpwl_appstream.cpp
@@ -1580,7 +1580,7 @@ void CPWL_AppStream::SetAsRadioButton() {
widget_->SetAppState("Off");
}
-void CPWL_AppStream::SetAsComboBox(const WideString* sValue) {
+void CPWL_AppStream::SetAsComboBox(Optional<WideString> sValue) {
CPDF_FormControl* pControl = widget_->GetFormControl();
CPDF_FormField* pField = pControl->GetField();
std::ostringstream sBody;
@@ -1614,8 +1614,8 @@ void CPWL_AppStream::SetAsComboBox(const WideString* sValue) {
pEdit->Initialize();
- if (sValue) {
- pEdit->SetText(*sValue);
+ if (sValue.has_value()) {
+ pEdit->SetText(sValue.value());
} else {
int32_t nCurSel = pField->GetSelectedIndex(0);
if (nCurSel < 0)
@@ -1736,7 +1736,7 @@ void CPWL_AppStream::SetAsListBox() {
"");
}
-void CPWL_AppStream::SetAsTextField(const WideString* sValue) {
+void CPWL_AppStream::SetAsTextField(Optional<WideString> sValue) {
CPDF_FormControl* pControl = widget_->GetFormControl();
CPDF_FormField* pField = pControl->GetField();
std::ostringstream sBody;
@@ -1775,11 +1775,8 @@ void CPWL_AppStream::SetAsTextField(const WideString* sValue) {
float fFontSize = widget_->GetFontSize();
#ifdef PDF_ENABLE_XFA
- WideString sValueTmp;
- if (!sValue && widget_->GetMixXFAWidget()) {
- sValueTmp = widget_->GetValue();
- sValue = &sValueTmp;
- }
+ if (!sValue.has_value() && widget_->GetMixXFAWidget())
+ sValue = widget_->GetValue();
#endif // PDF_ENABLE_XFA
if (nMaxLen > 0) {
@@ -1791,8 +1788,8 @@ void CPWL_AppStream::SetAsTextField(const WideString* sValue) {
rcClient, nMaxLen);
}
} else {
- if (sValue)
- nMaxLen = sValue->GetLength();
+ if (sValue.has_value())
+ nMaxLen = sValue.value().GetLength();
pEdit->SetLimitChar(nMaxLen);
}
}
@@ -1803,7 +1800,7 @@ void CPWL_AppStream::SetAsTextField(const WideString* sValue) {
pEdit->SetFontSize(fFontSize);
pEdit->Initialize();
- pEdit->SetText(sValue ? *sValue : pField->GetValue());
+ pEdit->SetText(sValue.value_or(pField->GetValue()));
CFX_FloatRect rcContent = pEdit->GetContentRect();
ByteString sEdit =
diff --git a/fpdfsdk/pwl/cpwl_appstream.h b/fpdfsdk/pwl/cpwl_appstream.h
index 810e79c9e7..1c3afab592 100644
--- a/fpdfsdk/pwl/cpwl_appstream.h
+++ b/fpdfsdk/pwl/cpwl_appstream.h
@@ -9,6 +9,7 @@
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/unowned_ptr.h"
+#include "third_party/base/optional.h"
class CPDFSDK_Widget;
class CPDF_Dictionary;
@@ -22,9 +23,9 @@ class CPWL_AppStream {
void SetAsPushButton();
void SetAsCheckBox();
void SetAsRadioButton();
- void SetAsComboBox(const WideString* sValue);
+ void SetAsComboBox(Optional<WideString> sValue);
void SetAsListBox();
- void SetAsTextField(const WideString* sValue);
+ void SetAsTextField(Optional<WideString> sValue);
private:
void AddImage(const ByteString& sAPType, CPDF_Stream* pImage);
@@ -36,8 +37,8 @@ class CPWL_AppStream {
ByteString GetBackgroundAppStream() const;
ByteString GetBorderAppStream() const;
- UnownedPtr<CPDFSDK_Widget> widget_;
- UnownedPtr<CPDF_Dictionary> dict_;
+ UnownedPtr<CPDFSDK_Widget> const widget_;
+ UnownedPtr<CPDF_Dictionary> const dict_;
};
#endif // FPDFSDK_PWL_CPWL_APPSTREAM_H_