Allow building of XFA branch without XFA

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1399833002 .
diff --git a/BUILD.gn b/BUILD.gn
index 9f40bc3..77498ce0 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -3,14 +3,7 @@
 # found in the LICENSE file.
 
 import("//testing/test.gni")
-
-pdf_use_skia = false
-
-declare_args() {
-  # On Android there's no system FreeType. On Windows and Mac, only a few
-  # methods are used from it.
-  pdfium_bundle_freetype = !is_linux
-}
+import("pdfium.gni")
 
 config("pdfium_config") {
   cflags = []
@@ -28,6 +21,10 @@
     defines += [ "_SKIA_SUPPORT_" ]
   }
 
+  if (pdf_enable_xfa) {
+    defines += [ "PDF_ENABLE_XFA" ]
+  }
+
   if (is_linux) {
     if (current_cpu == "x64") {
       defines += [ "_FX_CPU_=_FX_X64_" ]
@@ -114,9 +111,12 @@
     ":javascript",
     ":jsapi",
     ":pdfwindow",
-    ":xfa",
   ]
 
+  if (pdf_enable_xfa) {
+    deps += [ ":xfa" ]
+  }
+
   if (is_win) {
     libs += [ "advapi32.lib", "gdi32.lib", "user32.lib" ]
   }
diff --git a/fpdfsdk/src/fpdfsave.cpp b/fpdfsdk/src/fpdfsave.cpp
index 7a666bc..2cfbb81 100644
--- a/fpdfsdk/src/fpdfsave.cpp
+++ b/fpdfsdk/src/fpdfsave.cpp
@@ -60,6 +60,7 @@
 
 FX_BOOL _SaveXFADocumentData(CPDFXFA_Document* pDocument,
                              CFX_PtrArray& fileList) {
+#ifdef PDF_ENABLE_XFA
   if (!pDocument)
     return FALSE;
   if (pDocument->GetDocType() != DOCTYPE_DYNIMIC_XFA &&
@@ -228,6 +229,8 @@
     }
   }
   pContext->Release();
+#endif  // PDF_ENABLE_XFA
+
   return TRUE;
 }
 
diff --git a/fpdfsdk/src/fpdfxfa/fpdfxfa_app.cpp b/fpdfsdk/src/fpdfxfa/fpdfxfa_app.cpp
index 255d2bb..fed3f32 100644
--- a/fpdfsdk/src/fpdfxfa/fpdfxfa_app.cpp
+++ b/fpdfsdk/src/fpdfxfa/fpdfxfa_app.cpp
@@ -43,14 +43,17 @@
   delete m_pXFAApp;

   m_pXFAApp = NULL;

 

+#ifdef PDF_ENABLE_XFA

   FXJSE_Runtime_Release(m_hJSERuntime);

   m_hJSERuntime = NULL;

 

   FXJSE_Finalize();

   BC_Library_Destory();

+#endif

 }

 

 FX_BOOL CPDFXFA_App::Initialize() {

+#ifdef PDF_ENABLE_XFA

   BC_Library_Init();

   FXJSE_Initialize();

 

@@ -67,6 +70,7 @@
     return FALSE;

 

   m_pXFAApp->SetDefaultFontMgr(m_pFontMgr);

+#endif

   return TRUE;

 }

 

diff --git a/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp b/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
index cef65ac..365d5d7 100644
--- a/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
+++ b/fpdfsdk/src/fpdfxfa/fpdfxfa_doc.cpp
@@ -338,7 +338,10 @@
   CXFA_WidgetAcc* pWidgetAcc =

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

 

-  int nRotate = pWidgetAcc->GetRotate();

+  int nRotate = 0;

+#ifdef PDF_ENABLE_XFA

+  nRotate = pWidgetAcc->GetRotate();

+#endif

 

   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();

   if (pEnv == NULL)

@@ -894,6 +897,7 @@
 }

 

 FX_BOOL CPDFXFA_Document::_OnBeforeNotifySumbit() {

+#ifdef PDF_ENABLE_XFA

   if (m_iDocType != DOCTYPE_DYNIMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)

     return TRUE;

   if (m_pXFADocView == NULL)

@@ -939,7 +943,7 @@
     pWidgetAccIterator->Release();

     m_pXFADocView->UpdateDocView();

   }

