Pass v8::Isolate to PDFium at init time.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index edc8c25..5c463ce 100644 (file)
 #include "../../include/javascript/global.h"
 #include "../../include/javascript/console.h"
 
-CJS_RuntimeFactory::~CJS_RuntimeFactory() {}
-
-IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp) {
-  m_bInit = true;
-  return new CJS_Runtime(pApp);
-}
-void CJS_RuntimeFactory::AddRef() {
-  m_nRef++;
-}
-void CJS_RuntimeFactory::Release() {
-  if (m_bInit) {
-    if (--m_nRef == 0) {
-      FXJS_Release();
-      m_bInit = FALSE;
-    }
-  }
-}
-
-void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime) {
-  delete (CJS_Runtime*)pRuntime;
-}
-
 /* ------------------------------ CJS_Runtime ------------------------------ */
 
 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
     : m_pApp(pApp),
       m_pDocument(NULL),
       m_bBlocking(FALSE),
-      m_pFieldEventPath(NULL),
       m_isolate(NULL),
       m_isolateManaged(false) {
-  unsigned int embedderDataSlot = 0;
-  if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
-    m_isolate = reinterpret_cast<v8::Isolate*>(
-        m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
-    embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
-  }
-  if (!m_isolate) {
-    m_pArrayBufferAllocator.reset(new FXJS_ArrayBufferAllocator());
-
-    v8::Isolate::CreateParams params;
-    params.array_buffer_allocator = m_pArrayBufferAllocator.get();
-    m_isolate = v8::Isolate::New(params);
-    m_isolateManaged = true;
+  IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
+  if (pPlatform->version <= 2) {
+    unsigned int embedderDataSlot = 0;
+    v8::Isolate* pExternalIsolate = nullptr;
+    if (pPlatform->version == 2) {
+      pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
+      embedderDataSlot = pPlatform->m_v8EmbedderSlot;
+    }
+    FXJS_Initialize(embedderDataSlot, pExternalIsolate);
   }
-
-  FXJS_Initialize(embedderDataSlot);
+  m_isolateManaged = FXJS_GetIsolate(&m_isolate);
   DefineJSObjects();
 
   CJS_Context* pContext = (CJS_Context*)NewContext();
@@ -87,11 +59,9 @@ CJS_Runtime::~CJS_Runtime() {
 
   m_ContextArray.RemoveAll();
   FXJS_ReleaseRuntime(GetIsolate(), m_context);
-  RemoveEventsInLoop(m_pFieldEventPath);
 
   m_pApp = NULL;
   m_pDocument = NULL;
-  m_pFieldEventPath = NULL;
   m_context.Reset();
 
   if (m_isolateManaged)
@@ -194,75 +164,12 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
   }
 }
 
-FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName,
-                                    JS_EVENT_T eEventType) {
-  if (m_pFieldEventPath == NULL) {
-    m_pFieldEventPath = new CJS_FieldEvent;
-    m_pFieldEventPath->sTargetName = sTargetName;
-    m_pFieldEventPath->eEventType = eEventType;
-    m_pFieldEventPath->pNext = NULL;
-
-    return TRUE;
-  }
-
-  // to search
-  CJS_FieldEvent* p = m_pFieldEventPath;
-  CJS_FieldEvent* pLast = m_pFieldEventPath;
-  while (p) {
-    if (p->eEventType == eEventType && p->sTargetName == sTargetName)
-      return FALSE;
-
-    pLast = p;
-    p = p->pNext;
-  }
-
-  // to add
-  CJS_FieldEvent* pNew = new CJS_FieldEvent;
-  pNew->sTargetName = sTargetName;
-  pNew->eEventType = eEventType;
-  pNew->pNext = NULL;
-
-  pLast->pNext = pNew;
-
-  return TRUE;
+bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
+  return m_FieldEventSet.insert(event).second;
 }
 
-void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName,
-                                    JS_EVENT_T eEventType) {
-  FX_BOOL bFind = FALSE;
-
-  CJS_FieldEvent* p = m_pFieldEventPath;
-  CJS_FieldEvent* pLast = NULL;
-  while (p) {
-    if (p->eEventType == eEventType && p->sTargetName == sTargetName) {
-      bFind = TRUE;
-      break;
-    }
-
-    pLast = p;
-    p = p->pNext;
-  }
-
-  if (bFind) {
-    RemoveEventsInLoop(p);
-
-    if (p == m_pFieldEventPath)
-      m_pFieldEventPath = NULL;
-
-    if (pLast)
-      pLast->pNext = NULL;
-  }
-}
-
-void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) {
-  CJS_FieldEvent* p = pStart;
-
-  while (p) {
-    CJS_FieldEvent* pOld = p;
-    p = pOld->pNext;
-
-    delete pOld;
-  }
+void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
+  m_FieldEventSet.erase(event);
 }
 
 v8::Local<v8::Context> CJS_Runtime::NewJSContext() {