1 // Copyright 2017 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/xfa/cjx_list.h"
8 
9 #include <vector>
10 
11 #include "fxjs/js_resources.h"
12 #include "fxjs/xfa/cfxjse_class.h"
13 #include "fxjs/xfa/cfxjse_engine.h"
14 #include "fxjs/xfa/cfxjse_value.h"
15 #include "third_party/base/numerics/safe_conversions.h"
16 #include "xfa/fxfa/parser/cxfa_document.h"
17 #include "xfa/fxfa/parser/cxfa_list.h"
18 #include "xfa/fxfa/parser/cxfa_node.h"
19 
20 const CJX_MethodSpec CJX_List::MethodSpecs[] = {{"append", append_static},
21                                                 {"insert", insert_static},
22                                                 {"item", item_static},
23                                                 {"remove", remove_static}};
24 
CJX_List(CXFA_List * list)25 CJX_List::CJX_List(CXFA_List* list) : CJX_Object(list) {
26   DefineMethods(MethodSpecs);
27 }
28 
~CJX_List()29 CJX_List::~CJX_List() {}
30 
DynamicTypeIs(TypeTag eType) const31 bool CJX_List::DynamicTypeIs(TypeTag eType) const {
32   return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
33 }
34 
GetXFAList()35 CXFA_List* CJX_List::GetXFAList() {
36   return ToList(GetXFAObject());
37 }
38 
append(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)39 CJS_Result CJX_List::append(CFX_V8* runtime,
40                             const std::vector<v8::Local<v8::Value>>& params) {
41   if (params.size() != 1)
42     return CJS_Result::Failure(JSMessage::kParamError);
43 
44   auto* pNode =
45       ToNode(static_cast<CFXJSE_Engine*>(runtime)->ToXFAObject(params[0]));
46   if (!pNode)
47     return CJS_Result::Failure(JSMessage::kValueError);
48 
49   GetXFAList()->Append(pNode);
50   return CJS_Result::Success();
51 }
52 
insert(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)53 CJS_Result CJX_List::insert(CFX_V8* runtime,
54                             const std::vector<v8::Local<v8::Value>>& params) {
55   if (params.size() != 2)
56     return CJS_Result::Failure(JSMessage::kParamError);
57 
58   auto* pNewNode =
59       ToNode(static_cast<CFXJSE_Engine*>(runtime)->ToXFAObject(params[0]));
60   if (!pNewNode)
61     return CJS_Result::Failure(JSMessage::kValueError);
62 
63   auto* pBeforeNode =
64       ToNode(static_cast<CFXJSE_Engine*>(runtime)->ToXFAObject(params[1]));
65   if (!GetXFAList()->Insert(pNewNode, pBeforeNode))
66     return CJS_Result::Failure(JSMessage::kValueError);
67 
68   return CJS_Result::Success();
69 }
70 
remove(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)71 CJS_Result CJX_List::remove(CFX_V8* runtime,
72                             const std::vector<v8::Local<v8::Value>>& params) {
73   if (params.size() != 1)
74     return CJS_Result::Failure(JSMessage::kParamError);
75 
76   auto* pNode =
77       ToNode(static_cast<CFXJSE_Engine*>(runtime)->ToXFAObject(params[0]));
78   if (!pNode)
79     return CJS_Result::Failure(JSMessage::kValueError);
80 
81   GetXFAList()->Remove(pNode);
82   return CJS_Result::Success();
83 }
84 
item(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)85 CJS_Result CJX_List::item(CFX_V8* runtime,
86                           const std::vector<v8::Local<v8::Value>>& params) {
87   if (params.size() != 1)
88     return CJS_Result::Failure(JSMessage::kParamError);
89 
90   int32_t index = runtime->ToInt32(params[0]);
91   size_t cast_index = static_cast<size_t>(index);
92   if (index < 0 || cast_index >= GetXFAList()->GetLength())
93     return CJS_Result::Failure(JSMessage::kInvalidInputError);
94 
95   return CJS_Result::Success(static_cast<CFXJSE_Engine*>(runtime)->NewXFAObject(
96       GetXFAList()->Item(cast_index),
97       GetDocument()->GetScriptContext()->GetJseNormalClass()->GetTemplate()));
98 }
99 
length(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)100 void CJX_List::length(CFXJSE_Value* pValue,
101                       bool bSetting,
102                       XFA_Attribute eAttribute) {
103   if (bSetting) {
104     ThrowInvalidPropertyException();
105     return;
106   }
107   pValue->SetInteger(
108       pdfium::base::checked_cast<int32_t>(GetXFAList()->GetLength()));
109 }
110