blob: 2b00cbc10e326389e026469da597e34e8b8b120f [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/event.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Dan Sinclairf766ad22016-03-14 13:51:24 -04009#include "fpdfsdk/javascript/Field.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040010#include "fpdfsdk/javascript/JS_Define.h"
11#include "fpdfsdk/javascript/JS_EventHandler.h"
12#include "fpdfsdk/javascript/JS_Object.h"
13#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080014#include "fpdfsdk/javascript/cjs_event_context.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Tom Sepez04557b82017-02-16 09:43:10 -080016JSConstSpec CJS_Event::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Tom Sepez04557b82017-02-16 09:43:10 -080018JSPropertySpec CJS_Event::PropertySpecs[] = {
Tom Sepez4d5b8c52017-02-21 15:17:07 -080019 {"change", get_change_static, set_change_static},
20 {"changeEx", get_changeEx_static, set_changeEx_static},
21 {"commitKey", get_commitKey_static, set_commitKey_static},
22 {"fieldFull", get_fieldFull_static, set_fieldFull_static},
23 {"keyDown", get_keyDown_static, set_keyDown_static},
24 {"modifier", get_modifier_static, set_modifier_static},
25 {"name", get_name_static, set_name_static},
26 {"rc", get_rc_static, set_rc_static},
27 {"richChange", get_richChange_static, set_richChange_static},
28 {"richChangeEx", get_richChangeEx_static, set_richChangeEx_static},
29 {"richValue", get_richValue_static, set_richValue_static},
30 {"selEnd", get_selEnd_static, set_selEnd_static},
31 {"selStart", get_selStart_static, set_selStart_static},
32 {"shift", get_shift_static, set_shift_static},
33 {"source", get_source_static, set_source_static},
34 {"target", get_target_static, set_target_static},
35 {"targetName", get_targetName_static, set_targetName_static},
36 {"type", get_type_static, set_type_static},
37 {"value", get_value_static, set_value_static},
38 {"willCommit", get_willCommit_static, set_willCommit_static},
Tom Sepez04557b82017-02-16 09:43:10 -080039 {0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepez04557b82017-02-16 09:43:10 -080041JSMethodSpec CJS_Event::MethodSpecs[] = {{0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043IMPLEMENT_JS_CLASS(CJS_Event, event)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {}
46
Dan Sinclairf766ad22016-03-14 13:51:24 -040047event::~event() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048
Tom Sepezb1670b52017-02-16 17:01:00 -080049bool event::change(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -080050 CJS_PropValue& vp,
51 CFX_WideString& sError) {
52 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -080053 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 CFX_WideString& wChange = pEvent->Change();
55 if (vp.IsSetting()) {
tsepezf3dc8c62016-08-10 06:29:29 -070056 if (vp.GetJSValue()->GetType() == CJS_Value::VT_string)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 vp >> wChange;
Tom Sepezd6ae2af2017-02-16 11:49:55 -080058 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 }
Tom Sepezd6ae2af2017-02-16 11:49:55 -080060 vp << wChange;
tsepez4cf55152016-11-02 14:37:54 -070061 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Tom Sepezb1670b52017-02-16 17:01:00 -080064bool event::changeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -070065 CJS_PropValue& vp,
66 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -070068 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069
Tom Sepezd6ae2af2017-02-16 11:49:55 -080070 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -080071 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072
73 vp << pEvent->ChangeEx();
tsepez4cf55152016-11-02 14:37:54 -070074 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
Tom Sepezb1670b52017-02-16 17:01:00 -080077bool event::commitKey(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -070078 CJS_PropValue& vp,
79 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -070081 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Tom Sepezd6ae2af2017-02-16 11:49:55 -080083 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -080084 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085
86 vp << pEvent->CommitKey();
tsepez4cf55152016-11-02 14:37:54 -070087 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Tom Sepezb1670b52017-02-16 17:01:00 -080090bool event::fieldFull(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 CJS_PropValue& vp,
92 CFX_WideString& sError) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -080093 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -080094 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095
tsepez4cf55152016-11-02 14:37:54 -070096 if (!vp.IsGetting() &&
97 wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
98 return false;
99
Tom Sepezb1670b52017-02-16 17:01:00 -0800100 vp << pEvent->FieldFull();
tsepez4cf55152016-11-02 14:37:54 -0700101 return true;
102}
103
Tom Sepezb1670b52017-02-16 17:01:00 -0800104bool event::keyDown(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700105 CJS_PropValue& vp,
106 CFX_WideString& sError) {
107 if (!vp.IsGetting())
108 return false;
109
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800110 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800111 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepez4cf55152016-11-02 14:37:54 -0700112
Tom Sepezb1670b52017-02-16 17:01:00 -0800113 vp << pEvent->KeyDown();
tsepez4cf55152016-11-02 14:37:54 -0700114 return true;
115}
116
Tom Sepezb1670b52017-02-16 17:01:00 -0800117bool event::modifier(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700118 CJS_PropValue& vp,
119 CFX_WideString& sError) {
120 if (!vp.IsGetting())
121 return false;
122
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800123 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800124 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepez4cf55152016-11-02 14:37:54 -0700125
Tom Sepezb1670b52017-02-16 17:01:00 -0800126 vp << pEvent->Modifier();
tsepez4cf55152016-11-02 14:37:54 -0700127 return true;
128}
129
Tom Sepezb1670b52017-02-16 17:01:00 -0800130bool event::name(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800131 CJS_PropValue& vp,
132 CFX_WideString& sError) {
tsepez4cf55152016-11-02 14:37:54 -0700133 if (!vp.IsGetting())
134 return false;
135
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800136 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800137 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepez4cf55152016-11-02 14:37:54 -0700138
139 vp << pEvent->Name();
140 return true;
141}
142
Tom Sepezb1670b52017-02-16 17:01:00 -0800143bool event::rc(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800144 CJS_PropValue& vp,
145 CFX_WideString& sError) {
146 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800147 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepez4cf55152016-11-02 14:37:54 -0700148
149 bool& bRc = pEvent->Rc();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800150 if (vp.IsSetting())
tsepez4cf55152016-11-02 14:37:54 -0700151 vp >> bRc;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800152 else
tsepez4cf55152016-11-02 14:37:54 -0700153 vp << bRc;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800154
tsepez4cf55152016-11-02 14:37:54 -0700155 return true;
156}
157
Tom Sepezb1670b52017-02-16 17:01:00 -0800158bool event::richChange(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700159 CJS_PropValue& vp,
160 CFX_WideString& sError) {
161 return true;
162}
163
Tom Sepezb1670b52017-02-16 17:01:00 -0800164bool event::richChangeEx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700165 CJS_PropValue& vp,
166 CFX_WideString& sError) {
167 return true;
168}
169
Tom Sepezb1670b52017-02-16 17:01:00 -0800170bool event::richValue(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700171 CJS_PropValue& vp,
172 CFX_WideString& sError) {
173 return true;
174}
175
Tom Sepezb1670b52017-02-16 17:01:00 -0800176bool event::selEnd(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800177 CJS_PropValue& vp,
178 CFX_WideString& sError) {
179 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800180 pRuntime->GetCurrentEventContext()->GetEventHandler();
tsepez4cf55152016-11-02 14:37:54 -0700181
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800182 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
tsepez4cf55152016-11-02 14:37:54 -0700183 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184
185 int& iSelEnd = pEvent->SelEnd();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800186 if (vp.IsSetting())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 vp >> iSelEnd;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800188 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 vp << iSelEnd;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800190
tsepez4cf55152016-11-02 14:37:54 -0700191 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Tom Sepezb1670b52017-02-16 17:01:00 -0800194bool event::selStart(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700195 CJS_PropValue& vp,
196 CFX_WideString& sError) {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800197 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800198 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800200 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
tsepez4cf55152016-11-02 14:37:54 -0700201 return true;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 int& iSelStart = pEvent->SelStart();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800204 if (vp.IsSetting())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 vp >> iSelStart;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800206 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 vp << iSelStart;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800208
tsepez4cf55152016-11-02 14:37:54 -0700209 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Tom Sepezb1670b52017-02-16 17:01:00 -0800212bool event::shift(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800213 CJS_PropValue& vp,
214 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700216 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800218 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800219 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220
Tom Sepezb1670b52017-02-16 17:01:00 -0800221 vp << pEvent->Shift();
tsepez4cf55152016-11-02 14:37:54 -0700222 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Tom Sepezb1670b52017-02-16 17:01:00 -0800225bool event::source(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800226 CJS_PropValue& vp,
227 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700229 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800231 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800232 pRuntime->GetCurrentEventContext()->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233
234 vp << pEvent->Source()->GetJSObject();
tsepez4cf55152016-11-02 14:37:54 -0700235 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236}
237
Tom Sepezb1670b52017-02-16 17:01:00 -0800238bool event::target(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800239 CJS_PropValue& vp,
240 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700242 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800244 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800245 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 vp << pEvent->Target_Field()->GetJSObject();
tsepez4cf55152016-11-02 14:37:54 -0700248 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Tom Sepezb1670b52017-02-16 17:01:00 -0800251bool event::targetName(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700252 CJS_PropValue& vp,
253 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700255 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
Tom Sepezb1670b52017-02-16 17:01:00 -0800257 CJS_EventHandler* pEvent =
258 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 vp << pEvent->TargetName();
tsepez4cf55152016-11-02 14:37:54 -0700261 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Tom Sepezb1670b52017-02-16 17:01:00 -0800264bool event::type(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800265 CJS_PropValue& vp,
266 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700268 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800270 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800271 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 vp << pEvent->Type();
tsepez4cf55152016-11-02 14:37:54 -0700274 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
Tom Sepezb1670b52017-02-16 17:01:00 -0800277bool event::value(CJS_Runtime* pRuntime,
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800278 CJS_PropValue& vp,
279 CFX_WideString& sError) {
280 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800281 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
tsepez4cf55152016-11-02 14:37:54 -0700284 return false;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 if (!pEvent->m_pValue)
tsepez4cf55152016-11-02 14:37:54 -0700287 return false;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 CFX_WideString& val = pEvent->Value();
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800290 if (vp.IsSetting())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 vp >> val;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800292 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 vp << val;
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800294
tsepez4cf55152016-11-02 14:37:54 -0700295 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296}
297
Tom Sepezb1670b52017-02-16 17:01:00 -0800298bool event::willCommit(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700299 CJS_PropValue& vp,
300 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 if (!vp.IsGetting())
tsepez4cf55152016-11-02 14:37:54 -0700302 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800304 CJS_EventHandler* pEvent =
Tom Sepezb1670b52017-02-16 17:01:00 -0800305 pRuntime->GetCurrentEventContext()->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306
Tom Sepezb1670b52017-02-16 17:01:00 -0800307 vp << pEvent->WillCommit();
tsepez4cf55152016-11-02 14:37:54 -0700308 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}