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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 m_isolate(nullptr), | 57 m_isolate(nullptr), |
58 m_isolateManaged(false) { | 58 m_isolateManaged(false) { |
59 #ifndef PDF_ENABLE_XFA | 59 #ifndef PDF_ENABLE_XFA |
60 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 60 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
61 if (pPlatform->version <= 2) { | 61 if (pPlatform->version <= 2) { |
62 unsigned int embedderDataSlot = 0; | 62 unsigned int embedderDataSlot = 0; |
63 v8::Isolate* pExternalIsolate = nullptr; | 63 v8::Isolate* pExternalIsolate = nullptr; |
64 if (pPlatform->version == 2) { | 64 if (pPlatform->version == 2) { |
65 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 65 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
66 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 66 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
67 } | |
68 | |
Tom Sepez
2016/06/09 21:07:15
nit: no blank line here.
dsinclair
2016/06/09 21:09:29
Done.
| |
69 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | |
70 } | |
71 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | |
67 #else | 72 #else |
68 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { | 73 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
69 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. | 74 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
70 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); | 75 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
71 } else { | 76 } else { |
72 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 77 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
73 if (pPlatform->version <= 2) { | 78 if (pPlatform->version <= 2) { |
74 unsigned int embedderDataSlot = 0; | 79 unsigned int embedderDataSlot = 0; |
75 v8::Isolate* pExternalIsolate = nullptr; | 80 v8::Isolate* pExternalIsolate = nullptr; |
76 if (pPlatform->version == 2) { | 81 if (pPlatform->version == 2) { |
77 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 82 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
78 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 83 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
79 } | 84 } |
80 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 85 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
81 #endif | |
82 } | 86 } |
83 #ifndef PDF_ENABLE_XFA | |
84 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | |
85 #else | |
86 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | 87 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
87 } | 88 } |
88 | 89 |
89 v8::Isolate* isolate = m_isolate; | 90 v8::Isolate* isolate = m_isolate; |
90 v8::Isolate::Scope isolate_scope(isolate); | 91 v8::Isolate::Scope isolate_scope(isolate); |
91 v8::HandleScope handle_scope(isolate); | 92 v8::HandleScope handle_scope(isolate); |
92 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { | 93 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
93 CJS_Context* pContext = (CJS_Context*)NewContext(); | 94 CJS_Context* pContext = (CJS_Context*)NewContext(); |
94 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 95 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
95 ReleaseContext(pContext); | 96 ReleaseContext(pContext); |
96 return; | 97 return; |
98 } | |
97 #endif | 99 #endif |
98 } | |
99 #ifndef PDF_ENABLE_XFA | |
100 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | |
101 #else | |
102 | 100 |
103 #endif | |
104 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 101 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
105 DefineJSObjects(); | 102 DefineJSObjects(); |
106 | 103 |
107 #ifdef PDF_ENABLE_XFA | 104 #ifdef PDF_ENABLE_XFA |
108 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | 105 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
106 #endif | |
109 | 107 |
110 #endif | |
111 CJS_Context* pContext = (CJS_Context*)NewContext(); | 108 CJS_Context* pContext = (CJS_Context*)NewContext(); |
112 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 109 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
113 ReleaseContext(pContext); | 110 ReleaseContext(pContext); |
114 } | 111 } |
115 | 112 |
116 CJS_Runtime::~CJS_Runtime() { | 113 CJS_Runtime::~CJS_Runtime() { |
117 for (auto* obs : m_observers) | 114 for (auto* obs : m_observers) |
118 obs->OnDestroyed(); | 115 obs->OnDestroyed(); |
119 | 116 |
120 m_ContextArray.clear(); | 117 m_ContextArray.clear(); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 | 235 |
239 void CJS_Runtime::SetConstArray(const CFX_WideString& name, | 236 void CJS_Runtime::SetConstArray(const CFX_WideString& name, |
240 v8::Local<v8::Array> array) { | 237 v8::Local<v8::Array> array) { |
241 m_ConstArrays[name] = v8::Global<v8::Array>(m_isolate, array); | 238 m_ConstArrays[name] = v8::Global<v8::Array>(m_isolate, array); |
242 } | 239 } |
243 | 240 |
244 v8::Local<v8::Array> CJS_Runtime::GetConstArray(const CFX_WideString& name) { | 241 v8::Local<v8::Array> CJS_Runtime::GetConstArray(const CFX_WideString& name) { |
245 return v8::Local<v8::Array>::New(m_isolate, m_ConstArrays[name]); | 242 return v8::Local<v8::Array>::New(m_isolate, m_ConstArrays[name]); |
246 } | 243 } |
247 | 244 |
245 void CJS_Runtime::AddObserver(Observer* observer) { | |
246 ASSERT(!pdfium::ContainsKey(m_observers, observer)); | |
247 m_observers.insert(observer); | |
248 } | |
249 | |
250 void CJS_Runtime::RemoveObserver(Observer* observer) { | |
251 ASSERT(pdfium::ContainsKey(m_observers, observer)); | |
252 m_observers.erase(observer); | |
253 } | |
254 | |
248 #ifdef PDF_ENABLE_XFA | 255 #ifdef PDF_ENABLE_XFA |
249 CFX_WideString ChangeObjName(const CFX_WideString& str) { | 256 CFX_WideString ChangeObjName(const CFX_WideString& str) { |
250 CFX_WideString sRet = str; | 257 CFX_WideString sRet = str; |
251 sRet.Replace(L"_", L"."); | 258 sRet.Replace(L"_", L"."); |
252 return sRet; | 259 return sRet; |
253 } | 260 } |
254 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, | 261 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, |
255 CFXJSE_Value* pValue) { | 262 CFXJSE_Value* pValue) { |
256 #ifdef PDF_ENABLE_XFA | |
257 const FX_CHAR* name = utf8Name.c_str(); | 263 const FX_CHAR* name = utf8Name.c_str(); |
258 | 264 |
259 v8::Isolate::Scope isolate_scope(GetIsolate()); | 265 v8::Isolate::Scope isolate_scope(GetIsolate()); |
260 v8::HandleScope handle_scope(GetIsolate()); | 266 v8::HandleScope handle_scope(GetIsolate()); |
261 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); | 267 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); |
262 v8::Local<v8::Context> context = | 268 v8::Local<v8::Context> context = |
263 v8::Local<v8::Context>::New(GetIsolate(), m_context); | 269 v8::Local<v8::Context>::New(GetIsolate(), m_context); |
264 v8::Context::Scope context_scope(context); | 270 v8::Context::Scope context_scope(context); |
265 | 271 |
266 // Caution: We're about to hand to XFA an object that in order to invoke | 272 // Caution: We're about to hand to XFA an object that in order to invoke |
267 // methods will require that the current v8::Context always has a pointer | 273 // methods will require that the current v8::Context always has a pointer |
268 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates | 274 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates |
269 // its own v8::Context which has not initialized the embedder data slot. | 275 // its own v8::Context which has not initialized the embedder data slot. |
270 // Do so now. | 276 // Do so now. |
271 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's | 277 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's |
272 // embedder data slots, and/or to always use the right context. | 278 // embedder data slots, and/or to always use the right context. |
273 FXJS_SetRuntimeForV8Context(old_context, this); | 279 FXJS_SetRuntimeForV8Context(old_context, this); |
274 | 280 |
275 v8::Local<v8::Value> propvalue = | 281 v8::Local<v8::Value> propvalue = |
276 context->Global()->Get(v8::String::NewFromUtf8( | 282 context->Global()->Get(v8::String::NewFromUtf8( |
277 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); | 283 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
278 | 284 |
279 if (propvalue.IsEmpty()) { | 285 if (propvalue.IsEmpty()) { |
280 pValue->SetUndefined(); | 286 pValue->SetUndefined(); |
281 return FALSE; | 287 return FALSE; |
282 } | 288 } |
283 pValue->ForceSetValue(propvalue); | 289 pValue->ForceSetValue(propvalue); |
284 #endif | |
285 | |
286 return TRUE; | 290 return TRUE; |
287 } | 291 } |
288 FX_BOOL CJS_Runtime::SetValueByName(const CFX_ByteStringC& utf8Name, | 292 FX_BOOL CJS_Runtime::SetValueByName(const CFX_ByteStringC& utf8Name, |
289 CFXJSE_Value* pValue) { | 293 CFXJSE_Value* pValue) { |
290 #ifdef PDF_ENABLE_XFA | |
291 if (utf8Name.IsEmpty() || !pValue) | 294 if (utf8Name.IsEmpty() || !pValue) |
292 return FALSE; | 295 return FALSE; |
293 const FX_CHAR* name = utf8Name.c_str(); | 296 const FX_CHAR* name = utf8Name.c_str(); |
294 v8::Isolate* pIsolate = GetIsolate(); | 297 v8::Isolate* pIsolate = GetIsolate(); |
295 v8::Isolate::Scope isolate_scope(pIsolate); | 298 v8::Isolate::Scope isolate_scope(pIsolate); |
296 v8::HandleScope handle_scope(pIsolate); | 299 v8::HandleScope handle_scope(pIsolate); |
297 v8::Local<v8::Context> context = | 300 v8::Local<v8::Context> context = |
298 v8::Local<v8::Context>::New(pIsolate, m_context); | 301 v8::Local<v8::Context>::New(pIsolate, m_context); |
299 v8::Context::Scope context_scope(context); | 302 v8::Context::Scope context_scope(context); |
300 | 303 |
301 // v8::Local<v8::Context> tmpCotext = | 304 // v8::Local<v8::Context> tmpCotext = |
302 // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 305 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
303 v8::Local<v8::Value> propvalue = | 306 v8::Local<v8::Value> propvalue = |
304 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); | 307 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
305 context->Global()->Set( | 308 context->Global()->Set( |
306 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 309 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
307 utf8Name.GetLength()), | 310 utf8Name.GetLength()), |
308 propvalue); | 311 propvalue); |
309 #endif | |
310 return TRUE; | 312 return TRUE; |
311 } | 313 } |
312 | |
313 #endif | 314 #endif |
314 void CJS_Runtime::AddObserver(Observer* observer) { | |
315 ASSERT(!pdfium::ContainsKey(m_observers, observer)); | |
316 m_observers.insert(observer); | |
317 } | |
318 | |
319 void CJS_Runtime::RemoveObserver(Observer* observer) { | |
320 ASSERT(pdfium::ContainsKey(m_observers, observer)); | |
321 m_observers.erase(observer); | |
322 } | |
OLD | NEW |