John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/include/fsdk_mgr.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/javascript/Consts.h" |
| 13 | #include "fpdfsdk/javascript/Document.h" |
| 14 | #include "fpdfsdk/javascript/Field.h" |
| 15 | #include "fpdfsdk/javascript/Icon.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 16 | #include "fpdfsdk/javascript/JS_Define.h" |
| 17 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 18 | #include "fpdfsdk/javascript/JS_GlobalData.h" |
| 19 | #include "fpdfsdk/javascript/JS_Object.h" |
| 20 | #include "fpdfsdk/javascript/JS_Value.h" |
| 21 | #include "fpdfsdk/javascript/PublicMethods.h" |
| 22 | #include "fpdfsdk/javascript/app.h" |
dsinclair | 89bdd08 | 2016-04-06 10:47:54 -0700 | [diff] [blame] | 23 | #include "fpdfsdk/javascript/cjs_context.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 24 | #include "fpdfsdk/javascript/color.h" |
| 25 | #include "fpdfsdk/javascript/console.h" |
| 26 | #include "fpdfsdk/javascript/event.h" |
| 27 | #include "fpdfsdk/javascript/global.h" |
| 28 | #include "fpdfsdk/javascript/report.h" |
| 29 | #include "fpdfsdk/javascript/util.h" |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 30 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 32 | #ifdef PDF_ENABLE_XFA |
dsinclair | 89bdd08 | 2016-04-06 10:47:54 -0700 | [diff] [blame] | 33 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | #include "xfa/fxjse/value.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 35 | #endif // PDF_ENABLE_XFA |
| 36 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 37 | // static |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 38 | void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { |
| 39 | FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); |
| 40 | } |
| 41 | |
| 42 | // static |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 43 | IJS_Runtime* IJS_Runtime::Create(CPDFDoc_Environment* pEnv) { |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 44 | return new CJS_Runtime(pEnv); |
| 45 | } |
| 46 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 47 | // static |
| 48 | CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) { |
| 49 | const CJS_Context* pContext = static_cast<const CJS_Context*>(cc); |
| 50 | return pContext->GetJSRuntime(); |
| 51 | } |
| 52 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) |
| 54 | : m_pApp(pApp), |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 55 | m_pDocument(nullptr), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 56 | m_bBlocking(FALSE), |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 57 | m_isolate(nullptr), |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 58 | m_isolateManaged(false) { |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 59 | #ifndef PDF_ENABLE_XFA |
| 60 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 61 | if (pPlatform->version <= 2) { |
| 62 | unsigned int embedderDataSlot = 0; |
| 63 | v8::Isolate* pExternalIsolate = nullptr; |
| 64 | if (pPlatform->version == 2) { |
| 65 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 66 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 67 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { |
| 69 | // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. |
| 70 | m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 71 | } else { |
| 72 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; |
| 73 | if (pPlatform->version <= 2) { |
| 74 | unsigned int embedderDataSlot = 0; |
| 75 | v8::Isolate* pExternalIsolate = nullptr; |
| 76 | if (pPlatform->version == 2) { |
| 77 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 78 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
| 79 | } |
| 80 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 81 | #endif |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 82 | } |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 83 | #ifndef PDF_ENABLE_XFA |
| 84 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 85 | #else |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 86 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | } |
Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | v8::Isolate* isolate = m_isolate; |
| 90 | v8::Isolate::Scope isolate_scope(isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | v8::HandleScope handle_scope(isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 92 | if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 94 | FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 95 | ReleaseContext(pContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | return; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 97 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | } |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 99 | #ifndef PDF_ENABLE_XFA |
| 100 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); |
| 101 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 103 | #endif |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 104 | if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 105 | DefineJSObjects(); |
| 106 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 107 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 108 | CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 110 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | CJS_Context* pContext = (CJS_Context*)NewContext(); |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 112 | FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | ReleaseContext(pContext); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | CJS_Runtime::~CJS_Runtime() { |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 117 | for (auto* obs : m_observers) |
| 118 | obs->OnDestroyed(); |
| 119 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 120 | m_ContextArray.clear(); |
Tom Sepez | 297b515 | 2016-03-04 13:43:46 -0800 | [diff] [blame] | 121 | m_ConstArrays.clear(); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 122 | FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | m_context.Reset(); |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 124 | if (m_isolateManaged) |
| 125 | m_isolate->Dispose(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 128 | void CJS_Runtime::DefineJSObjects() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 129 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | v8::HandleScope handle_scope(GetIsolate()); |
| 131 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 132 | v8::Context::Scope context_scope(context); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 133 | |
| 134 | // The call order determines the "ObjDefID" assigned to each class. |
| 135 | // ObjDefIDs 0 - 2 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 136 | CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 137 | CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 138 | CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 139 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 140 | // ObjDefIDs 3 - 5 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 141 | CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 142 | CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 143 | CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 144 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 145 | // ObjDefIDs 6 - 8 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 146 | CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 147 | CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 148 | CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 150 | // ObjDefIDs 9 - 11 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 151 | CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 152 | CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 153 | CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 155 | // ObjDefIDs 12 - 14 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 156 | CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL); |
| 157 | CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 158 | CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 159 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 160 | // ObjDefIDs 15 - 17 |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 161 | CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
| 162 | CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 163 | CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 164 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 165 | // ObjDefIDs 18 - 20 (these can't fail, return void). |
| 166 | CJS_PublicMethods::DefineJSObjects(GetIsolate()); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 167 | CJS_GlobalConsts::DefineJSObjects(this); |
| 168 | CJS_GlobalArrays::DefineJSObjects(this); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 169 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 170 | // ObjDefIDs 21 - 22. |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 171 | CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
| 172 | CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 175 | IJS_Context* CJS_Runtime::NewContext() { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 176 | m_ContextArray.push_back(std::unique_ptr<CJS_Context>(new CJS_Context(this))); |
| 177 | return m_ContextArray.back().get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 180 | void CJS_Runtime::ReleaseContext(IJS_Context* pContext) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 181 | for (auto it = m_ContextArray.begin(); it != m_ContextArray.end(); ++it) { |
| 182 | if (it->get() == static_cast<CJS_Context*>(pContext)) { |
| 183 | m_ContextArray.erase(it); |
| 184 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 189 | IJS_Context* CJS_Runtime::GetCurrentContext() { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 190 | return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
| 194 | if (m_pDocument != pReaderDoc) { |
| 195 | v8::Isolate::Scope isolate_scope(m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | v8::HandleScope handle_scope(m_isolate); |
| 197 | v8::Local<v8::Context> context = |
| 198 | v8::Local<v8::Context>::New(m_isolate, m_context); |
| 199 | v8::Context::Scope context_scope(context); |
| 200 | |
| 201 | m_pDocument = pReaderDoc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | if (pReaderDoc) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 203 | v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | if (!pThis.IsEmpty()) { |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 205 | if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 206 | if (CJS_Document* pJSDocument = |
Tom Sepez | d5a0e95 | 2015-09-17 15:40:06 -0700 | [diff] [blame] | 207 | (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) |
| 209 | pDocument->AttachDoc(pReaderDoc); |
| 210 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 211 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 213 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 215 | } |
| 216 | |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 217 | int CJS_Runtime::Execute(const CFX_WideString& script, CFX_WideString* info) { |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 218 | FXJSErr error = {}; |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 219 | int nRet = FXJS_Execute(m_isolate, script, &error); |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 220 | if (nRet < 0) { |
| 221 | info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, |
| 222 | error.message); |
| 223 | } |
| 224 | return nRet; |
| 225 | } |
| 226 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 227 | bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { |
| 228 | return m_FieldEventSet.insert(event).second; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 231 | void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
| 232 | m_FieldEventSet.erase(event); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 235 | v8::Local<v8::Context> CJS_Runtime::NewJSContext() { |
| 236 | return v8::Local<v8::Context>::New(m_isolate, m_context); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Tom Sepez | 297b515 | 2016-03-04 13:43:46 -0800 | [diff] [blame] | 239 | void CJS_Runtime::SetConstArray(const CFX_WideString& name, |
| 240 | v8::Local<v8::Array> array) { |
| 241 | m_ConstArrays[name] = v8::Global<v8::Array>(m_isolate, array); |
| 242 | } |
| 243 | |
| 244 | v8::Local<v8::Array> CJS_Runtime::GetConstArray(const CFX_WideString& name) { |
| 245 | return v8::Local<v8::Array>::New(m_isolate, m_ConstArrays[name]); |
| 246 | } |
| 247 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 248 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | CFX_WideString ChangeObjName(const CFX_WideString& str) { |
| 250 | CFX_WideString sRet = str; |
| 251 | sRet.Replace(L"_", L"."); |
| 252 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 253 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 254 | FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, |
| 255 | CFXJSE_Value* pValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 256 | #ifdef PDF_ENABLE_XFA |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 257 | const FX_CHAR* name = utf8Name.c_str(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 258 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 260 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | 4f4603c | 2015-11-10 15:03:12 -0800 | [diff] [blame] | 261 | v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | v8::Local<v8::Context> context = |
| 263 | v8::Local<v8::Context>::New(GetIsolate(), m_context); |
| 264 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 265 | |
Tom Sepez | 4f4603c | 2015-11-10 15:03:12 -0800 | [diff] [blame] | 266 | // Caution: We're about to hand to XFA an object that in order to invoke |
| 267 | // methods will require that the current v8::Context always has a pointer |
| 268 | // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates |
| 269 | // its own v8::Context which has not initialized the embedder data slot. |
| 270 | // Do so now. |
| 271 | // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's |
| 272 | // embedder data slots, and/or to always use the right context. |
| 273 | FXJS_SetRuntimeForV8Context(old_context, this); |
| 274 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | v8::Local<v8::Value> propvalue = |
| 276 | context->Global()->Get(v8::String::NewFromUtf8( |
| 277 | GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 278 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 279 | if (propvalue.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame^] | 280 | pValue->SetUndefined(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | return FALSE; |
| 282 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 283 | pValue->ForceSetValue(propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 284 | #endif |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 285 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 287 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 288 | FX_BOOL CJS_Runtime::SetValueByName(const CFX_ByteStringC& utf8Name, |
| 289 | CFXJSE_Value* pValue) { |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 290 | #ifdef PDF_ENABLE_XFA |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 291 | if (utf8Name.IsEmpty() || !pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | return FALSE; |
dsinclair | 179bebb | 2016-04-05 11:02:18 -0700 | [diff] [blame] | 293 | const FX_CHAR* name = utf8Name.c_str(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | v8::Isolate* pIsolate = GetIsolate(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 296 | v8::HandleScope handle_scope(pIsolate); |
| 297 | v8::Local<v8::Context> context = |
| 298 | v8::Local<v8::Context>::New(pIsolate, m_context); |
| 299 | v8::Context::Scope context_scope(context); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 300 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 301 | // v8::Local<v8::Context> tmpCotext = |
| 302 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 303 | v8::Local<v8::Value> propvalue = |
| 304 | v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | context->Global()->Set( |
| 306 | v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, |
| 307 | utf8Name.GetLength()), |
| 308 | propvalue); |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 309 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | return TRUE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 311 | } |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 312 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 313 | #endif |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 314 | void CJS_Runtime::AddObserver(Observer* observer) { |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 315 | ASSERT(!pdfium::ContainsKey(m_observers, observer)); |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 316 | m_observers.insert(observer); |
| 317 | } |
| 318 | |
| 319 | void CJS_Runtime::RemoveObserver(Observer* observer) { |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 320 | ASSERT(pdfium::ContainsKey(m_observers, observer)); |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 321 | m_observers.erase(observer); |
| 322 | } |