XFA: add CPDFDocumentToFPDFDocument()

Abstracts away that master returns CPDF_Documents directly,
but XFA wraps them with a CPDFXFA_Document.

Tidy dead code along the way.

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1415803002 .
diff --git a/fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h b/fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h
index 3c33338..0564ed0 100644
--- a/fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h
+++ b/fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h
@@ -215,14 +215,14 @@
     }

   }

 

+  int m_iDocType;

   CPDF_Document* m_pPDFDoc;

+  CPDFSDK_Document* m_pSDKDoc;

   IXFA_Doc* m_pXFADoc;

   IXFA_DocView* m_pXFADocView;

-  CFX_ArrayTemplate<CPDFXFA_Page*> m_XFAPageList;

-  CPDFSDK_Document* m_pSDKDoc;

   CPDFXFA_App* m_pApp;

   IJS_Context* m_pJSContext;

-  int m_iDocType;

+  CFX_ArrayTemplate<CPDFXFA_Page*> m_XFAPageList;

 };

 

 #endif  // FPDFXFA_DOC_H_

diff --git a/fpdfsdk/include/fsdk_baseform.h b/fpdfsdk/include/fsdk_baseform.h
index da57430..07d21bb 100644
--- a/fpdfsdk/include/fsdk_baseform.h
+++ b/fpdfsdk/include/fsdk_baseform.h
@@ -66,9 +66,9 @@
 
 class CPDFSDK_Widget : public CPDFSDK_BAAnnot {
  public:
-  IXFA_Widget* GetMixXFAWidget();
+  IXFA_Widget* GetMixXFAWidget() const;
   IXFA_Widget* GetGroupMixXFAWidget();
-  IXFA_WidgetHandler* GetXFAWidgetHandler();
+  IXFA_WidgetHandler* GetXFAWidgetHandler() const;
 
   FX_BOOL HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT);
   FX_BOOL OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
@@ -114,14 +114,14 @@
   FX_BOOL GetTextColor(FX_COLORREF& color) const;
   FX_FLOAT GetFontSize() const;
 
-  int GetSelectedIndex(int nIndex);
-  CFX_WideString GetValue(FX_BOOL bDisplay = TRUE);
+  int GetSelectedIndex(int nIndex) const;
+  CFX_WideString GetValue(FX_BOOL bDisplay = TRUE) const;
   CFX_WideString GetDefaultValue() const;
   CFX_WideString GetOptionLabel(int nIndex) const;
   int CountOptions() const;
-  FX_BOOL IsOptionSelected(int nIndex);
+  FX_BOOL IsOptionSelected(int nIndex) const;
   int GetTopVisibleIndex() const;
-  FX_BOOL IsChecked();
+  FX_BOOL IsChecked() const;
   /*
   BF_ALIGN_LEFT
   BF_ALIGN_MIDDL
@@ -129,7 +129,7 @@
   */
   int GetAlignment() const;
   int GetMaxLen() const;
-  CFX_WideString GetName();
+  CFX_WideString GetName() const;
   CFX_WideString GetAlternateName() const;
 
   // Set Properties.
@@ -198,14 +198,13 @@
  public:
   FX_BOOL HitTest(FX_FLOAT pageX, FX_FLOAT pageY);
 
- private:
   CPDFSDK_InterForm* m_pInterForm;
   FX_BOOL m_bAppModified;
   int32_t m_nAppAge;
   int32_t m_nValueAge;
 
-  IXFA_Widget* m_hMixXFAWidget;
-  IXFA_WidgetHandler* m_pWidgetHandler;
+  mutable IXFA_Widget* m_hMixXFAWidget;
+  mutable IXFA_WidgetHandler* m_pWidgetHandler;
 };
 
 class CPDFSDK_XFAWidget : public CPDFSDK_Annot {
diff --git a/fpdfsdk/include/fsdk_define.h b/fpdfsdk/include/fsdk_define.h
index e001e75..2faaf01 100644
--- a/fpdfsdk/include/fsdk_define.h
+++ b/fpdfsdk/include/fsdk_define.h
@@ -87,8 +87,10 @@
   FX_FILESIZE m_nCurPos;
 };
 
-// Conversions from FPDF_ types.
+// Conversions to/from FPDF_ types.
 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
+FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc);
+
 CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page);
 
 void DropContext(void* data);
diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h
index 4c0ce1e..300ca22 100644
--- a/fpdfsdk/include/fsdk_mgr.h
+++ b/fpdfsdk/include/fsdk_mgr.h
@@ -475,7 +475,10 @@
   ~CPDFSDK_Document();
 
   CPDFSDK_InterForm* GetInterForm();
-  CPDFXFA_Document* GetDocument() { return m_pDoc; }
+  CPDFXFA_Document* GetDocument() const { return m_pDoc; }
+  CPDF_Document* GetPDFDocument() const {
+    return m_pDoc ? m_pDoc->GetPDFDoc() : nullptr;
+  }
 
   int GetPageViewCount() const { return m_pageMap.size(); }
   CPDFSDK_PageView* GetPageView(CPDFXFA_Page* pPDFXFAPage,
diff --git a/fpdfsdk/src/fpdf_dataavail.cpp b/fpdfsdk/src/fpdf_dataavail.cpp
index f09b173..0a90f49 100644
--- a/fpdfsdk/src/fpdf_dataavail.cpp
+++ b/fpdfsdk/src/fpdf_dataavail.cpp
@@ -113,9 +113,7 @@
   }
   ((CFPDF_DataAvail*)avail)->m_pDataAvail->SetDocument(pParser->GetDocument());
   CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS);
-  CPDF_Document* pPDFDoc = pParser->GetDocument();
-  CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
-  return new CPDFXFA_Document(pPDFDoc, pApp);
+  return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
 }
 
 DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) {
diff --git a/fpdfsdk/src/fpdfeditimg.cpp b/fpdfsdk/src/fpdfeditimg.cpp
index cd4618e..73fc251 100644
--- a/fpdfsdk/src/fpdfeditimg.cpp
+++ b/fpdfsdk/src/fpdfeditimg.cpp
@@ -6,8 +6,6 @@
 
 #include "../../public/fpdf_edit.h"
 #include "../include/fsdk_define.h"
-#include "../include/fpdfxfa/fpdfxfa_doc.h"
-#include "../include/fpdfxfa/fpdfxfa_page.h"
 
 DLLEXPORT FPDF_PAGEOBJECT STDCALL
 FPDFPageObj_NewImgeObj(FPDF_DOCUMENT document) {
diff --git a/fpdfsdk/src/fpdfeditpage.cpp b/fpdfsdk/src/fpdfeditpage.cpp
index 42ab3eb..8b4168f 100644
--- a/fpdfsdk/src/fpdfeditpage.cpp
+++ b/fpdfsdk/src/fpdfeditpage.cpp
@@ -43,8 +43,7 @@
     pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
   }
 
-  CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
-  return new CPDFXFA_Document(pDoc, pApp);
+  return FPDFDocumentFromCPDFDocument(pDoc);
 }
 
 DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
@@ -177,7 +176,7 @@
 }
 
 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp
index e58c536..bd7741d 100644
--- a/fpdfsdk/src/fpdfview.cpp
+++ b/fpdfsdk/src/fpdfview.cpp
@@ -26,6 +26,10 @@
   return doc ? static_cast<CPDFXFA_Document*>(doc)->GetPDFDoc() : nullptr;
 }
 
+FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
+  return doc ? new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()) : nullptr;
+}
+
 CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
   return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
 }
@@ -351,12 +355,7 @@
   CPDF_Document* pDoc = NULL;
   pDoc = pParser ? pParser->GetDocument() : NULL;
   CheckUnSupportError(pDoc, err_code);
-  CPDF_Document* pPDFDoc = pParser->GetDocument();
-  if (!pPDFDoc)
-    return NULL;
-
-  CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
-  return new CPDFXFA_Document(pPDFDoc, pProvider);
+  return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
 }
 
 DLLEXPORT FPDF_DOCUMENT STDCALL
@@ -374,12 +373,7 @@
   CPDF_Document* pDoc = NULL;
   pDoc = pParser ? pParser->GetDocument() : NULL;
   CheckUnSupportError(pDoc, err_code);
