1// Copyright 2014 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/src/foxitlib.h"
8#include "xfa/src/fxfa/src/common/xfa_utils.h"
9#include "xfa/src/fxfa/src/common/xfa_object.h"
10#include "xfa/src/fxfa/src/common/xfa_document.h"
11#include "xfa/src/fxfa/src/common/xfa_parser.h"
12#include "xfa/src/fxfa/src/common/xfa_script.h"
13#include "xfa/src/fxfa/src/common/xfa_docdata.h"
14#include "xfa/src/fxfa/src/common/xfa_doclayout.h"
15#include "xfa/src/fxfa/src/common/xfa_localemgr.h"
16#include "xfa/src/fxfa/src/common/xfa_fm2jsapi.h"
17#include "xfa_script_signaturepseudomodel.h"
18CScript_SignaturePseudoModel::CScript_SignaturePseudoModel(
19    CXFA_Document* pDocument)
20    : CXFA_OrdinaryObject(pDocument, XFA_ELEMENT_SignaturePseudoModel) {
21  m_uScriptHash = XFA_HASHCODE_Signature;
22}
23CScript_SignaturePseudoModel::~CScript_SignaturePseudoModel() {}
24void CScript_SignaturePseudoModel::Script_SignaturePseudoModel_Verify(
25    CFXJSE_Arguments* pArguments) {
26  int32_t iLength = pArguments->GetLength();
27  if (iLength < 1 || iLength > 4) {
28    ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"verify");
29    return;
30  }
31  IXFA_Notify* pNotify = m_pDocument->GetParser()->GetNotify();
32  if (!pNotify) {
33    return;
34  }
35  IXFA_Doc* hDoc = pNotify->GetHDOC();
36  CXFA_Node* pNode = NULL;
37  if (iLength >= 1) {
38    pNode = (CXFA_Node*)pArguments->GetObject(0);
39  }
40  int32_t bVerify = pNotify->GetDocProvider()->Verify(hDoc, pNode);
41  FXJSE_HVALUE hValue = pArguments->GetReturnValue();
42  if (hValue) {
43    FXJSE_Value_SetInteger(hValue, bVerify);
44  }
45}
46void CScript_SignaturePseudoModel::Script_SignaturePseudoModel_Sign(
47    CFXJSE_Arguments* pArguments) {
48  int32_t iLength = pArguments->GetLength();
49  if (iLength < 3 || iLength > 7) {
50    ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"sign");
51    return;
52  }
53  IXFA_Notify* pNotify = m_pDocument->GetParser()->GetNotify();
54  if (!pNotify) {
55    return;
56  }
57  IXFA_Doc* hDoc = pNotify->GetHDOC();
58  CXFA_NodeList* pNodeList = NULL;
59  CFX_WideString wsExpression;
60  CFX_WideString wsXMLIdent;
61  if (iLength >= 1) {
62    pNodeList = (CXFA_NodeList*)pArguments->GetObject(0);
63  }
64  if (iLength >= 2) {
65    CFX_ByteString bsExpression = pArguments->GetUTF8String(1);
66    wsExpression =
67        CFX_WideString::FromUTF8(bsExpression, bsExpression.GetLength());
68  }
69  if (iLength >= 3) {
70    CFX_ByteString bsXMLIdent = pArguments->GetUTF8String(2);
71    wsXMLIdent = CFX_WideString::FromUTF8(bsXMLIdent, bsXMLIdent.GetLength());
72  }
73  FX_BOOL bSign = pNotify->GetDocProvider()->Sign(hDoc, pNodeList, wsExpression,
74                                                  wsXMLIdent);
75  FXJSE_HVALUE hValue = pArguments->GetReturnValue();
76  if (hValue) {
77    FXJSE_Value_SetBoolean(hValue, bSign);
78  }
79}
80void CScript_SignaturePseudoModel::Script_SignaturePseudoModel_Enumerate(
81    CFXJSE_Arguments* pArguments) {
82  int32_t iLength = pArguments->GetLength();
83  if (iLength != 0) {
84    ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"enumerate");
85    return;
86  }
87  IXFA_Notify* pNotify = m_pDocument->GetParser()->GetNotify();
88  if (!pNotify) {
89    return;
90  }
91  IXFA_Doc* hDoc = pNotify->GetHDOC();
92  CXFA_NodeList* pList = pNotify->GetDocProvider()->Enumerate(hDoc);
93  FXJSE_Value_Set(pArguments->GetReturnValue(),
94                  m_pDocument->GetScriptContext()->GetJSValueFromMap(pList));
95}
96void CScript_SignaturePseudoModel::Script_SignaturePseudoModel_Clear(
97    CFXJSE_Arguments* pArguments) {
98  int32_t iLength = pArguments->GetLength();
99  if (iLength < 1 || iLength > 2) {
100    ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"clear");
101    return;
102  }
103  IXFA_Notify* pNotify = m_pDocument->GetParser()->GetNotify();
104  if (!pNotify) {
105    return;
106  }
107  IXFA_Doc* hDoc = pNotify->GetHDOC();
108  CXFA_Node* pNode = NULL;
109  FX_BOOL bClear = TRUE;
110  if (iLength >= 1) {
111    pNode = (CXFA_Node*)pArguments->GetObject(0);
112  }
113  if (iLength >= 2) {
114    bClear = pArguments->GetInt32(1) == 0 ? FALSE : TRUE;
115  }
116  FX_BOOL bFlag = pNotify->GetDocProvider()->Clear(hDoc, pNode, bClear);
117  FXJSE_HVALUE hValue = pArguments->GetReturnValue();
118  if (hValue) {
119    FXJSE_Value_SetBoolean(hValue, bFlag);
120  }
121}
122