Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: fpdfsdk/src/javascript/JS_Runtime.cpp

Issue 1334833003: Merge to XFA: Remove some abstractions in fxjs_v8.h. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/src/javascript/JS_Object.cpp ('k') | fpdfsdk/src/javascript/JS_Value.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "../../include/javascript/JavaScript.h" 7 #include "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/IJavaScript.h" 8 #include "../../include/javascript/IJavaScript.h"
9 #include "../../include/javascript/JS_EventHandler.h" 9 #include "../../include/javascript/JS_EventHandler.h"
10 #include "../../include/javascript/JS_Runtime.h" 10 #include "../../include/javascript/JS_Runtime.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 87
88 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { 88 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
89 return malloc(length); 89 return malloc(length);
90 } 90 }
91 91
92 void CJS_ArrayBufferAllocator::Free(void* data, size_t length) { 92 void CJS_ArrayBufferAllocator::Free(void* data, size_t length) {
93 free(data); 93 free(data);
94 } 94 }
95 95
96 /* ------------------------------ CJS_Runtime ------------------------------ */ 96 /* ------------------------------ CJS_Runtime ------------------------------ */
97 extern v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate( 97 v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate);
98 IJS_Runtime* pJSRuntime); 98
99 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) 99 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
100 : m_pApp(pApp), 100 : m_pApp(pApp),
101 m_pDocument(NULL), 101 m_pDocument(NULL),
102 m_bBlocking(FALSE), 102 m_bBlocking(FALSE),
103 m_pFieldEventPath(NULL), 103 m_pFieldEventPath(NULL),
104 m_isolate(NULL), 104 m_isolate(NULL),
105 m_isolateManaged(false) { 105 m_isolateManaged(false) {
106 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { 106 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
107 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. 107 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
108 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); 108 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
109 } else if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) { 109 } else if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
110 m_isolate = reinterpret_cast<v8::Isolate*>( 110 m_isolate = reinterpret_cast<v8::Isolate*>(
111 m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate); 111 m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
112 } 112 }
113 if (!m_isolate) { 113 if (!m_isolate) {
114 m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator()); 114 m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator());
115 115
116 v8::Isolate::CreateParams params; 116 v8::Isolate::CreateParams params;
117 params.array_buffer_allocator = m_pArrayBufferAllocator.get(); 117 params.array_buffer_allocator = m_pArrayBufferAllocator.get();
118 m_isolate = v8::Isolate::New(params); 118 m_isolate = v8::Isolate::New(params);
119 m_isolateManaged = true; 119 m_isolateManaged = true;
120 } 120 }
121 121
122 v8::Isolate* isolate = m_isolate; 122 v8::Isolate* isolate = m_isolate;
123 v8::Isolate::Scope isolate_scope(isolate); 123 v8::Isolate::Scope isolate_scope(isolate);
124 v8::Locker locker(isolate); 124 v8::Locker locker(isolate);
125 v8::HandleScope handle_scope(isolate); 125 v8::HandleScope handle_scope(isolate);
126 if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) { 126 if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) {
127 CJS_Context* pContext = (CJS_Context*)NewContext(); 127 CJS_Context* pContext = (CJS_Context*)NewContext();
128 JS_InitialRuntime(*this, this, pContext, m_context); 128 JS_InitialRuntime(GetIsolate(), this, pContext, m_context);
129 ReleaseContext(pContext); 129 ReleaseContext(pContext);
130 return; 130 return;
131 } 131 }
132 132
133 InitJSObjects(); 133 InitJSObjects();
134 134
135 CJS_Context* pContext = (CJS_Context*)NewContext(); 135 CJS_Context* pContext = (CJS_Context*)NewContext();
136 JS_InitialRuntime(*this, this, pContext, m_context); 136 JS_InitialRuntime(GetIsolate(), this, pContext, m_context);
137 ReleaseContext(pContext); 137 ReleaseContext(pContext);
138 } 138 }
139 139
140 CJS_Runtime::~CJS_Runtime() { 140 CJS_Runtime::~CJS_Runtime() {
141 int size = m_ContextArray.GetSize(); 141 int size = m_ContextArray.GetSize();
142 for (int i = 0; i < size; i++) 142 for (int i = 0; i < size; i++)
143 delete m_ContextArray.GetAt(i); 143 delete m_ContextArray.GetAt(i);
144 144
145 m_ContextArray.RemoveAll(); 145 m_ContextArray.RemoveAll();
146 146
147 RemoveEventsInLoop(m_pFieldEventPath); 147 RemoveEventsInLoop(m_pFieldEventPath);
148 148
149 m_pApp = NULL; 149 m_pApp = NULL;
150 m_pDocument = NULL; 150 m_pDocument = NULL;
151 m_pFieldEventPath = NULL; 151 m_pFieldEventPath = NULL;
152 m_context.Reset(); 152 m_context.Reset();
153 153
154 if (m_isolateManaged) 154 if (m_isolateManaged)
155 m_isolate->Dispose(); 155 m_isolate->Dispose();
156 m_isolate = NULL; 156 m_isolate = NULL;
157 } 157 }
158 158
159 FX_BOOL CJS_Runtime::InitJSObjects() { 159 FX_BOOL CJS_Runtime::InitJSObjects() {
160 v8::Isolate::Scope isolate_scope(GetIsolate()); 160 v8::Isolate::Scope isolate_scope(GetIsolate());
161 v8::Locker locker(GetIsolate()); 161 v8::Locker locker(GetIsolate());
162 v8::HandleScope handle_scope(GetIsolate()); 162 v8::HandleScope handle_scope(GetIsolate());
163 v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); 163 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
164 v8::Context::Scope context_scope(context); 164 v8::Context::Scope context_scope(context);
165 // 0 - 8 165 // 0 - 8
166 if (CJS_Border::Init(*this, JS_STATIC) < 0) 166 if (CJS_Border::Init(GetIsolate(), JS_STATIC) < 0)
167 return FALSE; 167 return FALSE;
168 if (CJS_Display::Init(*this, JS_STATIC) < 0) 168 if (CJS_Display::Init(GetIsolate(), JS_STATIC) < 0)
169 return FALSE; 169 return FALSE;
170 if (CJS_Font::Init(*this, JS_STATIC) < 0) 170 if (CJS_Font::Init(GetIsolate(), JS_STATIC) < 0)
171 return FALSE; 171 return FALSE;
172 if (CJS_Highlight::Init(*this, JS_STATIC) < 0) 172 if (CJS_Highlight::Init(GetIsolate(), JS_STATIC) < 0)
173 return FALSE; 173 return FALSE;
174 if (CJS_Position::Init(*this, JS_STATIC) < 0) 174 if (CJS_Position::Init(GetIsolate(), JS_STATIC) < 0)
175 return FALSE; 175 return FALSE;
176 if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) 176 if (CJS_ScaleHow::Init(GetIsolate(), JS_STATIC) < 0)
177 return FALSE; 177 return FALSE;
178 if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) 178 if (CJS_ScaleWhen::Init(GetIsolate(), JS_STATIC) < 0)
179 return FALSE; 179 return FALSE;
180 if (CJS_Style::Init(*this, JS_STATIC) < 0) 180 if (CJS_Style::Init(GetIsolate(), JS_STATIC) < 0)
181 return FALSE; 181 return FALSE;
182 if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) 182 if (CJS_Zoomtype::Init(GetIsolate(), JS_STATIC) < 0)
183 return FALSE; 183 return FALSE;
184 184
185 // 9 - 11 185 // 9 - 11
186 if (CJS_App::Init(*this, JS_STATIC) < 0) 186 if (CJS_App::Init(GetIsolate(), JS_STATIC) < 0)
187 return FALSE; 187 return FALSE;
188 if (CJS_Color::Init(*this, JS_STATIC) < 0) 188 if (CJS_Color::Init(GetIsolate(), JS_STATIC) < 0)
189 return FALSE; 189 return FALSE;
190 if (CJS_Console::Init(*this, JS_STATIC) < 0) 190 if (CJS_Console::Init(GetIsolate(), JS_STATIC) < 0)
191 return FALSE; 191 return FALSE;
192 192
193 // 12 - 14 193 // 12 - 14
194 if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) 194 if (CJS_Document::Init(GetIsolate(), JS_DYNAMIC) < 0)
195 return FALSE; 195 return FALSE;
196 if (CJS_Event::Init(*this, JS_STATIC) < 0) 196 if (CJS_Event::Init(GetIsolate(), JS_STATIC) < 0)
197 return FALSE; 197 return FALSE;
198 if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) 198 if (CJS_Field::Init(GetIsolate(), JS_DYNAMIC) < 0)
199 return FALSE; 199 return FALSE;
200 200
201 // 15 - 17 201 // 15 - 17
202 if (CJS_Global::Init(*this, JS_STATIC) < 0) 202 if (CJS_Global::Init(GetIsolate(), JS_STATIC) < 0)
203 return FALSE; 203 return FALSE;
204 if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) 204 if (CJS_Icon::Init(GetIsolate(), JS_DYNAMIC) < 0)
205 return FALSE; 205 return FALSE;
206 if (CJS_Util::Init(*this, JS_STATIC) < 0) 206 if (CJS_Util::Init(GetIsolate(), JS_STATIC) < 0)
207 return FALSE; 207 return FALSE;
208 208
209 if (CJS_PublicMethods::Init(*this) < 0) 209 if (CJS_PublicMethods::Init(GetIsolate()) < 0)
210 return FALSE; 210 return FALSE;
211 if (CJS_GlobalConsts::Init(*this) < 0) 211 if (CJS_GlobalConsts::Init(GetIsolate()) < 0)
212 return FALSE; 212 return FALSE;
213 if (CJS_GlobalArrays::Init(*this) < 0) 213 if (CJS_GlobalArrays::Init(GetIsolate()) < 0)
214 return FALSE; 214 return FALSE;
215 215
216 if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) 216 if (CJS_TimerObj::Init(GetIsolate(), JS_DYNAMIC) < 0)
217 return FALSE; 217 return FALSE;
218 if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) < 0) 218 if (CJS_PrintParamsObj::Init(GetIsolate(), JS_DYNAMIC) < 0)
219 return FALSE; 219 return FALSE;
220 220
221 return TRUE; 221 return TRUE;
222 } 222 }
223 223
224 IFXJS_Context* CJS_Runtime::NewContext() { 224 IFXJS_Context* CJS_Runtime::NewContext() {
225 CJS_Context* p = new CJS_Context(this); 225 CJS_Context* p = new CJS_Context(this);
226 m_ContextArray.Add(p); 226 m_ContextArray.Add(p);
227 return p; 227 return p;
228 } 228 }
(...skipping 19 matching lines...) Expand all
248 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { 248 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
249 if (m_pDocument != pReaderDoc) { 249 if (m_pDocument != pReaderDoc) {
250 v8::Isolate::Scope isolate_scope(m_isolate); 250 v8::Isolate::Scope isolate_scope(m_isolate);
251 v8::Locker locker(m_isolate); 251 v8::Locker locker(m_isolate);
252 v8::HandleScope handle_scope(m_isolate); 252 v8::HandleScope handle_scope(m_isolate);
253 v8::Local<v8::Context> context = 253 v8::Local<v8::Context> context =
254 v8::Local<v8::Context>::New(m_isolate, m_context); 254 v8::Local<v8::Context>::New(m_isolate, m_context);
255 v8::Context::Scope context_scope(context); 255 v8::Context::Scope context_scope(context);
256 256
257 m_pDocument = pReaderDoc; 257 m_pDocument = pReaderDoc;
258
259 if (pReaderDoc) { 258 if (pReaderDoc) {
260 JSObject pThis = JS_GetThisObj(*this); 259 v8::Local<v8::Object> pThis = JS_GetThisObj(GetIsolate());
261 if (!pThis.IsEmpty()) { 260 if (!pThis.IsEmpty()) {
262 if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document")) { 261 if (JS_GetObjDefnID(pThis) ==
262 JS_GetObjDefnID(GetIsolate(), L"Document")) {
263 if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis)) { 263 if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis)) {
264 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) 264 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
265 pDocument->AttachDoc(pReaderDoc); 265 pDocument->AttachDoc(pReaderDoc);
266 } 266 }
267 } 267 }
268 } 268 }
269 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document"));
270 } else {
271 JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app"));
272 } 269 }
273 } 270 }
274 } 271 }
275 272
276 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, 273 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName,
277 JS_EVENT_T eEventType) { 274 JS_EVENT_T eEventType) {
278 if (m_pFieldEventPath == NULL) { 275 if (m_pFieldEventPath == NULL) {
279 m_pFieldEventPath = new CJS_FieldEvent; 276 m_pFieldEventPath = new CJS_FieldEvent;
280 m_pFieldEventPath->sTargetName = sTargetName; 277 m_pFieldEventPath->sTargetName = sTargetName;
281 m_pFieldEventPath->eEventType = eEventType; 278 m_pFieldEventPath->eEventType = eEventType;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 // v8::Local<v8::Context>::New(GetIsolate(), m_context); 392 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
396 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( 393 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
397 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); 394 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
398 context->Global()->Set( 395 context->Global()->Set(
399 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, 396 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
400 utf8Name.GetLength()), 397 utf8Name.GetLength()),
401 propvalue); 398 propvalue);
402 399
403 return TRUE; 400 return TRUE;
404 } 401 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/JS_Object.cpp ('k') | fpdfsdk/src/javascript/JS_Value.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698