-

+#endif

   return TRUE;

 }

 void CPDFXFA_Document::_OnAfterNotifySumbit() {

@@ -1190,6 +1194,7 @@
 }

 

 FX_BOOL CPDFXFA_Document::_SubmitData(IXFA_Doc* hDoc, CXFA_Submit submit) {

+#ifdef PDF_ENABLE_XFA

   CFX_WideStringC csURLC;

   submit.GetSubmitTarget(csURLC);

   CFX_WideString csURL = csURLC;

@@ -1290,6 +1295,9 @@
   }

 

   return bRet;

+#else

+  return TRUE;

+#endif

 }

 

 FX_BOOL CPDFXFA_Document::SetGlobalProperty(IXFA_Doc* hDoc,

diff --git a/fpdfsdk/src/fsdk_annothandler.cpp b/fpdfsdk/src/fsdk_annothandler.cpp
index 287a26b..11d2aef 100644
--- a/fpdfsdk/src/fsdk_annothandler.cpp
+++ b/fpdfsdk/src/fsdk_annothandler.cpp
@@ -765,6 +765,8 @@
                                      CFX_RenderDevice* pDevice,
                                      CPDF_Matrix* pUser2Device,
                                      FX_DWORD dwFlags) {
+#ifdef PDF_ENABLE_XFA
+
   ASSERT(pPageView != NULL);
   ASSERT(pAnnot != NULL);
 
@@ -790,6 +792,7 @@
   pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight);
 
   // to do highlight and shadow
+#endif  // PDF_ENABLE_XFA
 }
 
 void CPDFSDK_XFAAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
@@ -811,13 +814,15 @@
   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
   ASSERT(pWidgetHandler != NULL);
 
+  CFX_RectF rcBBox;
+#ifdef PDF_ENABLE_XFA
   XFA_ELEMENT eType =
       pWidgetHandler->GetDataAcc(pAnnot->GetXFAWidget())->GetUIType();
-  CFX_RectF rcBBox;
   if (eType == XFA_ELEMENT_Signature)
     pWidgetHandler->GetBBox(pAnnot->GetXFAWidget(), rcBBox,
                             XFA_WIDGETSTATUS_Visible, TRUE);
   else
+#endif
     pWidgetHandler->GetBBox(pAnnot->GetXFAWidget(), rcBBox, 0);
 
   CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp
index 81b4ef4..4a6b987 100644
--- a/fpdfsdk/src/fsdk_baseform.cpp
+++ b/fpdfsdk/src/fsdk_baseform.cpp
@@ -276,6 +276,7 @@
 }
 
 void CPDFSDK_Widget::Synchronize(FX_BOOL bSynchronizeElse) {
+#ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
       CPDF_FormField* pFormField = GetFormField();
@@ -325,9 +326,11 @@
       }
     }
   }
+#endif  // PDF_ENABLE_XFA
 }
 
 void CPDFSDK_Widget::SynchronizeXFAValue() {
+#ifdef PDF_ENABLE_XFA
   ASSERT(m_pPageView != NULL);
 
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
@@ -352,9 +355,11 @@
                                           pFormCtrl);
     }
   }
+#endif  // PDF_ENABLE_XFA
 }
 
 void CPDFSDK_Widget::SynchronizeXFAItems() {
+#ifdef PDF_ENABLE_XFA
   ASSERT(m_pPageView != NULL);
 
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
@@ -375,12 +380,14 @@
       SynchronizeXFAItems(pXFADocView, hWidget, pFormField, NULL);
     }
   }
+#endif  // PDF_ENABLE_XFA
 }
 
 void CPDFSDK_Widget::SynchronizeXFAValue(IXFA_DocView* pXFADocView,
                                          IXFA_Widget* hWidget,
                                          CPDF_FormField* pFormField,
                                          CPDF_FormControl* pFormControl) {
+#ifdef PDF_ENABLE_XFA
   ASSERT(pXFADocView != NULL);
   ASSERT(hWidget != NULL);
 
@@ -449,12 +456,14 @@
       } break;
     }
   }
