| Index: fpdfsdk/src/javascript/JS_Runtime.cpp
|
| diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp
|
| index f52e23a0946437e321862cc4f13262e517a3174d..2d0d7f6e76cdfeede57c5bc9fcbb47a069154a8c 100644
|
| --- a/fpdfsdk/src/javascript/JS_Runtime.cpp
|
| +++ b/fpdfsdk/src/javascript/JS_Runtime.cpp
|
| @@ -6,6 +6,10 @@
|
|
|
| #include "JS_Runtime.h"
|
|
|
| +#ifdef PDF_ENABLE_XFA
|
| +#include "../../../xfa/src/fxjse/src/value.h"
|
| +#include "../../include/fpdfxfa/fpdfxfa_app.h"
|
| +#endif
|
| #include "Consts.h"
|
| #include "Document.h"
|
| #include "Field.h"
|
| @@ -49,6 +53,7 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
|
| m_bBlocking(FALSE),
|
| m_isolate(NULL),
|
| m_isolateManaged(false) {
|
| +#ifndef PDF_ENABLE_XFA
|
| IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
|
| if (pPlatform->version <= 2) {
|
| unsigned int embedderDataSlot = 0;
|
| @@ -56,13 +61,51 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
|
| if (pPlatform->version == 2) {
|
| pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
|
| embedderDataSlot = pPlatform->m_v8EmbedderSlot;
|
| +#else
|
| + if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
|
| + // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
|
| + m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
|
| + } else {
|
| + IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
|
| + if (pPlatform->version <= 2) {
|
| + unsigned int embedderDataSlot = 0;
|
| + v8::Isolate* pExternalIsolate = nullptr;
|
| + if (pPlatform->version == 2) {
|
| + pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
|
| + embedderDataSlot = pPlatform->m_v8EmbedderSlot;
|
| + }
|
| + FXJS_Initialize(embedderDataSlot, pExternalIsolate);
|
| +#endif
|
| }
|
| +#ifndef PDF_ENABLE_XFA
|
| FXJS_Initialize(embedderDataSlot, pExternalIsolate);
|
| +#else
|
| + m_isolateManaged = FXJS_GetIsolate(&m_isolate);
|
| + }
|
| +
|
| + v8::Isolate* isolate = m_isolate;
|
| + v8::Isolate::Scope isolate_scope(isolate);
|
| + v8::Locker locker(isolate);
|
| + v8::HandleScope handle_scope(isolate);
|
| + if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
|
| + CJS_Context* pContext = (CJS_Context*)NewContext();
|
| + FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
|
| + ReleaseContext(pContext);
|
| + return;
|
| +#endif
|
| }
|
| +#ifndef PDF_ENABLE_XFA
|
| m_isolateManaged = FXJS_GetIsolate(&m_isolate);
|
| +#else
|
| +
|
| +#endif
|
| if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
|
| DefineJSObjects();
|
|
|
| +#ifdef PDF_ENABLE_XFA
|
| + CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
|
| +
|
| +#endif
|
| CJS_Context* pContext = (CJS_Context*)NewContext();
|
| FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
|
| ReleaseContext(pContext);
|
| @@ -76,7 +119,9 @@ CJS_Runtime::~CJS_Runtime() {
|
| delete m_ContextArray.GetAt(i);
|
|
|
| m_ContextArray.RemoveAll();
|
| +#ifndef PDF_ENABLE_XFA
|
| FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects);
|
| +#endif
|
|
|
| m_pApp = NULL;
|
| m_pDocument = NULL;
|
| @@ -88,6 +133,9 @@ CJS_Runtime::~CJS_Runtime() {
|
|
|
| void CJS_Runtime::DefineJSObjects() {
|
| v8::Isolate::Scope isolate_scope(GetIsolate());
|
| +#ifdef PDF_ENABLE_XFA
|
| + v8::Locker locker(GetIsolate());
|
| +#endif
|
| v8::HandleScope handle_scope(GetIsolate());
|
| v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
|
| v8::Context::Scope context_scope(context);
|
| @@ -160,6 +208,9 @@ IJS_Context* CJS_Runtime::GetCurrentContext() {
|
| void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
|
| if (m_pDocument != pReaderDoc) {
|
| v8::Isolate::Scope isolate_scope(m_isolate);
|
| +#ifdef PDF_ENABLE_XFA
|
| + v8::Locker locker(m_isolate);
|
| +#endif
|
| v8::HandleScope handle_scope(m_isolate);
|
| v8::Local<v8::Context> context =
|
| v8::Local<v8::Context>::New(m_isolate, m_context);
|
| @@ -205,6 +256,74 @@ v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
|
| return v8::Local<v8::Context>::New(m_isolate, m_context);
|
| }
|
|
|
| +#ifdef PDF_ENABLE_XFA
|
| +CFX_WideString ChangeObjName(const CFX_WideString& str) {
|
| + CFX_WideString sRet = str;
|
| + sRet.Replace(L"_", L".");
|
| + return sRet;
|
| +}
|
| +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());
|
| + v8::Isolate::Scope isolate_scope(GetIsolate());
|
| + v8::HandleScope handle_scope(GetIsolate());
|
| + v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext();
|
| + v8::Local<v8::Context> context =
|
| + v8::Local<v8::Context>::New(GetIsolate(), m_context);
|
| + 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.
|
| + FXJS_SetRuntimeForV8Context(old_context, this);
|
| +
|
| + v8::Local<v8::Value> propvalue =
|
| + context->Global()->Get(v8::String::NewFromUtf8(
|
| + GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
|
| +
|
| + if (propvalue.IsEmpty()) {
|
| + FXJSE_Value_SetUndefined(hValue);
|
| + 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();
|
| + v8::Isolate* pIsolate = GetIsolate();
|
| + v8::Locker lock(pIsolate);
|
| + v8::Isolate::Scope isolate_scope(pIsolate);
|
| + v8::HandleScope handle_scope(pIsolate);
|
| + v8::Local<v8::Context> context =
|
| + v8::Local<v8::Context>::New(pIsolate, m_context);
|
| + v8::Context::Scope context_scope(context);
|
| +
|
| + // v8::Local<v8::Context> tmpCotext =
|
| + // v8::Local<v8::Context>::New(GetIsolate(), m_context);
|
| + v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
|
| + GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
|
| + context->Global()->Set(
|
| + v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
|
| + utf8Name.GetLength()),
|
| + propvalue);
|
| +#endif
|
| + return TRUE;
|
| +}
|
| +
|
| +#endif
|
| void CJS_Runtime::AddObserver(Observer* observer) {
|
| ASSERT(m_observers.find(observer) == m_observers.end());
|
| m_observers.insert(observer);
|
|
|