Improve ObserverPtr usage in CFFL_InteractiveFormFiller.

BUG=chromium:732322

Change-Id: I479f3edf48fcb2cac32d7fcb76651f9ad1246483
Reviewed-on: https://pdfium-review.googlesource.com/6553
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/formfiller/cffl_formfiller.cpp b/fpdfsdk/formfiller/cffl_formfiller.cpp
index cd53141..297d5b7 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_formfiller.cpp
@@ -514,13 +514,13 @@
     }
 
     SaveData(pPageView);
-    pFormFiller->OnCalculate(m_pWidget.Get(), pPageView, bExit, nFlag);
+    pFormFiller->OnCalculate(&pObserved, pPageView, bExit, nFlag);
     if (!pObserved)
       return false;
     if (bExit)
       return true;
 
-    pFormFiller->OnFormat(m_pWidget.Get(), pPageView, bExit, nFlag);
+    pFormFiller->OnFormat(&pObserved, pPageView, bExit, nFlag);
     if (!pObserved)
       return false;
     if (bExit)
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index 935e3ff..dca3194 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -249,7 +249,7 @@
     if (!pAnnot || bExit)
       return true;
 #ifdef PDF_ENABLE_XFA
-    OnClick(pWidget, pPageView, bReset, bExit, nFlags);
+    OnClick(pAnnot, pPageView, bReset, bExit, nFlags);
     if (!pAnnot || bExit)
       return true;
 #endif  // PDF_ENABLE_XFA
@@ -653,11 +653,12 @@
   }
 }
 
-void CFFL_InteractiveFormFiller::OnCalculate(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnCalculate(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                              CPDFSDK_PageView* pPageView,
                                              bool& bExit,
                                              uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     ASSERT(pWidget);
     CPDFSDK_InterForm* pInterForm = pPageView->GetFormFillEnv()->GetInterForm();
     pInterForm->OnCalculate(pWidget->GetFormField());
@@ -665,18 +666,20 @@
   }
 }
 
