• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/cjs_field.h"
8 
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12 
13 #include "constants/annotation_flags.h"
14 #include "constants/form_flags.h"
15 #include "core/fpdfdoc/cpdf_formcontrol.h"
16 #include "core/fpdfdoc/cpdf_formfield.h"
17 #include "core/fpdfdoc/cpdf_interactiveform.h"
18 #include "fpdfsdk/cpdfsdk_helpers.h"
19 #include "fpdfsdk/cpdfsdk_interactiveform.h"
20 #include "fpdfsdk/cpdfsdk_pageview.h"
21 #include "fpdfsdk/cpdfsdk_widget.h"
22 #include "fxjs/cjs_color.h"
23 #include "fxjs/cjs_delaydata.h"
24 #include "fxjs/cjs_document.h"
25 #include "fxjs/cjs_icon.h"
26 #include "fxjs/js_resources.h"
27 #include "third_party/base/ptr_util.h"
28 
29 namespace {
30 
IsCheckBoxOrRadioButton(const CPDF_FormField * pFormField)31 bool IsCheckBoxOrRadioButton(const CPDF_FormField* pFormField) {
32   return pFormField->GetFieldType() == FormFieldType::kCheckBox ||
33          pFormField->GetFieldType() == FormFieldType::kRadioButton;
34 }
35 
IsComboBoxOrListBox(const CPDF_FormField * pFormField)36 bool IsComboBoxOrListBox(const CPDF_FormField* pFormField) {
37   return pFormField->GetFieldType() == FormFieldType::kComboBox ||
38          pFormField->GetFieldType() == FormFieldType::kListBox;
39 }
40 
IsComboBoxOrTextField(const CPDF_FormField * pFormField)41 bool IsComboBoxOrTextField(const CPDF_FormField* pFormField) {
42   return pFormField->GetFieldType() == FormFieldType::kComboBox ||
43          pFormField->GetFieldType() == FormFieldType::kTextField;
44 }
45 
UpdateFormField(CPDFSDK_FormFillEnvironment * pFormFillEnv,CPDF_FormField * pFormField,bool bChangeMark,bool bResetAP,bool bRefresh)46 void UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv,
47                      CPDF_FormField* pFormField,
48                      bool bChangeMark,
49                      bool bResetAP,
50                      bool bRefresh) {
51   CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
52 
53   if (bResetAP) {
54     std::vector<ObservedPtr<CPDFSDK_Annot>> widgets;
55     pForm->GetWidgets(pFormField, &widgets);
56 
57     if (IsComboBoxOrTextField(pFormField)) {
58       for (auto& pObserved : widgets) {
59         if (pObserved) {
60           Optional<WideString> sValue =
61               ToCPDFSDKWidget(pObserved.Get())->OnFormat();
62           if (pObserved) {  // Not redundant, may be clobbered by OnFormat.
63             ToCPDFSDKWidget(pObserved.Get())->ResetAppearance(sValue, false);
64           }
65         }
66       }
67     } else {
68       for (auto& pObserved : widgets) {
69         if (pObserved)
70           ToCPDFSDKWidget(pObserved.Get())->ResetAppearance({}, false);
71       }
72     }
73   }
74 
75   if (bRefresh) {
76     // Refresh the widget list. The calls in |bResetAP| may have caused widgets
77     // to be removed from the list. We need to call |GetWidgets| again to be
78     // sure none of the widgets have been deleted.
79     std::vector<ObservedPtr<CPDFSDK_Annot>> widgets;
80     pForm->GetWidgets(pFormField, &widgets);
81 
82     // TODO(dsinclair): Determine if all widgets share the same
83     // CPDFSDK_InteractiveForm. If that's the case, we can move the code to
84     // |GetFormFillEnv| out of the loop.
85     for (auto& pObserved : widgets) {
86       if (pObserved) {
87         CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pObserved.Get());
88         pWidget->GetInteractiveForm()->GetFormFillEnv()->UpdateAllViews(
89             nullptr, pWidget);
90       }
91     }
92   }
93 
94   if (bChangeMark)
95     pFormFillEnv->SetChangeMark();
96 }
97 
UpdateFormControl(CPDFSDK_FormFillEnvironment * pFormFillEnv,CPDF_FormControl * pFormControl,bool bChangeMark,bool bResetAP,bool bRefresh)98 void UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv,
99                        CPDF_FormControl* pFormControl,
100                        bool bChangeMark,
101                        bool bResetAP,
102                        bool bRefresh) {
103   ASSERT(pFormControl);
104 
105   CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
106   CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl);
107 
108   if (pWidget) {
109     ObservedPtr<CPDFSDK_Widget> observed_widget(pWidget);
110     if (bResetAP) {
111       FormFieldType fieldType = pWidget->GetFieldType();
112       if (fieldType == FormFieldType::kComboBox ||
113           fieldType == FormFieldType::kTextField) {
114         Optional<WideString> sValue = pWidget->OnFormat();
115         if (!observed_widget)
116           return;
117         pWidget->ResetAppearance(sValue, false);
118       } else {
119         pWidget->ResetAppearance({}, false);
120       }
121       if (!observed_widget)
122         return;
123     }
124 
125     if (bRefresh) {
126       CPDFSDK_InteractiveForm* pWidgetForm = pWidget->GetInteractiveForm();
127       pWidgetForm->GetFormFillEnv()->UpdateAllViews(nullptr, pWidget);
128     }
129   }
130 
131   if (bChangeMark)
132     pFormFillEnv->SetChangeMark();
133 }
134 
135 // note: iControlNo = -1, means not a widget.
ParseFieldName(const WideString & strFieldNameParsed,WideString & strFieldName,int & iControlNo)136 void ParseFieldName(const WideString& strFieldNameParsed,
137                     WideString& strFieldName,
138                     int& iControlNo) {
139   auto reverse_it = strFieldNameParsed.rbegin();
140   while (reverse_it != strFieldNameParsed.rend()) {
141     if (*reverse_it == L'.')
142       break;
143     ++reverse_it;
144   }
145   if (reverse_it == strFieldNameParsed.rend()) {
146     strFieldName = strFieldNameParsed;
147     iControlNo = -1;
148     return;
149   }
150   WideString suffixal =
151       strFieldNameParsed.Last(reverse_it - strFieldNameParsed.rbegin());
152   iControlNo = FXSYS_wtoi(suffixal.c_str());
153   if (iControlNo == 0) {
154     suffixal.TrimRight(L' ');
155     if (suffixal != L"0") {
156       strFieldName = strFieldNameParsed;
157       iControlNo = -1;
158       return;
159     }
160   }
161   strFieldName =
162       strFieldNameParsed.First(strFieldNameParsed.rend() - reverse_it - 1);
163 }
164 
GetFormFieldsForName(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & csFieldName)165 std::vector<CPDF_FormField*> GetFormFieldsForName(
166     CPDFSDK_FormFillEnvironment* pFormFillEnv,
167     const WideString& csFieldName) {
168   std::vector<CPDF_FormField*> fields;
169   CPDFSDK_InteractiveForm* pReaderForm = pFormFillEnv->GetInteractiveForm();
170   CPDF_InteractiveForm* pForm = pReaderForm->GetInteractiveForm();
171   for (int i = 0, sz = pForm->CountFields(csFieldName); i < sz; ++i) {
172     if (CPDF_FormField* pFormField = pForm->GetField(i, csFieldName))
173       fields.push_back(pFormField);
174   }
175   return fields;
176 }
177 
SetWidgetDisplayStatus(CPDFSDK_Widget * pWidget,int value)178 bool SetWidgetDisplayStatus(CPDFSDK_Widget* pWidget, int value) {
179   if (!pWidget)
180     return false;
181 
182   uint32_t dwFlag = pWidget->GetFlags();
183   switch (value) {
184     case 0:
185       dwFlag &= ~pdfium::annotation_flags::kInvisible;
186       dwFlag &= ~pdfium::annotation_flags::kHidden;
187       dwFlag &= ~pdfium::annotation_flags::kNoView;
188       dwFlag |= pdfium::annotation_flags::kPrint;
189       break;
190     case 1:
191       dwFlag &= ~pdfium::annotation_flags::kInvisible;
192       dwFlag &= ~pdfium::annotation_flags::kNoView;
193       dwFlag |= (pdfium::annotation_flags::kHidden |
194                  pdfium::annotation_flags::kPrint);
195       break;
196     case 2:
197       dwFlag &= ~pdfium::annotation_flags::kInvisible;
198       dwFlag &= ~pdfium::annotation_flags::kPrint;
199       dwFlag &= ~pdfium::annotation_flags::kHidden;
200       dwFlag &= ~pdfium::annotation_flags::kNoView;
201       break;
202     case 3:
203       dwFlag |= pdfium::annotation_flags::kNoView;
204       dwFlag |= pdfium::annotation_flags::kPrint;
205       dwFlag &= ~pdfium::annotation_flags::kHidden;
206       break;
207   }
208 
209   if (dwFlag != pWidget->GetFlags()) {
210     pWidget->SetFlags(dwFlag);
211     return true;
212   }
213 
214   return false;
215 }
216 
SetBorderStyle(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,const ByteString & string)217 void SetBorderStyle(CPDFSDK_FormFillEnvironment* pFormFillEnv,
218                     const WideString& swFieldName,
219                     int nControlIndex,
220                     const ByteString& string) {
221   ASSERT(pFormFillEnv);
222 
223   BorderStyle nBorderStyle = BorderStyle::SOLID;
224   if (string == "solid")
225     nBorderStyle = BorderStyle::SOLID;
226   else if (string == "beveled")
227     nBorderStyle = BorderStyle::BEVELED;
228   else if (string == "dashed")
229     nBorderStyle = BorderStyle::DASH;
230   else if (string == "inset")
231     nBorderStyle = BorderStyle::INSET;
232   else if (string == "underline")
233     nBorderStyle = BorderStyle::UNDERLINE;
234   else
235     return;
236 
237   std::vector<CPDF_FormField*> FieldArray =
238       GetFormFieldsForName(pFormFillEnv, swFieldName);
239   auto* pForm = pFormFillEnv->GetInteractiveForm();
240   for (CPDF_FormField* pFormField : FieldArray) {
241     if (nControlIndex < 0) {
242       bool bSet = false;
243       for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) {
244         CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormField->GetControl(i));
245         if (pWidget) {
246           if (pWidget->GetBorderStyle() != nBorderStyle) {
247             pWidget->SetBorderStyle(nBorderStyle);
248             bSet = true;
249           }
250         }
251       }
252       if (bSet)
253         UpdateFormField(pFormFillEnv, pFormField, true, true, true);
254     } else {
255       if (nControlIndex >= pFormField->CountControls())
256         return;
257       if (CPDF_FormControl* pFormControl =
258               pFormField->GetControl(nControlIndex)) {
259         CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl);
260         if (pWidget) {
261           if (pWidget->GetBorderStyle() != nBorderStyle) {
262             pWidget->SetBorderStyle(nBorderStyle);
263             UpdateFormControl(pFormFillEnv, pFormControl, true, true, true);
264           }
265         }
266       }
267     }
268   }
269 }
270 
SetCurrentValueIndices(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,const std::vector<uint32_t> & array)271 void SetCurrentValueIndices(CPDFSDK_FormFillEnvironment* pFormFillEnv,
272                             const WideString& swFieldName,
273                             int nControlIndex,
274                             const std::vector<uint32_t>& array) {
275   ASSERT(pFormFillEnv);
276   std::vector<CPDF_FormField*> FieldArray =
277       GetFormFieldsForName(pFormFillEnv, swFieldName);
278 
279   for (CPDF_FormField* pFormField : FieldArray) {
280     if (!IsComboBoxOrListBox(pFormField))
281       continue;
282 
283     uint32_t dwFieldFlags = pFormField->GetFieldFlags();
284     pFormField->ClearSelection(NotificationOption::kNotify);
285     for (size_t i = 0; i < array.size(); ++i) {
286       if (i != 0 && !(dwFieldFlags & pdfium::form_flags::kChoiceMultiSelect))
287         break;
288       if (array[i] < static_cast<uint32_t>(pFormField->CountOptions()) &&
289           !pFormField->IsItemSelected(array[i])) {
290         pFormField->SetItemSelection(array[i], true,
291                                      NotificationOption::kDoNotNotify);
292       }
293     }
294     UpdateFormField(pFormFillEnv, pFormField, true, true, true);
295   }
296 }
297 
SetDisplay(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,int number)298 void SetDisplay(CPDFSDK_FormFillEnvironment* pFormFillEnv,
299                 const WideString& swFieldName,
300                 int nControlIndex,
301                 int number) {
302   CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
303   std::vector<CPDF_FormField*> FieldArray =
304       GetFormFieldsForName(pFormFillEnv, swFieldName);
305   for (CPDF_FormField* pFormField : FieldArray) {
306     if (nControlIndex < 0) {
307       bool bAnySet = false;
308       for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) {
309         CPDF_FormControl* pFormControl = pFormField->GetControl(i);
310         ASSERT(pFormControl);
311 
312         CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl);
313         if (SetWidgetDisplayStatus(pWidget, number))
314           bAnySet = true;
315       }
316 
317       if (bAnySet)
318         UpdateFormField(pFormFillEnv, pFormField, true, false, true);
319     } else {
320       if (nControlIndex >= pFormField->CountControls())
321         return;
322 
323       CPDF_FormControl* pFormControl = pFormField->GetControl(nControlIndex);
324       if (!pFormControl)
325         return;
326 
327       CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl);
328       if (SetWidgetDisplayStatus(pWidget, number))
329         UpdateFormControl(pFormFillEnv, pFormControl, true, false, true);
330     }
331   }
332 }
333 
SetHidden(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,bool b)334 void SetHidden(CPDFSDK_FormFillEnvironment* pFormFillEnv,
335                const WideString& swFieldName,
336                int nControlIndex,
337                bool b) {
338   int display = b ? 1 /*Hidden*/ : 0 /*Visible*/;
339   SetDisplay(pFormFillEnv, swFieldName, nControlIndex, display);
340 }
341 
SetLineWidth(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,int number)342 void SetLineWidth(CPDFSDK_FormFillEnvironment* pFormFillEnv,
343                   const WideString& swFieldName,
344                   int nControlIndex,
345                   int number) {
346   CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
347   std::vector<CPDF_FormField*> FieldArray =
348       GetFormFieldsForName(pFormFillEnv, swFieldName);
349   for (CPDF_FormField* pFormField : FieldArray) {
350     if (nControlIndex < 0) {
351       bool bSet = false;
352       for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) {
353         CPDF_FormControl* pFormControl = pFormField->GetControl(i);
354         ASSERT(pFormControl);
355 
356         if (CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl)) {
357           if (number != pWidget->GetBorderWidth()) {
358             pWidget->SetBorderWidth(number);
359             bSet = true;
360           }
361         }
362       }
363       if (bSet)
364         UpdateFormField(pFormFillEnv, pFormField, true, true, true);
365     } else {
366       if (nControlIndex >= pFormField->CountControls())
367         return;
368       if (CPDF_FormControl* pFormControl =
369               pFormField->GetControl(nControlIndex)) {
370         if (CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl)) {
371           if (number != pWidget->GetBorderWidth()) {
372             pWidget->SetBorderWidth(number);
373             UpdateFormControl(pFormFillEnv, pFormControl, true, true, true);
374           }
375         }
376       }
377     }
378   }
379 }
380 
SetRect(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,const CFX_FloatRect & rect)381 void SetRect(CPDFSDK_FormFillEnvironment* pFormFillEnv,
382              const WideString& swFieldName,
383              int nControlIndex,
384              const CFX_FloatRect& rect) {
385   CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
386   std::vector<CPDF_FormField*> FieldArray =
387       GetFormFieldsForName(pFormFillEnv, swFieldName);
388   for (CPDF_FormField* pFormField : FieldArray) {
389     if (nControlIndex < 0) {
390       bool bSet = false;
391       for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) {
392         CPDF_FormControl* pFormControl = pFormField->GetControl(i);
393         ASSERT(pFormControl);
394 
395         if (CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl)) {
396           CFX_FloatRect crRect = rect;
397 
398           CPDF_Page* pPDFPage = pWidget->GetPDFPage();
399           crRect.Intersect(pPDFPage->GetBBox());
400 
401           if (!crRect.IsEmpty()) {
402             CFX_FloatRect rcOld = pWidget->GetRect();
403             if (crRect.left != rcOld.left || crRect.right != rcOld.right ||
404                 crRect.top != rcOld.top || crRect.bottom != rcOld.bottom) {
405               pWidget->SetRect(crRect);
406               bSet = true;
407             }
408           }
409         }
410       }
411 
412       if (bSet)
413         UpdateFormField(pFormFillEnv, pFormField, true, true, true);
414 
415       continue;
416     }
417 
418     if (nControlIndex >= pFormField->CountControls())
419       return;
420     if (CPDF_FormControl* pFormControl =
421             pFormField->GetControl(nControlIndex)) {
422       if (CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl)) {
423         CFX_FloatRect crRect = rect;
424         CPDF_Page* pPDFPage = pWidget->GetPDFPage();
425         crRect.Intersect(pPDFPage->GetBBox());
426         if (!crRect.IsEmpty()) {
427           CFX_FloatRect rcOld = pWidget->GetRect();
428           if (crRect.left != rcOld.left || crRect.right != rcOld.right ||
429               crRect.top != rcOld.top || crRect.bottom != rcOld.bottom) {
430             pWidget->SetRect(crRect);
431             UpdateFormControl(pFormFillEnv, pFormControl, true, true, true);
432           }
433         }
434       }
435     }
436   }
437 }
438 
SetFieldValue(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & swFieldName,int nControlIndex,const std::vector<WideString> & strArray)439 void SetFieldValue(CPDFSDK_FormFillEnvironment* pFormFillEnv,
440                    const WideString& swFieldName,
441                    int nControlIndex,
442                    const std::vector<WideString>& strArray) {
443   ASSERT(pFormFillEnv);
444   if (strArray.empty())
445     return;
446 
447   std::vector<CPDF_FormField*> FieldArray =
448       GetFormFieldsForName(pFormFillEnv, swFieldName);
449 
450   for (CPDF_FormField* pFormField : FieldArray) {
451     if (pFormField->GetFullName().Compare(swFieldName) != 0)
452       continue;
453 
454     switch (pFormField->GetFieldType()) {
455       case FormFieldType::kTextField:
456       case FormFieldType::kComboBox:
457         if (pFormField->GetValue() != strArray[0]) {
458           pFormField->SetValue(strArray[0], NotificationOption::kNotify);
459           UpdateFormField(pFormFillEnv, pFormField, true, false, true);
460         }
461         break;
462       case FormFieldType::kCheckBox:
463       case FormFieldType::kRadioButton:
464         if (pFormField->GetValue() != strArray[0]) {
465           pFormField->SetValue(strArray[0], NotificationOption::kNotify);
466           UpdateFormField(pFormFillEnv, pFormField, true, false, true);
467         }
468         break;
469       case FormFieldType::kListBox: {
470         bool bModified = false;
471         for (const auto& str : strArray) {
472           if (!pFormField->IsItemSelected(pFormField->FindOption(str))) {
473             bModified = true;
474             break;
475           }
476         }
477         if (bModified) {
478           pFormField->ClearSelection(NotificationOption::kNotify);
479           for (const auto& str : strArray) {
480             int index = pFormField->FindOption(str);
481             if (!pFormField->IsItemSelected(index))
482               pFormField->SetItemSelection(index, true,
483                                            NotificationOption::kNotify);
484           }
485           UpdateFormField(pFormFillEnv, pFormField, true, false, true);
486         }
487         break;
488       }
489       default:
490         break;
491     }
492   }
493 }
494 
495 }  // namespace
496 
497 const JSPropertySpec CJS_Field::PropertySpecs[] = {
498     {"alignment", get_alignment_static, set_alignment_static},
499     {"borderStyle", get_border_style_static, set_border_style_static},
500     {"buttonAlignX", get_button_align_x_static, set_button_align_x_static},
501     {"buttonAlignY", get_button_align_y_static, set_button_align_y_static},
502     {"buttonFitBounds", get_button_fit_bounds_static,
503      set_button_fit_bounds_static},
504     {"buttonPosition", get_button_position_static, set_button_position_static},
505     {"buttonScaleHow", get_button_scale_how_static,
506      set_button_scale_how_static},
507     {"buttonScaleWhen", get_button_scale_when_static,
508      set_button_scale_when_static},
509     {"calcOrderIndex", get_calc_order_index_static,
510      set_calc_order_index_static},
511     {"charLimit", get_char_limit_static, set_char_limit_static},
512     {"comb", get_comb_static, set_comb_static},
513     {"commitOnSelChange", get_commit_on_sel_change_static,
514      set_commit_on_sel_change_static},
515     {"currentValueIndices", get_current_value_indices_static,
516      set_current_value_indices_static},
517     {"defaultStyle", get_default_style_static, set_default_style_static},
518     {"defaultValue", get_default_value_static, set_default_value_static},
519     {"doNotScroll", get_do_not_scroll_static, set_do_not_scroll_static},
520     {"doNotSpellCheck", get_do_not_spell_check_static,
521      set_do_not_spell_check_static},
522     {"delay", get_delay_static, set_delay_static},
523     {"display", get_display_static, set_display_static},
524     {"doc", get_doc_static, set_doc_static},
525     {"editable", get_editable_static, set_editable_static},
526     {"exportValues", get_export_values_static, set_export_values_static},
527     {"hidden", get_hidden_static, set_hidden_static},
528     {"fileSelect", get_file_select_static, set_file_select_static},
529     {"fillColor", get_fill_color_static, set_fill_color_static},
530     {"lineWidth", get_line_width_static, set_line_width_static},
531     {"highlight", get_highlight_static, set_highlight_static},
532     {"multiline", get_multiline_static, set_multiline_static},
533     {"multipleSelection", get_multiple_selection_static,
534      set_multiple_selection_static},
535     {"name", get_name_static, set_name_static},
536     {"numItems", get_num_items_static, set_num_items_static},
537     {"page", get_page_static, set_page_static},
538     {"password", get_password_static, set_password_static},
539     {"print", get_print_static, set_print_static},
540     {"radiosInUnison", get_radios_in_unison_static,
541      set_radios_in_unison_static},
542     {"readonly", get_readonly_static, set_readonly_static},
543     {"rect", get_rect_static, set_rect_static},
544     {"required", get_required_static, set_required_static},
545     {"richText", get_rich_text_static, set_rich_text_static},
546     {"richValue", get_rich_value_static, set_rich_value_static},
547     {"rotation", get_rotation_static, set_rotation_static},
548     {"source", get_source_static, set_source_static},
549     {"strokeColor", get_stroke_color_static, set_stroke_color_static},
550     {"style", get_style_static, set_style_static},
551     {"submitName", get_submit_name_static, set_submit_name_static},
552     {"textColor", get_text_color_static, set_text_color_static},
553     {"textFont", get_text_font_static, set_text_font_static},
554     {"textSize", get_text_size_static, set_text_size_static},
555     {"type", get_type_static, set_type_static},
556     {"userName", get_user_name_static, set_user_name_static},
557     {"value", get_value_static, set_value_static},
558     {"valueAsString", get_value_as_string_static, set_value_as_string_static}};
559 
560 const JSMethodSpec CJS_Field::MethodSpecs[] = {
561     {"browseForFileToSubmit", browseForFileToSubmit_static},
562     {"buttonGetCaption", buttonGetCaption_static},
563     {"buttonGetIcon", buttonGetIcon_static},
564     {"buttonImportIcon", buttonImportIcon_static},
565     {"buttonSetCaption", buttonSetCaption_static},
566     {"buttonSetIcon", buttonSetIcon_static},
567     {"checkThisBox", checkThisBox_static},
568     {"clearItems", clearItems_static},
569     {"defaultIsChecked", defaultIsChecked_static},
570     {"deleteItemAt", deleteItemAt_static},
571     {"getArray", getArray_static},
572     {"getItemAt", getItemAt_static},
573     {"getLock", getLock_static},
574     {"insertItemAt", insertItemAt_static},
575     {"isBoxChecked", isBoxChecked_static},
576     {"isDefaultChecked", isDefaultChecked_static},
577     {"setAction", setAction_static},
578     {"setFocus", setFocus_static},
579     {"setItems", setItems_static},
580     {"setLock", setLock_static},
581     {"signatureGetModifications", signatureGetModifications_static},
582     {"signatureGetSeedValue", signatureGetSeedValue_static},
583     {"signatureInfo", signatureInfo_static},
584     {"signatureSetSeedValue", signatureSetSeedValue_static},
585     {"signatureSign", signatureSign_static},
586     {"signatureValidate", signatureValidate_static}};
587 
588 int CJS_Field::ObjDefnID = -1;
589 const char CJS_Field::kName[] = "Field";
590 
591 // static
GetObjDefnID()592 int CJS_Field::GetObjDefnID() {
593   return ObjDefnID;
594 }
595 
596 // static
DefineJSObjects(CFXJS_Engine * pEngine)597 void CJS_Field::DefineJSObjects(CFXJS_Engine* pEngine) {
598   ObjDefnID = pEngine->DefineObj(CJS_Field::kName, FXJSOBJTYPE_DYNAMIC,
599                                  JSConstructor<CJS_Field>, JSDestructor);
600   DefineProps(pEngine, ObjDefnID, PropertySpecs);
601   DefineMethods(pEngine, ObjDefnID, MethodSpecs);
602 }
603 
CJS_Field(v8::Local<v8::Object> pObject,CJS_Runtime * pRuntime)604 CJS_Field::CJS_Field(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
605     : CJS_Object(pObject, pRuntime) {}
606 
607 CJS_Field::~CJS_Field() = default;
608 
AttachField(CJS_Document * pDocument,const WideString & csFieldName)609 bool CJS_Field::AttachField(CJS_Document* pDocument,
610                             const WideString& csFieldName) {
611   m_pJSDoc.Reset(pDocument);
612   m_pFormFillEnv.Reset(pDocument->GetFormFillEnv());
613   m_bCanSet = m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM) ||
614               m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
615               m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY);
616 
617   CPDFSDK_InteractiveForm* pRDForm = m_pFormFillEnv->GetInteractiveForm();
618   CPDF_InteractiveForm* pForm = pRDForm->GetInteractiveForm();
619   WideString swFieldNameTemp = csFieldName;
620   swFieldNameTemp.Replace(L"..", L".");
621 
622   if (pForm->CountFields(swFieldNameTemp) <= 0) {
623     WideString strFieldName;
624     int iControlNo = -1;
625     ParseFieldName(swFieldNameTemp, strFieldName, iControlNo);
626     if (iControlNo == -1)
627       return false;
628 
629     m_FieldName = strFieldName;
630     m_nFormControlIndex = iControlNo;
631     return true;
632   }
633 
634   m_FieldName = swFieldNameTemp;
635   m_nFormControlIndex = -1;
636 
637   return true;
638 }
639 
GetFormFields() const640 std::vector<CPDF_FormField*> CJS_Field::GetFormFields() const {
641   return GetFormFieldsForName(m_pFormFillEnv.Get(), m_FieldName);
642 }
643 
GetFirstFormField() const644 CPDF_FormField* CJS_Field::GetFirstFormField() const {
645   std::vector<CPDF_FormField*> fields = GetFormFields();
646   return fields.empty() ? nullptr : fields[0];
647 }
648 
GetSmartFieldControl(CPDF_FormField * pFormField)649 CPDF_FormControl* CJS_Field::GetSmartFieldControl(CPDF_FormField* pFormField) {
650   if (!pFormField->CountControls() ||
651       m_nFormControlIndex >= pFormField->CountControls())
652     return nullptr;
653   if (m_nFormControlIndex < 0)
654     return pFormField->GetControl(0);
655   return pFormField->GetControl(m_nFormControlIndex);
656 }
657 
get_alignment(CJS_Runtime * pRuntime)658 CJS_Result CJS_Field::get_alignment(CJS_Runtime* pRuntime) {
659   ASSERT(m_pFormFillEnv);
660 
661   CPDF_FormField* pFormField = GetFirstFormField();
662   if (!pFormField)
663     return CJS_Result::Failure(JSMessage::kBadObjectError);
664 
665   if (pFormField->GetFieldType() != FormFieldType::kTextField)
666     return CJS_Result::Failure(JSMessage::kObjectTypeError);
667 
668   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
669   if (!pFormControl)
670     return CJS_Result::Failure(JSMessage::kBadObjectError);
671 
672   switch (pFormControl->GetControlAlignment()) {
673     case 0:
674       return CJS_Result::Success(pRuntime->NewString("left"));
675     case 1:
676       return CJS_Result::Success(pRuntime->NewString("center"));
677     case 2:
678       return CJS_Result::Success(pRuntime->NewString("right"));
679   }
680   return CJS_Result::Success(pRuntime->NewString(""));
681 }
682 
set_alignment(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)683 CJS_Result CJS_Field::set_alignment(CJS_Runtime* pRuntime,
684                                     v8::Local<v8::Value> vp) {
685   ASSERT(m_pFormFillEnv);
686   if (!m_bCanSet)
687     return CJS_Result::Failure(JSMessage::kReadOnlyError);
688 
689   return CJS_Result::Success();
690 }
691 
get_border_style(CJS_Runtime * pRuntime)692 CJS_Result CJS_Field::get_border_style(CJS_Runtime* pRuntime) {
693   ASSERT(m_pFormFillEnv);
694 
695   CPDF_FormField* pFormField = GetFirstFormField();
696   if (!pFormField)
697     return CJS_Result::Failure(JSMessage::kBadObjectError);
698 
699   CPDFSDK_Widget* pWidget = m_pFormFillEnv->GetInteractiveForm()->GetWidget(
700       GetSmartFieldControl(pFormField));
701   if (!pWidget)
702     return CJS_Result::Failure(JSMessage::kBadObjectError);
703 
704   switch (pWidget->GetBorderStyle()) {
705     case BorderStyle::SOLID:
706       return CJS_Result::Success(pRuntime->NewString("solid"));
707     case BorderStyle::DASH:
708       return CJS_Result::Success(pRuntime->NewString("dashed"));
709     case BorderStyle::BEVELED:
710       return CJS_Result::Success(pRuntime->NewString("beveled"));
711     case BorderStyle::INSET:
712       return CJS_Result::Success(pRuntime->NewString("inset"));
713     case BorderStyle::UNDERLINE:
714       return CJS_Result::Success(pRuntime->NewString("underline"));
715   }
716   return CJS_Result::Success(pRuntime->NewString(""));
717 }
718 
set_border_style(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)719 CJS_Result CJS_Field::set_border_style(CJS_Runtime* pRuntime,
720                                        v8::Local<v8::Value> vp) {
721   ASSERT(m_pFormFillEnv);
722   if (!m_bCanSet)
723     return CJS_Result::Failure(JSMessage::kReadOnlyError);
724 
725   ByteString byte_str = pRuntime->ToWideString(vp).ToDefANSI();
726   if (m_bDelay) {
727     AddDelay_String(FP_BORDERSTYLE, byte_str);
728   } else {
729     SetBorderStyle(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex,
730                    byte_str);
731   }
732   return CJS_Result::Success();
733 }
734 
get_button_align_x(CJS_Runtime * pRuntime)735 CJS_Result CJS_Field::get_button_align_x(CJS_Runtime* pRuntime) {
736   ASSERT(m_pFormFillEnv);
737 
738   CPDF_FormField* pFormField = GetFirstFormField();
739   if (!pFormField)
740     return CJS_Result::Failure(JSMessage::kBadObjectError);
741 
742   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
743     return CJS_Result::Failure(JSMessage::kObjectTypeError);
744 
745   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
746   if (!pFormControl)
747     return CJS_Result::Failure(JSMessage::kBadObjectError);
748 
749   CPDF_IconFit IconFit = pFormControl->GetIconFit();
750   CFX_PointF pos = IconFit.GetIconBottomLeftPosition();
751   return CJS_Result::Success(pRuntime->NewNumber(static_cast<int32_t>(pos.x)));
752 }
753 
set_button_align_x(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)754 CJS_Result CJS_Field::set_button_align_x(CJS_Runtime* pRuntime,
755                                          v8::Local<v8::Value> vp) {
756   ASSERT(m_pFormFillEnv);
757   if (!m_bCanSet)
758     return CJS_Result::Failure(JSMessage::kReadOnlyError);
759   return CJS_Result::Success();
760 }
761 
get_button_align_y(CJS_Runtime * pRuntime)762 CJS_Result CJS_Field::get_button_align_y(CJS_Runtime* pRuntime) {
763   ASSERT(m_pFormFillEnv);
764 
765   CPDF_FormField* pFormField = GetFirstFormField();
766   if (!pFormField)
767     return CJS_Result::Failure(JSMessage::kBadObjectError);
768 
769   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
770     return CJS_Result::Failure(JSMessage::kObjectTypeError);
771 
772   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
773   if (!pFormControl)
774     return CJS_Result::Failure(JSMessage::kBadObjectError);
775 
776   CPDF_IconFit IconFit = pFormControl->GetIconFit();
777   CFX_PointF pos = IconFit.GetIconBottomLeftPosition();
778   return CJS_Result::Success(pRuntime->NewNumber(static_cast<int32_t>(pos.y)));
779 }
780 
set_button_align_y(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)781 CJS_Result CJS_Field::set_button_align_y(CJS_Runtime* pRuntime,
782                                          v8::Local<v8::Value> vp) {
783   ASSERT(m_pFormFillEnv);
784   if (!m_bCanSet)
785     return CJS_Result::Failure(JSMessage::kReadOnlyError);
786   return CJS_Result::Success();
787 }
788 
get_button_fit_bounds(CJS_Runtime * pRuntime)789 CJS_Result CJS_Field::get_button_fit_bounds(CJS_Runtime* pRuntime) {
790   ASSERT(m_pFormFillEnv);
791 
792   CPDF_FormField* pFormField = GetFirstFormField();
793   if (!pFormField)
794     return CJS_Result::Failure(JSMessage::kBadObjectError);
795 
796   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
797     return CJS_Result::Failure(JSMessage::kObjectTypeError);
798 
799   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
800   if (!pFormControl)
801     return CJS_Result::Failure(JSMessage::kBadObjectError);
802 
803   return CJS_Result::Success(
804       pRuntime->NewBoolean(pFormControl->GetIconFit().GetFittingBounds()));
805 }
806 
set_button_fit_bounds(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)807 CJS_Result CJS_Field::set_button_fit_bounds(CJS_Runtime* pRuntime,
808                                             v8::Local<v8::Value> vp) {
809   ASSERT(m_pFormFillEnv);
810   if (!m_bCanSet)
811     return CJS_Result::Failure(JSMessage::kReadOnlyError);
812   return CJS_Result::Success();
813 }
814 
get_button_position(CJS_Runtime * pRuntime)815 CJS_Result CJS_Field::get_button_position(CJS_Runtime* pRuntime) {
816   ASSERT(m_pFormFillEnv);
817 
818   CPDF_FormField* pFormField = GetFirstFormField();
819   if (!pFormField)
820     return CJS_Result::Failure(JSMessage::kBadObjectError);
821 
822   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
823     return CJS_Result::Failure(JSMessage::kObjectTypeError);
824 
825   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
826   if (!pFormControl)
827     return CJS_Result::Failure(JSMessage::kBadObjectError);
828 
829   return CJS_Result::Success(
830       pRuntime->NewNumber(pFormControl->GetTextPosition()));
831 }
832 
set_button_position(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)833 CJS_Result CJS_Field::set_button_position(CJS_Runtime* pRuntime,
834                                           v8::Local<v8::Value> vp) {
835   ASSERT(m_pFormFillEnv);
836   if (!m_bCanSet)
837     return CJS_Result::Failure(JSMessage::kBadObjectError);
838   return CJS_Result::Success();
839 }
840 
get_button_scale_how(CJS_Runtime * pRuntime)841 CJS_Result CJS_Field::get_button_scale_how(CJS_Runtime* pRuntime) {
842   ASSERT(m_pFormFillEnv);
843 
844   CPDF_FormField* pFormField = GetFirstFormField();
845   if (!pFormField)
846     return CJS_Result::Failure(JSMessage::kBadObjectError);
847 
848   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
849     return CJS_Result::Failure(JSMessage::kObjectTypeError);
850 
851   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
852   if (!pFormControl)
853     return CJS_Result::Failure(JSMessage::kBadObjectError);
854 
855   return CJS_Result::Success(pRuntime->NewBoolean(
856       pFormControl->GetIconFit().IsProportionalScale() ? 0 : 1));
857 }
858 
set_button_scale_how(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)859 CJS_Result CJS_Field::set_button_scale_how(CJS_Runtime* pRuntime,
860                                            v8::Local<v8::Value> vp) {
861   ASSERT(m_pFormFillEnv);
862   if (!m_bCanSet)
863     return CJS_Result::Failure(JSMessage::kReadOnlyError);
864   return CJS_Result::Success();
865 }
866 
get_button_scale_when(CJS_Runtime * pRuntime)867 CJS_Result CJS_Field::get_button_scale_when(CJS_Runtime* pRuntime) {
868   ASSERT(m_pFormFillEnv);
869 
870   CPDF_FormField* pFormField = GetFirstFormField();
871   if (!pFormField)
872     return CJS_Result::Failure(JSMessage::kBadObjectError);
873 
874   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
875     return CJS_Result::Failure(JSMessage::kObjectTypeError);
876 
877   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
878   if (!pFormControl)
879     return CJS_Result::Failure(JSMessage::kBadObjectError);
880 
881   CPDF_IconFit IconFit = pFormControl->GetIconFit();
882   int ScaleM = IconFit.GetScaleMethod();
883   switch (ScaleM) {
884     case CPDF_IconFit::Always:
885       return CJS_Result::Success(
886           pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Always)));
887     case CPDF_IconFit::Bigger:
888       return CJS_Result::Success(
889           pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Bigger)));
890     case CPDF_IconFit::Never:
891       return CJS_Result::Success(
892           pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Never)));
893     case CPDF_IconFit::Smaller:
894       return CJS_Result::Success(
895           pRuntime->NewNumber(static_cast<int32_t>(CPDF_IconFit::Smaller)));
896   }
897   return CJS_Result::Success();
898 }
899 
set_button_scale_when(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)900 CJS_Result CJS_Field::set_button_scale_when(CJS_Runtime* pRuntime,
901                                             v8::Local<v8::Value> vp) {
902   ASSERT(m_pFormFillEnv);
903   if (!m_bCanSet)
904     return CJS_Result::Failure(JSMessage::kReadOnlyError);
905   return CJS_Result::Success();
906 }
907 
get_calc_order_index(CJS_Runtime * pRuntime)908 CJS_Result CJS_Field::get_calc_order_index(CJS_Runtime* pRuntime) {
909   ASSERT(m_pFormFillEnv);
910 
911   CPDF_FormField* pFormField = GetFirstFormField();
912   if (!pFormField)
913     return CJS_Result::Failure(JSMessage::kBadObjectError);
914 
915   if (!IsComboBoxOrTextField(pFormField))
916     return CJS_Result::Failure(JSMessage::kObjectTypeError);
917 
918   CPDFSDK_InteractiveForm* pRDForm = m_pFormFillEnv->GetInteractiveForm();
919   CPDF_InteractiveForm* pForm = pRDForm->GetInteractiveForm();
920   return CJS_Result::Success(pRuntime->NewNumber(
921       static_cast<int32_t>(pForm->FindFieldInCalculationOrder(pFormField))));
922 }
923 
set_calc_order_index(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)924 CJS_Result CJS_Field::set_calc_order_index(CJS_Runtime* pRuntime,
925                                            v8::Local<v8::Value> vp) {
926   ASSERT(m_pFormFillEnv);
927   if (!m_bCanSet)
928     return CJS_Result::Failure(JSMessage::kReadOnlyError);
929   return CJS_Result::Success();
930 }
931 
get_char_limit(CJS_Runtime * pRuntime)932 CJS_Result CJS_Field::get_char_limit(CJS_Runtime* pRuntime) {
933   ASSERT(m_pFormFillEnv);
934 
935   CPDF_FormField* pFormField = GetFirstFormField();
936   if (!pFormField)
937     return CJS_Result::Failure(JSMessage::kBadObjectError);
938 
939   if (pFormField->GetFieldType() != FormFieldType::kTextField)
940     return CJS_Result::Failure(JSMessage::kObjectTypeError);
941   return CJS_Result::Success(
942       pRuntime->NewNumber(static_cast<int32_t>(pFormField->GetMaxLen())));
943 }
944 
set_char_limit(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)945 CJS_Result CJS_Field::set_char_limit(CJS_Runtime* pRuntime,
946                                      v8::Local<v8::Value> vp) {
947   ASSERT(m_pFormFillEnv);
948   if (!m_bCanSet)
949     return CJS_Result::Failure(JSMessage::kReadOnlyError);
950   return CJS_Result::Success();
951 }
952 
get_comb(CJS_Runtime * pRuntime)953 CJS_Result CJS_Field::get_comb(CJS_Runtime* pRuntime) {
954   ASSERT(m_pFormFillEnv);
955 
956   CPDF_FormField* pFormField = GetFirstFormField();
957   if (!pFormField)
958     return CJS_Result::Failure(JSMessage::kBadObjectError);
959 
960   if (pFormField->GetFieldType() != FormFieldType::kTextField)
961     return CJS_Result::Failure(JSMessage::kObjectTypeError);
962 
963   return CJS_Result::Success(pRuntime->NewBoolean(
964       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextComb)));
965 }
966 
set_comb(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)967 CJS_Result CJS_Field::set_comb(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
968   ASSERT(m_pFormFillEnv);
969   if (!m_bCanSet)
970     return CJS_Result::Failure(JSMessage::kReadOnlyError);
971   return CJS_Result::Success();
972 }
973 
get_commit_on_sel_change(CJS_Runtime * pRuntime)974 CJS_Result CJS_Field::get_commit_on_sel_change(CJS_Runtime* pRuntime) {
975   ASSERT(m_pFormFillEnv);
976 
977   CPDF_FormField* pFormField = GetFirstFormField();
978   if (!pFormField)
979     return CJS_Result::Failure(JSMessage::kBadObjectError);
980 
981   if (!IsComboBoxOrListBox(pFormField))
982     return CJS_Result::Failure(JSMessage::kObjectTypeError);
983 
984   uint32_t dwFieldFlags = pFormField->GetFieldFlags();
985   return CJS_Result::Success(pRuntime->NewBoolean(
986       !!(dwFieldFlags & pdfium::form_flags::kChoiceCommitOnSelChange)));
987 }
988 
set_commit_on_sel_change(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)989 CJS_Result CJS_Field::set_commit_on_sel_change(CJS_Runtime* pRuntime,
990                                                v8::Local<v8::Value> vp) {
991   ASSERT(m_pFormFillEnv);
992   if (!m_bCanSet)
993     return CJS_Result::Failure(JSMessage::kReadOnlyError);
994   return CJS_Result::Success();
995 }
996 
get_current_value_indices(CJS_Runtime * pRuntime)997 CJS_Result CJS_Field::get_current_value_indices(CJS_Runtime* pRuntime) {
998   CPDF_FormField* pFormField = GetFirstFormField();
999   if (!pFormField)
1000     return CJS_Result::Failure(JSMessage::kBadObjectError);
1001 
1002   if (!IsComboBoxOrListBox(pFormField))
1003     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1004 
1005   int count = pFormField->CountSelectedItems();
1006   if (count <= 0)
1007     return CJS_Result::Success(pRuntime->NewNumber(-1));
1008   if (count == 1)
1009     return CJS_Result::Success(
1010         pRuntime->NewNumber(pFormField->GetSelectedIndex(0)));
1011 
1012   v8::Local<v8::Array> SelArray = pRuntime->NewArray();
1013   for (int i = 0; i < count; i++) {
1014     pRuntime->PutArrayElement(
1015         SelArray, i, pRuntime->NewNumber(pFormField->GetSelectedIndex(i)));
1016   }
1017   if (SelArray.IsEmpty())
1018     return CJS_Result::Success(pRuntime->NewArray());
1019   return CJS_Result::Success(SelArray);
1020 }
1021 
set_current_value_indices(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1022 CJS_Result CJS_Field::set_current_value_indices(CJS_Runtime* pRuntime,
1023                                                 v8::Local<v8::Value> vp) {
1024   if (!m_bCanSet)
1025     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1026 
1027   std::vector<uint32_t> array;
1028   if (vp->IsNumber()) {
1029     array.push_back(pRuntime->ToInt32(vp));
1030   } else if (!vp.IsEmpty() && vp->IsArray()) {
1031     v8::Local<v8::Array> SelArray = pRuntime->ToArray(vp);
1032     for (size_t i = 0; i < pRuntime->GetArrayLength(SelArray); i++) {
1033       array.push_back(
1034           pRuntime->ToInt32(pRuntime->GetArrayElement(SelArray, i)));
1035     }
1036   }
1037 
1038   if (m_bDelay) {
1039     AddDelay_WordArray(FP_CURRENTVALUEINDICES, array);
1040   } else {
1041     SetCurrentValueIndices(m_pFormFillEnv.Get(), m_FieldName,
1042                            m_nFormControlIndex, array);
1043   }
1044   return CJS_Result::Success();
1045 }
1046 
get_default_style(CJS_Runtime * pRuntime)1047 CJS_Result CJS_Field::get_default_style(CJS_Runtime* pRuntime) {
1048   return CJS_Result::Failure(JSMessage::kNotSupportedError);
1049 }
1050 
set_default_style(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1051 CJS_Result CJS_Field::set_default_style(CJS_Runtime* pRuntime,
1052                                         v8::Local<v8::Value> vp) {
1053   return CJS_Result::Failure(JSMessage::kNotSupportedError);
1054 }
1055 
get_default_value(CJS_Runtime * pRuntime)1056 CJS_Result CJS_Field::get_default_value(CJS_Runtime* pRuntime) {
1057   ASSERT(m_pFormFillEnv);
1058 
1059   CPDF_FormField* pFormField = GetFirstFormField();
1060   if (!pFormField)
1061     return CJS_Result::Failure(JSMessage::kBadObjectError);
1062 
1063   if (pFormField->GetFieldType() == FormFieldType::kPushButton ||
1064       pFormField->GetFieldType() == FormFieldType::kSignature) {
1065     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1066   }
1067 
1068   return CJS_Result::Success(
1069       pRuntime->NewString(pFormField->GetDefaultValue().AsStringView()));
1070 }
1071 
set_default_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1072 CJS_Result CJS_Field::set_default_value(CJS_Runtime* pRuntime,
1073                                         v8::Local<v8::Value> vp) {
1074   ASSERT(m_pFormFillEnv);
1075   if (!m_bCanSet)
1076     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1077   return CJS_Result::Success();
1078 }
1079 
get_do_not_scroll(CJS_Runtime * pRuntime)1080 CJS_Result CJS_Field::get_do_not_scroll(CJS_Runtime* pRuntime) {
1081   ASSERT(m_pFormFillEnv);
1082 
1083   CPDF_FormField* pFormField = GetFirstFormField();
1084   if (!pFormField)
1085     return CJS_Result::Failure(JSMessage::kBadObjectError);
1086 
1087   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1088     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1089 
1090   return CJS_Result::Success(pRuntime->NewBoolean(
1091       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextDoNotScroll)));
1092 }
1093 
set_do_not_scroll(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1094 CJS_Result CJS_Field::set_do_not_scroll(CJS_Runtime* pRuntime,
1095                                         v8::Local<v8::Value> vp) {
1096   ASSERT(m_pFormFillEnv);
1097   if (!m_bCanSet)
1098     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1099   return CJS_Result::Success();
1100 }
1101 
get_do_not_spell_check(CJS_Runtime * pRuntime)1102 CJS_Result CJS_Field::get_do_not_spell_check(CJS_Runtime* pRuntime) {
1103   ASSERT(m_pFormFillEnv);
1104 
1105   CPDF_FormField* pFormField = GetFirstFormField();
1106   if (!pFormField)
1107     return CJS_Result::Failure(JSMessage::kBadObjectError);
1108 
1109   if (!IsComboBoxOrTextField(pFormField))
1110     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1111 
1112   uint32_t dwFieldFlags = pFormField->GetFieldFlags();
1113   return CJS_Result::Success(pRuntime->NewBoolean(
1114       !!(dwFieldFlags & pdfium::form_flags::kTextDoNotSpellCheck)));
1115 }
1116 
set_do_not_spell_check(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1117 CJS_Result CJS_Field::set_do_not_spell_check(CJS_Runtime* pRuntime,
1118                                              v8::Local<v8::Value> vp) {
1119   ASSERT(m_pFormFillEnv);
1120   if (!m_bCanSet)
1121     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1122   return CJS_Result::Success();
1123 }
1124 
SetDelay(bool bDelay)1125 void CJS_Field::SetDelay(bool bDelay) {
1126   m_bDelay = bDelay;
1127 
1128   if (m_bDelay)
1129     return;
1130   if (m_pJSDoc)
1131     m_pJSDoc->DoFieldDelay(m_FieldName, m_nFormControlIndex);
1132 }
1133 
get_delay(CJS_Runtime * pRuntime)1134 CJS_Result CJS_Field::get_delay(CJS_Runtime* pRuntime) {
1135   return CJS_Result::Success(pRuntime->NewBoolean(m_bDelay));
1136 }
1137 
set_delay(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1138 CJS_Result CJS_Field::set_delay(CJS_Runtime* pRuntime,
1139                                 v8::Local<v8::Value> vp) {
1140   if (!m_bCanSet)
1141     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1142 
1143   SetDelay(pRuntime->ToBoolean(vp));
1144   return CJS_Result::Success();
1145 }
1146 
get_display(CJS_Runtime * pRuntime)1147 CJS_Result CJS_Field::get_display(CJS_Runtime* pRuntime) {
1148   CPDF_FormField* pFormField = GetFirstFormField();
1149   if (!pFormField)
1150     return CJS_Result::Failure(JSMessage::kBadObjectError);
1151 
1152   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1153   CPDFSDK_Widget* pWidget = pForm->GetWidget(GetSmartFieldControl(pFormField));
1154   if (!pWidget)
1155     return CJS_Result::Failure(JSMessage::kBadObjectError);
1156 
1157   uint32_t dwFlag = pWidget->GetFlags();
1158   if (pdfium::annotation_flags::kInvisible & dwFlag ||
1159       pdfium::annotation_flags::kHidden & dwFlag) {
1160     return CJS_Result::Success(pRuntime->NewNumber(1));
1161   }
1162 
1163   if (pdfium::annotation_flags::kPrint & dwFlag) {
1164     if (pdfium::annotation_flags::kNoView & dwFlag)
1165       return CJS_Result::Success(pRuntime->NewNumber(3));
1166     return CJS_Result::Success(pRuntime->NewNumber(0));
1167   }
1168   return CJS_Result::Success(pRuntime->NewNumber(2));
1169 }
1170 
set_display(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1171 CJS_Result CJS_Field::set_display(CJS_Runtime* pRuntime,
1172                                   v8::Local<v8::Value> vp) {
1173   if (!m_bCanSet)
1174     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1175 
1176   if (m_bDelay) {
1177     AddDelay_Int(FP_DISPLAY, pRuntime->ToInt32(vp));
1178   } else {
1179     SetDisplay(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex,
1180                pRuntime->ToInt32(vp));
1181   }
1182   return CJS_Result::Success();
1183 }
1184 
get_doc(CJS_Runtime * pRuntime)1185 CJS_Result CJS_Field::get_doc(CJS_Runtime* pRuntime) {
1186   return CJS_Result::Success(m_pJSDoc->ToV8Object());
1187 }
1188 
set_doc(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1189 CJS_Result CJS_Field::set_doc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
1190   return CJS_Result::Failure(JSMessage::kNotSupportedError);
1191 }
1192 
get_editable(CJS_Runtime * pRuntime)1193 CJS_Result CJS_Field::get_editable(CJS_Runtime* pRuntime) {
1194   CPDF_FormField* pFormField = GetFirstFormField();
1195   if (!pFormField)
1196     return CJS_Result::Failure(JSMessage::kBadObjectError);
1197 
1198   if (pFormField->GetFieldType() != FormFieldType::kComboBox)
1199     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1200 
1201   return CJS_Result::Success(pRuntime->NewBoolean(
1202       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kChoiceEdit)));
1203 }
1204 
set_editable(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1205 CJS_Result CJS_Field::set_editable(CJS_Runtime* pRuntime,
1206                                    v8::Local<v8::Value> vp) {
1207   if (!m_bCanSet)
1208     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1209   return CJS_Result::Success();
1210 }
1211 
get_export_values(CJS_Runtime * pRuntime)1212 CJS_Result CJS_Field::get_export_values(CJS_Runtime* pRuntime) {
1213   CPDF_FormField* pFormField = GetFirstFormField();
1214   if (!pFormField)
1215     return CJS_Result::Failure(JSMessage::kBadObjectError);
1216 
1217   if (!IsCheckBoxOrRadioButton(pFormField))
1218     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1219 
1220   v8::Local<v8::Array> ExportValuesArray = pRuntime->NewArray();
1221   if (m_nFormControlIndex < 0) {
1222     for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
1223       CPDF_FormControl* pFormControl = pFormField->GetControl(i);
1224       pRuntime->PutArrayElement(
1225           ExportValuesArray, i,
1226           pRuntime->NewString(pFormControl->GetExportValue().AsStringView()));
1227     }
1228   } else {
1229     if (m_nFormControlIndex >= pFormField->CountControls())
1230       return CJS_Result::Failure(JSMessage::kValueError);
1231 
1232     CPDF_FormControl* pFormControl =
1233         pFormField->GetControl(m_nFormControlIndex);
1234     if (!pFormControl)
1235       return CJS_Result::Failure(JSMessage::kBadObjectError);
1236 
1237     pRuntime->PutArrayElement(
1238         ExportValuesArray, 0,
1239         pRuntime->NewString(pFormControl->GetExportValue().AsStringView()));
1240   }
1241   return CJS_Result::Success(ExportValuesArray);
1242 }
1243 
set_export_values(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1244 CJS_Result CJS_Field::set_export_values(CJS_Runtime* pRuntime,
1245                                         v8::Local<v8::Value> vp) {
1246   CPDF_FormField* pFormField = GetFirstFormField();
1247   if (!pFormField)
1248     return CJS_Result::Failure(JSMessage::kBadObjectError);
1249 
1250   if (!IsCheckBoxOrRadioButton(pFormField))
1251     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1252 
1253   if (!m_bCanSet)
1254     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1255 
1256   if (vp.IsEmpty() || !vp->IsArray())
1257     return CJS_Result::Failure(JSMessage::kBadObjectError);
1258 
1259   return CJS_Result::Success();
1260 }
1261 
get_file_select(CJS_Runtime * pRuntime)1262 CJS_Result CJS_Field::get_file_select(CJS_Runtime* pRuntime) {
1263   CPDF_FormField* pFormField = GetFirstFormField();
1264   if (!pFormField)
1265     return CJS_Result::Failure(JSMessage::kBadObjectError);
1266 
1267   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1268     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1269 
1270   return CJS_Result::Success(pRuntime->NewBoolean(
1271       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextFileSelect)));
1272 }
1273 
set_file_select(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1274 CJS_Result CJS_Field::set_file_select(CJS_Runtime* pRuntime,
1275                                       v8::Local<v8::Value> vp) {
1276   CPDF_FormField* pFormField = GetFirstFormField();
1277   if (!pFormField)
1278     return CJS_Result::Failure(JSMessage::kBadObjectError);
1279 
1280   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1281     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1282 
1283   if (!m_bCanSet)
1284     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1285 
1286   return CJS_Result::Success();
1287 }
1288 
get_fill_color(CJS_Runtime * pRuntime)1289 CJS_Result CJS_Field::get_fill_color(CJS_Runtime* pRuntime) {
1290   CPDF_FormField* pFormField = GetFirstFormField();
1291   if (!pFormField)
1292     return CJS_Result::Failure(JSMessage::kBadObjectError);
1293 
1294   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1295   if (!pFormControl)
1296     return CJS_Result::Failure(JSMessage::kBadObjectError);
1297 
1298   int iColorType;
1299   pFormControl->GetBackgroundColor(iColorType);
1300 
1301   CFX_Color color;
1302   if (iColorType == CFX_Color::kTransparent) {
1303     color = CFX_Color(CFX_Color::kTransparent);
1304   } else if (iColorType == CFX_Color::kGray) {
1305     color = CFX_Color(CFX_Color::kGray,
1306                       pFormControl->GetOriginalBackgroundColor(0));
1307   } else if (iColorType == CFX_Color::kRGB) {
1308     color =
1309         CFX_Color(CFX_Color::kRGB, pFormControl->GetOriginalBackgroundColor(0),
1310                   pFormControl->GetOriginalBackgroundColor(1),
1311                   pFormControl->GetOriginalBackgroundColor(2));
1312   } else if (iColorType == CFX_Color::kCMYK) {
1313     color =
1314         CFX_Color(CFX_Color::kCMYK, pFormControl->GetOriginalBackgroundColor(0),
1315                   pFormControl->GetOriginalBackgroundColor(1),
1316                   pFormControl->GetOriginalBackgroundColor(2),
1317                   pFormControl->GetOriginalBackgroundColor(3));
1318   } else {
1319     return CJS_Result::Failure(JSMessage::kValueError);
1320   }
1321 
1322   v8::Local<v8::Value> array =
1323       CJS_Color::ConvertPWLColorToArray(pRuntime, color);
1324   if (array.IsEmpty())
1325     return CJS_Result::Success(pRuntime->NewArray());
1326   return CJS_Result::Success(array);
1327 }
1328 
set_fill_color(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1329 CJS_Result CJS_Field::set_fill_color(CJS_Runtime* pRuntime,
1330                                      v8::Local<v8::Value> vp) {
1331   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1332   if (FieldArray.empty())
1333     return CJS_Result::Failure(JSMessage::kBadObjectError);
1334   if (!m_bCanSet)
1335     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1336   if (vp.IsEmpty() || !vp->IsArray())
1337     return CJS_Result::Failure(JSMessage::kBadObjectError);
1338   return CJS_Result::Success();
1339 }
1340 
get_hidden(CJS_Runtime * pRuntime)1341 CJS_Result CJS_Field::get_hidden(CJS_Runtime* pRuntime) {
1342   CPDF_FormField* pFormField = GetFirstFormField();
1343   if (!pFormField)
1344     return CJS_Result::Failure(JSMessage::kBadObjectError);
1345 
1346   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1347   CPDFSDK_Widget* pWidget = pForm->GetWidget(GetSmartFieldControl(pFormField));
1348   if (!pWidget)
1349     return CJS_Result::Failure(JSMessage::kBadObjectError);
1350 
1351   uint32_t dwFlags = pWidget->GetFlags();
1352   return CJS_Result::Success(
1353       pRuntime->NewBoolean(pdfium::annotation_flags::kInvisible & dwFlags ||
1354                            pdfium::annotation_flags::kHidden & dwFlags));
1355 }
1356 
set_hidden(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1357 CJS_Result CJS_Field::set_hidden(CJS_Runtime* pRuntime,
1358                                  v8::Local<v8::Value> vp) {
1359   if (!m_bCanSet)
1360     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1361 
1362   if (m_bDelay) {
1363     AddDelay_Bool(FP_HIDDEN, pRuntime->ToBoolean(vp));
1364   } else {
1365     SetHidden(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex,
1366               pRuntime->ToBoolean(vp));
1367   }
1368   return CJS_Result::Success();
1369 }
1370 
get_highlight(CJS_Runtime * pRuntime)1371 CJS_Result CJS_Field::get_highlight(CJS_Runtime* pRuntime) {
1372   ASSERT(m_pFormFillEnv);
1373 
1374   CPDF_FormField* pFormField = GetFirstFormField();
1375   if (!pFormField)
1376     return CJS_Result::Failure(JSMessage::kBadObjectError);
1377 
1378   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
1379     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1380 
1381   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1382   if (!pFormControl)
1383     return CJS_Result::Failure(JSMessage::kBadObjectError);
1384 
1385   int eHM = pFormControl->GetHighlightingMode();
1386   switch (eHM) {
1387     case CPDF_FormControl::None:
1388       return CJS_Result::Success(pRuntime->NewString("none"));
1389     case CPDF_FormControl::Push:
1390       return CJS_Result::Success(pRuntime->NewString("push"));
1391     case CPDF_FormControl::Invert:
1392       return CJS_Result::Success(pRuntime->NewString("invert"));
1393     case CPDF_FormControl::Outline:
1394       return CJS_Result::Success(pRuntime->NewString("outline"));
1395     case CPDF_FormControl::Toggle:
1396       return CJS_Result::Success(pRuntime->NewString("toggle"));
1397   }
1398   return CJS_Result::Success();
1399 }
1400 
set_highlight(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1401 CJS_Result CJS_Field::set_highlight(CJS_Runtime* pRuntime,
1402                                     v8::Local<v8::Value> vp) {
1403   ASSERT(m_pFormFillEnv);
1404   if (!m_bCanSet)
1405     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1406   return CJS_Result::Success();
1407 }
1408 
get_line_width(CJS_Runtime * pRuntime)1409 CJS_Result CJS_Field::get_line_width(CJS_Runtime* pRuntime) {
1410   CPDF_FormField* pFormField = GetFirstFormField();
1411   if (!pFormField)
1412     return CJS_Result::Failure(JSMessage::kBadObjectError);
1413 
1414   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1415   if (!pFormControl)
1416     return CJS_Result::Failure(JSMessage::kBadObjectError);
1417 
1418   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1419   if (!pFormField->CountControls())
1420     return CJS_Result::Failure(JSMessage::kBadObjectError);
1421 
1422   CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormField->GetControl(0));
1423   if (!pWidget)
1424     return CJS_Result::Failure(JSMessage::kBadObjectError);
1425 
1426   return CJS_Result::Success(pRuntime->NewNumber(pWidget->GetBorderWidth()));
1427 }
1428 
set_line_width(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1429 CJS_Result CJS_Field::set_line_width(CJS_Runtime* pRuntime,
1430                                      v8::Local<v8::Value> vp) {
1431   if (!m_bCanSet)
1432     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1433 
1434   if (m_bDelay) {
1435     AddDelay_Int(FP_LINEWIDTH, pRuntime->ToInt32(vp));
1436   } else {
1437     SetLineWidth(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex,
1438                  pRuntime->ToInt32(vp));
1439   }
1440   return CJS_Result::Success();
1441 }
1442 
get_multiline(CJS_Runtime * pRuntime)1443 CJS_Result CJS_Field::get_multiline(CJS_Runtime* pRuntime) {
1444   ASSERT(m_pFormFillEnv);
1445 
1446   CPDF_FormField* pFormField = GetFirstFormField();
1447   if (!pFormField)
1448     return CJS_Result::Failure(JSMessage::kBadObjectError);
1449 
1450   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1451     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1452 
1453   return CJS_Result::Success(pRuntime->NewBoolean(
1454       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextMultiline)));
1455 }
1456 
set_multiline(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1457 CJS_Result CJS_Field::set_multiline(CJS_Runtime* pRuntime,
1458                                     v8::Local<v8::Value> vp) {
1459   ASSERT(m_pFormFillEnv);
1460   if (!m_bCanSet)
1461     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1462   return CJS_Result::Success();
1463 }
1464 
get_multiple_selection(CJS_Runtime * pRuntime)1465 CJS_Result CJS_Field::get_multiple_selection(CJS_Runtime* pRuntime) {
1466   ASSERT(m_pFormFillEnv);
1467   CPDF_FormField* pFormField = GetFirstFormField();
1468   if (!pFormField)
1469     return CJS_Result::Failure(JSMessage::kBadObjectError);
1470 
1471   if (pFormField->GetFieldType() != FormFieldType::kListBox)
1472     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1473 
1474   uint32_t dwFieldFlags = pFormField->GetFieldFlags();
1475   return CJS_Result::Success(pRuntime->NewBoolean(
1476       !!(dwFieldFlags & pdfium::form_flags::kChoiceMultiSelect)));
1477 }
1478 
set_multiple_selection(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1479 CJS_Result CJS_Field::set_multiple_selection(CJS_Runtime* pRuntime,
1480                                              v8::Local<v8::Value> vp) {
1481   ASSERT(m_pFormFillEnv);
1482   if (!m_bCanSet)
1483     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1484   return CJS_Result::Success();
1485 }
1486 
get_name(CJS_Runtime * pRuntime)1487 CJS_Result CJS_Field::get_name(CJS_Runtime* pRuntime) {
1488   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1489   if (FieldArray.empty())
1490     return CJS_Result::Failure(JSMessage::kBadObjectError);
1491 
1492   return CJS_Result::Success(pRuntime->NewString(m_FieldName.AsStringView()));
1493 }
1494 
set_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1495 CJS_Result CJS_Field::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
1496   return CJS_Result::Failure(JSMessage::kNotSupportedError);
1497 }
1498 
get_num_items(CJS_Runtime * pRuntime)1499 CJS_Result CJS_Field::get_num_items(CJS_Runtime* pRuntime) {
1500   CPDF_FormField* pFormField = GetFirstFormField();
1501   if (!pFormField)
1502     return CJS_Result::Failure(JSMessage::kBadObjectError);
1503 
1504   if (!IsComboBoxOrListBox(pFormField))
1505     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1506 
1507   return CJS_Result::Success(pRuntime->NewNumber(pFormField->CountOptions()));
1508 }
1509 
set_num_items(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1510 CJS_Result CJS_Field::set_num_items(CJS_Runtime* pRuntime,
1511                                     v8::Local<v8::Value> vp) {
1512   return CJS_Result::Failure(JSMessage::kNotSupportedError);
1513 }
1514 
get_page(CJS_Runtime * pRuntime)1515 CJS_Result CJS_Field::get_page(CJS_Runtime* pRuntime) {
1516   CPDF_FormField* pFormField = GetFirstFormField();
1517   if (!pFormField)
1518     return CJS_Result::Failure(JSMessage::kBadObjectError);
1519 
1520   std::vector<ObservedPtr<CPDFSDK_Annot>> widgets;
1521   m_pFormFillEnv->GetInteractiveForm()->GetWidgets(pFormField, &widgets);
1522   if (widgets.empty())
1523     return CJS_Result::Success(pRuntime->NewNumber(-1));
1524 
1525   v8::Local<v8::Array> PageArray = pRuntime->NewArray();
1526   int i = 0;
1527   for (const auto& pObserved : widgets) {
1528     if (!pObserved)
1529       return CJS_Result::Failure(JSMessage::kBadObjectError);
1530 
1531     auto* pWidget = ToCPDFSDKWidget(pObserved.Get());
1532     CPDFSDK_PageView* pPageView = pWidget->GetPageView();
1533     if (!pPageView)
1534       return CJS_Result::Failure(JSMessage::kBadObjectError);
1535 
1536     pRuntime->PutArrayElement(
1537         PageArray, i,
1538         pRuntime->NewNumber(static_cast<int32_t>(pPageView->GetPageIndex())));
1539     ++i;
1540   }
1541   return CJS_Result::Success(PageArray);
1542 }
1543 
set_page(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1544 CJS_Result CJS_Field::set_page(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
1545   return CJS_Result::Failure(JSMessage::kReadOnlyError);
1546 }
1547 
get_password(CJS_Runtime * pRuntime)1548 CJS_Result CJS_Field::get_password(CJS_Runtime* pRuntime) {
1549   ASSERT(m_pFormFillEnv);
1550 
1551   CPDF_FormField* pFormField = GetFirstFormField();
1552   if (!pFormField)
1553     return CJS_Result::Failure(JSMessage::kBadObjectError);
1554 
1555   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1556     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1557 
1558   return CJS_Result::Success(pRuntime->NewBoolean(
1559       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextPassword)));
1560 }
1561 
set_password(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1562 CJS_Result CJS_Field::set_password(CJS_Runtime* pRuntime,
1563                                    v8::Local<v8::Value> vp) {
1564   ASSERT(m_pFormFillEnv);
1565   if (!m_bCanSet)
1566     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1567   return CJS_Result::Success();
1568 }
1569 
get_print(CJS_Runtime * pRuntime)1570 CJS_Result CJS_Field::get_print(CJS_Runtime* pRuntime) {
1571   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1572   CPDF_FormField* pFormField = GetFirstFormField();
1573   if (!pFormField)
1574     return CJS_Result::Failure(JSMessage::kBadObjectError);
1575 
1576   CPDFSDK_Widget* pWidget = pForm->GetWidget(GetSmartFieldControl(pFormField));
1577   if (!pWidget)
1578     return CJS_Result::Failure(JSMessage::kBadObjectError);
1579 
1580   return CJS_Result::Success(pRuntime->NewBoolean(
1581       !!(pWidget->GetFlags() & pdfium::annotation_flags::kPrint)));
1582 }
1583 
set_print(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1584 CJS_Result CJS_Field::set_print(CJS_Runtime* pRuntime,
1585                                 v8::Local<v8::Value> vp) {
1586   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1587   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1588   if (FieldArray.empty())
1589     return CJS_Result::Failure(JSMessage::kBadObjectError);
1590 
1591   if (!m_bCanSet)
1592     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1593 
1594   for (CPDF_FormField* pFormField : FieldArray) {
1595     if (m_nFormControlIndex < 0) {
1596       bool bSet = false;
1597       for (int i = 0, sz = pFormField->CountControls(); i < sz; ++i) {
1598         if (CPDFSDK_Widget* pWidget =
1599                 pForm->GetWidget(pFormField->GetControl(i))) {
1600           uint32_t dwFlags = pWidget->GetFlags();
1601           if (pRuntime->ToBoolean(vp))
1602             dwFlags |= pdfium::annotation_flags::kPrint;
1603           else
1604             dwFlags &= ~pdfium::annotation_flags::kPrint;
1605 
1606           if (dwFlags != pWidget->GetFlags()) {
1607             pWidget->SetFlags(dwFlags);
1608             bSet = true;
1609           }
1610         }
1611       }
1612 
1613       if (bSet)
1614         UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, false, true);
1615 
1616       continue;
1617     }
1618 
1619     if (m_nFormControlIndex >= pFormField->CountControls())
1620       return CJS_Result::Failure(JSMessage::kValueError);
1621 
1622     if (CPDF_FormControl* pFormControl =
1623             pFormField->GetControl(m_nFormControlIndex)) {
1624       if (CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl)) {
1625         uint32_t dwFlags = pWidget->GetFlags();
1626         if (pRuntime->ToBoolean(vp))
1627           dwFlags |= pdfium::annotation_flags::kPrint;
1628         else
1629           dwFlags &= ~pdfium::annotation_flags::kPrint;
1630 
1631         if (dwFlags != pWidget->GetFlags()) {
1632           pWidget->SetFlags(dwFlags);
1633           UpdateFormControl(m_pFormFillEnv.Get(),
1634                             pFormField->GetControl(m_nFormControlIndex), true,
1635                             false, true);
1636         }
1637       }
1638     }
1639   }
1640   return CJS_Result::Success();
1641 }
1642 
get_radios_in_unison(CJS_Runtime * pRuntime)1643 CJS_Result CJS_Field::get_radios_in_unison(CJS_Runtime* pRuntime) {
1644   CPDF_FormField* pFormField = GetFirstFormField();
1645   if (!pFormField)
1646     return CJS_Result::Failure(JSMessage::kBadObjectError);
1647 
1648   if (pFormField->GetFieldType() != FormFieldType::kRadioButton)
1649     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1650 
1651   uint32_t dwFieldFlags = pFormField->GetFieldFlags();
1652   return CJS_Result::Success(pRuntime->NewBoolean(
1653       !!(dwFieldFlags & pdfium::form_flags::kButtonRadiosInUnison)));
1654 }
1655 
set_radios_in_unison(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1656 CJS_Result CJS_Field::set_radios_in_unison(CJS_Runtime* pRuntime,
1657                                            v8::Local<v8::Value> vp) {
1658   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1659   if (FieldArray.empty())
1660     return CJS_Result::Failure(JSMessage::kBadObjectError);
1661   if (!m_bCanSet)
1662     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1663   return CJS_Result::Success();
1664 }
1665 
get_readonly(CJS_Runtime * pRuntime)1666 CJS_Result CJS_Field::get_readonly(CJS_Runtime* pRuntime) {
1667   CPDF_FormField* pFormField = GetFirstFormField();
1668   if (!pFormField)
1669     return CJS_Result::Failure(JSMessage::kBadObjectError);
1670 
1671   return CJS_Result::Success(pRuntime->NewBoolean(
1672       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kReadOnly)));
1673 }
1674 
set_readonly(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1675 CJS_Result CJS_Field::set_readonly(CJS_Runtime* pRuntime,
1676                                    v8::Local<v8::Value> vp) {
1677   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1678   if (FieldArray.empty())
1679     return CJS_Result::Failure(JSMessage::kBadObjectError);
1680   if (!m_bCanSet)
1681     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1682   return CJS_Result::Success();
1683 }
1684 
get_rect(CJS_Runtime * pRuntime)1685 CJS_Result CJS_Field::get_rect(CJS_Runtime* pRuntime) {
1686   CPDF_FormField* pFormField = GetFirstFormField();
1687   if (!pFormField)
1688     return CJS_Result::Failure(JSMessage::kBadObjectError);
1689 
1690   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
1691   CPDFSDK_Widget* pWidget = pForm->GetWidget(GetSmartFieldControl(pFormField));
1692   if (!pWidget)
1693     return CJS_Result::Failure(JSMessage::kBadObjectError);
1694 
1695   CFX_FloatRect crRect = pWidget->GetRect();
1696   v8::Local<v8::Array> rcArray = pRuntime->NewArray();
1697   pRuntime->PutArrayElement(
1698       rcArray, 0, pRuntime->NewNumber(static_cast<int32_t>(crRect.left)));
1699   pRuntime->PutArrayElement(
1700       rcArray, 1, pRuntime->NewNumber(static_cast<int32_t>(crRect.top)));
1701   pRuntime->PutArrayElement(
1702       rcArray, 2, pRuntime->NewNumber(static_cast<int32_t>(crRect.right)));
1703   pRuntime->PutArrayElement(
1704       rcArray, 3, pRuntime->NewNumber(static_cast<int32_t>(crRect.bottom)));
1705 
1706   return CJS_Result::Success(rcArray);
1707 }
1708 
set_rect(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1709 CJS_Result CJS_Field::set_rect(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
1710   if (!m_bCanSet)
1711     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1712   if (vp.IsEmpty() || !vp->IsArray())
1713     return CJS_Result::Failure(JSMessage::kValueError);
1714 
1715   v8::Local<v8::Array> rcArray = pRuntime->ToArray(vp);
1716   if (pRuntime->GetArrayLength(rcArray) < 4)
1717     return CJS_Result::Failure(JSMessage::kValueError);
1718 
1719   float pArray[4];
1720   pArray[0] = static_cast<float>(
1721       pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 0)));
1722   pArray[1] = static_cast<float>(
1723       pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 1)));
1724   pArray[2] = static_cast<float>(
1725       pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 2)));
1726   pArray[3] = static_cast<float>(
1727       pRuntime->ToInt32(pRuntime->GetArrayElement(rcArray, 3)));
1728 
1729   CFX_FloatRect crRect(pArray);
1730   if (m_bDelay) {
1731     AddDelay_Rect(FP_RECT, crRect);
1732   } else {
1733     SetRect(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex, crRect);
1734   }
1735   return CJS_Result::Success();
1736 }
1737 
get_required(CJS_Runtime * pRuntime)1738 CJS_Result CJS_Field::get_required(CJS_Runtime* pRuntime) {
1739   CPDF_FormField* pFormField = GetFirstFormField();
1740   if (!pFormField)
1741     return CJS_Result::Failure(JSMessage::kBadObjectError);
1742 
1743   if (pFormField->GetFieldType() == FormFieldType::kPushButton)
1744     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1745 
1746   return CJS_Result::Success(pRuntime->NewBoolean(
1747       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kRequired)));
1748 }
1749 
set_required(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1750 CJS_Result CJS_Field::set_required(CJS_Runtime* pRuntime,
1751                                    v8::Local<v8::Value> vp) {
1752   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
1753   if (FieldArray.empty())
1754     return CJS_Result::Failure(JSMessage::kBadObjectError);
1755   if (!m_bCanSet)
1756     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1757   return CJS_Result::Success();
1758 }
1759 
get_rich_text(CJS_Runtime * pRuntime)1760 CJS_Result CJS_Field::get_rich_text(CJS_Runtime* pRuntime) {
1761   ASSERT(m_pFormFillEnv);
1762 
1763   CPDF_FormField* pFormField = GetFirstFormField();
1764   if (!pFormField)
1765     return CJS_Result::Failure(JSMessage::kBadObjectError);
1766 
1767   if (pFormField->GetFieldType() != FormFieldType::kTextField)
1768     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1769 
1770   return CJS_Result::Success(pRuntime->NewBoolean(
1771       !!(pFormField->GetFieldFlags() & pdfium::form_flags::kTextRichText)));
1772 }
1773 
set_rich_text(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1774 CJS_Result CJS_Field::set_rich_text(CJS_Runtime* pRuntime,
1775                                     v8::Local<v8::Value> vp) {
1776   ASSERT(m_pFormFillEnv);
1777   if (!m_bCanSet)
1778     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1779   return CJS_Result::Success();
1780 }
1781 
get_rich_value(CJS_Runtime * pRuntime)1782 CJS_Result CJS_Field::get_rich_value(CJS_Runtime* pRuntime) {
1783   return CJS_Result::Success();
1784 }
1785 
set_rich_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1786 CJS_Result CJS_Field::set_rich_value(CJS_Runtime* pRuntime,
1787                                      v8::Local<v8::Value> vp) {
1788   return CJS_Result::Success();
1789 }
1790 
get_rotation(CJS_Runtime * pRuntime)1791 CJS_Result CJS_Field::get_rotation(CJS_Runtime* pRuntime) {
1792   ASSERT(m_pFormFillEnv);
1793 
1794   CPDF_FormField* pFormField = GetFirstFormField();
1795   if (!pFormField)
1796     return CJS_Result::Failure(JSMessage::kBadObjectError);
1797 
1798   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1799   if (!pFormControl)
1800     return CJS_Result::Failure(JSMessage::kBadObjectError);
1801 
1802   return CJS_Result::Success(pRuntime->NewNumber(pFormControl->GetRotation()));
1803 }
1804 
set_rotation(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1805 CJS_Result CJS_Field::set_rotation(CJS_Runtime* pRuntime,
1806                                    v8::Local<v8::Value> vp) {
1807   ASSERT(m_pFormFillEnv);
1808   if (!m_bCanSet)
1809     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1810   return CJS_Result::Success();
1811 }
1812 
get_source(CJS_Runtime * pRuntime)1813 CJS_Result CJS_Field::get_source(CJS_Runtime* pRuntime) {
1814   return CJS_Result::Success();
1815 }
1816 
set_source(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1817 CJS_Result CJS_Field::set_source(CJS_Runtime* pRuntime,
1818                                  v8::Local<v8::Value> vp) {
1819   return CJS_Result::Success();
1820 }
1821 
get_stroke_color(CJS_Runtime * pRuntime)1822 CJS_Result CJS_Field::get_stroke_color(CJS_Runtime* pRuntime) {
1823   CPDF_FormField* pFormField = GetFirstFormField();
1824   if (!pFormField)
1825     return CJS_Result::Failure(JSMessage::kBadObjectError);
1826 
1827   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1828   if (!pFormControl)
1829     return CJS_Result::Failure(JSMessage::kBadObjectError);
1830 
1831   int iColorType;
1832   pFormControl->GetBorderColor(iColorType);
1833 
1834   CFX_Color color;
1835   if (iColorType == CFX_Color::kTransparent) {
1836     color = CFX_Color(CFX_Color::kTransparent);
1837   } else if (iColorType == CFX_Color::kGray) {
1838     color =
1839         CFX_Color(CFX_Color::kGray, pFormControl->GetOriginalBorderColor(0));
1840   } else if (iColorType == CFX_Color::kRGB) {
1841     color = CFX_Color(CFX_Color::kRGB, pFormControl->GetOriginalBorderColor(0),
1842                       pFormControl->GetOriginalBorderColor(1),
1843                       pFormControl->GetOriginalBorderColor(2));
1844   } else if (iColorType == CFX_Color::kCMYK) {
1845     color = CFX_Color(CFX_Color::kCMYK, pFormControl->GetOriginalBorderColor(0),
1846                       pFormControl->GetOriginalBorderColor(1),
1847                       pFormControl->GetOriginalBorderColor(2),
1848                       pFormControl->GetOriginalBorderColor(3));
1849   } else {
1850     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1851   }
1852 
1853   v8::Local<v8::Value> array =
1854       CJS_Color::ConvertPWLColorToArray(pRuntime, color);
1855   if (array.IsEmpty())
1856     return CJS_Result::Success(pRuntime->NewArray());
1857   return CJS_Result::Success(array);
1858 }
1859 
set_stroke_color(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1860 CJS_Result CJS_Field::set_stroke_color(CJS_Runtime* pRuntime,
1861                                        v8::Local<v8::Value> vp) {
1862   if (!m_bCanSet)
1863     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1864   if (vp.IsEmpty() || !vp->IsArray())
1865     return CJS_Result::Failure(JSMessage::kBadObjectError);
1866   return CJS_Result::Success();
1867 }
1868 
get_style(CJS_Runtime * pRuntime)1869 CJS_Result CJS_Field::get_style(CJS_Runtime* pRuntime) {
1870   ASSERT(m_pFormFillEnv);
1871 
1872   CPDF_FormField* pFormField = GetFirstFormField();
1873   if (!pFormField)
1874     return CJS_Result::Failure(JSMessage::kBadObjectError);
1875 
1876   if (!IsCheckBoxOrRadioButton(pFormField))
1877     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1878 
1879   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1880   if (!pFormControl)
1881     return CJS_Result::Failure(JSMessage::kBadObjectError);
1882 
1883   WideString csWCaption = pFormControl->GetNormalCaption();
1884   wchar_t selector = !csWCaption.IsEmpty() ? csWCaption[0] : L'4';
1885 
1886   ByteString csBCaption;
1887   switch (selector) {
1888     case L'l':
1889       csBCaption = "circle";
1890       break;
1891     case L'8':
1892       csBCaption = "cross";
1893       break;
1894     case L'u':
1895       csBCaption = "diamond";
1896       break;
1897     case L'n':
1898       csBCaption = "square";
1899       break;
1900     case L'H':
1901       csBCaption = "star";
1902       break;
1903     default:  // L'4'
1904       csBCaption = "check";
1905       break;
1906   }
1907   return CJS_Result::Success(pRuntime->NewString(
1908       WideString::FromDefANSI(csBCaption.AsStringView()).AsStringView()));
1909 }
1910 
set_style(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1911 CJS_Result CJS_Field::set_style(CJS_Runtime* pRuntime,
1912                                 v8::Local<v8::Value> vp) {
1913   ASSERT(m_pFormFillEnv);
1914   if (!m_bCanSet)
1915     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1916   return CJS_Result::Success();
1917 }
1918 
get_submit_name(CJS_Runtime * pRuntime)1919 CJS_Result CJS_Field::get_submit_name(CJS_Runtime* pRuntime) {
1920   return CJS_Result::Success();
1921 }
1922 
set_submit_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1923 CJS_Result CJS_Field::set_submit_name(CJS_Runtime* pRuntime,
1924                                       v8::Local<v8::Value> vp) {
1925   return CJS_Result::Success();
1926 }
1927 
get_text_color(CJS_Runtime * pRuntime)1928 CJS_Result CJS_Field::get_text_color(CJS_Runtime* pRuntime) {
1929   CPDF_FormField* pFormField = GetFirstFormField();
1930   if (!pFormField)
1931     return CJS_Result::Failure(JSMessage::kBadObjectError);
1932 
1933   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1934   if (!pFormControl)
1935     return CJS_Result::Failure(JSMessage::kBadObjectError);
1936 
1937   Optional<CFX_Color::Type> iColorType;
1938   FX_ARGB color;
1939   CPDF_DefaultAppearance FieldAppearance = pFormControl->GetDefaultAppearance();
1940   std::tie(iColorType, color) = FieldAppearance.GetColor();
1941 
1942   CFX_Color crRet;
1943   if (!iColorType || *iColorType == CFX_Color::kTransparent) {
1944     crRet = CFX_Color(CFX_Color::kTransparent);
1945   } else {
1946     int32_t a;
1947     int32_t r;
1948     int32_t g;
1949     int32_t b;
1950     std::tie(a, r, g, b) = ArgbDecode(color);
1951     crRet = CFX_Color(CFX_Color::kRGB, r / 255.0f, g / 255.0f, b / 255.0f);
1952   }
1953 
1954   v8::Local<v8::Value> array =
1955       CJS_Color::ConvertPWLColorToArray(pRuntime, crRet);
1956   if (array.IsEmpty())
1957     return CJS_Result::Success(pRuntime->NewArray());
1958   return CJS_Result::Success(array);
1959 }
1960 
set_text_color(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1961 CJS_Result CJS_Field::set_text_color(CJS_Runtime* pRuntime,
1962                                      v8::Local<v8::Value> vp) {
1963   if (!m_bCanSet)
1964     return CJS_Result::Failure(JSMessage::kReadOnlyError);
1965   if (vp.IsEmpty() || !vp->IsArray())
1966     return CJS_Result::Failure(JSMessage::kBadObjectError);
1967   return CJS_Result::Success();
1968 }
1969 
get_text_font(CJS_Runtime * pRuntime)1970 CJS_Result CJS_Field::get_text_font(CJS_Runtime* pRuntime) {
1971   ASSERT(m_pFormFillEnv);
1972 
1973   CPDF_FormField* pFormField = GetFirstFormField();
1974   if (!pFormField)
1975     return CJS_Result::Failure(JSMessage::kBadObjectError);
1976 
1977   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
1978   if (!pFormControl)
1979     return CJS_Result::Failure(JSMessage::kBadObjectError);
1980 
1981   FormFieldType fieldType = pFormField->GetFieldType();
1982   if (fieldType != FormFieldType::kPushButton &&
1983       fieldType != FormFieldType::kComboBox &&
1984       fieldType != FormFieldType::kListBox &&
1985       fieldType != FormFieldType::kTextField) {
1986     return CJS_Result::Failure(JSMessage::kObjectTypeError);
1987   }
1988 
1989   Optional<WideString> wsFontName = pFormControl->GetDefaultControlFontName();
1990   if (!wsFontName.has_value())
1991     return CJS_Result::Failure(JSMessage::kBadObjectError);
1992 
1993   return CJS_Result::Success(
1994       pRuntime->NewString(wsFontName.value().AsStringView()));
1995 }
1996 
set_text_font(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)1997 CJS_Result CJS_Field::set_text_font(CJS_Runtime* pRuntime,
1998                                     v8::Local<v8::Value> vp) {
1999   ASSERT(m_pFormFillEnv);
2000 
2001   if (!m_bCanSet)
2002     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2003   if (pRuntime->ToWideString(vp).ToDefANSI().IsEmpty())
2004     return CJS_Result::Failure(JSMessage::kValueError);
2005   return CJS_Result::Success();
2006 }
2007 
get_text_size(CJS_Runtime * pRuntime)2008 CJS_Result CJS_Field::get_text_size(CJS_Runtime* pRuntime) {
2009   ASSERT(m_pFormFillEnv);
2010 
2011   CPDF_FormField* pFormField = GetFirstFormField();
2012   if (!pFormField)
2013     return CJS_Result::Failure(JSMessage::kBadObjectError);
2014 
2015   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
2016   if (!pFormControl)
2017     return CJS_Result::Failure(JSMessage::kBadObjectError);
2018 
2019   float fFontSize;
2020   CPDF_DefaultAppearance FieldAppearance = pFormControl->GetDefaultAppearance();
2021   FieldAppearance.GetFont(&fFontSize);
2022   return CJS_Result::Success(pRuntime->NewNumber(static_cast<int>(fFontSize)));
2023 }
2024 
set_text_size(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)2025 CJS_Result CJS_Field::set_text_size(CJS_Runtime* pRuntime,
2026                                     v8::Local<v8::Value> vp) {
2027   ASSERT(m_pFormFillEnv);
2028   if (!m_bCanSet)
2029     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2030   return CJS_Result::Success();
2031 }
2032 
get_type(CJS_Runtime * pRuntime)2033 CJS_Result CJS_Field::get_type(CJS_Runtime* pRuntime) {
2034   CPDF_FormField* pFormField = GetFirstFormField();
2035   if (!pFormField)
2036     return CJS_Result::Failure(JSMessage::kBadObjectError);
2037 
2038   switch (pFormField->GetFieldType()) {
2039     case FormFieldType::kUnknown:
2040       return CJS_Result::Success(pRuntime->NewString("unknown"));
2041     case FormFieldType::kPushButton:
2042       return CJS_Result::Success(pRuntime->NewString("button"));
2043     case FormFieldType::kCheckBox:
2044       return CJS_Result::Success(pRuntime->NewString("checkbox"));
2045     case FormFieldType::kRadioButton:
2046       return CJS_Result::Success(pRuntime->NewString("radiobutton"));
2047     case FormFieldType::kComboBox:
2048       return CJS_Result::Success(pRuntime->NewString("combobox"));
2049     case FormFieldType::kListBox:
2050       return CJS_Result::Success(pRuntime->NewString("listbox"));
2051     case FormFieldType::kTextField:
2052       return CJS_Result::Success(pRuntime->NewString("text"));
2053     case FormFieldType::kSignature:
2054       return CJS_Result::Success(pRuntime->NewString("signature"));
2055     default:
2056       return CJS_Result::Success(pRuntime->NewString("unknown"));
2057   }
2058 }
2059 
set_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)2060 CJS_Result CJS_Field::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
2061   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2062 }
2063 
get_user_name(CJS_Runtime * pRuntime)2064 CJS_Result CJS_Field::get_user_name(CJS_Runtime* pRuntime) {
2065   ASSERT(m_pFormFillEnv);
2066 
2067   CPDF_FormField* pFormField = GetFirstFormField();
2068   if (!pFormField)
2069     return CJS_Result::Failure(JSMessage::kBadObjectError);
2070 
2071   return CJS_Result::Success(
2072       pRuntime->NewString(pFormField->GetAlternateName().AsStringView()));
2073 }
2074 
set_user_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)2075 CJS_Result CJS_Field::set_user_name(CJS_Runtime* pRuntime,
2076                                     v8::Local<v8::Value> vp) {
2077   ASSERT(m_pFormFillEnv);
2078   if (!m_bCanSet)
2079     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2080   return CJS_Result::Success();
2081 }
2082 
get_value(CJS_Runtime * pRuntime)2083 CJS_Result CJS_Field::get_value(CJS_Runtime* pRuntime) {
2084   CPDF_FormField* pFormField = GetFirstFormField();
2085   if (!pFormField)
2086     return CJS_Result::Failure(JSMessage::kBadObjectError);
2087 
2088   v8::Local<v8::Value> ret;
2089   switch (pFormField->GetFieldType()) {
2090     case FormFieldType::kPushButton:
2091       return CJS_Result::Failure(JSMessage::kObjectTypeError);
2092     case FormFieldType::kComboBox:
2093     case FormFieldType::kTextField:
2094       ret = pRuntime->NewString(pFormField->GetValue().AsStringView());
2095       break;
2096     case FormFieldType::kListBox: {
2097       if (pFormField->CountSelectedItems() > 1) {
2098         v8::Local<v8::Array> ValueArray = pRuntime->NewArray();
2099         v8::Local<v8::Value> ElementValue;
2100         int iIndex;
2101         for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
2102           iIndex = pFormField->GetSelectedIndex(i);
2103           ElementValue = pRuntime->NewString(
2104               pFormField->GetOptionValue(iIndex).AsStringView());
2105           if (wcslen(pRuntime->ToWideString(ElementValue).c_str()) == 0) {
2106             ElementValue = pRuntime->NewString(
2107                 pFormField->GetOptionLabel(iIndex).AsStringView());
2108           }
2109           pRuntime->PutArrayElement(ValueArray, i, ElementValue);
2110         }
2111         ret = ValueArray;
2112       } else {
2113         ret = pRuntime->NewString(pFormField->GetValue().AsStringView());
2114       }
2115       break;
2116     }
2117     case FormFieldType::kCheckBox:
2118     case FormFieldType::kRadioButton: {
2119       bool bFind = false;
2120       for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2121         if (pFormField->GetControl(i)->IsChecked()) {
2122           ret = pRuntime->NewString(
2123               pFormField->GetControl(i)->GetExportValue().AsStringView());
2124           bFind = true;
2125           break;
2126         }
2127       }
2128       if (!bFind)
2129         ret = pRuntime->NewString("Off");
2130 
2131       break;
2132     }
2133     default:
2134       ret = pRuntime->NewString(pFormField->GetValue().AsStringView());
2135       break;
2136   }
2137   return CJS_Result::Success(pRuntime->MaybeCoerceToNumber(ret));
2138 }
2139 
set_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)2140 CJS_Result CJS_Field::set_value(CJS_Runtime* pRuntime,
2141                                 v8::Local<v8::Value> vp) {
2142   if (!m_bCanSet)
2143     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2144 
2145   std::vector<WideString> strArray;
2146   if (!vp.IsEmpty() && vp->IsArray()) {
2147     v8::Local<v8::Array> ValueArray = pRuntime->ToArray(vp);
2148     for (size_t i = 0; i < pRuntime->GetArrayLength(ValueArray); i++) {
2149       strArray.push_back(
2150           pRuntime->ToWideString(pRuntime->GetArrayElement(ValueArray, i)));
2151     }
2152   } else {
2153     strArray.push_back(pRuntime->ToWideString(vp));
2154   }
2155 
2156   if (m_bDelay) {
2157     AddDelay_WideStringArray(FP_VALUE, strArray);
2158   } else {
2159     SetFieldValue(m_pFormFillEnv.Get(), m_FieldName, m_nFormControlIndex,
2160                   strArray);
2161   }
2162   return CJS_Result::Success();
2163 }
2164 
get_value_as_string(CJS_Runtime * pRuntime)2165 CJS_Result CJS_Field::get_value_as_string(CJS_Runtime* pRuntime) {
2166   CPDF_FormField* pFormField = GetFirstFormField();
2167   if (!pFormField)
2168     return CJS_Result::Failure(JSMessage::kBadObjectError);
2169 
2170   if (pFormField->GetFieldType() == FormFieldType::kPushButton)
2171     return CJS_Result::Failure(JSMessage::kObjectTypeError);
2172 
2173   if (pFormField->GetFieldType() == FormFieldType::kCheckBox) {
2174     if (!pFormField->CountControls())
2175       return CJS_Result::Failure(JSMessage::kBadObjectError);
2176     return CJS_Result::Success(pRuntime->NewString(
2177         pFormField->GetControl(0)->IsChecked() ? L"Yes" : L"Off"));
2178   }
2179 
2180   if (pFormField->GetFieldType() == FormFieldType::kRadioButton &&
2181       !(pFormField->GetFieldFlags() &
2182         pdfium::form_flags::kButtonRadiosInUnison)) {
2183     for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2184       if (pFormField->GetControl(i)->IsChecked()) {
2185         return CJS_Result::Success(pRuntime->NewString(
2186             pFormField->GetControl(i)->GetExportValue().AsStringView()));
2187       }
2188     }
2189     return CJS_Result::Success(pRuntime->NewString("Off"));
2190   }
2191 
2192   if (pFormField->GetFieldType() == FormFieldType::kListBox &&
2193       (pFormField->CountSelectedItems() > 1)) {
2194     return CJS_Result::Success(pRuntime->NewString(""));
2195   }
2196   return CJS_Result::Success(
2197       pRuntime->NewString(pFormField->GetValue().AsStringView()));
2198 }
2199 
set_value_as_string(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)2200 CJS_Result CJS_Field::set_value_as_string(CJS_Runtime* pRuntime,
2201                                           v8::Local<v8::Value> vp) {
2202   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2203 }
2204 
browseForFileToSubmit(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2205 CJS_Result CJS_Field::browseForFileToSubmit(
2206     CJS_Runtime* pRuntime,
2207     const std::vector<v8::Local<v8::Value>>& params) {
2208   CPDF_FormField* pFormField = GetFirstFormField();
2209   if (!pFormField)
2210     return CJS_Result::Failure(JSMessage::kBadObjectError);
2211 
2212   if ((pFormField->GetFieldFlags() & pdfium::form_flags::kTextFileSelect) &&
2213       (pFormField->GetFieldType() == FormFieldType::kTextField)) {
2214     WideString wsFileName = m_pFormFillEnv->JS_fieldBrowse();
2215     if (!wsFileName.IsEmpty()) {
2216       pFormField->SetValue(wsFileName, NotificationOption::kDoNotNotify);
2217       UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, true, true);
2218     }
2219     return CJS_Result::Success();
2220   }
2221   return CJS_Result::Failure(JSMessage::kObjectTypeError);
2222 }
2223 
buttonGetCaption(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2224 CJS_Result CJS_Field::buttonGetCaption(
2225     CJS_Runtime* pRuntime,
2226     const std::vector<v8::Local<v8::Value>>& params) {
2227   int nface = 0;
2228   int iSize = params.size();
2229   if (iSize >= 1)
2230     nface = pRuntime->ToInt32(params[0]);
2231 
2232   CPDF_FormField* pFormField = GetFirstFormField();
2233   if (!pFormField)
2234     return CJS_Result::Failure(JSMessage::kBadObjectError);
2235 
2236   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
2237     return CJS_Result::Failure(JSMessage::kObjectTypeError);
2238 
2239   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
2240   if (!pFormControl)
2241     return CJS_Result::Failure(JSMessage::kBadObjectError);
2242 
2243   if (nface == 0) {
2244     return CJS_Result::Success(
2245         pRuntime->NewString(pFormControl->GetNormalCaption().AsStringView()));
2246   }
2247   if (nface == 1) {
2248     return CJS_Result::Success(
2249         pRuntime->NewString(pFormControl->GetDownCaption().AsStringView()));
2250   }
2251   if (nface == 2) {
2252     return CJS_Result::Success(
2253         pRuntime->NewString(pFormControl->GetRolloverCaption().AsStringView()));
2254   }
2255   return CJS_Result::Failure(JSMessage::kValueError);
2256 }
2257 
buttonGetIcon(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2258 CJS_Result CJS_Field::buttonGetIcon(
2259     CJS_Runtime* pRuntime,
2260     const std::vector<v8::Local<v8::Value>>& params) {
2261   if (params.size() >= 1) {
2262     int nFace = pRuntime->ToInt32(params[0]);
2263     if (nFace < 0 || nFace > 2)
2264       return CJS_Result::Failure(JSMessage::kValueError);
2265   }
2266 
2267   CPDF_FormField* pFormField = GetFirstFormField();
2268   if (!pFormField)
2269     return CJS_Result::Failure(JSMessage::kBadObjectError);
2270 
2271   if (pFormField->GetFieldType() != FormFieldType::kPushButton)
2272     return CJS_Result::Failure(JSMessage::kObjectTypeError);
2273 
2274   CPDF_FormControl* pFormControl = GetSmartFieldControl(pFormField);
2275   if (!pFormControl)
2276     return CJS_Result::Failure(JSMessage::kBadObjectError);
2277 
2278   v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
2279       CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
2280   if (pObj.IsEmpty())
2281     return CJS_Result::Failure(JSMessage::kBadObjectError);
2282 
2283   auto* pJS_Icon = static_cast<CJS_Icon*>(CFXJS_Engine::GetObjectPrivate(pObj));
2284   return pJS_Icon ? CJS_Result::Success(pJS_Icon->ToV8Object())
2285                   : CJS_Result::Failure(JSMessage::kBadObjectError);
2286 }
2287 
buttonImportIcon(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2288 CJS_Result CJS_Field::buttonImportIcon(
2289     CJS_Runtime* pRuntime,
2290     const std::vector<v8::Local<v8::Value>>& params) {
2291   return CJS_Result::Success();
2292 }
2293 
buttonSetCaption(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2294 CJS_Result CJS_Field::buttonSetCaption(
2295     CJS_Runtime* pRuntime,
2296     const std::vector<v8::Local<v8::Value>>& params) {
2297   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2298 }
2299 
buttonSetIcon(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2300 CJS_Result CJS_Field::buttonSetIcon(
2301     CJS_Runtime* pRuntime,
2302     const std::vector<v8::Local<v8::Value>>& params) {
2303   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2304 }
2305 
checkThisBox(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2306 CJS_Result CJS_Field::checkThisBox(
2307     CJS_Runtime* pRuntime,
2308     const std::vector<v8::Local<v8::Value>>& params) {
2309   int iSize = params.size();
2310   if (iSize < 1)
2311     return CJS_Result::Failure(JSMessage::kParamError);
2312 
2313   if (!m_bCanSet)
2314     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2315 
2316   int nWidget = pRuntime->ToInt32(params[0]);
2317   bool bCheckit = true;
2318   if (iSize >= 2)
2319     bCheckit = pRuntime->ToBoolean(params[1]);
2320 
2321   CPDF_FormField* pFormField = GetFirstFormField();
2322   if (!pFormField)
2323     return CJS_Result::Failure(JSMessage::kBadObjectError);
2324 
2325   if (!IsCheckBoxOrRadioButton(pFormField))
2326     return CJS_Result::Failure(JSMessage::kObjectTypeError);
2327 
2328   if (nWidget < 0 || nWidget >= pFormField->CountControls())
2329     return CJS_Result::Failure(JSMessage::kValueError);
2330 
2331   // TODO(weili): Check whether anything special needed for radio button.
2332   // (When pFormField->GetFieldType() == FormFieldType::kRadioButton.)
2333   pFormField->CheckControl(nWidget, bCheckit, NotificationOption::kNotify);
2334   UpdateFormField(m_pFormFillEnv.Get(), pFormField, true, true, true);
2335   return CJS_Result::Success();
2336 }
2337 
clearItems(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2338 CJS_Result CJS_Field::clearItems(
2339     CJS_Runtime* pRuntime,
2340     const std::vector<v8::Local<v8::Value>>& params) {
2341   return CJS_Result::Success();
2342 }
2343 
defaultIsChecked(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2344 CJS_Result CJS_Field::defaultIsChecked(
2345     CJS_Runtime* pRuntime,
2346     const std::vector<v8::Local<v8::Value>>& params) {
2347   if (!m_bCanSet)
2348     return CJS_Result::Failure(JSMessage::kReadOnlyError);
2349 
2350   int iSize = params.size();
2351   if (iSize < 1)
2352     return CJS_Result::Failure(JSMessage::kParamError);
2353 
2354   CPDF_FormField* pFormField = GetFirstFormField();
2355   if (!pFormField)
2356     return CJS_Result::Failure(JSMessage::kBadObjectError);
2357 
2358   int nWidget = pRuntime->ToInt32(params[0]);
2359   if (nWidget < 0 || nWidget >= pFormField->CountControls())
2360     return CJS_Result::Failure(JSMessage::kValueError);
2361 
2362   return CJS_Result::Success(
2363       pRuntime->NewBoolean(IsCheckBoxOrRadioButton(pFormField)));
2364 }
2365 
deleteItemAt(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2366 CJS_Result CJS_Field::deleteItemAt(
2367     CJS_Runtime* pRuntime,
2368     const std::vector<v8::Local<v8::Value>>& params) {
2369   return CJS_Result::Success();
2370 }
2371 
getArray(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2372 CJS_Result CJS_Field::getArray(
2373     CJS_Runtime* pRuntime,
2374     const std::vector<v8::Local<v8::Value>>& params) {
2375   std::vector<CPDF_FormField*> FieldArray = GetFormFields();
2376   if (FieldArray.empty())
2377     return CJS_Result::Failure(JSMessage::kBadObjectError);
2378 
2379   std::vector<std::unique_ptr<WideString>> swSort;
2380   for (CPDF_FormField* pFormField : FieldArray) {
2381     swSort.push_back(pdfium::MakeUnique<WideString>(pFormField->GetFullName()));
2382   }
2383 
2384   std::sort(swSort.begin(), swSort.end(),
2385             [](const std::unique_ptr<WideString>& p1,
2386                const std::unique_ptr<WideString>& p2) { return *p1 < *p2; });
2387 
2388   v8::Local<v8::Array> FormFieldArray = pRuntime->NewArray();
2389   int j = 0;
2390   for (const auto& pStr : swSort) {
2391     v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
2392         CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
2393     if (pObj.IsEmpty())
2394       return CJS_Result::Failure(JSMessage::kBadObjectError);
2395 
2396     auto* pJSField =
2397         static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pObj));
2398     pJSField->AttachField(m_pJSDoc.Get(), *pStr);
2399     pRuntime->PutArrayElement(FormFieldArray, j++,
2400                               pJSField
2401                                   ? v8::Local<v8::Value>(pJSField->ToV8Object())
2402                                   : v8::Local<v8::Value>());
2403   }
2404   return CJS_Result::Success(FormFieldArray);
2405 }
2406 
getItemAt(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2407 CJS_Result CJS_Field::getItemAt(
2408     CJS_Runtime* pRuntime,
2409     const std::vector<v8::Local<v8::Value>>& params) {
2410   int iSize = params.size();
2411   int nIdx = -1;
2412   if (iSize >= 1)
2413     nIdx = pRuntime->ToInt32(params[0]);
2414 
2415   bool bExport = true;
2416   if (iSize >= 2)
2417     bExport = pRuntime->ToBoolean(params[1]);
2418 
2419   CPDF_FormField* pFormField = GetFirstFormField();
2420   if (!pFormField)
2421     return CJS_Result::Failure(JSMessage::kBadObjectError);
2422 
2423   if (!IsComboBoxOrListBox(pFormField))
2424     return CJS_Result::Failure(JSMessage::kObjectTypeError);
2425 
2426   if (nIdx == -1 || nIdx > pFormField->CountOptions())
2427     nIdx = pFormField->CountOptions() - 1;
2428   if (!bExport) {
2429     return CJS_Result::Success(
2430         pRuntime->NewString(pFormField->GetOptionLabel(nIdx).AsStringView()));
2431   }
2432 
2433   WideString strval = pFormField->GetOptionValue(nIdx);
2434   if (strval.IsEmpty()) {
2435     return CJS_Result::Success(
2436         pRuntime->NewString(pFormField->GetOptionLabel(nIdx).AsStringView()));
2437   }
2438   return CJS_Result::Success(pRuntime->NewString(strval.AsStringView()));
2439 }
2440 
getLock(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2441 CJS_Result CJS_Field::getLock(CJS_Runtime* pRuntime,
2442                               const std::vector<v8::Local<v8::Value>>& params) {
2443   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2444 }
2445 
insertItemAt(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2446 CJS_Result CJS_Field::insertItemAt(
2447     CJS_Runtime* pRuntime,
2448     const std::vector<v8::Local<v8::Value>>& params) {
2449   return CJS_Result::Success();
2450 }
2451 
isBoxChecked(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2452 CJS_Result CJS_Field::isBoxChecked(
2453     CJS_Runtime* pRuntime,
2454     const std::vector<v8::Local<v8::Value>>& params) {
2455   int nIndex = -1;
2456   if (params.size() >= 1)
2457     nIndex = pRuntime->ToInt32(params[0]);
2458 
2459   CPDF_FormField* pFormField = GetFirstFormField();
2460   if (!pFormField)
2461     return CJS_Result::Failure(JSMessage::kBadObjectError);
2462 
2463   if (nIndex < 0 || nIndex >= pFormField->CountControls())
2464     return CJS_Result::Failure(JSMessage::kValueError);
2465 
2466   return CJS_Result::Success(
2467       pRuntime->NewBoolean((IsCheckBoxOrRadioButton(pFormField) &&
2468                             pFormField->GetControl(nIndex)->IsChecked() != 0)));
2469 }
2470 
isDefaultChecked(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2471 CJS_Result CJS_Field::isDefaultChecked(
2472     CJS_Runtime* pRuntime,
2473     const std::vector<v8::Local<v8::Value>>& params) {
2474   int nIndex = -1;
2475   if (params.size() >= 1)
2476     nIndex = pRuntime->ToInt32(params[0]);
2477 
2478   CPDF_FormField* pFormField = GetFirstFormField();
2479   if (!pFormField)
2480     return CJS_Result::Failure(JSMessage::kBadObjectError);
2481 
2482   if (nIndex < 0 || nIndex >= pFormField->CountControls())
2483     return CJS_Result::Failure(JSMessage::kValueError);
2484 
2485   return CJS_Result::Success(pRuntime->NewBoolean(
2486       (IsCheckBoxOrRadioButton(pFormField) &&
2487        pFormField->GetControl(nIndex)->IsDefaultChecked() != 0)));
2488 }
2489 
setAction(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2490 CJS_Result CJS_Field::setAction(
2491     CJS_Runtime* pRuntime,
2492     const std::vector<v8::Local<v8::Value>>& params) {
2493   return CJS_Result::Success();
2494 }
2495 
setFocus(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2496 CJS_Result CJS_Field::setFocus(
2497     CJS_Runtime* pRuntime,
2498     const std::vector<v8::Local<v8::Value>>& params) {
2499   CPDF_FormField* pFormField = GetFirstFormField();
2500   if (!pFormField)
2501     return CJS_Result::Failure(JSMessage::kBadObjectError);
2502 
2503   int32_t nCount = pFormField->CountControls();
2504   if (nCount < 1)
2505     return CJS_Result::Failure(JSMessage::kBadObjectError);
2506 
2507   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
2508   CPDFSDK_Widget* pWidget = nullptr;
2509   if (nCount == 1) {
2510     pWidget = pForm->GetWidget(pFormField->GetControl(0));
2511   } else {
2512     IPDF_Page* pPage = IPDFPageFromFPDFPage(m_pFormFillEnv->GetCurrentPage());
2513     if (!pPage)
2514       return CJS_Result::Failure(JSMessage::kBadObjectError);
2515     if (CPDFSDK_PageView* pCurPageView =
2516             m_pFormFillEnv->GetPageView(pPage, true)) {
2517       for (int32_t i = 0; i < nCount; i++) {
2518         if (CPDFSDK_Widget* pTempWidget =
2519                 pForm->GetWidget(pFormField->GetControl(i))) {
2520           if (pTempWidget->GetPDFPage() == pCurPageView->GetPDFPage()) {
2521             pWidget = pTempWidget;
2522             break;
2523           }
2524         }
2525       }
2526     }
2527   }
2528 
2529   if (pWidget) {
2530     ObservedPtr<CPDFSDK_Annot> pObserved(pWidget);
2531     m_pFormFillEnv->SetFocusAnnot(&pObserved);
2532   }
2533 
2534   return CJS_Result::Success();
2535 }
2536 
setItems(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2537 CJS_Result CJS_Field::setItems(
2538     CJS_Runtime* pRuntime,
2539     const std::vector<v8::Local<v8::Value>>& params) {
2540   return CJS_Result::Success();
2541 }
2542 
setLock(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2543 CJS_Result CJS_Field::setLock(CJS_Runtime* pRuntime,
2544                               const std::vector<v8::Local<v8::Value>>& params) {
2545   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2546 }
2547 
signatureGetModifications(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2548 CJS_Result CJS_Field::signatureGetModifications(
2549     CJS_Runtime* pRuntime,
2550     const std::vector<v8::Local<v8::Value>>& params) {
2551   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2552 }
2553 
signatureGetSeedValue(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2554 CJS_Result CJS_Field::signatureGetSeedValue(
2555     CJS_Runtime* pRuntime,
2556     const std::vector<v8::Local<v8::Value>>& params) {
2557   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2558 }
2559 
signatureInfo(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2560 CJS_Result CJS_Field::signatureInfo(
2561     CJS_Runtime* pRuntime,
2562     const std::vector<v8::Local<v8::Value>>& params) {
2563   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2564 }
2565 
signatureSetSeedValue(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2566 CJS_Result CJS_Field::signatureSetSeedValue(
2567     CJS_Runtime* pRuntime,
2568     const std::vector<v8::Local<v8::Value>>& params) {
2569   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2570 }
2571 
signatureSign(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2572 CJS_Result CJS_Field::signatureSign(
2573     CJS_Runtime* pRuntime,
2574     const std::vector<v8::Local<v8::Value>>& params) {
2575   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2576 }
2577 
signatureValidate(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)2578 CJS_Result CJS_Field::signatureValidate(
2579     CJS_Runtime* pRuntime,
2580     const std::vector<v8::Local<v8::Value>>& params) {
2581   return CJS_Result::Failure(JSMessage::kNotSupportedError);
2582 }
2583 
AddDelay_Int(FIELD_PROP prop,int32_t n)2584 void CJS_Field::AddDelay_Int(FIELD_PROP prop, int32_t n) {
2585   auto pNewData =
2586       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2587   pNewData->num = n;
2588   m_pJSDoc->AddDelayData(std::move(pNewData));
2589 }
2590 
AddDelay_Bool(FIELD_PROP prop,bool b)2591 void CJS_Field::AddDelay_Bool(FIELD_PROP prop, bool b) {
2592   auto pNewData =
2593       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2594   pNewData->b = b;
2595   m_pJSDoc->AddDelayData(std::move(pNewData));
2596 }
2597 
AddDelay_String(FIELD_PROP prop,const ByteString & str)2598 void CJS_Field::AddDelay_String(FIELD_PROP prop, const ByteString& str) {
2599   auto pNewData =
2600       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2601   pNewData->bytestring = str;
2602   m_pJSDoc->AddDelayData(std::move(pNewData));
2603 }
2604 
AddDelay_Rect(FIELD_PROP prop,const CFX_FloatRect & rect)2605 void CJS_Field::AddDelay_Rect(FIELD_PROP prop, const CFX_FloatRect& rect) {
2606   auto pNewData =
2607       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2608   pNewData->rect = rect;
2609   m_pJSDoc->AddDelayData(std::move(pNewData));
2610 }
2611 
AddDelay_WordArray(FIELD_PROP prop,const std::vector<uint32_t> & array)2612 void CJS_Field::AddDelay_WordArray(FIELD_PROP prop,
2613                                    const std::vector<uint32_t>& array) {
2614   auto pNewData =
2615       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2616   pNewData->wordarray = array;
2617   m_pJSDoc->AddDelayData(std::move(pNewData));
2618 }
2619 
AddDelay_WideStringArray(FIELD_PROP prop,const std::vector<WideString> & array)2620 void CJS_Field::AddDelay_WideStringArray(FIELD_PROP prop,
2621                                          const std::vector<WideString>& array) {
2622   auto pNewData =
2623       pdfium::MakeUnique<CJS_DelayData>(prop, m_nFormControlIndex, m_FieldName);
2624   pNewData->widestringarray = array;
2625   m_pJSDoc->AddDelayData(std::move(pNewData));
2626 }
2627 
DoDelay(CPDFSDK_FormFillEnvironment * pFormFillEnv,CJS_DelayData * pData)2628 void CJS_Field::DoDelay(CPDFSDK_FormFillEnvironment* pFormFillEnv,
2629                         CJS_DelayData* pData) {
2630   ASSERT(pFormFillEnv);
2631   switch (pData->eProp) {
2632     case FP_BORDERSTYLE:
2633       SetBorderStyle(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2634                      pData->bytestring);
2635       break;
2636     case FP_CURRENTVALUEINDICES:
2637       SetCurrentValueIndices(pFormFillEnv, pData->sFieldName,
2638                              pData->nControlIndex, pData->wordarray);
2639       break;
2640     case FP_DISPLAY:
2641       SetDisplay(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2642                  pData->num);
2643       break;
2644     case FP_HIDDEN:
2645       SetHidden(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2646                 pData->b);
2647       break;
2648     case FP_LINEWIDTH:
2649       SetLineWidth(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2650                    pData->num);
2651       break;
2652     case FP_RECT:
2653       SetRect(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2654               pData->rect);
2655       break;
2656     case FP_VALUE:
2657       SetFieldValue(pFormFillEnv, pData->sFieldName, pData->nControlIndex,
2658                     pData->widestringarray);
2659       break;
2660     default:
2661       NOTREACHED();
2662   }
2663 }
2664