-  CPDF_Document* pPDFDoc = pParser->GetDocument();
-  if (!pPDFDoc)
-    return NULL;
-
-  CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
-  return new CPDFXFA_Document(pPDFDoc, pProvider);
+  return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
@@ -437,15 +431,13 @@
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
-  if (!page)
-    return 0.0;
-  return ((CPDFXFA_Page*)page)->GetPageWidth();
+  CPDFXFA_Page* pPage = static_cast<CPDFXFA_Page*>(page);
+  return pPage ? pPage->GetPageWidth() : 0.0;
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
-  if (!page)
-    return 0.0;
-  return ((CPDFXFA_Page*)page)->GetPageHeight();
+  CPDFXFA_Page* pPage = static_cast<CPDFXFA_Page*>(page);
+  return pPage ? pPage->GetPageHeight() : 0.0;
 }
 
 void DropContext(void* data) {
@@ -1018,8 +1010,8 @@
     *buflen = 0;
   if (!document || index < 0)
     return NULL;
-  CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
 
+  CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
   CPDF_Dictionary* pRoot = pDoc->GetRoot();
   if (!pRoot)
     return NULL;
diff --git a/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp b/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
index 365d5d7..3763003 100644
--- a/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
+++ b/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
@@ -35,14 +35,13 @@
 

 CPDFXFA_Document::CPDFXFA_Document(CPDF_Document* pPDFDoc,

                                    CPDFXFA_App* pProvider)

-    : m_pPDFDoc(pPDFDoc),

+    : m_iDocType(DOCTYPE_PDF),

+      m_pPDFDoc(pPDFDoc),

+      m_pSDKDoc(nullptr),

+      m_pXFADoc(nullptr),

+      m_pXFADocView(nullptr),

       m_pApp(pProvider),

-      m_pXFADoc(NULL),

-      m_pXFADocView(NULL),

-      m_iDocType(DOCTYPE_PDF),

-      m_pJSContext(NULL),

-      m_pSDKDoc(NULL) {

-  m_XFAPageList.RemoveAll();

+      m_pJSContext(nullptr) {

 }

 

 CPDFXFA_Document::~CPDFXFA_Document() {

@@ -492,24 +491,6 @@
 

 void CPDFXFA_Document::PageViewEvent(IXFA_PageView* pPageView,

                                      FX_DWORD dwFlags) {

-  if (m_iDocType != DOCTYPE_DYNIMIC_XFA)

-    return;

-

-  CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();

-  if (pEnv == NULL)

-    return;

-

-  CPDFXFA_Page* pPage = GetPage(pPageView);

-  if (pPage == NULL)

-    return;

-

-  if (dwFlags == FXFA_PAGEVIEWEVENT_POSTADDED) {

-    // pEnv->FFI_PageEvent(pPage, FXFA_PAGEVIEWEVENT_POSTADDED);

-  } else if (dwFlags == FXFA_PAGEVIEWEVENT_POSTREMOVED) {

-    // pEnv->FFI_PageEvent(pPage, FXFA_PAGEVIEWEVENT_POSTREMOVED);

-    // RemovePage(pPage);

-    // delete pPage;

-  }

 }

 

 void CPDFXFA_Document::WidgetEvent(IXFA_Widget* hWidget,

@@ -517,60 +498,30 @@
                                    FX_DWORD dwEvent,

                                    void* pParam,

                                    void* pAdditional) {

-  if (m_iDocType != DOCTYPE_DYNIMIC_XFA || NULL == hWidget)

+  if (m_iDocType != DOCTYPE_DYNIMIC_XFA || !hWidget)

     return;

 

   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();

-  if (pEnv == NULL)

-    return;

-

-  if (NULL == hWidget)

+  if (!pEnv)

     return;

 

   IXFA_PageView* pPageView =

       m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);

-

   if (pPageView == NULL)

     return;

+

   CPDFXFA_Page* pXFAPage = GetPage(pPageView);

   if (pXFAPage == NULL)

     return;

 

   CPDFSDK_PageView* pSdkPageView = m_pSDKDoc->GetPageView(pXFAPage);

-

-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();

-

   if (dwEvent == XFA_WIDGETEVENT_PostAdded) {

-    // 			CPDFSDK_Annot* pAnnot =

-    // pAnnotHandlerMgr->NewAnnot(hWidget,

-    // pSdkPageView);

-    // 			pAnnotHandlerMgr->Annot_OnLoad(pAnnot);

-

-    // pEnv->FFI_WidgetEvent(hWidget, XFA_WIDGETEVENT_PostAdded);

-    // 		IXFA_PageView* pOldPageView = (IXFA_PageView*)pAdditional;

-    // 		if (pOldPageView)

-    // 		{

-    // 			CPDFXFA_Page* pDestPage =

-    // m_pSDKDoc->GetPageView((IXFA_PageView*)pOldPageView);

-    // 			ASSERT(pDestPage);

-    // 			CPDFSDK_Annot* pAnnot =

-    // pDestPage->GetAnnotByXFAWidget(hWidget);

-    // 			if (pAnnot)

-    // 			{

-    // 				if (m_pSDKDoc->GetFocusAnnot() == pAnnot)

-    // 				{

-    // 					m_pSDKDoc->SetFocusAnnot(NULL);

-    // 				}

-    // 				pDestPage->DeleteAnnot(pAnnot);

-    // 			}

-    // 		}

     pSdkPageView->AddAnnot(hWidget);

 

   } else if (dwEvent == XFA_WIDGETEVENT_PreRemoved) {

     CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget);

     if (pAnnot) {

       pSdkPageView->DeleteAnnot(pAnnot);

-      // pEnv->FFI_WidgetEvent(hWidget, XFA_WIDGETEVENT_PreRemoved);

     }

   }

 }

@@ -686,32 +637,9 @@
                          content.GetLength());

     CFX_WideStringC data(L"data");

     if (pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), data, &fileWrite)) {

-      NULL;

+      // TODO: Maybe report error.

     }

