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

Side by Side Diff: fpdfsdk/src/jsapi/fxjs_v8.cpp

Issue 1633083002: Fix memory leakage on Linux - part3 (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 4 years, 10 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
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 "fpdfsdk/include/jsapi/fxjs_v8.h" 7 #include "fpdfsdk/include/jsapi/fxjs_v8.h"
8 8
9 #include "core/include/fxcrt/fx_basic.h" 9 #include "core/include/fxcrt/fx_basic.h"
10 10
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 118 }
119 119
120 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { 120 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
121 return malloc(length); 121 return malloc(length);
122 } 122 }
123 123
124 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { 124 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) {
125 free(data); 125 free(data);
126 } 126 }
127 127
128 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate,
129 v8::Global<v8::Object> value,
130 void* key) {
131 v8::Local<v8::Object> obj = value.Get(isolate);
132 int id = FXJS_GetObjDefnID(obj);
133 if (id == -1)
jun_fang 2016/01/26 05:56:15 if (id == -1 || obj.IsEmpty())
134 return;
135
136 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id);
137 if (!pObjDef)
138 return;
139 if (!obj.IsEmpty()) {
jun_fang 2016/01/26 05:56:15 Test obj at line 133.
140 if (pObjDef->m_pDestructor)
141 pObjDef->m_pDestructor(obj);
142 FXJS_FreePrivate(obj);
143 }
144 }
145
146 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
147 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
148 if (V8TemplateMap* pMap =
jun_fang 2016/01/26 05:56:15 V8TemplateMap* pMap = FXJS_PerIsolateData::Get(dat
149 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap)
150 return &pMap->m_map;
151 return nullptr;
152 }
153
128 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { 154 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
129 if (g_isolate) { 155 if (g_isolate) {
130 ASSERT(g_embedderDataSlot == embedderDataSlot); 156 ASSERT(g_embedderDataSlot == embedderDataSlot);
131 ASSERT(g_isolate == pIsolate); 157 ASSERT(g_isolate == pIsolate);
132 return; 158 return;
133 } 159 }
134 g_embedderDataSlot = embedderDataSlot; 160 g_embedderDataSlot = embedderDataSlot;
135 g_isolate = pIsolate; 161 g_isolate = pIsolate;
136 } 162 }
137 163
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 v8::Isolate::Scope isolate_scope(pIsolate); 309 v8::Isolate::Scope isolate_scope(pIsolate);
284 #ifdef PDF_ENABLE_XFA 310 #ifdef PDF_ENABLE_XFA
285 v8::Locker locker(pIsolate); 311 v8::Locker locker(pIsolate);
286 #endif // PDF_ENABLE_XFA 312 #endif // PDF_ENABLE_XFA
287 v8::HandleScope handle_scope(pIsolate); 313 v8::HandleScope handle_scope(pIsolate);
288 v8::Local<v8::Context> v8Context = 314 v8::Local<v8::Context> v8Context =
289 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); 315 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate));
290 v8::Context::Scope context_scope(v8Context); 316 v8::Context::Scope context_scope(v8Context);
291 317
292 FXJS_PerIsolateData::SetUp(pIsolate); 318 FXJS_PerIsolateData::SetUp(pIsolate);
319 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
320 if (!pData)
321 return;
322 pData->CreateDynamicObjsMap(pIsolate);
293 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); 323 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime);
294 324
295 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 325 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
296 pStaticObjects->resize(maxID + 1); 326 pStaticObjects->resize(maxID + 1);
297 for (int i = 0; i < maxID; ++i) { 327 for (int i = 0; i < maxID; ++i) {
298 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 328 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
299 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { 329 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
300 v8Context->Global() 330 v8Context->Global()
301 ->GetPrototype() 331 ->GetPrototype()
302 ->ToObject(v8Context) 332 ->ToObject(v8Context)
303 .ToLocalChecked() 333 .ToLocalChecked()
304 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); 334 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i));
305 335
306 if (pObjDef->m_pConstructor) 336 if (pObjDef->m_pConstructor)
307 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() 337 pObjDef->m_pConstructor(pIRuntime, v8Context->Global()
308 ->GetPrototype() 338 ->GetPrototype()
309 ->ToObject(v8Context) 339 ->ToObject(v8Context)
310 .ToLocalChecked()); 340 .ToLocalChecked());
311 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { 341 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
312 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); 342 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode();
313 v8::Local<v8::String> m_ObjName = 343 v8::Local<v8::String> m_ObjName =
314 v8::String::NewFromUtf8(pIsolate, bs.c_str(), 344 v8::String::NewFromUtf8(pIsolate, bs.c_str(),
315 v8::NewStringType::kNormal, 345 v8::NewStringType::kNormal,
316 bs.GetLength()).ToLocalChecked(); 346 bs.GetLength()).ToLocalChecked();
317 347
318 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); 348 v8::Local<v8::Object> obj =
349 FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i, TRUE);
319 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); 350 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust();
320 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); 351 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj);
321 } 352 }
322 } 353 }
323 pV8PersistentContext->Reset(pIsolate, v8Context); 354 pV8PersistentContext->Reset(pIsolate, v8Context);
324 } 355 }
325 356
326 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, 357 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
327 v8::Global<v8::Context>* pV8PersistentContext, 358 v8::Global<v8::Context>* pV8PersistentContext,
328 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { 359 std::vector<v8::Global<v8::Object>*>* pStaticObjects) {
329 v8::Isolate::Scope isolate_scope(pIsolate); 360 v8::Isolate::Scope isolate_scope(pIsolate);
330 #ifdef PDF_ENABLE_XFA 361 #ifdef PDF_ENABLE_XFA
331 v8::Locker locker(pIsolate); 362 v8::Locker locker(pIsolate);
332 #endif // PDF_ENABLE_XFA 363 #endif // PDF_ENABLE_XFA
333 v8::HandleScope handle_scope(pIsolate); 364 v8::HandleScope handle_scope(pIsolate);
334 v8::Local<v8::Context> context = 365 v8::Local<v8::Context> context =
335 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); 366 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext);
336 v8::Context::Scope context_scope(context); 367 v8::Context::Scope context_scope(context);
337 368
338 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 369 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
339 if (!pData) 370 if (!pData)
340 return; 371 return;
372 pData->ReleaseDynamicObjsMap();
341 373
342 #ifdef PDF_ENABLE_XFA 374 #ifdef PDF_ENABLE_XFA
343 // XFA, if present, should have already cleaned itself up. 375 // XFA, if present, should have already cleaned itself up.
344 FXSYS_assert(!pData->m_pFXJSERuntimeData); 376 FXSYS_assert(!pData->m_pFXJSERuntimeData);
345 #endif // PDF_ENABLE_XFA 377 #endif // PDF_ENABLE_XFA
346 378
347 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 379 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
348 for (int i = 0; i < maxID; ++i) { 380 for (int i = 0; i < maxID; ++i) {
349 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 381 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
350 v8::Local<v8::Object> pObj; 382 v8::Local<v8::Object> pObj;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 if (!compiled_script->Run(context).ToLocal(&result)) { 442 if (!compiled_script->Run(context).ToLocal(&result)) {
411 v8::String::Utf8Value error(try_catch.Exception()); 443 v8::String::Utf8Value error(try_catch.Exception());
412 // TODO(tsepez): return error via pError->message. 444 // TODO(tsepez): return error via pError->message.
413 return -1; 445 return -1;
414 } 446 }
415 return 0; 447 return 0;
416 } 448 }
417 449
418 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, 450 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
419 IJS_Runtime* pIRuntime, 451 IJS_Runtime* pIRuntime,
420 int nObjDefnID) { 452 int nObjDefnID,
453 FX_BOOL bStatic) {
421 v8::Isolate::Scope isolate_scope(pIsolate); 454 v8::Isolate::Scope isolate_scope(pIsolate);
422 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 455 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
423 if (nObjDefnID == -1) { 456 if (nObjDefnID == -1) {
424 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); 457 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate);
425 v8::Local<v8::Object> obj; 458 v8::Local<v8::Object> obj;
426 if (!objTempl->NewInstance(context).ToLocal(&obj)) 459 if (!objTempl->NewInstance(context).ToLocal(&obj))
427 return v8::Local<v8::Object>(); 460 return v8::Local<v8::Object>();
428 return obj; 461 return obj;
429 } 462 }
430 463
431 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 464 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
432 if (!pData) 465 if (!pData)
433 return v8::Local<v8::Object>(); 466 return v8::Local<v8::Object>();
434 467
435 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) 468 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate))
436 return v8::Local<v8::Object>(); 469 return v8::Local<v8::Object>();
437 470
438 CFXJS_ObjDefinition* pObjDef = 471 CFXJS_ObjDefinition* pObjDef =
439 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 472 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID);
440 v8::Local<v8::Object> obj; 473 v8::Local<v8::Object> obj;
441 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) 474 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
442 return v8::Local<v8::Object>(); 475 return v8::Local<v8::Object>();
443 476
444 obj->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(nObjDefnID)); 477 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID);
478 obj->SetAlignedPointerInInternalField(0, pPerObjData);
445 if (pObjDef->m_pConstructor) 479 if (pObjDef->m_pConstructor)
446 pObjDef->m_pConstructor(pIRuntime, obj); 480 pObjDef->m_pConstructor(pIRuntime, obj);
447 481
482 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap)
483 FXJS_PerIsolateData::Get(pIsolate)
484 ->m_pDynamicObjsMap->set(pPerObjData, obj);
485
448 return obj; 486 return obj;
449 } 487 }
450 488
451 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { 489 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) {
452 v8::Isolate::Scope isolate_scope(pIsolate); 490 v8::Isolate::Scope isolate_scope(pIsolate);
453 if (!FXJS_PerIsolateData::Get(pIsolate)) 491 if (!FXJS_PerIsolateData::Get(pIsolate))
454 return v8::Local<v8::Object>(); 492 return v8::Local<v8::Object>();
455 493
456 // Return the global object. 494 // Return the global object.
457 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 495 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 return v8::Local<v8::Array>(); 807 return v8::Local<v8::Array>();
770 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 808 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
771 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); 809 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
772 } 810 }
773 811
774 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { 812 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
775 pTo = pFrom; 813 pTo = pFrom;
776 } 814 }
777 815
778 816
OLDNEW
« fpdfsdk/include/jsapi/fxjs_v8.h ('K') | « fpdfsdk/include/jsapi/fxjs_v8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698