| 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 29 matching lines...) Expand all Loading... |
| 40 void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { | 40 void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { |
| 41 FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); | 41 FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // static | 44 // static |
| 45 void IJS_Runtime::Destroy() { | 45 void IJS_Runtime::Destroy() { |
| 46 FXJS_Release(); | 46 FXJS_Release(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pEnv) { | 50 IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) { |
| 51 return new CJS_Runtime(pEnv); | 51 return new CJS_Runtime(pFormFillEnv); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // static | 54 // static |
| 55 CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { | 55 CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { |
| 56 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); | 56 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
| 57 return pContext->GetJSRuntime(); | 57 return pContext->GetJSRuntime(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // static | 60 // static |
| 61 CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { | 61 CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { |
| 62 return static_cast<CJS_Runtime*>( | 62 return static_cast<CJS_Runtime*>( |
| 63 CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); | 63 CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pEnv) | 66 CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) |
| 67 : m_pEnv(pEnv), | 67 : m_pFormFillEnv(pFormFillEnv), |
| 68 m_pDocument(nullptr), | |
| 69 m_bBlocking(false), | 68 m_bBlocking(false), |
| 70 m_isolateManaged(false) { | 69 m_isolateManaged(false) { |
| 71 v8::Isolate* pIsolate = nullptr; | 70 v8::Isolate* pIsolate = nullptr; |
| 72 #ifndef PDF_ENABLE_XFA | 71 #ifndef PDF_ENABLE_XFA |
| 73 IPDF_JSPLATFORM* pPlatform = m_pEnv->GetFormFillInfo()->m_pJsPlatform; | 72 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; |
| 74 if (pPlatform->version <= 2) { | 73 if (pPlatform->version <= 2) { |
| 75 unsigned int embedderDataSlot = 0; | 74 unsigned int embedderDataSlot = 0; |
| 76 v8::Isolate* pExternalIsolate = nullptr; | 75 v8::Isolate* pExternalIsolate = nullptr; |
| 77 if (pPlatform->version == 2) { | 76 if (pPlatform->version == 2) { |
| 78 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 77 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 79 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 78 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 80 } | 79 } |
| 81 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 80 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 82 } | 81 } |
| 83 m_isolateManaged = FXJS_GetIsolate(&pIsolate); | 82 m_isolateManaged = FXJS_GetIsolate(&pIsolate); |
| 84 SetIsolate(pIsolate); | 83 SetIsolate(pIsolate); |
| 85 #else | 84 #else |
| 86 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { | 85 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 87 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. | 86 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 88 pIsolate = CPDFXFA_App::GetInstance()->GetJSERuntime(); | 87 pIsolate = CPDFXFA_App::GetInstance()->GetJSERuntime(); |
| 89 SetIsolate(pIsolate); | 88 SetIsolate(pIsolate); |
| 90 } else { | 89 } else { |
| 91 IPDF_JSPLATFORM* pPlatform = m_pEnv->GetFormFillInfo()->m_pJsPlatform; | 90 IPDF_JSPLATFORM* pPlatform = |
| 91 m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; |
| 92 if (pPlatform->version <= 2) { | 92 if (pPlatform->version <= 2) { |
| 93 unsigned int embedderDataSlot = 0; | 93 unsigned int embedderDataSlot = 0; |
| 94 v8::Isolate* pExternalIsolate = nullptr; | 94 v8::Isolate* pExternalIsolate = nullptr; |
| 95 if (pPlatform->version == 2) { | 95 if (pPlatform->version == 2) { |
| 96 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 96 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 97 embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 97 embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 98 } | 98 } |
| 99 FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 99 FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 100 } | 100 } |
| 101 m_isolateManaged = FXJS_GetIsolate(&pIsolate); | 101 m_isolateManaged = FXJS_GetIsolate(&pIsolate); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) | 115 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 116 DefineJSObjects(); | 116 DefineJSObjects(); |
| 117 | 117 |
| 118 #ifdef PDF_ENABLE_XFA | 118 #ifdef PDF_ENABLE_XFA |
| 119 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | 119 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
| 120 #endif | 120 #endif |
| 121 | 121 |
| 122 CJS_Context* pContext = (CJS_Context*)NewContext(); | 122 CJS_Context* pContext = (CJS_Context*)NewContext(); |
| 123 InitializeEngine(); | 123 InitializeEngine(); |
| 124 ReleaseContext(pContext); | 124 ReleaseContext(pContext); |
| 125 |
| 126 SetFormFillEnvToDocument(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 CJS_Runtime::~CJS_Runtime() { | 129 CJS_Runtime::~CJS_Runtime() { |
| 128 NotifyObservedPtrs(); | 130 NotifyObservedPtrs(); |
| 129 ReleaseEngine(); | 131 ReleaseEngine(); |
| 130 if (m_isolateManaged) { | 132 if (m_isolateManaged) { |
| 131 GetIsolate()->Dispose(); | 133 GetIsolate()->Dispose(); |
| 132 SetIsolate(nullptr); | 134 SetIsolate(nullptr); |
| 133 } | 135 } |
| 134 } | 136 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 m_ContextArray.erase(it); | 194 m_ContextArray.erase(it); |
| 193 return; | 195 return; |
| 194 } | 196 } |
| 195 } | 197 } |
| 196 } | 198 } |
| 197 | 199 |
| 198 IJS_Context* CJS_Runtime::GetCurrentContext() { | 200 IJS_Context* CJS_Runtime::GetCurrentContext() { |
| 199 return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get(); | 201 return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get(); |
| 200 } | 202 } |
| 201 | 203 |
| 202 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { | 204 void CJS_Runtime::SetFormFillEnvToDocument() { |
| 203 if (m_pDocument == pReaderDoc) | |
| 204 return; | |
| 205 | |
| 206 v8::Isolate::Scope isolate_scope(GetIsolate()); | 205 v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 207 v8::HandleScope handle_scope(GetIsolate()); | 206 v8::HandleScope handle_scope(GetIsolate()); |
| 208 v8::Local<v8::Context> context = NewLocalContext(); | 207 v8::Local<v8::Context> context = NewLocalContext(); |
| 209 v8::Context::Scope context_scope(context); | 208 v8::Context::Scope context_scope(context); |
| 210 | 209 |
| 211 m_pDocument = pReaderDoc; | |
| 212 if (!pReaderDoc) | |
| 213 return; | |
| 214 | |
| 215 v8::Local<v8::Object> pThis = GetThisObj(); | 210 v8::Local<v8::Object> pThis = GetThisObj(); |
| 216 if (pThis.IsEmpty()) | 211 if (pThis.IsEmpty()) |
| 217 return; | 212 return; |
| 218 | 213 |
| 219 if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::g_nObjDefnID) | 214 if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::g_nObjDefnID) |
| 220 return; | 215 return; |
| 221 | 216 |
| 222 CJS_Document* pJSDocument = | 217 CJS_Document* pJSDocument = |
| 223 static_cast<CJS_Document*>(GetObjectPrivate(pThis)); | 218 static_cast<CJS_Document*>(GetObjectPrivate(pThis)); |
| 224 if (!pJSDocument) | 219 if (!pJSDocument) |
| 225 return; | 220 return; |
| 226 | 221 |
| 227 Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); | 222 Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); |
| 228 if (!pDocument) | 223 if (!pDocument) |
| 229 return; | 224 return; |
| 230 | 225 |
| 231 pDocument->AttachDoc(pReaderDoc); | 226 pDocument->SetFormFillEnv(m_pFormFillEnv); |
| 232 } | 227 } |
| 233 | 228 |
| 234 CPDFSDK_Document* CJS_Runtime::GetReaderDocument() { | 229 CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const { |
| 235 return m_pDocument; | 230 return m_pFormFillEnv; |
| 236 } | 231 } |
| 237 | 232 |
| 238 int CJS_Runtime::ExecuteScript(const CFX_WideString& script, | 233 int CJS_Runtime::ExecuteScript(const CFX_WideString& script, |
| 239 CFX_WideString* info) { | 234 CFX_WideString* info) { |
| 240 FXJSErr error = {}; | 235 FXJSErr error = {}; |
| 241 int nRet = Execute(script, &error); | 236 int nRet = Execute(script, &error); |
| 242 if (nRet < 0) { | 237 if (nRet < 0) { |
| 243 info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, | 238 info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, |
| 244 error.message); | 239 error.message); |
| 245 } | 240 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 300 // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 306 v8::Local<v8::Value> propvalue = | 301 v8::Local<v8::Value> propvalue = |
| 307 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); | 302 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
| 308 context->Global()->Set( | 303 context->Global()->Set( |
| 309 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 304 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 310 utf8Name.GetLength()), | 305 utf8Name.GetLength()), |
| 311 propvalue); | 306 propvalue); |
| 312 return TRUE; | 307 return TRUE; |
| 313 } | 308 } |
| 314 #endif | 309 #endif |
| OLD | NEW |