• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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_event.h"
8 
9 #include "fxjs/cjs_event_context.h"
10 #include "fxjs/cjs_field.h"
11 #include "fxjs/cjs_object.h"
12 #include "fxjs/js_define.h"
13 
14 const JSPropertySpec CJS_Event::PropertySpecs[] = {
15     {"change", get_change_static, set_change_static},
16     {"changeEx", get_change_ex_static, set_change_ex_static},
17     {"commitKey", get_commit_key_static, set_commit_key_static},
18     {"fieldFull", get_field_full_static, set_field_full_static},
19     {"keyDown", get_key_down_static, set_key_down_static},
20     {"modifier", get_modifier_static, set_modifier_static},
21     {"name", get_name_static, set_name_static},
22     {"rc", get_rc_static, set_rc_static},
23     {"richChange", get_rich_change_static, set_rich_change_static},
24     {"richChangeEx", get_rich_change_ex_static, set_rich_change_ex_static},
25     {"richValue", get_rich_value_static, set_rich_value_static},
26     {"selEnd", get_sel_end_static, set_sel_end_static},
27     {"selStart", get_sel_start_static, set_sel_start_static},
28     {"shift", get_shift_static, set_shift_static},
29     {"source", get_source_static, set_source_static},
30     {"target", get_target_static, set_target_static},
31     {"targetName", get_target_name_static, set_target_name_static},
32     {"type", get_type_static, set_type_static},
33     {"value", get_value_static, set_value_static},
34     {"willCommit", get_will_commit_static, set_will_commit_static}};
35 
36 uint32_t CJS_Event::ObjDefnID = 0;
37 const char CJS_Event::kName[] = "event";
38 
39 // static
GetObjDefnID()40 uint32_t CJS_Event::GetObjDefnID() {
41   return ObjDefnID;
42 }
43 
44 // static
DefineJSObjects(CFXJS_Engine * pEngine)45 void CJS_Event::DefineJSObjects(CFXJS_Engine* pEngine) {
46   ObjDefnID = pEngine->DefineObj(CJS_Event::kName, FXJSOBJTYPE_STATIC,
47                                  JSConstructor<CJS_Event>, JSDestructor);
48   DefineProps(pEngine, ObjDefnID, PropertySpecs);
49 }
50 
CJS_Event(v8::Local<v8::Object> pObject,CJS_Runtime * pRuntime)51 CJS_Event::CJS_Event(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
52     : CJS_Object(pObject, pRuntime) {}
53 
54 CJS_Event::~CJS_Event() = default;
55 
get_change(CJS_Runtime * pRuntime)56 CJS_Result CJS_Event::get_change(CJS_Runtime* pRuntime) {
57   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
58   return CJS_Result::Success(
59       pRuntime->NewString(pEvent->Change().AsStringView()));
60 }
61 
set_change(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)62 CJS_Result CJS_Event::set_change(CJS_Runtime* pRuntime,
63                                  v8::Local<v8::Value> vp) {
64   if (vp->IsString()) {
65     CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
66     pEvent->Change() = pRuntime->ToWideString(vp);
67   }
68   return CJS_Result::Success();
69 }
70 
get_change_ex(CJS_Runtime * pRuntime)71 CJS_Result CJS_Event::get_change_ex(CJS_Runtime* pRuntime) {
72   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
73   return CJS_Result::Success(
74       pRuntime->NewString(pEvent->ChangeEx().AsStringView()));
75 }
76 
set_change_ex(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)77 CJS_Result CJS_Event::set_change_ex(CJS_Runtime* pRuntime,
78                                     v8::Local<v8::Value> vp) {
79   return CJS_Result::Failure(JSMessage::kNotSupportedError);
80 }
81 
get_commit_key(CJS_Runtime * pRuntime)82 CJS_Result CJS_Event::get_commit_key(CJS_Runtime* pRuntime) {
83   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
84   return CJS_Result::Success(pRuntime->NewNumber(pEvent->CommitKey()));
85 }
86 
set_commit_key(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)87 CJS_Result CJS_Event::set_commit_key(CJS_Runtime* pRuntime,
88                                      v8::Local<v8::Value> vp) {
89   return CJS_Result::Failure(JSMessage::kNotSupportedError);
90 }
91 
get_field_full(CJS_Runtime * pRuntime)92 CJS_Result CJS_Event::get_field_full(CJS_Runtime* pRuntime) {
93   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
94   if (pEvent->Name() != "Keystroke")
95     return CJS_Result::Failure(L"unrecognized event");
96 
97   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->FieldFull()));
98 }
99 
set_field_full(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)100 CJS_Result CJS_Event::set_field_full(CJS_Runtime* pRuntime,
101                                      v8::Local<v8::Value> vp) {
102   return CJS_Result::Failure(JSMessage::kNotSupportedError);
103 }
104 
get_key_down(CJS_Runtime * pRuntime)105 CJS_Result CJS_Event::get_key_down(CJS_Runtime* pRuntime) {
106   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
107   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->KeyDown()));
108 }
109 
set_key_down(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)110 CJS_Result CJS_Event::set_key_down(CJS_Runtime* pRuntime,
111                                    v8::Local<v8::Value> vp) {
112   return CJS_Result::Failure(JSMessage::kNotSupportedError);
113 }
114 
get_modifier(CJS_Runtime * pRuntime)115 CJS_Result CJS_Event::get_modifier(CJS_Runtime* pRuntime) {
116   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
117   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->Modifier()));
118 }
119 
set_modifier(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)120 CJS_Result CJS_Event::set_modifier(CJS_Runtime* pRuntime,
121                                    v8::Local<v8::Value> vp) {
122   return CJS_Result::Failure(JSMessage::kNotSupportedError);
123 }
124 
get_name(CJS_Runtime * pRuntime)125 CJS_Result CJS_Event::get_name(CJS_Runtime* pRuntime) {
126   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
127   return CJS_Result::Success(pRuntime->NewString(pEvent->Name()));
128 }
129 
set_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)130 CJS_Result CJS_Event::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
131   return CJS_Result::Failure(JSMessage::kNotSupportedError);
132 }
133 
get_rc(CJS_Runtime * pRuntime)134 CJS_Result CJS_Event::get_rc(CJS_Runtime* pRuntime) {
135   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
136   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->Rc()));
137 }
138 
set_rc(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)139 CJS_Result CJS_Event::set_rc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
140   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
141   pEvent->Rc() = pRuntime->ToBoolean(vp);
142   return CJS_Result::Success();
143 }
144 
get_rich_change(CJS_Runtime * pRuntime)145 CJS_Result CJS_Event::get_rich_change(CJS_Runtime* pRuntime) {
146   return CJS_Result::Success();
147 }
148 
set_rich_change(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)149 CJS_Result CJS_Event::set_rich_change(CJS_Runtime* pRuntime,
150                                       v8::Local<v8::Value> vp) {
151   return CJS_Result::Success();
152 }
153 
get_rich_change_ex(CJS_Runtime * pRuntime)154 CJS_Result CJS_Event::get_rich_change_ex(CJS_Runtime* pRuntime) {
155   return CJS_Result::Success();
156 }
157 
set_rich_change_ex(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)158 CJS_Result CJS_Event::set_rich_change_ex(CJS_Runtime* pRuntime,
159                                          v8::Local<v8::Value> vp) {
160   return CJS_Result::Success();
161 }
162 
get_rich_value(CJS_Runtime * pRuntime)163 CJS_Result CJS_Event::get_rich_value(CJS_Runtime* pRuntime) {
164   return CJS_Result::Success();
165 }
166 
set_rich_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)167 CJS_Result CJS_Event::set_rich_value(CJS_Runtime* pRuntime,
168                                      v8::Local<v8::Value> vp) {
169   return CJS_Result::Success();
170 }
171 
get_sel_end(CJS_Runtime * pRuntime)172 CJS_Result CJS_Event::get_sel_end(CJS_Runtime* pRuntime) {
173   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
174   if (pEvent->Name() != "Keystroke")
175     return CJS_Result::Success();
176 
177   return CJS_Result::Success(pRuntime->NewNumber(pEvent->SelEnd()));
178 }
179 
set_sel_end(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)180 CJS_Result CJS_Event::set_sel_end(CJS_Runtime* pRuntime,
181                                   v8::Local<v8::Value> vp) {
182   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
183   if (pEvent->Name() == "Keystroke")
184     pEvent->SetSelEnd(pRuntime->ToInt32(vp));
185 
186   return CJS_Result::Success();
187 }
188 
get_sel_start(CJS_Runtime * pRuntime)189 CJS_Result CJS_Event::get_sel_start(CJS_Runtime* pRuntime) {
190   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
191   if (pEvent->Name() != "Keystroke")
192     return CJS_Result::Success();
193 
194   return CJS_Result::Success(pRuntime->NewNumber(pEvent->SelStart()));
195 }
196 
set_sel_start(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)197 CJS_Result CJS_Event::set_sel_start(CJS_Runtime* pRuntime,
198                                     v8::Local<v8::Value> vp) {
199   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
200   if (pEvent->Name() == "Keystroke")
201     pEvent->SetSelStart(pRuntime->ToInt32(vp));
202 
203   return CJS_Result::Success();
204 }
205 
get_shift(CJS_Runtime * pRuntime)206 CJS_Result CJS_Event::get_shift(CJS_Runtime* pRuntime) {
207   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
208   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->Shift()));
209 }
210 
set_shift(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)211 CJS_Result CJS_Event::set_shift(CJS_Runtime* pRuntime,
212                                 v8::Local<v8::Value> vp) {
213   return CJS_Result::Failure(JSMessage::kNotSupportedError);
214 }
215 
get_source(CJS_Runtime * pRuntime)216 CJS_Result CJS_Event::get_source(CJS_Runtime* pRuntime) {
217   CJS_Field* pField = pRuntime->GetCurrentEventContext()->SourceField();
218   if (!pField)
219     return CJS_Result::Failure(JSMessage::kBadObjectError);
220   return CJS_Result::Success(pField->ToV8Object());
221 }
222 
set_source(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)223 CJS_Result CJS_Event::set_source(CJS_Runtime* pRuntime,
224                                  v8::Local<v8::Value> vp) {
225   return CJS_Result::Failure(JSMessage::kNotSupportedError);
226 }
227 
get_target(CJS_Runtime * pRuntime)228 CJS_Result CJS_Event::get_target(CJS_Runtime* pRuntime) {
229   CJS_Field* pField = pRuntime->GetCurrentEventContext()->TargetField();
230   if (!pField)
231     return CJS_Result::Failure(JSMessage::kBadObjectError);
232   return CJS_Result::Success(pField->ToV8Object());
233 }
234 
set_target(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)235 CJS_Result CJS_Event::set_target(CJS_Runtime* pRuntime,
236                                  v8::Local<v8::Value> vp) {
237   return CJS_Result::Failure(JSMessage::kNotSupportedError);
238 }
239 
get_target_name(CJS_Runtime * pRuntime)240 CJS_Result CJS_Event::get_target_name(CJS_Runtime* pRuntime) {
241   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
242   return CJS_Result::Success(
243       pRuntime->NewString(pEvent->TargetName().AsStringView()));
244 }
245 
set_target_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)246 CJS_Result CJS_Event::set_target_name(CJS_Runtime* pRuntime,
247                                       v8::Local<v8::Value> vp) {
248   return CJS_Result::Failure(JSMessage::kNotSupportedError);
249 }
250 
get_type(CJS_Runtime * pRuntime)251 CJS_Result CJS_Event::get_type(CJS_Runtime* pRuntime) {
252   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
253   return CJS_Result::Success(pRuntime->NewString(pEvent->Type()));
254 }
255 
set_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)256 CJS_Result CJS_Event::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
257   return CJS_Result::Failure(JSMessage::kNotSupportedError);
258 }
259 
get_value(CJS_Runtime * pRuntime)260 CJS_Result CJS_Event::get_value(CJS_Runtime* pRuntime) {
261   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
262   if (pEvent->Type() != "Field")
263     return CJS_Result::Failure(L"Bad event type.");
264 
265   if (!pEvent->HasValue())
266     return CJS_Result::Failure(JSMessage::kBadObjectError);
267 
268   return CJS_Result::Success(
269       pRuntime->NewString(pEvent->Value().AsStringView()));
270 }
271 
set_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)272 CJS_Result CJS_Event::set_value(CJS_Runtime* pRuntime,
273                                 v8::Local<v8::Value> vp) {
274   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
275   if (pEvent->Type() != "Field")
276     return CJS_Result::Failure(L"Bad event type.");
277 
278   if (!pEvent->HasValue())
279     return CJS_Result::Failure(JSMessage::kBadObjectError);
280 
281   if (vp.IsEmpty())
282     return CJS_Result::Failure(JSMessage::kBadObjectError);
283 
284   if (vp->IsNullOrUndefined() || vp->IsBoolean())
285     return CJS_Result::Failure(JSMessage::kInvalidSetError);
286 
287   pEvent->Value() = pRuntime->ToWideString(vp);
288   return CJS_Result::Success();
289 }
290 
get_will_commit(CJS_Runtime * pRuntime)291 CJS_Result CJS_Event::get_will_commit(CJS_Runtime* pRuntime) {
292   CJS_EventContext* pEvent = pRuntime->GetCurrentEventContext();
293 
294   return CJS_Result::Success(pRuntime->NewBoolean(pEvent->WillCommit()));
295 }
296 
set_will_commit(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)297 CJS_Result CJS_Event::set_will_commit(CJS_Runtime* pRuntime,
298                                       v8::Local<v8::Value> vp) {
299   return CJS_Result::Failure(JSMessage::kNotSupportedError);
300 }
301