From 8837c91c0db8f9205b7e58db97946e9af7958b39 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Tue, 1 Nov 2016 11:22:37 -0700 Subject: Reland: Make the CPDFXFA_App non-global This reverts commit a282c7380f3964de41ea93c9980b12c4513d3473. This CL changes CPDFXFA_App from a global object to a child of the CPDFXFA_Document objects. BUG=pdfium:623 Review-Url: https://codereview.chromium.org/2421603002 --- fpdfsdk/DEPS | 3 +- fpdfsdk/cpdfsdk_formfillenvironment.cpp | 5 - fpdfsdk/fpdfdoc_unittest.cpp | 3 +- fpdfsdk/fpdfformfill.cpp | 3 - fpdfsdk/fpdfsave.cpp | 3 - fpdfsdk/fpdfview.cpp | 21 ++--- fpdfsdk/fpdfxfa/DEPS | 1 + fpdfsdk/fpdfxfa/cpdfxfa_app.cpp | 157 +++++++++++--------------------- fpdfsdk/fpdfxfa/cpdfxfa_app.h | 24 ++--- fpdfsdk/fpdfxfa/cpdfxfa_document.cpp | 51 ++++++++--- fpdfsdk/fpdfxfa/cpdfxfa_document.h | 20 ++-- fpdfsdk/javascript/cjs_runtime.cpp | 43 +-------- 12 files changed, 114 insertions(+), 220 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/DEPS b/fpdfsdk/DEPS index 5b1f3c4f60..ce037d902e 100644 --- a/fpdfsdk/DEPS +++ b/fpdfsdk/DEPS @@ -3,6 +3,7 @@ include_rules = [ '+public', '+v8', '+xfa/fwl/core', + '+xfa/fxbarcode', '+xfa/fxfa', '+xfa/fxgraphics', -] + ] diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index 1f39de9069..2c44211efe 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp @@ -58,11 +58,6 @@ CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { // Those widgets may call things like KillTimer() as they are shutdown. m_pFormFiller.reset(); -#ifdef PDF_ENABLE_XFA - CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); - if (pProvider->m_pFormFillEnvList.GetSize() == 0) - pProvider->SetJavaScriptInitialized(FALSE); -#endif // PDF_ENABLE_XFA if (m_pInfo && m_pInfo->Release) m_pInfo->Release(m_pInfo); } diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index 48be72399c..adc135e2cc 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -35,8 +35,7 @@ class CPDF_TestDocument : public CPDF_Document { class CPDF_TestXFADocument : public CPDFXFA_Document { public: CPDF_TestXFADocument() - : CPDFXFA_Document(pdfium::MakeUnique(), - CPDFXFA_App::GetInstance()) {} + : CPDFXFA_Document(pdfium::MakeUnique()) {} void SetRoot(CPDF_Dictionary* root) { reinterpret_cast(GetPDFDoc())->SetRoot(root); diff --git a/fpdfsdk/fpdfformfill.cpp b/fpdfsdk/fpdfformfill.cpp index 34e206d6bc..0f44b1dfa2 100644 --- a/fpdfsdk/fpdfformfill.cpp +++ b/fpdfsdk/fpdfformfill.cpp @@ -258,7 +258,6 @@ FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, #ifdef PDF_ENABLE_XFA pDocument->SetFormFillEnv(pFormFillEnv); - CPDFXFA_App::GetInstance()->AddFormFillEnv(pFormFillEnv); #endif // PDF_ENABLE_XFA return pFormFillEnv; @@ -273,8 +272,6 @@ FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) { HandleToCPDFSDKEnvironment(hHandle); #ifdef PDF_ENABLE_XFA - CPDFXFA_App::GetInstance()->RemoveFormFillEnv(pFormFillEnv); - // Reset the focused annotations and remove the SDK document from the // XFA document. pFormFillEnv->ClearAllFocusedAnnots(); diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index 03a9db1168..c98bb77aa3 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -85,9 +85,6 @@ bool SaveXFADocumentData(CPDFXFA_Document* pDocument, pDocument->GetDocType() != DOCTYPE_STATIC_XFA) return true; - if (!CPDFXFA_App::GetInstance()->GetXFAApp()) - return true; - CXFA_FFDocView* pXFADocView = pDocument->GetXFADocView(); if (!pXFADocView) return true; diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 03aedf6874..f8e8e47e0f 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -42,6 +42,7 @@ #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" #include "public/fpdf_formfill.h" +#include "xfa/fxbarcode/BC_Library.h" #endif // PDF_ENABLE_XFA #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ @@ -70,8 +71,8 @@ CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { #ifdef PDF_ENABLE_XFA - return doc ? FPDFDocumentFromUnderlying(new CPDFXFA_Document( - pdfium::WrapUnique(doc), CPDFXFA_App::GetInstance())) + return doc ? FPDFDocumentFromUnderlying( + new CPDFXFA_Document(pdfium::WrapUnique(doc))) : nullptr; #else // PDF_ENABLE_XFA return FPDFDocumentFromUnderlying(doc); @@ -249,19 +250,19 @@ FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) { pModuleMgr->LoadEmbeddedKorea1CMaps(); #ifdef PDF_ENABLE_XFA - CPDFXFA_App::GetInstance()->Initialize( - (cfg && cfg->version >= 2) ? static_cast(cfg->m_pIsolate) - : nullptr); + FXJSE_Initialize(); + BC_Library_Init(); #endif // PDF_ENABLE_XFA - if (cfg && cfg->version >= 2) IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate); } DLLEXPORT void STDCALL FPDF_DestroyLibrary() { #ifdef PDF_ENABLE_XFA - CPDFXFA_App::ReleaseInstance(); + BC_Library_Destory(); + FXJSE_Finalize(); #endif // PDF_ENABLE_XFA + CPDF_ModuleMgr::Destroy(); CFX_GEModule::Destroy(); @@ -342,11 +343,7 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, ProcessParseError(error); return nullptr; } -#ifdef PDF_ENABLE_XFA - return new CPDFXFA_Document(std::move(pDocument), CPDFXFA_App::GetInstance()); -#else // PDF_ENABLE_XFA - return pDocument.release(); -#endif // PDF_ENABLE_XFA + return FPDFDocumentFromCPDFDocument(pDocument.release()); } #ifdef PDF_ENABLE_XFA diff --git a/fpdfsdk/fpdfxfa/DEPS b/fpdfsdk/fpdfxfa/DEPS index d4de6f1e89..1687e308b5 100644 --- a/fpdfsdk/fpdfxfa/DEPS +++ b/fpdfsdk/fpdfxfa/DEPS @@ -1,3 +1,4 @@ include_rules = [ + '+fxjs', '+xfa/fxbarcode', ] diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_app.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_app.cpp index dc5c4be0d6..27ca1e0c61 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_app.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_app.cpp @@ -11,110 +11,58 @@ #include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" #include "fpdfsdk/fsdk_define.h" -#include "third_party/base/ptr_util.h" -#include "xfa/fxbarcode/BC_Library.h" +#include "fpdfsdk/javascript/cjs_runtime.h" +#include "fxjs/fxjs_v8.h" #include "xfa/fxfa/xfa_ffapp.h" #include "xfa/fxfa/xfa_fontmgr.h" -namespace { - -CPDFXFA_App* g_pApp = nullptr; - -} // namespace - -CPDFXFA_App* CPDFXFA_App::GetInstance() { - if (!g_pApp) { - g_pApp = new CPDFXFA_App(); - } - return g_pApp; -} - -void CPDFXFA_App::ReleaseInstance() { - delete g_pApp; - g_pApp = nullptr; -} - -CPDFXFA_App::CPDFXFA_App() - : m_bJavaScriptInitialized(FALSE), m_pIsolate(nullptr) { - m_pFormFillEnvList.RemoveAll(); -} - -CPDFXFA_App::~CPDFXFA_App() { - FXJSE_Runtime_Release(m_pIsolate); - m_pIsolate = nullptr; - - FXJSE_Finalize(); - BC_Library_Destory(); -} - -FX_BOOL CPDFXFA_App::Initialize(v8::Isolate* pIsolate) { - BC_Library_Init(); - FXJSE_Initialize(); - - m_pIsolate = pIsolate ? pIsolate : FXJSE_Runtime_Create_Own(); - if (!m_pIsolate) - return FALSE; - +CPDFXFA_App::CPDFXFA_App() { m_pXFAApp = pdfium::MakeUnique(this); - m_pXFAApp->SetDefaultFontMgr( - std::unique_ptr(new CXFA_DefFontMgr)); - - return TRUE; + m_pXFAApp->SetDefaultFontMgr(pdfium::MakeUnique()); } -FX_BOOL CPDFXFA_App::AddFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) { - if (!pFormFillEnv) - return FALSE; +CPDFXFA_App::~CPDFXFA_App() {} - m_pFormFillEnvList.Add(pFormFillEnv); - return TRUE; -} - -FX_BOOL CPDFXFA_App::RemoveFormFillEnv( - CPDFSDK_FormFillEnvironment* pFormFillEnv) { - if (!pFormFillEnv) - return FALSE; +v8::Isolate* CPDFXFA_App::GetJSERuntime() const { + if (!m_pFormFillEnv) + return nullptr; - int nFind = m_pFormFillEnvList.Find(pFormFillEnv); - if (nFind != -1) { - m_pFormFillEnvList.RemoveAt(nFind); - return TRUE; - } + // XFA requires V8, if we have V8 then we have a CJS_Runtime and not the stub. + CJS_Runtime* runtime = + static_cast(m_pFormFillEnv->GetJSRuntime()); + return runtime->GetIsolate(); +} - return FALSE; +void CPDFXFA_App::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pFormFillEnv = pFormFillEnv; } void CPDFXFA_App::GetAppName(CFX_WideString& wsName) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) - wsName = pFormFillEnv->FFI_GetAppName(); + if (m_pFormFillEnv) + wsName = m_pFormFillEnv->FFI_GetAppName(); } void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) - wsLanguage = pFormFillEnv->GetLanguage(); + if (m_pFormFillEnv) + wsLanguage = m_pFormFillEnv->GetLanguage(); } void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) { - wsPlatform = pFormFillEnv->GetPlatform(); + if (m_pFormFillEnv) { + wsPlatform = m_pFormFillEnv->GetPlatform(); } } void CPDFXFA_App::Beep(uint32_t dwType) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) - pFormFillEnv->JS_appBeep(dwType); + if (m_pFormFillEnv) + m_pFormFillEnv->JS_appBeep(dwType); } int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage, const CFX_WideString& wsTitle, uint32_t dwIconType, uint32_t dwButtonType) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (!pFormFillEnv) + if (!m_pFormFillEnv) return -1; uint32_t iconType = 0; @@ -147,8 +95,8 @@ int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage, iButtonType |= 3; break; } - int32_t iRet = pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(), - iButtonType, iconType); + int32_t iRet = m_pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(), + iButtonType, iconType); switch (iRet) { case 1: return XFA_IDOK; @@ -167,29 +115,29 @@ CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion, const CFX_WideString& wsDefaultAnswer, FX_BOOL bMark) { CFX_WideString wsAnswer; - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) { - int nLength = 2048; - char* pBuff = new char[nLength]; - nLength = pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(), + if (!m_pFormFillEnv) + return wsAnswer; + + int nLength = 2048; + char* pBuff = new char[nLength]; + nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(), wsDefaultAnswer.c_str(), nullptr, bMark, pBuff, nLength); - if (nLength > 0) { - nLength = nLength > 2046 ? 2046 : nLength; - pBuff[nLength] = 0; - pBuff[nLength + 1] = 0; - wsAnswer = CFX_WideString::FromUTF16LE( - reinterpret_cast(pBuff), - nLength / sizeof(unsigned short)); - } - delete[] pBuff; + if (nLength > 0) { + nLength = nLength > 2046 ? 2046 : nLength; + pBuff[nLength] = 0; + pBuff[nLength + 1] = 0; + wsAnswer = CFX_WideString::FromUTF16LE( + reinterpret_cast(pBuff), + nLength / sizeof(unsigned short)); } + delete[] pBuff; return wsAnswer; } IFX_SeekableReadStream* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - return pFormFillEnv ? pFormFillEnv->DownloadFromURL(wsURL.c_str()) : nullptr; + return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL.c_str()) + : nullptr; } FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL, @@ -198,23 +146,21 @@ FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL, const CFX_WideString& wsEncode, const CFX_WideString& wsHeader, CFX_WideString& wsResponse) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (!pFormFillEnv) + if (!m_pFormFillEnv) return FALSE; - wsResponse = pFormFillEnv->PostRequestURL(wsURL.c_str(), wsData.c_str(), - wsContentType.c_str(), - wsEncode.c_str(), wsHeader.c_str()); + wsResponse = m_pFormFillEnv->PostRequestURL( + wsURL.c_str(), wsData.c_str(), wsContentType.c_str(), wsEncode.c_str(), + wsHeader.c_str()); return TRUE; } FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL, const CFX_WideString& wsData, const CFX_WideString& wsEncode) { - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - return pFormFillEnv && - pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(), - wsEncode.c_str()); + return m_pFormFillEnv && + m_pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(), + wsEncode.c_str()); } void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) { @@ -316,8 +262,7 @@ void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) { IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() { CXFA_FWLAdapterTimerMgr* pAdapter = nullptr; - CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0); - if (pFormFillEnv) - pAdapter = new CXFA_FWLAdapterTimerMgr(pFormFillEnv); + if (m_pFormFillEnv) + pAdapter = new CXFA_FWLAdapterTimerMgr(m_pFormFillEnv); return pAdapter; } diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_app.h b/fpdfsdk/fpdfxfa/cpdfxfa_app.h index 9ec045f540..763f88da96 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_app.h +++ b/fpdfsdk/fpdfxfa/cpdfxfa_app.h @@ -7,31 +7,22 @@ #ifndef FPDFSDK_FPDFXFA_CPDFXFA_APP_H_ #define FPDFSDK_FPDFXFA_CPDFXFA_APP_H_ +#include "third_party/base/ptr_util.h" #include "xfa/fxfa/fxfa.h" class CPDFSDK_FormFillEnvironment; -class IFXJS_Runtime; +class CJS_Runtime; class CPDFXFA_App : public IXFA_AppProvider { public: - static CPDFXFA_App* GetInstance(); - static void ReleaseInstance(); - CPDFXFA_App(); ~CPDFXFA_App() override; - FX_BOOL Initialize(v8::Isolate* pIsolate); CXFA_FFApp* GetXFAApp() { return m_pXFAApp.get(); } - FX_BOOL AddFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); - FX_BOOL RemoveFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); - - FX_BOOL IsJavaScriptInitialized() const { return m_bJavaScriptInitialized; } - void SetJavaScriptInitialized(FX_BOOL bInitialized) { - m_bJavaScriptInitialized = bInitialized; - } + void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); - v8::Isolate* GetJSERuntime() const { return m_pIsolate; } + v8::Isolate* GetJSERuntime() const; // IFXA_AppProvider: void GetLanguage(CFX_WideString& wsLanguage) override; @@ -62,12 +53,9 @@ class CPDFXFA_App : public IXFA_AppProvider { void LoadString(int32_t iStringID, CFX_WideString& wsString) override; IFWL_AdapterTimerMgr* GetTimerMgr() override; - CFX_ArrayTemplate m_pFormFillEnvList; - - protected: - FX_BOOL m_bJavaScriptInitialized; + private: + CPDFSDK_FormFillEnvironment* m_pFormFillEnv; // Not owned. std::unique_ptr m_pXFAApp; - v8::Isolate* m_pIsolate; }; #endif // FPDFSDK_FPDFXFA_CPDFXFA_APP_H_ diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp index df0da31beb..b04b9504dd 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp @@ -16,6 +16,7 @@ #include "fpdfsdk/fsdk_define.h" #include "fpdfsdk/javascript/ijs_runtime.h" #include "public/fpdf_formfill.h" +#include "third_party/base/ptr_util.h" #include "xfa/fxfa/cxfa_eventparam.h" #include "xfa/fxfa/xfa_ffapp.h" #include "xfa/fxfa/xfa_ffdoc.h" @@ -28,13 +29,11 @@ extern void SetLastError(int err); extern int GetLastError(); #endif -CPDFXFA_Document::CPDFXFA_Document(std::unique_ptr pPDFDoc, - CPDFXFA_App* pProvider) +CPDFXFA_Document::CPDFXFA_Document(std::unique_ptr pPDFDoc) : m_iDocType(DOCTYPE_PDF), m_pPDFDoc(std::move(pPDFDoc)), m_pFormFillEnv(nullptr), m_pXFADocView(nullptr), - m_pApp(pProvider), m_nLoadStatus(FXFA_LOADSTATUS_PRELOAD), m_nPageCount(0), m_DocEnv(this) {} @@ -42,6 +41,12 @@ CPDFXFA_Document::CPDFXFA_Document(std::unique_ptr pPDFDoc, CPDFXFA_Document::~CPDFXFA_Document() { m_nLoadStatus = FXFA_LOADSTATUS_CLOSING; + // Must happen before we remove the form fill environment. + if (m_pXFADoc) { + if (CXFA_FFApp* pApp = GetApp()->GetXFAApp()) + CloseXFADoc(pApp->GetDocHandler()); + } + if (m_pFormFillEnv) { m_pFormFillEnv->ClearAllFocusedAnnots(); // Once we're deleted the FormFillEnvironment will point at a bad underlying @@ -50,19 +55,35 @@ CPDFXFA_Document::~CPDFXFA_Document() { m_pFormFillEnv = nullptr; } - if (m_pXFADoc) { - CXFA_FFApp* pApp = m_pApp->GetXFAApp(); - if (pApp) { - CXFA_FFDocHandler* pDocHandler = pApp->GetDocHandler(); - if (pDocHandler) - CloseXFADoc(pDocHandler); - } - m_pXFADoc.reset(); - } - m_nLoadStatus = FXFA_LOADSTATUS_CLOSED; } +void CPDFXFA_Document::CloseXFADoc(CXFA_FFDocHandler* pDoc) { + if (!pDoc) + return; + m_pXFADoc->CloseDoc(); + m_pXFADoc.reset(); + m_pXFADocView = nullptr; +} + +CPDFXFA_App* CPDFXFA_Document::GetApp() { + if (!m_pApp) + m_pApp = pdfium::MakeUnique(); + return m_pApp.get(); +} + +void CPDFXFA_Document::SetFormFillEnv( + CPDFSDK_FormFillEnvironment* pFormFillEnv) { + // The layout data can have pointers back into the script context. That + // context will be different if the form fill environment closes, so, force + // the layout data to clear. + if (m_pXFADoc && m_pXFADoc->GetXFADoc()) + m_pXFADoc->GetXFADoc()->ClearLayoutData(); + + GetApp()->SetFormFillEnv(pFormFillEnv); + m_pFormFillEnv = pFormFillEnv; +} + FX_BOOL CPDFXFA_Document::LoadXFADoc() { m_nLoadStatus = FXFA_LOADSTATUS_LOADING; @@ -71,7 +92,7 @@ FX_BOOL CPDFXFA_Document::LoadXFADoc() { m_XFAPageList.RemoveAll(); - CXFA_FFApp* pApp = m_pApp->GetXFAApp(); + CXFA_FFApp* pApp = GetApp()->GetXFAApp(); if (!pApp) return FALSE; @@ -95,7 +116,7 @@ FX_BOOL CPDFXFA_Document::LoadXFADoc() { return FALSE; } m_pXFADoc->StopLoad(); - m_pXFADoc->GetXFADoc()->InitScriptContext(m_pApp->GetJSERuntime()); + m_pXFADoc->GetXFADoc()->InitScriptContext(GetApp()->GetJSERuntime()); if (m_pXFADoc->GetDocType() == XFA_DOCTYPE_Dynamic) m_iDocType = DOCTYPE_DYNAMIC_XFA; diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_document.h b/fpdfsdk/fpdfxfa/cpdfxfa_document.h index de997c16f3..00a9f255ff 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_document.h +++ b/fpdfsdk/fpdfxfa/cpdfxfa_document.h @@ -9,11 +9,11 @@ #include +#include "fpdfsdk/fpdfxfa/cpdfxfa_app.h" #include "fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.h" #include "xfa/fxfa/xfa_ffdoc.h" class CPDFSDK_FormFillEnvironment; -class CPDFXFA_App; class CPDFXFA_Page; class CXFA_FFDocHandler; class IJS_Runtime; @@ -29,8 +29,7 @@ enum LoadStatus { class CPDFXFA_Document { public: - CPDFXFA_Document(std::unique_ptr pPDFDoc, - CPDFXFA_App* pProvider); + CPDFXFA_Document(std::unique_ptr pPDFDoc); ~CPDFXFA_Document(); FX_BOOL LoadXFADoc(); @@ -38,11 +37,10 @@ class CPDFXFA_Document { CXFA_FFDoc* GetXFADoc() { return m_pXFADoc.get(); } CXFA_FFDocView* GetXFADocView() { return m_pXFADocView; } int GetDocType() const { return m_iDocType; } + CPDFXFA_App* GetApp(); // Creates if needed. CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { return m_pFormFillEnv; } - void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) { - m_pFormFillEnv = pFormFillEnv; - } + void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); void DeletePage(int page_index); int GetPageCount() const; @@ -68,13 +66,7 @@ class CPDFXFA_Document { CFX_ArrayTemplate* GetXFAPageList() { return &m_XFAPageList; } private: - void CloseXFADoc(CXFA_FFDocHandler* pDoc) { - if (pDoc) { - m_pXFADoc->CloseDoc(); - m_pXFADoc.reset(); - m_pXFADocView = nullptr; - } - } + void CloseXFADoc(CXFA_FFDocHandler* pDoc); int m_iDocType; @@ -82,7 +74,7 @@ class CPDFXFA_Document { std::unique_ptr m_pXFADoc; CPDFSDK_FormFillEnvironment* m_pFormFillEnv; // not owned. CXFA_FFDocView* m_pXFADocView; // not owned. - CPDFXFA_App* const m_pApp; + std::unique_ptr m_pApp; CFX_ArrayTemplate m_XFAPageList; LoadStatus m_nLoadStatus; int m_nPageCount; diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp index d41726b315..637fa0a411 100644 --- a/fpdfsdk/javascript/cjs_runtime.cpp +++ b/fpdfsdk/javascript/cjs_runtime.cpp @@ -68,7 +68,7 @@ CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) m_bBlocking(false), m_isolateManaged(false) { v8::Isolate* pIsolate = nullptr; -#ifndef PDF_ENABLE_XFA + IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; if (pPlatform->version <= 2) { unsigned int embedderDataSlot = 0; @@ -81,44 +81,15 @@ CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) } m_isolateManaged = FXJS_GetIsolate(&pIsolate); SetIsolate(pIsolate); -#else - if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { - // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. - pIsolate = CPDFXFA_App::GetInstance()->GetJSERuntime(); - SetIsolate(pIsolate); - } else { - IPDF_JSPLATFORM* pPlatform = - m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; - if (pPlatform->version <= 2) { - unsigned int embedderDataSlot = 0; - v8::Isolate* pExternalIsolate = nullptr; - if (pPlatform->version == 2) { - pExternalIsolate = reinterpret_cast(pPlatform->m_isolate); - embedderDataSlot = pPlatform->m_v8EmbedderSlot; - } - FXJS_Initialize(embedderDataSlot, pExternalIsolate); - } - m_isolateManaged = FXJS_GetIsolate(&pIsolate); - SetIsolate(pIsolate); - } +#ifdef PDF_ENABLE_XFA v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); - if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { - CJS_Context* pContext = (CJS_Context*)NewContext(); - InitializeEngine(); - ReleaseContext(pContext); - return; - } #endif if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) DefineJSObjects(); -#ifdef PDF_ENABLE_XFA - CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); -#endif - CJS_Context* pContext = (CJS_Context*)NewContext(); InitializeEngine(); ReleaseContext(pContext); @@ -261,19 +232,9 @@ FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); - v8::Local old_context = GetIsolate()->GetCurrentContext(); v8::Local context = NewLocalContext(); v8::Context::Scope context_scope(context); - // Caution: We're about to hand to XFA an object that in order to invoke - // methods will require that the current v8::Context always has a pointer - // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates - // its own v8::Context which has not initialized the embedder data slot. - // Do so now. - // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's - // embedder data slots, and/or to always use the right context. - CFXJS_Engine::SetForV8Context(old_context, this); - v8::Local propvalue = context->Global()->Get(v8::String::NewFromUtf8( GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); -- cgit v1.2.3