-void CFFL_InteractiveFormFiller::OnFormat(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnFormat(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                           CPDFSDK_PageView* pPageView,
                                           bool& bExit,
                                           uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     ASSERT(pWidget);
     CPDFSDK_InterForm* pInterForm = pPageView->GetFormFillEnv()->GetInterForm();
 
     bool bFormatted = false;
     CFX_WideString sValue =
         pInterForm->OnFormat(pWidget->GetFormField(), bFormatted);
-
+    if (!(*pAnnot))
+      return;
     if (bExit)
       return;
 
@@ -690,12 +693,13 @@
 }
 
 #ifdef PDF_ENABLE_XFA
-void CFFL_InteractiveFormFiller::OnClick(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnClick(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                          CPDFSDK_PageView* pPageView,
                                          bool& bReset,
                                          bool& bExit,
                                          uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     if (pWidget->HasXFAAAction(PDFSDK_XFA_Click)) {
       m_bNotifying = true;
       int nAge = pWidget->GetAppearanceAge();
@@ -707,6 +711,10 @@
 
       pWidget->OnXFAAAction(PDFSDK_XFA_Click, fa, pPageView);
       m_bNotifying = false;
+      if (!(*pAnnot)) {
+        bExit = true;
+        return;
+      }
 
       if (!IsValidAnnot(pPageView, pWidget)) {
         bExit = true;
@@ -725,12 +733,13 @@
   }
 }
 
-void CFFL_InteractiveFormFiller::OnFull(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnFull(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                         CPDFSDK_PageView* pPageView,
                                         bool& bReset,
                                         bool& bExit,
                                         uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     if (pWidget->HasXFAAAction(PDFSDK_XFA_Full)) {
       m_bNotifying = true;
       int nAge = pWidget->GetAppearanceAge();
@@ -742,6 +751,10 @@
 
       pWidget->OnXFAAAction(PDFSDK_XFA_Full, fa, pPageView);
       m_bNotifying = false;
+      if (!(*pAnnot)) {
+        bExit = true;
+        return;
+      }
 
       if (!IsValidAnnot(pPageView, pWidget)) {
         bExit = true;
@@ -769,8 +782,9 @@
 
   bool bTempReset = false;
   bool bTempExit = false;
-  OnPreOpen(pData->pWidget, pData->pPageView, bTempReset, bTempExit, nFlag);
-  if (bTempReset || bTempExit)
+  CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget);
+  OnPreOpen(&pObserved, pData->pPageView, bTempReset, bTempExit, nFlag);
+  if (!pObserved || bTempReset || bTempExit)
     bExit = true;
 }
 
@@ -783,17 +797,19 @@
 
   bool bTempReset = false;
   bool bTempExit = false;
-  OnPostOpen(pData->pWidget, pData->pPageView, bTempReset, bTempExit, nFlag);
-  if (bTempReset || bTempExit)
+  CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget);
+  OnPostOpen(&pObserved, pData->pPageView, bTempReset, bTempExit, nFlag);
+  if (!pObserved || bTempReset || bTempExit)
     bExit = true;
 }
 
-void CFFL_InteractiveFormFiller::OnPreOpen(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnPreOpen(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                            CPDFSDK_PageView* pPageView,
                                            bool& bReset,
                                            bool& bExit,
                                            uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     if (pWidget->HasXFAAAction(PDFSDK_XFA_PreOpen)) {
       m_bNotifying = true;
       int nAge = pWidget->GetAppearanceAge();
@@ -805,6 +821,10 @@
 
       pWidget->OnXFAAAction(PDFSDK_XFA_PreOpen, fa, pPageView);
       m_bNotifying = false;
+      if (!(*pAnnot)) {
+        bExit = true;
+        return;
+      }
 
       if (!IsValidAnnot(pPageView, pWidget)) {
         bExit = true;
@@ -823,12 +843,13 @@
   }
 }
 
-void CFFL_InteractiveFormFiller::OnPostOpen(CPDFSDK_Widget* pWidget,
+void CFFL_InteractiveFormFiller::OnPostOpen(CPDFSDK_Annot::ObservedPtr* pAnnot,
                                             CPDFSDK_PageView* pPageView,
                                             bool& bReset,
                                             bool& bExit,
                                             uint32_t nFlag) {
   if (!m_bNotifying) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot->Get());
     if (pWidget->HasXFAAAction(PDFSDK_XFA_PostOpen)) {
       m_bNotifying = true;
       int nAge = pWidget->GetAppearanceAge();
@@ -840,6 +861,10 @@
 
       pWidget->OnXFAAAction(PDFSDK_XFA_PostOpen, fa, pPageView);
       m_bNotifying = false;
+      if (!(*pAnnot)) {
+        bExit = true;
+        return;
+      }
 
       if (!IsValidAnnot(pPageView, pWidget)) {
         bExit = true;
@@ -883,9 +908,9 @@
   if (pFormFiller->IsFieldFull(pData->pPageView)) {
     bool bFullExit = false;
     bool bFullReset = false;
-    OnFull(pData->pWidget, pData->pPageView, bFullReset, bFullExit, nFlag);
-
-    if (bFullReset || bFullExit) {
+    CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget);
+    OnFull(&pObserved, pData->pPageView, bFullReset, bFullExit, nFlag);
+    if (!pObserved || bFullReset || bFullExit) {
       bExit = true;
       return;
     }
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.h b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
index d84b8b8..f72dfbc 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.h
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
@@ -92,42 +92,42 @@
                          bool& bRC,
                          bool& bExit,
                          uint32_t nFlag);
-  void OnValidate(CPDFSDK_Annot::ObservedPtr* pWidget,
+  void OnValidate(CPDFSDK_Annot::ObservedPtr* pAnnot,
                   CPDFSDK_PageView* pPageView,
                   bool& bRC,
                   bool& bExit,
                   uint32_t nFlag);
 
-  void OnCalculate(CPDFSDK_Widget* pWidget,
+  void OnCalculate(CPDFSDK_Annot::ObservedPtr* pAnnot,
                    CPDFSDK_PageView* pPageView,
                    bool& bExit,
                    uint32_t nFlag);
-  void OnFormat(CPDFSDK_Widget* pWidget,
+  void OnFormat(CPDFSDK_Annot::ObservedPtr* pAnnot,
                 CPDFSDK_PageView* pPageView,
                 bool& bExit,
                 uint32_t nFlag);
-  void OnButtonUp(CPDFSDK_Annot::ObservedPtr* pWidget,
+  void OnButtonUp(CPDFSDK_Annot::ObservedPtr* pAnnot,
                   CPDFSDK_PageView* pPageView,
                   bool& bReset,
                   bool& bExit,
                   uint32_t nFlag);
 #ifdef PDF_ENABLE_XFA
-  void OnClick(CPDFSDK_Widget* pWidget,
+  void OnClick(CPDFSDK_Annot::ObservedPtr* pAnnot,
                CPDFSDK_PageView* pPageView,
                bool& bReset,
                bool& bExit,
                uint32_t nFlag);
-  void OnFull(CPDFSDK_Widget* pWidget,
+  void OnFull(CPDFSDK_Annot::ObservedPtr* pAnnot,
               CPDFSDK_PageView* pPageView,
               bool& bReset,
               bool& bExit,
               uint32_t nFlag);
-  void OnPreOpen(CPDFSDK_Widget* pWidget,
+  void OnPreOpen(CPDFSDK_Annot::ObservedPtr* pAnnot,
                  CPDFSDK_PageView* pPageView,
                  bool& bReset,
                  bool& bExit,
                  uint32_t nFlag);
-  void OnPostOpen(CPDFSDK_Widget* pWidget,
+  void OnPostOpen(CPDFSDK_Annot::ObservedPtr* pAnnot,
                   CPDFSDK_PageView* pPageView,
                   bool& bReset,
                   bool& bExit,