blob: 38a4c121771bc5523458fd01fe2985d4d231c4c2 [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
Lei Zhang313c4252017-02-27 16:37:36 -08009#include <algorithm>
thestig7c292e02016-09-28 14:14:26 -070010#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
12
dsinclairbc5e6d22016-10-04 11:08:49 -070013#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#include "core/fpdfapi/parser/cpdf_array.h"
16#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080017#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair488b7ad2016-10-04 11:55:50 -070018#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070019#include "core/fpdfdoc/cpdf_interform.h"
20#include "core/fpdfdoc/cpdf_nametree.h"
tsepezd805eec2017-01-11 14:03:54 -080021#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070022#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070023#include "fpdfsdk/cpdfsdk_interform.h"
24#include "fpdfsdk/cpdfsdk_pageview.h"
25#include "fpdfsdk/cpdfsdk_widget.h"
tonikitoo618cb1f2016-08-18 20:10:17 -070026#include "fpdfsdk/javascript/Annot.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040027#include "fpdfsdk/javascript/Field.h"
28#include "fpdfsdk/javascript/Icon.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040029#include "fpdfsdk/javascript/JS_Define.h"
30#include "fpdfsdk/javascript/JS_EventHandler.h"
31#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040032#include "fpdfsdk/javascript/JS_Value.h"
33#include "fpdfsdk/javascript/app.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080034#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070035#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040036#include "fpdfsdk/javascript/resource.h"
Lei Zhang8241df72015-11-06 14:38:48 -080037#include "third_party/base/numerics/safe_math.h"
thestigdadea5f2016-10-19 13:29:48 -070038#include "third_party/base/ptr_util.h"
Chris Palmer9108ad22014-06-26 16:01:46 -070039
Tom Sepez04557b82017-02-16 09:43:10 -080040JSConstSpec CJS_PrintParamsObj::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Tom Sepez04557b82017-02-16 09:43:10 -080042JSPropertySpec CJS_PrintParamsObj::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Tom Sepez04557b82017-02-16 09:43:10 -080044JSMethodSpec CJS_PrintParamsObj::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
46IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
47
48PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070050 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 nStart = 0;
52 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070053 bSilent = false;
54 bShrinkToFit = false;
55 bPrintAsImage = false;
56 bReverse = false;
57 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061#define MINHEIGHT 5.0f
62
Tom Sepez04557b82017-02-16 09:43:10 -080063JSConstSpec CJS_Document::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Tom Sepez04557b82017-02-16 09:43:10 -080065JSPropertySpec CJS_Document::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080066 {"ADBE", get_ADBE_static, set_ADBE_static},
67 {"author", get_author_static, set_author_static},
68 {"baseURL", get_baseURL_static, set_baseURL_static},
69 {"bookmarkRoot", get_bookmarkRoot_static, set_bookmarkRoot_static},
70 {"calculate", get_calculate_static, set_calculate_static},
71 {"Collab", get_Collab_static, set_Collab_static},
72 {"creationDate", get_creationDate_static, set_creationDate_static},
73 {"creator", get_creator_static, set_creator_static},
74 {"delay", get_delay_static, set_delay_static},
75 {"dirty", get_dirty_static, set_dirty_static},
76 {"documentFileName", get_documentFileName_static,
Tom Sepez04557b82017-02-16 09:43:10 -080077 set_documentFileName_static},
Tom Sepez4d5b8c52017-02-21 15:17:07 -080078 {"external", get_external_static, set_external_static},
79 {"filesize", get_filesize_static, set_filesize_static},
80 {"icons", get_icons_static, set_icons_static},
81 {"info", get_info_static, set_info_static},
82 {"keywords", get_keywords_static, set_keywords_static},
83 {"layout", get_layout_static, set_layout_static},
84 {"media", get_media_static, set_media_static},
85 {"modDate", get_modDate_static, set_modDate_static},
86 {"mouseX", get_mouseX_static, set_mouseX_static},
87 {"mouseY", get_mouseY_static, set_mouseY_static},
88 {"numFields", get_numFields_static, set_numFields_static},
89 {"numPages", get_numPages_static, set_numPages_static},
90 {"pageNum", get_pageNum_static, set_pageNum_static},
91 {"pageWindowRect", get_pageWindowRect_static, set_pageWindowRect_static},
92 {"path", get_path_static, set_path_static},
93 {"producer", get_producer_static, set_producer_static},
94 {"subject", get_subject_static, set_subject_static},
95 {"title", get_title_static, set_title_static},
96 {"URL", get_URL_static, set_URL_static},
97 {"zoom", get_zoom_static, set_zoom_static},
98 {"zoomType", get_zoomType_static, set_zoomType_static},
Tom Sepez04557b82017-02-16 09:43:10 -080099 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Tom Sepez04557b82017-02-16 09:43:10 -0800101JSMethodSpec CJS_Document::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -0800102 {"addAnnot", addAnnot_static},
103 {"addField", addField_static},
104 {"addLink", addLink_static},
105 {"addIcon", addIcon_static},
106 {"calculateNow", calculateNow_static},
107 {"closeDoc", closeDoc_static},
108 {"createDataObject", createDataObject_static},
109 {"deletePages", deletePages_static},
110 {"exportAsText", exportAsText_static},
111 {"exportAsFDF", exportAsFDF_static},
112 {"exportAsXFDF", exportAsXFDF_static},
113 {"extractPages", extractPages_static},
114 {"getAnnot", getAnnot_static},
115 {"getAnnots", getAnnots_static},
116 {"getAnnot3D", getAnnot3D_static},
117 {"getAnnots3D", getAnnots3D_static},
118 {"getField", getField_static},
119 {"getIcon", getIcon_static},
120 {"getLinks", getLinks_static},
121 {"getNthFieldName", getNthFieldName_static},
122 {"getOCGs", getOCGs_static},
123 {"getPageBox", getPageBox_static},
124 {"getPageNthWord", getPageNthWord_static},
125 {"getPageNthWordQuads", getPageNthWordQuads_static},
126 {"getPageNumWords", getPageNumWords_static},
127 {"getPrintParams", getPrintParams_static},
128 {"getURL", getURL_static},
129 {"gotoNamedDest", gotoNamedDest_static},
130 {"importAnFDF", importAnFDF_static},
131 {"importAnXFDF", importAnXFDF_static},
132 {"importTextData", importTextData_static},
133 {"insertPages", insertPages_static},
134 {"mailForm", mailForm_static},
135 {"print", print_static},
136 {"removeField", removeField_static},
137 {"replacePages", replacePages_static},
138 {"resetForm", resetForm_static},
139 {"removeIcon", removeIcon_static},
140 {"saveAs", saveAs_static},
141 {"submitForm", submitForm_static},
142 {"syncAnnotScan", syncAnnotScan_static},
143 {"mailDoc", mailDoc_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800144 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
146IMPLEMENT_JS_CLASS(CJS_Document, Document)
147
Tom Sepez33420902015-10-13 15:00:10 -0700148void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
149 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
150 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700151 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700152}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154Document::Document(CJS_Object* pJSObject)
155 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700156 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700158 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800164bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700165 CJS_PropValue& vp,
166 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700168 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700169 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 }
dsinclair82e17672016-10-11 12:38:01 -0700171 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700172 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700173 return false;
tsepez55be02e2016-09-12 11:21:42 -0700174 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700175 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700177 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700178 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Tom Sepezb1670b52017-02-16 17:01:00 -0800181bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700182 CJS_PropValue& vp,
183 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700184 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700185 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700186 return false;
tsepez55be02e2016-09-12 11:21:42 -0700187 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700189 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800190 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800192 bool bChanged = false;
193 vp >> bChanged;
194 if (bChanged)
195 m_pFormFillEnv->SetChangeMark();
196 else
197 m_pFormFillEnv->ClearChangeMark();
198
tsepez4cf55152016-11-02 14:37:54 -0700199 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
Tom Sepezb1670b52017-02-16 17:01:00 -0800202bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700203 CJS_PropValue& vp,
204 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700205 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800206 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207
tsepez4cf55152016-11-02 14:37:54 -0700208 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209}
210
Tom Sepezb1670b52017-02-16 17:01:00 -0800211bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700212 CJS_PropValue& vp,
213 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700214 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700215 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700216 return false;
tsepez55be02e2016-09-12 11:21:42 -0700217 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800219 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800221 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800223 int iPageCount = m_pFormFillEnv->GetPageCount();
224 int iPageNum = 0;
225 vp >> iPageNum;
226 if (iPageNum >= 0 && iPageNum < iPageCount)
227 m_pFormFillEnv->JS_docgotoPage(iPageNum);
228 else if (iPageNum >= iPageCount)
229 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
230 else if (iPageNum < 0)
231 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232
tsepez4cf55152016-11-02 14:37:54 -0700233 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234}
235
Tom Sepezb1670b52017-02-16 17:01:00 -0800236bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700237 const std::vector<CJS_Value>& params,
238 CJS_Value& vRet,
239 CFX_WideString& sError) {
240 // Not supported.
241 return true;
242}
243
Tom Sepezb1670b52017-02-16 17:01:00 -0800244bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700245 const std::vector<CJS_Value>& params,
246 CJS_Value& vRet,
247 CFX_WideString& sError) {
248 // Not supported.
249 return true;
250}
251
Tom Sepezb1670b52017-02-16 17:01:00 -0800252bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700253 const std::vector<CJS_Value>& params,
254 CJS_Value& vRet,
255 CFX_WideString& sError) {
256 // Unsafe, not supported.
257 return true;
258}
259
Tom Sepezb1670b52017-02-16 17:01:00 -0800260bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800261 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 CJS_Value& vRet,
263 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800264 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700265 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Tom Sepezb1670b52017-02-16 17:01:00 -0800268bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700269 const std::vector<CJS_Value>& params,
270 CJS_Value& vRet,
271 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700273 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276// Maps a field object in PDF document to a JavaScript variable
277// comment:
278// note: the paremter cName, this is clue how to treat if the cName is not a
279// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Tom Sepezb1670b52017-02-16 17:01:00 -0800281bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700282 const std::vector<CJS_Value>& params,
283 CJS_Value& vRet,
284 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700286 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700287 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 }
dsinclair82e17672016-10-11 12:38:01 -0700289 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700290 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700291 return false;
tsepez55be02e2016-09-12 11:21:42 -0700292 }
tsepezb4694242016-08-15 16:44:55 -0700293 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700294 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
296 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700297 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700298 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 }
300
tsepezf3dc8c62016-08-10 06:29:29 -0700301 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700302 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -0800303 if (pFieldObj.IsEmpty())
304 return false;
305
tsepezf3dc8c62016-08-10 06:29:29 -0700306 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700307 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700308 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 pField->AttachField(this, wideName);
tsepezf3dc8c62016-08-10 06:29:29 -0700310 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700311 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312}
313
314// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800315bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700316 const std::vector<CJS_Value>& params,
317 CJS_Value& vRet,
318 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700319 if (params.size() != 1) {
320 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700321 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700322 }
dsinclair82e17672016-10-11 12:38:01 -0700323 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700324 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700325 return false;
tsepez55be02e2016-09-12 11:21:42 -0700326 }
tsepezb4694242016-08-15 16:44:55 -0700327 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700329 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700330 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700332 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700334 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700336 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337
tsepezf3dc8c62016-08-10 06:29:29 -0700338 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700339 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340}
341
Tom Sepezb1670b52017-02-16 17:01:00 -0800342bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700343 const std::vector<CJS_Value>& params,
344 CJS_Value& vRet,
345 CFX_WideString& sError) {
346 // Unsafe, not supported.
347 return true;
348}
349
Tom Sepezb1670b52017-02-16 17:01:00 -0800350bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700351 const std::vector<CJS_Value>& params,
352 CJS_Value& vRet,
353 CFX_WideString& sError) {
354 // Unsafe, not supported.
355 return true;
356}
357
Tom Sepezb1670b52017-02-16 17:01:00 -0800358bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800359 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 CJS_Value& vRet,
361 CFX_WideString& sError) {
362 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700363 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364}
365
366// exports the form data and mails the resulting fdf file as an attachment to
367// all recipients.
368// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800369bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700370 const std::vector<CJS_Value>& params,
371 CJS_Value& vRet,
372 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700373 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700374 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700375 return false;
tsepez55be02e2016-09-12 11:21:42 -0700376 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700377 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700378 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700379 return false;
tsepez55be02e2016-09-12 11:21:42 -0700380 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700382 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700383 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
384 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
385 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700386 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700387 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
388 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700389 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 CFX_ByteTextBuf textBuf;
391 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700392 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800395 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700396 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
397 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
398 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700400 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401}
402
Tom Sepezb1670b52017-02-16 17:01:00 -0800403bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700404 const std::vector<CJS_Value>& params,
405 CJS_Value& vRet,
406 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700407 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700408 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700409 return false;
tsepez55be02e2016-09-12 11:21:42 -0700410 }
tsepez4cf55152016-11-02 14:37:54 -0700411 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 int nStart = 0;
413 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700414 bool bSilent = false;
415 bool bShrinkToFit = false;
416 bool bPrintAsImage = false;
417 bool bReverse = false;
418 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 int nlength = params.size();
420 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700421 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700422 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
423 if (CFXJS_Engine::GetObjDefnID(pObj) ==
424 CJS_PrintParamsObj::g_nObjDefnID) {
425 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700426 if (PrintParamsObj* pprintparamsObj =
427 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
428 bUI = pprintparamsObj->bUI;
429 nStart = pprintparamsObj->nStart;
430 nEnd = pprintparamsObj->nEnd;
431 bSilent = pprintparamsObj->bSilent;
432 bShrinkToFit = pprintparamsObj->bShrinkToFit;
433 bPrintAsImage = pprintparamsObj->bPrintAsImage;
434 bReverse = pprintparamsObj->bReverse;
435 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700437 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 }
439 }
440 } else {
441 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700442 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700444 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700446 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700448 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700450 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700452 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700454 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700456 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 }
458
dsinclair82e17672016-10-11 12:38:01 -0700459 if (m_pFormFillEnv) {
460 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
461 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700462 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 }
tsepez4cf55152016-11-02 14:37:54 -0700464 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465}
466
467// removes the specified field from the document.
468// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700469// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470
Tom Sepezb1670b52017-02-16 17:01:00 -0800471bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700472 const std::vector<CJS_Value>& params,
473 CJS_Value& vRet,
474 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700475 if (params.size() != 1) {
476 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700477 return false;
tsepez55be02e2016-09-12 11:21:42 -0700478 }
dsinclair82e17672016-10-11 12:38:01 -0700479 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700480 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
tsepez55be02e2016-09-12 11:21:42 -0700482 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700483 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
484 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700485 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700486 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 }
tsepezb4694242016-08-15 16:44:55 -0700488 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700489 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800490 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800491 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800492 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700493 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494
tsepez8fa82792017-01-11 09:32:33 -0800495 for (const auto& pAnnot : widgets) {
496 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
497 if (!pWidget)
498 continue;
499
Tom Sepez281a9ea2016-02-26 14:24:28 -0800500 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800501 --rcAnnot.left;
502 --rcAnnot.bottom;
503 ++rcAnnot.right;
504 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505
tsepezdf964df2016-04-21 12:09:41 -0700506 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800507 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800508 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509
dsinclair1897bdc2016-09-06 14:18:57 -0700510 // If there is currently no pageview associated with the page being used
511 // do not create one. We may be in the process of tearing down the document
512 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700513 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700514 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700515#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700516 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700517#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700518 pPageView->UpdateRects(aRefresh);
519 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700521 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522
tsepez4cf55152016-11-02 14:37:54 -0700523 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524}
525
526// reset filed values within a document.
527// comment:
528// note: if the fields names r not rational, aodbe is dumb for it.
529
Tom Sepezb1670b52017-02-16 17:01:00 -0800530bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700531 const std::vector<CJS_Value>& params,
532 CJS_Value& vRet,
533 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700534 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700535 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700536 return false;
tsepez55be02e2016-09-12 11:21:42 -0700537 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700538 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
539 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
540 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700541 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700542 return false;
tsepez55be02e2016-09-12 11:21:42 -0700543 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544
dsinclair7cbe68e2016-10-12 11:56:23 -0700545 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700547 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548
Lei Zhangdd734de2015-11-11 10:16:52 -0800549 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700550 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700551 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700552 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800553 }
554
555 switch (params[0].GetType()) {
556 default:
tsepezb4694242016-08-15 16:44:55 -0700557 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 break;
559 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700560 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800561 break;
562 }
563
564 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700565 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700567 aName.GetElement(pRuntime, i, valElement);
568 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800569 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
570 aFields.push_back(pPDFForm->GetField(j, swVal));
571 }
572
573 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700574 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700575 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 }
577
tsepez4cf55152016-11-02 14:37:54 -0700578 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579}
580
Tom Sepezb1670b52017-02-16 17:01:00 -0800581bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700582 const std::vector<CJS_Value>& params,
583 CJS_Value& vRet,
584 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587}
588
Tom Sepezb1670b52017-02-16 17:01:00 -0800589bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800590 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591 CJS_Value& vRet,
592 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700593 return true;
594}
595
Tom Sepezb1670b52017-02-16 17:01:00 -0800596bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700597 const std::vector<CJS_Value>& params,
598 CJS_Value& vRet,
599 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 int nSize = params.size();
601 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700602 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700603 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
dsinclair82e17672016-10-11 12:38:01 -0700605 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700606 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700607 return false;
tsepez55be02e2016-09-12 11:21:42 -0700608 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800609
tsepeze5aff742016-08-08 09:49:42 -0700610 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700612 bool bFDF = true;
613 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700615 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700616 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700618 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700620 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700622 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700623 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700624 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
625 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700627 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700628
tsepezb4694242016-08-15 16:44:55 -0700629 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
630 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700631
tsepezb4694242016-08-15 16:44:55 -0700632 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
633 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700634
tsepezb4694242016-08-15 16:44:55 -0700635 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
636 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 }
638
dsinclair7cbe68e2016-10-12 11:56:23 -0700639 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700641 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800642 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700644 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700646 }
tsepez4cf55152016-11-02 14:37:54 -0700647 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 }
649
Lei Zhangd88a3642015-11-10 09:38:57 -0800650 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700651 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700652 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700653 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654
tsepezb4694242016-08-15 16:44:55 -0700655 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
657 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
658 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
659 if (!bEmpty && pField->GetValue().IsEmpty())
660 continue;
661
Lei Zhangd88a3642015-11-10 09:38:57 -0800662 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 }
664 }
665
Lei Zhangd88a3642015-11-10 09:38:57 -0800666 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800668 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 pRuntime->EndBlock();
670 }
tsepez4cf55152016-11-02 14:37:54 -0700671 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
dsinclair82e17672016-10-11 12:38:01 -0700674void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
675 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700676}
677
Tom Sepezb1670b52017-02-16 17:01:00 -0800678bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700679 CJS_PropValue& vp,
680 CFX_WideString& sError) {
681 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682}
683
Tom Sepezb1670b52017-02-16 17:01:00 -0800684bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700685 const std::vector<CJS_Value>& params,
686 CJS_Value& vRet,
687 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700688 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700689 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 CFX_WideString cTo = L"";
691 CFX_WideString cCc = L"";
692 CFX_WideString cBcc = L"";
693 CFX_WideString cSubject = L"";
694 CFX_WideString cMsg = L"";
695
tsepezf3dc8c62016-08-10 06:29:29 -0700696 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700697 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700698 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700699 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700700 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700701 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700702 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700703 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700704 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700705 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700706 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700707 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708
Tom Sepez39bfe122015-09-17 15:25:23 -0700709 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700710 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
tsepezb4694242016-08-15 16:44:55 -0700712 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700713 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714
tsepezb4694242016-08-15 16:44:55 -0700715 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
716 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717
tsepezb4694242016-08-15 16:44:55 -0700718 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
719 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720
tsepezb4694242016-08-15 16:44:55 -0700721 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
722 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723
tsepezb4694242016-08-15 16:44:55 -0700724 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
725 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726
tsepezb4694242016-08-15 16:44:55 -0700727 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
728 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 }
730
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700732 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
733 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
734 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700736 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737}
738
Tom Sepezb1670b52017-02-16 17:01:00 -0800739bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700740 CJS_PropValue& vp,
741 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800742 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743}
744
Tom Sepezb1670b52017-02-16 17:01:00 -0800745bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700746 CJS_PropValue& vp,
747 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700748 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700749 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700750 return false;
tonikitooec268762016-08-10 12:29:28 -0700751 }
dsinclair82e17672016-10-11 12:38:01 -0700752 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700753 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700754 return false;
tsepez55be02e2016-09-12 11:21:42 -0700755 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700756 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700758 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800759
dsinclair38fd8442016-09-15 10:15:32 -0700760 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
761 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
762 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
763 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
764 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
765 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
766 CFX_WideString cwCreationDate =
767 pDictionary->GetUnicodeTextFor("CreationDate");
768 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
769 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800770
tsepezb4694242016-08-15 16:44:55 -0700771 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800772 pRuntime->PutObjectProperty(pObj, L"Author",
773 pRuntime->NewString(cwAuthor.AsStringC()));
774 pRuntime->PutObjectProperty(pObj, L"Title",
775 pRuntime->NewString(cwTitle.AsStringC()));
776 pRuntime->PutObjectProperty(pObj, L"Subject",
777 pRuntime->NewString(cwSubject.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800778 pRuntime->PutObjectProperty(pObj, L"Keywords",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800779 pRuntime->NewString(cwKeywords.AsStringC()));
780 pRuntime->PutObjectProperty(pObj, L"Creator",
781 pRuntime->NewString(cwCreator.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800782 pRuntime->PutObjectProperty(pObj, L"Producer",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800783 pRuntime->NewString(cwProducer.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800784 pRuntime->PutObjectProperty(pObj, L"CreationDate",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800785 pRuntime->NewString(cwCreationDate.AsStringC()));
786 pRuntime->PutObjectProperty(pObj, L"ModDate",
787 pRuntime->NewString(cwModDate.AsStringC()));
788 pRuntime->PutObjectProperty(pObj, L"Trapped",
789 pRuntime->NewString(cwTrapped.AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700790
791 // It's to be compatible to non-standard info dictionary.
792 for (const auto& it : *pDictionary) {
793 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800794 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700795 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
796 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800797 pRuntime->PutObjectProperty(
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800798 pObj, wsKey,
799 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700800 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800801 pRuntime->PutObjectProperty(pObj, wsKey,
802 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700803 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800804 pRuntime->PutObjectProperty(
805 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700806 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700807 }
tonikitooec268762016-08-10 12:29:28 -0700808 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700809 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700810}
811
Tom Sepezb1670b52017-02-16 17:01:00 -0800812bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700813 CJS_PropValue& vp,
814 const CFX_ByteString& propName,
815 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700816 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700817 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700818 return false;
tsepez55be02e2016-09-12 11:21:42 -0700819 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700820 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700822 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823
824 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700825 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700827 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700828 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700829 return false;
tsepez55be02e2016-09-12 11:21:42 -0700830 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700831 CFX_WideString csProperty;
832 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800833 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
834 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700835 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 }
tsepez4cf55152016-11-02 14:37:54 -0700837 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700838}
839
Tom Sepezb1670b52017-02-16 17:01:00 -0800840bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700841 CJS_PropValue& vp,
842 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800843 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700844}
845
Tom Sepezb1670b52017-02-16 17:01:00 -0800846bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700847 CJS_PropValue& vp,
848 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800849 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700850}
851
Tom Sepezb1670b52017-02-16 17:01:00 -0800852bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700853 CJS_PropValue& vp,
854 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700855 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700856 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700857 return false;
tsepez55be02e2016-09-12 11:21:42 -0700858 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 if (vp.IsGetting()) {
860 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800861 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800863 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
864 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
865 return false;
866 }
867 vp >> m_bDelay;
868 if (m_bDelay) {
869 m_DelayData.clear();
870 return true;
871 }
872 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
873 DelayDataToProcess.swap(m_DelayData);
874 for (const auto& pData : DelayDataToProcess)
875 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
876
tsepez4cf55152016-11-02 14:37:54 -0700877 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878}
879
Tom Sepezb1670b52017-02-16 17:01:00 -0800880bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700881 CJS_PropValue& vp,
882 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800883 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884}
885
Tom Sepezb1670b52017-02-16 17:01:00 -0800886bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700887 CJS_PropValue& vp,
888 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800889 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890}
891
Tom Sepezb1670b52017-02-16 17:01:00 -0800892bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700893 CJS_PropValue& vp,
894 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800895 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896}
897
Tom Sepezb1670b52017-02-16 17:01:00 -0800898bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700899 CJS_PropValue& vp,
900 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800901 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902}
903
Tom Sepezb1670b52017-02-16 17:01:00 -0800904bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700905 CJS_PropValue& vp,
906 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700907 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700908 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700909 return false;
tsepez55be02e2016-09-12 11:21:42 -0700910 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800911 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912}
913
Tom Sepezb1670b52017-02-16 17:01:00 -0800914bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700915 CJS_PropValue& vp,
916 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700918 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700919 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700920 }
dsinclair82e17672016-10-11 12:38:01 -0700921 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700922 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700923 return false;
tsepez55be02e2016-09-12 11:21:42 -0700924 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700925 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700926 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927}
928
Tom Sepezb1670b52017-02-16 17:01:00 -0800929bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700930 CJS_PropValue& vp,
931 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700932 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 if (vp.IsGetting()) {
934 vp << true;
935 }
tsepez4cf55152016-11-02 14:37:54 -0700936 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937}
938
Tom Sepezb1670b52017-02-16 17:01:00 -0800939bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700940 CJS_PropValue& vp,
941 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700943 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700944 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 }
946 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700947 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948}
949
Tom Sepezb1670b52017-02-16 17:01:00 -0800950bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700951 CJS_PropValue& vp,
952 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700953 return true;
954}
955
Tom Sepezb1670b52017-02-16 17:01:00 -0800956bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700957 CJS_PropValue& vp,
958 CFX_WideString& sError) {
959 return true;
960}
961
Tom Sepezb1670b52017-02-16 17:01:00 -0800962bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800963 CJS_PropValue& vp,
964 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700965 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700966 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700967 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700968 }
dsinclair82e17672016-10-11 12:38:01 -0700969 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700970 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700971 return false;
tsepez55be02e2016-09-12 11:21:42 -0700972 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700973 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700974 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700975}
976
Tom Sepezb1670b52017-02-16 17:01:00 -0800977bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700978 CJS_PropValue& vp,
979 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 if (vp.IsGetting()) {
981 vp << m_cwBaseURL;
982 } else {
983 vp >> m_cwBaseURL;
984 }
tsepez4cf55152016-11-02 14:37:54 -0700985 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986}
987
Tom Sepezb1670b52017-02-16 17:01:00 -0800988bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700989 CJS_PropValue& vp,
990 CFX_WideString& sError) {
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 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700997 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800998 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001000 bool bCalculate;
1001 vp >> bCalculate;
1002 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -07001003 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004}
1005
Tom Sepezb1670b52017-02-16 17:01:00 -08001006bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001007 CJS_PropValue& vp,
1008 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001010 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001011 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 }
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 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 int32_t i = wsFilePath.GetLength() - 1;
1019 for (; i >= 0; i--) {
1020 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1021 break;
1022 }
1023 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1024 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1025 } else {
1026 vp << L"";
1027 }
tsepez4cf55152016-11-02 14:37:54 -07001028 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029}
1030
Tom Sepezb1670b52017-02-16 17:01:00 -08001031bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001032 CJS_PropValue& vp,
1033 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001035 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001036 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 }
dsinclair82e17672016-10-11 12:38:01 -07001038 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001039 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001040 return false;
tsepez55be02e2016-09-12 11:21:42 -07001041 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001042 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001043 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044}
1045
Tom Sepezb1670b52017-02-16 17:01:00 -08001046bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001047 CJS_PropValue& vp,
1048 CFX_WideString& sError) {
1049 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050}
1051
Tom Sepezb1670b52017-02-16 17:01:00 -08001052bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001053 CJS_PropValue& vp,
1054 CFX_WideString& sError) {
1055 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056}
1057
Tom Sepezb1670b52017-02-16 17:01:00 -08001058bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001059 const std::vector<CJS_Value>& params,
1060 CJS_Value& vRet,
1061 CFX_WideString& sError) {
1062 return true;
1063}
1064
Tom Sepezb1670b52017-02-16 17:01:00 -08001065bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001066 const std::vector<CJS_Value>& params,
1067 CJS_Value& vRet,
1068 CFX_WideString& sError) {
1069 return true;
1070}
1071
Tom Sepezb1670b52017-02-16 17:01:00 -08001072bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001073 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 CJS_Value& vRet,
1075 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001076 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077}
1078
Tom Sepezb1670b52017-02-16 17:01:00 -08001079bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001080 const std::vector<CJS_Value>& params,
1081 CJS_Value& vRet,
1082 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001083 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001084 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001085 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001086 }
dsinclair82e17672016-10-11 12:38:01 -07001087 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001088 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001089 return false;
tsepez55be02e2016-09-12 11:21:42 -07001090 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001091 int nPageNo = params[0].ToInt(pRuntime);
1092 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001093 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001094 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001095 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001096
tsepezd805eec2017-01-11 14:03:54 -08001097 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001098 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001099 for (const auto& pSDKAnnotCur : annotIteration) {
1100 CPDFSDK_BAAnnot* pBAAnnot =
1101 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001102 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1103 pSDKBAAnnot = pBAAnnot;
1104 break;
1105 }
1106 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001107 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001108 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001109
1110 v8::Local<v8::Object> pObj =
1111 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1112 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001113 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001114
1115 CJS_Annot* pJS_Annot =
1116 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo618cb1f2016-08-18 20:10:17 -07001117 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tonikitoo618cb1f2016-08-18 20:10:17 -07001118 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001119 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001120 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121}
1122
Tom Sepezb1670b52017-02-16 17:01:00 -08001123bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001124 const std::vector<CJS_Value>& params,
1125 CJS_Value& vRet,
1126 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001127 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001128 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001129 return false;
tsepez55be02e2016-09-12 11:21:42 -07001130 }
tonikitoo0ee35902016-08-26 14:41:29 -07001131 // TODO(tonikitoo): Add support supported parameters as per
1132 // the PDF spec.
1133
dsinclair7cbe68e2016-10-12 11:56:23 -07001134 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001135 CJS_Array annots;
1136
1137 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001138 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001139 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001140 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001141
tsepezd805eec2017-01-11 14:03:54 -08001142 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1143 for (const auto& pSDKAnnotCur : annotIteration) {
1144 if (!pSDKAnnotCur) {
1145 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001146 return false;
tsepezd805eec2017-01-11 14:03:54 -08001147 }
tonikitoo0ee35902016-08-26 14:41:29 -07001148 v8::Local<v8::Object> pObj =
1149 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1150 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001151 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001152
1153 CJS_Annot* pJS_Annot =
1154 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo0ee35902016-08-26 14:41:29 -07001155 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tsepezd805eec2017-01-11 14:03:54 -08001156 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001157 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1158 }
1159 }
tonikitoo0ee35902016-08-26 14:41:29 -07001160 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001161 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162}
1163
Tom Sepezb1670b52017-02-16 17:01:00 -08001164bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001165 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 CJS_Value& vRet,
1167 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001168 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001169 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001170}
1171
Tom Sepezb1670b52017-02-16 17:01:00 -08001172bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001173 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174 CJS_Value& vRet,
1175 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001176 return true;
1177}
1178
Tom Sepezb1670b52017-02-16 17:01:00 -08001179bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001180 const std::vector<CJS_Value>& params,
1181 CJS_Value& vRet,
1182 CFX_WideString& sError) {
1183 return true;
1184}
1185
Tom Sepezb1670b52017-02-16 17:01:00 -08001186bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001187 const std::vector<CJS_Value>& params,
1188 CJS_Value& vRet,
1189 CFX_WideString& sError) {
1190 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191}
1192
1193bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1194 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1195 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1196}
1197
Tom Sepezb1670b52017-02-16 17:01:00 -08001198bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001199 const std::vector<CJS_Value>& params,
1200 CJS_Value& vRet,
1201 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001203 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001204 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 }
tsepezf3dc8c62016-08-10 06:29:29 -07001206
tsepezb4694242016-08-15 16:44:55 -07001207 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001208 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001209 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001210 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001211 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212
tsepezb4694242016-08-15 16:44:55 -07001213 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1214 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001215 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001216 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217 }
1218
Lei Zhang313c4252017-02-27 16:37:36 -08001219 if (!params[1].ToCJSObject(pRuntime)->GetEmbedObject()) {
tsepezcd5dc852016-09-08 11:23:24 -07001220 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001221 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 }
1223
Lei Zhang313c4252017-02-27 16:37:36 -08001224 m_IconNames.push_back(swIconName);
tsepez4cf55152016-11-02 14:37:54 -07001225 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226}
1227
Tom Sepezb1670b52017-02-16 17:01:00 -08001228bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001229 CJS_PropValue& vp,
1230 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001232 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001233 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234 }
Lei Zhang313c4252017-02-27 16:37:36 -08001235 if (m_IconNames.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001236 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001237 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001238 }
1239
tsepeze5aff742016-08-08 09:49:42 -07001240 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001241 int i = 0;
Lei Zhang313c4252017-02-27 16:37:36 -08001242 for (const auto& name : m_IconNames) {
tsepezb4694242016-08-15 16:44:55 -07001243 v8::Local<v8::Object> pObj =
1244 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001246 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001247
tsepezb4694242016-08-15 16:44:55 -07001248 CJS_Icon* pJS_Icon =
1249 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
thestigdadea5f2016-10-19 13:29:48 -07001250 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Lei Zhang313c4252017-02-27 16:37:36 -08001251 pIcon->SetIconName(name);
tsepezb4694242016-08-15 16:44:55 -07001252 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 }
1254
1255 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001256 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257}
1258
Tom Sepezb1670b52017-02-16 17:01:00 -08001259bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001260 const std::vector<CJS_Value>& params,
1261 CJS_Value& vRet,
1262 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001264 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001265 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 }
1267
Lei Zhang313c4252017-02-27 16:37:36 -08001268 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
1269 auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName);
1270 if (it == m_IconNames.end())
tsepez4cf55152016-11-02 14:37:54 -07001271 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001272
Lei Zhang313c4252017-02-27 16:37:36 -08001273 v8::Local<v8::Object> pObj =
1274 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1275 if (pObj.IsEmpty())
1276 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277
Lei Zhang313c4252017-02-27 16:37:36 -08001278 CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1279 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
1280 pIcon->SetIconName(*it);
1281 vRet = CJS_Value(pRuntime, pJS_Icon);
1282 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283}
1284
Tom Sepezb1670b52017-02-16 17:01:00 -08001285bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001286 const std::vector<CJS_Value>& params,
1287 CJS_Value& vRet,
1288 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001289 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001290 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291}
1292
Tom Sepezb1670b52017-02-16 17:01:00 -08001293bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001294 const std::vector<CJS_Value>& params,
1295 CJS_Value& vRet,
1296 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001297 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001298 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001299}
1300
Tom Sepezb1670b52017-02-16 17:01:00 -08001301bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001302 CJS_PropValue& vp,
1303 CFX_WideString& sError) {
1304 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001305}
1306
Tom Sepezb1670b52017-02-16 17:01:00 -08001307bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001308 const std::vector<CJS_Value>& params,
1309 CJS_Value& vRet,
1310 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001311 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001312 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001313 return false;
tsepez55be02e2016-09-12 11:21:42 -07001314 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001315 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1316 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1317 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001318 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001319 return false;
tsepez55be02e2016-09-12 11:21:42 -07001320 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001321 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001322 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001323}
1324
Tom Sepezb1670b52017-02-16 17:01:00 -08001325bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001326 CJS_PropValue& vp,
1327 CFX_WideString& sError) {
1328 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001329}
1330
Tom Sepezb1670b52017-02-16 17:01:00 -08001331bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001332 const std::vector<CJS_Value>& params,
1333 CJS_Value& vRet,
1334 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001335 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001336 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001337 return false;
tsepez55be02e2016-09-12 11:21:42 -07001338 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001339 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001340 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001341 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001342 }
tsepez55be02e2016-09-12 11:21:42 -07001343
1344 // TODO(tsepez): check maximum allowable params.
1345
tsepezb4694242016-08-15 16:44:55 -07001346 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1347 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1348 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001349
dsinclair7cbe68e2016-10-12 11:56:23 -07001350 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001351 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001352 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001353
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001355 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001356 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001358
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1360 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001361 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001362
thestig5cc24652016-04-26 11:46:02 -07001363 CPDF_Page page(pDocument, pPageDict, true);
1364 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001365
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001368 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001369 if (pPageObj->IsText()) {
1370 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001371 int nObjWords = CountWords(pTextObj);
1372 if (nWords + nObjWords >= nWordNo) {
1373 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1374 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 }
Tom Sepez2398d892016-02-17 16:46:26 -08001376 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001377 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001379
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 if (bStrip) {
1381 swRet.TrimLeft();
1382 swRet.TrimRight();
1383 }
1384
tsepezf3dc8c62016-08-10 06:29:29 -07001385 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001386 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001387}
1388
Tom Sepezb1670b52017-02-16 17:01:00 -08001389bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001390 const std::vector<CJS_Value>& params,
1391 CJS_Value& vRet,
1392 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001393 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001394 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001395 return false;
tsepez55be02e2016-09-12 11:21:42 -07001396 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001397 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001398 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001399 return false;
tsepez55be02e2016-09-12 11:21:42 -07001400 }
tsepez4cf55152016-11-02 14:37:54 -07001401 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402}
1403
Tom Sepezb1670b52017-02-16 17:01:00 -08001404bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001405 const std::vector<CJS_Value>& params,
1406 CJS_Value& vRet,
1407 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001408 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001409 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001410 return false;
tsepez55be02e2016-09-12 11:21:42 -07001411 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001412 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001413 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001414 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001415 }
tsepezb4694242016-08-15 16:44:55 -07001416 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001417 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001419 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001420 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 }
1422
1423 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1424 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001425 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426
thestig5cc24652016-04-26 11:46:02 -07001427 CPDF_Page page(pDocument, pPageDict, true);
1428 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429
1430 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001431 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001432 if (pPageObj->IsText())
1433 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434 }
1435
tsepezf3dc8c62016-08-10 06:29:29 -07001436 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001437 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001438}
1439
Tom Sepezb1670b52017-02-16 17:01:00 -08001440bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001441 const std::vector<CJS_Value>& params,
1442 CJS_Value& vRet,
1443 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001444 v8::Local<v8::Object> pRetObj =
1445 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -08001446 if (pRetObj.IsEmpty())
1447 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448
1449 // Not implemented yet.
1450
tsepezf3dc8c62016-08-10 06:29:29 -07001451 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001452 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001453}
1454
1455#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1456
1457int Document::CountWords(CPDF_TextObject* pTextObj) {
1458 if (!pTextObj)
1459 return 0;
1460
1461 int nWords = 0;
1462
1463 CPDF_Font* pFont = pTextObj->GetFont();
1464 if (!pFont)
1465 return 0;
1466
tsepez4cf55152016-11-02 14:37:54 -07001467 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468
1469 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001470 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001471 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001473 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1475
Tom Sepez62a70f92016-03-21 15:00:20 -07001476 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 if (swUnicode.GetLength() > 0)
1478 unicode = swUnicode[0];
1479
1480 if (ISLATINWORD(unicode) && bIsLatin)
1481 continue;
1482
1483 bIsLatin = ISLATINWORD(unicode);
1484 if (unicode != 0x20)
1485 nWords++;
1486 }
1487
1488 return nWords;
1489}
1490
1491CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1492 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 CFX_WideString swRet;
1494
1495 CPDF_Font* pFont = pTextObj->GetFont();
1496 if (!pFont)
1497 return L"";
1498
1499 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001500 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501
1502 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001503 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Dan Sinclair05df0752017-03-14 14:43:42 -04001504 float kerning;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001506 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001507 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1508
Tom Sepez62a70f92016-03-21 15:00:20 -07001509 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001510 if (swUnicode.GetLength() > 0)
1511 unicode = swUnicode[0];
1512
1513 if (ISLATINWORD(unicode) && bIsLatin) {
1514 } else {
1515 bIsLatin = ISLATINWORD(unicode);
1516 if (unicode != 0x20)
1517 nWords++;
1518 }
1519
1520 if (nWords - 1 == nWordIndex)
1521 swRet += unicode;
1522 }
1523
1524 return swRet;
1525}
1526
Tom Sepezb1670b52017-02-16 17:01:00 -08001527bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001528 CJS_PropValue& vp,
1529 CFX_WideString& sError) {
1530 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001531}
1532
1533/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001534(none, NoVary)
1535(fitP, FitPage)
1536(fitW, FitWidth)
1537(fitH, FitHeight)
1538(fitV, FitVisibleWidth)
1539(pref, Preferred)
1540(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001541*/
1542
Tom Sepezb1670b52017-02-16 17:01:00 -08001543bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001544 CJS_PropValue& vp,
1545 CFX_WideString& sError) {
1546 return true;
1547}
1548
Tom Sepezb1670b52017-02-16 17:01:00 -08001549bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001550 const std::vector<CJS_Value>& params,
1551 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001552 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001553 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001554 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001555}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001556
Tom Sepezb1670b52017-02-16 17:01:00 -08001557bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001558 const std::vector<CJS_Value>& params,
1559 CJS_Value& vRet,
1560 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001561 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001562 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001563}
1564
Tom Sepezb1670b52017-02-16 17:01:00 -08001565bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001566 const std::vector<CJS_Value>& params,
1567 CJS_Value& vRet,
1568 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001569 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001570 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001571}
1572
Tom Sepezb1670b52017-02-16 17:01:00 -08001573bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001574 const std::vector<CJS_Value>& params,
1575 CJS_Value& vRet,
1576 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001578 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001579}
1580
Tom Sepezb1670b52017-02-16 17:01:00 -08001581bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001582 const std::vector<CJS_Value>& params,
1583 CJS_Value& vRet,
1584 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001585 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001586 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001587}
1588
Tom Sepezb1670b52017-02-16 17:01:00 -08001589bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001590 const std::vector<CJS_Value>& params,
1591 CJS_Value& vRet,
1592 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001593 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001594 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001595 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001596 }
dsinclair82e17672016-10-11 12:38:01 -07001597 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001598 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001599 return false;
tsepez55be02e2016-09-12 11:21:42 -07001600 }
tsepezb4694242016-08-15 16:44:55 -07001601 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001602 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001603 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001604 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001605 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001606
tonikitoo1c836752016-08-08 16:14:05 -07001607 CPDF_NameTree nameTree(pDocument, "Dests");
1608 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1609 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001610 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001611
1612 CPDF_Dest dest(destArray);
1613 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
Tom Sepezd7188f72017-05-02 15:10:58 -07001614 std::vector<float> scrollPositionArray;
tonikitoo1c836752016-08-08 16:14:05 -07001615 if (arrayObject) {
tonikitoo1c836752016-08-08 16:14:05 -07001616 for (size_t i = 2; i < arrayObject->GetCount(); i++)
Tom Sepezd7188f72017-05-02 15:10:58 -07001617 scrollPositionArray.push_back(arrayObject->GetFloatAt(i));
tonikitoo1c836752016-08-08 16:14:05 -07001618 }
tsepezb4694242016-08-15 16:44:55 -07001619 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001620 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
Tom Sepezd7188f72017-05-02 15:10:58 -07001621 scrollPositionArray.data(),
1622 scrollPositionArray.size());
tsepezb4694242016-08-15 16:44:55 -07001623 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -07001624 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001625}
1626
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001627void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001628 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001629}
1630
1631void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1632 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001633 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1634 auto iter = m_DelayData.begin();
1635 while (iter != m_DelayData.end()) {
1636 auto old = iter++;
1637 if ((*old)->sFieldName == sFieldName &&
1638 (*old)->nControlIndex == nControlIndex) {
1639 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1640 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001641 }
1642 }
1643
dsinclair82e17672016-10-11 12:38:01 -07001644 for (const auto& pData : DelayDataForFieldAndControlIndex)
1645 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646}
1647
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648CJS_Document* Document::GetCJSDoc() const {
1649 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001650}