Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: fpdfsdk/src/javascript/JS_GlobalData.cpp

Issue 1512763013: Get rid of most instance of 'foo != NULL' (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: nits Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/src/javascript/JS_EventHandler.cpp ('k') | fpdfsdk/src/javascript/JS_Object.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "JS_GlobalData.h" 7 #include "JS_GlobalData.h"
8 8
9 #include "core/include/fdrm/fx_crypt.h" 9 #include "core/include/fdrm/fx_crypt.h"
10 #include "fpdfsdk/include/javascript/IJavaScript.h" 10 #include "fpdfsdk/include/javascript/IJavaScript.h"
11 11
12 #define JS_MAXGLOBALDATA (1024 * 4 - 8) 12 #define JS_MAXGLOBALDATA (1024 * 4 - 8)
13 13
14 /* --------------------- CJS_GlobalVariableArray --------------------- */ 14 /* --------------------- CJS_GlobalVariableArray --------------------- */
15 15
16 CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} 16 CJS_GlobalVariableArray::CJS_GlobalVariableArray() {}
17 17
18 CJS_GlobalVariableArray::~CJS_GlobalVariableArray() { 18 CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {
19 Empty(); 19 Empty();
20 } 20 }
21 21
22 void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) { 22 void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) {
23 Empty(); 23 Empty();
24 for (int i = 0, sz = array.Count(); i < sz; i++) { 24 for (int i = 0, sz = array.Count(); i < sz; i++) {
25 CJS_KeyValue* pOldObjData = array.GetAt(i); 25 CJS_KeyValue* pOldObjData = array.GetAt(i);
26 ASSERT(pOldObjData != NULL);
27
28 switch (pOldObjData->nType) { 26 switch (pOldObjData->nType) {
29 case JS_GLOBALDATA_TYPE_NUMBER: { 27 case JS_GLOBALDATA_TYPE_NUMBER: {
30 CJS_KeyValue* pNewObjData = new CJS_KeyValue; 28 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
31 pNewObjData->sKey = pOldObjData->sKey; 29 pNewObjData->sKey = pOldObjData->sKey;
32 pNewObjData->nType = pOldObjData->nType; 30 pNewObjData->nType = pOldObjData->nType;
33 pNewObjData->dData = pOldObjData->dData; 31 pNewObjData->dData = pOldObjData->dData;
34 Add(pNewObjData); 32 Add(pNewObjData);
35 } break; 33 } break;
36 case JS_GLOBALDATA_TYPE_BOOLEAN: { 34 case JS_GLOBALDATA_TYPE_BOOLEAN: {
37 CJS_KeyValue* pNewObjData = new CJS_KeyValue; 35 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) { 132 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
135 CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i); 133 CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i);
136 if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname) 134 if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname)
137 return i; 135 return i;
138 } 136 }
139 return -1; 137 return -1;
140 } 138 }
141 139
142 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( 140 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
143 const FX_CHAR* propname) { 141 const FX_CHAR* propname) {
144 ASSERT(propname != NULL); 142 ASSERT(propname);
145 143
146 int nFind = FindGlobalVariable(propname); 144 int nFind = FindGlobalVariable(propname);
147 if (nFind >= 0) 145 return nFind >= 0 ? m_arrayGlobalData.GetAt(nFind) : nullptr;
148 return m_arrayGlobalData.GetAt(nFind);
149
150 return NULL;
151 } 146 }
152 147
153 void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, 148 void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname,
154 double dData) { 149 double dData) {
155 ASSERT(propname != NULL); 150 ASSERT(propname);
156
157 CFX_ByteString sPropName = propname; 151 CFX_ByteString sPropName = propname;
158 sPropName.TrimLeft(); 152 sPropName.TrimLeft();
159 sPropName.TrimRight(); 153 sPropName.TrimRight();
160 if (sPropName.GetLength() == 0) 154 if (sPropName.GetLength() == 0)
161 return; 155 return;
162 156
163 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 157 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
164 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; 158 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
165 pData->data.dData = dData; 159 pData->data.dData = dData;
166 } else { 160 } else {
167 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element; 161 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
168 pNewData->data.sKey = sPropName; 162 pNewData->data.sKey = sPropName;
169 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; 163 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
170 pNewData->data.dData = dData; 164 pNewData->data.dData = dData;
171 m_arrayGlobalData.Add(pNewData); 165 m_arrayGlobalData.Add(pNewData);
172 } 166 }
173 } 167 }
174 168
175 void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, 169 void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname,
176 bool bData) { 170 bool bData) {
177 ASSERT(propname != NULL); 171 ASSERT(propname);
178 CFX_ByteString sPropName = propname; 172 CFX_ByteString sPropName = propname;
179 173
180 sPropName.TrimLeft(); 174 sPropName.TrimLeft();
181 sPropName.TrimRight(); 175 sPropName.TrimRight();
182 176
183 if (sPropName.GetLength() == 0) 177 if (sPropName.GetLength() == 0)
184 return; 178 return;
185 179
186 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 180 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
187 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; 181 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
188 pData->data.bData = bData; 182 pData->data.bData = bData;
189 } else { 183 } else {
190 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element; 184 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
191 pNewData->data.sKey = sPropName; 185 pNewData->data.sKey = sPropName;
192 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; 186 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
193 pNewData->data.bData = bData; 187 pNewData->data.bData = bData;
194 188
195 m_arrayGlobalData.Add(pNewData); 189 m_arrayGlobalData.Add(pNewData);
196 } 190 }
197 } 191 }
198 192
199 void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, 193 void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname,
200 const CFX_ByteString& sData) { 194 const CFX_ByteString& sData) {
201 ASSERT(propname != NULL); 195 ASSERT(propname);
202 CFX_ByteString sPropName = propname; 196 CFX_ByteString sPropName = propname;
203 197
204 sPropName.TrimLeft(); 198 sPropName.TrimLeft();
205 sPropName.TrimRight(); 199 sPropName.TrimRight();
206 200
207 if (sPropName.GetLength() == 0) 201 if (sPropName.GetLength() == 0)
208 return; 202 return;
209 203
210 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 204 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
211 pData->data.nType = JS_GLOBALDATA_TYPE_STRING; 205 pData->data.nType = JS_GLOBALDATA_TYPE_STRING;
212 pData->data.sData = sData; 206 pData->data.sData = sData;
213 } else { 207 } else {
214 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element; 208 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
215 pNewData->data.sKey = sPropName; 209 pNewData->data.sKey = sPropName;
216 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; 210 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING;
217 pNewData->data.sData = sData; 211 pNewData->data.sData = sData;
218 212
219 m_arrayGlobalData.Add(pNewData); 213 m_arrayGlobalData.Add(pNewData);
220 } 214 }
221 } 215 }
222 216
223 void CJS_GlobalData::SetGlobalVariableObject( 217 void CJS_GlobalData::SetGlobalVariableObject(
224 const FX_CHAR* propname, 218 const FX_CHAR* propname,
225 const CJS_GlobalVariableArray& array) { 219 const CJS_GlobalVariableArray& array) {
226 ASSERT(propname != NULL); 220 ASSERT(propname);
227 CFX_ByteString sPropName = propname; 221 CFX_ByteString sPropName = propname;
228 222
229 sPropName.TrimLeft(); 223 sPropName.TrimLeft();
230 sPropName.TrimRight(); 224 sPropName.TrimRight();
231 225
232 if (sPropName.GetLength() == 0) 226 if (sPropName.GetLength() == 0)
233 return; 227 return;
234 228
235 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 229 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
236 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; 230 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
237 pData->data.objData.Copy(array); 231 pData->data.objData.Copy(array);
238 } else { 232 } else {
239 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element; 233 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
240 pNewData->data.sKey = sPropName; 234 pNewData->data.sKey = sPropName;
241 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; 235 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
242 pNewData->data.objData.Copy(array); 236 pNewData->data.objData.Copy(array);
243 237
244 m_arrayGlobalData.Add(pNewData); 238 m_arrayGlobalData.Add(pNewData);
245 } 239 }
246 } 240 }
247 241
248 void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { 242 void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) {
249 ASSERT(propname != NULL); 243 ASSERT(propname);
250 CFX_ByteString sPropName = propname; 244 CFX_ByteString sPropName = propname;
251 245
252 sPropName.TrimLeft(); 246 sPropName.TrimLeft();
253 sPropName.TrimRight(); 247 sPropName.TrimRight();
254 248
255 if (sPropName.GetLength() == 0) 249 if (sPropName.GetLength() == 0)
256 return; 250 return;
257 251
258 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 252 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
259 pData->data.nType = JS_GLOBALDATA_TYPE_NULL; 253 pData->data.nType = JS_GLOBALDATA_TYPE_NULL;
260 } else { 254 } else {
261 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element; 255 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
262 pNewData->data.sKey = sPropName; 256 pNewData->data.sKey = sPropName;
263 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; 257 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL;
264 258
265 m_arrayGlobalData.Add(pNewData); 259 m_arrayGlobalData.Add(pNewData);
266 } 260 }
267 } 261 }
268 262
269 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, 263 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname,
270 FX_BOOL bPersistent) { 264 FX_BOOL bPersistent) {
271 ASSERT(propname != NULL); 265 ASSERT(propname);
272 CFX_ByteString sPropName = propname; 266 CFX_ByteString sPropName = propname;
273 267
274 sPropName.TrimLeft(); 268 sPropName.TrimLeft();
275 sPropName.TrimRight(); 269 sPropName.TrimRight();
276 270
277 if (sPropName.GetLength() == 0) 271 if (sPropName.GetLength() == 0)
278 return FALSE; 272 return FALSE;
279 273
280 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 274 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
281 pData->bPersistent = bPersistent; 275 pData->bPersistent = bPersistent;
282 return TRUE; 276 return TRUE;
283 } 277 }
284 278
285 return FALSE; 279 return FALSE;
286 } 280 }
287 281
288 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { 282 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) {
289 ASSERT(propname != NULL); 283 ASSERT(propname);
290 CFX_ByteString sPropName = propname; 284 CFX_ByteString sPropName = propname;
291 285
292 sPropName.TrimLeft(); 286 sPropName.TrimLeft();
293 sPropName.TrimRight(); 287 sPropName.TrimRight();
294 288
295 if (sPropName.GetLength() == 0) 289 if (sPropName.GetLength() == 0)
296 return FALSE; 290 return FALSE;
297 291
298 int nFind = FindGlobalVariable(sPropName); 292 int nFind = FindGlobalVariable(sPropName);
299 293
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 FX_Free(pBuffer); 396 FX_Free(pBuffer);
403 } 397 }
404 } 398 }
405 399
406 void CJS_GlobalData::SaveGlobalPersisitentVariables() { 400 void CJS_GlobalData::SaveGlobalPersisitentVariables() {
407 FX_DWORD nCount = 0; 401 FX_DWORD nCount = 0;
408 CFX_BinaryBuf sData; 402 CFX_BinaryBuf sData;
409 403
410 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) { 404 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
411 CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i); 405 CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i);
412 ASSERT(pElement != NULL);
413
414 if (pElement->bPersistent) { 406 if (pElement->bPersistent) {
415 CFX_BinaryBuf sElement; 407 CFX_BinaryBuf sElement;
416 MakeByteString(pElement->data.sKey, &pElement->data, sElement); 408 MakeByteString(pElement->data.sKey, &pElement->data, sElement);
417 409
418 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) 410 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
419 break; 411 break;
420 412
421 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); 413 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
422 nCount++; 414 nCount++;
423 } 415 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 case JS_GLOBALDATA_TYPE_NULL: { 481 case JS_GLOBALDATA_TYPE_NULL: {
490 FX_DWORD dwNameLen = (FX_DWORD)name.GetLength(); 482 FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();
491 sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD)); 483 sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));
492 sData.AppendString(name); 484 sData.AppendString(name);
493 sData.AppendBlock(&wType, sizeof(FX_DWORD)); 485 sData.AppendBlock(&wType, sizeof(FX_DWORD));
494 } break; 486 } break;
495 default: 487 default:
496 break; 488 break;
497 } 489 }
498 } 490 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/JS_EventHandler.cpp ('k') | fpdfsdk/src/javascript/JS_Object.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698