blob: 29f97648063381c2609b8c447336af2674bba997 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Tom Sepezc6ab1722015-02-05 15:27:25 -08004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/Document.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008
thestig7c292e02016-09-28 14:14:26 -07009#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <vector>
11
dsinclairbc5e6d22016-10-04 11:08:49 -070012#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070013#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080016#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair488b7ad2016-10-04 11:55:50 -070017#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_interform.h"
19#include "core/fpdfdoc/cpdf_nametree.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_annotiterator.h"
dsinclair735606d2016-10-05 15:47:02 -070021#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070022#include "fpdfsdk/cpdfsdk_interform.h"
23#include "fpdfsdk/cpdfsdk_pageview.h"
24#include "fpdfsdk/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070025#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040026#include "fpdfsdk/javascript/Field.h"
27#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040028#include "fpdfsdk/javascript/JS_Define.h"
29#include "fpdfsdk/javascript/JS_EventHandler.h"
30#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040031#include "fpdfsdk/javascript/JS_Value.h"
32#include "fpdfsdk/javascript/app.h"
dsinclair64376be2016-03-31 20:03:24 -070033#include "fpdfsdk/javascript/cjs_context.h"
34#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040035#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080036#include "third_party/base/numerics/safe_math.h"
thestigdadea5f2016-10-19 13:29:48 -070037#include "third_party/base/ptr_util.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070038
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039BEGIN_JS_STATIC_CONST(CJS_PrintParamsObj)
40END_JS_STATIC_CONST()
41
42BEGIN_JS_STATIC_PROP(CJS_PrintParamsObj)
43END_JS_STATIC_PROP()
44
45BEGIN_JS_STATIC_METHOD(CJS_PrintParamsObj)
46END_JS_STATIC_METHOD()
47
48IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
49
50PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070052 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 nStart = 0;
54 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070055 bSilent = false;
56 bShrinkToFit = false;
57 bPrintAsImage = false;
58 bReverse = false;
59 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063#define MINHEIGHT 5.0f
64
65BEGIN_JS_STATIC_CONST(CJS_Document)
66END_JS_STATIC_CONST()
67
68BEGIN_JS_STATIC_PROP(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069JS_STATIC_PROP_ENTRY(ADBE)
70JS_STATIC_PROP_ENTRY(author)
71JS_STATIC_PROP_ENTRY(baseURL)
72JS_STATIC_PROP_ENTRY(bookmarkRoot)
73JS_STATIC_PROP_ENTRY(calculate)
74JS_STATIC_PROP_ENTRY(Collab)
75JS_STATIC_PROP_ENTRY(creationDate)
76JS_STATIC_PROP_ENTRY(creator)
77JS_STATIC_PROP_ENTRY(delay)
78JS_STATIC_PROP_ENTRY(dirty)
79JS_STATIC_PROP_ENTRY(documentFileName)
80JS_STATIC_PROP_ENTRY(external)
81JS_STATIC_PROP_ENTRY(filesize)
82JS_STATIC_PROP_ENTRY(icons)
83JS_STATIC_PROP_ENTRY(info)
84JS_STATIC_PROP_ENTRY(keywords)
85JS_STATIC_PROP_ENTRY(layout)
86JS_STATIC_PROP_ENTRY(media)
87JS_STATIC_PROP_ENTRY(modDate)
88JS_STATIC_PROP_ENTRY(mouseX)
89JS_STATIC_PROP_ENTRY(mouseY)
90JS_STATIC_PROP_ENTRY(numFields)
91JS_STATIC_PROP_ENTRY(numPages)
92JS_STATIC_PROP_ENTRY(pageNum)
93JS_STATIC_PROP_ENTRY(pageWindowRect)
94JS_STATIC_PROP_ENTRY(path)
95JS_STATIC_PROP_ENTRY(producer)
96JS_STATIC_PROP_ENTRY(subject)
97JS_STATIC_PROP_ENTRY(title)
tonikitoo33c4cdb2016-08-08 10:52:51 -070098JS_STATIC_PROP_ENTRY(URL)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099JS_STATIC_PROP_ENTRY(zoom)
100JS_STATIC_PROP_ENTRY(zoomType)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101END_JS_STATIC_PROP()
102
103BEGIN_JS_STATIC_METHOD(CJS_Document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104JS_STATIC_METHOD_ENTRY(addAnnot)
105JS_STATIC_METHOD_ENTRY(addField)
106JS_STATIC_METHOD_ENTRY(addLink)
107JS_STATIC_METHOD_ENTRY(addIcon)
108JS_STATIC_METHOD_ENTRY(calculateNow)
109JS_STATIC_METHOD_ENTRY(closeDoc)
110JS_STATIC_METHOD_ENTRY(createDataObject)
111JS_STATIC_METHOD_ENTRY(deletePages)
112JS_STATIC_METHOD_ENTRY(exportAsText)
113JS_STATIC_METHOD_ENTRY(exportAsFDF)
114JS_STATIC_METHOD_ENTRY(exportAsXFDF)
115JS_STATIC_METHOD_ENTRY(extractPages)
116JS_STATIC_METHOD_ENTRY(getAnnot)
117JS_STATIC_METHOD_ENTRY(getAnnots)
118JS_STATIC_METHOD_ENTRY(getAnnot3D)
119JS_STATIC_METHOD_ENTRY(getAnnots3D)
120JS_STATIC_METHOD_ENTRY(getField)
121JS_STATIC_METHOD_ENTRY(getIcon)
122JS_STATIC_METHOD_ENTRY(getLinks)
123JS_STATIC_METHOD_ENTRY(getNthFieldName)
124JS_STATIC_METHOD_ENTRY(getOCGs)
125JS_STATIC_METHOD_ENTRY(getPageBox)
126JS_STATIC_METHOD_ENTRY(getPageNthWord)
127JS_STATIC_METHOD_ENTRY(getPageNthWordQuads)
128JS_STATIC_METHOD_ENTRY(getPageNumWords)
129JS_STATIC_METHOD_ENTRY(getPrintParams)
130JS_STATIC_METHOD_ENTRY(getURL)
tonikitoo1c836752016-08-08 16:14:05 -0700131JS_STATIC_METHOD_ENTRY(gotoNamedDest)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132JS_STATIC_METHOD_ENTRY(importAnFDF)
133JS_STATIC_METHOD_ENTRY(importAnXFDF)
134JS_STATIC_METHOD_ENTRY(importTextData)
135JS_STATIC_METHOD_ENTRY(insertPages)
136JS_STATIC_METHOD_ENTRY(mailForm)
137JS_STATIC_METHOD_ENTRY(print)
138JS_STATIC_METHOD_ENTRY(removeField)
139JS_STATIC_METHOD_ENTRY(replacePages)
140JS_STATIC_METHOD_ENTRY(resetForm)
141JS_STATIC_METHOD_ENTRY(removeIcon)
142JS_STATIC_METHOD_ENTRY(saveAs)
143JS_STATIC_METHOD_ENTRY(submitForm)
tonikitoobb5fa042016-08-19 11:18:29 -0700144JS_STATIC_METHOD_ENTRY(syncAnnotScan)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145JS_STATIC_METHOD_ENTRY(mailDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146END_JS_STATIC_METHOD()
147
148IMPLEMENT_JS_CLASS(CJS_Document, Document)
149
Tom Sepez33420902015-10-13 15:00:10 -0700150void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
151 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
152 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700153 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700154}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156Document::Document(CJS_Object* pJSObject)
157 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700158 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700160 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165// the total number of fileds in document.
tsepez4cf55152016-11-02 14:37:54 -0700166bool Document::numFields(IJS_Context* cc,
167 CJS_PropValue& vp,
168 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700170 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700171 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 }
dsinclair82e17672016-10-11 12:38:01 -0700173 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700174 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700175 return false;
tsepez55be02e2016-09-12 11:21:42 -0700176 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700177 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700179 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700180 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
182
tsepez4cf55152016-11-02 14:37:54 -0700183bool Document::dirty(IJS_Context* cc,
184 CJS_PropValue& vp,
185 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700186 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700187 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
tsepez55be02e2016-09-12 11:21:42 -0700189 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700191 vp << !!m_pFormFillEnv->GetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 } else {
193 bool bChanged = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 vp >> bChanged;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (bChanged)
dsinclair7cbe68e2016-10-12 11:56:23 -0700197 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 else
dsinclair7cbe68e2016-10-12 11:56:23 -0700199 m_pFormFillEnv->ClearChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 }
tsepez4cf55152016-11-02 14:37:54 -0700201 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
tsepez4cf55152016-11-02 14:37:54 -0700204bool Document::ADBE(IJS_Context* cc,
205 CJS_PropValue& vp,
206 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700207 if (vp.IsGetting())
208 vp.GetJSValue()->SetNull(CJS_Runtime::FromContext(cc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209
tsepez4cf55152016-11-02 14:37:54 -0700210 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211}
212
tsepez4cf55152016-11-02 14:37:54 -0700213bool Document::pageNum(IJS_Context* cc,
214 CJS_PropValue& vp,
215 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700216 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700217 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700218 return false;
tsepez55be02e2016-09-12 11:21:42 -0700219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700221 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 vp << pPageView->GetPageIndex();
223 }
224 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700225 int iPageCount = m_pFormFillEnv->GetPageCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 int iPageNum = 0;
227 vp >> iPageNum;
228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 if (iPageNum >= 0 && iPageNum < iPageCount) {
dsinclair82e17672016-10-11 12:38:01 -0700230 m_pFormFillEnv->JS_docgotoPage(iPageNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 } else if (iPageNum >= iPageCount) {
dsinclair82e17672016-10-11 12:38:01 -0700232 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 } else if (iPageNum < 0) {
dsinclair82e17672016-10-11 12:38:01 -0700234 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 }
236 }
237
tsepez4cf55152016-11-02 14:37:54 -0700238 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239}
240
tsepez4cf55152016-11-02 14:37:54 -0700241bool Document::addAnnot(IJS_Context* cc,
242 const std::vector<CJS_Value>& params,
243 CJS_Value& vRet,
244 CFX_WideString& sError) {
245 // Not supported.
246 return true;
247}
248
249bool Document::addField(IJS_Context* cc,
250 const std::vector<CJS_Value>& params,
251 CJS_Value& vRet,
252 CFX_WideString& sError) {
253 // Not supported.
254 return true;
255}
256
257bool Document::exportAsText(IJS_Context* cc,
258 const std::vector<CJS_Value>& params,
259 CJS_Value& vRet,
260 CFX_WideString& sError) {
261 // Unsafe, not supported.
262 return true;
263}
264
265bool Document::exportAsFDF(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800266 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 CJS_Value& vRet,
268 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800269 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700270 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
tsepez4cf55152016-11-02 14:37:54 -0700273bool Document::exportAsXFDF(IJS_Context* cc,
274 const std::vector<CJS_Value>& params,
275 CJS_Value& vRet,
276 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700278 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281// Maps a field object in PDF document to a JavaScript variable
282// comment:
283// note: the paremter cName, this is clue how to treat if the cName is not a
284// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
tsepez4cf55152016-11-02 14:37:54 -0700286bool Document::getField(IJS_Context* cc,
287 const std::vector<CJS_Value>& params,
288 CJS_Value& vRet,
289 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700291 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700292 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 }
dsinclair82e17672016-10-11 12:38:01 -0700294 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700295 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700296 return false;
tsepez55be02e2016-09-12 11:21:42 -0700297 }
tsepezcd5dc852016-09-08 11:23:24 -0700298 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700299 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700300 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700301 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
303 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700304 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700305 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 }
307
tsepezf3dc8c62016-08-10 06:29:29 -0700308 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700309 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
tsepezf3dc8c62016-08-10 06:29:29 -0700310 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700311 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700312 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 pField->AttachField(this, wideName);
314
tsepezf3dc8c62016-08-10 06:29:29 -0700315 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700316 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317}
318
319// Gets the name of the nth field in the document
tsepez4cf55152016-11-02 14:37:54 -0700320bool Document::getNthFieldName(IJS_Context* cc,
321 const std::vector<CJS_Value>& params,
322 CJS_Value& vRet,
323 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700324 if (params.size() != 1) {
325 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700326 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700327 }
dsinclair82e17672016-10-11 12:38:01 -0700328 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700329 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700330 return false;
tsepez55be02e2016-09-12 11:21:42 -0700331 }
tsepezf3dc8c62016-08-10 06:29:29 -0700332 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700333 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700334 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700336 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700337 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700339 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700341 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700343 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344
tsepezf3dc8c62016-08-10 06:29:29 -0700345 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700346 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347}
348
tsepez4cf55152016-11-02 14:37:54 -0700349bool Document::importAnFDF(IJS_Context* cc,
350 const std::vector<CJS_Value>& params,
351 CJS_Value& vRet,
352 CFX_WideString& sError) {
353 // Unsafe, not supported.
354 return true;
355}
356
357bool Document::importAnXFDF(IJS_Context* cc,
358 const std::vector<CJS_Value>& params,
359 CJS_Value& vRet,
360 CFX_WideString& sError) {
361 // Unsafe, not supported.
362 return true;
363}
364
365bool Document::importTextData(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800366 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 CJS_Value& vRet,
368 CFX_WideString& sError) {
369 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700370 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371}
372
373// exports the form data and mails the resulting fdf file as an attachment to
374// all recipients.
375// comment: need reader supports
tsepez4cf55152016-11-02 14:37:54 -0700376bool Document::mailForm(IJS_Context* cc,
377 const std::vector<CJS_Value>& params,
378 CJS_Value& vRet,
379 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700380 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700381 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700382 return false;
tsepez55be02e2016-09-12 11:21:42 -0700383 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700384 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700385 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700386 return false;
tsepez55be02e2016-09-12 11:21:42 -0700387 }
388
tsepezf3dc8c62016-08-10 06:29:29 -0700389 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezb4694242016-08-15 16:44:55 -0700390 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700393 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700394 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
395 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
396 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700397 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700398 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
399 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400
dsinclair7cbe68e2016-10-12 11:56:23 -0700401 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 CFX_ByteTextBuf textBuf;
403 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700404 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700407 CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
408 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
409 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
410 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700412 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413}
414
tsepez4cf55152016-11-02 14:37:54 -0700415bool Document::print(IJS_Context* cc,
416 const std::vector<CJS_Value>& params,
417 CJS_Value& vRet,
418 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700419 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700420 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700421 return false;
tsepez55be02e2016-09-12 11:21:42 -0700422 }
423
tsepezf3dc8c62016-08-10 06:29:29 -0700424 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
425 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700426
tsepez4cf55152016-11-02 14:37:54 -0700427 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 int nStart = 0;
429 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700430 bool bSilent = false;
431 bool bShrinkToFit = false;
432 bool bPrintAsImage = false;
433 bool bReverse = false;
434 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435
436 int nlength = params.size();
437 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700438 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700439 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
440 if (CFXJS_Engine::GetObjDefnID(pObj) ==
441 CJS_PrintParamsObj::g_nObjDefnID) {
442 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700443 if (PrintParamsObj* pprintparamsObj =
444 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
445 bUI = pprintparamsObj->bUI;
446 nStart = pprintparamsObj->nStart;
447 nEnd = pprintparamsObj->nEnd;
448 bSilent = pprintparamsObj->bSilent;
449 bShrinkToFit = pprintparamsObj->bShrinkToFit;
450 bPrintAsImage = pprintparamsObj->bPrintAsImage;
451 bReverse = pprintparamsObj->bReverse;
452 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700454 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 }
456 }
457 } else {
458 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700459 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700461 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700463 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700465 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700467 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700469 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700471 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700473 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 }
475
dsinclair82e17672016-10-11 12:38:01 -0700476 if (m_pFormFillEnv) {
477 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
478 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700479 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 }
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482}
483
484// removes the specified field from the document.
485// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700486// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487
tsepez4cf55152016-11-02 14:37:54 -0700488bool Document::removeField(IJS_Context* cc,
489 const std::vector<CJS_Value>& params,
490 CJS_Value& vRet,
491 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700492 if (params.size() != 1) {
493 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700494 return false;
tsepez55be02e2016-09-12 11:21:42 -0700495 }
dsinclair82e17672016-10-11 12:38:01 -0700496 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700497 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700498 return false;
tsepez55be02e2016-09-12 11:21:42 -0700499 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700500 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
501 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700502 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700503 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 }
tsepezcd5dc852016-09-08 11:23:24 -0700505 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700506 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -0700507 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700508 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Lei Zhangd88a3642015-11-10 09:38:57 -0800509 std::vector<CPDFSDK_Widget*> widgets;
510 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800511 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700512 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513
Lei Zhangd88a3642015-11-10 09:38:57 -0800514 for (CPDFSDK_Widget* pWidget : widgets) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800515 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800516 --rcAnnot.left;
517 --rcAnnot.bottom;
518 ++rcAnnot.right;
519 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520
tsepezdf964df2016-04-21 12:09:41 -0700521 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800522 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800523 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524
dsinclair1897bdc2016-09-06 14:18:57 -0700525 // If there is currently no pageview associated with the page being used
526 // do not create one. We may be in the process of tearing down the document
527 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700528 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700529 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700530#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700531 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700532#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700533 pPageView->UpdateRects(aRefresh);
534 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700536 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537
tsepez4cf55152016-11-02 14:37:54 -0700538 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539}
540
541// reset filed values within a document.
542// comment:
543// note: if the fields names r not rational, aodbe is dumb for it.
544
tsepez4cf55152016-11-02 14:37:54 -0700545bool Document::resetForm(IJS_Context* cc,
546 const std::vector<CJS_Value>& params,
547 CJS_Value& vRet,
548 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700549 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700550 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700551 return false;
tsepez55be02e2016-09-12 11:21:42 -0700552 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700553 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
554 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
555 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700556 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700557 return false;
tsepez55be02e2016-09-12 11:21:42 -0700558 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559
dsinclair7cbe68e2016-10-12 11:56:23 -0700560 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700562 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563
Lei Zhangdd734de2015-11-11 10:16:52 -0800564 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700565 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700566 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700567 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800568 }
569
tsepez55be02e2016-09-12 11:21:42 -0700570 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700571 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700572
Lei Zhangd88a3642015-11-10 09:38:57 -0800573 switch (params[0].GetType()) {
574 default:
tsepezb4694242016-08-15 16:44:55 -0700575 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800576 break;
577 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700578 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800579 break;
580 }
581
582 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700583 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800584 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700585 aName.GetElement(pRuntime, i, valElement);
586 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800587 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
588 aFields.push_back(pPDFForm->GetField(j, swVal));
589 }
590
591 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700592 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700593 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 }
595
tsepez4cf55152016-11-02 14:37:54 -0700596 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597}
598
tsepez4cf55152016-11-02 14:37:54 -0700599bool Document::saveAs(IJS_Context* cc,
600 const std::vector<CJS_Value>& params,
601 CJS_Value& vRet,
602 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700604 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605}
606
tsepez4cf55152016-11-02 14:37:54 -0700607bool Document::syncAnnotScan(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800608 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 CJS_Value& vRet,
610 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700611 return true;
612}
613
614bool Document::submitForm(IJS_Context* cc,
615 const std::vector<CJS_Value>& params,
616 CJS_Value& vRet,
617 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 int nSize = params.size();
619 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700620 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700621 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 }
dsinclair82e17672016-10-11 12:38:01 -0700623 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700624 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700625 return false;
tsepez55be02e2016-09-12 11:21:42 -0700626 }
tsepezcd5dc852016-09-08 11:23:24 -0700627 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
628 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepeze5aff742016-08-08 09:49:42 -0700629 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700631 bool bFDF = true;
632 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700634 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700635 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700637 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700639 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700641 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700642 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700643 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
644 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700646 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700647
tsepezb4694242016-08-15 16:44:55 -0700648 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
649 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700650
tsepezb4694242016-08-15 16:44:55 -0700651 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
652 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700653
tsepezb4694242016-08-15 16:44:55 -0700654 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
655 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 }
657
dsinclair7cbe68e2016-10-12 11:56:23 -0700658 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700660 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800661 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700663 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700665 }
tsepez4cf55152016-11-02 14:37:54 -0700666 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 }
668
Lei Zhangd88a3642015-11-10 09:38:57 -0800669 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700670 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700671 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700672 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673
tsepezb4694242016-08-15 16:44:55 -0700674 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
676 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
677 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
678 if (!bEmpty && pField->GetValue().IsEmpty())
679 continue;
680
Lei Zhangd88a3642015-11-10 09:38:57 -0800681 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682 }
683 }
684
Lei Zhangd88a3642015-11-10 09:38:57 -0800685 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800687 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 pRuntime->EndBlock();
689 }
tsepez4cf55152016-11-02 14:37:54 -0700690 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700691}
692
dsinclair82e17672016-10-11 12:38:01 -0700693void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
694 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695}
696
tsepez4cf55152016-11-02 14:37:54 -0700697bool Document::bookmarkRoot(IJS_Context* cc,
698 CJS_PropValue& vp,
699 CFX_WideString& sError) {
700 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701}
702
tsepez4cf55152016-11-02 14:37:54 -0700703bool Document::mailDoc(IJS_Context* cc,
704 const std::vector<CJS_Value>& params,
705 CJS_Value& vRet,
706 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700707 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
708
709 // TODO(tsepez): Check maximum number of allowed params.
710
tsepez4cf55152016-11-02 14:37:54 -0700711 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712 CFX_WideString cTo = L"";
713 CFX_WideString cCc = L"";
714 CFX_WideString cBcc = L"";
715 CFX_WideString cSubject = L"";
716 CFX_WideString cMsg = L"";
717
tsepezf3dc8c62016-08-10 06:29:29 -0700718 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719
tsepezf3dc8c62016-08-10 06:29:29 -0700720 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700721 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700722 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700723 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700724 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700725 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700726 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700727 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700728 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700729 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700730 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700731 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732
Tom Sepez39bfe122015-09-17 15:25:23 -0700733 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700734 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735
tsepezb4694242016-08-15 16:44:55 -0700736 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700737 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738
tsepezb4694242016-08-15 16:44:55 -0700739 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
740 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741
tsepezb4694242016-08-15 16:44:55 -0700742 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
743 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744
tsepezb4694242016-08-15 16:44:55 -0700745 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
746 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747
tsepezb4694242016-08-15 16:44:55 -0700748 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
749 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750
tsepezb4694242016-08-15 16:44:55 -0700751 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
752 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753 }
754
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700756 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
757 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
758 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 pRuntime->EndBlock();
760
tsepez4cf55152016-11-02 14:37:54 -0700761 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762}
763
tsepez4cf55152016-11-02 14:37:54 -0700764bool Document::author(IJS_Context* cc,
765 CJS_PropValue& vp,
766 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700767 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700768}
769
tsepez4cf55152016-11-02 14:37:54 -0700770bool Document::info(IJS_Context* cc,
771 CJS_PropValue& vp,
772 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700773 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700774 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700775 return false;
tonikitooec268762016-08-10 12:29:28 -0700776 }
dsinclair82e17672016-10-11 12:38:01 -0700777 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700778 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700779 return false;
tsepez55be02e2016-09-12 11:21:42 -0700780 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700781 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700783 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800784
dsinclair38fd8442016-09-15 10:15:32 -0700785 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
786 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
787 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
788 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
789 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
790 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
791 CFX_WideString cwCreationDate =
792 pDictionary->GetUnicodeTextFor("CreationDate");
793 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
794 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800795
tonikitooec268762016-08-10 12:29:28 -0700796 CJS_Context* pContext = (CJS_Context*)cc;
797 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepezc6ab1722015-02-05 15:27:25 -0800798
tsepezb4694242016-08-15 16:44:55 -0700799 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
800 pRuntime->PutObjectString(pObj, L"Author", cwAuthor);
801 pRuntime->PutObjectString(pObj, L"Title", cwTitle);
802 pRuntime->PutObjectString(pObj, L"Subject", cwSubject);
803 pRuntime->PutObjectString(pObj, L"Keywords", cwKeywords);
804 pRuntime->PutObjectString(pObj, L"Creator", cwCreator);
805 pRuntime->PutObjectString(pObj, L"Producer", cwProducer);
806 pRuntime->PutObjectString(pObj, L"CreationDate", cwCreationDate);
807 pRuntime->PutObjectString(pObj, L"ModDate", cwModDate);
808 pRuntime->PutObjectString(pObj, L"Trapped", cwTrapped);
tonikitooec268762016-08-10 12:29:28 -0700809
810 // It's to be compatible to non-standard info dictionary.
811 for (const auto& it : *pDictionary) {
812 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800813 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700814 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
815 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepezb4694242016-08-15 16:44:55 -0700816 pRuntime->PutObjectString(pObj, wsKey, pValueObj->GetUnicodeText());
tonikitooec268762016-08-10 12:29:28 -0700817 } else if (pValueObj->IsNumber()) {
tsepezb4694242016-08-15 16:44:55 -0700818 pRuntime->PutObjectNumber(pObj, wsKey, (float)pValueObj->GetNumber());
tonikitooec268762016-08-10 12:29:28 -0700819 } else if (pValueObj->IsBoolean()) {
tsepezb4694242016-08-15 16:44:55 -0700820 pRuntime->PutObjectBoolean(pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700821 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 }
tonikitooec268762016-08-10 12:29:28 -0700823 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700824 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700825}
826
tsepez4cf55152016-11-02 14:37:54 -0700827bool Document::getPropertyInternal(IJS_Context* cc,
828 CJS_PropValue& vp,
829 const CFX_ByteString& propName,
830 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700831 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700832 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700833 return false;
tsepez55be02e2016-09-12 11:21:42 -0700834 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700835 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700837 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838
839 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700840 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700842 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700843 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700844 return false;
tsepez55be02e2016-09-12 11:21:42 -0700845 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700846 CFX_WideString csProperty;
847 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800848 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
849 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700850 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 }
tsepez4cf55152016-11-02 14:37:54 -0700852 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700853}
854
tsepez4cf55152016-11-02 14:37:54 -0700855bool Document::creationDate(IJS_Context* cc,
856 CJS_PropValue& vp,
857 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700858 return getPropertyInternal(cc, vp, "CreationDate", sError);
859}
860
tsepez4cf55152016-11-02 14:37:54 -0700861bool Document::creator(IJS_Context* cc,
862 CJS_PropValue& vp,
863 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700864 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700865}
866
tsepez4cf55152016-11-02 14:37:54 -0700867bool Document::delay(IJS_Context* cc,
868 CJS_PropValue& vp,
869 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700870 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700871 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700872 return false;
tsepez55be02e2016-09-12 11:21:42 -0700873 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 if (vp.IsGetting()) {
875 vp << m_bDelay;
876 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700877 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700878 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700879 return false;
tsepez55be02e2016-09-12 11:21:42 -0700880 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 vp >> m_bDelay;
882 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700883 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700885 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
886 DelayDataToProcess.swap(m_DelayData);
dsinclair82e17672016-10-11 12:38:01 -0700887 for (const auto& pData : DelayDataToProcess)
888 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 }
890 }
tsepez4cf55152016-11-02 14:37:54 -0700891 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892}
893
tsepez4cf55152016-11-02 14:37:54 -0700894bool Document::keywords(IJS_Context* cc,
895 CJS_PropValue& vp,
896 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700897 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898}
899
tsepez4cf55152016-11-02 14:37:54 -0700900bool Document::modDate(IJS_Context* cc,
901 CJS_PropValue& vp,
902 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700903 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904}
905
tsepez4cf55152016-11-02 14:37:54 -0700906bool Document::producer(IJS_Context* cc,
907 CJS_PropValue& vp,
908 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700909 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910}
911
tsepez4cf55152016-11-02 14:37:54 -0700912bool Document::subject(IJS_Context* cc,
913 CJS_PropValue& vp,
914 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700915 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916}
917
tsepez4cf55152016-11-02 14:37:54 -0700918bool Document::title(IJS_Context* cc,
919 CJS_PropValue& vp,
920 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700921 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700922 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700923 return false;
tsepez55be02e2016-09-12 11:21:42 -0700924 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700925 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926}
927
tsepez4cf55152016-11-02 14:37:54 -0700928bool Document::numPages(IJS_Context* cc,
929 CJS_PropValue& vp,
930 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700931 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700932 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700933 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 }
dsinclair82e17672016-10-11 12:38:01 -0700935 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700936 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700937 return false;
tsepez55be02e2016-09-12 11:21:42 -0700938 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700939 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700940 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941}
942
tsepez4cf55152016-11-02 14:37:54 -0700943bool Document::external(IJS_Context* cc,
944 CJS_PropValue& vp,
945 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700946 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 if (vp.IsGetting()) {
948 vp << true;
949 }
tsepez4cf55152016-11-02 14:37:54 -0700950 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951}
952
tsepez4cf55152016-11-02 14:37:54 -0700953bool Document::filesize(IJS_Context* cc,
954 CJS_PropValue& vp,
955 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700957 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700958 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 }
960 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700961 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962}
963
tsepez4cf55152016-11-02 14:37:54 -0700964bool Document::mouseX(IJS_Context* cc,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700965 CJS_PropValue& vp,
966 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700967 return true;
968}
969
970bool Document::mouseY(IJS_Context* cc,
971 CJS_PropValue& vp,
972 CFX_WideString& sError) {
973 return true;
974}
975
976bool Document::URL(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700977 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700978 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700979 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700980 }
dsinclair82e17672016-10-11 12:38:01 -0700981 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700982 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700983 return false;
tsepez55be02e2016-09-12 11:21:42 -0700984 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700985 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700986 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700987}
988
tsepez4cf55152016-11-02 14:37:54 -0700989bool Document::baseURL(IJS_Context* cc,
990 CJS_PropValue& vp,
991 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 if (vp.IsGetting()) {
993 vp << m_cwBaseURL;
994 } else {
995 vp >> m_cwBaseURL;
996 }
tsepez4cf55152016-11-02 14:37:54 -0700997 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998}
999
tsepez4cf55152016-11-02 14:37:54 -07001000bool Document::calculate(IJS_Context* cc,
1001 CJS_PropValue& vp,
1002 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001003 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001004 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
tsepez55be02e2016-09-12 11:21:42 -07001006 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001007 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001009 vp << !!pInterForm->IsCalculateEnabled();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 } else {
1011 bool bCalculate;
1012 vp >> bCalculate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013 pInterForm->EnableCalculate(bCalculate);
1014 }
tsepez4cf55152016-11-02 14:37:54 -07001015 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016}
1017
tsepez4cf55152016-11-02 14:37:54 -07001018bool Document::documentFileName(IJS_Context* cc,
1019 CJS_PropValue& vp,
1020 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001022 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001023 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 }
dsinclair82e17672016-10-11 12:38:01 -07001025 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001026 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001027 return false;
tsepez55be02e2016-09-12 11:21:42 -07001028 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001029 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 int32_t i = wsFilePath.GetLength() - 1;
1031 for (; i >= 0; i--) {
1032 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1033 break;
1034 }
1035 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1036 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1037 } else {
1038 vp << L"";
1039 }
tsepez4cf55152016-11-02 14:37:54 -07001040 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041}
1042
tsepez4cf55152016-11-02 14:37:54 -07001043bool Document::path(IJS_Context* cc,
1044 CJS_PropValue& vp,
1045 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001047 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001048 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 }
dsinclair82e17672016-10-11 12:38:01 -07001050 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001051 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001052 return false;
tsepez55be02e2016-09-12 11:21:42 -07001053 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001054 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001055 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056}
1057
tsepez4cf55152016-11-02 14:37:54 -07001058bool Document::pageWindowRect(IJS_Context* cc,
1059 CJS_PropValue& vp,
1060 CFX_WideString& sError) {
1061 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062}
1063
tsepez4cf55152016-11-02 14:37:54 -07001064bool Document::layout(IJS_Context* cc,
1065 CJS_PropValue& vp,
1066 CFX_WideString& sError) {
1067 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068}
1069
tsepez4cf55152016-11-02 14:37:54 -07001070bool Document::addLink(IJS_Context* cc,
1071 const std::vector<CJS_Value>& params,
1072 CJS_Value& vRet,
1073 CFX_WideString& sError) {
1074 return true;
1075}
1076
1077bool Document::closeDoc(IJS_Context* cc,
1078 const std::vector<CJS_Value>& params,
1079 CJS_Value& vRet,
1080 CFX_WideString& sError) {
1081 return true;
1082}
1083
1084bool Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001085 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 CJS_Value& vRet,
1087 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001088 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089}
1090
tsepez4cf55152016-11-02 14:37:54 -07001091bool Document::getAnnot(IJS_Context* cc,
1092 const std::vector<CJS_Value>& params,
1093 CJS_Value& vRet,
1094 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001095 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001096 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001097 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001098 }
dsinclair82e17672016-10-11 12:38:01 -07001099 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001100 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001101 return false;
tsepez55be02e2016-09-12 11:21:42 -07001102 }
tsepezcd5dc852016-09-08 11:23:24 -07001103 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001104 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1105 int nPageNo = params[0].ToInt(pRuntime);
1106 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001107 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001108 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001109 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001110
1111 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1112 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
1113 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1114 CPDFSDK_BAAnnot* pBAAnnot = static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1115 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1116 pSDKBAAnnot = pBAAnnot;
1117 break;
1118 }
1119 }
1120
1121 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001122 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001123
1124 v8::Local<v8::Object> pObj =
1125 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1126 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001127 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001128
1129 CJS_Annot* pJS_Annot =
1130 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1131 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001132 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001133
1134 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1135 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001136 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001137
1138 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1139
1140 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001141 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142}
1143
tsepez4cf55152016-11-02 14:37:54 -07001144bool Document::getAnnots(IJS_Context* cc,
1145 const std::vector<CJS_Value>& params,
1146 CJS_Value& vRet,
1147 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001148 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001149 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001150 return false;
tsepez55be02e2016-09-12 11:21:42 -07001151 }
tonikitoo0ee35902016-08-26 14:41:29 -07001152 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepez55be02e2016-09-12 11:21:42 -07001153 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tonikitoo0ee35902016-08-26 14:41:29 -07001154
1155 // TODO(tonikitoo): Add support supported parameters as per
1156 // the PDF spec.
1157
dsinclair7cbe68e2016-10-12 11:56:23 -07001158 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001159 CJS_Array annots;
1160
1161 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001162 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001163 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001164 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001165
1166 CPDFSDK_AnnotIterator annotIterator(pPageView, false);
1167 while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
1168 CPDFSDK_BAAnnot* pSDKBAAnnot =
1169 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
1170 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001171 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001172
1173 v8::Local<v8::Object> pObj =
1174 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1175 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001176 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001177
1178 CJS_Annot* pJS_Annot =
1179 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1180 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001181 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001182
1183 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1184 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001185 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001186
1187 pAnnot->SetSDKAnnot(pSDKBAAnnot);
1188 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1189 }
1190 }
1191
1192 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001193 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194}
1195
tsepez4cf55152016-11-02 14:37:54 -07001196bool Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001197 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 CJS_Value& vRet,
1199 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001200 vRet.SetNull(CJS_Runtime::FromContext(cc));
1201 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202}
1203
tsepez4cf55152016-11-02 14:37:54 -07001204bool Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001205 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 CJS_Value& vRet,
1207 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001208 return true;
1209}
1210
1211bool Document::getOCGs(IJS_Context* cc,
1212 const std::vector<CJS_Value>& params,
1213 CJS_Value& vRet,
1214 CFX_WideString& sError) {
1215 return true;
1216}
1217
1218bool Document::getLinks(IJS_Context* cc,
1219 const std::vector<CJS_Value>& params,
1220 CJS_Value& vRet,
1221 CFX_WideString& sError) {
1222 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001223}
1224
1225bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1226 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1227 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1228}
1229
tsepez4cf55152016-11-02 14:37:54 -07001230bool Document::addIcon(IJS_Context* cc,
1231 const std::vector<CJS_Value>& params,
1232 CJS_Value& vRet,
1233 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001235 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001236 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 }
tsepezf3dc8c62016-08-10 06:29:29 -07001238
tsepezcd5dc852016-09-08 11:23:24 -07001239 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001240 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001241 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001242
Tom Sepez39bfe122015-09-17 15:25:23 -07001243 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001244 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001245 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001246 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247
tsepezb4694242016-08-15 16:44:55 -07001248 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1249 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001250 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001251 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001252 }
1253
tsepezb4694242016-08-15 16:44:55 -07001254 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001255 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001256 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001257 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 }
1259
thestigdadea5f2016-10-19 13:29:48 -07001260 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1261 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001262 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263}
1264
tsepez4cf55152016-11-02 14:37:54 -07001265bool Document::icons(IJS_Context* cc,
1266 CJS_PropValue& vp,
1267 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001269 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001270 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271 }
1272
tsepezf3dc8c62016-08-10 06:29:29 -07001273 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
thestigdadea5f2016-10-19 13:29:48 -07001274 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001275 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001276 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 }
1278
tsepeze5aff742016-08-08 09:49:42 -07001279 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280
Tom Sepez6ba32d92016-01-05 16:26:32 -08001281 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001282 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001283 v8::Local<v8::Object> pObj =
1284 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001286 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287
tsepezb4694242016-08-15 16:44:55 -07001288 CJS_Icon* pJS_Icon =
1289 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001291 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292
thestigdadea5f2016-10-19 13:29:48 -07001293 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001295 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001296
dsinclair09bad1c2016-10-18 19:26:30 -07001297 pIcon->SetStream(pIconElement->IconStream->GetStream());
1298 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001299 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 }
1301
1302 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001303 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001304}
1305
tsepez4cf55152016-11-02 14:37:54 -07001306bool Document::getIcon(IJS_Context* cc,
1307 const std::vector<CJS_Value>& params,
1308 CJS_Value& vRet,
1309 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001311 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001312 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313 }
1314
thestigdadea5f2016-10-19 13:29:48 -07001315 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001316 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317
tsepezcd5dc852016-09-08 11:23:24 -07001318 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001319 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320
thestigdadea5f2016-10-19 13:29:48 -07001321 for (const auto& pIconElement : m_Icons) {
1322 if (pIconElement->IconName != swIconName)
1323 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324
thestigdadea5f2016-10-19 13:29:48 -07001325 v8::Local<v8::Object> pObj =
1326 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1327 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001328 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329
thestigdadea5f2016-10-19 13:29:48 -07001330 CJS_Icon* pJS_Icon =
1331 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1332 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001333 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001334
thestigdadea5f2016-10-19 13:29:48 -07001335 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1336 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001337 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338
thestigdadea5f2016-10-19 13:29:48 -07001339 pIcon->SetIconName(swIconName);
1340 pIcon->SetStream(pIconElement->IconStream->GetStream());
1341
1342 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001343 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001344 }
1345
tsepez4cf55152016-11-02 14:37:54 -07001346 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347}
1348
tsepez4cf55152016-11-02 14:37:54 -07001349bool Document::removeIcon(IJS_Context* cc,
1350 const std::vector<CJS_Value>& params,
1351 CJS_Value& vRet,
1352 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001353 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001354 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001355}
1356
tsepez4cf55152016-11-02 14:37:54 -07001357bool Document::createDataObject(IJS_Context* cc,
1358 const std::vector<CJS_Value>& params,
1359 CJS_Value& vRet,
1360 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001361 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001362 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001363}
1364
tsepez4cf55152016-11-02 14:37:54 -07001365bool Document::media(IJS_Context* cc,
1366 CJS_PropValue& vp,
1367 CFX_WideString& sError) {
1368 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001369}
1370
tsepez4cf55152016-11-02 14:37:54 -07001371bool Document::calculateNow(IJS_Context* cc,
1372 const std::vector<CJS_Value>& params,
1373 CJS_Value& vRet,
1374 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001375 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001376 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001377 return false;
tsepez55be02e2016-09-12 11:21:42 -07001378 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001379 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1380 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1381 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001382 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001383 return false;
tsepez55be02e2016-09-12 11:21:42 -07001384 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001385 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001386 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001387}
1388
tsepez4cf55152016-11-02 14:37:54 -07001389bool Document::Collab(IJS_Context* cc,
1390 CJS_PropValue& vp,
1391 CFX_WideString& sError) {
1392 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001393}
1394
tsepez4cf55152016-11-02 14:37:54 -07001395bool Document::getPageNthWord(IJS_Context* cc,
1396 const std::vector<CJS_Value>& params,
1397 CJS_Value& vRet,
1398 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001399 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001400 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001401 return false;
tsepez55be02e2016-09-12 11:21:42 -07001402 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001403 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001404 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001405 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001406 }
1407 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001408
1409 // TODO(tsepez): check maximum allowable params.
1410
tsepezb4694242016-08-15 16:44:55 -07001411 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1412 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1413 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001414
dsinclair7cbe68e2016-10-12 11:56:23 -07001415 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001416 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001417 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001418
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001420 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001421 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001423
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1425 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001426 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001427
thestig5cc24652016-04-26 11:46:02 -07001428 CPDF_Page page(pDocument, pPageDict, true);
1429 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001430
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001433 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001434 if (pPageObj->IsText()) {
1435 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001436 int nObjWords = CountWords(pTextObj);
1437 if (nWords + nObjWords >= nWordNo) {
1438 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1439 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001440 }
Tom Sepez2398d892016-02-17 16:46:26 -08001441 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001442 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001443 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001444
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 if (bStrip) {
1446 swRet.TrimLeft();
1447 swRet.TrimRight();
1448 }
1449
tsepezf3dc8c62016-08-10 06:29:29 -07001450 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001451 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001452}
1453
tsepez4cf55152016-11-02 14:37:54 -07001454bool Document::getPageNthWordQuads(IJS_Context* cc,
1455 const std::vector<CJS_Value>& params,
1456 CJS_Value& vRet,
1457 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001458 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001459 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001460 return false;
tsepez55be02e2016-09-12 11:21:42 -07001461 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001462 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001463 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001464 return false;
tsepez55be02e2016-09-12 11:21:42 -07001465 }
tsepez4cf55152016-11-02 14:37:54 -07001466 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467}
1468
tsepez4cf55152016-11-02 14:37:54 -07001469bool Document::getPageNumWords(IJS_Context* cc,
1470 const std::vector<CJS_Value>& params,
1471 CJS_Value& vRet,
1472 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001473 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001474 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001475 return false;
tsepez55be02e2016-09-12 11:21:42 -07001476 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001477 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001478 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001479 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001480 }
1481 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001482 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001483 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001485 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001486 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 }
1488
1489 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1490 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001491 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492
thestig5cc24652016-04-26 11:46:02 -07001493 CPDF_Page page(pDocument, pPageDict, true);
1494 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495
1496 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001497 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001498 if (pPageObj->IsText())
1499 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 }
1501
tsepezf3dc8c62016-08-10 06:29:29 -07001502 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001503 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001504}
1505
tsepez4cf55152016-11-02 14:37:54 -07001506bool Document::getPrintParams(IJS_Context* cc,
1507 const std::vector<CJS_Value>& params,
1508 CJS_Value& vRet,
1509 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001510 CJS_Context* pContext = (CJS_Context*)cc;
1511 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001512 v8::Local<v8::Object> pRetObj =
1513 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514
1515 // Not implemented yet.
1516
tsepezf3dc8c62016-08-10 06:29:29 -07001517 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001518 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001519}
1520
1521#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1522
1523int Document::CountWords(CPDF_TextObject* pTextObj) {
1524 if (!pTextObj)
1525 return 0;
1526
1527 int nWords = 0;
1528
1529 CPDF_Font* pFont = pTextObj->GetFont();
1530 if (!pFont)
1531 return 0;
1532
tsepez4cf55152016-11-02 14:37:54 -07001533 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001534
1535 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001536 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001537 FX_FLOAT kerning;
1538
1539 pTextObj->GetCharInfo(i, charcode, kerning);
1540 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1541
Tom Sepez62a70f92016-03-21 15:00:20 -07001542 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001543 if (swUnicode.GetLength() > 0)
1544 unicode = swUnicode[0];
1545
1546 if (ISLATINWORD(unicode) && bIsLatin)
1547 continue;
1548
1549 bIsLatin = ISLATINWORD(unicode);
1550 if (unicode != 0x20)
1551 nWords++;
1552 }
1553
1554 return nWords;
1555}
1556
1557CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1558 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001559 CFX_WideString swRet;
1560
1561 CPDF_Font* pFont = pTextObj->GetFont();
1562 if (!pFont)
1563 return L"";
1564
1565 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001566 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001567
1568 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001569 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 FX_FLOAT kerning;
1571
1572 pTextObj->GetCharInfo(i, charcode, kerning);
1573 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1574
Tom Sepez62a70f92016-03-21 15:00:20 -07001575 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001576 if (swUnicode.GetLength() > 0)
1577 unicode = swUnicode[0];
1578
1579 if (ISLATINWORD(unicode) && bIsLatin) {
1580 } else {
1581 bIsLatin = ISLATINWORD(unicode);
1582 if (unicode != 0x20)
1583 nWords++;
1584 }
1585
1586 if (nWords - 1 == nWordIndex)
1587 swRet += unicode;
1588 }
1589
1590 return swRet;
1591}
1592
tsepez4cf55152016-11-02 14:37:54 -07001593bool Document::zoom(IJS_Context* cc,
1594 CJS_PropValue& vp,
1595 CFX_WideString& sError) {
1596 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001597}
1598
1599/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001600(none, NoVary)
1601(fitP, FitPage)
1602(fitW, FitWidth)
1603(fitH, FitHeight)
1604(fitV, FitVisibleWidth)
1605(pref, Preferred)
1606(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001607*/
1608
tsepez4cf55152016-11-02 14:37:54 -07001609bool Document::zoomType(IJS_Context* cc,
1610 CJS_PropValue& vp,
1611 CFX_WideString& sError) {
1612 return true;
1613}
1614
1615bool Document::deletePages(IJS_Context* cc,
1616 const std::vector<CJS_Value>& params,
1617 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001618 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001619 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001620 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001621}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001622
tsepez4cf55152016-11-02 14:37:54 -07001623bool Document::extractPages(IJS_Context* cc,
1624 const std::vector<CJS_Value>& params,
1625 CJS_Value& vRet,
1626 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001628 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629}
1630
tsepez4cf55152016-11-02 14:37:54 -07001631bool Document::insertPages(IJS_Context* cc,
1632 const std::vector<CJS_Value>& params,
1633 CJS_Value& vRet,
1634 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001635 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001636 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001637}
1638
tsepez4cf55152016-11-02 14:37:54 -07001639bool Document::replacePages(IJS_Context* cc,
1640 const std::vector<CJS_Value>& params,
1641 CJS_Value& vRet,
1642 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001644 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645}
1646
tsepez4cf55152016-11-02 14:37:54 -07001647bool Document::getURL(IJS_Context* cc,
1648 const std::vector<CJS_Value>& params,
1649 CJS_Value& vRet,
1650 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001651 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001652 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653}
1654
tsepez4cf55152016-11-02 14:37:54 -07001655bool Document::gotoNamedDest(IJS_Context* cc,
1656 const std::vector<CJS_Value>& params,
1657 CJS_Value& vRet,
1658 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001659 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001660 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001661 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001662 }
dsinclair82e17672016-10-11 12:38:01 -07001663 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001664 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001665 return false;
tsepez55be02e2016-09-12 11:21:42 -07001666 }
tsepezcd5dc852016-09-08 11:23:24 -07001667 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001668 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001669 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001670 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001671 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001672 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001673
tonikitoo1c836752016-08-08 16:14:05 -07001674 CPDF_NameTree nameTree(pDocument, "Dests");
1675 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1676 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001677 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001678
1679 CPDF_Dest dest(destArray);
1680 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1681
1682 std::unique_ptr<float[]> scrollPositionArray;
1683 int scrollPositionArraySize = 0;
1684
1685 if (arrayObject) {
1686 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1687 int j = 0;
1688 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1689 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1690 scrollPositionArraySize = j;
1691 }
1692
tsepezb4694242016-08-15 16:44:55 -07001693 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001694 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1695 scrollPositionArray.get(),
1696 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001697 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001698
tsepez4cf55152016-11-02 14:37:54 -07001699 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001700}
1701
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001703 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001704}
1705
1706void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1707 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001708 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1709 auto iter = m_DelayData.begin();
1710 while (iter != m_DelayData.end()) {
1711 auto old = iter++;
1712 if ((*old)->sFieldName == sFieldName &&
1713 (*old)->nControlIndex == nControlIndex) {
1714 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1715 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 }
1717 }
1718
dsinclair82e17672016-10-11 12:38:01 -07001719 for (const auto& pData : DelayDataForFieldAndControlIndex)
1720 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721}
1722
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723CJS_Document* Document::GetCJSDoc() const {
1724 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001725}