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