blob: 79d68ec3a78e3772373f369a72faebe9634b4723 [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);
804 pRuntime->PutObjectString(pObj, L"Author", cwAuthor);
805 pRuntime->PutObjectString(pObj, L"Title", cwTitle);
806 pRuntime->PutObjectString(pObj, L"Subject", cwSubject);
807 pRuntime->PutObjectString(pObj, L"Keywords", cwKeywords);
808 pRuntime->PutObjectString(pObj, L"Creator", cwCreator);
809 pRuntime->PutObjectString(pObj, L"Producer", cwProducer);
810 pRuntime->PutObjectString(pObj, L"CreationDate", cwCreationDate);
811 pRuntime->PutObjectString(pObj, L"ModDate", cwModDate);
812 pRuntime->PutObjectString(pObj, L"Trapped", cwTrapped);
tonikitooec268762016-08-10 12:29:28 -0700813
814 // It's to be compatible to non-standard info dictionary.
815 for (const auto& it : *pDictionary) {
816 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800817 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700818 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
819 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepezb4694242016-08-15 16:44:55 -0700820 pRuntime->PutObjectString(pObj, wsKey, pValueObj->GetUnicodeText());
tonikitooec268762016-08-10 12:29:28 -0700821 } else if (pValueObj->IsNumber()) {
tsepezb4694242016-08-15 16:44:55 -0700822 pRuntime->PutObjectNumber(pObj, wsKey, (float)pValueObj->GetNumber());
tonikitooec268762016-08-10 12:29:28 -0700823 } else if (pValueObj->IsBoolean()) {
tsepezb4694242016-08-15 16:44:55 -0700824 pRuntime->PutObjectBoolean(pObj, wsKey, !!pValueObj->GetInteger());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700825 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 }
tonikitooec268762016-08-10 12:29:28 -0700827 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700828 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700829}
830
tsepez4cf55152016-11-02 14:37:54 -0700831bool Document::getPropertyInternal(IJS_Context* cc,
832 CJS_PropValue& vp,
833 const CFX_ByteString& propName,
834 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700835 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700836 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700837 return false;
tsepez55be02e2016-09-12 11:21:42 -0700838 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700839 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700841 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842
843 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700844 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700846 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700847 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700848 return false;
tsepez55be02e2016-09-12 11:21:42 -0700849 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700850 CFX_WideString csProperty;
851 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800852 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
853 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700854 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 }
tsepez4cf55152016-11-02 14:37:54 -0700856 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700857}
858
tsepez4cf55152016-11-02 14:37:54 -0700859bool Document::creationDate(IJS_Context* cc,
860 CJS_PropValue& vp,
861 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700862 return getPropertyInternal(cc, vp, "CreationDate", sError);
863}
864
tsepez4cf55152016-11-02 14:37:54 -0700865bool Document::creator(IJS_Context* cc,
866 CJS_PropValue& vp,
867 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700868 return getPropertyInternal(cc, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869}
870
tsepez4cf55152016-11-02 14:37:54 -0700871bool Document::delay(IJS_Context* cc,
872 CJS_PropValue& vp,
873 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700874 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700875 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700876 return false;
tsepez55be02e2016-09-12 11:21:42 -0700877 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 if (vp.IsGetting()) {
879 vp << m_bDelay;
880 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700881 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700882 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700883 return false;
tsepez55be02e2016-09-12 11:21:42 -0700884 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 vp >> m_bDelay;
886 if (m_bDelay) {
Tom Sepez56c10192016-03-15 12:34:17 -0700887 m_DelayData.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888 } else {
Tom Sepez56c10192016-03-15 12:34:17 -0700889 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
890 DelayDataToProcess.swap(m_DelayData);
dsinclair82e17672016-10-11 12:38:01 -0700891 for (const auto& pData : DelayDataToProcess)
892 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 }
894 }
tsepez4cf55152016-11-02 14:37:54 -0700895 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896}
897
tsepez4cf55152016-11-02 14:37:54 -0700898bool Document::keywords(IJS_Context* cc,
899 CJS_PropValue& vp,
900 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700901 return getPropertyInternal(cc, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902}
903
tsepez4cf55152016-11-02 14:37:54 -0700904bool Document::modDate(IJS_Context* cc,
905 CJS_PropValue& vp,
906 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700907 return getPropertyInternal(cc, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908}
909
tsepez4cf55152016-11-02 14:37:54 -0700910bool Document::producer(IJS_Context* cc,
911 CJS_PropValue& vp,
912 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700913 return getPropertyInternal(cc, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914}
915
tsepez4cf55152016-11-02 14:37:54 -0700916bool Document::subject(IJS_Context* cc,
917 CJS_PropValue& vp,
918 CFX_WideString& sError) {
tonikitooea3ff9e2016-08-02 11:52:28 -0700919 return getPropertyInternal(cc, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920}
921
tsepez4cf55152016-11-02 14:37:54 -0700922bool Document::title(IJS_Context* cc,
923 CJS_PropValue& vp,
924 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700925 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700926 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700927 return false;
tsepez55be02e2016-09-12 11:21:42 -0700928 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700929 return getPropertyInternal(cc, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930}
931
tsepez4cf55152016-11-02 14:37:54 -0700932bool Document::numPages(IJS_Context* cc,
933 CJS_PropValue& vp,
934 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700936 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700937 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 }
dsinclair82e17672016-10-11 12:38:01 -0700939 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700940 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700941 return false;
tsepez55be02e2016-09-12 11:21:42 -0700942 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700943 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700944 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945}
946
tsepez4cf55152016-11-02 14:37:54 -0700947bool Document::external(IJS_Context* cc,
948 CJS_PropValue& vp,
949 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700950 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (vp.IsGetting()) {
952 vp << true;
953 }
tsepez4cf55152016-11-02 14:37:54 -0700954 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955}
956
tsepez4cf55152016-11-02 14:37:54 -0700957bool Document::filesize(IJS_Context* cc,
958 CJS_PropValue& vp,
959 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700961 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700962 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 }
964 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700965 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966}
967
tsepez4cf55152016-11-02 14:37:54 -0700968bool Document::mouseX(IJS_Context* cc,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700969 CJS_PropValue& vp,
970 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700971 return true;
972}
973
974bool Document::mouseY(IJS_Context* cc,
975 CJS_PropValue& vp,
976 CFX_WideString& sError) {
977 return true;
978}
979
980bool Document::URL(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700981 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700982 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700983 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700984 }
dsinclair82e17672016-10-11 12:38:01 -0700985 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700986 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700987 return false;
tsepez55be02e2016-09-12 11:21:42 -0700988 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700989 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700990 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700991}
992
tsepez4cf55152016-11-02 14:37:54 -0700993bool Document::baseURL(IJS_Context* cc,
994 CJS_PropValue& vp,
995 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (vp.IsGetting()) {
997 vp << m_cwBaseURL;
998 } else {
999 vp >> m_cwBaseURL;
1000 }
tsepez4cf55152016-11-02 14:37:54 -07001001 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002}
1003
tsepez4cf55152016-11-02 14:37:54 -07001004bool Document::calculate(IJS_Context* cc,
1005 CJS_PropValue& vp,
1006 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001007 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001008 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001009 return false;
tsepez55be02e2016-09-12 11:21:42 -07001010 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001011 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -07001013 vp << !!pInterForm->IsCalculateEnabled();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 } else {
1015 bool bCalculate;
1016 vp >> bCalculate;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 pInterForm->EnableCalculate(bCalculate);
1018 }
tsepez4cf55152016-11-02 14:37:54 -07001019 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020}
1021
tsepez4cf55152016-11-02 14:37:54 -07001022bool Document::documentFileName(IJS_Context* cc,
1023 CJS_PropValue& vp,
1024 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001026 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001027 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 }
dsinclair82e17672016-10-11 12:38:01 -07001029 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001030 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001031 return false;
tsepez55be02e2016-09-12 11:21:42 -07001032 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001033 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 int32_t i = wsFilePath.GetLength() - 1;
1035 for (; i >= 0; i--) {
1036 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1037 break;
1038 }
1039 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1040 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1041 } else {
1042 vp << L"";
1043 }
tsepez4cf55152016-11-02 14:37:54 -07001044 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045}
1046
tsepez4cf55152016-11-02 14:37:54 -07001047bool Document::path(IJS_Context* cc,
1048 CJS_PropValue& vp,
1049 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001051 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001052 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 }
dsinclair82e17672016-10-11 12:38:01 -07001054 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001055 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001056 return false;
tsepez55be02e2016-09-12 11:21:42 -07001057 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001058 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001059 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060}
1061
tsepez4cf55152016-11-02 14:37:54 -07001062bool Document::pageWindowRect(IJS_Context* cc,
1063 CJS_PropValue& vp,
1064 CFX_WideString& sError) {
1065 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066}
1067
tsepez4cf55152016-11-02 14:37:54 -07001068bool Document::layout(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::addLink(IJS_Context* cc,
1075 const std::vector<CJS_Value>& params,
1076 CJS_Value& vRet,
1077 CFX_WideString& sError) {
1078 return true;
1079}
1080
1081bool Document::closeDoc(IJS_Context* cc,
1082 const std::vector<CJS_Value>& params,
1083 CJS_Value& vRet,
1084 CFX_WideString& sError) {
1085 return true;
1086}
1087
1088bool Document::getPageBox(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001089 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001090 CJS_Value& vRet,
1091 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001092 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093}
1094
tsepez4cf55152016-11-02 14:37:54 -07001095bool Document::getAnnot(IJS_Context* cc,
1096 const std::vector<CJS_Value>& params,
1097 CJS_Value& vRet,
1098 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001099 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001100 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001101 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001102 }
dsinclair82e17672016-10-11 12:38:01 -07001103 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001104 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001105 return false;
tsepez55be02e2016-09-12 11:21:42 -07001106 }
tsepezcd5dc852016-09-08 11:23:24 -07001107 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tonikitoo618cb1f2016-08-18 20:10:17 -07001108 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
1109 int nPageNo = params[0].ToInt(pRuntime);
1110 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001111 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001112 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001113 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001114
tsepezd805eec2017-01-11 14:03:54 -08001115 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001116 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001117 for (const auto& pSDKAnnotCur : annotIteration) {
1118 CPDFSDK_BAAnnot* pBAAnnot =
1119 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001120 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1121 pSDKBAAnnot = pBAAnnot;
1122 break;
1123 }
1124 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001125 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001126 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001127
1128 v8::Local<v8::Object> pObj =
1129 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1130 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001131 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001132
1133 CJS_Annot* pJS_Annot =
1134 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1135 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001136 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001137
1138 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1139 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001140 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001141
1142 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001143 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001144 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145}
1146
tsepez4cf55152016-11-02 14:37:54 -07001147bool Document::getAnnots(IJS_Context* cc,
1148 const std::vector<CJS_Value>& params,
1149 CJS_Value& vRet,
1150 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001151 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001152 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001153 return false;
tsepez55be02e2016-09-12 11:21:42 -07001154 }
tonikitoo0ee35902016-08-26 14:41:29 -07001155 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepez55be02e2016-09-12 11:21:42 -07001156 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tonikitoo0ee35902016-08-26 14:41:29 -07001157
1158 // TODO(tonikitoo): Add support supported parameters as per
1159 // the PDF spec.
1160
dsinclair7cbe68e2016-10-12 11:56:23 -07001161 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001162 CJS_Array annots;
1163
1164 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001165 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001166 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001167 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001168
tsepezd805eec2017-01-11 14:03:54 -08001169 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1170 for (const auto& pSDKAnnotCur : annotIteration) {
1171 if (!pSDKAnnotCur) {
1172 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001173 return false;
tsepezd805eec2017-01-11 14:03:54 -08001174 }
tonikitoo0ee35902016-08-26 14:41:29 -07001175 v8::Local<v8::Object> pObj =
1176 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1177 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001178 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001179
1180 CJS_Annot* pJS_Annot =
1181 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1182 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001183 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001184
1185 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1186 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001187 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001188
tsepezd805eec2017-01-11 14:03:54 -08001189 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001190 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1191 }
1192 }
tonikitoo0ee35902016-08-26 14:41:29 -07001193 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001194 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195}
1196
tsepez4cf55152016-11-02 14:37:54 -07001197bool Document::getAnnot3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001198 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 CJS_Value& vRet,
1200 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001201 vRet.SetNull(CJS_Runtime::FromContext(cc));
1202 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203}
1204
tsepez4cf55152016-11-02 14:37:54 -07001205bool Document::getAnnots3D(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -08001206 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207 CJS_Value& vRet,
1208 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001209 return true;
1210}
1211
1212bool Document::getOCGs(IJS_Context* cc,
1213 const std::vector<CJS_Value>& params,
1214 CJS_Value& vRet,
1215 CFX_WideString& sError) {
1216 return true;
1217}
1218
1219bool Document::getLinks(IJS_Context* cc,
1220 const std::vector<CJS_Value>& params,
1221 CJS_Value& vRet,
1222 CFX_WideString& sError) {
1223 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001224}
1225
1226bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1227 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1228 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1229}
1230
tsepez4cf55152016-11-02 14:37:54 -07001231bool Document::addIcon(IJS_Context* cc,
1232 const std::vector<CJS_Value>& params,
1233 CJS_Value& vRet,
1234 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001236 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001237 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 }
tsepezf3dc8c62016-08-10 06:29:29 -07001239
tsepezcd5dc852016-09-08 11:23:24 -07001240 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
tsepezf3dc8c62016-08-10 06:29:29 -07001241 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001242 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepezaecd9d02015-08-31 15:04:12 -07001243
Tom Sepez39bfe122015-09-17 15:25:23 -07001244 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001245 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001246 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001247 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248
tsepezb4694242016-08-15 16:44:55 -07001249 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1250 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001251 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001252 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 }
1254
tsepezb4694242016-08-15 16:44:55 -07001255 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 if (!pEmbedObj) {
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
thestigdadea5f2016-10-19 13:29:48 -07001261 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1262 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001263 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264}
1265
tsepez4cf55152016-11-02 14:37:54 -07001266bool Document::icons(IJS_Context* cc,
1267 CJS_PropValue& vp,
1268 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001269 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001270 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001271 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272 }
1273
tsepezf3dc8c62016-08-10 06:29:29 -07001274 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
thestigdadea5f2016-10-19 13:29:48 -07001275 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001276 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001277 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001278 }
1279
tsepeze5aff742016-08-08 09:49:42 -07001280 CJS_Array Icons;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281
Tom Sepez6ba32d92016-01-05 16:26:32 -08001282 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001283 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001284 v8::Local<v8::Object> pObj =
1285 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001287 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288
tsepezb4694242016-08-15 16:44:55 -07001289 CJS_Icon* pJS_Icon =
1290 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001292 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001293
thestigdadea5f2016-10-19 13:29:48 -07001294 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001295 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001296 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
dsinclair09bad1c2016-10-18 19:26:30 -07001298 pIcon->SetStream(pIconElement->IconStream->GetStream());
1299 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001300 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001301 }
1302
1303 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001304 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305}
1306
tsepez4cf55152016-11-02 14:37:54 -07001307bool Document::getIcon(IJS_Context* cc,
1308 const std::vector<CJS_Value>& params,
1309 CJS_Value& vRet,
1310 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001312 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001313 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 }
1315
thestigdadea5f2016-10-19 13:29:48 -07001316 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001317 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318
tsepezcd5dc852016-09-08 11:23:24 -07001319 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001320 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321
thestigdadea5f2016-10-19 13:29:48 -07001322 for (const auto& pIconElement : m_Icons) {
1323 if (pIconElement->IconName != swIconName)
1324 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325
thestigdadea5f2016-10-19 13:29:48 -07001326 v8::Local<v8::Object> pObj =
1327 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1328 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001329 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330
thestigdadea5f2016-10-19 13:29:48 -07001331 CJS_Icon* pJS_Icon =
1332 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1333 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001334 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001335
thestigdadea5f2016-10-19 13:29:48 -07001336 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1337 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001338 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339
thestigdadea5f2016-10-19 13:29:48 -07001340 pIcon->SetIconName(swIconName);
1341 pIcon->SetStream(pIconElement->IconStream->GetStream());
1342
1343 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001344 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001345 }
1346
tsepez4cf55152016-11-02 14:37:54 -07001347 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348}
1349
tsepez4cf55152016-11-02 14:37:54 -07001350bool Document::removeIcon(IJS_Context* cc,
1351 const std::vector<CJS_Value>& params,
1352 CJS_Value& vRet,
1353 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001354 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001355 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356}
1357
tsepez4cf55152016-11-02 14:37:54 -07001358bool Document::createDataObject(IJS_Context* cc,
1359 const std::vector<CJS_Value>& params,
1360 CJS_Value& vRet,
1361 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001362 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001363 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001364}
1365
tsepez4cf55152016-11-02 14:37:54 -07001366bool Document::media(IJS_Context* cc,
1367 CJS_PropValue& vp,
1368 CFX_WideString& sError) {
1369 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001370}
1371
tsepez4cf55152016-11-02 14:37:54 -07001372bool Document::calculateNow(IJS_Context* cc,
1373 const std::vector<CJS_Value>& params,
1374 CJS_Value& vRet,
1375 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001376 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001377 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001378 return false;
tsepez55be02e2016-09-12 11:21:42 -07001379 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001380 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1381 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1382 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001383 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001384 return false;
tsepez55be02e2016-09-12 11:21:42 -07001385 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001386 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001387 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001388}
1389
tsepez4cf55152016-11-02 14:37:54 -07001390bool Document::Collab(IJS_Context* cc,
1391 CJS_PropValue& vp,
1392 CFX_WideString& sError) {
1393 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001394}
1395
tsepez4cf55152016-11-02 14:37:54 -07001396bool Document::getPageNthWord(IJS_Context* cc,
1397 const std::vector<CJS_Value>& params,
1398 CJS_Value& vRet,
1399 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001400 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001401 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001402 return false;
tsepez55be02e2016-09-12 11:21:42 -07001403 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001404 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001405 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001406 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001407 }
1408 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez55be02e2016-09-12 11:21:42 -07001409
1410 // TODO(tsepez): check maximum allowable params.
1411
tsepezb4694242016-08-15 16:44:55 -07001412 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1413 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1414 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001415
dsinclair7cbe68e2016-10-12 11:56:23 -07001416 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001417 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001418 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001419
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001420 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001421 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001422 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001424
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1426 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001427 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001428
thestig5cc24652016-04-26 11:46:02 -07001429 CPDF_Page page(pDocument, pPageDict, true);
1430 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001431
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001434 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001435 if (pPageObj->IsText()) {
1436 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001437 int nObjWords = CountWords(pTextObj);
1438 if (nWords + nObjWords >= nWordNo) {
1439 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1440 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 }
Tom Sepez2398d892016-02-17 16:46:26 -08001442 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001443 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001445
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 if (bStrip) {
1447 swRet.TrimLeft();
1448 swRet.TrimRight();
1449 }
1450
tsepezf3dc8c62016-08-10 06:29:29 -07001451 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001452 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001453}
1454
tsepez4cf55152016-11-02 14:37:54 -07001455bool Document::getPageNthWordQuads(IJS_Context* cc,
1456 const std::vector<CJS_Value>& params,
1457 CJS_Value& vRet,
1458 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001459 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001460 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001461 return false;
tsepez55be02e2016-09-12 11:21:42 -07001462 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001463 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001464 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001465 return false;
tsepez55be02e2016-09-12 11:21:42 -07001466 }
tsepez4cf55152016-11-02 14:37:54 -07001467 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468}
1469
tsepez4cf55152016-11-02 14:37:54 -07001470bool Document::getPageNumWords(IJS_Context* cc,
1471 const std::vector<CJS_Value>& params,
1472 CJS_Value& vRet,
1473 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001474 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001475 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001476 return false;
tsepez55be02e2016-09-12 11:21:42 -07001477 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001478 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001479 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001480 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001481 }
1482 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001483 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001484 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001486 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001487 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 }
1489
1490 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1491 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001492 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493
thestig5cc24652016-04-26 11:46:02 -07001494 CPDF_Page page(pDocument, pPageDict, true);
1495 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496
1497 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001498 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001499 if (pPageObj->IsText())
1500 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 }
1502
tsepezf3dc8c62016-08-10 06:29:29 -07001503 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001504 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505}
1506
tsepez4cf55152016-11-02 14:37:54 -07001507bool Document::getPrintParams(IJS_Context* cc,
1508 const std::vector<CJS_Value>& params,
1509 CJS_Value& vRet,
1510 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 CJS_Context* pContext = (CJS_Context*)cc;
1512 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
tsepezb4694242016-08-15 16:44:55 -07001513 v8::Local<v8::Object> pRetObj =
1514 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515
1516 // Not implemented yet.
1517
tsepezf3dc8c62016-08-10 06:29:29 -07001518 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001519 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520}
1521
1522#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1523
1524int Document::CountWords(CPDF_TextObject* pTextObj) {
1525 if (!pTextObj)
1526 return 0;
1527
1528 int nWords = 0;
1529
1530 CPDF_Font* pFont = pTextObj->GetFont();
1531 if (!pFont)
1532 return 0;
1533
tsepez4cf55152016-11-02 14:37:54 -07001534 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535
1536 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001537 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 FX_FLOAT kerning;
1539
1540 pTextObj->GetCharInfo(i, charcode, kerning);
1541 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1542
Tom Sepez62a70f92016-03-21 15:00:20 -07001543 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 if (swUnicode.GetLength() > 0)
1545 unicode = swUnicode[0];
1546
1547 if (ISLATINWORD(unicode) && bIsLatin)
1548 continue;
1549
1550 bIsLatin = ISLATINWORD(unicode);
1551 if (unicode != 0x20)
1552 nWords++;
1553 }
1554
1555 return nWords;
1556}
1557
1558CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1559 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 CFX_WideString swRet;
1561
1562 CPDF_Font* pFont = pTextObj->GetFont();
1563 if (!pFont)
1564 return L"";
1565
1566 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001567 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568
1569 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001570 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571 FX_FLOAT kerning;
1572
1573 pTextObj->GetCharInfo(i, charcode, kerning);
1574 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1575
Tom Sepez62a70f92016-03-21 15:00:20 -07001576 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 if (swUnicode.GetLength() > 0)
1578 unicode = swUnicode[0];
1579
1580 if (ISLATINWORD(unicode) && bIsLatin) {
1581 } else {
1582 bIsLatin = ISLATINWORD(unicode);
1583 if (unicode != 0x20)
1584 nWords++;
1585 }
1586
1587 if (nWords - 1 == nWordIndex)
1588 swRet += unicode;
1589 }
1590
1591 return swRet;
1592}
1593
tsepez4cf55152016-11-02 14:37:54 -07001594bool Document::zoom(IJS_Context* cc,
1595 CJS_PropValue& vp,
1596 CFX_WideString& sError) {
1597 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001598}
1599
1600/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001601(none, NoVary)
1602(fitP, FitPage)
1603(fitW, FitWidth)
1604(fitH, FitHeight)
1605(fitV, FitVisibleWidth)
1606(pref, Preferred)
1607(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001608*/
1609
tsepez4cf55152016-11-02 14:37:54 -07001610bool Document::zoomType(IJS_Context* cc,
1611 CJS_PropValue& vp,
1612 CFX_WideString& sError) {
1613 return true;
1614}
1615
1616bool Document::deletePages(IJS_Context* cc,
1617 const std::vector<CJS_Value>& params,
1618 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001619 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001620 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001621 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001622}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001623
tsepez4cf55152016-11-02 14:37:54 -07001624bool Document::extractPages(IJS_Context* cc,
1625 const std::vector<CJS_Value>& params,
1626 CJS_Value& vRet,
1627 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001628 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001629 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001630}
1631
tsepez4cf55152016-11-02 14:37:54 -07001632bool Document::insertPages(IJS_Context* cc,
1633 const std::vector<CJS_Value>& params,
1634 CJS_Value& vRet,
1635 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001636 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001637 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001638}
1639
tsepez4cf55152016-11-02 14:37:54 -07001640bool Document::replacePages(IJS_Context* cc,
1641 const std::vector<CJS_Value>& params,
1642 CJS_Value& vRet,
1643 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001644 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001645 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646}
1647
tsepez4cf55152016-11-02 14:37:54 -07001648bool Document::getURL(IJS_Context* cc,
1649 const std::vector<CJS_Value>& params,
1650 CJS_Value& vRet,
1651 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001653 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001654}
1655
tsepez4cf55152016-11-02 14:37:54 -07001656bool Document::gotoNamedDest(IJS_Context* cc,
1657 const std::vector<CJS_Value>& params,
1658 CJS_Value& vRet,
1659 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001660 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001661 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001662 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001663 }
dsinclair82e17672016-10-11 12:38:01 -07001664 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001665 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001666 return false;
tsepez55be02e2016-09-12 11:21:42 -07001667 }
tsepezcd5dc852016-09-08 11:23:24 -07001668 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -07001669 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001670 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001671 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001672 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001673 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001674
tonikitoo1c836752016-08-08 16:14:05 -07001675 CPDF_NameTree nameTree(pDocument, "Dests");
1676 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1677 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001678 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001679
1680 CPDF_Dest dest(destArray);
1681 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1682
1683 std::unique_ptr<float[]> scrollPositionArray;
1684 int scrollPositionArraySize = 0;
1685
1686 if (arrayObject) {
1687 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1688 int j = 0;
1689 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1690 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1691 scrollPositionArraySize = j;
1692 }
1693
tsepezb4694242016-08-15 16:44:55 -07001694 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001695 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1696 scrollPositionArray.get(),
1697 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001698 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001699
tsepez4cf55152016-11-02 14:37:54 -07001700 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001701}
1702
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001704 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001705}
1706
1707void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1708 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001709 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1710 auto iter = m_DelayData.begin();
1711 while (iter != m_DelayData.end()) {
1712 auto old = iter++;
1713 if ((*old)->sFieldName == sFieldName &&
1714 (*old)->nControlIndex == nControlIndex) {
1715 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1716 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001717 }
1718 }
1719
dsinclair82e17672016-10-11 12:38:01 -07001720 for (const auto& pData : DelayDataForFieldAndControlIndex)
1721 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001722}
1723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001724CJS_Document* Document::GetCJSDoc() const {
1725 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001726}