Reuse existing CPDF_InteractiveForm in two more APIs.

This is the same change as
https://pdfium-review.googlesource.com/c/50710, but for
FPDFAnnot_GetFormFieldFlags() and FPDFAnnot_GetFormFieldAtPoint().

This breaks API compatibility for FPDFAnnot_GetFormFieldFlags(), which
is marked experimental.

Change-Id: I50117a6ea0d5f22ec97787521fd0953a6700c017
Reviewed-on: https://pdfium-review.googlesource.com/c/50711
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 7e39154..46a7c1c 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -16,6 +16,7 @@
 #include "core/fpdfdoc/cpdf_annot.h"
 #include "core/fpdfdoc/cpdf_interactiveform.h"
 #include "core/fpdfdoc/cpdf_metadata.h"
+#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "public/fpdf_ext.h"
 
 #ifdef PDF_ENABLE_XFA
@@ -175,6 +176,12 @@
   return page ? IPDFPageFromFPDFPage(page)->AsPDFPage() : nullptr;
 }
 
+CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) {
+  CPDFSDK_FormFillEnvironment* pFormFillEnv =
+      CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
+  return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr;
+}
+
 ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
   return WideStringFromFPDFWideString(wide_string).ToUTF8();
 }
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index 31ffc9d..8f837f3 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -38,6 +38,7 @@
 class CPDF_TextPage;
 class CPDF_TextPageFind;
 class CPDFSDK_FormFillEnvironment;
+class CPDFSDK_InteractiveForm;
 class IPDFSDK_PauseAdapter;
 class FX_PATHPOINT;
 
@@ -206,6 +207,8 @@
   return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
 }
 
+CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
+
 ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
 
 WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 9517997..a9f1a78 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -25,6 +25,7 @@
 #include "core/fxge/cfx_color.h"
 #include "fpdfsdk/cpdf_annotcontext.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
+#include "fpdfsdk/cpdfsdk_interactiveform.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
 
@@ -868,7 +869,13 @@
 }
 
 FPDF_EXPORT int FPDF_CALLCONV
-FPDFAnnot_GetFormFieldFlags(FPDF_PAGE page, FPDF_ANNOTATION annot) {
+FPDFAnnot_GetFormFieldFlags(FPDF_FORMHANDLE hHandle,
+                            FPDF_PAGE page,
+                            FPDF_ANNOTATION annot) {
+  CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
+  if (!pForm)
+    return FPDF_FORMFLAG_NONE;
+
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !annot)
     return FPDF_FORMFLAG_NONE;
@@ -878,8 +885,8 @@
   if (!pAnnotDict)
     return FPDF_FORMFLAG_NONE;
 
-  CPDF_InteractiveForm interactive_form(pPage->GetDocument());
-  CPDF_FormField* pFormField = interactive_form.GetFieldByDict(pAnnotDict);
+  CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
+  CPDF_FormField* pFormField = pPDFForm->GetFieldByDict(pAnnotDict);
   return pFormField ? pFormField->GetFieldFlags() : FPDF_FORMFLAG_NONE;
 }
 
@@ -888,13 +895,17 @@
                               FPDF_PAGE page,
                               double page_x,
                               double page_y) {
-  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
-  if (!hHandle || !pPage)
+  CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
+  if (!pForm)
     return nullptr;
 
-  CPDF_InteractiveForm interactive_form(pPage->GetDocument());
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
+    return nullptr;
+
+  CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
   int annot_index = -1;
-  CPDF_FormControl* pFormCtrl = interactive_form.GetControlAtPoint(
+  CPDF_FormControl* pFormCtrl = pPDFForm->GetControlAtPoint(
       pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
       &annot_index);
   if (!pFormCtrl || annot_index == -1)
diff --git a/fpdfsdk/fpdf_annot_embeddertest.cpp b/fpdfsdk/fpdf_annot_embeddertest.cpp
index 185974b..d78e08e 100644
--- a/fpdfsdk/fpdf_annot_embeddertest.cpp
+++ b/fpdfsdk/fpdf_annot_embeddertest.cpp
@@ -1386,7 +1386,7 @@
     ASSERT_TRUE(annot);
 
     // Check that the flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
   }
 
@@ -1396,7 +1396,7 @@
     ASSERT_TRUE(annot);
 
     // Check that the flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
   }
 
@@ -1415,7 +1415,7 @@
     ASSERT_TRUE(annot);
 
     // Check that the flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
@@ -1427,7 +1427,7 @@
     ASSERT_TRUE(annot);
 
     // Check that the flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
@@ -1439,7 +1439,7 @@
     ASSERT_TRUE(annot);
 
     // Check that the flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
@@ -1485,7 +1485,7 @@
     ASSERT_TRUE(annot);
 
     // Check that interactive form annotation flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
   }
 
@@ -1496,7 +1496,7 @@
     ASSERT_TRUE(annot);
 
     // Check that interactive form annotation flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
   }
 
@@ -1516,7 +1516,7 @@
     ASSERT_TRUE(annot);
 
     // Check that interactive form annotation flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
@@ -1529,7 +1529,7 @@
     ASSERT_TRUE(annot);
 
     // Check that interactive form annotation flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
@@ -1542,7 +1542,7 @@
     ASSERT_TRUE(annot);
 
     // Check that interactive form annotation flag values are as expected.
-    int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
+    int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
     EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
     EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
     EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 41ce671..b84d906 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -159,12 +159,6 @@
 
 namespace {
 
-CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle) {
-  CPDFSDK_FormFillEnvironment* pFormFillEnv =
-      CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
-  return pFormFillEnv ? pFormFillEnv->GetInteractiveForm() : nullptr;
-}
-
 CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
                                        FPDF_PAGE fpdf_page) {
   IPDF_Page* pPage = IPDFPageFromFPDFPage(fpdf_page);
diff --git a/public/fpdf_annot.h b/public/fpdf_annot.h
index 8e9b93e..22d46fa 100644
--- a/public/fpdf_annot.h
+++ b/public/fpdf_annot.h
@@ -506,12 +506,16 @@
 // Get the annotation flags of |annot|, which is an interactive form
 // annotation in |page|.
 //
-//   page     - handle to a page.
-//   annot    - handle to an interactive form annotation.
+//    hHandle     -   handle to the form fill module, returned by
+//                    FPDFDOC_InitFormFillEnvironment().
+//    page        -   handle to a page.
+//    annot       -   handle to an interactive form annotation.
 //
 // Returns the annotation flags specific to interactive forms.
 FPDF_EXPORT int FPDF_CALLCONV
-FPDFAnnot_GetFormFieldFlags(FPDF_PAGE page, FPDF_ANNOTATION annot);
+FPDFAnnot_GetFormFieldFlags(FPDF_FORMHANDLE handle,
+                            FPDF_PAGE page,
+                            FPDF_ANNOTATION annot);
 
 // Experimental API.
 // Retrieves an interactive form annotation whose rectangle contains a given
@@ -520,7 +524,7 @@
 //
 //
 //    hHandle     -   handle to the form fill module, returned by
-//                    FPDFDOC_InitFormFillEnvironment.
+//                    FPDFDOC_InitFormFillEnvironment().
 //    page        -   handle to the page, returned by FPDF_LoadPage function.
 //    page_x      -   X position in PDF "user space".
 //    page_y      -   Y position in PDF "user space".