blob: 23763544f56beae68367ff214643fa0c0a928522 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclaire0345a42017-10-30 20:20:42 +00007#include "fxjs/cjs_global.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Dan Sinclair4b172c42017-10-23 11:22:31 -04009#include <map>
10#include <memory>
Tom Sepeza7757232017-04-18 11:10:39 -070011#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012#include <vector>
13
Dan Sinclaircfb19442017-04-20 13:13:04 -040014#include "core/fxcrt/fx_extension.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000015#include "fxjs/JS_Define.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000016#include "fxjs/cjs_event_context.h"
17#include "fxjs/cjs_eventhandler.h"
Dan Sinclaira3254622017-10-30 20:24:14 +000018#include "fxjs/cjs_globaldata.h"
19#include "fxjs/cjs_keyvalue.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000020#include "fxjs/cjs_object.h"
21#include "fxjs/js_resources.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Dan Sinclair4b172c42017-10-23 11:22:31 -040023namespace {
24
Dan Sinclair8f524d62017-10-25 13:30:31 -040025WideString PropFromV8Prop(v8::Local<v8::String> property) {
26 v8::String::Utf8Value utf8_value(property);
27 return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length()));
28}
29
Dan Sinclair4b172c42017-10-23 11:22:31 -040030template <class Alt>
31void JSSpecialPropQuery(const char*,
32 v8::Local<v8::String> property,
33 const v8::PropertyCallbackInfo<v8::Integer>& info) {
34 CJS_Runtime* pRuntime =
35 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());
36 if (!pRuntime)
37 return;
38
39 CJS_Object* pJSObj =
40 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder()));
41 if (!pJSObj)
42 return;
43
44 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
Dan Sinclair8f524d62017-10-25 13:30:31 -040045 CJS_Return result = pObj->QueryProperty(PropFromV8Prop(property).c_str());
46 info.GetReturnValue().Set(!result.HasError() ? 4 : 0);
Dan Sinclair4b172c42017-10-23 11:22:31 -040047}
48
49template <class Alt>
50void JSSpecialPropGet(const char* class_name,
51 v8::Local<v8::String> property,
52 const v8::PropertyCallbackInfo<v8::Value>& info) {
53 CJS_Runtime* pRuntime =
54 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());
55 if (!pRuntime)
56 return;
57
58 CJS_Object* pJSObj =
59 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder()));
60 if (!pJSObj)
61 return;
62
63 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
Dan Sinclair8f524d62017-10-25 13:30:31 -040064 CJS_Return result =
65 pObj->GetProperty(pRuntime, PropFromV8Prop(property).c_str());
66 if (result.HasError()) {
67 pRuntime->Error(
68 JSFormatErrorString(class_name, "GetProperty", result.Error()));
Dan Sinclair4b172c42017-10-23 11:22:31 -040069 return;
70 }
Dan Sinclair8f524d62017-10-25 13:30:31 -040071
72 if (result.HasReturn())
73 info.GetReturnValue().Set(result.Return());
Dan Sinclair4b172c42017-10-23 11:22:31 -040074}
75
76template <class Alt>
77void JSSpecialPropPut(const char* class_name,
78 v8::Local<v8::String> property,
79 v8::Local<v8::Value> value,
80 const v8::PropertyCallbackInfo<v8::Value>& info) {
81 CJS_Runtime* pRuntime =
82 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());
83 if (!pRuntime)
84 return;
85
86 CJS_Object* pJSObj =
87 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder()));
88 if (!pJSObj)
89 return;
90
91 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
Dan Sinclair8f524d62017-10-25 13:30:31 -040092 CJS_Return result =
93 pObj->SetProperty(pRuntime, PropFromV8Prop(property).c_str(), value);
94 if (result.HasError()) {
95 pRuntime->Error(
96 JSFormatErrorString(class_name, "PutProperty", result.Error()));
97 }
Dan Sinclair4b172c42017-10-23 11:22:31 -040098}
99
100template <class Alt>
101void JSSpecialPropDel(const char* class_name,
102 v8::Local<v8::String> property,
103 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
104 CJS_Runtime* pRuntime =
105 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate());
106 if (!pRuntime)
107 return;
108
109 CJS_Object* pJSObj =
110 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder()));
111 if (!pJSObj)
112 return;
113
114 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
Dan Sinclair8f524d62017-10-25 13:30:31 -0400115 CJS_Return result =
116 pObj->DelProperty(pRuntime, PropFromV8Prop(property).c_str());
117 if (result.HasError()) {
118 // TODO(dsinclair): Should this set the pRuntime->Error result?
Henrique Nakashima11506302017-12-06 22:09:40 +0000119 // ByteString cbName =
120 // ByteString::Format("%s.%s", class_name, "DelProperty");
Dan Sinclair4b172c42017-10-23 11:22:31 -0400121 }
122}
123
124struct JSGlobalData {
125 JSGlobalData();
126 ~JSGlobalData();
127
128 JS_GlobalDataType nType;
129 double dData;
130 bool bData;
131 ByteString sData;
132 v8::Global<v8::Object> pData;
133 bool bPersistent;
134 bool bDeleted;
135};
136
137class JSGlobalAlternate : public CJS_EmbedObj {
138 public:
139 explicit JSGlobalAlternate(CJS_Object* pJSObject);
140 ~JSGlobalAlternate() override;
141
Dan Sinclair8f524d62017-10-25 13:30:31 -0400142 CJS_Return setPersistent(CJS_Runtime* pRuntime,
143 const std::vector<v8::Local<v8::Value>>& params);
144 CJS_Return QueryProperty(const wchar_t* propname);
145 CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname);
146 CJS_Return SetProperty(CJS_Runtime* pRuntime,
147 const wchar_t* propname,
148 v8::Local<v8::Value> vp);
149 CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname);
Dan Sinclair4b172c42017-10-23 11:22:31 -0400150 void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv);
151
152 private:
153 void UpdateGlobalPersistentVariables();
154 void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime);
155 void DestroyGlobalPersisitentVariables();
Dan Sinclair8f524d62017-10-25 13:30:31 -0400156 CJS_Return SetGlobalVariables(const ByteString& propname,
157 JS_GlobalDataType nType,
158 double dData,
159 bool bData,
160 const ByteString& sData,
161 v8::Local<v8::Object> pData,
162 bool bDefaultPersistent);
Dan Sinclair4b172c42017-10-23 11:22:31 -0400163 void ObjectToArray(CJS_Runtime* pRuntime,
164 v8::Local<v8::Object> pObj,
165 CJS_GlobalVariableArray& array);
166 void PutObjectProperty(v8::Local<v8::Object> obj, CJS_KeyValue* pData);
167
168 std::map<ByteString, std::unique_ptr<JSGlobalData>> m_MapGlobal;
169 WideString m_sFilePath;
170 CJS_GlobalData* m_pGlobalData;
171 CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
172};
173
174} // namespace
175
Dan Sinclairc94a7932017-10-26 16:48:57 -0400176const JSMethodSpec CJS_Global::MethodSpecs[] = {
Dan Sinclair909fa2d2017-12-12 01:53:28 +0000177 {"setPersistent", setPersistent_static}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
Dan Sinclairef299532017-10-26 16:48:30 -0400179int CJS_Global::ObjDefnID = -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Dan Sinclair4b172c42017-10-23 11:22:31 -0400181// static
182void CJS_Global::setPersistent_static(
183 const v8::FunctionCallbackInfo<v8::Value>& info) {
184 JSMethod<JSGlobalAlternate, &JSGlobalAlternate::setPersistent>(
185 "setPersistent", "global", info);
186}
187
Dan Sinclairef299532017-10-26 16:48:30 -0400188// static
Dan Sinclair89d26c82017-10-26 12:21:28 -0400189void CJS_Global::queryprop_static(
190 v8::Local<v8::String> property,
191 const v8::PropertyCallbackInfo<v8::Integer>& info) {
192 JSSpecialPropQuery<JSGlobalAlternate>("global", property, info);
193}
194
Dan Sinclairef299532017-10-26 16:48:30 -0400195// static
Dan Sinclair89d26c82017-10-26 12:21:28 -0400196void CJS_Global::getprop_static(
197 v8::Local<v8::String> property,
198 const v8::PropertyCallbackInfo<v8::Value>& info) {
199 JSSpecialPropGet<JSGlobalAlternate>("global", property, info);
200}
201
Dan Sinclairef299532017-10-26 16:48:30 -0400202// static
Dan Sinclair89d26c82017-10-26 12:21:28 -0400203void CJS_Global::putprop_static(
204 v8::Local<v8::String> property,
205 v8::Local<v8::Value> value,
206 const v8::PropertyCallbackInfo<v8::Value>& info) {
207 JSSpecialPropPut<JSGlobalAlternate>("global", property, value, info);
208}
209
Dan Sinclairef299532017-10-26 16:48:30 -0400210// static
Dan Sinclair89d26c82017-10-26 12:21:28 -0400211void CJS_Global::delprop_static(
212 v8::Local<v8::String> property,
213 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
214 JSSpecialPropDel<JSGlobalAlternate>("global", property, info);
215}
216
Dan Sinclairef299532017-10-26 16:48:30 -0400217// static
Dan Sinclair89d26c82017-10-26 12:21:28 -0400218void CJS_Global::DefineAllProperties(CFXJS_Engine* pEngine) {
219 pEngine->DefineObjAllProperties(
Dan Sinclairef299532017-10-26 16:48:30 -0400220 ObjDefnID, CJS_Global::queryprop_static, CJS_Global::getprop_static,
Dan Sinclair89d26c82017-10-26 12:21:28 -0400221 CJS_Global::putprop_static, CJS_Global::delprop_static);
222}
223
Dan Sinclairef299532017-10-26 16:48:30 -0400224// static
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400225void CJS_Global::DefineJSObjects(CFXJS_Engine* pEngine) {
226 ObjDefnID = pEngine->DefineObj("global", FXJSOBJTYPE_STATIC,
Dan Sinclairef299532017-10-26 16:48:30 -0400227 JSConstructor<CJS_Global, JSGlobalAlternate>,
228 JSDestructor<CJS_Global>);
Dan Sinclair909fa2d2017-12-12 01:53:28 +0000229 DefineMethods(pEngine, ObjDefnID, MethodSpecs, FX_ArraySize(MethodSpecs));
Dan Sinclair89d26c82017-10-26 12:21:28 -0400230 DefineAllProperties(pEngine);
231}
232
Dan Sinclairef299532017-10-26 16:48:30 -0400233void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) {
234 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
235 JSGlobalAlternate* pGlobal =
236 static_cast<JSGlobalAlternate*>(GetEmbedObject());
237 pGlobal->Initial(pRuntime->GetFormFillEnv());
238}
239
weili625ad662016-06-15 11:21:33 -0700240JSGlobalData::JSGlobalData()
weili47228ac2016-07-20 10:35:31 -0700241 : nType(JS_GlobalDataType::NUMBER),
weili625ad662016-06-15 11:21:33 -0700242 dData(0),
tsepez4cf55152016-11-02 14:37:54 -0700243 bData(false),
weili625ad662016-06-15 11:21:33 -0700244 sData(""),
tsepez4cf55152016-11-02 14:37:54 -0700245 bPersistent(false),
246 bDeleted(false) {}
weili625ad662016-06-15 11:21:33 -0700247
248JSGlobalData::~JSGlobalData() {
249 pData.Reset();
250}
251
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700252JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject)
dsinclair8779fa82016-10-12 12:05:44 -0700253 : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700255JSGlobalAlternate::~JSGlobalAlternate() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 DestroyGlobalPersisitentVariables();
Tom Sepezf4583622015-09-14 15:06:53 -0700257 m_pGlobalData->Release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
dsinclair8779fa82016-10-12 12:05:44 -0700260void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
Tom Sepez77f6d0f2017-02-23 12:14:10 -0800261 m_pFormFillEnv.Reset(pFormFillEnv);
dsinclair8779fa82016-10-12 12:05:44 -0700262 m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 UpdateGlobalPersistentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700265
Dan Sinclair8f524d62017-10-25 13:30:31 -0400266CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) {
267 return CJS_Return(WideString(propname) != L"setPersistent");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Dan Sinclair8f524d62017-10-25 13:30:31 -0400270CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime,
271 const wchar_t* propname) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400272 auto it = m_MapGlobal.find(ByteString::FromUnicode(propname));
Tom Sepeza7757232017-04-18 11:10:39 -0700273 if (it == m_MapGlobal.end())
Dan Sinclair8f524d62017-10-25 13:30:31 -0400274 return CJS_Return(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275
tsepez4cf55152016-11-02 14:37:54 -0700276 it->second->bDeleted = true;
Dan Sinclair8f524d62017-10-25 13:30:31 -0400277 return CJS_Return(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Dan Sinclair8f524d62017-10-25 13:30:31 -0400280CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime,
281 const wchar_t* propname) {
dan sinclaircbe23db2017-10-19 14:29:33 -0400282 auto it = m_MapGlobal.find(ByteString::FromUnicode(propname));
dan sinclair5daf07a2017-10-24 21:46:57 -0400283 if (it == m_MapGlobal.end())
Dan Sinclair8f524d62017-10-25 13:30:31 -0400284 return CJS_Return(true);
dan sinclaircbe23db2017-10-19 14:29:33 -0400285
286 JSGlobalData* pData = it->second.get();
dan sinclair5daf07a2017-10-24 21:46:57 -0400287 if (pData->bDeleted)
Dan Sinclair8f524d62017-10-25 13:30:31 -0400288 return CJS_Return(true);
dan sinclaircbe23db2017-10-19 14:29:33 -0400289
290 switch (pData->nType) {
291 case JS_GlobalDataType::NUMBER:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400292 return CJS_Return(pRuntime->NewNumber(pData->dData));
dan sinclaircbe23db2017-10-19 14:29:33 -0400293 case JS_GlobalDataType::BOOLEAN:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400294 return CJS_Return(pRuntime->NewBoolean(pData->bData));
dan sinclaircbe23db2017-10-19 14:29:33 -0400295 case JS_GlobalDataType::STRING:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400296 return CJS_Return(pRuntime->NewString(
Dan Sinclaire4974922017-10-24 09:36:16 -0400297 WideString::FromLocal(pData->sData.c_str()).c_str()));
Dan Sinclair8f524d62017-10-25 13:30:31 -0400298 case JS_GlobalDataType::OBJECT:
299 return CJS_Return(
300 v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData));
dan sinclaircbe23db2017-10-19 14:29:33 -0400301 case JS_GlobalDataType::NULLOBJ:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400302 return CJS_Return(pRuntime->NewNull());
dan sinclaircbe23db2017-10-19 14:29:33 -0400303 default:
304 break;
305 }
Dan Sinclair8f524d62017-10-25 13:30:31 -0400306 return CJS_Return(false);
dan sinclaircbe23db2017-10-19 14:29:33 -0400307}
308
Dan Sinclair8f524d62017-10-25 13:30:31 -0400309CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime,
310 const wchar_t* propname,
311 v8::Local<v8::Value> vp) {
dan sinclaircbe23db2017-10-19 14:29:33 -0400312 ByteString sPropName = ByteString::FromUnicode(propname);
dan sinclair80435cb2017-10-24 21:40:24 -0400313 if (vp->IsNumber()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400314 return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER,
dan sinclair80435cb2017-10-24 21:40:24 -0400315 pRuntime->ToDouble(vp), false, "",
Dan Sinclair3cac3602017-10-24 15:15:27 -0400316 v8::Local<v8::Object>(), false);
317 }
dan sinclair80435cb2017-10-24 21:40:24 -0400318 if (vp->IsBoolean()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400319 return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0,
dan sinclair80435cb2017-10-24 21:40:24 -0400320 pRuntime->ToBoolean(vp), "",
Dan Sinclair3cac3602017-10-24 15:15:27 -0400321 v8::Local<v8::Object>(), false);
322 }
dan sinclair80435cb2017-10-24 21:40:24 -0400323 if (vp->IsString()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400324 return SetGlobalVariables(
325 sPropName, JS_GlobalDataType::STRING, 0, false,
dan sinclair80435cb2017-10-24 21:40:24 -0400326 ByteString::FromUnicode(pRuntime->ToWideString(vp)),
Dan Sinclair3cac3602017-10-24 15:15:27 -0400327 v8::Local<v8::Object>(), false);
328 }
dan sinclair80435cb2017-10-24 21:40:24 -0400329 if (vp->IsObject()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400330 return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false,
dan sinclair80435cb2017-10-24 21:40:24 -0400331 "", pRuntime->ToObject(vp), false);
Dan Sinclair3cac3602017-10-24 15:15:27 -0400332 }
dan sinclair80435cb2017-10-24 21:40:24 -0400333 if (vp->IsNull()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400334 return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false,
335 "", v8::Local<v8::Object>(), false);
336 }
dan sinclair80435cb2017-10-24 21:40:24 -0400337 if (vp->IsUndefined()) {
Dan Sinclair3cac3602017-10-24 15:15:27 -0400338 DelProperty(pRuntime, propname);
Dan Sinclair8f524d62017-10-25 13:30:31 -0400339 return CJS_Return(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 }
Dan Sinclair8f524d62017-10-25 13:30:31 -0400341 return CJS_Return(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342}
343
Dan Sinclair8f524d62017-10-25 13:30:31 -0400344CJS_Return JSGlobalAlternate::setPersistent(
dan sinclair80435cb2017-10-24 21:40:24 -0400345 CJS_Runtime* pRuntime,
Dan Sinclair8f524d62017-10-25 13:30:31 -0400346 const std::vector<v8::Local<v8::Value>>& params) {
347 if (params.size() != 2)
Dan Sinclaird6e9cfa2017-10-30 21:19:42 +0000348 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
Dan Sinclair8f524d62017-10-25 13:30:31 -0400349
Dan Sinclair1b2a18e2017-10-24 13:56:29 -0400350 auto it = m_MapGlobal.find(
dan sinclair80435cb2017-10-24 21:40:24 -0400351 ByteString::FromUnicode(pRuntime->ToWideString(params[0])));
Dan Sinclair8f524d62017-10-25 13:30:31 -0400352 if (it == m_MapGlobal.end() || it->second->bDeleted)
Dan Sinclaird6e9cfa2017-10-30 21:19:42 +0000353 return CJS_Return(JSGetStringFromID(JSMessage::kGlobalNotFoundError));
Dan Sinclair8f524d62017-10-25 13:30:31 -0400354
dan sinclair80435cb2017-10-24 21:40:24 -0400355 it->second->bPersistent = pRuntime->ToBoolean(params[1]);
Dan Sinclair8f524d62017-10-25 13:30:31 -0400356 return CJS_Return(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700357}
358
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700359void JSGlobalAlternate::UpdateGlobalPersistentVariables() {
tsepezb4694242016-08-15 16:44:55 -0700360 CJS_Runtime* pRuntime =
361 static_cast<CJS_Runtime*>(CFXJS_Engine::CurrentEngineFromIsolate(
362 m_pJSObject->ToV8Object()->GetIsolate()));
363
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) {
365 CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 switch (pData->data.nType) {
weili47228ac2016-07-20 10:35:31 -0700367 case JS_GlobalDataType::NUMBER:
368 SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 pData->data.dData, false, "",
370 v8::Local<v8::Object>(), pData->bPersistent == 1);
tsepeze6cf0132017-01-18 14:38:18 -0800371 pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(),
372 pData->data.sKey.UTF8Decode(),
373 pRuntime->NewNumber(pData->data.dData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 break;
weili47228ac2016-07-20 10:35:31 -0700375 case JS_GlobalDataType::BOOLEAN:
376 SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0,
tsepeze6cf0132017-01-18 14:38:18 -0800377 pData->data.bData == 1, "", v8::Local<v8::Object>(),
378 pData->bPersistent == 1);
379 pRuntime->PutObjectProperty(
380 m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(),
381 pRuntime->NewBoolean(pData->data.bData == 1));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 break;
weili47228ac2016-07-20 10:35:31 -0700383 case JS_GlobalDataType::STRING:
384 SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 false, pData->data.sData, v8::Local<v8::Object>(),
386 pData->bPersistent == 1);
tsepeze6cf0132017-01-18 14:38:18 -0800387 pRuntime->PutObjectProperty(
388 m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(),
Ryan Harrison275e2602017-09-18 14:23:18 -0400389 pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 break;
weili47228ac2016-07-20 10:35:31 -0700391 case JS_GlobalDataType::OBJECT: {
tsepezb4694242016-08-15 16:44:55 -0700392 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700393 if (!pObj.IsEmpty()) {
394 PutObjectProperty(pObj, &pData->data);
395 SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0,
396 false, "", pObj, pData->bPersistent == 1);
397 pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(),
398 pData->data.sKey.UTF8Decode(), pObj);
399 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 } break;
weili47228ac2016-07-20 10:35:31 -0700401 case JS_GlobalDataType::NULLOBJ:
402 SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0,
403 false, "", v8::Local<v8::Object>(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 pData->bPersistent == 1);
tsepeze6cf0132017-01-18 14:38:18 -0800405 pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(),
406 pData->data.sKey.UTF8Decode(),
407 pRuntime->NewNull());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700409 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}
412
Tom Sepezb1670b52017-02-16 17:01:00 -0800413void JSGlobalAlternate::CommitGlobalPersisitentVariables(
414 CJS_Runtime* pRuntime) {
Tom Sepeza7757232017-04-18 11:10:39 -0700415 for (const auto& iter : m_MapGlobal) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400416 ByteString name = iter.first;
Tom Sepeza7757232017-04-18 11:10:39 -0700417 JSGlobalData* pData = iter.second.get();
Tom Sepez09d33bc2015-08-19 09:49:24 -0700418 if (pData->bDeleted) {
419 m_pGlobalData->DeleteGlobalVariable(name);
Tom Sepeza7757232017-04-18 11:10:39 -0700420 continue;
421 }
422 switch (pData->nType) {
423 case JS_GlobalDataType::NUMBER:
424 m_pGlobalData->SetGlobalVariableNumber(name, pData->dData);
425 m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
426 break;
427 case JS_GlobalDataType::BOOLEAN:
428 m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData);
429 m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
430 break;
431 case JS_GlobalDataType::STRING:
432 m_pGlobalData->SetGlobalVariableString(name, pData->sData);
433 m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
434 break;
435 case JS_GlobalDataType::OBJECT: {
436 CJS_GlobalVariableArray array;
437 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(
438 GetJSObject()->GetIsolate(), pData->pData);
439 ObjectToArray(pRuntime, obj, array);
440 m_pGlobalData->SetGlobalVariableObject(name, array);
441 m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
442 } break;
443 case JS_GlobalDataType::NULLOBJ:
444 m_pGlobalData->SetGlobalVariableNull(name);
445 m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
446 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 }
448 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449}
450
Tom Sepezb1670b52017-02-16 17:01:00 -0800451void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700452 v8::Local<v8::Object> pObj,
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700453 CJS_GlobalVariableArray& array) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400454 std::vector<WideString> pKeyList = pRuntime->GetObjectPropertyNames(pObj);
tsepezd0b6ed12016-08-11 19:50:57 -0700455 for (const auto& ws : pKeyList) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400456 ByteString sKey = ws.UTF8Encode();
tsepezb4694242016-08-15 16:44:55 -0700457 v8::Local<v8::Value> v = pRuntime->GetObjectProperty(pObj, ws);
Dan Sinclair3cac3602017-10-24 15:15:27 -0400458 if (v->IsNumber()) {
459 CJS_KeyValue* pObjElement = new CJS_KeyValue;
460 pObjElement->nType = JS_GlobalDataType::NUMBER;
461 pObjElement->sKey = sKey;
462 pObjElement->dData = pRuntime->ToDouble(v);
463 array.Add(pObjElement);
464 continue;
465 }
466 if (v->IsBoolean()) {
467 CJS_KeyValue* pObjElement = new CJS_KeyValue;
468 pObjElement->nType = JS_GlobalDataType::BOOLEAN;
469 pObjElement->sKey = sKey;
470 pObjElement->dData = pRuntime->ToBoolean(v);
471 array.Add(pObjElement);
472 continue;
473 }
474 if (v->IsString()) {
475 ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v));
476 CJS_KeyValue* pObjElement = new CJS_KeyValue;
477 pObjElement->nType = JS_GlobalDataType::STRING;
478 pObjElement->sKey = sKey;
479 pObjElement->sData = sValue;
480 array.Add(pObjElement);
481 continue;
482 }
483 if (v->IsObject()) {
484 CJS_KeyValue* pObjElement = new CJS_KeyValue;
485 pObjElement->nType = JS_GlobalDataType::OBJECT;
486 pObjElement->sKey = sKey;
487 ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData);
488 array.Add(pObjElement);
489 continue;
490 }
491 if (v->IsNull()) {
492 CJS_KeyValue* pObjElement = new CJS_KeyValue;
493 pObjElement->nType = JS_GlobalDataType::NULLOBJ;
494 pObjElement->sKey = sKey;
495 array.Add(pObjElement);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 }
497 }
498}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700499
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700500void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj,
501 CJS_KeyValue* pData) {
tsepezb4694242016-08-15 16:44:55 -0700502 CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate(
503 m_pJSObject->ToV8Object()->GetIsolate());
504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 for (int i = 0, sz = pData->objData.Count(); i < sz; i++) {
506 CJS_KeyValue* pObjData = pData->objData.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 switch (pObjData->nType) {
weili47228ac2016-07-20 10:35:31 -0700508 case JS_GlobalDataType::NUMBER:
tsepeze6cf0132017-01-18 14:38:18 -0800509 pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(),
510 pRuntime->NewNumber(pObjData->dData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 break;
weili47228ac2016-07-20 10:35:31 -0700512 case JS_GlobalDataType::BOOLEAN:
tsepeze6cf0132017-01-18 14:38:18 -0800513 pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(),
514 pRuntime->NewBoolean(pObjData->bData == 1));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 break;
weili47228ac2016-07-20 10:35:31 -0700516 case JS_GlobalDataType::STRING:
tsepeze6cf0132017-01-18 14:38:18 -0800517 pRuntime->PutObjectProperty(
518 pObj, pObjData->sKey.UTF8Decode(),
Ryan Harrison275e2602017-09-18 14:23:18 -0400519 pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringView()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 break;
weili47228ac2016-07-20 10:35:31 -0700521 case JS_GlobalDataType::OBJECT: {
tsepezb4694242016-08-15 16:44:55 -0700522 v8::Local<v8::Object> pNewObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700523 if (!pNewObj.IsEmpty()) {
524 PutObjectProperty(pNewObj, pObjData);
525 pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(),
526 pNewObj);
527 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 } break;
weili47228ac2016-07-20 10:35:31 -0700529 case JS_GlobalDataType::NULLOBJ:
tsepeze6cf0132017-01-18 14:38:18 -0800530 pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(),
531 pRuntime->NewNull());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 break;
533 }
534 }
535}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700536
Tom Sepez7dc5cc12015-08-17 12:15:26 -0700537void JSGlobalAlternate::DestroyGlobalPersisitentVariables() {
Tom Sepeza7757232017-04-18 11:10:39 -0700538 m_MapGlobal.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539}
540
Dan Sinclair8f524d62017-10-25 13:30:31 -0400541CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname,
542 JS_GlobalDataType nType,
543 double dData,
544 bool bData,
545 const ByteString& sData,
546 v8::Local<v8::Object> pData,
547 bool bDefaultPersistent) {
tsepezb4c9f3f2016-04-13 15:41:21 -0700548 if (propname.IsEmpty())
Dan Sinclair8f524d62017-10-25 13:30:31 -0400549 return CJS_Return(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550
Tom Sepeza7757232017-04-18 11:10:39 -0700551 auto it = m_MapGlobal.find(propname);
552 if (it != m_MapGlobal.end()) {
553 JSGlobalData* pTemp = it->second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 if (pTemp->bDeleted || pTemp->nType != nType) {
555 pTemp->dData = 0;
556 pTemp->bData = 0;
Lei Zhangfe2cd4d2017-11-22 20:04:12 +0000557 pTemp->sData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 pTemp->nType = nType;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700559 }
tsepez4cf55152016-11-02 14:37:54 -0700560 pTemp->bDeleted = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 switch (nType) {
Tom Sepeza7757232017-04-18 11:10:39 -0700562 case JS_GlobalDataType::NUMBER:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 pTemp->dData = dData;
Tom Sepeza7757232017-04-18 11:10:39 -0700564 break;
565 case JS_GlobalDataType::BOOLEAN:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 pTemp->bData = bData;
Tom Sepeza7757232017-04-18 11:10:39 -0700567 break;
568 case JS_GlobalDataType::STRING:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 pTemp->sData = sData;
Tom Sepeza7757232017-04-18 11:10:39 -0700570 break;
571 case JS_GlobalDataType::OBJECT:
Tom Sepez371379d2015-11-06 08:29:39 -0800572 pTemp->pData.Reset(pData->GetIsolate(), pData);
Tom Sepeza7757232017-04-18 11:10:39 -0700573 break;
weili47228ac2016-07-20 10:35:31 -0700574 case JS_GlobalDataType::NULLOBJ:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700575 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 default:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400577 return CJS_Return(false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700578 }
Dan Sinclair8f524d62017-10-25 13:30:31 -0400579 return CJS_Return(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 }
581
Tom Sepeza7757232017-04-18 11:10:39 -0700582 auto pNewData = pdfium::MakeUnique<JSGlobalData>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 switch (nType) {
Tom Sepeza7757232017-04-18 11:10:39 -0700584 case JS_GlobalDataType::NUMBER:
weili47228ac2016-07-20 10:35:31 -0700585 pNewData->nType = JS_GlobalDataType::NUMBER;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 pNewData->dData = dData;
587 pNewData->bPersistent = bDefaultPersistent;
Tom Sepeza7757232017-04-18 11:10:39 -0700588 break;
589 case JS_GlobalDataType::BOOLEAN:
weili47228ac2016-07-20 10:35:31 -0700590 pNewData->nType = JS_GlobalDataType::BOOLEAN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591 pNewData->bData = bData;
592 pNewData->bPersistent = bDefaultPersistent;
Tom Sepeza7757232017-04-18 11:10:39 -0700593 break;
594 case JS_GlobalDataType::STRING:
weili47228ac2016-07-20 10:35:31 -0700595 pNewData->nType = JS_GlobalDataType::STRING;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 pNewData->sData = sData;
597 pNewData->bPersistent = bDefaultPersistent;
Tom Sepeza7757232017-04-18 11:10:39 -0700598 break;
599 case JS_GlobalDataType::OBJECT:
weili47228ac2016-07-20 10:35:31 -0700600 pNewData->nType = JS_GlobalDataType::OBJECT;
Tom Sepez371379d2015-11-06 08:29:39 -0800601 pNewData->pData.Reset(pData->GetIsolate(), pData);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 pNewData->bPersistent = bDefaultPersistent;
Tom Sepeza7757232017-04-18 11:10:39 -0700603 break;
604 case JS_GlobalDataType::NULLOBJ:
weili47228ac2016-07-20 10:35:31 -0700605 pNewData->nType = JS_GlobalDataType::NULLOBJ;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 pNewData->bPersistent = bDefaultPersistent;
Tom Sepeza7757232017-04-18 11:10:39 -0700607 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 default:
Dan Sinclair8f524d62017-10-25 13:30:31 -0400609 return CJS_Return(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 }
Tom Sepeza7757232017-04-18 11:10:39 -0700611 m_MapGlobal[propname] = std::move(pNewData);
Dan Sinclair8f524d62017-10-25 13:30:31 -0400612 return CJS_Return(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700613}