OLD | NEW |
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/javascript/global.h" | 7 #include "fpdfsdk/javascript/global.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { | 32 void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { |
33 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime); | 33 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime); |
34 JSGlobalAlternate* pGlobal = | 34 JSGlobalAlternate* pGlobal = |
35 static_cast<JSGlobalAlternate*>(GetEmbedObject()); | 35 static_cast<JSGlobalAlternate*>(GetEmbedObject()); |
36 pGlobal->Initial(pRuntime->GetFormFillEnv()); | 36 pGlobal->Initial(pRuntime->GetFormFillEnv()); |
37 } | 37 } |
38 | 38 |
39 JSGlobalData::JSGlobalData() | 39 JSGlobalData::JSGlobalData() |
40 : nType(JS_GlobalDataType::NUMBER), | 40 : nType(JS_GlobalDataType::NUMBER), |
41 dData(0), | 41 dData(0), |
42 bData(FALSE), | 42 bData(false), |
43 sData(""), | 43 sData(""), |
44 bPersistent(FALSE), | 44 bPersistent(false), |
45 bDeleted(FALSE) {} | 45 bDeleted(false) {} |
46 | 46 |
47 JSGlobalData::~JSGlobalData() { | 47 JSGlobalData::~JSGlobalData() { |
48 pData.Reset(); | 48 pData.Reset(); |
49 } | 49 } |
50 | 50 |
51 JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) | 51 JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) |
52 : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} | 52 : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} |
53 | 53 |
54 JSGlobalAlternate::~JSGlobalAlternate() { | 54 JSGlobalAlternate::~JSGlobalAlternate() { |
55 DestroyGlobalPersisitentVariables(); | 55 DestroyGlobalPersisitentVariables(); |
56 m_pGlobalData->Release(); | 56 m_pGlobalData->Release(); |
57 } | 57 } |
58 | 58 |
59 void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { | 59 void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { |
60 m_pFormFillEnv = pFormFillEnv; | 60 m_pFormFillEnv = pFormFillEnv; |
61 m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); | 61 m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); |
62 UpdateGlobalPersistentVariables(); | 62 UpdateGlobalPersistentVariables(); |
63 } | 63 } |
64 | 64 |
65 FX_BOOL JSGlobalAlternate::QueryProperty(const FX_WCHAR* propname) { | 65 bool JSGlobalAlternate::QueryProperty(const FX_WCHAR* propname) { |
66 return CFX_WideString(propname) != L"setPersistent"; | 66 return CFX_WideString(propname) != L"setPersistent"; |
67 } | 67 } |
68 | 68 |
69 FX_BOOL JSGlobalAlternate::DelProperty(IJS_Context* cc, | 69 bool JSGlobalAlternate::DelProperty(IJS_Context* cc, |
70 const FX_WCHAR* propname, | 70 const FX_WCHAR* propname, |
71 CFX_WideString& sError) { | 71 CFX_WideString& sError) { |
72 auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname)); | 72 auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname)); |
73 if (it == m_mapGlobal.end()) | 73 if (it == m_mapGlobal.end()) |
74 return FALSE; | 74 return false; |
75 | 75 |
76 it->second->bDeleted = TRUE; | 76 it->second->bDeleted = true; |
77 return TRUE; | 77 return true; |
78 } | 78 } |
79 | 79 |
80 FX_BOOL JSGlobalAlternate::DoProperty(IJS_Context* cc, | 80 bool JSGlobalAlternate::DoProperty(IJS_Context* cc, |
81 const FX_WCHAR* propname, | 81 const FX_WCHAR* propname, |
82 CJS_PropValue& vp, | 82 CJS_PropValue& vp, |
83 CFX_WideString& sError) { | 83 CFX_WideString& sError) { |
84 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 84 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
85 if (vp.IsSetting()) { | 85 if (vp.IsSetting()) { |
86 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); | 86 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); |
87 switch (vp.GetJSValue()->GetType()) { | 87 switch (vp.GetJSValue()->GetType()) { |
88 case CJS_Value::VT_number: { | 88 case CJS_Value::VT_number: { |
89 double dData; | 89 double dData; |
90 vp >> dData; | 90 vp >> dData; |
91 return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, dData, | 91 return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, dData, |
92 false, "", v8::Local<v8::Object>(), FALSE); | 92 false, "", v8::Local<v8::Object>(), false); |
93 } | 93 } |
94 case CJS_Value::VT_boolean: { | 94 case CJS_Value::VT_boolean: { |
95 bool bData; | 95 bool bData; |
96 vp >> bData; | 96 vp >> bData; |
97 return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, | 97 return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, |
98 bData, "", v8::Local<v8::Object>(), FALSE); | 98 bData, "", v8::Local<v8::Object>(), false); |
99 } | 99 } |
100 case CJS_Value::VT_string: { | 100 case CJS_Value::VT_string: { |
101 CFX_ByteString sData; | 101 CFX_ByteString sData; |
102 vp >> sData; | 102 vp >> sData; |
103 return SetGlobalVariables(sPropName, JS_GlobalDataType::STRING, 0, | 103 return SetGlobalVariables(sPropName, JS_GlobalDataType::STRING, 0, |
104 false, sData, v8::Local<v8::Object>(), FALSE); | 104 false, sData, v8::Local<v8::Object>(), false); |
105 } | 105 } |
106 case CJS_Value::VT_object: { | 106 case CJS_Value::VT_object: { |
107 v8::Local<v8::Object> pData; | 107 v8::Local<v8::Object> pData; |
108 vp >> pData; | 108 vp >> pData; |
109 return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, | 109 return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, |
110 false, "", pData, FALSE); | 110 false, "", pData, false); |
111 } | 111 } |
112 case CJS_Value::VT_null: { | 112 case CJS_Value::VT_null: { |
113 return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, | 113 return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, |
114 false, "", v8::Local<v8::Object>(), FALSE); | 114 false, "", v8::Local<v8::Object>(), false); |
115 } | 115 } |
116 case CJS_Value::VT_undefined: { | 116 case CJS_Value::VT_undefined: { |
117 DelProperty(cc, propname, sError); | 117 DelProperty(cc, propname, sError); |
118 return TRUE; | 118 return true; |
119 } | 119 } |
120 default: | 120 default: |
121 break; | 121 break; |
122 } | 122 } |
123 } else { | 123 } else { |
124 auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname)); | 124 auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname)); |
125 if (it == m_mapGlobal.end()) { | 125 if (it == m_mapGlobal.end()) { |
126 vp.GetJSValue()->SetNull(pRuntime); | 126 vp.GetJSValue()->SetNull(pRuntime); |
127 return TRUE; | 127 return true; |
128 } | 128 } |
129 JSGlobalData* pData = it->second; | 129 JSGlobalData* pData = it->second; |
130 if (pData->bDeleted) { | 130 if (pData->bDeleted) { |
131 vp.GetJSValue()->SetNull(pRuntime); | 131 vp.GetJSValue()->SetNull(pRuntime); |
132 return TRUE; | 132 return true; |
133 } | 133 } |
134 switch (pData->nType) { | 134 switch (pData->nType) { |
135 case JS_GlobalDataType::NUMBER: | 135 case JS_GlobalDataType::NUMBER: |
136 vp << pData->dData; | 136 vp << pData->dData; |
137 return TRUE; | 137 return true; |
138 case JS_GlobalDataType::BOOLEAN: | 138 case JS_GlobalDataType::BOOLEAN: |
139 vp << pData->bData; | 139 vp << pData->bData; |
140 return TRUE; | 140 return true; |
141 case JS_GlobalDataType::STRING: | 141 case JS_GlobalDataType::STRING: |
142 vp << pData->sData; | 142 vp << pData->sData; |
143 return TRUE; | 143 return true; |
144 case JS_GlobalDataType::OBJECT: { | 144 case JS_GlobalDataType::OBJECT: { |
145 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( | 145 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( |
146 vp.GetJSRuntime()->GetIsolate(), pData->pData); | 146 vp.GetJSRuntime()->GetIsolate(), pData->pData); |
147 vp << obj; | 147 vp << obj; |
148 return TRUE; | 148 return true; |
149 } | 149 } |
150 case JS_GlobalDataType::NULLOBJ: | 150 case JS_GlobalDataType::NULLOBJ: |
151 vp.GetJSValue()->SetNull(pRuntime); | 151 vp.GetJSValue()->SetNull(pRuntime); |
152 return TRUE; | 152 return true; |
153 default: | 153 default: |
154 break; | 154 break; |
155 } | 155 } |
156 } | 156 } |
157 return FALSE; | 157 return false; |
158 } | 158 } |
159 | 159 |
160 FX_BOOL JSGlobalAlternate::setPersistent(IJS_Context* cc, | 160 bool JSGlobalAlternate::setPersistent(IJS_Context* cc, |
161 const std::vector<CJS_Value>& params, | 161 const std::vector<CJS_Value>& params, |
162 CJS_Value& vRet, | 162 CJS_Value& vRet, |
163 CFX_WideString& sError) { | 163 CFX_WideString& sError) { |
164 if (params.size() != 2) { | 164 if (params.size() != 2) { |
165 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); | 165 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
166 return FALSE; | 166 return false; |
167 } | 167 } |
168 | 168 |
169 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 169 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
170 auto it = m_mapGlobal.find(params[0].ToCFXByteString(pRuntime)); | 170 auto it = m_mapGlobal.find(params[0].ToCFXByteString(pRuntime)); |
171 if (it != m_mapGlobal.end()) { | 171 if (it != m_mapGlobal.end()) { |
172 JSGlobalData* pData = it->second; | 172 JSGlobalData* pData = it->second; |
173 if (!pData->bDeleted) { | 173 if (!pData->bDeleted) { |
174 pData->bPersistent = params[1].ToBool(pRuntime); | 174 pData->bPersistent = params[1].ToBool(pRuntime); |
175 return TRUE; | 175 return true; |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 sError = JSGetStringFromID(IDS_STRING_JSNOGLOBAL); | 179 sError = JSGetStringFromID(IDS_STRING_JSNOGLOBAL); |
180 return FALSE; | 180 return false; |
181 } | 181 } |
182 | 182 |
183 void JSGlobalAlternate::UpdateGlobalPersistentVariables() { | 183 void JSGlobalAlternate::UpdateGlobalPersistentVariables() { |
184 CJS_Runtime* pRuntime = | 184 CJS_Runtime* pRuntime = |
185 static_cast<CJS_Runtime*>(CFXJS_Engine::CurrentEngineFromIsolate( | 185 static_cast<CJS_Runtime*>(CFXJS_Engine::CurrentEngineFromIsolate( |
186 m_pJSObject->ToV8Object()->GetIsolate())); | 186 m_pJSObject->ToV8Object()->GetIsolate())); |
187 | 187 |
188 for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { | 188 for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { |
189 CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); | 189 CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); |
190 switch (pData->data.nType) { | 190 switch (pData->data.nType) { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 } | 352 } |
353 } | 353 } |
354 | 354 |
355 void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { | 355 void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { |
356 for (const auto& pair : m_mapGlobal) { | 356 for (const auto& pair : m_mapGlobal) { |
357 delete pair.second; | 357 delete pair.second; |
358 } | 358 } |
359 m_mapGlobal.clear(); | 359 m_mapGlobal.clear(); |
360 } | 360 } |
361 | 361 |
362 FX_BOOL JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, | 362 bool JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, |
363 JS_GlobalDataType nType, | 363 JS_GlobalDataType nType, |
364 double dData, | 364 double dData, |
365 bool bData, | 365 bool bData, |
366 const CFX_ByteString& sData, | 366 const CFX_ByteString& sData, |
367 v8::Local<v8::Object> pData, | 367 v8::Local<v8::Object> pData, |
368 bool bDefaultPersistent) { | 368 bool bDefaultPersistent) { |
369 if (propname.IsEmpty()) | 369 if (propname.IsEmpty()) |
370 return FALSE; | 370 return false; |
371 | 371 |
372 auto it = m_mapGlobal.find(propname); | 372 auto it = m_mapGlobal.find(propname); |
373 if (it != m_mapGlobal.end()) { | 373 if (it != m_mapGlobal.end()) { |
374 JSGlobalData* pTemp = it->second; | 374 JSGlobalData* pTemp = it->second; |
375 if (pTemp->bDeleted || pTemp->nType != nType) { | 375 if (pTemp->bDeleted || pTemp->nType != nType) { |
376 pTemp->dData = 0; | 376 pTemp->dData = 0; |
377 pTemp->bData = 0; | 377 pTemp->bData = 0; |
378 pTemp->sData = ""; | 378 pTemp->sData = ""; |
379 pTemp->nType = nType; | 379 pTemp->nType = nType; |
380 } | 380 } |
381 | 381 |
382 pTemp->bDeleted = FALSE; | 382 pTemp->bDeleted = false; |
383 switch (nType) { | 383 switch (nType) { |
384 case JS_GlobalDataType::NUMBER: { | 384 case JS_GlobalDataType::NUMBER: { |
385 pTemp->dData = dData; | 385 pTemp->dData = dData; |
386 } break; | 386 } break; |
387 case JS_GlobalDataType::BOOLEAN: { | 387 case JS_GlobalDataType::BOOLEAN: { |
388 pTemp->bData = bData; | 388 pTemp->bData = bData; |
389 } break; | 389 } break; |
390 case JS_GlobalDataType::STRING: { | 390 case JS_GlobalDataType::STRING: { |
391 pTemp->sData = sData; | 391 pTemp->sData = sData; |
392 } break; | 392 } break; |
393 case JS_GlobalDataType::OBJECT: { | 393 case JS_GlobalDataType::OBJECT: { |
394 pTemp->pData.Reset(pData->GetIsolate(), pData); | 394 pTemp->pData.Reset(pData->GetIsolate(), pData); |
395 } break; | 395 } break; |
396 case JS_GlobalDataType::NULLOBJ: | 396 case JS_GlobalDataType::NULLOBJ: |
397 break; | 397 break; |
398 default: | 398 default: |
399 return FALSE; | 399 return false; |
400 } | 400 } |
401 return TRUE; | 401 return true; |
402 } | 402 } |
403 | 403 |
404 JSGlobalData* pNewData = nullptr; | 404 JSGlobalData* pNewData = nullptr; |
405 | 405 |
406 switch (nType) { | 406 switch (nType) { |
407 case JS_GlobalDataType::NUMBER: { | 407 case JS_GlobalDataType::NUMBER: { |
408 pNewData = new JSGlobalData; | 408 pNewData = new JSGlobalData; |
409 pNewData->nType = JS_GlobalDataType::NUMBER; | 409 pNewData->nType = JS_GlobalDataType::NUMBER; |
410 pNewData->dData = dData; | 410 pNewData->dData = dData; |
411 pNewData->bPersistent = bDefaultPersistent; | 411 pNewData->bPersistent = bDefaultPersistent; |
(...skipping 15 matching lines...) Expand all Loading... |
427 pNewData->nType = JS_GlobalDataType::OBJECT; | 427 pNewData->nType = JS_GlobalDataType::OBJECT; |
428 pNewData->pData.Reset(pData->GetIsolate(), pData); | 428 pNewData->pData.Reset(pData->GetIsolate(), pData); |
429 pNewData->bPersistent = bDefaultPersistent; | 429 pNewData->bPersistent = bDefaultPersistent; |
430 } break; | 430 } break; |
431 case JS_GlobalDataType::NULLOBJ: { | 431 case JS_GlobalDataType::NULLOBJ: { |
432 pNewData = new JSGlobalData; | 432 pNewData = new JSGlobalData; |
433 pNewData->nType = JS_GlobalDataType::NULLOBJ; | 433 pNewData->nType = JS_GlobalDataType::NULLOBJ; |
434 pNewData->bPersistent = bDefaultPersistent; | 434 pNewData->bPersistent = bDefaultPersistent; |
435 } break; | 435 } break; |
436 default: | 436 default: |
437 return FALSE; | 437 return false; |
438 } | 438 } |
439 | 439 |
440 m_mapGlobal[propname] = pNewData; | 440 m_mapGlobal[propname] = pNewData; |
441 return TRUE; | 441 return true; |
442 } | 442 } |
OLD | NEW |