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