Remove some abstractions in fxjs_v8.h.
[pdfium.git] / fpdfsdk / src / fsdk_actionhandler.cpp
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 "../include/fsdk_define.h"
8 #include "../include/fsdk_mgr.h"
9 #include "../include/fsdk_actionhandler.h"
10 #include "../include/javascript/IJavaScript.h"
11
12 CPDFSDK_ActionHandler::CPDFSDK_ActionHandler()
13     : m_pFormActionHandler(new CPDFSDK_FormActionHandler) {
14 }
15
16 FX_BOOL CPDFSDK_ActionHandler::DoAction_DocOpen(const CPDF_Action& action,
17                                                 CPDFSDK_Document* pDocument) {
18   CFX_PtrList list;
19   return ExecuteDocumentOpenAction(action, pDocument, list);
20 }
21
22 FX_BOOL CPDFSDK_ActionHandler::DoAction_JavaScript(
23     const CPDF_Action& JsAction,
24     CFX_WideString csJSName,
25     CPDFSDK_Document* pDocument) {
26   if (JsAction.GetType() == CPDF_Action::JavaScript) {
27     CFX_WideString swJS = JsAction.GetJavaScript();
28     if (!swJS.IsEmpty()) {
29       RunDocumentOpenJavaScript(pDocument, csJSName, swJS);
30       return TRUE;
31     }
32   }
33
34   return FALSE;
35 }
36
37 FX_BOOL CPDFSDK_ActionHandler::DoAction_FieldJavaScript(
38     const CPDF_Action& JsAction,
39     CPDF_AAction::AActionType type,
40     CPDFSDK_Document* pDocument,
41     CPDF_FormField* pFormField,
42     PDFSDK_FieldAction& data) {
43   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
44   ASSERT(pEnv);
45   if (pEnv->IsJSInitiated() && JsAction.GetType() == CPDF_Action::JavaScript) {
46     CFX_WideString swJS = JsAction.GetJavaScript();
47     if (!swJS.IsEmpty()) {
48       RunFieldJavaScript(pDocument, pFormField, type, data, swJS);
49       return TRUE;
50     }
51   }
52   return FALSE;
53 }
54
55 FX_BOOL CPDFSDK_ActionHandler::DoAction_Page(
56     const CPDF_Action& action,
57     enum CPDF_AAction::AActionType eType,
58     CPDFSDK_Document* pDocument) {
59   CFX_PtrList list;
60   return ExecuteDocumentPageAction(action, eType, pDocument, list);
61 }
62
63 FX_BOOL CPDFSDK_ActionHandler::DoAction_Document(
64     const CPDF_Action& action,
65     enum CPDF_AAction::AActionType eType,
66     CPDFSDK_Document* pDocument) {
67   CFX_PtrList list;
68   return ExecuteDocumentPageAction(action, eType, pDocument, list);
69 }
70
71 FX_BOOL CPDFSDK_ActionHandler::DoAction_BookMark(CPDF_Bookmark* pBookMark,
72                                                  const CPDF_Action& action,
73                                                  CPDF_AAction::AActionType type,
74                                                  CPDFSDK_Document* pDocument) {
75   CFX_PtrList list;
76   return ExecuteBookMark(action, pDocument, pBookMark, list);
77 }
78
79 FX_BOOL CPDFSDK_ActionHandler::DoAction_Screen(const CPDF_Action& action,
80                                                CPDF_AAction::AActionType type,
81                                                CPDFSDK_Document* pDocument,
82                                                CPDFSDK_Annot* pScreen) {
83   CFX_PtrList list;
84   return ExecuteScreenAction(action, type, pDocument, pScreen, list);
85 }
86
87 FX_BOOL CPDFSDK_ActionHandler::DoAction_Link(const CPDF_Action& action,
88                                              CPDFSDK_Document* pDocument) {
89   CFX_PtrList list;
90   return ExecuteLinkAction(action, pDocument, list);
91 }
92
93 FX_BOOL CPDFSDK_ActionHandler::DoAction_Field(const CPDF_Action& action,
94                                               CPDF_AAction::AActionType type,
95                                               CPDFSDK_Document* pDocument,
96                                               CPDF_FormField* pFormField,
97                                               PDFSDK_FieldAction& data) {
98   CFX_PtrList list;
99   return ExecuteFieldAction(action, type, pDocument, pFormField, data, list);
100 }
101
102 FX_BOOL CPDFSDK_ActionHandler::ExecuteDocumentOpenAction(
103     const CPDF_Action& action,
104     CPDFSDK_Document* pDocument,
105     CFX_PtrList& list) {
106   CPDF_Dictionary* pDict = action.GetDict();
107   if (list.Find(pDict))
108     return FALSE;
109
110   list.AddTail(pDict);
111
112   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
113   ASSERT(pEnv);
114   if (action.GetType() == CPDF_Action::JavaScript) {
115     if (pEnv->IsJSInitiated()) {
116       CFX_WideString swJS = action.GetJavaScript();
117       if (!swJS.IsEmpty()) {
118         RunDocumentOpenJavaScript(pDocument, L"", swJS);
119       }
120     }
121   } else {
122     DoAction_NoJs(action, pDocument);
123   }
124
125   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
126     CPDF_Action subaction = action.GetSubAction(i);
127     if (!ExecuteDocumentOpenAction(subaction, pDocument, list))
128       return FALSE;
129   }
130
131   return TRUE;
132 }
133
134 FX_BOOL CPDFSDK_ActionHandler::ExecuteLinkAction(const CPDF_Action& action,
135                                                  CPDFSDK_Document* pDocument,
136                                                  CFX_PtrList& list) {
137   ASSERT(pDocument != NULL);
138
139   CPDF_Dictionary* pDict = action.GetDict();
140   if (list.Find(pDict))
141     return FALSE;
142
143   list.AddTail(pDict);
144
145   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
146   ASSERT(pEnv);
147   if (action.GetType() == CPDF_Action::JavaScript) {
148     if (pEnv->IsJSInitiated()) {
149       CFX_WideString swJS = action.GetJavaScript();
150       if (!swJS.IsEmpty()) {
151         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();  //????
152         ASSERT(pRuntime != NULL);
153
154         pRuntime->SetReaderDocument(pDocument);
155
156         IFXJS_Context* pContext = pRuntime->NewContext();
157         ASSERT(pContext != NULL);
158
159         pContext->OnLink_MouseUp(pDocument);
160
161         CFX_WideString csInfo;
162         FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
163         if (!bRet) {
164           // FIXME: return error.
165         }
166
167         pRuntime->ReleaseContext(pContext);
168       }
169     }
170   } else {
171     DoAction_NoJs(action, pDocument);
172   }
173
174   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
175     CPDF_Action subaction = action.GetSubAction(i);
176     if (!ExecuteLinkAction(subaction, pDocument, list))
177       return FALSE;
178   }
179
180   return TRUE;
181 }
182
183 FX_BOOL CPDFSDK_ActionHandler::ExecuteDocumentPageAction(
184     const CPDF_Action& action,
185     CPDF_AAction::AActionType type,
186     CPDFSDK_Document* pDocument,
187     CFX_PtrList& list) {
188   ASSERT(pDocument != NULL);
189
190   CPDF_Dictionary* pDict = action.GetDict();
191   if (list.Find(pDict))
192     return FALSE;
193
194   list.AddTail(pDict);
195
196   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
197   ASSERT(pEnv);
198   if (action.GetType() == CPDF_Action::JavaScript) {
199     if (pEnv->IsJSInitiated()) {
200       CFX_WideString swJS = action.GetJavaScript();
201       if (!swJS.IsEmpty()) {
202         RunDocumentPageJavaScript(pDocument, type, swJS);
203       }
204     }
205   } else {
206     DoAction_NoJs(action, pDocument);
207   }
208
209   if (!IsValidDocView(pDocument))
210     return FALSE;
211
212   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
213     CPDF_Action subaction = action.GetSubAction(i);
214     if (!ExecuteDocumentPageAction(subaction, type, pDocument, list))
215       return FALSE;
216   }
217
218   return TRUE;
219 }
220
221 FX_BOOL CPDFSDK_ActionHandler::IsValidField(CPDFSDK_Document* pDocument,
222                                             CPDF_Dictionary* pFieldDict) {
223   ASSERT(pDocument != NULL);
224   ASSERT(pFieldDict != NULL);
225
226   CPDFSDK_InterForm* pInterForm = pDocument->GetInterForm();
227   ASSERT(pInterForm != NULL);
228
229   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
230   ASSERT(pPDFInterForm != NULL);
231
232   return pPDFInterForm->GetFieldByDict(pFieldDict) != NULL;
233 }
234
235 FX_BOOL CPDFSDK_ActionHandler::ExecuteFieldAction(
236     const CPDF_Action& action,
237     CPDF_AAction::AActionType type,
238     CPDFSDK_Document* pDocument,
239     CPDF_FormField* pFormField,
240     PDFSDK_FieldAction& data,
241     CFX_PtrList& list) {
242   ASSERT(pDocument != NULL);
243
244   CPDF_Dictionary* pDict = action.GetDict();
245   if (list.Find(pDict))
246     return FALSE;
247
248   list.AddTail(pDict);
249
250   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
251   ASSERT(pEnv);
252   if (action.GetType() == CPDF_Action::JavaScript) {
253     if (pEnv->IsJSInitiated()) {
254       CFX_WideString swJS = action.GetJavaScript();
255       if (!swJS.IsEmpty()) {
256         RunFieldJavaScript(pDocument, pFormField, type, data, swJS);
257         if (!IsValidField(pDocument, pFormField->GetFieldDict()))
258           return FALSE;
259       }
260     }
261   } else {
262     DoAction_NoJs(action, pDocument);
263   }
264
265   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
266     CPDF_Action subaction = action.GetSubAction(i);
267     if (!ExecuteFieldAction(subaction, type, pDocument, pFormField, data, list))
268       return FALSE;
269   }
270
271   return TRUE;
272 }
273
274 FX_BOOL CPDFSDK_ActionHandler::ExecuteScreenAction(
275     const CPDF_Action& action,
276     CPDF_AAction::AActionType type,
277     CPDFSDK_Document* pDocument,
278     CPDFSDK_Annot* pScreen,
279     CFX_PtrList& list) {
280   ASSERT(pDocument != NULL);
281
282   CPDF_Dictionary* pDict = action.GetDict();
283   if (list.Find(pDict))
284     return FALSE;
285
286   list.AddTail(pDict);
287
288   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
289   ASSERT(pEnv);
290   if (action.GetType() == CPDF_Action::JavaScript) {
291     if (pEnv->IsJSInitiated()) {
292       CFX_WideString swJS = action.GetJavaScript();
293       if (!swJS.IsEmpty()) {
294         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
295         ASSERT(pRuntime != NULL);
296
297         pRuntime->SetReaderDocument(pDocument);
298
299         IFXJS_Context* pContext = pRuntime->NewContext();
300         ASSERT(pContext != NULL);
301
302         //                      switch (type)
303         //                      {
304         //                      case CPDF_AAction::CursorEnter:
305         //                              pContext->OnScreen_MouseEnter(IsCTRLpressed(),
306         // IsSHIFTpressed(), pScreen);
307         //                              break;
308         //                      case CPDF_AAction::CursorExit:
309         //                              pContext->OnScreen_MouseExit(IsCTRLpressed(),
310         // IsSHIFTpressed(), pScreen);
311         //                              break;
312         //                      case CPDF_AAction::ButtonDown:
313         //                              pContext->OnScreen_MouseDown(IsCTRLpressed(),
314         // IsSHIFTpressed(), pScreen);
315         //                              break;
316         //                      case CPDF_AAction::ButtonUp:
317         //                              pContext->OnScreen_MouseUp(IsCTRLpressed(),
318         // IsSHIFTpressed(), pScreen);
319         //                              break;
320         //                      case CPDF_AAction::GetFocus:
321         //                              pContext->OnScreen_Focus(IsCTRLpressed(),
322         // IsSHIFTpressed(), pScreen);
323         //                              break;
324         //                      case CPDF_AAction::LoseFocus:
325         //                              pContext->OnScreen_Blur(IsCTRLpressed(),
326         // IsSHIFTpressed(), pScreen);
327         //                              break;
328         //                      case CPDF_AAction::PageOpen:
329         //                              pContext->OnScreen_Open(IsCTRLpressed(),
330         // IsSHIFTpressed(), pScreen);
331         //                              break;
332         //                      case CPDF_AAction::PageClose:
333         //                              pContext->OnScreen_Close(IsCTRLpressed(),
334         // IsSHIFTpressed(), pScreen);
335         //                              break;
336         //                      case CPDF_AAction::PageVisible:
337         //                              pContext->OnScreen_InView(IsCTRLpressed(),
338         // IsSHIFTpressed(), pScreen);
339         //                              break;
340         //                      case CPDF_AAction::PageInvisible:
341         //                              pContext->OnScreen_OutView(IsCTRLpressed(),
342         // IsSHIFTpressed(), pScreen);
343         //                              break;
344         //                      default:
345         //                              ASSERT(FALSE);
346         //                              break;
347         //                      }
348
349         CFX_WideString csInfo;
350         FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
351         if (!bRet) {
352           // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(),
353           // csInfo);
354         }
355
356         pRuntime->ReleaseContext(pContext);
357       }
358     }
359   } else {
360     DoAction_NoJs(action, pDocument);
361   }
362
363   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
364     CPDF_Action subaction = action.GetSubAction(i);
365     if (!ExecuteScreenAction(subaction, type, pDocument, pScreen, list))
366       return FALSE;
367   }
368
369   return TRUE;
370 }
371
372 FX_BOOL CPDFSDK_ActionHandler::ExecuteBookMark(const CPDF_Action& action,
373                                                CPDFSDK_Document* pDocument,
374                                                CPDF_Bookmark* pBookmark,
375                                                CFX_PtrList& list) {
376   ASSERT(pDocument != NULL);
377
378   CPDF_Dictionary* pDict = action.GetDict();
379   if (list.Find(pDict))
380     return FALSE;
381
382   list.AddTail(pDict);
383
384   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
385   ASSERT(pEnv);
386   if (action.GetType() == CPDF_Action::JavaScript) {
387     if (pEnv->IsJSInitiated()) {
388       CFX_WideString swJS = action.GetJavaScript();
389       if (!swJS.IsEmpty()) {
390         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
391         ASSERT(pRuntime != NULL);
392
393         pRuntime->SetReaderDocument(pDocument);
394
395         IFXJS_Context* pContext = pRuntime->NewContext();
396         ASSERT(pContext != NULL);
397
398         pContext->OnBookmark_MouseUp(pBookmark);
399
400         CFX_WideString csInfo;
401         FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
402         if (!bRet) {
403           // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(),
404           // csInfo);
405         }
406
407         pRuntime->ReleaseContext(pContext);
408       }
409     }
410   } else {
411     DoAction_NoJs(action, pDocument);
412   }
413
414   for (int32_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) {
415     CPDF_Action subaction = action.GetSubAction(i);
416     if (!ExecuteBookMark(subaction, pDocument, pBookmark, list))
417       return FALSE;
418   }
419
420   return TRUE;
421 }
422
423 void CPDFSDK_ActionHandler::DoAction_NoJs(const CPDF_Action& action,
424                                           CPDFSDK_Document* pDocument) {
425   ASSERT(pDocument != NULL);
426
427   switch (action.GetType()) {
428     case CPDF_Action::GoTo:
429       DoAction_GoTo(pDocument, action);
430       break;
431     case CPDF_Action::GoToR:
432       DoAction_GoToR(pDocument, action);
433       break;
434     case CPDF_Action::GoToE:
435       break;
436     case CPDF_Action::Launch:
437       DoAction_Launch(pDocument, action);
438       break;
439     case CPDF_Action::Thread:
440       break;
441     case CPDF_Action::URI:
442       DoAction_URI(pDocument, action);
443       break;
444     case CPDF_Action::Sound:
445       break;
446     case CPDF_Action::Movie:
447       break;
448     case CPDF_Action::Hide:
449       if (m_pFormActionHandler) {
450         m_pFormActionHandler->DoAction_Hide(action, pDocument);
451       }
452       break;
453     case CPDF_Action::Named:
454       DoAction_Named(pDocument, action);
455       break;
456     case CPDF_Action::SubmitForm:
457       if (m_pFormActionHandler) {
458         m_pFormActionHandler->DoAction_SubmitForm(action, pDocument);
459       }
460       break;
461     case CPDF_Action::ResetForm:
462       if (m_pFormActionHandler) {
463         m_pFormActionHandler->DoAction_ResetForm(action, pDocument);
464       }
465       break;
466     case CPDF_Action::ImportData:
467       if (m_pFormActionHandler) {
468         m_pFormActionHandler->DoAction_ImportData(action, pDocument);
469       }
470       break;
471     case CPDF_Action::JavaScript:
472       ASSERT(FALSE);
473       break;
474     case CPDF_Action::SetOCGState:
475       DoAction_SetOCGState(pDocument, action);
476       break;
477     case CPDF_Action::Rendition:
478       break;
479     case CPDF_Action::Trans:
480       break;
481     case CPDF_Action::GoTo3DView:
482       break;
483     default:
484       break;
485   }
486 }
487
488 FX_BOOL CPDFSDK_ActionHandler::IsValidDocView(CPDFSDK_Document* pDocument) {
489   ASSERT(pDocument != NULL);
490   return TRUE;
491 }
492
493 void CPDFSDK_ActionHandler::DoAction_GoTo(CPDFSDK_Document* pDocument,
494                                           const CPDF_Action& action) {
495   ASSERT(action);
496
497   CPDF_Document* pPDFDocument = pDocument->GetDocument();
498   ASSERT(pPDFDocument != NULL);
499   CPDFDoc_Environment* pApp = pDocument->GetEnv();
500   ASSERT(pApp != NULL);
501
502   CPDF_Dest MyDest = action.GetDest(pPDFDocument);
503   int nPageIndex = MyDest.GetPageIndex(pPDFDocument);
504   int nFitType = MyDest.GetZoomMode();
505   const CPDF_Array* pMyArray = (CPDF_Array*)MyDest.GetObject();
506   float* pPosAry = NULL;
507   int sizeOfAry = 0;
508   if (pMyArray != NULL) {
509     pPosAry = new float[pMyArray->GetCount()];
510     int j = 0;
511     for (int i = 2; i < (int)pMyArray->GetCount(); i++) {
512       pPosAry[j++] = pMyArray->GetFloat(i);
513     }
514     sizeOfAry = j;
515   }
516   pApp->FFI_DoGoToAction(nPageIndex, nFitType, pPosAry, sizeOfAry);
517   delete[] pPosAry;
518 }
519
520 void CPDFSDK_ActionHandler::DoAction_GoToR(CPDFSDK_Document* pDocument,
521                                            const CPDF_Action& action) {}
522
523 void CPDFSDK_ActionHandler::DoAction_Launch(CPDFSDK_Document* pDocument,
524                                             const CPDF_Action& action) {}
525
526 void CPDFSDK_ActionHandler::DoAction_URI(CPDFSDK_Document* pDocument,
527                                          const CPDF_Action& action) {
528   ASSERT(action);
529
530   CPDFDoc_Environment* pApp = pDocument->GetEnv();
531   ASSERT(pApp != NULL);
532
533   CFX_ByteString sURI = action.GetURI(pDocument->GetDocument());
534   pApp->FFI_DoURIAction(sURI.c_str());
535 }
536
537 void CPDFSDK_ActionHandler::DoAction_Named(CPDFSDK_Document* pDocument,
538                                            const CPDF_Action& action) {
539   ASSERT(action);
540
541   CFX_ByteString csName = action.GetNamedAction();
542   pDocument->GetEnv()->FFI_ExecuteNamedAction(csName);
543 }
544
545 void CPDFSDK_ActionHandler::DoAction_SetOCGState(CPDFSDK_Document* pDocument,
546                                                  const CPDF_Action& action) {}
547
548 void CPDFSDK_ActionHandler::RunFieldJavaScript(CPDFSDK_Document* pDocument,
549                                                CPDF_FormField* pFormField,
550                                                CPDF_AAction::AActionType type,
551                                                PDFSDK_FieldAction& data,
552                                                const CFX_WideString& script) {
553   ASSERT(type != CPDF_AAction::Calculate);
554   ASSERT(type != CPDF_AAction::Format);
555
556   ASSERT(pDocument != NULL);
557
558   IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
559   ASSERT(pRuntime != NULL);
560
561   pRuntime->SetReaderDocument(pDocument);
562
563   IFXJS_Context* pContext = pRuntime->NewContext();
564   ASSERT(pContext != NULL);
565
566   switch (type) {
567     case CPDF_AAction::CursorEnter:
568       pContext->OnField_MouseEnter(data.bModifier, data.bShift, pFormField);
569       break;
570     case CPDF_AAction::CursorExit:
571       pContext->OnField_MouseExit(data.bModifier, data.bShift, pFormField);
572       break;
573     case CPDF_AAction::ButtonDown:
574       pContext->OnField_MouseDown(data.bModifier, data.bShift, pFormField);
575       break;
576     case CPDF_AAction::ButtonUp:
577       pContext->OnField_MouseUp(data.bModifier, data.bShift, pFormField);
578       break;
579     case CPDF_AAction::GetFocus:
580       pContext->OnField_Focus(data.bModifier, data.bShift, pFormField,
581                               data.sValue);
582       break;
583     case CPDF_AAction::LoseFocus:
584       pContext->OnField_Blur(data.bModifier, data.bShift, pFormField,
585                              data.sValue);
586       break;
587     case CPDF_AAction::KeyStroke:
588       pContext->OnField_Keystroke(data.sChange, data.sChangeEx, data.bKeyDown,
589                                   data.bModifier, data.nSelEnd, data.nSelStart,
590                                   data.bShift, pFormField, data.sValue,
591                                   data.bWillCommit, data.bFieldFull, data.bRC);
592       break;
593     case CPDF_AAction::Validate:
594       pContext->OnField_Validate(data.sChange, data.sChangeEx, data.bKeyDown,
595                                  data.bModifier, data.bShift, pFormField,
596                                  data.sValue, data.bRC);
597       break;
598     default:
599       ASSERT(FALSE);
600       break;
601   }
602
603   CFX_WideString csInfo;
604   FX_BOOL bRet = pContext->RunScript(script, csInfo);
605   if (!bRet) {
606     // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
607   }
608
609   pRuntime->ReleaseContext(pContext);
610 }
611
612 void CPDFSDK_ActionHandler::RunDocumentOpenJavaScript(
613     CPDFSDK_Document* pDocument,
614     const CFX_WideString& sScriptName,
615     const CFX_WideString& script) {
616   ASSERT(pDocument != NULL);
617
618   IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
619   ASSERT(pRuntime != NULL);
620
621   pRuntime->SetReaderDocument(pDocument);
622
623   IFXJS_Context* pContext = pRuntime->NewContext();
624   ASSERT(pContext != NULL);
625
626   pContext->OnDoc_Open(pDocument, sScriptName);
627
628   CFX_WideString csInfo;
629   FX_BOOL bRet = pContext->RunScript(script, csInfo);
630   if (!bRet) {
631     // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
632   }
633
634   pRuntime->ReleaseContext(pContext);
635 }
636
637 void CPDFSDK_ActionHandler::RunDocumentPageJavaScript(
638     CPDFSDK_Document* pDocument,
639     CPDF_AAction::AActionType type,
640     const CFX_WideString& script) {
641   ASSERT(pDocument != NULL);
642
643   IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
644   ASSERT(pRuntime != NULL);
645
646   pRuntime->SetReaderDocument(pDocument);
647
648   IFXJS_Context* pContext = pRuntime->NewContext();
649   ASSERT(pContext != NULL);
650
651   switch (type) {
652     case CPDF_AAction::OpenPage:
653       pContext->OnPage_Open(pDocument);
654       break;
655     case CPDF_AAction::ClosePage:
656       pContext->OnPage_Close(pDocument);
657       break;
658     case CPDF_AAction::CloseDocument:
659       pContext->OnDoc_WillClose(pDocument);
660       break;
661     case CPDF_AAction::SaveDocument:
662       pContext->OnDoc_WillSave(pDocument);
663       break;
664     case CPDF_AAction::DocumentSaved:
665       pContext->OnDoc_DidSave(pDocument);
666       break;
667     case CPDF_AAction::PrintDocument:
668       pContext->OnDoc_WillPrint(pDocument);
669       break;
670     case CPDF_AAction::DocumentPrinted:
671       pContext->OnDoc_DidPrint(pDocument);
672       break;
673     case CPDF_AAction::PageVisible:
674       pContext->OnPage_InView(pDocument);
675       break;
676     case CPDF_AAction::PageInvisible:
677       pContext->OnPage_OutView(pDocument);
678       break;
679     default:
680       ASSERT(FALSE);
681       break;
682   }
683
684   CFX_WideString csInfo;
685   FX_BOOL bRet = pContext->RunScript(script, csInfo);
686   if (!bRet) {
687     // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
688   }
689
690   pRuntime->ReleaseContext(pContext);
691 }
692
693 FX_BOOL CPDFSDK_FormActionHandler::DoAction_Hide(const CPDF_Action& action,
694                                                  CPDFSDK_Document* pDocument) {
695   ASSERT(pDocument != NULL);
696
697   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
698   ASSERT(pInterForm != NULL);
699
700   if (pInterForm->DoAction_Hide(action)) {
701     pDocument->SetChangeMark();
702     return TRUE;
703   }
704
705   return FALSE;
706 }
707
708 FX_BOOL CPDFSDK_FormActionHandler::DoAction_SubmitForm(
709     const CPDF_Action& action,
710     CPDFSDK_Document* pDocument) {
711   ASSERT(pDocument != NULL);
712
713   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
714   ASSERT(pInterForm != NULL);
715
716   return pInterForm->DoAction_SubmitForm(action);
717 }
718
719 FX_BOOL CPDFSDK_FormActionHandler::DoAction_ResetForm(
720     const CPDF_Action& action,
721     CPDFSDK_Document* pDocument) {
722   ASSERT(pDocument != NULL);
723
724   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
725   ASSERT(pInterForm != NULL);
726
727   if (pInterForm->DoAction_ResetForm(action)) {
728     return TRUE;
729   }
730
731   return FALSE;
732 }
733
734 FX_BOOL CPDFSDK_FormActionHandler::DoAction_ImportData(
735     const CPDF_Action& action,
736     CPDFSDK_Document* pDocument) {
737   ASSERT(pDocument != NULL);
738
739   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
740   ASSERT(pInterForm != NULL);
741
742   if (pInterForm->DoAction_ImportData(action)) {
743     pDocument->SetChangeMark();
744     return TRUE;
745   }
746
747   return FALSE;
748 }