| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "fpdfsdk/javascript/cjs_runtime.h" | 7 #include "fpdfsdk/javascript/cjs_runtime.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { | 61 CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { |
| 62 return static_cast<CJS_Runtime*>( | 62 return static_cast<CJS_Runtime*>( |
| 63 CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); | 63 CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) | 66 CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) |
| 67 : m_pFormFillEnv(pFormFillEnv), | 67 : m_pFormFillEnv(pFormFillEnv), |
| 68 m_bBlocking(false), | 68 m_bBlocking(false), |
| 69 m_isolateManaged(false) { | 69 m_isolateManaged(false) { |
| 70 v8::Isolate* pIsolate = nullptr; | 70 v8::Isolate* pIsolate = nullptr; |
| 71 #ifndef PDF_ENABLE_XFA | 71 |
| 72 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; | 72 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; |
| 73 if (pPlatform->version <= 2) { | 73 if (pPlatform->version <= 2) { |
| 74 unsigned int embedderDataSlot = 0; | 74 unsigned int embedderDataSlot = 0; |
| 75 v8::Isolate* pExternalIsolate = nullptr; | 75 v8::Isolate* pExternalIsolate = nullptr; |
| 76 if (pPlatform->version == 2) { | 76 if (pPlatform->version == 2) { |
| 77 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 77 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 78 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 78 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 79 } | 79 } |
| 80 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 80 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 81 } | 81 } |
| 82 m_isolateManaged = FXJS_GetIsolate(&pIsolate); | 82 m_isolateManaged = FXJS_GetIsolate(&pIsolate); |
| 83 SetIsolate(pIsolate); | 83 SetIsolate(pIsolate); |
| 84 #else | |
| 85 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { | |
| 86 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. | |
| 87 pIsolate = CPDFXFA_App::GetInstance()->GetJSERuntime(); | |
| 88 SetIsolate(pIsolate); | |
| 89 } else { | |
| 90 IPDF_JSPLATFORM* pPlatform = | |
| 91 m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; | |
| 92 if (pPlatform->version <= 2) { | |
| 93 unsigned int embedderDataSlot = 0; | |
| 94 v8::Isolate* pExternalIsolate = nullptr; | |
| 95 if (pPlatform->version == 2) { | |
| 96 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | |
| 97 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | |
| 98 } | |
| 99 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | |
| 100 } | |
| 101 m_isolateManaged = FXJS_GetIsolate(&pIsolate); | |
| 102 SetIsolate(pIsolate); | |
| 103 } | |
| 104 | 84 |
| 85 #ifdef PDF_ENABLE_XFA |
| 105 v8::Isolate::Scope isolate_scope(pIsolate); | 86 v8::Isolate::Scope isolate_scope(pIsolate); |
| 106 v8::HandleScope handle_scope(pIsolate); | 87 v8::HandleScope handle_scope(pIsolate); |
| 107 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { | |
| 108 CJS_Context* pContext = (CJS_Context*)NewContext(); | |
| 109 InitializeEngine(); | |
| 110 ReleaseContext(pContext); | |
| 111 return; | |
| 112 } | |
| 113 #endif | 88 #endif |
| 114 | 89 |
| 115 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 90 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 116 DefineJSObjects(); | 91 DefineJSObjects(); |
| 117 | 92 |
| 118 #ifdef PDF_ENABLE_XFA | |
| 119 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | |
| 120 #endif | |
| 121 | |
| 122 CJS_Context* pContext = (CJS_Context*)NewContext(); | 93 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 123 InitializeEngine(); | 94 InitializeEngine(); |
| 124 ReleaseContext(pContext); | 95 ReleaseContext(pContext); |
| 125 | 96 |
| 126 SetFormFillEnvToDocument(); | 97 SetFormFillEnvToDocument(); |
| 127 } | 98 } |
| 128 | 99 |
| 129 CJS_Runtime::~CJS_Runtime() { | 100 CJS_Runtime::~CJS_Runtime() { |
| 130 NotifyObservedPtrs(); | 101 NotifyObservedPtrs(); |
| 131 ReleaseEngine(); | 102 ReleaseEngine(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 CFX_WideString sRet = str; | 225 CFX_WideString sRet = str; |
| 255 sRet.Replace(L"_", L"."); | 226 sRet.Replace(L"_", L"."); |
| 256 return sRet; | 227 return sRet; |
| 257 } | 228 } |
| 258 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, | 229 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, |
| 259 CFXJSE_Value* pValue) { | 230 CFXJSE_Value* pValue) { |
| 260 const FX_CHAR* name = utf8Name.c_str(); | 231 const FX_CHAR* name = utf8Name.c_str(); |
| 261 | 232 |
| 262 v8::Isolate::Scope isolate_scope(GetIsolate()); | 233 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 263 v8::HandleScope handle_scope(GetIsolate()); | 234 v8::HandleScope handle_scope(GetIsolate()); |
| 264 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); | |
| 265 v8::Local<v8::Context> context = NewLocalContext(); | 235 v8::Local<v8::Context> context = NewLocalContext(); |
| 266 v8::Context::Scope context_scope(context); | 236 v8::Context::Scope context_scope(context); |
| 267 | 237 |
| 268 // Caution: We're about to hand to XFA an object that in order to invoke | |
| 269 // methods will require that the current v8::Context always has a pointer | |
| 270 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates | |
| 271 // its own v8::Context which has not initialized the embedder data slot. | |
| 272 // Do so now. | |
| 273 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's | |
| 274 // embedder data slots, and/or to always use the right context. | |
| 275 CFXJS_Engine::SetForV8Context(old_context, this); | |
| 276 | |
| 277 v8::Local<v8::Value> propvalue = | 238 v8::Local<v8::Value> propvalue = |
| 278 context->Global()->Get(v8::String::NewFromUtf8( | 239 context->Global()->Get(v8::String::NewFromUtf8( |
| 279 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); | 240 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
| 280 | 241 |
| 281 if (propvalue.IsEmpty()) { | 242 if (propvalue.IsEmpty()) { |
| 282 pValue->SetUndefined(); | 243 pValue->SetUndefined(); |
| 283 return FALSE; | 244 return FALSE; |
| 284 } | 245 } |
| 285 pValue->ForceSetValue(propvalue); | 246 pValue->ForceSetValue(propvalue); |
| 286 return TRUE; | 247 return TRUE; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 300 // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 261 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 301 v8::Local<v8::Value> propvalue = | 262 v8::Local<v8::Value> propvalue = |
| 302 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); | 263 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
| 303 context->Global()->Set( | 264 context->Global()->Set( |
| 304 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 265 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 305 utf8Name.GetLength()), | 266 utf8Name.GetLength()), |
| 306 propvalue); | 267 propvalue); |
| 307 return TRUE; | 268 return TRUE; |
| 308 } | 269 } |
| 309 #endif | 270 #endif |
| OLD | NEW |