-  }

-  /*else if (fileType == FXFA_FILE_STATIC_XDP)

-  {

-          content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";

-          fileWrite.WriteBlock((const FX_CHAR*)content, fileWrite.GetSize(),

-content.GetLength());

-          CFX_WideStringC data(L"data");

-          if( pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), data,

-&fileWrite))

-          {

-                  NULL;

-          }

-          CFX_WideString wPath = pEnv->FFI_GetFilePath(pFileHandler);

-// 		CFX_WideString wPath;

-// 		wPath.FromUTF16LE(filePath);

-          CFX_ByteString bPath = wPath.UTF8Encode();

-          CFX_ByteString szFormat = "\n<pdf href=\"%s\"

-xmlns=\"http://ns.adobe.com/xdp/pdf/\"/>";

-          content.Format(szFormat,(char*)(const FX_CHAR*)bPath);

-          fileWrite.WriteBlock((const FX_CHAR*)content,fileWrite.GetSize(),

-content.GetLength());

-  }

-  */

-  else if (fileType == FXFA_SAVEAS_XDP) {

+  } else if (fileType == FXFA_SAVEAS_XDP) {

     if (m_pPDFDoc == NULL)

       return;

     CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot();

@@ -729,8 +657,6 @@
     if (NULL == pArray)

       return;

     int size = pArray->GetCount();

-    int iFormIndex = -1;

-    int iDataSetsIndex = -1;

     for (int i = 1; i < size; i += 2) {

       CPDF_Object* pPDFObj = pArray->GetElement(i);

       CPDF_Object* pPrePDFObj = pArray->GetElement(i - 1);

@@ -750,7 +676,6 @@
                                    &fileWrite);

       } else {

         if (i == size - 1) {

-          // CFX_WideString wPath = pEnv->FFI_GetFilePath(pFileHandler);

           CFX_WideString wPath = CFX_WideString::FromUTF16LE(

               (unsigned short*)(const FX_CHAR*)bs,

               bs.GetLength() / sizeof(unsigned short));

@@ -771,7 +696,9 @@
       }

     }

   }

-  FX_BOOL bError = fileWrite.Flush();

+  if (!fileWrite.Flush()) {

+    // TODO: Report error.

+  }

 }

 void CPDFXFA_Document::ImportData(IXFA_Doc* hDoc,

                                   const CFX_WideStringC& wsFilePath) {

@@ -1051,8 +978,6 @@
       return FALSE;

     }

     int size = pArray->GetCount();

-    int iFormIndex = -1;

