blob: 6928a061a241fe7315b981646368bbac62e76e39 [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"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080033#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070034#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
Tom Sepez04557b82017-02-16 09:43:10 -080039JSConstSpec CJS_PrintParamsObj::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepez04557b82017-02-16 09:43:10 -080041JSPropertySpec CJS_PrintParamsObj::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepez04557b82017-02-16 09:43:10 -080043JSMethodSpec CJS_PrintParamsObj::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
45IMPLEMENT_JS_CLASS(CJS_PrintParamsObj, PrintParamsObj)
46
47PrintParamsObj::PrintParamsObj(CJS_Object* pJSObject)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 : CJS_EmbedObj(pJSObject) {
tsepez4cf55152016-11-02 14:37:54 -070049 bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 nStart = 0;
51 nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -070052 bSilent = false;
53 bShrinkToFit = false;
54 bPrintAsImage = false;
55 bReverse = false;
56 bAnnotations = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059#define MINWIDTH 5.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060#define MINHEIGHT 5.0f
61
Tom Sepez04557b82017-02-16 09:43:10 -080062JSConstSpec CJS_Document::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Tom Sepez04557b82017-02-16 09:43:10 -080064JSPropertySpec CJS_Document::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080065 {"ADBE", get_ADBE_static, set_ADBE_static},
66 {"author", get_author_static, set_author_static},
67 {"baseURL", get_baseURL_static, set_baseURL_static},
68 {"bookmarkRoot", get_bookmarkRoot_static, set_bookmarkRoot_static},
69 {"calculate", get_calculate_static, set_calculate_static},
70 {"Collab", get_Collab_static, set_Collab_static},
71 {"creationDate", get_creationDate_static, set_creationDate_static},
72 {"creator", get_creator_static, set_creator_static},
73 {"delay", get_delay_static, set_delay_static},
74 {"dirty", get_dirty_static, set_dirty_static},
75 {"documentFileName", get_documentFileName_static,
Tom Sepez04557b82017-02-16 09:43:10 -080076 set_documentFileName_static},
Tom Sepez4d5b8c52017-02-21 15:17:07 -080077 {"external", get_external_static, set_external_static},
78 {"filesize", get_filesize_static, set_filesize_static},
79 {"icons", get_icons_static, set_icons_static},
80 {"info", get_info_static, set_info_static},
81 {"keywords", get_keywords_static, set_keywords_static},
82 {"layout", get_layout_static, set_layout_static},
83 {"media", get_media_static, set_media_static},
84 {"modDate", get_modDate_static, set_modDate_static},
85 {"mouseX", get_mouseX_static, set_mouseX_static},
86 {"mouseY", get_mouseY_static, set_mouseY_static},
87 {"numFields", get_numFields_static, set_numFields_static},
88 {"numPages", get_numPages_static, set_numPages_static},
89 {"pageNum", get_pageNum_static, set_pageNum_static},
90 {"pageWindowRect", get_pageWindowRect_static, set_pageWindowRect_static},
91 {"path", get_path_static, set_path_static},
92 {"producer", get_producer_static, set_producer_static},
93 {"subject", get_subject_static, set_subject_static},
94 {"title", get_title_static, set_title_static},
95 {"URL", get_URL_static, set_URL_static},
96 {"zoom", get_zoom_static, set_zoom_static},
97 {"zoomType", get_zoomType_static, set_zoomType_static},
Tom Sepez04557b82017-02-16 09:43:10 -080098 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Tom Sepez04557b82017-02-16 09:43:10 -0800100JSMethodSpec CJS_Document::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -0800101 {"addAnnot", addAnnot_static},
102 {"addField", addField_static},
103 {"addLink", addLink_static},
104 {"addIcon", addIcon_static},
105 {"calculateNow", calculateNow_static},
106 {"closeDoc", closeDoc_static},
107 {"createDataObject", createDataObject_static},
108 {"deletePages", deletePages_static},
109 {"exportAsText", exportAsText_static},
110 {"exportAsFDF", exportAsFDF_static},
111 {"exportAsXFDF", exportAsXFDF_static},
112 {"extractPages", extractPages_static},
113 {"getAnnot", getAnnot_static},
114 {"getAnnots", getAnnots_static},
115 {"getAnnot3D", getAnnot3D_static},
116 {"getAnnots3D", getAnnots3D_static},
117 {"getField", getField_static},
118 {"getIcon", getIcon_static},
119 {"getLinks", getLinks_static},
120 {"getNthFieldName", getNthFieldName_static},
121 {"getOCGs", getOCGs_static},
122 {"getPageBox", getPageBox_static},
123 {"getPageNthWord", getPageNthWord_static},
124 {"getPageNthWordQuads", getPageNthWordQuads_static},
125 {"getPageNumWords", getPageNumWords_static},
126 {"getPrintParams", getPrintParams_static},
127 {"getURL", getURL_static},
128 {"gotoNamedDest", gotoNamedDest_static},
129 {"importAnFDF", importAnFDF_static},
130 {"importAnXFDF", importAnXFDF_static},
131 {"importTextData", importTextData_static},
132 {"insertPages", insertPages_static},
133 {"mailForm", mailForm_static},
134 {"print", print_static},
135 {"removeField", removeField_static},
136 {"replacePages", replacePages_static},
137 {"resetForm", resetForm_static},
138 {"removeIcon", removeIcon_static},
139 {"saveAs", saveAs_static},
140 {"submitForm", submitForm_static},
141 {"syncAnnotScan", syncAnnotScan_static},
142 {"mailDoc", mailDoc_static},
Tom Sepez04557b82017-02-16 09:43:10 -0800143 {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
145IMPLEMENT_JS_CLASS(CJS_Document, Document)
146
Tom Sepez33420902015-10-13 15:00:10 -0700147void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
148 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
149 Document* pDoc = static_cast<Document*>(GetEmbedObject());
dsinclair82e17672016-10-11 12:38:01 -0700150 pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
Tom Sepez33420902015-10-13 15:00:10 -0700151}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153Document::Document(CJS_Object* pJSObject)
154 : CJS_EmbedObj(pJSObject),
dsinclair82e17672016-10-11 12:38:01 -0700155 m_pFormFillEnv(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 m_cwBaseURL(L""),
tsepez4cf55152016-11-02 14:37:54 -0700157 m_bDelay(false) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159Document::~Document() {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162// the total number of fileds in document.
Tom Sepezb1670b52017-02-16 17:01:00 -0800163bool Document::numFields(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700164 CJS_PropValue& vp,
165 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700167 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700168 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 }
dsinclair82e17672016-10-11 12:38:01 -0700170 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700171 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700172 return false;
tsepez55be02e2016-09-12 11:21:42 -0700173 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700174 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700176 vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
tsepez4cf55152016-11-02 14:37:54 -0700177 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Tom Sepezb1670b52017-02-16 17:01:00 -0800180bool Document::dirty(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700181 CJS_PropValue& vp,
182 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700183 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700184 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700185 return false;
tsepez55be02e2016-09-12 11:21:42 -0700186 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 if (vp.IsGetting()) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700188 vp << !!m_pFormFillEnv->GetChangeMark();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800189 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800191 bool bChanged = false;
192 vp >> bChanged;
193 if (bChanged)
194 m_pFormFillEnv->SetChangeMark();
195 else
196 m_pFormFillEnv->ClearChangeMark();
197
tsepez4cf55152016-11-02 14:37:54 -0700198 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Tom Sepezb1670b52017-02-16 17:01:00 -0800201bool Document::ADBE(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700202 CJS_PropValue& vp,
203 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700204 if (vp.IsGetting())
Tom Sepezb1670b52017-02-16 17:01:00 -0800205 vp.GetJSValue()->SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206
tsepez4cf55152016-11-02 14:37:54 -0700207 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208}
209
Tom Sepezb1670b52017-02-16 17:01:00 -0800210bool Document::pageNum(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700211 CJS_PropValue& vp,
212 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700213 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700214 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700215 return false;
tsepez55be02e2016-09-12 11:21:42 -0700216 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 if (vp.IsGetting()) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800218 if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 vp << pPageView->GetPageIndex();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800220 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800222 int iPageCount = m_pFormFillEnv->GetPageCount();
223 int iPageNum = 0;
224 vp >> iPageNum;
225 if (iPageNum >= 0 && iPageNum < iPageCount)
226 m_pFormFillEnv->JS_docgotoPage(iPageNum);
227 else if (iPageNum >= iPageCount)
228 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
229 else if (iPageNum < 0)
230 m_pFormFillEnv->JS_docgotoPage(0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231
tsepez4cf55152016-11-02 14:37:54 -0700232 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233}
234
Tom Sepezb1670b52017-02-16 17:01:00 -0800235bool Document::addAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700236 const std::vector<CJS_Value>& params,
237 CJS_Value& vRet,
238 CFX_WideString& sError) {
239 // Not supported.
240 return true;
241}
242
Tom Sepezb1670b52017-02-16 17:01:00 -0800243bool Document::addField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700244 const std::vector<CJS_Value>& params,
245 CJS_Value& vRet,
246 CFX_WideString& sError) {
247 // Not supported.
248 return true;
249}
250
Tom Sepezb1670b52017-02-16 17:01:00 -0800251bool Document::exportAsText(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700252 const std::vector<CJS_Value>& params,
253 CJS_Value& vRet,
254 CFX_WideString& sError) {
255 // Unsafe, not supported.
256 return true;
257}
258
Tom Sepezb1670b52017-02-16 17:01:00 -0800259bool Document::exportAsFDF(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800260 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CJS_Value& vRet,
262 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -0800263 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700264 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Tom Sepezb1670b52017-02-16 17:01:00 -0800267bool Document::exportAsXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700268 const std::vector<CJS_Value>& params,
269 CJS_Value& vRet,
270 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700272 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275// Maps a field object in PDF document to a JavaScript variable
276// comment:
277// note: the paremter cName, this is clue how to treat if the cName is not a
278// valiable filed name in this document
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Tom Sepezb1670b52017-02-16 17:01:00 -0800280bool Document::getField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700281 const std::vector<CJS_Value>& params,
282 CJS_Value& vRet,
283 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700285 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700286 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
dsinclair82e17672016-10-11 12:38:01 -0700288 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700289 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700290 return false;
tsepez55be02e2016-09-12 11:21:42 -0700291 }
tsepezb4694242016-08-15 16:44:55 -0700292 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700293 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
295 if (pPDFForm->CountFields(wideName) <= 0) {
tsepezf3dc8c62016-08-10 06:29:29 -0700296 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -0700297 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 }
299
tsepezf3dc8c62016-08-10 06:29:29 -0700300 v8::Local<v8::Object> pFieldObj =
tsepezb4694242016-08-15 16:44:55 -0700301 pRuntime->NewFxDynamicObj(CJS_Field::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -0800302 if (pFieldObj.IsEmpty())
303 return false;
304
tsepezf3dc8c62016-08-10 06:29:29 -0700305 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700306 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700307 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 pField->AttachField(this, wideName);
tsepezf3dc8c62016-08-10 06:29:29 -0700309 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700310 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311}
312
313// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800314bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700315 const std::vector<CJS_Value>& params,
316 CJS_Value& vRet,
317 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700318 if (params.size() != 1) {
319 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700320 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700321 }
dsinclair82e17672016-10-11 12:38:01 -0700322 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700323 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700324 return false;
tsepez55be02e2016-09-12 11:21:42 -0700325 }
tsepezb4694242016-08-15 16:44:55 -0700326 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700328 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700329 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700331 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700333 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700335 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336
tsepezf3dc8c62016-08-10 06:29:29 -0700337 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700338 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339}
340
Tom Sepezb1670b52017-02-16 17:01:00 -0800341bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700342 const std::vector<CJS_Value>& params,
343 CJS_Value& vRet,
344 CFX_WideString& sError) {
345 // Unsafe, not supported.
346 return true;
347}
348
Tom Sepezb1670b52017-02-16 17:01:00 -0800349bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700350 const std::vector<CJS_Value>& params,
351 CJS_Value& vRet,
352 CFX_WideString& sError) {
353 // Unsafe, not supported.
354 return true;
355}
356
Tom Sepezb1670b52017-02-16 17:01:00 -0800357bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800358 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 CJS_Value& vRet,
360 CFX_WideString& sError) {
361 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700362 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363}
364
365// exports the form data and mails the resulting fdf file as an attachment to
366// all recipients.
367// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800368bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700369 const std::vector<CJS_Value>& params,
370 CJS_Value& vRet,
371 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700372 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700373 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700374 return false;
tsepez55be02e2016-09-12 11:21:42 -0700375 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700376 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700377 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700378 return false;
tsepez55be02e2016-09-12 11:21:42 -0700379 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700381 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700382 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
383 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
384 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700385 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700386 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
387 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700388 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 CFX_ByteTextBuf textBuf;
390 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700391 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800394 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700395 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
396 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
397 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700399 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400}
401
Tom Sepezb1670b52017-02-16 17:01:00 -0800402bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700403 const std::vector<CJS_Value>& params,
404 CJS_Value& vRet,
405 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700406 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700407 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700408 return false;
tsepez55be02e2016-09-12 11:21:42 -0700409 }
tsepez4cf55152016-11-02 14:37:54 -0700410 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 int nStart = 0;
412 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700413 bool bSilent = false;
414 bool bShrinkToFit = false;
415 bool bPrintAsImage = false;
416 bool bReverse = false;
417 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 int nlength = params.size();
419 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700420 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700421 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
422 if (CFXJS_Engine::GetObjDefnID(pObj) ==
423 CJS_PrintParamsObj::g_nObjDefnID) {
424 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700425 if (PrintParamsObj* pprintparamsObj =
426 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
427 bUI = pprintparamsObj->bUI;
428 nStart = pprintparamsObj->nStart;
429 nEnd = pprintparamsObj->nEnd;
430 bSilent = pprintparamsObj->bSilent;
431 bShrinkToFit = pprintparamsObj->bShrinkToFit;
432 bPrintAsImage = pprintparamsObj->bPrintAsImage;
433 bReverse = pprintparamsObj->bReverse;
434 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700436 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 }
438 }
439 } else {
440 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700441 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700443 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700445 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700447 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700449 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700451 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700453 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700455 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 }
457
dsinclair82e17672016-10-11 12:38:01 -0700458 if (m_pFormFillEnv) {
459 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
460 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700461 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 }
tsepez4cf55152016-11-02 14:37:54 -0700463 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464}
465
466// removes the specified field from the document.
467// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700468// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469
Tom Sepezb1670b52017-02-16 17:01:00 -0800470bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700471 const std::vector<CJS_Value>& params,
472 CJS_Value& vRet,
473 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700474 if (params.size() != 1) {
475 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700476 return false;
tsepez55be02e2016-09-12 11:21:42 -0700477 }
dsinclair82e17672016-10-11 12:38:01 -0700478 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700479 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700480 return false;
tsepez55be02e2016-09-12 11:21:42 -0700481 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700482 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
483 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700484 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700485 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 }
tsepezb4694242016-08-15 16:44:55 -0700487 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700488 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800489 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800490 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800491 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700492 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493
tsepez8fa82792017-01-11 09:32:33 -0800494 for (const auto& pAnnot : widgets) {
495 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
496 if (!pWidget)
497 continue;
498
Tom Sepez281a9ea2016-02-26 14:24:28 -0800499 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800500 --rcAnnot.left;
501 --rcAnnot.bottom;
502 ++rcAnnot.right;
503 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504
tsepezdf964df2016-04-21 12:09:41 -0700505 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800506 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800507 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508
dsinclair1897bdc2016-09-06 14:18:57 -0700509 // If there is currently no pageview associated with the page being used
510 // do not create one. We may be in the process of tearing down the document
511 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700512 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700513 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700514#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700515 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700516#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700517 pPageView->UpdateRects(aRefresh);
518 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700520 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521
tsepez4cf55152016-11-02 14:37:54 -0700522 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523}
524
525// reset filed values within a document.
526// comment:
527// note: if the fields names r not rational, aodbe is dumb for it.
528
Tom Sepezb1670b52017-02-16 17:01:00 -0800529bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700530 const std::vector<CJS_Value>& params,
531 CJS_Value& vRet,
532 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700533 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700534 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700535 return false;
tsepez55be02e2016-09-12 11:21:42 -0700536 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700537 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
538 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
539 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700540 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700541 return false;
tsepez55be02e2016-09-12 11:21:42 -0700542 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543
dsinclair7cbe68e2016-10-12 11:56:23 -0700544 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700546 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547
Lei Zhangdd734de2015-11-11 10:16:52 -0800548 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700549 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700550 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700551 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800552 }
553
554 switch (params[0].GetType()) {
555 default:
tsepezb4694242016-08-15 16:44:55 -0700556 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800557 break;
558 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700559 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800560 break;
561 }
562
563 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700564 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800565 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700566 aName.GetElement(pRuntime, i, valElement);
567 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800568 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
569 aFields.push_back(pPDFForm->GetField(j, swVal));
570 }
571
572 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700573 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700574 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 }
576
tsepez4cf55152016-11-02 14:37:54 -0700577 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578}
579
Tom Sepezb1670b52017-02-16 17:01:00 -0800580bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700581 const std::vector<CJS_Value>& params,
582 CJS_Value& vRet,
583 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700585 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586}
587
Tom Sepezb1670b52017-02-16 17:01:00 -0800588bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800589 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 CJS_Value& vRet,
591 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700592 return true;
593}
594
Tom Sepezb1670b52017-02-16 17:01:00 -0800595bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700596 const std::vector<CJS_Value>& params,
597 CJS_Value& vRet,
598 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 int nSize = params.size();
600 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700601 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700602 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 }
dsinclair82e17672016-10-11 12:38:01 -0700604 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700605 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700606 return false;
tsepez55be02e2016-09-12 11:21:42 -0700607 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800608
tsepeze5aff742016-08-08 09:49:42 -0700609 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700611 bool bFDF = true;
612 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700614 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700615 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700617 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700619 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700621 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700622 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700623 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
624 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700626 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700627
tsepezb4694242016-08-15 16:44:55 -0700628 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
629 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700630
tsepezb4694242016-08-15 16:44:55 -0700631 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
632 bEmpty = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700633
tsepezb4694242016-08-15 16:44:55 -0700634 pValue = pRuntime->GetObjectProperty(pObj, L"aFields");
635 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 }
637
dsinclair7cbe68e2016-10-12 11:56:23 -0700638 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700640 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800641 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700643 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700645 }
tsepez4cf55152016-11-02 14:37:54 -0700646 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647 }
648
Lei Zhangd88a3642015-11-10 09:38:57 -0800649 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700650 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700651 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700652 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653
tsepezb4694242016-08-15 16:44:55 -0700654 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
656 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
657 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
658 if (!bEmpty && pField->GetValue().IsEmpty())
659 continue;
660
Lei Zhangd88a3642015-11-10 09:38:57 -0800661 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 }
663 }
664
Lei Zhangd88a3642015-11-10 09:38:57 -0800665 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800667 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 pRuntime->EndBlock();
669 }
tsepez4cf55152016-11-02 14:37:54 -0700670 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700671}
672
dsinclair82e17672016-10-11 12:38:01 -0700673void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
674 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700675}
676
Tom Sepezb1670b52017-02-16 17:01:00 -0800677bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700678 CJS_PropValue& vp,
679 CFX_WideString& sError) {
680 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700681}
682
Tom Sepezb1670b52017-02-16 17:01:00 -0800683bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700684 const std::vector<CJS_Value>& params,
685 CJS_Value& vRet,
686 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700687 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700688 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 CFX_WideString cTo = L"";
690 CFX_WideString cCc = L"";
691 CFX_WideString cBcc = L"";
692 CFX_WideString cSubject = L"";
693 CFX_WideString cMsg = L"";
694
tsepezf3dc8c62016-08-10 06:29:29 -0700695 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700696 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700697 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700698 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700700 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700701 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700702 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700703 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700704 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700705 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700706 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707
Tom Sepez39bfe122015-09-17 15:25:23 -0700708 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700709 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710
tsepezb4694242016-08-15 16:44:55 -0700711 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700712 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713
tsepezb4694242016-08-15 16:44:55 -0700714 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
715 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716
tsepezb4694242016-08-15 16:44:55 -0700717 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
718 cCc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719
tsepezb4694242016-08-15 16:44:55 -0700720 pValue = pRuntime->GetObjectProperty(pObj, L"cBcc");
721 cBcc = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722
tsepezb4694242016-08-15 16:44:55 -0700723 pValue = pRuntime->GetObjectProperty(pObj, L"cSubject");
724 cSubject = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725
tsepezb4694242016-08-15 16:44:55 -0700726 pValue = pRuntime->GetObjectProperty(pObj, L"cMsg");
727 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 }
729
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700731 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
732 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
733 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700735 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736}
737
Tom Sepezb1670b52017-02-16 17:01:00 -0800738bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700739 CJS_PropValue& vp,
740 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800741 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700742}
743
Tom Sepezb1670b52017-02-16 17:01:00 -0800744bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700745 CJS_PropValue& vp,
746 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700747 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700748 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700749 return false;
tonikitooec268762016-08-10 12:29:28 -0700750 }
dsinclair82e17672016-10-11 12:38:01 -0700751 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700752 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700753 return false;
tsepez55be02e2016-09-12 11:21:42 -0700754 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700755 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700757 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800758
dsinclair38fd8442016-09-15 10:15:32 -0700759 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
760 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
761 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
762 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
763 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
764 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
765 CFX_WideString cwCreationDate =
766 pDictionary->GetUnicodeTextFor("CreationDate");
767 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
768 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800769
tsepezb4694242016-08-15 16:44:55 -0700770 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800771 pRuntime->PutObjectProperty(pObj, L"Author",
772 pRuntime->NewString(cwAuthor.AsStringC()));
773 pRuntime->PutObjectProperty(pObj, L"Title",
774 pRuntime->NewString(cwTitle.AsStringC()));
775 pRuntime->PutObjectProperty(pObj, L"Subject",
776 pRuntime->NewString(cwSubject.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800777 pRuntime->PutObjectProperty(pObj, L"Keywords",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800778 pRuntime->NewString(cwKeywords.AsStringC()));
779 pRuntime->PutObjectProperty(pObj, L"Creator",
780 pRuntime->NewString(cwCreator.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800781 pRuntime->PutObjectProperty(pObj, L"Producer",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800782 pRuntime->NewString(cwProducer.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800783 pRuntime->PutObjectProperty(pObj, L"CreationDate",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800784 pRuntime->NewString(cwCreationDate.AsStringC()));
785 pRuntime->PutObjectProperty(pObj, L"ModDate",
786 pRuntime->NewString(cwModDate.AsStringC()));
787 pRuntime->PutObjectProperty(pObj, L"Trapped",
788 pRuntime->NewString(cwTrapped.AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700789
790 // It's to be compatible to non-standard info dictionary.
791 for (const auto& it : *pDictionary) {
792 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800793 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700794 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
795 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800796 pRuntime->PutObjectProperty(
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800797 pObj, wsKey,
798 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700799 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800800 pRuntime->PutObjectProperty(pObj, wsKey,
801 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700802 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800803 pRuntime->PutObjectProperty(
804 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700805 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806 }
tonikitooec268762016-08-10 12:29:28 -0700807 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700808 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700809}
810
Tom Sepezb1670b52017-02-16 17:01:00 -0800811bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700812 CJS_PropValue& vp,
813 const CFX_ByteString& propName,
814 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700815 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700816 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700817 return false;
tsepez55be02e2016-09-12 11:21:42 -0700818 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700819 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700821 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
823 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700824 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700826 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700827 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700828 return false;
tsepez55be02e2016-09-12 11:21:42 -0700829 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700830 CFX_WideString csProperty;
831 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800832 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
833 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700834 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 }
tsepez4cf55152016-11-02 14:37:54 -0700836 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700837}
838
Tom Sepezb1670b52017-02-16 17:01:00 -0800839bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700840 CJS_PropValue& vp,
841 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800842 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700843}
844
Tom Sepezb1670b52017-02-16 17:01:00 -0800845bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700846 CJS_PropValue& vp,
847 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800848 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700849}
850
Tom Sepezb1670b52017-02-16 17:01:00 -0800851bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700852 CJS_PropValue& vp,
853 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700854 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700855 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700856 return false;
tsepez55be02e2016-09-12 11:21:42 -0700857 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 if (vp.IsGetting()) {
859 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800860 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800862 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
863 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
864 return false;
865 }
866 vp >> m_bDelay;
867 if (m_bDelay) {
868 m_DelayData.clear();
869 return true;
870 }
871 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
872 DelayDataToProcess.swap(m_DelayData);
873 for (const auto& pData : DelayDataToProcess)
874 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
875
tsepez4cf55152016-11-02 14:37:54 -0700876 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877}
878
Tom Sepezb1670b52017-02-16 17:01:00 -0800879bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700880 CJS_PropValue& vp,
881 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800882 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883}
884
Tom Sepezb1670b52017-02-16 17:01:00 -0800885bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700886 CJS_PropValue& vp,
887 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800888 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889}
890
Tom Sepezb1670b52017-02-16 17:01:00 -0800891bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700892 CJS_PropValue& vp,
893 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800894 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895}
896
Tom Sepezb1670b52017-02-16 17:01:00 -0800897bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700898 CJS_PropValue& vp,
899 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800900 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901}
902
Tom Sepezb1670b52017-02-16 17:01:00 -0800903bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700904 CJS_PropValue& vp,
905 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700906 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700907 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700908 return false;
tsepez55be02e2016-09-12 11:21:42 -0700909 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800910 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911}
912
Tom Sepezb1670b52017-02-16 17:01:00 -0800913bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700914 CJS_PropValue& vp,
915 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700917 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700918 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919 }
dsinclair82e17672016-10-11 12:38:01 -0700920 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700921 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700922 return false;
tsepez55be02e2016-09-12 11:21:42 -0700923 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700924 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700925 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926}
927
Tom Sepezb1670b52017-02-16 17:01:00 -0800928bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700929 CJS_PropValue& vp,
930 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700931 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700932 if (vp.IsGetting()) {
933 vp << true;
934 }
tsepez4cf55152016-11-02 14:37:54 -0700935 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936}
937
Tom Sepezb1670b52017-02-16 17:01:00 -0800938bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700939 CJS_PropValue& vp,
940 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700942 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700943 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 }
945 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700946 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947}
948
Tom Sepezb1670b52017-02-16 17:01:00 -0800949bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700950 CJS_PropValue& vp,
951 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700952 return true;
953}
954
Tom Sepezb1670b52017-02-16 17:01:00 -0800955bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700956 CJS_PropValue& vp,
957 CFX_WideString& sError) {
958 return true;
959}
960
Tom Sepezb1670b52017-02-16 17:01:00 -0800961bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800962 CJS_PropValue& vp,
963 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700964 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700965 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700966 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700967 }
dsinclair82e17672016-10-11 12:38:01 -0700968 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700969 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700970 return false;
tsepez55be02e2016-09-12 11:21:42 -0700971 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700972 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700973 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700974}
975
Tom Sepezb1670b52017-02-16 17:01:00 -0800976bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700977 CJS_PropValue& vp,
978 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 if (vp.IsGetting()) {
980 vp << m_cwBaseURL;
981 } else {
982 vp >> m_cwBaseURL;
983 }
tsepez4cf55152016-11-02 14:37:54 -0700984 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985}
986
Tom Sepezb1670b52017-02-16 17:01:00 -0800987bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700988 CJS_PropValue& vp,
989 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700990 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700991 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700992 return false;
tsepez55be02e2016-09-12 11:21:42 -0700993 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700994 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700996 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800997 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800999 bool bCalculate;
1000 vp >> bCalculate;
1001 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -07001002 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003}
1004
Tom Sepezb1670b52017-02-16 17:01:00 -08001005bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001006 CJS_PropValue& vp,
1007 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001009 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001010 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 }
dsinclair82e17672016-10-11 12:38:01 -07001012 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001013 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001014 return false;
tsepez55be02e2016-09-12 11:21:42 -07001015 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001016 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 int32_t i = wsFilePath.GetLength() - 1;
1018 for (; i >= 0; i--) {
1019 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1020 break;
1021 }
1022 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1023 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1024 } else {
1025 vp << L"";
1026 }
tsepez4cf55152016-11-02 14:37:54 -07001027 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028}
1029
Tom Sepezb1670b52017-02-16 17:01:00 -08001030bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001031 CJS_PropValue& vp,
1032 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001034 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001035 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036 }
dsinclair82e17672016-10-11 12:38:01 -07001037 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001038 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001039 return false;
tsepez55be02e2016-09-12 11:21:42 -07001040 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001041 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001042 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043}
1044
Tom Sepezb1670b52017-02-16 17:01:00 -08001045bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001046 CJS_PropValue& vp,
1047 CFX_WideString& sError) {
1048 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049}
1050
Tom Sepezb1670b52017-02-16 17:01:00 -08001051bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001052 CJS_PropValue& vp,
1053 CFX_WideString& sError) {
1054 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055}
1056
Tom Sepezb1670b52017-02-16 17:01:00 -08001057bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001058 const std::vector<CJS_Value>& params,
1059 CJS_Value& vRet,
1060 CFX_WideString& sError) {
1061 return true;
1062}
1063
Tom Sepezb1670b52017-02-16 17:01:00 -08001064bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001065 const std::vector<CJS_Value>& params,
1066 CJS_Value& vRet,
1067 CFX_WideString& sError) {
1068 return true;
1069}
1070
Tom Sepezb1670b52017-02-16 17:01:00 -08001071bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001072 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 CJS_Value& vRet,
1074 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001075 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076}
1077
Tom Sepezb1670b52017-02-16 17:01:00 -08001078bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001079 const std::vector<CJS_Value>& params,
1080 CJS_Value& vRet,
1081 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001082 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001083 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001084 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001085 }
dsinclair82e17672016-10-11 12:38:01 -07001086 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001087 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001088 return false;
tsepez55be02e2016-09-12 11:21:42 -07001089 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001090 int nPageNo = params[0].ToInt(pRuntime);
1091 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001092 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001093 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001094 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001095
tsepezd805eec2017-01-11 14:03:54 -08001096 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001097 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001098 for (const auto& pSDKAnnotCur : annotIteration) {
1099 CPDFSDK_BAAnnot* pBAAnnot =
1100 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001101 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1102 pSDKBAAnnot = pBAAnnot;
1103 break;
1104 }
1105 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001106 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001107 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001108
1109 v8::Local<v8::Object> pObj =
1110 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1111 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001112 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001113
1114 CJS_Annot* pJS_Annot =
1115 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo618cb1f2016-08-18 20:10:17 -07001116 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tonikitoo618cb1f2016-08-18 20:10:17 -07001117 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001118 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001119 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120}
1121
Tom Sepezb1670b52017-02-16 17:01:00 -08001122bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001123 const std::vector<CJS_Value>& params,
1124 CJS_Value& vRet,
1125 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001126 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001127 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001128 return false;
tsepez55be02e2016-09-12 11:21:42 -07001129 }
tonikitoo0ee35902016-08-26 14:41:29 -07001130 // TODO(tonikitoo): Add support supported parameters as per
1131 // the PDF spec.
1132
dsinclair7cbe68e2016-10-12 11:56:23 -07001133 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001134 CJS_Array annots;
1135
1136 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001137 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001138 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001139 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001140
tsepezd805eec2017-01-11 14:03:54 -08001141 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1142 for (const auto& pSDKAnnotCur : annotIteration) {
1143 if (!pSDKAnnotCur) {
1144 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001145 return false;
tsepezd805eec2017-01-11 14:03:54 -08001146 }
tonikitoo0ee35902016-08-26 14:41:29 -07001147 v8::Local<v8::Object> pObj =
1148 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1149 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001150 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001151
1152 CJS_Annot* pJS_Annot =
1153 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
tonikitoo0ee35902016-08-26 14:41:29 -07001154 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
tsepezd805eec2017-01-11 14:03:54 -08001155 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001156 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1157 }
1158 }
tonikitoo0ee35902016-08-26 14:41:29 -07001159 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001160 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161}
1162
Tom Sepezb1670b52017-02-16 17:01:00 -08001163bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001164 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 CJS_Value& vRet,
1166 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001167 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001168 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169}
1170
Tom Sepezb1670b52017-02-16 17:01:00 -08001171bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001172 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 CJS_Value& vRet,
1174 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001175 return true;
1176}
1177
Tom Sepezb1670b52017-02-16 17:01:00 -08001178bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001179 const std::vector<CJS_Value>& params,
1180 CJS_Value& vRet,
1181 CFX_WideString& sError) {
1182 return true;
1183}
1184
Tom Sepezb1670b52017-02-16 17:01:00 -08001185bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001186 const std::vector<CJS_Value>& params,
1187 CJS_Value& vRet,
1188 CFX_WideString& sError) {
1189 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190}
1191
1192bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1193 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1194 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1195}
1196
Tom Sepezb1670b52017-02-16 17:01:00 -08001197bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001198 const std::vector<CJS_Value>& params,
1199 CJS_Value& vRet,
1200 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001202 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001203 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 }
tsepezf3dc8c62016-08-10 06:29:29 -07001205
tsepezb4694242016-08-15 16:44:55 -07001206 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001207 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001208 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001209 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001210 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211
tsepezb4694242016-08-15 16:44:55 -07001212 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1213 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001214 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001215 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 }
1217
tsepezb4694242016-08-15 16:44:55 -07001218 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 if (!pEmbedObj) {
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
thestigdadea5f2016-10-19 13:29:48 -07001224 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1225 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001226 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227}
1228
Tom Sepezb1670b52017-02-16 17:01:00 -08001229bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001230 CJS_PropValue& vp,
1231 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001233 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001234 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235 }
thestigdadea5f2016-10-19 13:29:48 -07001236 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001237 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001238 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239 }
1240
tsepeze5aff742016-08-08 09:49:42 -07001241 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001242 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001243 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001244 v8::Local<v8::Object> pObj =
1245 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001246 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001247 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248
tsepezb4694242016-08-15 16:44:55 -07001249 CJS_Icon* pJS_Icon =
1250 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
thestigdadea5f2016-10-19 13:29:48 -07001251 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
dsinclair09bad1c2016-10-18 19:26:30 -07001252 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001253 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 }
1255
1256 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001257 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258}
1259
Tom Sepezb1670b52017-02-16 17:01:00 -08001260bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001261 const std::vector<CJS_Value>& params,
1262 CJS_Value& vRet,
1263 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001265 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001266 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 }
1268
thestigdadea5f2016-10-19 13:29:48 -07001269 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001270 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271
tsepezb4694242016-08-15 16:44:55 -07001272 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
thestigdadea5f2016-10-19 13:29:48 -07001273 for (const auto& pIconElement : m_Icons) {
1274 if (pIconElement->IconName != swIconName)
1275 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001276
thestigdadea5f2016-10-19 13:29:48 -07001277 v8::Local<v8::Object> pObj =
1278 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1279 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001280 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001281
thestigdadea5f2016-10-19 13:29:48 -07001282 CJS_Icon* pJS_Icon =
1283 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Tom Sepezc5a14722017-02-24 15:31:12 -08001284 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
thestigdadea5f2016-10-19 13:29:48 -07001285 pIcon->SetIconName(swIconName);
thestigdadea5f2016-10-19 13:29:48 -07001286 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001287 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001288 }
1289
tsepez4cf55152016-11-02 14:37:54 -07001290 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291}
1292
Tom Sepezb1670b52017-02-16 17:01:00 -08001293bool Document::removeIcon(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 Sepezdfbf8e72015-10-14 14:17:26 -07001297 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001298 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299}
1300
Tom Sepezb1670b52017-02-16 17:01:00 -08001301bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001302 const std::vector<CJS_Value>& params,
1303 CJS_Value& vRet,
1304 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001305 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001306 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001307}
1308
Tom Sepezb1670b52017-02-16 17:01:00 -08001309bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001310 CJS_PropValue& vp,
1311 CFX_WideString& sError) {
1312 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001313}
1314
Tom Sepezb1670b52017-02-16 17:01:00 -08001315bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001316 const std::vector<CJS_Value>& params,
1317 CJS_Value& vRet,
1318 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001319 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001320 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001321 return false;
tsepez55be02e2016-09-12 11:21:42 -07001322 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001323 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1324 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1325 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001326 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001327 return false;
tsepez55be02e2016-09-12 11:21:42 -07001328 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001329 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001330 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001331}
1332
Tom Sepezb1670b52017-02-16 17:01:00 -08001333bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001334 CJS_PropValue& vp,
1335 CFX_WideString& sError) {
1336 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001337}
1338
Tom Sepezb1670b52017-02-16 17:01:00 -08001339bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001340 const std::vector<CJS_Value>& params,
1341 CJS_Value& vRet,
1342 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001343 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001344 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001345 return false;
tsepez55be02e2016-09-12 11:21:42 -07001346 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001347 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001348 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001349 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001350 }
tsepez55be02e2016-09-12 11:21:42 -07001351
1352 // TODO(tsepez): check maximum allowable params.
1353
tsepezb4694242016-08-15 16:44:55 -07001354 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1355 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1356 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001357
dsinclair7cbe68e2016-10-12 11:56:23 -07001358 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001360 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001361
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001363 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001364 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001365 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001366
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1368 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001369 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001370
thestig5cc24652016-04-26 11:46:02 -07001371 CPDF_Page page(pDocument, pPageDict, true);
1372 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001373
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001376 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001377 if (pPageObj->IsText()) {
1378 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001379 int nObjWords = CountWords(pTextObj);
1380 if (nWords + nObjWords >= nWordNo) {
1381 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1382 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383 }
Tom Sepez2398d892016-02-17 16:46:26 -08001384 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001385 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001387
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001388 if (bStrip) {
1389 swRet.TrimLeft();
1390 swRet.TrimRight();
1391 }
1392
tsepezf3dc8c62016-08-10 06:29:29 -07001393 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001394 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001395}
1396
Tom Sepezb1670b52017-02-16 17:01:00 -08001397bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001398 const std::vector<CJS_Value>& params,
1399 CJS_Value& vRet,
1400 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001401 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001402 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001403 return false;
tsepez55be02e2016-09-12 11:21:42 -07001404 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001405 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001406 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001407 return false;
tsepez55be02e2016-09-12 11:21:42 -07001408 }
tsepez4cf55152016-11-02 14:37:54 -07001409 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410}
1411
Tom Sepezb1670b52017-02-16 17:01:00 -08001412bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001413 const std::vector<CJS_Value>& params,
1414 CJS_Value& vRet,
1415 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001416 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001417 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001418 return false;
tsepez55be02e2016-09-12 11:21:42 -07001419 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001420 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001421 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001422 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001423 }
tsepezb4694242016-08-15 16:44:55 -07001424 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001425 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001426 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001427 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001428 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429 }
1430
1431 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1432 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001433 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001434
thestig5cc24652016-04-26 11:46:02 -07001435 CPDF_Page page(pDocument, pPageDict, true);
1436 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001437
1438 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001439 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001440 if (pPageObj->IsText())
1441 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001442 }
1443
tsepezf3dc8c62016-08-10 06:29:29 -07001444 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001445 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446}
1447
Tom Sepezb1670b52017-02-16 17:01:00 -08001448bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001449 const std::vector<CJS_Value>& params,
1450 CJS_Value& vRet,
1451 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001452 v8::Local<v8::Object> pRetObj =
1453 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Tom Sepezc5a14722017-02-24 15:31:12 -08001454 if (pRetObj.IsEmpty())
1455 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456
1457 // Not implemented yet.
1458
tsepezf3dc8c62016-08-10 06:29:29 -07001459 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001460 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461}
1462
1463#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1464
1465int Document::CountWords(CPDF_TextObject* pTextObj) {
1466 if (!pTextObj)
1467 return 0;
1468
1469 int nWords = 0;
1470
1471 CPDF_Font* pFont = pTextObj->GetFont();
1472 if (!pFont)
1473 return 0;
1474
tsepez4cf55152016-11-02 14:37:54 -07001475 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476
1477 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001478 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 FX_FLOAT kerning;
1480
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001481 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1483
Tom Sepez62a70f92016-03-21 15:00:20 -07001484 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001485 if (swUnicode.GetLength() > 0)
1486 unicode = swUnicode[0];
1487
1488 if (ISLATINWORD(unicode) && bIsLatin)
1489 continue;
1490
1491 bIsLatin = ISLATINWORD(unicode);
1492 if (unicode != 0x20)
1493 nWords++;
1494 }
1495
1496 return nWords;
1497}
1498
1499CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1500 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 CFX_WideString swRet;
1502
1503 CPDF_Font* pFont = pTextObj->GetFont();
1504 if (!pFont)
1505 return L"";
1506
1507 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001508 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509
1510 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001511 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 FX_FLOAT kerning;
1513
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001514 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1516
Tom Sepez62a70f92016-03-21 15:00:20 -07001517 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 if (swUnicode.GetLength() > 0)
1519 unicode = swUnicode[0];
1520
1521 if (ISLATINWORD(unicode) && bIsLatin) {
1522 } else {
1523 bIsLatin = ISLATINWORD(unicode);
1524 if (unicode != 0x20)
1525 nWords++;
1526 }
1527
1528 if (nWords - 1 == nWordIndex)
1529 swRet += unicode;
1530 }
1531
1532 return swRet;
1533}
1534
Tom Sepezb1670b52017-02-16 17:01:00 -08001535bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001536 CJS_PropValue& vp,
1537 CFX_WideString& sError) {
1538 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001539}
1540
1541/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001542(none, NoVary)
1543(fitP, FitPage)
1544(fitW, FitWidth)
1545(fitH, FitHeight)
1546(fitV, FitVisibleWidth)
1547(pref, Preferred)
1548(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001549*/
1550
Tom Sepezb1670b52017-02-16 17:01:00 -08001551bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001552 CJS_PropValue& vp,
1553 CFX_WideString& sError) {
1554 return true;
1555}
1556
Tom Sepezb1670b52017-02-16 17:01:00 -08001557bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001558 const std::vector<CJS_Value>& params,
1559 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001560 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001561 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001562 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001563}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001564
Tom Sepezb1670b52017-02-16 17:01:00 -08001565bool Document::extractPages(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::insertPages(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::replacePages(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::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001590 const std::vector<CJS_Value>& params,
1591 CJS_Value& vRet,
1592 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001593 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001594 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001595}
1596
Tom Sepezb1670b52017-02-16 17:01:00 -08001597bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001598 const std::vector<CJS_Value>& params,
1599 CJS_Value& vRet,
1600 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001601 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001602 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001603 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001604 }
dsinclair82e17672016-10-11 12:38:01 -07001605 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001606 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001607 return false;
tsepez55be02e2016-09-12 11:21:42 -07001608 }
tsepezb4694242016-08-15 16:44:55 -07001609 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001610 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001611 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001612 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001613 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001614
tonikitoo1c836752016-08-08 16:14:05 -07001615 CPDF_NameTree nameTree(pDocument, "Dests");
1616 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1617 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001618 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001619
1620 CPDF_Dest dest(destArray);
1621 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1622
1623 std::unique_ptr<float[]> scrollPositionArray;
1624 int scrollPositionArraySize = 0;
1625
1626 if (arrayObject) {
1627 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1628 int j = 0;
1629 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1630 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1631 scrollPositionArraySize = j;
1632 }
1633
tsepezb4694242016-08-15 16:44:55 -07001634 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001635 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1636 scrollPositionArray.get(),
1637 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001638 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001639
tsepez4cf55152016-11-02 14:37:54 -07001640 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001641}
1642
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001643void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001644 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645}
1646
1647void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1648 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001649 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1650 auto iter = m_DelayData.begin();
1651 while (iter != m_DelayData.end()) {
1652 auto old = iter++;
1653 if ((*old)->sFieldName == sFieldName &&
1654 (*old)->nControlIndex == nControlIndex) {
1655 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1656 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 }
1658 }
1659
dsinclair82e17672016-10-11 12:38:01 -07001660 for (const auto& pData : DelayDataForFieldAndControlIndex)
1661 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001662}
1663
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001664CJS_Document* Document::GetCJSDoc() const {
1665 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001666}