blob: 2a23110ad33b09516671362d2a4897936aebf28f [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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
dsinclairc1515ef2016-07-20 06:16:06 -07007#include "xfa/fxfa/parser/cscript_signaturepseudomodel.h"
Dan Sinclair1770c022016-03-14 14:14:16 -04008
dsinclair08fea802016-07-12 10:37:52 -07009#include "fxjs/include/cfxjse_arguments.h"
dsinclairdf4bc592016-03-31 20:34:43 -070010#include "xfa/fxfa/app/xfa_ffnotify.h"
dsinclair31f87402016-07-20 06:34:45 -070011#include "xfa/fxfa/parser/cxfa_scriptcontext.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040012#include "xfa/fxfa/parser/xfa_doclayout.h"
13#include "xfa/fxfa/parser/xfa_document.h"
14#include "xfa/fxfa/parser/xfa_localemgr.h"
15#include "xfa/fxfa/parser/xfa_object.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040016#include "xfa/fxfa/parser/xfa_utils.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040017
18CScript_SignaturePseudoModel::CScript_SignaturePseudoModel(
19 CXFA_Document* pDocument)
dsinclair937a1132016-06-23 07:52:01 -070020 : CXFA_Object(pDocument,
21 XFA_ObjectType::Object,
dsinclairc1df5d42016-07-18 06:36:51 -070022 XFA_Element::SignaturePseudoModel,
23 CFX_WideStringC(L"signaturePseudoModel")) {}
24
Dan Sinclair1770c022016-03-14 14:14:16 -040025CScript_SignaturePseudoModel::~CScript_SignaturePseudoModel() {}
dsinclairc1df5d42016-07-18 06:36:51 -070026
dsinclaird1cf2392016-07-11 06:46:59 -070027void CScript_SignaturePseudoModel::Verify(CFXJSE_Arguments* pArguments) {
Dan Sinclair1770c022016-03-14 14:14:16 -040028 int32_t iLength = pArguments->GetLength();
29 if (iLength < 1 || iLength > 4) {
dsinclair2235b7b2016-06-02 07:42:25 -070030 ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"verify");
Dan Sinclair1770c022016-03-14 14:14:16 -040031 return;
32 }
dsinclaira1b07722016-07-11 08:20:58 -070033 CXFA_FFNotify* pNotify = m_pDocument->GetNotify();
Dan Sinclair1770c022016-03-14 14:14:16 -040034 if (!pNotify) {
35 return;
36 }
dsinclairdf4bc592016-03-31 20:34:43 -070037 CXFA_FFDoc* hDoc = pNotify->GetHDOC();
dsinclair85d1f2c2016-06-23 12:40:16 -070038 CXFA_Node* pNode = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040039 if (iLength >= 1) {
40 pNode = static_cast<CXFA_Node*>(pArguments->GetObject(0));
41 }
42 int32_t bVerify = pNotify->GetDocProvider()->Verify(hDoc, pNode);
dsinclair12a6b0c2016-05-26 11:14:08 -070043 CFXJSE_Value* pValue = pArguments->GetReturnValue();
dsinclairf27aeec2016-06-07 19:36:18 -070044 if (pValue)
45 pValue->SetInteger(bVerify);
Dan Sinclair1770c022016-03-14 14:14:16 -040046}
dsinclairc1df5d42016-07-18 06:36:51 -070047
dsinclaird1cf2392016-07-11 06:46:59 -070048void CScript_SignaturePseudoModel::Sign(CFXJSE_Arguments* pArguments) {
Dan Sinclair1770c022016-03-14 14:14:16 -040049 int32_t iLength = pArguments->GetLength();
50 if (iLength < 3 || iLength > 7) {
dsinclair2235b7b2016-06-02 07:42:25 -070051 ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"sign");
Dan Sinclair1770c022016-03-14 14:14:16 -040052 return;
53 }
dsinclaira1b07722016-07-11 08:20:58 -070054 CXFA_FFNotify* pNotify = m_pDocument->GetNotify();
Dan Sinclair1770c022016-03-14 14:14:16 -040055 if (!pNotify) {
56 return;
57 }
dsinclairdf4bc592016-03-31 20:34:43 -070058 CXFA_FFDoc* hDoc = pNotify->GetHDOC();
dsinclair85d1f2c2016-06-23 12:40:16 -070059 CXFA_NodeList* pNodeList = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040060 CFX_WideString wsExpression;
61 CFX_WideString wsXMLIdent;
62 if (iLength >= 1) {
63 pNodeList = (CXFA_NodeList*)pArguments->GetObject(0);
64 }
65 if (iLength >= 2) {
66 CFX_ByteString bsExpression = pArguments->GetUTF8String(1);
tsepez4c3debb2016-04-08 12:20:38 -070067 wsExpression = CFX_WideString::FromUTF8(bsExpression.AsStringC());
Dan Sinclair1770c022016-03-14 14:14:16 -040068 }
69 if (iLength >= 3) {
70 CFX_ByteString bsXMLIdent = pArguments->GetUTF8String(2);
tsepez4c3debb2016-04-08 12:20:38 -070071 wsXMLIdent = CFX_WideString::FromUTF8(bsXMLIdent.AsStringC());
Dan Sinclair1770c022016-03-14 14:14:16 -040072 }
tsepez4c3debb2016-04-08 12:20:38 -070073 FX_BOOL bSign = pNotify->GetDocProvider()->Sign(
74 hDoc, pNodeList, wsExpression.AsStringC(), wsXMLIdent.AsStringC());
dsinclair12a6b0c2016-05-26 11:14:08 -070075 CFXJSE_Value* pValue = pArguments->GetReturnValue();
dsinclairf27aeec2016-06-07 19:36:18 -070076 if (pValue)
77 pValue->SetBoolean(bSign);
Dan Sinclair1770c022016-03-14 14:14:16 -040078}
dsinclairc1df5d42016-07-18 06:36:51 -070079
dsinclaird1cf2392016-07-11 06:46:59 -070080void CScript_SignaturePseudoModel::Enumerate(CFXJSE_Arguments* pArguments) {
Dan Sinclair1770c022016-03-14 14:14:16 -040081 int32_t iLength = pArguments->GetLength();
82 if (iLength != 0) {
dsinclair2235b7b2016-06-02 07:42:25 -070083 ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"enumerate");
Dan Sinclair1770c022016-03-14 14:14:16 -040084 return;
85 }
dsinclaira1b07722016-07-11 08:20:58 -070086 CXFA_FFNotify* pNotify = m_pDocument->GetNotify();
Dan Sinclair1770c022016-03-14 14:14:16 -040087 if (!pNotify) {
88 return;
89 }
dsinclairdf4bc592016-03-31 20:34:43 -070090 CXFA_FFDoc* hDoc = pNotify->GetHDOC();
Dan Sinclair1770c022016-03-14 14:14:16 -040091 CXFA_NodeList* pList = pNotify->GetDocProvider()->Enumerate(hDoc);
92 if (!pList)
93 return;
dsinclairf27aeec2016-06-07 19:36:18 -070094 pArguments->GetReturnValue()->Assign(
95 m_pDocument->GetScriptContext()->GetJSValueFromMap(pList));
Dan Sinclair1770c022016-03-14 14:14:16 -040096}
dsinclairc1df5d42016-07-18 06:36:51 -070097
dsinclaird1cf2392016-07-11 06:46:59 -070098void CScript_SignaturePseudoModel::Clear(CFXJSE_Arguments* pArguments) {
Dan Sinclair1770c022016-03-14 14:14:16 -040099 int32_t iLength = pArguments->GetLength();
100 if (iLength < 1 || iLength > 2) {
dsinclair2235b7b2016-06-02 07:42:25 -0700101 ThrowException(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, L"clear");
Dan Sinclair1770c022016-03-14 14:14:16 -0400102 return;
103 }
dsinclaira1b07722016-07-11 08:20:58 -0700104 CXFA_FFNotify* pNotify = m_pDocument->GetNotify();
Dan Sinclair1770c022016-03-14 14:14:16 -0400105 if (!pNotify) {
106 return;
107 }
dsinclairdf4bc592016-03-31 20:34:43 -0700108 CXFA_FFDoc* hDoc = pNotify->GetHDOC();
dsinclair85d1f2c2016-06-23 12:40:16 -0700109 CXFA_Node* pNode = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400110 FX_BOOL bClear = TRUE;
111 if (iLength >= 1) {
112 pNode = static_cast<CXFA_Node*>(pArguments->GetObject(0));
113 }
114 if (iLength >= 2) {
115 bClear = pArguments->GetInt32(1) == 0 ? FALSE : TRUE;
116 }
117 FX_BOOL bFlag = pNotify->GetDocProvider()->Clear(hDoc, pNode, bClear);
dsinclair12a6b0c2016-05-26 11:14:08 -0700118 CFXJSE_Value* pValue = pArguments->GetReturnValue();
dsinclairf27aeec2016-06-07 19:36:18 -0700119 if (pValue)
120 pValue->SetBoolean(bFlag);
Dan Sinclair1770c022016-03-14 14:14:16 -0400121}