Home | History | Annotate | Download | only in src
      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.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "fpdfsdk/include/jsapi/fxjs_v8.h"  // For per-isolate data.
      8 #include "xfa/src/foxitlib.h"
      9 #include "fxv8.h"
     10 #include "runtime.h"
     11 #include "scope_inline.h"
     12 
     13 // Duplicates fpdfsdk's JS_Runtime.h, but keeps XFA from depending on it.
     14 // TODO(tsepez): make a single version of this.
     15 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
     16   void* Allocate(size_t length) override { return calloc(1, length); }
     17   void* AllocateUninitialized(size_t length) override { return malloc(length); }
     18   void Free(void* data, size_t length) override { free(data); }
     19 };
     20 
     21 static void FXJSE_KillV8() {
     22   v8::V8::Dispose();
     23 }
     24 void FXJSE_Initialize() {
     25   if (!CFXJSE_RuntimeData::g_RuntimeList) {
     26     CFXJSE_RuntimeData::g_RuntimeList = new CFXJSE_RuntimeList;
     27   }
     28   static FX_BOOL bV8Initialized = FALSE;
     29   if (bV8Initialized) {
     30     return;
     31   }
     32   bV8Initialized = TRUE;
     33   atexit(FXJSE_KillV8);
     34 }
     35 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate) {
     36   {
     37     v8::Locker locker(pIsolate);
     38     if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) {
     39       delete pData->m_pFXJSERuntimeData;
     40       pData->m_pFXJSERuntimeData = nullptr;
     41     }
     42   }
     43   pIsolate->Dispose();
     44 }
     45 void FXJSE_Finalize() {
     46   if (CFXJSE_RuntimeData::g_RuntimeList) {
     47     CFXJSE_RuntimeData::g_RuntimeList->RemoveAllRuntimes(
     48         FXJSE_Runtime_DisposeCallback);
     49     delete CFXJSE_RuntimeData::g_RuntimeList;
     50     CFXJSE_RuntimeData::g_RuntimeList = NULL;
     51   }
     52 }
     53 FXJSE_HRUNTIME FXJSE_Runtime_Create() {
     54   v8::Isolate::CreateParams params;
     55   params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator();
     56   v8::Isolate* pIsolate = v8::Isolate::New(params);
     57   ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList);
     58   CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate);
     59   return reinterpret_cast<FXJSE_HRUNTIME>(pIsolate);
     60 }
     61 void FXJSE_Runtime_Release(FXJSE_HRUNTIME hRuntime) {
     62   v8::Isolate* pIsolate = reinterpret_cast<v8::Isolate*>(hRuntime);
     63   if (pIsolate) {
     64     ASSERT(CFXJSE_RuntimeData::g_RuntimeList);
     65     CFXJSE_RuntimeData::g_RuntimeList->RemoveRuntime(
     66         pIsolate, FXJSE_Runtime_DisposeCallback);
     67   }
     68 }
     69 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) {
     70   CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate);
     71   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
     72   v8::Local<v8::FunctionTemplate> hFuncTemplate =
     73       v8::FunctionTemplate::New(pIsolate);
     74   v8::Local<v8::Context> hContext =
     75       v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate());
     76   hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));
     77   pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);
     78   pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);
     79   return pRuntimeData;
     80 }
     81 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
     82   FXJS_PerIsolateData::SetUp(pIsolate);
     83   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
     84   if (!pData->m_pFXJSERuntimeData)
     85     pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate);
     86   return pData->m_pFXJSERuntimeData;
     87 }
     88 CFXJSE_RuntimeList* CFXJSE_RuntimeData::g_RuntimeList = NULL;
     89 void CFXJSE_RuntimeList::AppendRuntime(v8::Isolate* pIsolate) {
     90   m_RuntimeList.Add(pIsolate);
     91 }
     92 void CFXJSE_RuntimeList::RemoveRuntime(
     93     v8::Isolate* pIsolate,
     94     CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) {
     95   int32_t iIdx = m_RuntimeList.Find(pIsolate, 0);
     96   if (iIdx >= 0) {
     97     m_RuntimeList.RemoveAt(iIdx, 1);
     98   }
     99   if (lpfnDisposeCallback) {
    100     lpfnDisposeCallback(pIsolate);
    101   }
    102 }
    103 void CFXJSE_RuntimeList::RemoveAllRuntimes(
    104     CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) {
    105   int32_t iSize = m_RuntimeList.GetSize();
    106   if (lpfnDisposeCallback) {
    107     for (int32_t iIdx = 0; iIdx < iSize; iIdx++) {
    108       lpfnDisposeCallback(m_RuntimeList[iIdx]);
    109     }
    110   }
    111   m_RuntimeList.RemoveAll();
    112 }
    113