+#endif  // PDF_ENABLE_XFA
 }
 
 void CPDFSDK_Widget::SynchronizeXFAItems(IXFA_DocView* pXFADocView,
                                          IXFA_Widget* hWidget,
                                          CPDF_FormField* pFormField,
                                          CPDF_FormControl* pFormControl) {
+#ifdef PDF_ENABLE_XFA
   ASSERT(pXFADocView != NULL);
   ASSERT(hWidget != NULL);
 
@@ -496,6 +505,7 @@
       } break;
     }
   }
+#endif  // PDF_ENABLE_XFA
 }
 
 FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
@@ -672,6 +682,7 @@
 }
 
 int CPDFSDK_Widget::GetSelectedIndex(int nIndex) {
+#ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
       if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
@@ -680,14 +691,14 @@
       }
     }
   }
+#endif  // PDF_ENABLE_XFA
 
   CPDF_FormField* pFormField = GetFormField();
-  ASSERT(pFormField != NULL);
-
   return pFormField->GetSelectedIndex(nIndex);
 }
 
 CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) {
+#ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
       if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
@@ -698,10 +709,9 @@
       }
     }
   }
+#endif  // PDF_ENABLE_XFA
 
   CPDF_FormField* pFormField = GetFormField();
-  ASSERT(pFormField != NULL);
-
   return pFormField->GetValue();
 }
 
@@ -727,6 +737,7 @@
 }
 
 FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) {
+#ifdef PDF_ENABLE_XFA
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
       if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
@@ -737,6 +748,7 @@
       }
     }
   }
+#endif  // PDF_ENABLE_XFA
 
   CPDF_FormField* pFormField = GetFormField();
   return pFormField->IsItemSelected(nIndex);
@@ -748,6 +760,7 @@
 }
 
 FX_BOOL CPDFSDK_Widget::IsChecked() {
+#ifdef PDF_ENABLE_XFA
   if (IXFA_WidgetHandler* pXFAWidgetHandler = this->GetXFAWidgetHandler()) {
     if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
       if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
@@ -756,10 +769,9 @@
       }
     }
   }
+#endif  // PDF_ENABLE_XFA
 
   CPDF_FormControl* pFormCtrl = GetFormControl();
-  ASSERT(pFormCtrl != NULL);
-
   return pFormCtrl->IsChecked();
 }
 
diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp
index 4c684cf..2ab9d8b 100644
--- a/fpdfsdk/src/fsdk_mgr.cpp
+++ b/fpdfsdk/src/fsdk_mgr.cpp
@@ -694,6 +694,7 @@
   if (pPage == NULL)
     return;
 
+#ifdef PDF_ENABLE_XFA
   if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
     CFX_Graphics gs;
     gs.Create(pDevice);
@@ -717,6 +718,8 @@
     pRenderContext->Release();
     return;
   }
+#endif  // PDF_ENABLE_XFA
+
   // for pdf/static xfa.
   CPDFSDK_AnnotIterator annotIterator(this, TRUE);
   CPDFSDK_Annot* pSDKAnnot = NULL;
diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp
index d01c1fa..8a0d666 100644
--- a/fpdfsdk/src/javascript/JS_Runtime.cpp
+++ b/fpdfsdk/src/javascript/JS_Runtime.cpp
@@ -222,6 +222,7 @@
 }
 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name,
                                      FXJSE_HVALUE hValue) {
+#ifdef PDF_ENABLE_XFA
   const FX_CHAR* name = utf8Name.GetCStr();
 
   v8::Locker lock(GetIsolate());
@@ -242,11 +243,13 @@
     return FALSE;
   }
   ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
+#endif
 
   return TRUE;
 }
 FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name,
                                      FXJSE_HVALUE hValue) {
+#ifdef PDF_ENABLE_XFA
   if (utf8Name.IsEmpty() || hValue == NULL)
     return FALSE;
   const FX_CHAR* name = utf8Name.GetCStr();
@@ -266,7 +269,7 @@
       v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
                               utf8Name.GetLength()),
       propvalue);
