blob: 536d65446398cf84c0e0a9bb8f04c7e4884300f7 [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);
tsepezf3dc8c62016-08-10 06:29:29 -0700302 CJS_Field* pJSField =
tsepezb4694242016-08-15 16:44:55 -0700303 static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
tsepezf3dc8c62016-08-10 06:29:29 -0700304 Field* pField = static_cast<Field*>(pJSField->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 pField->AttachField(this, wideName);
306
tsepezf3dc8c62016-08-10 06:29:29 -0700307 vRet = CJS_Value(pRuntime, pJSField);
tsepez4cf55152016-11-02 14:37:54 -0700308 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309}
310
311// Gets the name of the nth field in the document
Tom Sepezb1670b52017-02-16 17:01:00 -0800312bool Document::getNthFieldName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700313 const std::vector<CJS_Value>& params,
314 CJS_Value& vRet,
315 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700316 if (params.size() != 1) {
317 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700318 return false;
tsepezcd5dc852016-09-08 11:23:24 -0700319 }
dsinclair82e17672016-10-11 12:38:01 -0700320 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700321 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700322 return false;
tsepez55be02e2016-09-12 11:21:42 -0700323 }
tsepezb4694242016-08-15 16:44:55 -0700324 int nIndex = params[0].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 if (nIndex < 0) {
tsepezcd5dc852016-09-08 11:23:24 -0700326 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700327 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700329 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
thestig7c292e02016-09-28 14:14:26 -0700331 CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 if (!pField)
tsepez4cf55152016-11-02 14:37:54 -0700333 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334
tsepezf3dc8c62016-08-10 06:29:29 -0700335 vRet = CJS_Value(pRuntime, pField->GetFullName().c_str());
tsepez4cf55152016-11-02 14:37:54 -0700336 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337}
338
Tom Sepezb1670b52017-02-16 17:01:00 -0800339bool Document::importAnFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700340 const std::vector<CJS_Value>& params,
341 CJS_Value& vRet,
342 CFX_WideString& sError) {
343 // Unsafe, not supported.
344 return true;
345}
346
Tom Sepezb1670b52017-02-16 17:01:00 -0800347bool Document::importAnXFDF(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700348 const std::vector<CJS_Value>& params,
349 CJS_Value& vRet,
350 CFX_WideString& sError) {
351 // Unsafe, not supported.
352 return true;
353}
354
Tom Sepezb1670b52017-02-16 17:01:00 -0800355bool Document::importTextData(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800356 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 CJS_Value& vRet,
358 CFX_WideString& sError) {
359 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700360 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361}
362
363// exports the form data and mails the resulting fdf file as an attachment to
364// all recipients.
365// comment: need reader supports
Tom Sepezb1670b52017-02-16 17:01:00 -0800366bool Document::mailForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700367 const std::vector<CJS_Value>& params,
368 CJS_Value& vRet,
369 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700370 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700371 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700372 return false;
tsepez55be02e2016-09-12 11:21:42 -0700373 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700374 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -0700375 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700376 return false;
tsepez55be02e2016-09-12 11:21:42 -0700377 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 int iLength = params.size();
tsepez4cf55152016-11-02 14:37:54 -0700379 bool bUI = iLength > 0 ? params[0].ToBool(pRuntime) : true;
tsepezb4694242016-08-15 16:44:55 -0700380 CFX_WideString cTo = iLength > 1 ? params[1].ToCFXWideString(pRuntime) : L"";
381 CFX_WideString cCc = iLength > 2 ? params[2].ToCFXWideString(pRuntime) : L"";
382 CFX_WideString cBcc = iLength > 3 ? params[3].ToCFXWideString(pRuntime) : L"";
tsepezf3dc8c62016-08-10 06:29:29 -0700383 CFX_WideString cSubject =
tsepezb4694242016-08-15 16:44:55 -0700384 iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
385 CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
dsinclair7cbe68e2016-10-12 11:56:23 -0700386 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CFX_ByteTextBuf textBuf;
388 if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
tsepez4cf55152016-11-02 14:37:54 -0700389 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 pRuntime->BeginBlock();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800392 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
dsinclair8779fa82016-10-12 12:05:44 -0700393 pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
394 cTo.c_str(), cSubject.c_str(), cCc.c_str(),
395 cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700397 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398}
399
Tom Sepezb1670b52017-02-16 17:01:00 -0800400bool Document::print(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700401 const std::vector<CJS_Value>& params,
402 CJS_Value& vRet,
403 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700404 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700405 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700406 return false;
tsepez55be02e2016-09-12 11:21:42 -0700407 }
tsepez4cf55152016-11-02 14:37:54 -0700408 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 int nStart = 0;
410 int nEnd = 0;
tsepez4cf55152016-11-02 14:37:54 -0700411 bool bSilent = false;
412 bool bShrinkToFit = false;
413 bool bPrintAsImage = false;
414 bool bReverse = false;
415 bool bAnnotations = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 int nlength = params.size();
417 if (nlength == 9) {
tsepez40faa792016-07-15 17:58:02 -0700418 if (params[8].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700419 v8::Local<v8::Object> pObj = params[8].ToV8Object(pRuntime);
420 if (CFXJS_Engine::GetObjDefnID(pObj) ==
421 CJS_PrintParamsObj::g_nObjDefnID) {
422 if (CJS_Object* pJSObj = params[8].ToCJSObject(pRuntime)) {
tsepez40faa792016-07-15 17:58:02 -0700423 if (PrintParamsObj* pprintparamsObj =
424 static_cast<PrintParamsObj*>(pJSObj->GetEmbedObject())) {
425 bUI = pprintparamsObj->bUI;
426 nStart = pprintparamsObj->nStart;
427 nEnd = pprintparamsObj->nEnd;
428 bSilent = pprintparamsObj->bSilent;
429 bShrinkToFit = pprintparamsObj->bShrinkToFit;
430 bPrintAsImage = pprintparamsObj->bPrintAsImage;
431 bReverse = pprintparamsObj->bReverse;
432 bAnnotations = pprintparamsObj->bAnnotations;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700434 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 }
436 }
437 } else {
438 if (nlength >= 1)
tsepezb4694242016-08-15 16:44:55 -0700439 bUI = params[0].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 if (nlength >= 2)
tsepezb4694242016-08-15 16:44:55 -0700441 nStart = params[1].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (nlength >= 3)
tsepezb4694242016-08-15 16:44:55 -0700443 nEnd = params[2].ToInt(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (nlength >= 4)
tsepezb4694242016-08-15 16:44:55 -0700445 bSilent = params[3].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 if (nlength >= 5)
tsepezb4694242016-08-15 16:44:55 -0700447 bShrinkToFit = params[4].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (nlength >= 6)
tsepezb4694242016-08-15 16:44:55 -0700449 bPrintAsImage = params[5].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 if (nlength >= 7)
tsepezb4694242016-08-15 16:44:55 -0700451 bReverse = params[6].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (nlength >= 8)
tsepezb4694242016-08-15 16:44:55 -0700453 bAnnotations = params[7].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 }
455
dsinclair82e17672016-10-11 12:38:01 -0700456 if (m_pFormFillEnv) {
457 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
458 bPrintAsImage, bReverse, bAnnotations);
tsepez4cf55152016-11-02 14:37:54 -0700459 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 }
tsepez4cf55152016-11-02 14:37:54 -0700461 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462}
463
464// removes the specified field from the document.
465// comment:
dsinclair1897bdc2016-09-06 14:18:57 -0700466// note: if the filed name is not rational, adobe is dumb for it.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467
Tom Sepezb1670b52017-02-16 17:01:00 -0800468bool Document::removeField(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700469 const std::vector<CJS_Value>& params,
470 CJS_Value& vRet,
471 CFX_WideString& sError) {
tsepez55be02e2016-09-12 11:21:42 -0700472 if (params.size() != 1) {
473 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700474 return false;
tsepez55be02e2016-09-12 11:21:42 -0700475 }
dsinclair82e17672016-10-11 12:38:01 -0700476 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700477 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
tsepez55be02e2016-09-12 11:21:42 -0700479 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700480 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
481 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
tsepezcd5dc852016-09-08 11:23:24 -0700482 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700483 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 }
tsepezb4694242016-08-15 16:44:55 -0700485 CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -0700486 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
tsepez8fa82792017-01-11 09:32:33 -0800487 std::vector<CPDFSDK_Annot::ObservedPtr> widgets;
Lei Zhangd88a3642015-11-10 09:38:57 -0800488 pInterForm->GetWidgets(sFieldName, &widgets);
Lei Zhangd88a3642015-11-10 09:38:57 -0800489 if (widgets.empty())
tsepez4cf55152016-11-02 14:37:54 -0700490 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491
tsepez8fa82792017-01-11 09:32:33 -0800492 for (const auto& pAnnot : widgets) {
493 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot.Get());
494 if (!pWidget)
495 continue;
496
Tom Sepez281a9ea2016-02-26 14:24:28 -0800497 CFX_FloatRect rcAnnot = pWidget->GetRect();
Lei Zhangd88a3642015-11-10 09:38:57 -0800498 --rcAnnot.left;
499 --rcAnnot.bottom;
500 ++rcAnnot.right;
501 ++rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502
tsepezdf964df2016-04-21 12:09:41 -0700503 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800504 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Lei Zhangd88a3642015-11-10 09:38:57 -0800505 ASSERT(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506
dsinclair1897bdc2016-09-06 14:18:57 -0700507 // If there is currently no pageview associated with the page being used
508 // do not create one. We may be in the process of tearing down the document
509 // and creating a new pageview at this point will cause bad things.
dsinclair7cbe68e2016-10-12 11:56:23 -0700510 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
dsinclair1897bdc2016-09-06 14:18:57 -0700511 if (pPageView) {
dsinclair8afe15a2016-10-05 12:00:34 -0700512#if PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700513 pPageView->DeleteAnnot(pWidget);
dsinclair8afe15a2016-10-05 12:00:34 -0700514#endif // PDF_ENABLE_XFA
dsinclair1897bdc2016-09-06 14:18:57 -0700515 pPageView->UpdateRects(aRefresh);
516 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700518 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519
tsepez4cf55152016-11-02 14:37:54 -0700520 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521}
522
523// reset filed values within a document.
524// comment:
525// note: if the fields names r not rational, aodbe is dumb for it.
526
Tom Sepezb1670b52017-02-16 17:01:00 -0800527bool Document::resetForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700528 const std::vector<CJS_Value>& params,
529 CJS_Value& vRet,
530 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700531 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700532 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700533 return false;
tsepez55be02e2016-09-12 11:21:42 -0700534 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700535 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
536 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
537 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -0700538 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700539 return false;
tsepez55be02e2016-09-12 11:21:42 -0700540 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541
dsinclair7cbe68e2016-10-12 11:56:23 -0700542 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
tsepeze5aff742016-08-08 09:49:42 -0700544 CJS_Array aName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545
Lei Zhangdd734de2015-11-11 10:16:52 -0800546 if (params.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700547 pPDFForm->ResetForm(true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700548 m_pFormFillEnv->SetChangeMark();
tsepez4cf55152016-11-02 14:37:54 -0700549 return true;
Lei Zhangd88a3642015-11-10 09:38:57 -0800550 }
551
552 switch (params[0].GetType()) {
553 default:
tsepezb4694242016-08-15 16:44:55 -0700554 aName.Attach(params[0].ToV8Array(pRuntime));
Lei Zhangd88a3642015-11-10 09:38:57 -0800555 break;
556 case CJS_Value::VT_string:
tsepezb4694242016-08-15 16:44:55 -0700557 aName.SetElement(pRuntime, 0, params[0]);
Lei Zhangd88a3642015-11-10 09:38:57 -0800558 break;
559 }
560
561 std::vector<CPDF_FormField*> aFields;
tsepezb4694242016-08-15 16:44:55 -0700562 for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800563 CJS_Value valElement(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700564 aName.GetElement(pRuntime, i, valElement);
565 CFX_WideString swVal = valElement.ToCFXWideString(pRuntime);
Lei Zhangd88a3642015-11-10 09:38:57 -0800566 for (int j = 0, jsz = pPDFForm->CountFields(swVal); j < jsz; ++j)
567 aFields.push_back(pPDFForm->GetField(j, swVal));
568 }
569
570 if (!aFields.empty()) {
tsepez4cf55152016-11-02 14:37:54 -0700571 pPDFForm->ResetForm(aFields, true, true);
dsinclair7cbe68e2016-10-12 11:56:23 -0700572 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 }
574
tsepez4cf55152016-11-02 14:37:54 -0700575 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576}
577
Tom Sepezb1670b52017-02-16 17:01:00 -0800578bool Document::saveAs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700579 const std::vector<CJS_Value>& params,
580 CJS_Value& vRet,
581 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -0700583 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584}
585
Tom Sepezb1670b52017-02-16 17:01:00 -0800586bool Document::syncAnnotScan(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -0800587 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 CJS_Value& vRet,
589 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700590 return true;
591}
592
Tom Sepezb1670b52017-02-16 17:01:00 -0800593bool Document::submitForm(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700594 const std::vector<CJS_Value>& params,
595 CJS_Value& vRet,
596 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597 int nSize = params.size();
598 if (nSize < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700599 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700600 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 }
dsinclair82e17672016-10-11 12:38:01 -0700602 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700603 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700604 return false;
tsepez55be02e2016-09-12 11:21:42 -0700605 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800606
tsepeze5aff742016-08-08 09:49:42 -0700607 CJS_Array aFields;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 CFX_WideString strURL;
tsepez4cf55152016-11-02 14:37:54 -0700609 bool bFDF = true;
610 bool bEmpty = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 CJS_Value v = params[0];
Tom Sepez39bfe122015-09-17 15:25:23 -0700612 if (v.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700613 strURL = params[0].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 if (nSize > 1)
tsepezb4694242016-08-15 16:44:55 -0700615 bFDF = params[1].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 if (nSize > 2)
tsepezb4694242016-08-15 16:44:55 -0700617 bEmpty = params[2].ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 if (nSize > 3)
tsepezb4694242016-08-15 16:44:55 -0700619 aFields.Attach(params[3].ToV8Array(pRuntime));
Tom Sepez39bfe122015-09-17 15:25:23 -0700620 } else if (v.GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700621 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
622 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"cURL");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 if (!pValue.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700624 strURL = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700625
tsepezb4694242016-08-15 16:44:55 -0700626 pValue = pRuntime->GetObjectProperty(pObj, L"bFDF");
627 bFDF = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700628
tsepezb4694242016-08-15 16:44:55 -0700629 pValue = pRuntime->GetObjectProperty(pObj, L"bEmpty");
630 bEmpty = 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"aFields");
633 aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 }
635
dsinclair7cbe68e2016-10-12 11:56:23 -0700636 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
tsepezb4694242016-08-15 16:44:55 -0700638 if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
Lei Zhangd88a3642015-11-10 09:38:57 -0800639 if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 pRuntime->BeginBlock();
tsepez4cf55152016-11-02 14:37:54 -0700641 pInterForm->SubmitForm(strURL, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 pRuntime->EndBlock();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700643 }
tsepez4cf55152016-11-02 14:37:54 -0700644 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 }
646
Lei Zhangd88a3642015-11-10 09:38:57 -0800647 std::vector<CPDF_FormField*> fieldObjects;
tsepezb4694242016-08-15 16:44:55 -0700648 for (int i = 0, sz = aFields.GetLength(pRuntime); i < sz; ++i) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700649 CJS_Value valName(pRuntime);
tsepezb4694242016-08-15 16:44:55 -0700650 aFields.GetElement(pRuntime, i, valName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651
tsepezb4694242016-08-15 16:44:55 -0700652 CFX_WideString sName = valName.ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653 CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
654 for (int j = 0, jsz = pPDFForm->CountFields(sName); j < jsz; ++j) {
655 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
656 if (!bEmpty && pField->GetValue().IsEmpty())
657 continue;
658
Lei Zhangd88a3642015-11-10 09:38:57 -0800659 fieldObjects.push_back(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 }
661 }
662
Lei Zhangd88a3642015-11-10 09:38:57 -0800663 if (pPDFInterForm->CheckRequiredFields(&fieldObjects, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 pRuntime->BeginBlock();
Wei Li97da9762016-03-11 17:00:48 -0800665 pInterForm->SubmitFields(strURL, fieldObjects, true, !bFDF);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 pRuntime->EndBlock();
667 }
tsepez4cf55152016-11-02 14:37:54 -0700668 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700669}
670
dsinclair82e17672016-10-11 12:38:01 -0700671void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
672 m_pFormFillEnv.Reset(pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Tom Sepezb1670b52017-02-16 17:01:00 -0800675bool Document::bookmarkRoot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700676 CJS_PropValue& vp,
677 CFX_WideString& sError) {
678 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679}
680
Tom Sepezb1670b52017-02-16 17:01:00 -0800681bool Document::mailDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700682 const std::vector<CJS_Value>& params,
683 CJS_Value& vRet,
684 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700685 // TODO(tsepez): Check maximum number of allowed params.
tsepez4cf55152016-11-02 14:37:54 -0700686 bool bUI = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 CFX_WideString cTo = L"";
688 CFX_WideString cCc = L"";
689 CFX_WideString cBcc = L"";
690 CFX_WideString cSubject = L"";
691 CFX_WideString cMsg = L"";
692
tsepezf3dc8c62016-08-10 06:29:29 -0700693 if (params.size() >= 1)
tsepezb4694242016-08-15 16:44:55 -0700694 bUI = params[0].ToBool(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700695 if (params.size() >= 2)
tsepezb4694242016-08-15 16:44:55 -0700696 cTo = params[1].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700697 if (params.size() >= 3)
tsepezb4694242016-08-15 16:44:55 -0700698 cCc = params[2].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700699 if (params.size() >= 4)
tsepezb4694242016-08-15 16:44:55 -0700700 cBcc = params[3].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700701 if (params.size() >= 5)
tsepezb4694242016-08-15 16:44:55 -0700702 cSubject = params[4].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -0700703 if (params.size() >= 6)
tsepezb4694242016-08-15 16:44:55 -0700704 cMsg = params[5].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705
Tom Sepez39bfe122015-09-17 15:25:23 -0700706 if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) {
tsepezb4694242016-08-15 16:44:55 -0700707 v8::Local<v8::Object> pObj = params[0].ToV8Object(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708
tsepezb4694242016-08-15 16:44:55 -0700709 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, L"bUI");
tsepez71ba5882016-10-28 09:35:49 -0700710 bUI = CJS_Value(pRuntime, pValue).ToBool(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
tsepezb4694242016-08-15 16:44:55 -0700712 pValue = pRuntime->GetObjectProperty(pObj, L"cTo");
713 cTo = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714
tsepezb4694242016-08-15 16:44:55 -0700715 pValue = pRuntime->GetObjectProperty(pObj, L"cCc");
716 cCc = 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"cBcc");
719 cBcc = 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"cSubject");
722 cSubject = 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"cMsg");
725 cMsg = CJS_Value(pRuntime, pValue).ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 }
727
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 pRuntime->BeginBlock();
dsinclair8779fa82016-10-12 12:05:44 -0700729 CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
730 pFormFillEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
731 cCc.c_str(), cBcc.c_str(), cMsg.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 pRuntime->EndBlock();
tsepez4cf55152016-11-02 14:37:54 -0700733 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734}
735
Tom Sepezb1670b52017-02-16 17:01:00 -0800736bool Document::author(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700737 CJS_PropValue& vp,
738 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800739 return getPropertyInternal(pRuntime, vp, "Author", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700740}
741
Tom Sepezb1670b52017-02-16 17:01:00 -0800742bool Document::info(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700743 CJS_PropValue& vp,
744 CFX_WideString& sError) {
tonikitooec268762016-08-10 12:29:28 -0700745 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700746 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700747 return false;
tonikitooec268762016-08-10 12:29:28 -0700748 }
dsinclair82e17672016-10-11 12:38:01 -0700749 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700750 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700751 return false;
tsepez55be02e2016-09-12 11:21:42 -0700752 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700753 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700755 return false;
Tom Sepezc6ab1722015-02-05 15:27:25 -0800756
dsinclair38fd8442016-09-15 10:15:32 -0700757 CFX_WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
758 CFX_WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
759 CFX_WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
760 CFX_WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
761 CFX_WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
762 CFX_WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
763 CFX_WideString cwCreationDate =
764 pDictionary->GetUnicodeTextFor("CreationDate");
765 CFX_WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
766 CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
Tom Sepezc6ab1722015-02-05 15:27:25 -0800767
tsepezb4694242016-08-15 16:44:55 -0700768 v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1);
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800769 pRuntime->PutObjectProperty(pObj, L"Author",
770 pRuntime->NewString(cwAuthor.AsStringC()));
771 pRuntime->PutObjectProperty(pObj, L"Title",
772 pRuntime->NewString(cwTitle.AsStringC()));
773 pRuntime->PutObjectProperty(pObj, L"Subject",
774 pRuntime->NewString(cwSubject.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800775 pRuntime->PutObjectProperty(pObj, L"Keywords",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800776 pRuntime->NewString(cwKeywords.AsStringC()));
777 pRuntime->PutObjectProperty(pObj, L"Creator",
778 pRuntime->NewString(cwCreator.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800779 pRuntime->PutObjectProperty(pObj, L"Producer",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800780 pRuntime->NewString(cwProducer.AsStringC()));
tsepeze6cf0132017-01-18 14:38:18 -0800781 pRuntime->PutObjectProperty(pObj, L"CreationDate",
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800782 pRuntime->NewString(cwCreationDate.AsStringC()));
783 pRuntime->PutObjectProperty(pObj, L"ModDate",
784 pRuntime->NewString(cwModDate.AsStringC()));
785 pRuntime->PutObjectProperty(pObj, L"Trapped",
786 pRuntime->NewString(cwTrapped.AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700787
788 // It's to be compatible to non-standard info dictionary.
789 for (const auto& it : *pDictionary) {
790 const CFX_ByteString& bsKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800791 CPDF_Object* pValueObj = it.second.get();
tonikitooec268762016-08-10 12:29:28 -0700792 CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
793 if (pValueObj->IsString() || pValueObj->IsName()) {
tsepeze6cf0132017-01-18 14:38:18 -0800794 pRuntime->PutObjectProperty(
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800795 pObj, wsKey,
796 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC()));
tonikitooec268762016-08-10 12:29:28 -0700797 } else if (pValueObj->IsNumber()) {
tsepeze6cf0132017-01-18 14:38:18 -0800798 pRuntime->PutObjectProperty(pObj, wsKey,
799 pRuntime->NewNumber(pValueObj->GetNumber()));
tonikitooec268762016-08-10 12:29:28 -0700800 } else if (pValueObj->IsBoolean()) {
tsepeze6cf0132017-01-18 14:38:18 -0800801 pRuntime->PutObjectProperty(
802 pObj, wsKey, pRuntime->NewBoolean(!!pValueObj->GetInteger()));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700803 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700804 }
tonikitooec268762016-08-10 12:29:28 -0700805 vp << pObj;
tsepez4cf55152016-11-02 14:37:54 -0700806 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700807}
808
Tom Sepezb1670b52017-02-16 17:01:00 -0800809bool Document::getPropertyInternal(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700810 CJS_PropValue& vp,
811 const CFX_ByteString& propName,
812 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700813 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700814 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700815 return false;
tsepez55be02e2016-09-12 11:21:42 -0700816 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700817 CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 if (!pDictionary)
tsepez4cf55152016-11-02 14:37:54 -0700819 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820
821 if (vp.IsGetting()) {
dsinclair38fd8442016-09-15 10:15:32 -0700822 vp << pDictionary->GetUnicodeTextFor(propName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 } else {
dsinclair7cbe68e2016-10-12 11:56:23 -0700824 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
tsepez55be02e2016-09-12 11:21:42 -0700825 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -0700826 return false;
tsepez55be02e2016-09-12 11:21:42 -0700827 }
tonikitooea3ff9e2016-08-02 11:52:28 -0700828 CFX_WideString csProperty;
829 vp >> csProperty;
tsepez0e606b52016-11-18 16:22:41 -0800830 pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
831 false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700832 m_pFormFillEnv->SetChangeMark();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833 }
tsepez4cf55152016-11-02 14:37:54 -0700834 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700835}
836
Tom Sepezb1670b52017-02-16 17:01:00 -0800837bool Document::creationDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700838 CJS_PropValue& vp,
839 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800840 return getPropertyInternal(pRuntime, vp, "CreationDate", sError);
tonikitooea3ff9e2016-08-02 11:52:28 -0700841}
842
Tom Sepezb1670b52017-02-16 17:01:00 -0800843bool Document::creator(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700844 CJS_PropValue& vp,
845 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800846 return getPropertyInternal(pRuntime, vp, "Creator", sError);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700847}
848
Tom Sepezb1670b52017-02-16 17:01:00 -0800849bool Document::delay(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700850 CJS_PropValue& vp,
851 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700852 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700853 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700854 return false;
tsepez55be02e2016-09-12 11:21:42 -0700855 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 if (vp.IsGetting()) {
857 vp << m_bDelay;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800858 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800860 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
861 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
862 return false;
863 }
864 vp >> m_bDelay;
865 if (m_bDelay) {
866 m_DelayData.clear();
867 return true;
868 }
869 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
870 DelayDataToProcess.swap(m_DelayData);
871 for (const auto& pData : DelayDataToProcess)
872 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
873
tsepez4cf55152016-11-02 14:37:54 -0700874 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875}
876
Tom Sepezb1670b52017-02-16 17:01:00 -0800877bool Document::keywords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700878 CJS_PropValue& vp,
879 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800880 return getPropertyInternal(pRuntime, vp, "Keywords", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881}
882
Tom Sepezb1670b52017-02-16 17:01:00 -0800883bool Document::modDate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700884 CJS_PropValue& vp,
885 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800886 return getPropertyInternal(pRuntime, vp, "ModDate", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887}
888
Tom Sepezb1670b52017-02-16 17:01:00 -0800889bool Document::producer(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700890 CJS_PropValue& vp,
891 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800892 return getPropertyInternal(pRuntime, vp, "Producer", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893}
894
Tom Sepezb1670b52017-02-16 17:01:00 -0800895bool Document::subject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700896 CJS_PropValue& vp,
897 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -0800898 return getPropertyInternal(pRuntime, vp, "Subject", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899}
900
Tom Sepezb1670b52017-02-16 17:01:00 -0800901bool Document::title(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700902 CJS_PropValue& vp,
903 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700904 if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
tsepez55be02e2016-09-12 11:21:42 -0700905 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700906 return false;
tsepez55be02e2016-09-12 11:21:42 -0700907 }
Tom Sepezb1670b52017-02-16 17:01:00 -0800908 return getPropertyInternal(pRuntime, vp, "Title", sError);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909}
910
Tom Sepezb1670b52017-02-16 17:01:00 -0800911bool Document::numPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700912 CJS_PropValue& vp,
913 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700915 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700916 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 }
dsinclair82e17672016-10-11 12:38:01 -0700918 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700919 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700920 return false;
tsepez55be02e2016-09-12 11:21:42 -0700921 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700922 vp << m_pFormFillEnv->GetPageCount();
tsepez4cf55152016-11-02 14:37:54 -0700923 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924}
925
Tom Sepezb1670b52017-02-16 17:01:00 -0800926bool Document::external(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700927 CJS_PropValue& vp,
928 CFX_WideString& sError) {
tsepezcd5dc852016-09-08 11:23:24 -0700929 // In Chrome case, should always return true.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 if (vp.IsGetting()) {
931 vp << true;
932 }
tsepez4cf55152016-11-02 14:37:54 -0700933 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934}
935
Tom Sepezb1670b52017-02-16 17:01:00 -0800936bool Document::filesize(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700937 CJS_PropValue& vp,
938 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700940 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700941 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 }
943 vp << 0;
tsepez4cf55152016-11-02 14:37:54 -0700944 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945}
946
Tom Sepezb1670b52017-02-16 17:01:00 -0800947bool Document::mouseX(CJS_Runtime* pRuntime,
tonikitoo33c4cdb2016-08-08 10:52:51 -0700948 CJS_PropValue& vp,
949 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700950 return true;
951}
952
Tom Sepezb1670b52017-02-16 17:01:00 -0800953bool Document::mouseY(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700954 CJS_PropValue& vp,
955 CFX_WideString& sError) {
956 return true;
957}
958
Tom Sepezb1670b52017-02-16 17:01:00 -0800959bool Document::URL(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800960 CJS_PropValue& vp,
961 CFX_WideString& sError) {
tonikitoo33c4cdb2016-08-08 10:52:51 -0700962 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -0700963 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -0700964 return false;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700965 }
dsinclair82e17672016-10-11 12:38:01 -0700966 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700967 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700968 return false;
tsepez55be02e2016-09-12 11:21:42 -0700969 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700970 vp << m_pFormFillEnv->JS_docGetFilePath();
tsepez4cf55152016-11-02 14:37:54 -0700971 return true;
tonikitoo33c4cdb2016-08-08 10:52:51 -0700972}
973
Tom Sepezb1670b52017-02-16 17:01:00 -0800974bool Document::baseURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700975 CJS_PropValue& vp,
976 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 if (vp.IsGetting()) {
978 vp << m_cwBaseURL;
979 } else {
980 vp >> m_cwBaseURL;
981 }
tsepez4cf55152016-11-02 14:37:54 -0700982 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983}
984
Tom Sepezb1670b52017-02-16 17:01:00 -0800985bool Document::calculate(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700986 CJS_PropValue& vp,
987 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -0700988 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -0700989 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -0700990 return false;
tsepez55be02e2016-09-12 11:21:42 -0700991 }
dsinclair7cbe68e2016-10-12 11:56:23 -0700992 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 if (vp.IsGetting()) {
tsepez55be02e2016-09-12 11:21:42 -0700994 vp << !!pInterForm->IsCalculateEnabled();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800995 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800997 bool bCalculate;
998 vp >> bCalculate;
999 pInterForm->EnableCalculate(bCalculate);
tsepez4cf55152016-11-02 14:37:54 -07001000 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001}
1002
Tom Sepezb1670b52017-02-16 17:01:00 -08001003bool Document::documentFileName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001004 CJS_PropValue& vp,
1005 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001007 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001008 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 }
dsinclair82e17672016-10-11 12:38:01 -07001010 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001011 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001012 return false;
tsepez55be02e2016-09-12 11:21:42 -07001013 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001014 CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 int32_t i = wsFilePath.GetLength() - 1;
1016 for (; i >= 0; i--) {
1017 if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
1018 break;
1019 }
1020 if (i >= 0 && i < wsFilePath.GetLength() - 1) {
1021 vp << (wsFilePath.GetBuffer(wsFilePath.GetLength()) + i + 1);
1022 } else {
1023 vp << L"";
1024 }
tsepez4cf55152016-11-02 14:37:54 -07001025 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026}
1027
Tom Sepezb1670b52017-02-16 17:01:00 -08001028bool Document::path(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001029 CJS_PropValue& vp,
1030 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001032 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001033 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 }
dsinclair82e17672016-10-11 12:38:01 -07001035 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001036 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001037 return false;
tsepez55be02e2016-09-12 11:21:42 -07001038 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001039 vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
tsepez4cf55152016-11-02 14:37:54 -07001040 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041}
1042
Tom Sepezb1670b52017-02-16 17:01:00 -08001043bool Document::pageWindowRect(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001044 CJS_PropValue& vp,
1045 CFX_WideString& sError) {
1046 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047}
1048
Tom Sepezb1670b52017-02-16 17:01:00 -08001049bool Document::layout(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001050 CJS_PropValue& vp,
1051 CFX_WideString& sError) {
1052 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053}
1054
Tom Sepezb1670b52017-02-16 17:01:00 -08001055bool Document::addLink(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001056 const std::vector<CJS_Value>& params,
1057 CJS_Value& vRet,
1058 CFX_WideString& sError) {
1059 return true;
1060}
1061
Tom Sepezb1670b52017-02-16 17:01:00 -08001062bool Document::closeDoc(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001063 const std::vector<CJS_Value>& params,
1064 CJS_Value& vRet,
1065 CFX_WideString& sError) {
1066 return true;
1067}
1068
Tom Sepezb1670b52017-02-16 17:01:00 -08001069bool Document::getPageBox(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001070 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 CJS_Value& vRet,
1072 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001073 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074}
1075
Tom Sepezb1670b52017-02-16 17:01:00 -08001076bool Document::getAnnot(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001077 const std::vector<CJS_Value>& params,
1078 CJS_Value& vRet,
1079 CFX_WideString& sError) {
tonikitoo618cb1f2016-08-18 20:10:17 -07001080 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001081 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001082 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001083 }
dsinclair82e17672016-10-11 12:38:01 -07001084 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001085 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001086 return false;
tsepez55be02e2016-09-12 11:21:42 -07001087 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001088 int nPageNo = params[0].ToInt(pRuntime);
1089 CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
dsinclair7cbe68e2016-10-12 11:56:23 -07001090 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
tonikitoo618cb1f2016-08-18 20:10:17 -07001091 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001092 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001093
tsepezd805eec2017-01-11 14:03:54 -08001094 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
tonikitoo618cb1f2016-08-18 20:10:17 -07001095 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
tsepezd805eec2017-01-11 14:03:54 -08001096 for (const auto& pSDKAnnotCur : annotIteration) {
1097 CPDFSDK_BAAnnot* pBAAnnot =
1098 static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
tonikitoo618cb1f2016-08-18 20:10:17 -07001099 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1100 pSDKBAAnnot = pBAAnnot;
1101 break;
1102 }
1103 }
tonikitoo618cb1f2016-08-18 20:10:17 -07001104 if (!pSDKBAAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001105 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001106
1107 v8::Local<v8::Object> pObj =
1108 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1109 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001110 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001111
1112 CJS_Annot* pJS_Annot =
1113 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1114 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001115 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001116
1117 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1118 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001119 return false;
tonikitoo618cb1f2016-08-18 20:10:17 -07001120
1121 pAnnot->SetSDKAnnot(pSDKBAAnnot);
tonikitoo618cb1f2016-08-18 20:10:17 -07001122 vRet = CJS_Value(pRuntime, pJS_Annot);
tsepez4cf55152016-11-02 14:37:54 -07001123 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124}
1125
Tom Sepezb1670b52017-02-16 17:01:00 -08001126bool Document::getAnnots(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001127 const std::vector<CJS_Value>& params,
1128 CJS_Value& vRet,
1129 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001130 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001131 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001132 return false;
tsepez55be02e2016-09-12 11:21:42 -07001133 }
tonikitoo0ee35902016-08-26 14:41:29 -07001134 // TODO(tonikitoo): Add support supported parameters as per
1135 // the PDF spec.
1136
dsinclair7cbe68e2016-10-12 11:56:23 -07001137 int nPageNo = m_pFormFillEnv->GetPageCount();
tonikitoo0ee35902016-08-26 14:41:29 -07001138 CJS_Array annots;
1139
1140 for (int i = 0; i < nPageNo; ++i) {
dsinclair7cbe68e2016-10-12 11:56:23 -07001141 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
tonikitoo0ee35902016-08-26 14:41:29 -07001142 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -07001143 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001144
tsepezd805eec2017-01-11 14:03:54 -08001145 CPDFSDK_AnnotIteration annotIteration(pPageView, false);
1146 for (const auto& pSDKAnnotCur : annotIteration) {
1147 if (!pSDKAnnotCur) {
1148 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001149 return false;
tsepezd805eec2017-01-11 14:03:54 -08001150 }
tonikitoo0ee35902016-08-26 14:41:29 -07001151 v8::Local<v8::Object> pObj =
1152 pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
1153 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001154 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001155
1156 CJS_Annot* pJS_Annot =
1157 static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
1158 if (!pJS_Annot)
tsepez4cf55152016-11-02 14:37:54 -07001159 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001160
1161 Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
1162 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -07001163 return false;
tonikitoo0ee35902016-08-26 14:41:29 -07001164
tsepezd805eec2017-01-11 14:03:54 -08001165 pAnnot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
tonikitoo0ee35902016-08-26 14:41:29 -07001166 annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
1167 }
1168 }
tonikitoo0ee35902016-08-26 14:41:29 -07001169 vRet = CJS_Value(pRuntime, annots);
tsepez4cf55152016-11-02 14:37:54 -07001170 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171}
1172
Tom Sepezb1670b52017-02-16 17:01:00 -08001173bool Document::getAnnot3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001174 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 CJS_Value& vRet,
1176 CFX_WideString& sError) {
Tom Sepezb1670b52017-02-16 17:01:00 -08001177 vRet.SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001178 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179}
1180
Tom Sepezb1670b52017-02-16 17:01:00 -08001181bool Document::getAnnots3D(CJS_Runtime* pRuntime,
Lei Zhang945fdb72015-11-11 10:18:16 -08001182 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 CJS_Value& vRet,
1184 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -07001185 return true;
1186}
1187
Tom Sepezb1670b52017-02-16 17:01:00 -08001188bool Document::getOCGs(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001189 const std::vector<CJS_Value>& params,
1190 CJS_Value& vRet,
1191 CFX_WideString& sError) {
1192 return true;
1193}
1194
Tom Sepezb1670b52017-02-16 17:01:00 -08001195bool Document::getLinks(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001196 const std::vector<CJS_Value>& params,
1197 CJS_Value& vRet,
1198 CFX_WideString& sError) {
1199 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200}
1201
1202bool Document::IsEnclosedInRect(CFX_FloatRect rect, CFX_FloatRect LinkRect) {
1203 return (rect.left <= LinkRect.left && rect.top <= LinkRect.top &&
1204 rect.right >= LinkRect.right && rect.bottom >= LinkRect.bottom);
1205}
1206
Tom Sepezb1670b52017-02-16 17:01:00 -08001207bool Document::addIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001208 const std::vector<CJS_Value>& params,
1209 CJS_Value& vRet,
1210 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 if (params.size() != 2) {
tsepezcd5dc852016-09-08 11:23:24 -07001212 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001213 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214 }
tsepezf3dc8c62016-08-10 06:29:29 -07001215
tsepezb4694242016-08-15 16:44:55 -07001216 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
Tom Sepez39bfe122015-09-17 15:25:23 -07001217 if (params[1].GetType() != CJS_Value::VT_object) {
tsepezcd5dc852016-09-08 11:23:24 -07001218 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001219 return false;
Tom Sepezaecd9d02015-08-31 15:04:12 -07001220 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221
tsepezb4694242016-08-15 16:44:55 -07001222 v8::Local<v8::Object> pJSIcon = params[1].ToV8Object(pRuntime);
1223 if (pRuntime->GetObjDefnID(pJSIcon) != CJS_Icon::g_nObjDefnID) {
tsepezcd5dc852016-09-08 11:23:24 -07001224 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001225 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001226 }
1227
tsepezb4694242016-08-15 16:44:55 -07001228 CJS_EmbedObj* pEmbedObj = params[1].ToCJSObject(pRuntime)->GetEmbedObject();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 if (!pEmbedObj) {
tsepezcd5dc852016-09-08 11:23:24 -07001230 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001231 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 }
1233
thestigdadea5f2016-10-19 13:29:48 -07001234 m_Icons.push_back(pdfium::MakeUnique<IconElement>(
1235 swIconName, static_cast<Icon*>(pEmbedObj)));
tsepez4cf55152016-11-02 14:37:54 -07001236 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237}
1238
Tom Sepezb1670b52017-02-16 17:01:00 -08001239bool Document::icons(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001240 CJS_PropValue& vp,
1241 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242 if (vp.IsSetting()) {
tsepezcd5dc852016-09-08 11:23:24 -07001243 sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
tsepez4cf55152016-11-02 14:37:54 -07001244 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 }
thestigdadea5f2016-10-19 13:29:48 -07001246 if (m_Icons.empty()) {
tsepezf3dc8c62016-08-10 06:29:29 -07001247 vp.GetJSValue()->SetNull(pRuntime);
tsepez4cf55152016-11-02 14:37:54 -07001248 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001249 }
1250
tsepeze5aff742016-08-08 09:49:42 -07001251 CJS_Array Icons;
Tom Sepez6ba32d92016-01-05 16:26:32 -08001252 int i = 0;
thestigdadea5f2016-10-19 13:29:48 -07001253 for (const auto& pIconElement : m_Icons) {
tsepezb4694242016-08-15 16:44:55 -07001254 v8::Local<v8::Object> pObj =
1255 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001257 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258
tsepezb4694242016-08-15 16:44:55 -07001259 CJS_Icon* pJS_Icon =
1260 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001262 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263
thestigdadea5f2016-10-19 13:29:48 -07001264 Icon* pIcon = static_cast<Icon*>(pJS_Icon->GetEmbedObject());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001266 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267
dsinclair09bad1c2016-10-18 19:26:30 -07001268 pIcon->SetIconName(pIconElement->IconName);
tsepezb4694242016-08-15 16:44:55 -07001269 Icons.SetElement(pRuntime, i++, CJS_Value(pRuntime, pJS_Icon));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 }
1271
1272 vp << Icons;
tsepez4cf55152016-11-02 14:37:54 -07001273 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274}
1275
Tom Sepezb1670b52017-02-16 17:01:00 -08001276bool Document::getIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001277 const std::vector<CJS_Value>& params,
1278 CJS_Value& vRet,
1279 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001281 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001282 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001283 }
1284
thestigdadea5f2016-10-19 13:29:48 -07001285 if (m_Icons.empty())
tsepez4cf55152016-11-02 14:37:54 -07001286 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287
tsepezb4694242016-08-15 16:44:55 -07001288 CFX_WideString swIconName = params[0].ToCFXWideString(pRuntime);
thestigdadea5f2016-10-19 13:29:48 -07001289 for (const auto& pIconElement : m_Icons) {
1290 if (pIconElement->IconName != swIconName)
1291 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292
thestigdadea5f2016-10-19 13:29:48 -07001293 v8::Local<v8::Object> pObj =
1294 pRuntime->NewFxDynamicObj(CJS_Icon::g_nObjDefnID);
1295 if (pObj.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -07001296 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297
thestigdadea5f2016-10-19 13:29:48 -07001298 CJS_Icon* pJS_Icon =
1299 static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
1300 if (!pJS_Icon)
tsepez4cf55152016-11-02 14:37:54 -07001301 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302
thestigdadea5f2016-10-19 13:29:48 -07001303 Icon* pIcon = (Icon*)pJS_Icon->GetEmbedObject();
1304 if (!pIcon)
tsepez4cf55152016-11-02 14:37:54 -07001305 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306
thestigdadea5f2016-10-19 13:29:48 -07001307 pIcon->SetIconName(swIconName);
thestigdadea5f2016-10-19 13:29:48 -07001308 vRet = CJS_Value(pRuntime, pJS_Icon);
tsepez4cf55152016-11-02 14:37:54 -07001309 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310 }
1311
tsepez4cf55152016-11-02 14:37:54 -07001312 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313}
1314
Tom Sepezb1670b52017-02-16 17:01:00 -08001315bool Document::removeIcon(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001316 const std::vector<CJS_Value>& params,
1317 CJS_Value& vRet,
1318 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001319 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001320 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321}
1322
Tom Sepezb1670b52017-02-16 17:01:00 -08001323bool Document::createDataObject(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001324 const std::vector<CJS_Value>& params,
1325 CJS_Value& vRet,
1326 CFX_WideString& sError) {
Tom Sepezc6ab1722015-02-05 15:27:25 -08001327 // Unsafe, not implemented.
tsepez4cf55152016-11-02 14:37:54 -07001328 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001329}
1330
Tom Sepezb1670b52017-02-16 17:01:00 -08001331bool Document::media(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001332 CJS_PropValue& vp,
1333 CFX_WideString& sError) {
1334 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001335}
1336
Tom Sepezb1670b52017-02-16 17:01:00 -08001337bool Document::calculateNow(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001338 const std::vector<CJS_Value>& params,
1339 CJS_Value& vRet,
1340 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001341 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001342 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001343 return false;
tsepez55be02e2016-09-12 11:21:42 -07001344 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001345 if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
1346 m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
1347 m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
tsepez55be02e2016-09-12 11:21:42 -07001348 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001349 return false;
tsepez55be02e2016-09-12 11:21:42 -07001350 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001351 m_pFormFillEnv->GetInterForm()->OnCalculate();
tsepez4cf55152016-11-02 14:37:54 -07001352 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001353}
1354
Tom Sepezb1670b52017-02-16 17:01:00 -08001355bool Document::Collab(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001356 CJS_PropValue& vp,
1357 CFX_WideString& sError) {
1358 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001359}
1360
Tom Sepezb1670b52017-02-16 17:01:00 -08001361bool Document::getPageNthWord(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001362 const std::vector<CJS_Value>& params,
1363 CJS_Value& vRet,
1364 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001365 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001366 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001367 return false;
tsepez55be02e2016-09-12 11:21:42 -07001368 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001369 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001370 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001371 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001372 }
tsepez55be02e2016-09-12 11:21:42 -07001373
1374 // TODO(tsepez): check maximum allowable params.
1375
tsepezb4694242016-08-15 16:44:55 -07001376 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
1377 int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
1378 bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001379
dsinclair7cbe68e2016-10-12 11:56:23 -07001380 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001382 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001383
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001384 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001385 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001386 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001388
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001389 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1390 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001391 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001392
thestig5cc24652016-04-26 11:46:02 -07001393 CPDF_Page page(pDocument, pPageDict, true);
1394 page.ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001395
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 int nWords = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001397 CFX_WideString swRet;
Tom Sepez2398d892016-02-17 16:46:26 -08001398 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001399 if (pPageObj->IsText()) {
1400 CPDF_TextObject* pTextObj = pPageObj->AsText();
Tom Sepez2398d892016-02-17 16:46:26 -08001401 int nObjWords = CountWords(pTextObj);
1402 if (nWords + nObjWords >= nWordNo) {
1403 swRet = GetObjWordStr(pTextObj, nWordNo - nWords);
1404 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405 }
Tom Sepez2398d892016-02-17 16:46:26 -08001406 nWords += nObjWords;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001407 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001408 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001409
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 if (bStrip) {
1411 swRet.TrimLeft();
1412 swRet.TrimRight();
1413 }
1414
tsepezf3dc8c62016-08-10 06:29:29 -07001415 vRet = CJS_Value(pRuntime, swRet.c_str());
tsepez4cf55152016-11-02 14:37:54 -07001416 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001417}
1418
Tom Sepezb1670b52017-02-16 17:01:00 -08001419bool Document::getPageNthWordQuads(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001420 const std::vector<CJS_Value>& params,
1421 CJS_Value& vRet,
1422 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001423 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001424 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001425 return false;
tsepez55be02e2016-09-12 11:21:42 -07001426 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001427 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepez55be02e2016-09-12 11:21:42 -07001428 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001429 return false;
tsepez55be02e2016-09-12 11:21:42 -07001430 }
tsepez4cf55152016-11-02 14:37:54 -07001431 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001432}
1433
Tom Sepezb1670b52017-02-16 17:01:00 -08001434bool Document::getPageNumWords(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001435 const std::vector<CJS_Value>& params,
1436 CJS_Value& vRet,
1437 CFX_WideString& sError) {
dsinclair82e17672016-10-11 12:38:01 -07001438 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001439 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001440 return false;
tsepez55be02e2016-09-12 11:21:42 -07001441 }
dsinclair7cbe68e2016-10-12 11:56:23 -07001442 if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
tsepezcd5dc852016-09-08 11:23:24 -07001443 sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
tsepez4cf55152016-11-02 14:37:54 -07001444 return false;
tsepezcd5dc852016-09-08 11:23:24 -07001445 }
tsepezb4694242016-08-15 16:44:55 -07001446 int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
dsinclair7cbe68e2016-10-12 11:56:23 -07001447 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001448 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
tsepezcd5dc852016-09-08 11:23:24 -07001449 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -07001450 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 }
1452
1453 CPDF_Dictionary* pPageDict = pDocument->GetPage(nPageNo);
1454 if (!pPageDict)
tsepez4cf55152016-11-02 14:37:54 -07001455 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001456
thestig5cc24652016-04-26 11:46:02 -07001457 CPDF_Page page(pDocument, pPageDict, true);
1458 page.ParseContent();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459
1460 int nWords = 0;
Tom Sepez2398d892016-02-17 16:46:26 -08001461 for (auto& pPageObj : *page.GetPageObjectList()) {
Wei Li7cf13c92016-02-19 11:53:03 -08001462 if (pPageObj->IsText())
1463 nWords += CountWords(pPageObj->AsText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001464 }
1465
tsepezf3dc8c62016-08-10 06:29:29 -07001466 vRet = CJS_Value(pRuntime, nWords);
tsepez4cf55152016-11-02 14:37:54 -07001467 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001468}
1469
Tom Sepezb1670b52017-02-16 17:01:00 -08001470bool Document::getPrintParams(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001471 const std::vector<CJS_Value>& params,
1472 CJS_Value& vRet,
1473 CFX_WideString& sError) {
tsepezb4694242016-08-15 16:44:55 -07001474 v8::Local<v8::Object> pRetObj =
1475 pRuntime->NewFxDynamicObj(CJS_PrintParamsObj::g_nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001476
1477 // Not implemented yet.
1478
tsepezf3dc8c62016-08-10 06:29:29 -07001479 vRet = CJS_Value(pRuntime, pRetObj);
tsepez4cf55152016-11-02 14:37:54 -07001480 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481}
1482
1483#define ISLATINWORD(u) (u != 0x20 && u <= 0x28FF)
1484
1485int Document::CountWords(CPDF_TextObject* pTextObj) {
1486 if (!pTextObj)
1487 return 0;
1488
1489 int nWords = 0;
1490
1491 CPDF_Font* pFont = pTextObj->GetFont();
1492 if (!pFont)
1493 return 0;
1494
tsepez4cf55152016-11-02 14:37:54 -07001495 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496
1497 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001498 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 FX_FLOAT kerning;
1500
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001501 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1503
Tom Sepez62a70f92016-03-21 15:00:20 -07001504 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 if (swUnicode.GetLength() > 0)
1506 unicode = swUnicode[0];
1507
1508 if (ISLATINWORD(unicode) && bIsLatin)
1509 continue;
1510
1511 bIsLatin = ISLATINWORD(unicode);
1512 if (unicode != 0x20)
1513 nWords++;
1514 }
1515
1516 return nWords;
1517}
1518
1519CFX_WideString Document::GetObjWordStr(CPDF_TextObject* pTextObj,
1520 int nWordIndex) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001521 CFX_WideString swRet;
1522
1523 CPDF_Font* pFont = pTextObj->GetFont();
1524 if (!pFont)
1525 return L"";
1526
1527 int nWords = 0;
tsepez4cf55152016-11-02 14:37:54 -07001528 bool bIsLatin = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001529
1530 for (int i = 0, sz = pTextObj->CountChars(); i < sz; i++) {
Wei Li89409932016-03-28 10:33:33 -07001531 uint32_t charcode = CPDF_Font::kInvalidCharCode;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001532 FX_FLOAT kerning;
1533
Nicolas Pena58e4c5a2017-02-13 16:08:51 -05001534 pTextObj->GetCharInfo(i, &charcode, &kerning);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001535 CFX_WideString swUnicode = pFont->UnicodeFromCharCode(charcode);
1536
Tom Sepez62a70f92016-03-21 15:00:20 -07001537 uint16_t unicode = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 if (swUnicode.GetLength() > 0)
1539 unicode = swUnicode[0];
1540
1541 if (ISLATINWORD(unicode) && bIsLatin) {
1542 } else {
1543 bIsLatin = ISLATINWORD(unicode);
1544 if (unicode != 0x20)
1545 nWords++;
1546 }
1547
1548 if (nWords - 1 == nWordIndex)
1549 swRet += unicode;
1550 }
1551
1552 return swRet;
1553}
1554
Tom Sepezb1670b52017-02-16 17:01:00 -08001555bool Document::zoom(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001556 CJS_PropValue& vp,
1557 CFX_WideString& sError) {
1558 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001559}
1560
1561/**
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001562(none, NoVary)
1563(fitP, FitPage)
1564(fitW, FitWidth)
1565(fitH, FitHeight)
1566(fitV, FitVisibleWidth)
1567(pref, Preferred)
1568(refW, ReflowWidth)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001569*/
1570
Tom Sepezb1670b52017-02-16 17:01:00 -08001571bool Document::zoomType(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001572 CJS_PropValue& vp,
1573 CFX_WideString& sError) {
1574 return true;
1575}
1576
Tom Sepezb1670b52017-02-16 17:01:00 -08001577bool Document::deletePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001578 const std::vector<CJS_Value>& params,
1579 CJS_Value& vRet,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001580 CFX_WideString& sError) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -07001581 // Unsafe, no supported.
tsepez4cf55152016-11-02 14:37:54 -07001582 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001583}
JUN FANG33f6f0d2015-04-06 12:39:51 -07001584
Tom Sepezb1670b52017-02-16 17:01:00 -08001585bool Document::extractPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001586 const std::vector<CJS_Value>& params,
1587 CJS_Value& vRet,
1588 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001590 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001591}
1592
Tom Sepezb1670b52017-02-16 17:01:00 -08001593bool Document::insertPages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001594 const std::vector<CJS_Value>& params,
1595 CJS_Value& vRet,
1596 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001598 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001599}
1600
Tom Sepezb1670b52017-02-16 17:01:00 -08001601bool Document::replacePages(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001602 const std::vector<CJS_Value>& params,
1603 CJS_Value& vRet,
1604 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001605 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001606 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001607}
1608
Tom Sepezb1670b52017-02-16 17:01:00 -08001609bool Document::getURL(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001610 const std::vector<CJS_Value>& params,
1611 CJS_Value& vRet,
1612 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613 // Unsafe, not supported.
tsepez4cf55152016-11-02 14:37:54 -07001614 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001615}
1616
Tom Sepezb1670b52017-02-16 17:01:00 -08001617bool Document::gotoNamedDest(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -07001618 const std::vector<CJS_Value>& params,
1619 CJS_Value& vRet,
1620 CFX_WideString& sError) {
tonikitoo1c836752016-08-08 16:14:05 -07001621 if (params.size() != 1) {
tsepezcd5dc852016-09-08 11:23:24 -07001622 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -07001623 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001624 }
dsinclair82e17672016-10-11 12:38:01 -07001625 if (!m_pFormFillEnv) {
tsepez55be02e2016-09-12 11:21:42 -07001626 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
tsepez4cf55152016-11-02 14:37:54 -07001627 return false;
tsepez55be02e2016-09-12 11:21:42 -07001628 }
tsepezb4694242016-08-15 16:44:55 -07001629 CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
tsepezf3dc8c62016-08-10 06:29:29 -07001630 CFX_ByteString utf8Name = wideName.UTF8Encode();
dsinclair7cbe68e2016-10-12 11:56:23 -07001631 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
tonikitoo1c836752016-08-08 16:14:05 -07001632 if (!pDocument)
tsepez4cf55152016-11-02 14:37:54 -07001633 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001634
tonikitoo1c836752016-08-08 16:14:05 -07001635 CPDF_NameTree nameTree(pDocument, "Dests");
1636 CPDF_Array* destArray = nameTree.LookupNamedDest(pDocument, utf8Name);
1637 if (!destArray)
tsepez4cf55152016-11-02 14:37:54 -07001638 return false;
tonikitoo1c836752016-08-08 16:14:05 -07001639
1640 CPDF_Dest dest(destArray);
1641 const CPDF_Array* arrayObject = ToArray(dest.GetObject());
1642
1643 std::unique_ptr<float[]> scrollPositionArray;
1644 int scrollPositionArraySize = 0;
1645
1646 if (arrayObject) {
1647 scrollPositionArray.reset(new float[arrayObject->GetCount()]);
1648 int j = 0;
1649 for (size_t i = 2; i < arrayObject->GetCount(); i++)
1650 scrollPositionArray[j++] = arrayObject->GetFloatAt(i);
1651 scrollPositionArraySize = j;
1652 }
1653
tsepezb4694242016-08-15 16:44:55 -07001654 pRuntime->BeginBlock();
dsinclair82e17672016-10-11 12:38:01 -07001655 m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
1656 scrollPositionArray.get(),
1657 scrollPositionArraySize);
tsepezb4694242016-08-15 16:44:55 -07001658 pRuntime->EndBlock();
tonikitoo1c836752016-08-08 16:14:05 -07001659
tsepez4cf55152016-11-02 14:37:54 -07001660 return true;
tonikitoo1c836752016-08-08 16:14:05 -07001661}
1662
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663void Document::AddDelayData(CJS_DelayData* pData) {
Tom Sepez56c10192016-03-15 12:34:17 -07001664 m_DelayData.push_back(std::unique_ptr<CJS_DelayData>(pData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001665}
1666
1667void Document::DoFieldDelay(const CFX_WideString& sFieldName,
1668 int nControlIndex) {
Tom Sepez56c10192016-03-15 12:34:17 -07001669 std::vector<std::unique_ptr<CJS_DelayData>> DelayDataForFieldAndControlIndex;
1670 auto iter = m_DelayData.begin();
1671 while (iter != m_DelayData.end()) {
1672 auto old = iter++;
1673 if ((*old)->sFieldName == sFieldName &&
1674 (*old)->nControlIndex == nControlIndex) {
1675 DelayDataForFieldAndControlIndex.push_back(std::move(*old));
1676 m_DelayData.erase(old);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 }
1678 }
1679
dsinclair82e17672016-10-11 12:38:01 -07001680 for (const auto& pData : DelayDataForFieldAndControlIndex)
1681 Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682}
1683
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001684CJS_Document* Document::GetCJSDoc() const {
1685 return static_cast<CJS_Document*>(m_pJSObject);
JUN FANG33f6f0d2015-04-06 12:39:51 -07001686}