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 "JS_Runtime.h" | 7 #include "JS_Runtime.h" |
8 | 8 |
| 9 #ifdef PDF_ENABLE_XFA |
| 10 #include "../../../xfa/src/fxjse/src/value.h" |
| 11 #include "../../include/fpdfxfa/fpdfxfa_app.h" |
| 12 #endif |
9 #include "Consts.h" | 13 #include "Consts.h" |
10 #include "Document.h" | 14 #include "Document.h" |
11 #include "Field.h" | 15 #include "Field.h" |
12 #include "Icon.h" | 16 #include "Icon.h" |
13 #include "JS_Context.h" | 17 #include "JS_Context.h" |
14 #include "JS_Define.h" | 18 #include "JS_Define.h" |
15 #include "JS_EventHandler.h" | 19 #include "JS_EventHandler.h" |
16 #include "JS_GlobalData.h" | 20 #include "JS_GlobalData.h" |
17 #include "JS_Object.h" | 21 #include "JS_Object.h" |
18 #include "JS_Value.h" | 22 #include "JS_Value.h" |
(...skipping 23 matching lines...) Expand all Loading... |
42 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); | 46 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
43 return pContext->GetJSRuntime(); | 47 return pContext->GetJSRuntime(); |
44 } | 48 } |
45 | 49 |
46 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) | 50 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
47 : m_pApp(pApp), | 51 : m_pApp(pApp), |
48 m_pDocument(NULL), | 52 m_pDocument(NULL), |
49 m_bBlocking(FALSE), | 53 m_bBlocking(FALSE), |
50 m_isolate(NULL), | 54 m_isolate(NULL), |
51 m_isolateManaged(false) { | 55 m_isolateManaged(false) { |
| 56 #ifndef PDF_ENABLE_XFA |
52 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 57 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
53 if (pPlatform->version <= 2) { | 58 if (pPlatform->version <= 2) { |
54 unsigned int embedderDataSlot = 0; | 59 unsigned int embedderDataSlot = 0; |
55 v8::Isolate* pExternalIsolate = nullptr; | 60 v8::Isolate* pExternalIsolate = nullptr; |
56 if (pPlatform->version == 2) { | 61 if (pPlatform->version == 2) { |
57 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 62 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
58 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 63 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 64 #else |
| 65 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 66 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 67 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
| 68 } else { |
| 69 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 70 if (pPlatform->version <= 2) { |
| 71 unsigned int embedderDataSlot = 0; |
| 72 v8::Isolate* pExternalIsolate = nullptr; |
| 73 if (pPlatform->version == 2) { |
| 74 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 75 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 76 } |
| 77 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 78 #endif |
59 } | 79 } |
| 80 #ifndef PDF_ENABLE_XFA |
60 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 81 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 82 #else |
| 83 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
61 } | 84 } |
| 85 |
| 86 v8::Isolate* isolate = m_isolate; |
| 87 v8::Isolate::Scope isolate_scope(isolate); |
| 88 v8::Locker locker(isolate); |
| 89 v8::HandleScope handle_scope(isolate); |
| 90 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
| 91 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 92 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
| 93 ReleaseContext(pContext); |
| 94 return; |
| 95 #endif |
| 96 } |
| 97 #ifndef PDF_ENABLE_XFA |
62 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | 98 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 99 #else |
| 100 |
| 101 #endif |
63 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 102 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
64 DefineJSObjects(); | 103 DefineJSObjects(); |
65 | 104 |
| 105 #ifdef PDF_ENABLE_XFA |
| 106 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
| 107 |
| 108 #endif |
66 CJS_Context* pContext = (CJS_Context*)NewContext(); | 109 CJS_Context* pContext = (CJS_Context*)NewContext(); |
67 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 110 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
68 ReleaseContext(pContext); | 111 ReleaseContext(pContext); |
69 } | 112 } |
70 | 113 |
71 CJS_Runtime::~CJS_Runtime() { | 114 CJS_Runtime::~CJS_Runtime() { |
72 for (auto* obs : m_observers) | 115 for (auto* obs : m_observers) |
73 obs->OnDestroyed(); | 116 obs->OnDestroyed(); |
74 | 117 |
75 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) | 118 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) |
76 delete m_ContextArray.GetAt(i); | 119 delete m_ContextArray.GetAt(i); |
77 | 120 |
78 m_ContextArray.RemoveAll(); | 121 m_ContextArray.RemoveAll(); |
| 122 #ifndef PDF_ENABLE_XFA |
79 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); | 123 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); |
| 124 #endif |
80 | 125 |
81 m_pApp = NULL; | 126 m_pApp = NULL; |
82 m_pDocument = NULL; | 127 m_pDocument = NULL; |
83 m_context.Reset(); | 128 m_context.Reset(); |
84 | 129 |
85 if (m_isolateManaged) | 130 if (m_isolateManaged) |
86 m_isolate->Dispose(); | 131 m_isolate->Dispose(); |
87 } | 132 } |
88 | 133 |
89 void CJS_Runtime::DefineJSObjects() { | 134 void CJS_Runtime::DefineJSObjects() { |
90 v8::Isolate::Scope isolate_scope(GetIsolate()); | 135 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 136 #ifdef PDF_ENABLE_XFA |
| 137 v8::Locker locker(GetIsolate()); |
| 138 #endif |
91 v8::HandleScope handle_scope(GetIsolate()); | 139 v8::HandleScope handle_scope(GetIsolate()); |
92 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | 140 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
93 v8::Context::Scope context_scope(context); | 141 v8::Context::Scope context_scope(context); |
94 | 142 |
95 // The call order determines the "ObjDefID" assigned to each class. | 143 // The call order determines the "ObjDefID" assigned to each class. |
96 // ObjDefIDs 0 - 2 | 144 // ObjDefIDs 0 - 2 |
97 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 145 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
98 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 146 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
99 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 147 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
100 | 148 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 201 |
154 IJS_Context* CJS_Runtime::GetCurrentContext() { | 202 IJS_Context* CJS_Runtime::GetCurrentContext() { |
155 if (!m_ContextArray.GetSize()) | 203 if (!m_ContextArray.GetSize()) |
156 return NULL; | 204 return NULL; |
157 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); | 205 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
158 } | 206 } |
159 | 207 |
160 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { | 208 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
161 if (m_pDocument != pReaderDoc) { | 209 if (m_pDocument != pReaderDoc) { |
162 v8::Isolate::Scope isolate_scope(m_isolate); | 210 v8::Isolate::Scope isolate_scope(m_isolate); |
| 211 #ifdef PDF_ENABLE_XFA |
| 212 v8::Locker locker(m_isolate); |
| 213 #endif |
163 v8::HandleScope handle_scope(m_isolate); | 214 v8::HandleScope handle_scope(m_isolate); |
164 v8::Local<v8::Context> context = | 215 v8::Local<v8::Context> context = |
165 v8::Local<v8::Context>::New(m_isolate, m_context); | 216 v8::Local<v8::Context>::New(m_isolate, m_context); |
166 v8::Context::Scope context_scope(context); | 217 v8::Context::Scope context_scope(context); |
167 | 218 |
168 m_pDocument = pReaderDoc; | 219 m_pDocument = pReaderDoc; |
169 if (pReaderDoc) { | 220 if (pReaderDoc) { |
170 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); | 221 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); |
171 if (!pThis.IsEmpty()) { | 222 if (!pThis.IsEmpty()) { |
172 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { | 223 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { |
(...skipping 25 matching lines...) Expand all Loading... |
198 } | 249 } |
199 | 250 |
200 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { | 251 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
201 m_FieldEventSet.erase(event); | 252 m_FieldEventSet.erase(event); |
202 } | 253 } |
203 | 254 |
204 v8::Local<v8::Context> CJS_Runtime::NewJSContext() { | 255 v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
205 return v8::Local<v8::Context>::New(m_isolate, m_context); | 256 return v8::Local<v8::Context>::New(m_isolate, m_context); |
206 } | 257 } |
207 | 258 |
| 259 #ifdef PDF_ENABLE_XFA |
| 260 CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 261 CFX_WideString sRet = str; |
| 262 sRet.Replace(L"_", L"."); |
| 263 return sRet; |
| 264 } |
| 265 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, |
| 266 FXJSE_HVALUE hValue) { |
| 267 #ifdef PDF_ENABLE_XFA |
| 268 const FX_CHAR* name = utf8Name.GetCStr(); |
| 269 |
| 270 v8::Locker lock(GetIsolate()); |
| 271 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 272 v8::HandleScope handle_scope(GetIsolate()); |
| 273 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); |
| 274 v8::Local<v8::Context> context = |
| 275 v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 276 v8::Context::Scope context_scope(context); |
| 277 |
| 278 // Caution: We're about to hand to XFA an object that in order to invoke |
| 279 // methods will require that the current v8::Context always has a pointer |
| 280 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates |
| 281 // its own v8::Context which has not initialized the embedder data slot. |
| 282 // Do so now. |
| 283 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's |
| 284 // embedder data slots, and/or to always use the right context. |
| 285 FXJS_SetRuntimeForV8Context(old_context, this); |
| 286 |
| 287 v8::Local<v8::Value> propvalue = |
| 288 context->Global()->Get(v8::String::NewFromUtf8( |
| 289 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
| 290 |
| 291 if (propvalue.IsEmpty()) { |
| 292 FXJSE_Value_SetUndefined(hValue); |
| 293 return FALSE; |
| 294 } |
| 295 ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue); |
| 296 #endif |
| 297 |
| 298 return TRUE; |
| 299 } |
| 300 FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, |
| 301 FXJSE_HVALUE hValue) { |
| 302 #ifdef PDF_ENABLE_XFA |
| 303 if (utf8Name.IsEmpty() || hValue == NULL) |
| 304 return FALSE; |
| 305 const FX_CHAR* name = utf8Name.GetCStr(); |
| 306 v8::Isolate* pIsolate = GetIsolate(); |
| 307 v8::Locker lock(pIsolate); |
| 308 v8::Isolate::Scope isolate_scope(pIsolate); |
| 309 v8::HandleScope handle_scope(pIsolate); |
| 310 v8::Local<v8::Context> context = |
| 311 v8::Local<v8::Context>::New(pIsolate, m_context); |
| 312 v8::Context::Scope context_scope(context); |
| 313 |
| 314 // v8::Local<v8::Context> tmpCotext = |
| 315 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 316 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( |
| 317 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 318 context->Global()->Set( |
| 319 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 320 utf8Name.GetLength()), |
| 321 propvalue); |
| 322 #endif |
| 323 return TRUE; |
| 324 } |
| 325 |
| 326 #endif |
208 void CJS_Runtime::AddObserver(Observer* observer) { | 327 void CJS_Runtime::AddObserver(Observer* observer) { |
209 ASSERT(m_observers.find(observer) == m_observers.end()); | 328 ASSERT(m_observers.find(observer) == m_observers.end()); |
210 m_observers.insert(observer); | 329 m_observers.insert(observer); |
211 } | 330 } |
212 | 331 |
213 void CJS_Runtime::RemoveObserver(Observer* observer) { | 332 void CJS_Runtime::RemoveObserver(Observer* observer) { |
214 ASSERT(m_observers.find(observer) != m_observers.end()); | 333 ASSERT(m_observers.find(observer) != m_observers.end()); |
215 m_observers.erase(observer); | 334 m_observers.erase(observer); |
216 } | 335 } |
OLD | NEW |