blob: 4bf1afd5a3af46feee298d8ae57f8839132e61dc [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"
tsepezd805eec2017-01-11 14:03:54 -080020#include "fpdfsdk/cpdfsdk_annotiteration.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();
tsepez8fa82792017-01-11 09:32:33 -0800509 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800510 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
tsepez8fa82792017-01-11 09:32:33 -0800514 for (const auto& pAnnot : widgets) {
515 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
516 if (!pWidget)
517 continue;
518
Tom Sepez281a9ea2016-02-26 14:24:28 -0800519 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800520 --rcAnnot.left;
521 --rcAnnot.bottom;
522 ++rcAnnot.right;
523 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524
tsepezdf964df2016-04-21 12:09:41 -0700525 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800526 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800527 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528
dsinclair1897bdc2016-09-06 14:18:57 -0700529 // If there is currently no pageview associated with the page being used
530 // do not create one. We may be in the process of tearing down the document
531 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700532 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700533 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700534#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700535 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700536#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700537 pPageView->UpdateRects(aRefresh);
538 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700540 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541
tsepez4cf55152016-11-02 14:37:54 -0700542 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543}
544
545// reset filed values within a document.
546// comment:
547// note: if the fields names r not rational, aodbe is dumb for it.
548
tsepez4cf55152016-11-02 14:37:54 -0700549bool Document::resetForm(IJS_Context* cc,
550 const std::vector<CJS_Value>& params,
551 CJS_Value& vRet,
552 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700553 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700554 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700555 return false;
tsepez55be02e2016-09-12 11:21:42 -0700556 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700557 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
558 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
559 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700560 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700561 return false;
tsepez55be02e2016-09-12 11:21:42 -0700562 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563
dsinclair7cbe68e2016-10-12 11:56:23 -0700564 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700566 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567
Lei Zhangdd734de2015-11-11 10:16:52 -0800568 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700569 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700570 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700571 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800572 }
573
tsepez55be02e2016-09-12 11:21:42 -0700574 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -0700575 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezf3dc8c62016-08-10 06:29:29 -0700576
Lei Zhangd88a3642015-11-10 09:38:57 -0800577 switch (params[0].GetType()) {
578 default:
tsepezb4694242016-08-15 16:44:55 -0700579 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800580 break;
581 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700582 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800583 break;
584 }
585
586 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700587 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800588 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700589 aName.GetElement(pRuntime, i, valElement);
590 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800591 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
592 aFields.push_back(pPDFForm->GetField(j, swVal));
593 }
594
595 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700596 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700597 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598 }
599
tsepez4cf55152016-11-02 14:37:54 -0700600 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601}
602
tsepez4cf55152016-11-02 14:37:54 -0700603bool Document::saveAs(IJS_Context* cc,
604 const std::vector<CJS_Value>& params,
605 CJS_Value& vRet,
606 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700608 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609}
610
tsepez4cf55152016-11-02 14:37:54 -0700611bool Document::syncAnnotScan(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800612 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 CJS_Value& vRet,
614 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700615 return true;
616}
617
618bool Document::submitForm(IJS_Context* cc,
619 const std::vector<CJS_Value>& params,
620 CJS_Value& vRet,
621 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 int nSize = params.size();
623 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700624 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700625 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626 }
dsinclair82e17672016-10-11 12:38:01 -0700627 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700628 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700629 return false;
tsepez55be02e2016-09-12 11:21:42 -0700630 }
tsepezcd5dc852016-09-08 11:23:24 -0700631 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
632 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepeze5aff742016-08-08 09:49:42 -0700633 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700635 bool bFDF = true;
636 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700638 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700639 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700641 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700643 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700645 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700646 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700647 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
648 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700650 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700651
tsepezb4694242016-08-15 16:44:55 -0700652 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
653 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700654
tsepezb4694242016-08-15 16:44:55 -0700655 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
656 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700657
tsepezb4694242016-08-15 16:44:55 -0700658 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
659 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 }
661
dsinclair7cbe68e2016-10-12 11:56:23 -0700662 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700664 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800665 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700667 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700669 }
tsepez4cf55152016-11-02 14:37:54 -0700670 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 }
672
Lei Zhangd88a3642015-11-10 09:38:57 -0800673 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700674 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700675 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700676 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677
tsepezb4694242016-08-15 16:44:55 -0700678 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
680 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
681 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
682 if (!bEmpty && pField->GetValue().IsEmpty())
683 continue;
684
Lei Zhangd88a3642015-11-10 09:38:57 -0800685 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 }
687 }
688
Lei Zhangd88a3642015-11-10 09:38:57 -0800689 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800691 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692 pRuntime->EndBlock();
693 }
tsepez4cf55152016-11-02 14:37:54 -0700694 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695}
696
dsinclair82e17672016-10-11 12:38:01 -0700697void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
698 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700699}
700
tsepez4cf55152016-11-02 14:37:54 -0700701bool Document::bookmarkRoot(IJS_Context* cc,
702 CJS_PropValue& vp,
703 CFX_WideString& sError) {
704 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705}
706
tsepez4cf55152016-11-02 14:37:54 -0700707bool Document::mailDoc(IJS_Context* cc,
708 const std::vector<CJS_Value>& params,
709 CJS_Value& vRet,
710 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700711 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
712
713 // TODO(tsepez): Check maximum number of allowed params.
714
tsepez4cf55152016-11-02 14:37:54 -0700715 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716 CFX_WideString cTo = L"";
717 CFX_WideString cCc = L"";
718 CFX_WideString cBcc = L"";
719 CFX_WideString cSubject = L"";
720 CFX_WideString cMsg = L"";
721
tsepezf3dc8c62016-08-10 06:29:29 -0700722 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723
tsepezf3dc8c62016-08-10 06:29:29 -0700724 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700725 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700726 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700727 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700728 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700729 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700730 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700731 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700732 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700733 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700734 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700735 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736
Tom Sepez39bfe122015-09-17 15:25:23 -0700737 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700738 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739
tsepezb4694242016-08-15 16:44:55 -0700740 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700741 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742
tsepezb4694242016-08-15 16:44:55 -0700743 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
744 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745
tsepezb4694242016-08-15 16:44:55 -0700746 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
747 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700748
tsepezb4694242016-08-15 16:44:55 -0700749 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
750 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751
tsepezb4694242016-08-15 16:44:55 -0700752 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
753 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754
tsepezb4694242016-08-15 16:44:55 -0700755 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
756 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 }
758
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700760 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
761 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
762 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 pRuntime->EndBlock();
764
tsepez4cf55152016-11-02 14:37:54 -0700765 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766}
767
tsepez4cf55152016-11-02 14:37:54 -0700768bool Document::author(IJS_Context* cc,
769 CJS_PropValue& vp,
770 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700771 return getPropertyInternal(cc, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700772}
773
tsepez4cf55152016-11-02 14:37:54 -0700774bool Document::info(IJS_Context* cc,
775 CJS_PropValue& vp,
776 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700777 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700778 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700779 return false;
tonikitooec268762016-08-10 12:29:28 -0700780 }
dsinclair82e17672016-10-11 12:38:01 -0700781 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700782 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700783 return false;
tsepez55be02e2016-09-12 11:21:42 -0700784 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700785 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700786 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700787 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800788
dsinclair38fd8442016-09-15 10:15:32 -0700789 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
790 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
791 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
792 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
793 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
794 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
795 CFX_WideString cwCreationDate =
796 pDictionary->GetUnicodeTextFor("CreationDate");
797 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
798 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800799
tonikitooec268762016-08-10 12:29:28 -0700800 CJS_Context* pContext = (CJS_Context*)cc;
801 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
Tom Sepezc6ab1722015-02-05 15:27:25 -0800802
tsepezb4694242016-08-15 16:44:55 -0700803 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
tsepeze6cf0132017-01-18 14:38:18 -0800804 pRuntime->PutObjectProperty(pObj, L"Author", pRuntime->NewString(cwAuthor));
805 pRuntime->PutObjectProperty(pObj, L"Title", pRuntime->NewString(cwTitle));
806 pRuntime->PutObjectProperty(pObj, L"Subject", pRuntime->NewString(cwSubject));
807 pRuntime->PutObjectProperty(pObj, L"Keywords",
808 pRuntime->NewString(cwKeywords));
809 pRuntime->PutObjectProperty(pObj, L"Creator", pRuntime->NewString(cwCreator));
810 pRuntime->PutObjectProperty(pObj, L"Producer",
811 pRuntime->NewString(cwProducer));
812 pRuntime->PutObjectProperty(pObj, L"CreationDate",
813 pRuntime->NewString(cwCreationDate));
814 pRuntime->PutObjectProperty(pObj, L"ModDate", pRuntime->NewString(cwModDate));
815 pRuntime->PutObjectProperty(pObj, L"Trapped", pRuntime->NewString(cwTrapped));
tonikitooec268762016-08-10 12:29:28 -0700816
817 // It's to be compatible to non-standard info dictionary.
818 for (const auto& it : *pDictionary) {
819 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800820 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700821 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
822 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800823 pRuntime->PutObjectProperty(
824 pObj, wsKey, pRuntime->NewString(pValueObj->GetUnicodeText()));
tonikitooec268762016-08-10 12:29:28 -0700825 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800826 pRuntime->PutObjectProperty(pObj, wsKey,
827 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700828 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800829 pRuntime->PutObjectProperty(
830 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700831 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 }
tonikitooec268762016-08-10 12:29:28 -0700833 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700834 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700835}
836
tsepez4cf55152016-11-02 14:37:54 -0700837bool Document::getPropertyInternal(IJS_Context* cc,
838 CJS_PropValue& vp,
839 const CFX_ByteString& propName,
840 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700841 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700842 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700843 return false;
tsepez55be02e2016-09-12 11:21:42 -0700844 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700845 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700847 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848
849 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700850 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700852 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700853 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700854 return false;
tsepez55be02e2016-09-12 11:21:42 -0700855 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700856 CFX_WideString csProperty;
857 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800858 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
859 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700860 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 }
tsepez4cf55152016-11-02 14:37:54 -0700862 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863}
864
tsepez4cf55152016-11-02 14:37:54 -0700865bool Document::creationDate(IJS_Context* cc,
866 CJS_PropValue& vp,
867 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700868 return getPropertyInternal(cc, vp, "CreationDate", sError);
869}
870
tsepez4cf55152016-11-02 14:37:54 -0700871bool Document::creator(IJS_Context* cc,
872 CJS_PropValue& vp,
873 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700874 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700875}
876
tsepez4cf55152016-11-02 14:37:54 -0700877bool Document::delay(IJS_Context* cc,
878 CJS_PropValue& vp,
879 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700880 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700881 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700882 return false;
tsepez55be02e2016-09-12 11:21:42 -0700883 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 if (vp.IsGetting()) {
885 vp << m_bDelay;
886 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700887 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700888 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700889 return false;
tsepez55be02e2016-09-12 11:21:42 -0700890 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 vp >> m_bDelay;
892 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700893 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700895 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
896 DelayDataToProcess.swap(m_DelayData);
dsinclair82e17672016-10-11 12:38:01 -0700897 for (const auto& pData : DelayDataToProcess)
898 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 }
900 }
tsepez4cf55152016-11-02 14:37:54 -0700901 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902}
903
tsepez4cf55152016-11-02 14:37:54 -0700904bool Document::keywords(IJS_Context* cc,
905 CJS_PropValue& vp,
906 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700907 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908}
909
tsepez4cf55152016-11-02 14:37:54 -0700910bool Document::modDate(IJS_Context* cc,
911 CJS_PropValue& vp,
912 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700913 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914}
915
tsepez4cf55152016-11-02 14:37:54 -0700916bool Document::producer(IJS_Context* cc,
917 CJS_PropValue& vp,
918 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700919 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920}
921
tsepez4cf55152016-11-02 14:37:54 -0700922bool Document::subject(IJS_Context* cc,
923 CJS_PropValue& vp,
924 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700925 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926}
927
tsepez4cf55152016-11-02 14:37:54 -0700928bool Document::title(IJS_Context* cc,
929 CJS_PropValue& vp,
930 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700931 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700932 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700933 return false;
tsepez55be02e2016-09-12 11:21:42 -0700934 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700935 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936}
937
tsepez4cf55152016-11-02 14:37:54 -0700938bool Document::numPages(IJS_Context* cc,
939 CJS_PropValue& vp,
940 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700942 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700943 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 }
dsinclair82e17672016-10-11 12:38:01 -0700945 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700946 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700947 return false;
tsepez55be02e2016-09-12 11:21:42 -0700948 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700949 vp << m_pFormFillEnv->GetPageCount();
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::external(IJS_Context* cc,
954 CJS_PropValue& vp,
955 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700956 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 if (vp.IsGetting()) {
958 vp << true;
959 }
tsepez4cf55152016-11-02 14:37:54 -0700960 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961}
962
tsepez4cf55152016-11-02 14:37:54 -0700963bool Document::filesize(IJS_Context* cc,
964 CJS_PropValue& vp,
965 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700967 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700968 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 }
970 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700971 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972}
973
tsepez4cf55152016-11-02 14:37:54 -0700974bool Document::mouseX(IJS_Context* cc,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700975 CJS_PropValue& vp,
976 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700977 return true;
978}
979
980bool Document::mouseY(IJS_Context* cc,
981 CJS_PropValue& vp,
982 CFX_WideString& sError) {
983 return true;
984}
985
986bool Document::URL(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700987 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700988 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700989 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700990 }
dsinclair82e17672016-10-11 12:38:01 -0700991 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700992 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700993 return false;
tsepez55be02e2016-09-12 11:21:42 -0700994 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700995 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700996 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700997}
998
tsepez4cf55152016-11-02 14:37:54 -0700999bool Document::baseURL(IJS_Context* cc,
1000 CJS_PropValue& vp,
1001 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 if (vp.IsGetting()) {
1003 vp << m_cwBaseURL;
1004 } else {
1005 vp >> m_cwBaseURL;
1006 }
tsepez4cf55152016-11-02 14:37:54 -07001007 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008}
1009
tsepez4cf55152016-11-02 14:37:54 -07001010bool Document::calculate(IJS_Context* cc,
1011 CJS_PropValue& vp,
1012 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001013 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001014 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001015 return false;
tsepez55be02e2016-09-12 11:21:42 -07001016 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001017 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001019 vp << !!pInterForm->IsCalculateEnabled();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 } else {
1021 bool bCalculate;
1022 vp >> bCalculate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 pInterForm->EnableCalculate(bCalculate);
1024 }
tsepez4cf55152016-11-02 14:37:54 -07001025 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026}
1027
tsepez4cf55152016-11-02 14:37:54 -07001028bool Document::documentFileName(IJS_Context* cc,
1029 CJS_PropValue& vp,
1030 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001032 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001033 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 }
dsinclair82e17672016-10-11 12:38:01 -07001035 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001036 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001037 return false;
tsepez55be02e2016-09-12 11:21:42 -07001038 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001039 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 int32_t i = wsFilePath.GetLength() - 1;
1041 for (; i >= 0; i--) {
1042 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1043 break;
1044 }
1045 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1046 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1047 } else {
1048 vp << L"";
1049 }
tsepez4cf55152016-11-02 14:37:54 -07001050 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051}
1052
tsepez4cf55152016-11-02 14:37:54 -07001053bool Document::path(IJS_Context* cc,
1054 CJS_PropValue& vp,
1055 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001057 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001058 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 }
dsinclair82e17672016-10-11 12:38:01 -07001060 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001061 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001062 return false;
tsepez55be02e2016-09-12 11:21:42 -07001063 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001064 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001065 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066}
1067
tsepez4cf55152016-11-02 14:37:54 -07001068bool Document::pageWindowRect(IJS_Context* cc,
1069 CJS_PropValue& vp,
1070 CFX_WideString& sError) {
1071 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072}
1073
tsepez4cf55152016-11-02 14:37:54 -07001074bool Document::layout(IJS_Context* cc,
1075 CJS_PropValue& vp,
1076 CFX_WideString& sError) {
1077 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078}
1079
tsepez4cf55152016-11-02 14:37:54 -07001080bool Document::addLink(IJS_Context* cc,
1081 const std::vector<CJS_Value>& params,
1082 CJS_Value& vRet,
1083 CFX_WideString& sError) {
1084 return true;
1085}
1086
1087bool Document::closeDoc(IJS_Context* cc,
1088 const std::vector<CJS_Value>& params,
1089 CJS_Value& vRet,
1090 CFX_WideString& sError) {
1091 return true;
1092}
1093
1094bool Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001095 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 CJS_Value& vRet,
1097 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001098 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099}
1100
tsepez4cf55152016-11-02 14:37:54 -07001101bool Document::getAnnot(IJS_Context* cc,
1102 const std::vector<CJS_Value>& params,
1103 CJS_Value& vRet,
1104 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001105 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001106 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001107 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001108 }
dsinclair82e17672016-10-11 12:38:01 -07001109 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001110 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001111 return false;
tsepez55be02e2016-09-12 11:21:42 -07001112 }
tsepezcd5dc852016-09-08 11:23:24 -07001113 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001114 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1115 int nPageNo = params[0].ToInt(pRuntime);
1116 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001117 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001118 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001119 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001120
tsepezd805eec2017-01-11 14:03:54 -08001121 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001122 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001123 for (const auto& pSDKAnnotCur : annotIteration) {
1124 CPDFSDK_BAAnnot* pBAAnnot =
1125 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001126 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1127 pSDKBAAnnot = pBAAnnot;
1128 break;
1129 }
1130 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001131 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001132 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001133
1134 v8::Local<v8::Object> pObj =
1135 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1136 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001137 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001138
1139 CJS_Annot* pJS_Annot =
1140 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1141 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001142 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001143
1144 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1145 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001146 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001147
1148 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001149 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001150 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151}
1152
tsepez4cf55152016-11-02 14:37:54 -07001153bool Document::getAnnots(IJS_Context* cc,
1154 const std::vector<CJS_Value>& params,
1155 CJS_Value& vRet,
1156 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001157 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001158 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001159 return false;
tsepez55be02e2016-09-12 11:21:42 -07001160 }
tonikitoo0ee35902016-08-26 14:41:29 -07001161 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepez55be02e2016-09-12 11:21:42 -07001162 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tonikitoo0ee35902016-08-26 14:41:29 -07001163
1164 // TODO(tonikitoo): Add support supported parameters as per
1165 // the PDF spec.
1166
dsinclair7cbe68e2016-10-12 11:56:23 -07001167 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001168 CJS_Array annots;
1169
1170 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001171 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001172 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001173 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001174
tsepezd805eec2017-01-11 14:03:54 -08001175 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1176 for (const auto& pSDKAnnotCur : annotIteration) {
1177 if (!pSDKAnnotCur) {
1178 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001179 return false;
tsepezd805eec2017-01-11 14:03:54 -08001180 }
tonikitoo0ee35902016-08-26 14:41:29 -07001181 v8::Local<v8::Object> pObj =
1182 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1183 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001184 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001185
1186 CJS_Annot* pJS_Annot =
1187 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1188 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001189 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001190
1191 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1192 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001193 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001194
tsepezd805eec2017-01-11 14:03:54 -08001195 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001196 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1197 }
1198 }
tonikitoo0ee35902016-08-26 14:41:29 -07001199 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001200 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201}
1202
tsepez4cf55152016-11-02 14:37:54 -07001203bool Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001204 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 CJS_Value& vRet,
1206 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001207 vRet.SetNull(CJS_Runtime::FromContext(cc));
1208 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209}
1210
tsepez4cf55152016-11-02 14:37:54 -07001211bool Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001212 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 CJS_Value& vRet,
1214 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001215 return true;
1216}
1217
1218bool Document::getOCGs(IJS_Context* cc,
1219 const std::vector<CJS_Value>& params,
1220 CJS_Value& vRet,
1221 CFX_WideString& sError) {
1222 return true;
1223}
1224
1225bool Document::getLinks(IJS_Context* cc,
1226 const std::vector<CJS_Value>& params,
1227 CJS_Value& vRet,
1228 CFX_WideString& sError) {
1229 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230}
1231
1232bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1233 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1234 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1235}
1236
tsepez4cf55152016-11-02 14:37:54 -07001237bool Document::addIcon(IJS_Context* cc,
1238 const std::vector<CJS_Value>& params,
1239 CJS_Value& vRet,
1240 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001242 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001243 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001244 }
tsepezf3dc8c62016-08-10 06:29:29 -07001245
tsepezcd5dc852016-09-08 11:23:24 -07001246 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001247 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001248 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001249
Tom Sepez39bfe122015-09-17 15:25:23 -07001250 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001251 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001252 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001253 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254
tsepezb4694242016-08-15 16:44:55 -07001255 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1256 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001257 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001258 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259 }
1260
tsepezb4694242016-08-15 16:44:55 -07001261 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001263 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001264 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 }
1266
thestigdadea5f2016-10-19 13:29:48 -07001267 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1268 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001269 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270}
1271
tsepez4cf55152016-11-02 14:37:54 -07001272bool Document::icons(IJS_Context* cc,
1273 CJS_PropValue& vp,
1274 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001275 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001276 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001277 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278 }
1279
tsepezf3dc8c62016-08-10 06:29:29 -07001280 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
thestigdadea5f2016-10-19 13:29:48 -07001281 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001282 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001283 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284 }
1285
tsepeze5aff742016-08-08 09:49:42 -07001286 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287
Tom Sepez6ba32d92016-01-05 16:26:32 -08001288 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001289 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001290 v8::Local<v8::Object> pObj =
1291 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001293 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294
tsepezb4694242016-08-15 16:44:55 -07001295 CJS_Icon* pJS_Icon =
1296 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001298 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299
thestigdadea5f2016-10-19 13:29:48 -07001300 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001302 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303
dsinclair09bad1c2016-10-18 19:26:30 -07001304 pIcon->SetStream(pIconElement->IconStream->GetStream());
1305 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001306 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001307 }
1308
1309 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001310 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311}
1312
tsepez4cf55152016-11-02 14:37:54 -07001313bool Document::getIcon(IJS_Context* cc,
1314 const std::vector<CJS_Value>& params,
1315 CJS_Value& vRet,
1316 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001318 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001319 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001320 }
1321
thestigdadea5f2016-10-19 13:29:48 -07001322 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001323 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324
tsepezcd5dc852016-09-08 11:23:24 -07001325 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001326 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001327
thestigdadea5f2016-10-19 13:29:48 -07001328 for (const auto& pIconElement : m_Icons) {
1329 if (pIconElement->IconName != swIconName)
1330 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331
thestigdadea5f2016-10-19 13:29:48 -07001332 v8::Local<v8::Object> pObj =
1333 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1334 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001335 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001336
thestigdadea5f2016-10-19 13:29:48 -07001337 CJS_Icon* pJS_Icon =
1338 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1339 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001340 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001341
thestigdadea5f2016-10-19 13:29:48 -07001342 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1343 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001344 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345
thestigdadea5f2016-10-19 13:29:48 -07001346 pIcon->SetIconName(swIconName);
1347 pIcon->SetStream(pIconElement->IconStream->GetStream());
1348
1349 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001350 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001351 }
1352
tsepez4cf55152016-11-02 14:37:54 -07001353 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354}
1355
tsepez4cf55152016-11-02 14:37:54 -07001356bool Document::removeIcon(IJS_Context* cc,
1357 const std::vector<CJS_Value>& params,
1358 CJS_Value& vRet,
1359 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001360 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001361 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362}
1363
tsepez4cf55152016-11-02 14:37:54 -07001364bool Document::createDataObject(IJS_Context* cc,
1365 const std::vector<CJS_Value>& params,
1366 CJS_Value& vRet,
1367 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001368 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001369 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001370}
1371
tsepez4cf55152016-11-02 14:37:54 -07001372bool Document::media(IJS_Context* cc,
1373 CJS_PropValue& vp,
1374 CFX_WideString& sError) {
1375 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001376}
1377
tsepez4cf55152016-11-02 14:37:54 -07001378bool Document::calculateNow(IJS_Context* cc,
1379 const std::vector<CJS_Value>& params,
1380 CJS_Value& vRet,
1381 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001382 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001383 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001384 return false;
tsepez55be02e2016-09-12 11:21:42 -07001385 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001386 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1387 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1388 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001389 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001390 return false;
tsepez55be02e2016-09-12 11:21:42 -07001391 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001392 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001393 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001394}
1395
tsepez4cf55152016-11-02 14:37:54 -07001396bool Document::Collab(IJS_Context* cc,
1397 CJS_PropValue& vp,
1398 CFX_WideString& sError) {
1399 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001400}
1401
tsepez4cf55152016-11-02 14:37:54 -07001402bool Document::getPageNthWord(IJS_Context* cc,
1403 const std::vector<CJS_Value>& params,
1404 CJS_Value& vRet,
1405 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001406 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001407 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001408 return false;
tsepez55be02e2016-09-12 11:21:42 -07001409 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001410 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001411 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001412 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001413 }
1414 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001415
1416 // TODO(tsepez): check maximum allowable params.
1417
tsepezb4694242016-08-15 16:44:55 -07001418 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1419 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1420 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001421
dsinclair7cbe68e2016-10-12 11:56:23 -07001422 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001424 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001425
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001427 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001428 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001430
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1432 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001433 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001434
thestig5cc24652016-04-26 11:46:02 -07001435 CPDF_Page page(pDocument, pPageDict, true);
1436 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001437
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001440 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001441 if (pPageObj->IsText()) {
1442 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001443 int nObjWords = CountWords(pTextObj);
1444 if (nWords + nObjWords >= nWordNo) {
1445 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1446 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001447 }
Tom Sepez2398d892016-02-17 16:46:26 -08001448 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001449 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001450 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001451
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 if (bStrip) {
1453 swRet.TrimLeft();
1454 swRet.TrimRight();
1455 }
1456
tsepezf3dc8c62016-08-10 06:29:29 -07001457 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001458 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001459}
1460
tsepez4cf55152016-11-02 14:37:54 -07001461bool Document::getPageNthWordQuads(IJS_Context* cc,
1462 const std::vector<CJS_Value>& params,
1463 CJS_Value& vRet,
1464 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001465 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001466 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001467 return false;
tsepez55be02e2016-09-12 11:21:42 -07001468 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001469 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001470 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001471 return false;
tsepez55be02e2016-09-12 11:21:42 -07001472 }
tsepez4cf55152016-11-02 14:37:54 -07001473 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474}
1475
tsepez4cf55152016-11-02 14:37:54 -07001476bool Document::getPageNumWords(IJS_Context* cc,
1477 const std::vector<CJS_Value>& params,
1478 CJS_Value& vRet,
1479 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001480 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001481 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001482 return false;
tsepez55be02e2016-09-12 11:21:42 -07001483 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001484 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001485 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001486 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001487 }
1488 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001489 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001490 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001492 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001493 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 }
1495
1496 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1497 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001498 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499
thestig5cc24652016-04-26 11:46:02 -07001500 CPDF_Page page(pDocument, pPageDict, true);
1501 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502
1503 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001504 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001505 if (pPageObj->IsText())
1506 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507 }
1508
tsepezf3dc8c62016-08-10 06:29:29 -07001509 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001510 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511}
1512
tsepez4cf55152016-11-02 14:37:54 -07001513bool Document::getPrintParams(IJS_Context* cc,
1514 const std::vector<CJS_Value>& params,
1515 CJS_Value& vRet,
1516 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 CJS_Context* pContext = (CJS_Context*)cc;
1518 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001519 v8::Local<v8::Object> pRetObj =
1520 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521
1522 // Not implemented yet.
1523
tsepezf3dc8c62016-08-10 06:29:29 -07001524 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001525 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001526}
1527
1528#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1529
1530int Document::CountWords(CPDF_TextObject* pTextObj) {
1531 if (!pTextObj)
1532 return 0;
1533
1534 int nWords = 0;
1535
1536 CPDF_Font* pFont = pTextObj->GetFont();
1537 if (!pFont)
1538 return 0;
1539
tsepez4cf55152016-11-02 14:37:54 -07001540 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541
1542 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001543 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 FX_FLOAT kerning;
1545
1546 pTextObj->GetCharInfo(i, charcode, kerning);
1547 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1548
Tom Sepez62a70f92016-03-21 15:00:20 -07001549 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001550 if (swUnicode.GetLength() > 0)
1551 unicode = swUnicode[0];
1552
1553 if (ISLATINWORD(unicode) && bIsLatin)
1554 continue;
1555
1556 bIsLatin = ISLATINWORD(unicode);
1557 if (unicode != 0x20)
1558 nWords++;
1559 }
1560
1561 return nWords;
1562}
1563
1564CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1565 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001566 CFX_WideString swRet;
1567
1568 CPDF_Font* pFont = pTextObj->GetFont();
1569 if (!pFont)
1570 return L"";
1571
1572 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001573 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574
1575 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001576 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 FX_FLOAT kerning;
1578
1579 pTextObj->GetCharInfo(i, charcode, kerning);
1580 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1581
Tom Sepez62a70f92016-03-21 15:00:20 -07001582 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001583 if (swUnicode.GetLength() > 0)
1584 unicode = swUnicode[0];
1585
1586 if (ISLATINWORD(unicode) && bIsLatin) {
1587 } else {
1588 bIsLatin = ISLATINWORD(unicode);
1589 if (unicode != 0x20)
1590 nWords++;
1591 }
1592
1593 if (nWords - 1 == nWordIndex)
1594 swRet += unicode;
1595 }
1596
1597 return swRet;
1598}
1599
tsepez4cf55152016-11-02 14:37:54 -07001600bool Document::zoom(IJS_Context* cc,
1601 CJS_PropValue& vp,
1602 CFX_WideString& sError) {
1603 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001604}
1605
1606/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001607(none, NoVary)
1608(fitP, FitPage)
1609(fitW, FitWidth)
1610(fitH, FitHeight)
1611(fitV, FitVisibleWidth)
1612(pref, Preferred)
1613(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001614*/
1615
tsepez4cf55152016-11-02 14:37:54 -07001616bool Document::zoomType(IJS_Context* cc,
1617 CJS_PropValue& vp,
1618 CFX_WideString& sError) {
1619 return true;
1620}
1621
1622bool Document::deletePages(IJS_Context* cc,
1623 const std::vector<CJS_Value>& params,
1624 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001625 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001626 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001627 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001628}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001629
tsepez4cf55152016-11-02 14:37:54 -07001630bool Document::extractPages(IJS_Context* cc,
1631 const std::vector<CJS_Value>& params,
1632 CJS_Value& vRet,
1633 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001634 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001635 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636}
1637
tsepez4cf55152016-11-02 14:37:54 -07001638bool Document::insertPages(IJS_Context* cc,
1639 const std::vector<CJS_Value>& params,
1640 CJS_Value& vRet,
1641 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001642 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001643 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644}
1645
tsepez4cf55152016-11-02 14:37:54 -07001646bool Document::replacePages(IJS_Context* cc,
1647 const std::vector<CJS_Value>& params,
1648 CJS_Value& vRet,
1649 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001650 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001651 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652}
1653
tsepez4cf55152016-11-02 14:37:54 -07001654bool Document::getURL(IJS_Context* cc,
1655 const std::vector<CJS_Value>& params,
1656 CJS_Value& vRet,
1657 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001658 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001659 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660}
1661
tsepez4cf55152016-11-02 14:37:54 -07001662bool Document::gotoNamedDest(IJS_Context* cc,
1663 const std::vector<CJS_Value>& params,
1664 CJS_Value& vRet,
1665 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001666 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001667 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001668 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001669 }
dsinclair82e17672016-10-11 12:38:01 -07001670 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001671 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001672 return false;
tsepez55be02e2016-09-12 11:21:42 -07001673 }
tsepezcd5dc852016-09-08 11:23:24 -07001674 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001675 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001676 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001677 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001678 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001679 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001680
tonikitoo1c836752016-08-08 16:14:05 -07001681 CPDF_NameTree nameTree(pDocument, "Dests");
1682 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1683 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001684 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001685
1686 CPDF_Dest dest(destArray);
1687 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1688
1689 std::unique_ptr<float[]> scrollPositionArray;
1690 int scrollPositionArraySize = 0;
1691
1692 if (arrayObject) {
1693 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1694 int j = 0;
1695 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1696 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1697 scrollPositionArraySize = j;
1698 }
1699
tsepezb4694242016-08-15 16:44:55 -07001700 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001701 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1702 scrollPositionArray.get(),
1703 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001704 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001705
tsepez4cf55152016-11-02 14:37:54 -07001706 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001707}
1708
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001710 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711}
1712
1713void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1714 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001715 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1716 auto iter = m_DelayData.begin();
1717 while (iter != m_DelayData.end()) {
1718 auto old = iter++;
1719 if ((*old)->sFieldName == sFieldName &&
1720 (*old)->nControlIndex == nControlIndex) {
1721 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1722 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 }
1724 }
1725
dsinclair82e17672016-10-11 12:38:01 -07001726 for (const auto& pData : DelayDataForFieldAndControlIndex)
1727 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001728}
1729
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001730CJS_Document* Document::GetCJSDoc() const {
1731 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001732}