-
+#endif
   return TRUE;
 }
 
diff --git a/pdfium.gni b/pdfium.gni
new file mode 100644
index 0000000..8e99e5f
--- /dev/null
+++ b/pdfium.gni
@@ -0,0 +1,17 @@
+# Copyright 2015 PDFium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# This file contains PDFium-related build flags.
+
+declare_args() {
+  # On Android there's no system FreeType. On Windows and Mac, only a few
+  # methods are used from it.
+  pdfium_bundle_freetype = !is_linux
+
+  # Build PDFium either with or without XFA Forms support.
+  pdf_enable_xfa = true
+
+  # Build PDFium against skia (experimental) rather than agg.
+  pdf_use_skia = false
+}
\ No newline at end of file
diff --git a/pdfium.gyp b/pdfium.gyp
index 72d1785..6ef54ab 100644
--- a/pdfium.gyp
+++ b/pdfium.gyp
@@ -1,6 +1,7 @@
 {
   'variables': {
     'pdf_use_skia%': 0,
+    'pdf_enable_xfa%': 1,
     'conditions': [
       ['OS=="linux"', {
         'bundle_freetype%': 0,
@@ -26,6 +27,9 @@
       ['pdf_use_skia==1', {
         'defines': ['_SKIA_SUPPORT_'],
       }],
+      ['pdf_enable_xfa==1', {
+        'defines': ['PDF_ENABLE_XFA'],
+      }],
       ['OS=="linux"', {
         'conditions': [
           ['target_arch=="x64"', {
@@ -625,9 +629,8 @@
     {
       'target_name': 'fpdfxfa',
       'type': 'static_library',
-      'ldflags': [ '-L<(PRODUCT_DIR)',],
       'dependencies': [
-        'xfa.gyp:xfa',
+        'javascript'
       ],
       'sources': [
         'fpdfsdk/src/fpdfxfa/fpdfxfa_app.cpp',
@@ -639,6 +642,14 @@
         'fpdfsdk/include/fpdfxfa/fpdfxfa_page.h',
         'fpdfsdk/include/fpdfxfa/fpdfxfa_util.h',
       ],
+      'ldflags': [ '-L<(PRODUCT_DIR)',],
+      'conditions': [
+        [ "pdf_enable_xfa==1", {
+          'dependencies': [
+            'xfa.gyp:xfa',
+          ],
+        }],
+      ],
     },
     {
       'target_name': 'javascript',
@@ -768,8 +779,14 @@
         'testing/fx_string_testhelpers.h',
         'testing/fx_string_testhelpers.cpp',
         'third_party/base/nonstd_unique_ptr_unittest.cpp',
-        'xfa/src/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp',
-        'xfa/src/fxfa/src/parser/xfa_utils_imp_unittest.cpp',
+      ],
+      'conditions': [
+        ['pdf_enable_xfa==1', {
+          'sources': [
+            'xfa/src/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp',
+            'xfa/src/fxfa/src/parser/xfa_utils_imp_unittest.cpp',
+          ],
+        }],
       ],
     },
     {
diff --git a/samples/BUILD.gn b/samples/BUILD.gn
index ca7cafa..957b1b6 100644
--- a/samples/BUILD.gn
+++ b/samples/BUILD.gn
@@ -2,6 +2,8 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+import("../pdfium.gni")
+
 group("samples") {
   deps = [
     ":pdfium_test",
@@ -14,6 +16,9 @@
     "PNGPREFIX_H",
     "PNG_USE_READ_MACROS",
   ]
+  if (pdf_enable_xfa) {
+    defines += [ "PDF_ENABLE_XFA" ]
+  }
 }
 
 executable("pdfium_test") {
diff --git a/xfa.gyp b/xfa.gyp
index 5051343..25dd8ff 100644
--- a/xfa.gyp
+++ b/xfa.gyp
@@ -1,4 +1,9 @@
 {
+  "target_defaults": {
+    "defines": [
+      "PDF_ENABLE_XFA",
+    ],
+  },
   "targets":[
     {
       "target_name":"xfa",