| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 return new CJS_Runtime(pEnv); | 49 return new CJS_Runtime(pEnv); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // static | 52 // static |
| 53 CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { | 53 CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { |
| 54 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); | 54 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
| 55 return pContext->GetJSRuntime(); | 55 return pContext->GetJSRuntime(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) | 58 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 59 : m_pApp(pApp), | 59 : m_pApp(pApp), m_pDocument(nullptr), m_bBlocking(FALSE) { |
| 60 m_pDocument(nullptr), | |
| 61 m_bBlocking(FALSE), | |
| 62 m_isolate(nullptr), | |
| 63 m_isolateManaged(false) { | |
| 64 #ifndef PDF_ENABLE_XFA | 60 #ifndef PDF_ENABLE_XFA |
| 65 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 61 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 66 if (pPlatform->version <= 2) { | 62 if (pPlatform->version <= 2) { |
| 67 unsigned int embedderDataSlot = 0; | 63 unsigned int embedderDataSlot = 0; |
| 68 v8::Isolate* pExternalIsolate = nullptr; | 64 v8::Isolate* pExternalIsolate = nullptr; |
| 69 if (pPlatform->version == 2) { | 65 if (pPlatform->version == 2) { |
| 70 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 66 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 71 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 67 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 72 } | 68 } |
| 73 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 69 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 89 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 85 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 90 } | 86 } |
| 91 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | 87 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 92 } | 88 } |
| 93 | 89 |
| 94 v8::Isolate* isolate = m_isolate; | 90 v8::Isolate* isolate = m_isolate; |
| 95 v8::Isolate::Scope isolate_scope(isolate); | 91 v8::Isolate::Scope isolate_scope(isolate); |
| 96 v8::HandleScope handle_scope(isolate); | 92 v8::HandleScope handle_scope(isolate); |
| 97 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { | 93 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
| 98 CJS_Context* pContext = (CJS_Context*)NewContext(); | 94 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 99 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 95 FXJS_InitializeEngine(GetIsolate(), this, &m_context, &m_StaticObjects); |
| 100 ReleaseContext(pContext); | 96 ReleaseContext(pContext); |
| 101 return; | 97 return; |
| 102 } | 98 } |
| 103 #endif | 99 #endif |
| 104 | 100 |
| 105 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 101 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 106 DefineJSObjects(); | 102 DefineJSObjects(); |
| 107 | 103 |
| 108 #ifdef PDF_ENABLE_XFA | 104 #ifdef PDF_ENABLE_XFA |
| 109 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | 105 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
| 110 #endif | 106 #endif |
| 111 | 107 |
| 112 CJS_Context* pContext = (CJS_Context*)NewContext(); | 108 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 113 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 109 FXJS_InitializeEngine(GetIsolate(), this, &m_context, &m_StaticObjects); |
| 114 ReleaseContext(pContext); | 110 ReleaseContext(pContext); |
| 115 } | 111 } |
| 116 | 112 |
| 117 CJS_Runtime::~CJS_Runtime() { | 113 CJS_Runtime::~CJS_Runtime() { |
| 118 for (auto* obs : m_observers) | 114 for (auto* obs : m_observers) |
| 119 obs->OnDestroyed(); | 115 obs->OnDestroyed(); |
| 120 | 116 |
| 121 m_ContextArray.clear(); | 117 m_ContextArray.clear(); |
| 122 m_ConstArrays.clear(); | 118 m_ConstArrays.clear(); |
| 123 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); | 119 FXJS_ReleaseEngine(GetIsolate(), &m_context, &m_StaticObjects); |
| 124 m_context.Reset(); | 120 m_context.Reset(); |
| 125 if (m_isolateManaged) | 121 if (m_isolateManaged) |
| 126 m_isolate->Dispose(); | 122 m_isolate->Dispose(); |
| 127 } | 123 } |
| 128 | 124 |
| 129 void CJS_Runtime::DefineJSObjects() { | 125 void CJS_Runtime::DefineJSObjects() { |
| 130 v8::Isolate::Scope isolate_scope(GetIsolate()); | 126 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 131 v8::HandleScope handle_scope(GetIsolate()); | 127 v8::HandleScope handle_scope(GetIsolate()); |
| 132 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | 128 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 133 v8::Context::Scope context_scope(context); | 129 v8::Context::Scope context_scope(context); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 v8::Local<v8::Context>::New(GetIsolate(), m_context); | 273 v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 278 v8::Context::Scope context_scope(context); | 274 v8::Context::Scope context_scope(context); |
| 279 | 275 |
| 280 // Caution: We're about to hand to XFA an object that in order to invoke | 276 // Caution: We're about to hand to XFA an object that in order to invoke |
| 281 // methods will require that the current v8::Context always has a pointer | 277 // methods will require that the current v8::Context always has a pointer |
| 282 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates | 278 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates |
| 283 // its own v8::Context which has not initialized the embedder data slot. | 279 // its own v8::Context which has not initialized the embedder data slot. |
| 284 // Do so now. | 280 // Do so now. |
| 285 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's | 281 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's |
| 286 // embedder data slots, and/or to always use the right context. | 282 // embedder data slots, and/or to always use the right context. |
| 287 FXJS_SetRuntimeForV8Context(old_context, this); | 283 FXJS_SetEngineForV8Context(old_context, this); |
| 288 | 284 |
| 289 v8::Local<v8::Value> propvalue = | 285 v8::Local<v8::Value> propvalue = |
| 290 context->Global()->Get(v8::String::NewFromUtf8( | 286 context->Global()->Get(v8::String::NewFromUtf8( |
| 291 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); | 287 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
| 292 | 288 |
| 293 if (propvalue.IsEmpty()) { | 289 if (propvalue.IsEmpty()) { |
| 294 pValue->SetUndefined(); | 290 pValue->SetUndefined(); |
| 295 return FALSE; | 291 return FALSE; |
| 296 } | 292 } |
| 297 pValue->ForceSetValue(propvalue); | 293 pValue->ForceSetValue(propvalue); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 313 // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 309 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 314 v8::Local<v8::Value> propvalue = | 310 v8::Local<v8::Value> propvalue = |
| 315 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); | 311 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
| 316 context->Global()->Set( | 312 context->Global()->Set( |
| 317 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 313 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 318 utf8Name.GetLength()), | 314 utf8Name.GetLength()), |
| 319 propvalue); | 315 propvalue); |
| 320 return TRUE; | 316 return TRUE; |
| 321 } | 317 } |
| 322 #endif | 318 #endif |
| OLD | NEW |