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 | 71 #ifndef PDF_ENABLE_XFA |
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 } |
84 | 104 |
85 #ifdef PDF_ENABLE_XFA | |
86 v8::Isolate::Scope isolate_scope(pIsolate); | 105 v8::Isolate::Scope isolate_scope(pIsolate); |
87 v8::HandleScope handle_scope(pIsolate); | 106 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 } |
88 #endif | 113 #endif |
89 | 114 |
90 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 115 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
91 DefineJSObjects(); | 116 DefineJSObjects(); |
92 | 117 |
| 118 #ifdef PDF_ENABLE_XFA |
| 119 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
| 120 #endif |
| 121 |
93 CJS_Context* pContext = (CJS_Context*)NewContext(); | 122 CJS_Context* pContext = (CJS_Context*)NewContext(); |
94 InitializeEngine(); | 123 InitializeEngine(); |
95 ReleaseContext(pContext); | 124 ReleaseContext(pContext); |
96 | 125 |
97 SetFormFillEnvToDocument(); | 126 SetFormFillEnvToDocument(); |
98 } | 127 } |
99 | 128 |
100 CJS_Runtime::~CJS_Runtime() { | 129 CJS_Runtime::~CJS_Runtime() { |
101 NotifyObservedPtrs(); | 130 NotifyObservedPtrs(); |
102 ReleaseEngine(); | 131 ReleaseEngine(); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 CFX_WideString sRet = str; | 254 CFX_WideString sRet = str; |
226 sRet.Replace(L"_", L"."); | 255 sRet.Replace(L"_", L"."); |
227 return sRet; | 256 return sRet; |
228 } | 257 } |
229 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, | 258 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, |
230 CFXJSE_Value* pValue) { | 259 CFXJSE_Value* pValue) { |
231 const FX_CHAR* name = utf8Name.c_str(); | 260 const FX_CHAR* name = utf8Name.c_str(); |
232 | 261 |
233 v8::Isolate::Scope isolate_scope(GetIsolate()); | 262 v8::Isolate::Scope isolate_scope(GetIsolate()); |
234 v8::HandleScope handle_scope(GetIsolate()); | 263 v8::HandleScope handle_scope(GetIsolate()); |
| 264 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); |
235 v8::Local<v8::Context> context = NewLocalContext(); | 265 v8::Local<v8::Context> context = NewLocalContext(); |
236 v8::Context::Scope context_scope(context); | 266 v8::Context::Scope context_scope(context); |
237 | 267 |
| 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 |
238 v8::Local<v8::Value> propvalue = | 277 v8::Local<v8::Value> propvalue = |
239 context->Global()->Get(v8::String::NewFromUtf8( | 278 context->Global()->Get(v8::String::NewFromUtf8( |
240 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); | 279 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
241 | 280 |
242 if (propvalue.IsEmpty()) { | 281 if (propvalue.IsEmpty()) { |
243 pValue->SetUndefined(); | 282 pValue->SetUndefined(); |
244 return FALSE; | 283 return FALSE; |
245 } | 284 } |
246 pValue->ForceSetValue(propvalue); | 285 pValue->ForceSetValue(propvalue); |
247 return TRUE; | 286 return TRUE; |
(...skipping 13 matching lines...) Expand all Loading... |
261 // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 300 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
262 v8::Local<v8::Value> propvalue = | 301 v8::Local<v8::Value> propvalue = |
263 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); | 302 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
264 context->Global()->Set( | 303 context->Global()->Set( |
265 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 304 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
266 utf8Name.GetLength()), | 305 utf8Name.GetLength()), |
267 propvalue); | 306 propvalue); |
268 return TRUE; | 307 return TRUE; |
269 } | 308 } |
270 #endif | 309 #endif |
OLD | NEW |