1 // Copyright 2016 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 "xfa/fxfa/parser/cxfa_object.h"
8 
9 #include <utility>
10 
11 #include "core/fxcrt/fx_extension.h"
12 #include "fxjs/xfa/cfxjse_engine.h"
13 #include "fxjs/xfa/cfxjse_value.h"
14 #include "fxjs/xfa/cjx_object.h"
15 #include "xfa/fxfa/cxfa_ffnotify.h"
16 #include "xfa/fxfa/parser/cxfa_document.h"
17 #include "xfa/fxfa/parser/cxfa_node.h"
18 #include "xfa/fxfa/parser/cxfa_thisproxy.h"
19 #include "xfa/fxfa/parser/cxfa_treelist.h"
20 #include "xfa/fxfa/parser/xfa_basic_data.h"
21 
CXFA_Object(CXFA_Document * pDocument,XFA_ObjectType objectType,XFA_Element elementType,std::unique_ptr<CJX_Object> jsObject)22 CXFA_Object::CXFA_Object(CXFA_Document* pDocument,
23                          XFA_ObjectType objectType,
24                          XFA_Element elementType,
25                          std::unique_ptr<CJX_Object> jsObject)
26     : m_pDocument(pDocument),
27       m_objectType(objectType),
28       m_elementType(elementType),
29       m_elementName(XFA_ElementToName(elementType)),
30       m_elementNameHash(FX_HashCode_GetAsIfW(m_elementName, false)),
31       m_pJSObject(std::move(jsObject)) {}
32 
~CXFA_Object()33 CXFA_Object::~CXFA_Object() {
34   if (!GetDocument()->IsBeingDestroyed() && GetDocument()->HasScriptContext())
35     GetDocument()->GetScriptContext()->RemoveJSBindingFromMap(this);
36 }
37 
AsCXFAObject()38 CXFA_Object* CXFA_Object::AsCXFAObject() {
39   return this;
40 }
41 
GetSOMExpression()42 WideString CXFA_Object::GetSOMExpression() {
43   CXFA_Node* pNode = AsNode();
44   return pNode ? pNode->GetNameExpression() : WideString();
45 }
46 
AsList()47 CXFA_List* CXFA_Object::AsList() {
48   return IsList() ? static_cast<CXFA_List*>(this) : nullptr;
49 }
50 
AsNode()51 CXFA_Node* CXFA_Object::AsNode() {
52   return IsNode() ? static_cast<CXFA_Node*>(this) : nullptr;
53 }
54 
AsTreeList()55 CXFA_TreeList* CXFA_Object::AsTreeList() {
56   return IsTreeList() ? static_cast<CXFA_TreeList*>(this) : nullptr;
57 }
58 
AsThisProxy()59 CXFA_ThisProxy* CXFA_Object::AsThisProxy() {
60   return IsThisProxy() ? static_cast<CXFA_ThisProxy*>(this) : nullptr;
61 }
62 
ToList(CXFA_Object * pObj)63 CXFA_List* ToList(CXFA_Object* pObj) {
64   return pObj ? pObj->AsList() : nullptr;
65 }
66 
ToNode(CXFA_Object * pObj)67 CXFA_Node* ToNode(CXFA_Object* pObj) {
68   return pObj ? pObj->AsNode() : nullptr;
69 }
70 
ToTreeList(CXFA_Object * pObj)71 CXFA_TreeList* ToTreeList(CXFA_Object* pObj) {
72   return pObj ? pObj->AsTreeList() : nullptr;
73 }
74 
ToThisProxy(CXFA_Object * pObj)75 CXFA_ThisProxy* ToThisProxy(CXFA_Object* pObj) {
76   return pObj ? pObj->AsThisProxy() : nullptr;
77 }
78