Reland of Remove pageview from map immediately

This reverts commit f2cee9894b9f7cf2e50060965ad1eedd90ab55b6.

This CL removes the default parameter from the CPDFSDK_Document::GetPageView
|ReNew| flag and updates the code as needed. In
CFFL_FormFillter::KillFocusForAnnot we flip the flag to |FALSE| as we don't want
to re-create the page view if it is already removed. If we don't do this then
the page view will be re-created in the map, the page associated to the page
view, but then the page can be deleted out from under the pageview as it isn't
owned by the page view.

BUG=chromium:630654

Review-Url: https://codereview.chromium.org/2179163004
diff --git a/fpdfsdk/formfiller/cffl_combobox.cpp b/fpdfsdk/formfiller/cffl_combobox.cpp
index 4b84733..d66977a 100644
--- a/fpdfsdk/formfiller/cffl_combobox.cpp
+++ b/fpdfsdk/formfiller/cffl_combobox.cpp
@@ -274,7 +274,7 @@
   CFX_WideString swRet;
 
   int nExport = -1;
-  CPDFSDK_PageView* pPageView = GetCurPageView();
+  CPDFSDK_PageView* pPageView = GetCurPageView(true);
   if (CPWL_ComboBox* pComboBox =
           (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
     nExport = pComboBox->GetSelect();
diff --git a/fpdfsdk/formfiller/cffl_formfiller.cpp b/fpdfsdk/formfiller/cffl_formfiller.cpp
index d7bc958..479a1bc 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_formfiller.cpp
@@ -220,7 +220,7 @@
                                    FX_UINT nKeyCode,
                                    FX_UINT nFlags) {
   if (IsValid()) {
-    CPDFSDK_PageView* pPageView = GetCurPageView();
+    CPDFSDK_PageView* pPageView = GetCurPageView(true);
     ASSERT(pPageView);
 
     if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, FALSE)) {
@@ -235,7 +235,7 @@
                                 FX_UINT nChar,
                                 FX_UINT nFlags) {
   if (IsValid()) {
-    CPDFSDK_PageView* pPageView = GetCurPageView();
+    CPDFSDK_PageView* pPageView = GetCurPageView(true);
     ASSERT(pPageView);
 
     if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, FALSE)) {
@@ -250,7 +250,7 @@
   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
   UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
   CPDFSDK_Document* pDoc = m_pApp->GetSDKDocument();
-  CPDFSDK_PageView* pPageView = pDoc->GetPageView(pPage);
+  CPDFSDK_PageView* pPageView = pDoc->GetPageView(pPage, true);
   if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, TRUE))
     pWnd->SetFocus();
 
@@ -263,7 +263,7 @@
   if (!IsValid())
     return;
 
-  CPDFSDK_PageView* pPageView = GetCurPageView();
+  CPDFSDK_PageView* pPageView = GetCurPageView(false);
   if (!pPageView)
     return;
 
@@ -456,10 +456,10 @@
   return CFX_FloatRect(0, 0, fWidth, fHeight);
 }
 
-CPDFSDK_PageView* CFFL_FormFiller::GetCurPageView() {
+CPDFSDK_PageView* CFFL_FormFiller::GetCurPageView(bool renew) {
   UnderlyingPageType* pPage = m_pAnnot->GetUnderlyingPage();
   CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument();
-  return pSDKDoc ? pSDKDoc->GetPageView(pPage) : nullptr;
+  return pSDKDoc ? pSDKDoc->GetPageView(pPage, renew) : nullptr;
 }
 
 CFX_FloatRect CFFL_FormFiller::GetFocusBox(CPDFSDK_PageView* pPageView) {
diff --git a/fpdfsdk/formfiller/cffl_formfiller.h b/fpdfsdk/formfiller/cffl_formfiller.h
index 7360957..7ae724d 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.h
+++ b/fpdfsdk/formfiller/cffl_formfiller.h
@@ -141,7 +141,7 @@
   FX_BOOL IsValid() const;
   CFX_FloatRect GetPDFWindowRect() const;
 
-  CPDFSDK_PageView* GetCurPageView();
+  CPDFSDK_PageView* GetCurPageView(bool renew);
   void SetChangeMark();
 
   virtual void InvalidateRect(double left,
diff --git a/fpdfsdk/formfiller/cffl_textfield.cpp b/fpdfsdk/formfiller/cffl_textfield.cpp
index 5e69031..d24452f 100644
--- a/fpdfsdk/formfiller/cffl_textfield.cpp
+++ b/fpdfsdk/formfiller/cffl_textfield.cpp
@@ -106,7 +106,7 @@
   switch (nChar) {
     case FWL_VKEY_Return:
       if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)) {
-        CPDFSDK_PageView* pPageView = GetCurPageView();
+        CPDFSDK_PageView* pPageView = GetCurPageView(true);
         ASSERT(pPageView);
         m_bValid = !m_bValid;
         CFX_FloatRect rcAnnot = pAnnot->GetRect();
@@ -126,7 +126,7 @@
       }
       break;
     case FWL_VKEY_Escape: {
-      CPDFSDK_PageView* pPageView = GetCurPageView();
+      CPDFSDK_PageView* pPageView = GetCurPageView(true);
       ASSERT(pPageView);
       EscapeFiller(pPageView, TRUE);
       return TRUE;
diff --git a/fpdfsdk/fpdfformfill.cpp b/fpdfsdk/fpdfformfill.cpp
index 60e9e52..35f53c4 100644
--- a/fpdfsdk/fpdfformfill.cpp
+++ b/fpdfsdk/fpdfformfill.cpp
@@ -41,7 +41,7 @@
     return nullptr;
 
   CPDFSDK_Document* pSDKDoc = CPDFSDK_Document::FromFPDFFormHandle(hHandle);
-  return pSDKDoc ? pSDKDoc->GetPageView(pPage, TRUE) : nullptr;
+  return pSDKDoc ? pSDKDoc->GetPageView(pPage, true) : nullptr;
 }
 
 #ifdef PDF_ENABLE_XFA
@@ -131,7 +131,7 @@
   options.m_AddFlags = flags >> 8;
   options.m_pOCContext = new CPDF_OCContext(pPDFDoc, CPDF_OCContext::View);
 
-  if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView(pPage))
+  if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView(pPage, true))
     pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
 #endif  // PDF_ENABLE_XFA
 
