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 "../../include/javascript/IJavaScript.h" | 7 #include "../../include/javascript/IJavaScript.h" |
8 #include "../../include/javascript/JS_Context.h" | 8 #include "../../include/javascript/JS_Context.h" |
9 #include "../../include/javascript/JS_Define.h" | 9 #include "../../include/javascript/JS_Define.h" |
10 #include "../../include/javascript/JS_EventHandler.h" | 10 #include "../../include/javascript/JS_EventHandler.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 BEGIN_JS_STATIC_PROP(CJS_Global) | 90 BEGIN_JS_STATIC_PROP(CJS_Global) |
91 END_JS_STATIC_PROP() | 91 END_JS_STATIC_PROP() |
92 | 92 |
93 BEGIN_JS_STATIC_METHOD(CJS_Global) | 93 BEGIN_JS_STATIC_METHOD(CJS_Global) |
94 JS_STATIC_METHOD_ENTRY(setPersistent) | 94 JS_STATIC_METHOD_ENTRY(setPersistent) |
95 END_JS_STATIC_METHOD() | 95 END_JS_STATIC_METHOD() |
96 | 96 |
97 IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, global_alternate, global); | 97 IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, global_alternate, global); |
98 | 98 |
99 FX_BOOL CJS_Global::InitInstance(IFXJS_Context* cc) | 99 bool CJS_Global::InitInstance(IFXJS_Context* cc) |
100 { | 100 { |
101 CJS_Context* pContext = (CJS_Context*)cc; | 101 CJS_Context* pContext = (CJS_Context*)cc; |
102 ASSERT(pContext != NULL); | 102 ASSERT(pContext != NULL); |
103 | 103 |
104 global_alternate* pGlobal = (global_alternate*)GetEmbedObject(); | 104 global_alternate* pGlobal = (global_alternate*)GetEmbedObject(); |
105 ASSERT(pGlobal != NULL); | 105 ASSERT(pGlobal != NULL); |
106 | 106 |
107 pGlobal->Initial(pContext->GetReaderApp()); | 107 pGlobal->Initial(pContext->GetReaderApp()); |
108 | 108 |
109 return TRUE; | 109 return true; |
110 }; | 110 }; |
111 | 111 |
112 global_alternate::global_alternate(CJS_Object* pJSObject) | 112 global_alternate::global_alternate(CJS_Object* pJSObject) |
113 : CJS_EmbedObj(pJSObject), | 113 : CJS_EmbedObj(pJSObject), |
114 m_pApp(NULL) | 114 m_pApp(NULL) |
115 { | 115 { |
116 } | 116 } |
117 | 117 |
118 global_alternate::~global_alternate(void) | 118 global_alternate::~global_alternate(void) |
119 { | 119 { |
120 DestroyGlobalPersisitentVariables(); | 120 DestroyGlobalPersisitentVariables(); |
121 m_pApp->GetRuntimeFactory()->ReleaseGlobalData(); | 121 m_pApp->GetRuntimeFactory()->ReleaseGlobalData(); |
122 } | 122 } |
123 | 123 |
124 void global_alternate::Initial(CPDFDoc_Environment* pApp) | 124 void global_alternate::Initial(CPDFDoc_Environment* pApp) |
125 { | 125 { |
126 m_pApp = pApp; | 126 m_pApp = pApp; |
127 m_pGlobalData = pApp->GetRuntimeFactory()->NewGlobalData(pApp); | 127 m_pGlobalData = pApp->GetRuntimeFactory()->NewGlobalData(pApp); |
128 UpdateGlobalPersistentVariables(); | 128 UpdateGlobalPersistentVariables(); |
129 } | 129 } |
130 | 130 |
131 FX_BOOL global_alternate::QueryProperty(const FX_WCHAR* propname) | 131 bool global_alternate::QueryProperty(const FX_WCHAR* propname) |
132 { | 132 { |
133 return CFX_WideString(propname) != L"setPersistent"; | 133 return CFX_WideString(propname) != L"setPersistent"; |
134 } | 134 } |
135 | 135 |
136 FX_BOOL global_alternate::DelProperty(IFXJS_Context* cc, const FX_WCHAR* propnam
e, CFX_WideString& sError) | 136 bool global_alternate::DelProperty(IFXJS_Context* cc, const FX_WCHAR* propname,
CFX_WideString& sError) |
137 { | 137 { |
138 js_global_data* pData = NULL; | 138 js_global_data* pData = NULL; |
139 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); | 139 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); |
140 | 140 |
141 if (m_mapGlobal.Lookup(sPropName, (void*&)pData)) | 141 if (m_mapGlobal.Lookup(sPropName, (void*&)pData)) |
142 { | 142 { |
143 pData->bDeleted = TRUE; | 143 pData->bDeleted = true; |
144 return TRUE; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 return FALSE; | 147 return false; |
148 } | 148 } |
149 | 149 |
150 FX_BOOL global_alternate::DoProperty(IFXJS_Context* cc, const FX_WCHAR* propname
, CJS_PropValue& vp, CFX_WideString& sError) | 150 bool global_alternate::DoProperty(IFXJS_Context* cc, const FX_WCHAR* propname, C
JS_PropValue& vp, CFX_WideString& sError) |
151 { | 151 { |
152 if (vp.IsSetting()) | 152 if (vp.IsSetting()) |
153 { | 153 { |
154 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); | 154 CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname); |
155 switch (vp.GetType()) | 155 switch (vp.GetType()) |
156 { | 156 { |
157 case VT_number: | 157 case VT_number: |
158 { | 158 { |
159 double dData; | 159 double dData; |
160 vp >> dData; | 160 vp >> dData; |
161 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NUMBER,
dData, false, "", v8::Local<v8::Object>(), FALSE); | 161 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NUMBER,
dData, false, "", v8::Local<v8::Object>(), false); |
162 } | 162 } |
163 case VT_boolean: | 163 case VT_boolean: |
164 { | 164 { |
165 bool bData; | 165 bool bData; |
166 vp >> bData; | 166 vp >> bData; |
167 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_BOOLEAN,
0, bData, "", v8::Local<v8::Object>(), FALSE); | 167 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_BOOLEAN,
0, bData, "", v8::Local<v8::Object>(), false); |
168 } | 168 } |
169 case VT_string: | 169 case VT_string: |
170 { | 170 { |
171 CFX_ByteString sData; | 171 CFX_ByteString sData; |
172 vp >> sData; | 172 vp >> sData; |
173 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_STRING,
0, false, sData, v8::Local<v8::Object>(), FALSE); | 173 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_STRING,
0, false, sData, v8::Local<v8::Object>(), false); |
174 } | 174 } |
175 case VT_object: | 175 case VT_object: |
176 { | 176 { |
177 JSObject pData; | 177 JSObject pData; |
178 vp >> pData; | 178 vp >> pData; |
179 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_OBJECT,
0, false, "", pData, FALSE); | 179 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_OBJECT,
0, false, "", pData, false); |
180 } | 180 } |
181 case VT_null: | 181 case VT_null: |
182 { | 182 { |
183 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NULL, 0,
false, "", v8::Local<v8::Object>(), FALSE); | 183 return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NULL, 0,
false, "", v8::Local<v8::Object>(), false); |
184 } | 184 } |
185 case VT_undefined: | 185 case VT_undefined: |
186 { | 186 { |
187 DelProperty(cc, propname, sError); | 187 DelProperty(cc, propname, sError); |
188 return TRUE; | 188 return true; |
189 } | 189 } |
190 default: | 190 default: |
191 break; | 191 break; |
192 } | 192 } |
193 } | 193 } |
194 else | 194 else |
195 { | 195 { |
196 void* pVoid = nullptr; | 196 void* pVoid = nullptr; |
197 if (!m_mapGlobal.Lookup(CFX_ByteString::FromUnicode(propname), pVoid)) | 197 if (!m_mapGlobal.Lookup(CFX_ByteString::FromUnicode(propname), pVoid)) |
198 { | 198 { |
199 vp.SetNull(); | 199 vp.SetNull(); |
200 return TRUE; | 200 return true; |
201 } | 201 } |
202 if (!pVoid) | 202 if (!pVoid) |
203 { | 203 { |
204 vp.SetNull(); | 204 vp.SetNull(); |
205 return TRUE; | 205 return true; |
206 } | 206 } |
207 js_global_data* pData = (js_global_data*)pVoid; | 207 js_global_data* pData = (js_global_data*)pVoid; |
208 if (pData->bDeleted) | 208 if (pData->bDeleted) |
209 return TRUE; | 209 return true; |
210 | 210 |
211 switch (pData->nType) | 211 switch (pData->nType) |
212 { | 212 { |
213 case JS_GLOBALDATA_TYPE_NUMBER: | 213 case JS_GLOBALDATA_TYPE_NUMBER: |
214 vp << pData->dData; | 214 vp << pData->dData; |
215 return TRUE; | 215 return true; |
216 case JS_GLOBALDATA_TYPE_BOOLEAN: | 216 case JS_GLOBALDATA_TYPE_BOOLEAN: |
217 vp << pData->bData; | 217 vp << pData->bData; |
218 return TRUE; | 218 return true; |
219 case JS_GLOBALDATA_TYPE_STRING: | 219 case JS_GLOBALDATA_TYPE_STRING: |
220 vp << pData->sData; | 220 vp << pData->sData; |
221 return TRUE; | 221 return true; |
222 case JS_GLOBALDATA_TYPE_OBJECT: | 222 case JS_GLOBALDATA_TYPE_OBJECT: |
223 { | 223 { |
224 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(vp.Ge
tIsolate(),pData->pData); | 224 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(vp.Ge
tIsolate(),pData->pData); |
225 vp << obj; | 225 vp << obj; |
226 return TRUE; | 226 return true; |
227 } | 227 } |
228 case JS_GLOBALDATA_TYPE_NULL: | 228 case JS_GLOBALDATA_TYPE_NULL: |
229 vp.SetNull(); | 229 vp.SetNull(); |
230 return TRUE; | 230 return true; |
231 default: | 231 default: |
232 break; | 232 break; |
233 } | 233 } |
234 } | 234 } |
235 return FALSE; | 235 return false; |
236 } | 236 } |
237 | 237 |
238 FX_BOOL global_alternate::setPersistent(IFXJS_Context* cc, const CJS_Parameters&
params, CJS_Value& vRet, CFX_WideString& sError) | 238 bool global_alternate::setPersistent(IFXJS_Context* cc, const CJS_Parameters& pa
rams, CJS_Value& vRet, CFX_WideString& sError) |
239 { | 239 { |
240 CJS_Context* pContext = static_cast<CJS_Context*>(cc); | 240 CJS_Context* pContext = static_cast<CJS_Context*>(cc); |
241 if (params.size() != 2) | 241 if (params.size() != 2) |
242 { | 242 { |
243 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 243 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
244 return FALSE; | 244 return false; |
245 } | 245 } |
246 | 246 |
247 CFX_ByteString sName = params[0].ToCFXByteString(); | 247 CFX_ByteString sName = params[0].ToCFXByteString(); |
248 | 248 |
249 js_global_data* pData = NULL; | 249 js_global_data* pData = NULL; |
250 if (m_mapGlobal.Lookup(sName, (void*&)pData)) | 250 if (m_mapGlobal.Lookup(sName, (void*&)pData)) |
251 { | 251 { |
252 if (pData && !pData->bDeleted) | 252 if (pData && !pData->bDeleted) |
253 { | 253 { |
254 pData->bPersistent = params[1].ToBool(); | 254 pData->bPersistent = params[1].ToBool(); |
255 return TRUE; | 255 return true; |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 sError = JSGetStringFromID(pContext, IDS_STRING_JSNOGLOBAL); | 259 sError = JSGetStringFromID(pContext, IDS_STRING_JSNOGLOBAL); |
260 return FALSE; | 260 return false; |
261 } | 261 } |
262 | 262 |
263 void global_alternate::UpdateGlobalPersistentVariables() | 263 void global_alternate::UpdateGlobalPersistentVariables() |
264 { | 264 { |
265 ASSERT(m_pGlobalData != NULL); | 265 ASSERT(m_pGlobalData != NULL); |
266 | 266 |
267 for (int i=0,sz=m_pGlobalData->GetSize(); i<sz; i++) | 267 for (int i=0,sz=m_pGlobalData->GetSize(); i<sz; i++) |
268 { | 268 { |
269 CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); | 269 CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); |
270 ASSERT(pData != NULL); | 270 ASSERT(pData != NULL); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 CFX_ByteString name; | 473 CFX_ByteString name; |
474 js_global_data* pData = NULL; | 474 js_global_data* pData = NULL; |
475 m_mapGlobal.GetNextAssoc(pos, name, (void*&)pData); | 475 m_mapGlobal.GetNextAssoc(pos, name, (void*&)pData); |
476 delete pData; | 476 delete pData; |
477 } | 477 } |
478 | 478 |
479 m_mapGlobal.RemoveAll(); | 479 m_mapGlobal.RemoveAll(); |
480 } | 480 } |
481 | 481 |
482 | 482 |
483 FX_BOOL global_alternate::SetGlobalVariables(const FX_CHAR* propname, int nType, | 483 bool global_alternate::SetGlobalVariables(const FX_CHAR* propname, int nType, |
484 double dData, bool bData, const CFX_ByteString& sData, JSObject
pData, bool bDefaultPersistent) | 484 double dData, bool bData, const CFX_ByteString& sData, JSObject
pData, bool bDefaultPersistent) |
485 { | 485 { |
486 if (propname == NULL) return FALSE; | 486 if (propname == NULL) return false; |
487 | 487 |
488 js_global_data* pTemp = NULL; | 488 js_global_data* pTemp = NULL; |
489 m_mapGlobal.Lookup(propname, (void*&)pTemp); | 489 m_mapGlobal.Lookup(propname, (void*&)pTemp); |
490 | 490 |
491 if (pTemp) | 491 if (pTemp) |
492 { | 492 { |
493 if (pTemp->bDeleted || pTemp->nType != nType) | 493 if (pTemp->bDeleted || pTemp->nType != nType) |
494 { | 494 { |
495 pTemp->dData = 0; | 495 pTemp->dData = 0; |
496 pTemp->bData = 0; | 496 pTemp->bData = 0; |
497 pTemp->sData = ""; | 497 pTemp->sData = ""; |
498 pTemp->nType = nType; | 498 pTemp->nType = nType; |
499 } | 499 } |
500 | 500 |
501 pTemp->bDeleted = FALSE; | 501 pTemp->bDeleted = false; |
502 | 502 |
503 switch (nType) | 503 switch (nType) |
504 { | 504 { |
505 case JS_GLOBALDATA_TYPE_NUMBER: | 505 case JS_GLOBALDATA_TYPE_NUMBER: |
506 { | 506 { |
507 pTemp->dData = dData; | 507 pTemp->dData = dData; |
508 } | 508 } |
509 break; | 509 break; |
510 case JS_GLOBALDATA_TYPE_BOOLEAN: | 510 case JS_GLOBALDATA_TYPE_BOOLEAN: |
511 { | 511 { |
512 pTemp->bData = bData; | 512 pTemp->bData = bData; |
513 } | 513 } |
514 break; | 514 break; |
515 case JS_GLOBALDATA_TYPE_STRING: | 515 case JS_GLOBALDATA_TYPE_STRING: |
516 { | 516 { |
517 pTemp->sData = sData; | 517 pTemp->sData = sData; |
518 } | 518 } |
519 break; | 519 break; |
520 case JS_GLOBALDATA_TYPE_OBJECT: | 520 case JS_GLOBALDATA_TYPE_OBJECT: |
521 { | 521 { |
522 pTemp->pData.Reset(JS_GetRuntime(pData), pData); | 522 pTemp->pData.Reset(JS_GetRuntime(pData), pData); |
523 } | 523 } |
524 break; | 524 break; |
525 case JS_GLOBALDATA_TYPE_NULL: | 525 case JS_GLOBALDATA_TYPE_NULL: |
526 break; | 526 break; |
527 default: | 527 default: |
528 return FALSE; | 528 return false; |
529 } | 529 } |
530 | 530 |
531 return TRUE; | 531 return true; |
532 } | 532 } |
533 | 533 |
534 js_global_data* pNewData = NULL; | 534 js_global_data* pNewData = NULL; |
535 | 535 |
536 switch (nType) | 536 switch (nType) |
537 { | 537 { |
538 case JS_GLOBALDATA_TYPE_NUMBER: | 538 case JS_GLOBALDATA_TYPE_NUMBER: |
539 { | 539 { |
540 pNewData = new js_global_data; | 540 pNewData = new js_global_data; |
541 pNewData->nType = JS_GLOBALDATA_TYPE_NUMBER; | 541 pNewData->nType = JS_GLOBALDATA_TYPE_NUMBER; |
(...skipping 26 matching lines...) Expand all Loading... |
568 } | 568 } |
569 break; | 569 break; |
570 case JS_GLOBALDATA_TYPE_NULL: | 570 case JS_GLOBALDATA_TYPE_NULL: |
571 { | 571 { |
572 pNewData = new js_global_data; | 572 pNewData = new js_global_data; |
573 pNewData->nType = JS_GLOBALDATA_TYPE_NULL; | 573 pNewData->nType = JS_GLOBALDATA_TYPE_NULL; |
574 pNewData->bPersistent = bDefaultPersistent; | 574 pNewData->bPersistent = bDefaultPersistent; |
575 } | 575 } |
576 break; | 576 break; |
577 default: | 577 default: |
578 return FALSE; | 578 return false; |
579 } | 579 } |
580 | 580 |
581 m_mapGlobal.SetAt(propname, (void*)pNewData); | 581 m_mapGlobal.SetAt(propname, (void*)pNewData); |
582 | 582 |
583 return TRUE; | 583 return true; |
584 } | 584 } |
585 | 585 |
586 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p) | 586 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p) |
587 { | 587 { |
588 const unsigned int nHash = JS_CalcHash(JS_GetTypeof(p)); | 588 const unsigned int nHash = JS_CalcHash(JS_GetTypeof(p)); |
589 | 589 |
590 if (nHash == JSCONST_nUndefHash) | 590 if (nHash == JSCONST_nUndefHash) |
591 return VT_undefined; | 591 return VT_undefined; |
592 if (nHash == JSCONST_nNullHash) | 592 if (nHash == JSCONST_nNullHash) |
593 return VT_null; | 593 return VT_null; |
594 if (nHash == JSCONST_nStringHash) | 594 if (nHash == JSCONST_nStringHash) |
595 return VT_string; | 595 return VT_string; |
596 if (nHash == JSCONST_nNumberHash) | 596 if (nHash == JSCONST_nNumberHash) |
597 return VT_number; | 597 return VT_number; |
598 if (nHash == JSCONST_nBoolHash) | 598 if (nHash == JSCONST_nBoolHash) |
599 return VT_boolean; | 599 return VT_boolean; |
600 if (nHash == JSCONST_nDateHash) | 600 if (nHash == JSCONST_nDateHash) |
601 return VT_date; | 601 return VT_date; |
602 if (nHash == JSCONST_nObjectHash) | 602 if (nHash == JSCONST_nObjectHash) |
603 return VT_object; | 603 return VT_object; |
604 if (nHash == JSCONST_nFXobjHash) | 604 if (nHash == JSCONST_nFXobjHash) |
605 return VT_fxobject; | 605 return VT_fxobject; |
606 | 606 |
607 return VT_unknown; | 607 return VT_unknown; |
608 } | 608 } |
OLD | NEW |