Remove bool argument from CPDFSDK_FormFillEnvironment::GetPageView()

Split into a GetOrCreate() method that never returns null, and a
Get() method that may return null.

Change-Id: If79c60e40da79cbf9c86a14bc5987847c8338fed
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/81330
Auto-Submit: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_baannothandler_embeddertest.cpp b/fpdfsdk/cpdfsdk_baannothandler_embeddertest.cpp
index 245d500..f402e3c 100644
--- a/fpdfsdk/cpdfsdk_baannothandler_embeddertest.cpp
+++ b/fpdfsdk/cpdfsdk_baannothandler_embeddertest.cpp
@@ -34,7 +34,7 @@
         CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form_handle());
     ASSERT_TRUE(m_pFormFillEnv);
     m_pPageView =
-        m_pFormFillEnv->GetPageView(IPDFPageFromFPDFPage(m_page), true);
+        m_pFormFillEnv->GetOrCreatePageView(IPDFPageFromFPDFPage(m_page));
     ASSERT_TRUE(m_pPageView);
 
     CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 427fb27..fd6dffc 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -131,7 +131,7 @@
 #ifdef PDF_ENABLE_V8
 CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
   IPDF_Page* pPage = IPDFPageFromFPDFPage(GetCurrentPage());
-  return pPage ? GetPageView(pPage, true) : nullptr;
+  return pPage ? GetOrCreatePageView(pPage) : nullptr;
 }
 
 FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage() const {
@@ -589,15 +589,11 @@
   }
 }
 
-CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
-    IPDF_Page* pUnderlyingPage,
-    bool renew) {
-  auto it = m_PageMap.find(pUnderlyingPage);
-  if (it != m_PageMap.end())
-    return it->second.get();
-
-  if (!renew)
-    return nullptr;
+CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetOrCreatePageView(
+    IPDF_Page* pUnderlyingPage) {
+  CPDFSDK_PageView* pExisting = GetPageView(pUnderlyingPage);
+  if (pExisting)
+    return pExisting;
 
   auto pNew = std::make_unique<CPDFSDK_PageView>(this, pUnderlyingPage);
   CPDFSDK_PageView* pPageView = pNew.get();
@@ -608,13 +604,15 @@
   return pPageView;
 }
 
+CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
+    IPDF_Page* pUnderlyingPage) {
+  auto it = m_PageMap.find(pUnderlyingPage);
+  return it != m_PageMap.end() ? it->second.get() : nullptr;
+}
+
 CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageViewAtIndex(int nIndex) {
   IPDF_Page* pTempPage = GetPage(nIndex);
-  if (!pTempPage)
-    return nullptr;
-
-  auto it = m_PageMap.find(pTempPage);
-  return it != m_PageMap.end() ? it->second.get() : nullptr;
+  return pTempPage ? GetPageView(pTempPage) : nullptr;
 }
 
 void CPDFSDK_FormFillEnvironment::ProcJavascriptAction() {
@@ -679,7 +677,7 @@
   m_PageMap.erase(it);
 }
 
-IPDF_Page* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
+IPDF_Page* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) const {
   if (!m_pInfo || !m_pInfo->FFI_GetPage)
     return nullptr;
   return IPDFPageFromFPDFPage(m_pInfo->FFI_GetPage(
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.h b/fpdfsdk/cpdfsdk_formfillenvironment.h
index 78a35f0..d713eed 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.h
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.h
@@ -66,9 +66,9 @@
   bool IsSelectionImplemented() const override;
   void SetCursor(CursorStyle nCursorType) override;
 
-  CPDFSDK_PageView* GetPageView(IPDF_Page* pUnderlyingPage, bool renew);
+  CPDFSDK_PageView* GetOrCreatePageView(IPDF_Page* pUnderlyingPage);
+  CPDFSDK_PageView* GetPageView(IPDF_Page* pUnderlyingPage);
   CPDFSDK_PageView* GetPageViewAtIndex(int nIndex);
-
   void RemovePageView(IPDF_Page* pUnderlyingPage);
   void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
 
@@ -218,7 +218,7 @@
   CPDFSDK_InteractiveForm* GetInteractiveForm();  // Creates if not present.
 
  private:
-  IPDF_Page* GetPage(int nIndex);
+  IPDF_Page* GetPage(int nIndex) const;
   void SendOnFocusChange(ObservedPtr<CPDFSDK_Annot>* pAnnot);
 
   UnownedPtr<FPDF_FORMFILLINFO> const m_pInfo;
diff --git a/fpdfsdk/cpdfsdk_interactiveform.cpp b/fpdfsdk/cpdfsdk_interactiveform.cpp
index a5dec66..289520d 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.cpp
+++ b/fpdfsdk/cpdfsdk_interactiveform.cpp
@@ -338,8 +338,8 @@
       continue;
 
     IPDF_Page* pPage = pWidget->GetPage();
-    FX_RECT rect = formfiller->GetViewBBox(
-        m_pFormFillEnv->GetPageView(pPage, false), pWidget);
+    FX_RECT rect =
+        formfiller->GetViewBBox(m_pFormFillEnv->GetPageView(pPage), pWidget);
     m_pFormFillEnv->Invalidate(pPage, rect);
   }
 }
diff --git a/fpdfsdk/formfiller/cffl_formfield.cpp b/fpdfsdk/formfiller/cffl_formfield.cpp
index 263e8ec..187ec43 100644
--- a/fpdfsdk/formfiller/cffl_formfield.cpp
+++ b/fpdfsdk/formfiller/cffl_formfield.cpp
@@ -258,8 +258,9 @@
 void CFFL_FormField::SetFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) {
   CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
   IPDF_Page* pPage = pWidget->GetPage();
-  CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, true);
-  if (CPWL_Wnd* pWnd = CreateOrUpdatePWLWindow(pPageView))
+  CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetOrCreatePageView(pPage);
+  CPWL_Wnd* pWnd = CreateOrUpdatePWLWindow(pPageView);
+  if (pWnd)
     pWnd->SetFocus();
 
   m_bValid = true;
