| 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 |
| 9 #include "../../../xfa/src/fxjse/src/value.h" | 10 #include "../../../xfa/src/fxjse/src/value.h" |
| 10 #include "../../include/fpdfxfa/fpdfxfa_app.h" | 11 #include "../../include/fpdfxfa/fpdfxfa_app.h" |
| 12 #endif |
| 11 #include "Consts.h" | 13 #include "Consts.h" |
| 12 #include "Document.h" | 14 #include "Document.h" |
| 13 #include "Field.h" | 15 #include "Field.h" |
| 14 #include "Icon.h" | 16 #include "Icon.h" |
| 15 #include "JS_Context.h" | 17 #include "JS_Context.h" |
| 16 #include "JS_Define.h" | 18 #include "JS_Define.h" |
| 17 #include "JS_EventHandler.h" | 19 #include "JS_EventHandler.h" |
| 18 #include "JS_GlobalData.h" | 20 #include "JS_GlobalData.h" |
| 19 #include "JS_Object.h" | 21 #include "JS_Object.h" |
| 20 #include "JS_Value.h" | 22 #include "JS_Value.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 44 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); | 46 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
| 45 return pContext->GetJSRuntime(); | 47 return pContext->GetJSRuntime(); |
| 46 } | 48 } |
| 47 | 49 |
| 48 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) | 50 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 49 : m_pApp(pApp), | 51 : m_pApp(pApp), |
| 50 m_pDocument(NULL), | 52 m_pDocument(NULL), |
| 51 m_bBlocking(FALSE), | 53 m_bBlocking(FALSE), |
| 52 m_isolate(NULL), | 54 m_isolate(NULL), |
| 53 m_isolateManaged(false) { | 55 m_isolateManaged(false) { |
| 56 #ifndef PDF_ENABLE_XFA |
| 57 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 58 if (pPlatform->version <= 2) { |
| 59 unsigned int embedderDataSlot = 0; |
| 60 v8::Isolate* pExternalIsolate = nullptr; |
| 61 if (pPlatform->version == 2) { |
| 62 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 63 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 64 #else |
| 54 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { | 65 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 55 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. | 66 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 56 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); | 67 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
| 57 } else { | 68 } else { |
| 58 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 69 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 59 if (pPlatform->version <= 2) { | 70 if (pPlatform->version <= 2) { |
| 60 unsigned int embedderDataSlot = 0; | 71 unsigned int embedderDataSlot = 0; |
| 61 v8::Isolate* pExternalIsolate = nullptr; | 72 v8::Isolate* pExternalIsolate = nullptr; |
| 62 if (pPlatform->version == 2) { | 73 if (pPlatform->version == 2) { |
| 63 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 74 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 64 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 75 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 65 } | 76 } |
| 66 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 77 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 78 #endif |
| 67 } | 79 } |
| 80 #ifndef PDF_ENABLE_XFA |
| 81 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 82 #else |
| 68 m_isolateManaged = FXJS_GetIsolate(&m_isolate); | 83 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 69 } | 84 } |
| 70 | 85 |
| 71 v8::Isolate* isolate = m_isolate; | 86 v8::Isolate* isolate = m_isolate; |
| 72 v8::Isolate::Scope isolate_scope(isolate); | 87 v8::Isolate::Scope isolate_scope(isolate); |
| 73 v8::Locker locker(isolate); | 88 v8::Locker locker(isolate); |
| 74 v8::HandleScope handle_scope(isolate); | 89 v8::HandleScope handle_scope(isolate); |
| 75 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { | 90 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
| 76 CJS_Context* pContext = (CJS_Context*)NewContext(); | 91 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 77 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 92 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
| 78 ReleaseContext(pContext); | 93 ReleaseContext(pContext); |
| 79 return; | 94 return; |
| 95 #endif |
| 80 } | 96 } |
| 97 #ifndef PDF_ENABLE_XFA |
| 98 m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 99 #else |
| 81 | 100 |
| 101 #endif |
| 82 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 102 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 83 DefineJSObjects(); | 103 DefineJSObjects(); |
| 84 | 104 |
| 105 #ifdef PDF_ENABLE_XFA |
| 85 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | 106 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
| 86 | 107 |
| 108 #endif |
| 87 CJS_Context* pContext = (CJS_Context*)NewContext(); | 109 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 88 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 110 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
| 89 ReleaseContext(pContext); | 111 ReleaseContext(pContext); |
| 90 } | 112 } |
| 91 | 113 |
| 92 CJS_Runtime::~CJS_Runtime() { | 114 CJS_Runtime::~CJS_Runtime() { |
| 93 for (auto* obs : m_observers) | 115 for (auto* obs : m_observers) |
| 94 obs->OnDestroyed(); | 116 obs->OnDestroyed(); |
| 95 | 117 |
| 96 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) | 118 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) |
| 97 delete m_ContextArray.GetAt(i); | 119 delete m_ContextArray.GetAt(i); |
| 98 | 120 |
| 99 m_ContextArray.RemoveAll(); | 121 m_ContextArray.RemoveAll(); |
| 122 #ifndef PDF_ENABLE_XFA |
| 123 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); |
| 124 #endif |
| 100 | 125 |
| 101 m_pApp = NULL; | 126 m_pApp = NULL; |
| 102 m_pDocument = NULL; | 127 m_pDocument = NULL; |
| 103 m_context.Reset(); | 128 m_context.Reset(); |
| 104 | 129 |
| 105 if (m_isolateManaged) | 130 if (m_isolateManaged) |
| 106 m_isolate->Dispose(); | 131 m_isolate->Dispose(); |
| 107 } | 132 } |
| 108 | 133 |
| 109 void CJS_Runtime::DefineJSObjects() { | 134 void CJS_Runtime::DefineJSObjects() { |
| 110 v8::Isolate::Scope isolate_scope(GetIsolate()); | 135 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 136 #ifdef PDF_ENABLE_XFA |
| 111 v8::Locker locker(GetIsolate()); | 137 v8::Locker locker(GetIsolate()); |
| 138 #endif |
| 112 v8::HandleScope handle_scope(GetIsolate()); | 139 v8::HandleScope handle_scope(GetIsolate()); |
| 113 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | 140 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 114 v8::Context::Scope context_scope(context); | 141 v8::Context::Scope context_scope(context); |
| 115 | 142 |
| 116 // The call order determines the "ObjDefID" assigned to each class. | 143 // The call order determines the "ObjDefID" assigned to each class. |
| 117 // ObjDefIDs 0 - 2 | 144 // ObjDefIDs 0 - 2 |
| 118 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 145 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 119 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 146 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 120 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 147 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 121 | 148 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 201 |
| 175 IJS_Context* CJS_Runtime::GetCurrentContext() { | 202 IJS_Context* CJS_Runtime::GetCurrentContext() { |
| 176 if (!m_ContextArray.GetSize()) | 203 if (!m_ContextArray.GetSize()) |
| 177 return NULL; | 204 return NULL; |
| 178 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); | 205 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); |
| 179 } | 206 } |
| 180 | 207 |
| 181 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { | 208 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 182 if (m_pDocument != pReaderDoc) { | 209 if (m_pDocument != pReaderDoc) { |
| 183 v8::Isolate::Scope isolate_scope(m_isolate); | 210 v8::Isolate::Scope isolate_scope(m_isolate); |
| 211 #ifdef PDF_ENABLE_XFA |
| 184 v8::Locker locker(m_isolate); | 212 v8::Locker locker(m_isolate); |
| 213 #endif |
| 185 v8::HandleScope handle_scope(m_isolate); | 214 v8::HandleScope handle_scope(m_isolate); |
| 186 v8::Local<v8::Context> context = | 215 v8::Local<v8::Context> context = |
| 187 v8::Local<v8::Context>::New(m_isolate, m_context); | 216 v8::Local<v8::Context>::New(m_isolate, m_context); |
| 188 v8::Context::Scope context_scope(context); | 217 v8::Context::Scope context_scope(context); |
| 189 | 218 |
| 190 m_pDocument = pReaderDoc; | 219 m_pDocument = pReaderDoc; |
| 191 if (pReaderDoc) { | 220 if (pReaderDoc) { |
| 192 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); | 221 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); |
| 193 if (!pThis.IsEmpty()) { | 222 if (!pThis.IsEmpty()) { |
| 194 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { | 223 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 220 } | 249 } |
| 221 | 250 |
| 222 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { | 251 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
| 223 m_FieldEventSet.erase(event); | 252 m_FieldEventSet.erase(event); |
| 224 } | 253 } |
| 225 | 254 |
| 226 v8::Local<v8::Context> CJS_Runtime::NewJSContext() { | 255 v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 227 return v8::Local<v8::Context>::New(m_isolate, m_context); | 256 return v8::Local<v8::Context>::New(m_isolate, m_context); |
| 228 } | 257 } |
| 229 | 258 |
| 259 #ifdef PDF_ENABLE_XFA |
| 230 CFX_WideString ChangeObjName(const CFX_WideString& str) { | 260 CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 231 CFX_WideString sRet = str; | 261 CFX_WideString sRet = str; |
| 232 sRet.Replace(L"_", L"."); | 262 sRet.Replace(L"_", L"."); |
| 233 return sRet; | 263 return sRet; |
| 234 } | 264 } |
| 235 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, | 265 FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, |
| 236 FXJSE_HVALUE hValue) { | 266 FXJSE_HVALUE hValue) { |
| 237 #ifdef PDF_ENABLE_XFA | 267 #ifdef PDF_ENABLE_XFA |
| 238 const FX_CHAR* name = utf8Name.GetCStr(); | 268 const FX_CHAR* name = utf8Name.GetCStr(); |
| 239 | 269 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( | 316 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( |
| 287 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); | 317 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); |
| 288 context->Global()->Set( | 318 context->Global()->Set( |
| 289 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 319 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 290 utf8Name.GetLength()), | 320 utf8Name.GetLength()), |
| 291 propvalue); | 321 propvalue); |
| 292 #endif | 322 #endif |
| 293 return TRUE; | 323 return TRUE; |
| 294 } | 324 } |
| 295 | 325 |
| 326 #endif |
| 296 void CJS_Runtime::AddObserver(Observer* observer) { | 327 void CJS_Runtime::AddObserver(Observer* observer) { |
| 297 ASSERT(m_observers.find(observer) == m_observers.end()); | 328 ASSERT(m_observers.find(observer) == m_observers.end()); |
| 298 m_observers.insert(observer); | 329 m_observers.insert(observer); |
| 299 } | 330 } |
| 300 | 331 |
| 301 void CJS_Runtime::RemoveObserver(Observer* observer) { | 332 void CJS_Runtime::RemoveObserver(Observer* observer) { |
| 302 ASSERT(m_observers.find(observer) != m_observers.end()); | 333 ASSERT(m_observers.find(observer) != m_observers.end()); |
| 303 m_observers.erase(observer); | 334 m_observers.erase(observer); |
| 304 } | 335 } |
| OLD | NEW |