Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxjse / src / runtime.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../foxitlib.h"\r
8 #include "fxv8.h"\r
9 #include "runtime.h"\r
10 #include "scope_inline.h"\r
11 static void FXJSE_KillV8()\r
12 {\r
13     v8::V8::Dispose();\r
14 }\r
15 void FXJSE_Initialize()\r
16 {\r
17     static FX_BOOL bV8Initialized = FALSE;\r
18     if (bV8Initialized) {\r
19         return;\r
20     }\r
21     bV8Initialized = TRUE;\r
22     atexit(FXJSE_KillV8);\r
23     FX_LPCSTR szCmdFlags =\r
24         "--harmony_proxies "\r
25         "--block_concurrent_recompilation "\r
26         ;\r
27     v8::V8::SetFlagsFromString(szCmdFlags, FXSYS_strlen(szCmdFlags));\r
28     v8::V8::InitializeICU();\r
29         v8::Platform* platform = v8::platform::CreateDefaultPlatform();\r
30         v8::V8::InitializePlatform(platform);\r
31     v8::V8::Initialize();\r
32     if(!CFXJSE_RuntimeData::g_RuntimeList) {\r
33         CFXJSE_RuntimeData::g_RuntimeList = FX_NEW CFXJSE_RuntimeList;\r
34     }\r
35 }\r
36 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate)\r
37 {\r
38     {\r
39         v8::Locker locker(pIsolate);\r
40         CFXJSE_RuntimeData *pRuntimeData = reinterpret_cast<CFXJSE_RuntimeData*>(pIsolate->GetData(0));\r
41         if(pRuntimeData) {\r
42             pIsolate->SetData(0, NULL);\r
43             delete pRuntimeData;\r
44         }\r
45     }\r
46     pIsolate->Dispose();\r
47 }\r
48 void FXJSE_Finalize()\r
49 {\r
50     if(CFXJSE_RuntimeData::g_RuntimeList) {\r
51         CFXJSE_RuntimeData::g_RuntimeList->RemoveAllRuntimes(FXJSE_Runtime_DisposeCallback);\r
52         delete CFXJSE_RuntimeData::g_RuntimeList;\r
53         CFXJSE_RuntimeData::g_RuntimeList = NULL;\r
54     }\r
55 }\r
56 FXJSE_HRUNTIME  FXJSE_Runtime_Create()\r
57 {\r
58     v8::Isolate* pIsolate = v8::Isolate::New();\r
59     ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList);\r
60     CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate);\r
61     return reinterpret_cast<FXJSE_HRUNTIME>(pIsolate);\r
62 }\r
63 void FXJSE_Runtime_Release(FXJSE_HRUNTIME hRuntime)\r
64 {\r
65     v8::Isolate* pIsolate = reinterpret_cast<v8::Isolate*>(hRuntime);\r
66     if(pIsolate) {\r
67         ASSERT(CFXJSE_RuntimeData::g_RuntimeList);\r
68         CFXJSE_RuntimeData::g_RuntimeList->RemoveRuntime(pIsolate, FXJSE_Runtime_DisposeCallback);\r
69     }\r
70 }\r
71 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate)\r
72 {\r
73     CFXJSE_RuntimeData* pRuntimeData = FX_NEW CFXJSE_RuntimeData(pIsolate);\r
74     ASSERT(pRuntimeData);\r
75     CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);\r
76     v8::Local<v8::FunctionTemplate> hFuncTemplate = v8::FunctionTemplate::New(pIsolate);\r
77     v8::Local<v8::Context> hContext = v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate());\r
78     hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));\r
79     pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);\r
80     pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);\r
81     return pRuntimeData;\r
82 }\r
83 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate)\r
84 {\r
85     ASSERT(pIsolate);\r
86     CFXJSE_RuntimeData* pRuntimeData = static_cast<CFXJSE_RuntimeData*>(pIsolate->GetData(0));\r
87     if(!pRuntimeData) {\r
88         pRuntimeData = CFXJSE_RuntimeData::Create(pIsolate);\r
89         ASSERT(pRuntimeData);\r
90         pIsolate->SetData(0, pRuntimeData);\r
91     }\r
92     return pRuntimeData;\r
93 }\r
94 CFXJSE_RuntimeList * CFXJSE_RuntimeData::g_RuntimeList = NULL;\r
95 void CFXJSE_RuntimeList::AppendRuntime(v8::Isolate* pIsolate)\r
96 {\r
97     m_RuntimeList.Add(pIsolate);\r
98 }\r
99 void CFXJSE_RuntimeList::RemoveRuntime(v8::Isolate* pIsolate, CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback)\r
100 {\r
101     FX_INT32 iIdx = m_RuntimeList.Find(pIsolate, 0);\r
102     if(iIdx >= 0) {\r
103         m_RuntimeList.RemoveAt(iIdx, 1);\r
104     }\r
105     if(lpfnDisposeCallback) {\r
106         lpfnDisposeCallback(pIsolate);\r
107     }\r
108 }\r
109 void CFXJSE_RuntimeList::RemoveAllRuntimes(CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback)\r
110 {\r
111     FX_INT32 iSize = m_RuntimeList.GetSize();\r
112     if(lpfnDisposeCallback) {\r
113         for(FX_INT32 iIdx = 0; iIdx < iSize; iIdx++) {\r
114             lpfnDisposeCallback(m_RuntimeList[iIdx]);\r
115         }\r
116     }\r
117     m_RuntimeList.RemoveAll();\r
118 }\r