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/JS_Runtime.h" | 7 #include "fpdfsdk/javascript/JS_Runtime.h" |
8 | 8 |
| 9 #include <algorithm> |
| 10 |
9 #include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment. | 11 #include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment. |
10 #include "fpdfsdk/include/javascript/IJavaScript.h" | 12 #include "fpdfsdk/include/javascript/IJavaScript.h" |
11 #include "fpdfsdk/javascript/Consts.h" | 13 #include "fpdfsdk/javascript/Consts.h" |
12 #include "fpdfsdk/javascript/Document.h" | 14 #include "fpdfsdk/javascript/Document.h" |
13 #include "fpdfsdk/javascript/Field.h" | 15 #include "fpdfsdk/javascript/Field.h" |
14 #include "fpdfsdk/javascript/Icon.h" | 16 #include "fpdfsdk/javascript/Icon.h" |
15 #include "fpdfsdk/javascript/JS_Context.h" | 17 #include "fpdfsdk/javascript/JS_Context.h" |
16 #include "fpdfsdk/javascript/JS_Define.h" | 18 #include "fpdfsdk/javascript/JS_Define.h" |
17 #include "fpdfsdk/javascript/JS_EventHandler.h" | 19 #include "fpdfsdk/javascript/JS_EventHandler.h" |
18 #include "fpdfsdk/javascript/JS_GlobalData.h" | 20 #include "fpdfsdk/javascript/JS_GlobalData.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 #endif | 112 #endif |
111 CJS_Context* pContext = (CJS_Context*)NewContext(); | 113 CJS_Context* pContext = (CJS_Context*)NewContext(); |
112 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); | 114 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); |
113 ReleaseContext(pContext); | 115 ReleaseContext(pContext); |
114 } | 116 } |
115 | 117 |
116 CJS_Runtime::~CJS_Runtime() { | 118 CJS_Runtime::~CJS_Runtime() { |
117 for (auto* obs : m_observers) | 119 for (auto* obs : m_observers) |
118 obs->OnDestroyed(); | 120 obs->OnDestroyed(); |
119 | 121 |
120 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) | 122 m_ContextArray.clear(); |
121 delete m_ContextArray.GetAt(i); | |
122 | |
123 m_ContextArray.RemoveAll(); | |
124 m_ConstArrays.clear(); | 123 m_ConstArrays.clear(); |
125 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); | 124 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); |
126 | |
127 m_pApp = NULL; | |
128 m_pDocument = NULL; | |
129 m_context.Reset(); | 125 m_context.Reset(); |
130 | |
131 if (m_isolateManaged) | 126 if (m_isolateManaged) |
132 m_isolate->Dispose(); | 127 m_isolate->Dispose(); |
133 } | 128 } |
134 | 129 |
135 void CJS_Runtime::DefineJSObjects() { | 130 void CJS_Runtime::DefineJSObjects() { |
136 v8::Isolate::Scope isolate_scope(GetIsolate()); | 131 v8::Isolate::Scope isolate_scope(GetIsolate()); |
137 #ifdef PDF_ENABLE_XFA | 132 #ifdef PDF_ENABLE_XFA |
138 v8::Locker locker(GetIsolate()); | 133 v8::Locker locker(GetIsolate()); |
139 #endif | 134 #endif |
140 v8::HandleScope handle_scope(GetIsolate()); | 135 v8::HandleScope handle_scope(GetIsolate()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 CJS_PublicMethods::DefineJSObjects(GetIsolate()); | 171 CJS_PublicMethods::DefineJSObjects(GetIsolate()); |
177 CJS_GlobalConsts::DefineJSObjects(this); | 172 CJS_GlobalConsts::DefineJSObjects(this); |
178 CJS_GlobalArrays::DefineJSObjects(this); | 173 CJS_GlobalArrays::DefineJSObjects(this); |
179 | 174 |
180 // ObjDefIDs 21 - 22. | 175 // ObjDefIDs 21 - 22. |
181 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 176 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
182 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 177 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); |
183 } | 178 } |
184 | 179 |
185 IJS_Context* CJS_Runtime::NewContext() { | 180 IJS_Context* CJS_Runtime::NewContext() { |
186 CJS_Context* p = new CJS_Context(this); | 181 m_ContextArray.push_back(std::unique_ptr<CJS_Context>(new CJS_Context(this))); |
187 m_ContextArray.Add(p); | 182 return m_ContextArray.back().get(); |
188 return p; | |
189 } | 183 } |
190 | 184 |
191 void CJS_Runtime::ReleaseContext(IJS_Context* pContext) { | 185 void CJS_Runtime::ReleaseContext(IJS_Context* pContext) { |
192 CJS_Context* pJSContext = (CJS_Context*)pContext; | 186 for (auto it = m_ContextArray.begin(); it != m_ContextArray.end(); ++it) { |
193 | 187 if (it->get() == static_cast<CJS_Context*>(pContext)) { |
194 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { | 188 m_ContextArray.erase(it); |
195 if (pJSContext == m_ContextArray.GetAt(i)) { | 189 return; |
196 delete pJSContext; | |
197 m_ContextArray.RemoveAt(i); | |
198 break; | |
199 } | 190 } |
200 } | 191 } |
201 } | 192 } |
202 | 193 |
203 IJS_Context* CJS_Runtime::GetCurrentContext() { | 194 IJS_Context* CJS_Runtime::GetCurrentContext() { |
204 if (!m_ContextArray.GetSize()) | 195 return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get(); |
205 return NULL; | |
206 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); | |
207 } | 196 } |
208 | 197 |
209 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { | 198 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { |
210 if (m_pDocument != pReaderDoc) { | 199 if (m_pDocument != pReaderDoc) { |
211 v8::Isolate::Scope isolate_scope(m_isolate); | 200 v8::Isolate::Scope isolate_scope(m_isolate); |
212 #ifdef PDF_ENABLE_XFA | 201 #ifdef PDF_ENABLE_XFA |
213 v8::Locker locker(m_isolate); | 202 v8::Locker locker(m_isolate); |
214 #endif | 203 #endif |
215 v8::HandleScope handle_scope(m_isolate); | 204 v8::HandleScope handle_scope(m_isolate); |
216 v8::Local<v8::Context> context = | 205 v8::Local<v8::Context> context = |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 #endif | 325 #endif |
337 void CJS_Runtime::AddObserver(Observer* observer) { | 326 void CJS_Runtime::AddObserver(Observer* observer) { |
338 ASSERT(!pdfium::ContainsKey(m_observers, observer)); | 327 ASSERT(!pdfium::ContainsKey(m_observers, observer)); |
339 m_observers.insert(observer); | 328 m_observers.insert(observer); |
340 } | 329 } |
341 | 330 |
342 void CJS_Runtime::RemoveObserver(Observer* observer) { | 331 void CJS_Runtime::RemoveObserver(Observer* observer) { |
343 ASSERT(pdfium::ContainsKey(m_observers, observer)); | 332 ASSERT(pdfium::ContainsKey(m_observers, observer)); |
344 m_observers.erase(observer); | 333 m_observers.erase(observer); |
345 } | 334 } |
OLD | NEW |