RDK Documentation (Open Sourced RDK Components)
jsevent.h
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file jsevent.h
22  * @brief JavaScript Event Impl for AAMP_JSController and AAMPMediaPlayer_JS
23  */
24 
25 #ifndef __AAMP_JSEVENT_H__
26 #define __AAMP_JSEVENT_H__
27 
28 #include <JavaScriptCore/JavaScript.h>
29 
30 // DELIA-39828: JS garbage collector has pretty high threshold values for execution
31 // Also GC doesn't monitor the native memory attached to a JS object. So its in our best
32 // interest, we don't enabled below macro, so no native objects are allocated for/tied to a JS event
33 //#define JSEVENT_WITH_NATIVE_MEMORY
34 
35 #ifdef JSEVENT_WITH_NATIVE_MEMORY
36 /**
37  * @enum EventPhase
38  * @brief Phase of the event flow that is currently being evaluated.
39  */
40 enum EventPhase
41 {
42  pNone = 0,
43  pCapturingPhase, /**< Event flow is in capturing phase **/
44  pAtTarget, /**< Event flow is in target phase **/
45  pBubblingPhase /**< Event flow is in bubbling phase **/
46 };
47 
48 /**
49  * @class AAMPJSEvent
50  * @brief Class represents the native object for a AAMP JS Event
51  */
52 class AAMPJSEvent
53 {
54 
55 public:
56  /**
57  * @fn AAMPJSEvent
58  */
59  AAMPJSEvent();
60  /**
61  * @fn AAMPJSEvent
62  * @param[in] type event type
63  * @param[in] bubble true if event is a bubbling event
64  * @param[in] cancelable true if event default operation can be cancelled
65  */
66  AAMPJSEvent(const char *type, bool bubble, bool cancelable);
67  /**
68  * @brief Copy constructor disabled
69  *
70  */
71  AAMPJSEvent(const AAMPJSEvent&) = delete;
72  /**
73  * @brief assignment operator disabled
74  *
75  */
76  AAMPJSEvent& operator=(const AAMPJSEvent&) = delete;
77  /**
78  * @fn ~AAMPJSEvent
79  */
80  ~AAMPJSEvent();
81  /**
82  * @fn initEvent
83  * @param[in] type event type
84  * @param[in] bubble true if event is a bubbling event
85  * @param[in] cancelable true if event default operation can be cancelled
86  */
87  void initEvent(const char *type, bool bubble, bool cancelable);
88 
89  /**
90  * @brief Returns the name of the event
91  * @retval event type
92  */
93  const char* getType() { return _typeName; }
94 
95  /**
96  * @brief Returns whether or not a specific event is a bubbling event
97  * @retval true if event is a bubbling event
98  */
99  bool getBubbles() { return _bubbles; }
100 
101  /**
102  * @brief Returns whether or not an event can have its default action prevented
103  * @retval true if default action of event can be prevented
104  */
105  bool getCancelable() { return _cancelable; }
106 
107  /**
108  * @brief Returns the element that triggered the event
109  * @retval instance of the element
110  */
111  JSObjectRef getTarget() { return _target; }
112 
113  /**
114  * @brief Returns the element whose event listeners triggered the event
115  * @retval instance of the element
116  */
117  JSObjectRef getCurrentTarget() { return _currentTarget; }
118 
119  /**
120  * @brief Returns which phase of the event flow is currently being evaluated
121  * @retval event phase
122  */
123  EventPhase getEventPhase() { return _phase; }
124 
125  /**
126  * @brief Returns whether or not the preventDefault() method was called for the event
127  * @retval true if default was prevented
128  */
129  bool getIsDefaultPrevented() { return _canceled; }
130 
131  /**
132  * @brief Returns whether or not an event is trusted
133  * @retval true if event is trusted
134  */
135  bool getIsTrusted() { return _isTrusted; }
136 
137  /**
138  * @brief Returns the time (in milliseconds relative to the epoch) at which the event was created
139  * @retval timestamp
140  */
141  double getTimestamp() { return _timestamp; }
142 
143  /**
144  * @brief Cancels the event if it is cancelable
145  */
146  void preventDefault()
147  {
148  if(_cancelable)
149  _canceled = true;
150  }
151 
152  /**
153  * @brief Prevents other listeners of the same event from being called
154  */
155  void stopImmediatePropagation()
156  {
157  _stopImmediatePropagation = true;
158  }
159 
160  /**
161  * @brief Prevents further propagation of an event during event flow
162  */
163  void stopPropagation()
164  {
165  _stopPropagation = true;
166  }
167 
168  /**
169  * @brief Set the target instance
170  * @param[in] context JS execution context
171  * @param[in] obj target instance
172  */
173  void setTarget(JSContextRef context, JSObjectRef obj)
174  {
175  if (_ctx == NULL)
176  {
177  _ctx = context;
178  }
179  if (_target)
180  {
181  JSValueUnprotect(_ctx, _target);
182  }
183  _target = obj;
184  JSValueProtect(_ctx, _target);
185  }
186 
187  /**
188  * @brief Set the current target instance
189  * @param[in] context JS execution context
190  * @param[in] obj current target instance
191  */
192  void setCurrentTarget(JSContextRef context, JSObjectRef obj)
193  {
194  if (_ctx == NULL)
195  {
196  _ctx = context;
197  }
198  if (_currentTarget)
199  {
200  JSValueUnprotect(_ctx, _currentTarget);
201  }
202  _currentTarget = obj;
203  JSValueProtect(_ctx, _currentTarget);
204  }
205 
206 private:
207  bool _bubbles; /**< denotes if event is a bubbling event */
208  bool _cancelable; /**< denotes if event default operation can be cancelled */
209  bool _canceled; /**< denotes if event default operation was cancelled */
210  JSObjectRef _currentTarget; /**< element whose event listeners triggered the event */
211  bool _defaultPrevented;
212  EventPhase _phase; /**< phase of the event flow is currently being evaluated */
213  JSObjectRef _target; /**< element that triggered the event */
214  double _timestamp; /**< event timestamp */
215  const char * _typeName; /**< event type name */
216  bool _isTrusted; /**< denotes if event is trusted or not */
217 
218  bool _stopImmediatePropagation; /**< denotes if other listeners need not be called */
219  bool _stopPropagation; /**< denotes if further propagation of an event to be prevented */
220 
221  JSContextRef _ctx; /**< JS execution context */
222 };
223 #endif
224 
225 /**
226  * @brief To create a new JS event instance
227  * @param[in] ctx JS execution context
228  * @param[in] type event type
229  * @param[in] bubbles denotes if event support bubbling
230  * @param[in] cancelable denotes if event is cancelable
231  * @retval JSObject of the new instance created
232  */
233 JSObjectRef createNewAAMPJSEvent(JSGlobalContextRef ctx, const char *type, bool bubbles, bool cancelable);
234 
235 #endif // __AAMP_JSEVENT_H__
createNewAAMPJSEvent
JSObjectRef createNewAAMPJSEvent(JSGlobalContextRef ctx, const char *type, bool bubbles, bool cancelable)
To create a new JS event instance.
Definition: jsevent.cpp:519