@@ -655,7 +655,7 @@
   if (!pPage)
     return;
 
-  CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
+  CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, false);
   if (pPageView) {
     pPageView->SetValid(FALSE);
     // RemovePageView() takes care of the delete for us.
@@ -706,7 +706,7 @@
   CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
   if (!pPDFPage)
     return;
-  if (pSDKDoc->GetPageView(pPage, FALSE)) {
+  if (pSDKDoc->GetPageView(pPage, false)) {
     CPDFDoc_Environment* pEnv = pSDKDoc->GetEnv();
     CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
     CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict;
diff --git a/fpdfsdk/fpdfxfa/fpdfxfa_doc.cpp b/fpdfsdk/fpdfxfa/fpdfxfa_doc.cpp
index 42dad45..95f0cbe 100644
--- a/fpdfsdk/fpdfxfa/fpdfxfa_doc.cpp
+++ b/fpdfsdk/fpdfxfa/fpdfxfa_doc.cpp
@@ -503,7 +503,7 @@
   if (!pXFAPage)
     return;
 
-  m_pSDKDoc->GetPageView(pXFAPage)->AddAnnot(hWidget);
+  m_pSDKDoc->GetPageView(pXFAPage, true)->AddAnnot(hWidget);
 }
 
 void CPDFXFA_Document::WidgetPreRemove(CXFA_FFWidget* hWidget,
@@ -519,7 +519,7 @@
   if (!pXFAPage)
     return;
 
-  CPDFSDK_PageView* pSdkPageView = m_pSDKDoc->GetPageView(pXFAPage);
+  CPDFSDK_PageView* pSdkPageView = m_pSDKDoc->GetPageView(pXFAPage, true);
   if (CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget))
     pSdkPageView->DeleteAnnot(pAnnot);
 }
diff --git a/fpdfsdk/fsdk_baseform.cpp b/fpdfsdk/fsdk_baseform.cpp
index 05b4dd0..afbd958 100644
--- a/fpdfsdk/fsdk_baseform.cpp
+++ b/fpdfsdk/fsdk_baseform.cpp
@@ -2278,7 +2278,7 @@
       CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
       CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
       UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
-      CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
+      CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false);
       FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
 
       pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
diff --git a/fpdfsdk/fsdk_mgr.cpp b/fpdfsdk/fsdk_mgr.cpp
index 68143d2..8587de1 100644
--- a/fpdfsdk/fsdk_mgr.cpp
+++ b/fpdfsdk/fsdk_mgr.cpp
@@ -263,7 +263,7 @@
 
 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
     UnderlyingPageType* pUnderlyingPage,
-    FX_BOOL ReNew) {
+    bool ReNew) {
   auto it = m_pageMap.find(pUnderlyingPage);
   if (it != m_pageMap.end())
     return it->second;
@@ -281,7 +281,7 @@
 CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
   UnderlyingPageType* pPage =
       UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
-  return pPage ? GetPageView(pPage, TRUE) : nullptr;
+  return pPage ? GetPageView(pPage, true) : nullptr;
 }
 
 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
@@ -353,9 +353,12 @@
   if (pPageView->IsLocked())
     return;
 
+  // Remove the page from the map to make sure we don't accidentally attempt
+  // to use the |pPageView| while we're cleaning it up.
+  m_pageMap.erase(it);
+
   pPageView->KillFocusAnnotIfNeeded();
   delete pPageView;
-  m_pageMap.erase(it);
 }
 
 UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h
index 84dea23..16aef4a 100644
--- a/fpdfsdk/include/fsdk_mgr.h
+++ b/fpdfsdk/include/fsdk_mgr.h
@@ -479,8 +479,7 @@
   int GetPageViewCount() const { return m_pageMap.size(); }
 #endif  // PDF_ENABLE_XFA
 
-  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage,
-                                FX_BOOL ReNew = TRUE);
+  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage, bool ReNew);
   CPDFSDK_PageView* GetPageView(int nIndex);
   CPDFSDK_PageView* GetCurrentView();
   void RemovePageView(UnderlyingPageType* pPage);
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index caaeba8..ace805f 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -492,7 +492,7 @@
     UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
     ASSERT(pPage);
 
-    CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage);
+    CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, true);
     pPageView->DeleteAnnot(pWidget);
     pPageView->UpdateRects(aRefresh);
   }
diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp
index ee91032..9b109f7 100644
--- a/fpdfsdk/javascript/Field.cpp
+++ b/fpdfsdk/javascript/Field.cpp
@@ -3247,7 +3247,8 @@
         pEnv->FFI_GetCurrentPage(m_pDocument->GetUnderlyingDocument()));
     if (!pPage)
       return FALSE;
-    if (CPDFSDK_PageView* pCurPageView = m_pDocument->GetPageView(pPage)) {
+    if (CPDFSDK_PageView* pCurPageView =
+            m_pDocument->GetPageView(pPage, true)) {
       for (int32_t i = 0; i < nCount; i++) {
         if (CPDFSDK_Widget* pTempWidget =
                 pInterForm->GetWidget(pFormField->GetControl(i))) {