@@ -271,7 +272,7 @@
     return;
 
   CPDFSDK_PageView* pPageView =
-      m_pFormFillEnv->GetPageView(m_pWidget->GetPage(), false);
+      m_pFormFillEnv->GetPageView(m_pWidget->GetPage());
   if (!pPageView || !CommitData(pPageView, nFlag))
     return;
   if (CPWL_Wnd* pWnd = GetPWLWindow(pPageView))
@@ -428,7 +429,7 @@
 
 CPDFSDK_PageView* CFFL_FormField::GetCurPageView() {
   IPDF_Page* pPage = m_pWidget->GetPage();
-  return m_pFormFillEnv->GetPageView(pPage, true);
+  return m_pFormFillEnv->GetOrCreatePageView(pPage);
 }
 
 CFX_FloatRect CFFL_FormField::GetFocusBox(CPDFSDK_PageView* pPageView) {
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index c612885..04b6606 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -169,7 +169,7 @@
 
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
-  return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
+  return pFormFillEnv ? pFormFillEnv->GetOrCreatePageView(pPage) : nullptr;
 }
 
 void FFLCommon(FPDF_FORMHANDLE hHandle,
@@ -646,16 +646,16 @@
     return false;
 
   CPDFSDK_PageView* page_view =
-      form_fill_env->GetPageView(annot_context->GetPage(), true);
+      form_fill_env->GetOrCreatePageView(annot_context->GetPage());
   if (!page_view->IsValid())
     return false;
 
   CPDF_Dictionary* annot_dict = annot_context->GetAnnotDict();
-
   ObservedPtr<CPDFSDK_Annot> cpdfsdk_annot(
       page_view->GetAnnotByDict(annot_dict));
   if (!cpdfsdk_annot)
     return false;
+
   return form_fill_env->SetFocusAnnot(&cpdfsdk_annot);
 }
 
@@ -735,7 +735,7 @@
   if (!pPage)
     return;
 
-  CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
+  CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage);
   if (pPageView) {
     pPageView->SetValid(false);
     // RemovePageView() takes care of the delete for us.
@@ -793,7 +793,7 @@
   if (!pPDFPage)
     return;
 
-  if (!pFormFillEnv->GetPageView(pPage, false))
+  if (!pFormFillEnv->GetPageView(pPage))
     return;
 
   CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHandler();
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 38011c5..6990b4e 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -330,7 +330,7 @@
     return;
 
   m_pContext->GetFormFillEnv()
-      ->GetPageView(pXFAPage.Get(), true)
+      ->GetOrCreatePageView(pXFAPage.Get())
       ->AddAnnot(hWidget);
 }
 
@@ -347,7 +347,7 @@
     return;
 
   CPDFSDK_PageView* pSdkPageView =
-      m_pContext->GetFormFillEnv()->GetPageView(pXFAPage.Get(), true);
+      m_pContext->GetFormFillEnv()->GetOrCreatePageView(pXFAPage.Get());
   CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget);
   if (pAnnot)
     pSdkPageView->DeleteAnnot(pAnnot);
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index ccfdc36..d84c11c 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -490,7 +490,7 @@
     // If there is currently no pageview associated with the page being used
     // do not create one. We may be in the process of tearing down the document
     // and creating a new pageview at this point will cause bad things.
-    CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
+    CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage);
     if (!pPageView)
       continue;
 
diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp
index b1ac511..5b5d525 100644
--- a/fxjs/cjs_field.cpp
+++ b/fxjs/cjs_field.cpp
@@ -2507,15 +2507,13 @@
     IPDF_Page* pPage = IPDFPageFromFPDFPage(m_pFormFillEnv->GetCurrentPage());
     if (!pPage)
       return CJS_Result::Failure(JSMessage::kBadObjectError);
-    if (CPDFSDK_PageView* pCurPageView =
-            m_pFormFillEnv->GetPageView(pPage, true)) {
-      for (int32_t i = 0; i < nCount; i++) {
-        if (CPDFSDK_Widget* pTempWidget =
-                pForm->GetWidget(pFormField->GetControl(i))) {
-          if (pTempWidget->GetPDFPage() == pCurPageView->GetPDFPage()) {
-            pWidget = pTempWidget;
-            break;
-          }
+    CPDFSDK_PageView* pCurPageView = m_pFormFillEnv->GetOrCreatePageView(pPage);
+    for (int32_t i = 0; i < nCount; i++) {
+      if (CPDFSDK_Widget* pTempWidget =
+              pForm->GetWidget(pFormField->GetControl(i))) {
+        if (pTempWidget->GetPDFPage() == pCurPageView->GetPDFPage()) {
+          pWidget = pTempWidget;
+          break;
         }
       }
     }