-    int iDataSetsIndex = -1;

     for (int i = 1; i < size; i += 2) {

       CPDF_Object* pPDFObj = pArray->GetElement(i);

       CPDF_Object* pPrePDFObj = pArray->GetElement(i - 1);

diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp
index 68c320b..024287d 100644
--- a/fpdfsdk/src/fsdk_baseform.cpp
+++ b/fpdfsdk/src/fsdk_baseform.cpp
@@ -39,15 +39,9 @@
 
 CPDFSDK_Widget::~CPDFSDK_Widget() {}
 
-IXFA_Widget* CPDFSDK_Widget::GetMixXFAWidget() {
-  ASSERT(m_pPageView != NULL);
-
+IXFA_Widget* CPDFSDK_Widget::GetMixXFAWidget() const {
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
   CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_hMixXFAWidget) {
       if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
@@ -90,15 +84,9 @@
   return NULL;
 }
 
-IXFA_WidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() {
-  ASSERT(m_pPageView != NULL);
-
+IXFA_WidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
   CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_pWidgetHandler) {
       if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
@@ -343,16 +331,10 @@
   if (!pXFADocView)
     return;
 
-  if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
-    if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
-      CPDF_FormField* pFormField = GetFormField();
-      ASSERT(pFormField != NULL);
-
-      CPDF_FormControl* pFormCtrl = GetFormControl();
-      ASSERT(pFormCtrl != NULL);
-
-      CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, pFormField,
-                                          pFormCtrl);
+  if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
+    if (GetXFAWidgetHandler()) {
+      CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, GetFormField(),
+                                          GetFormControl());
     }
   }
 #endif  // PDF_ENABLE_XFA
@@ -372,13 +354,9 @@
   if (!pXFADocView)
     return;
 
-  if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
-    if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
-      CPDF_FormField* pFormField = GetFormField();
-      ASSERT(pFormField != NULL);
-
-      SynchronizeXFAItems(pXFADocView, hWidget, pFormField, NULL);
-    }
+  if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
+    if (GetXFAWidgetHandler())
+      SynchronizeXFAItems(pXFADocView, hWidget, GetFormField(), nullptr);
   }
 #endif  // PDF_ENABLE_XFA
 }
@@ -622,12 +600,8 @@
   return pCtrl->GetRotation() % 360;
 }
 
-CFX_WideString CPDFSDK_Widget::GetName() {
-  ASSERT(m_pInterForm != NULL);
-
+CFX_WideString CPDFSDK_Widget::GetName() const {
   CPDF_FormField* pFormField = GetFormField();
-  ASSERT(pFormField != NULL);
-
   return pFormField->GetFullName();
 }
 
@@ -680,7 +654,7 @@
   return fFontSize;
 }
 
-int CPDFSDK_Widget::GetSelectedIndex(int nIndex) {
+int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
 #ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
@@ -696,7 +670,7 @@
   return pFormField->GetSelectedIndex(nIndex);
 }
 
-CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) {
+CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) const {
 #ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
@@ -735,7 +709,7 @@
   return pFormField->CountOptions();
 }
 
-FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) {
+FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
 #ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
@@ -758,7 +732,7 @@
   return pFormField->GetTopVisibleIndex();
 }
 
-FX_BOOL CPDFSDK_Widget::IsChecked() {
+FX_BOOL CPDFSDK_Widget::IsChecked() const {
 #ifdef PDF_ENABLE_XFA
   if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
     if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp
index 80a9b17..379170f 100644
--- a/fpdfsdk/src/javascript/app.cpp
+++ b/fpdfsdk/src/javascript/app.cpp
@@ -201,11 +201,12 @@
   CJS_Context* pContext = (CJS_Context*)cc;
   CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument();
   CPDFXFA_Document* pDoc = pCurDoc->GetDocument();
-  if (pDoc->GetDocType() == 1 || pDoc->GetDocType() == 2)
+  if (pDoc->GetDocType() == 1 || pDoc->GetDocType() == 2) {
     vp << JS_NUM_VIEWERVERSION_XFA;
-  else
-    vp << JS_NUM_VIEWERVERSION;
+    return TRUE;
+  }
 
+  vp << JS_NUM_VIEWERVERSION;
   return TRUE;
 }