• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_JS_FWK_COMMON_H
17 #define OHOS_ACELITE_JS_FWK_COMMON_H
18 
19 #include "handler.h"
20 #include "js_config.h"
21 #include "non_copyable.h"
22 #ifdef JS_EXTRA_EVENT_SUPPORT
23 #include "root_view.h"
24 #endif
25 #include "graphic_config.h"
26 #include "wrapper/js.h"
27 namespace OHOS {
28 namespace ACELite {
29 struct Watcher : public MemoryHeap {
30     ACE_DISALLOW_COPY_AND_MOVE(Watcher);
WatcherWatcher31     Watcher() : watcher(jerry_create_undefined()), next(nullptr) {}
32     jerry_value_t watcher;
33     struct Watcher *next;
34 };
35 
36 /*
37  * Remove parameters, variables, or expressions compiling warning.
38  */
39 #undef UNUSED // incase others define the same macro
40 #define UNUSED(a) (void)(a)
41 
42 #ifndef UNDEFINED
43 #define UNDEFINED jerry_create_undefined()
44 #endif
45 
46 #ifndef IS_UNDEFINED
47 #define IS_UNDEFINED(v) jerry_value_is_undefined(v)
48 #endif
49 
50 #ifndef IS_ERROR_VALUE
51 #define IS_ERROR_VALUE(v) jerry_value_is_error(v)
52 #endif
53 
54 #ifndef ACE_FREE
55 #define ACE_FREE(pointer)     \
56     if (pointer != nullptr) { \
57         ace_free(pointer);    \
58         pointer = nullptr;    \
59     }
60 #endif // ACE_FREE
61 
62 #ifndef ACE_DELETE
63 #define ACE_DELETE(pointer)   \
64     if (pointer != nullptr) { \
65         delete pointer;       \
66         pointer = nullptr;    \
67     }
68 #endif // ACE_DELETE
69 
70 #if IS_ENABLED(JS_PROFILER)
71 #ifndef LOG_PROFILER_TRACE
72 #define LOG_PROFILER(format, ...) printf(format "\n", ##__VA_ARGS__)
73 #define LOG_PROFILER_TRACE(format, ...) printf("[PERFORMANCE]:" format "\n", ##__VA_ARGS__)
74 #endif
75 #else
76 #ifndef LOG_PROFILER_TRACE
77 #define LOG_PROFILER(format, ...)
78 #define LOG_PROFILER_TRACE(format, ...)
79 #endif
80 #endif
81 
82 constexpr uint16_t PATH_LENGTH_MAX = 1024;
83 constexpr uint8_t NAME_LENGTH_MAX = 255;
84 constexpr uint8_t DEVICE_ID_LENGTH_MAX = 65;
85 constexpr uint16_t FILE_CONTENT_LENGTH_MAX = 1024 * 48;
86 
87 // hex code
88 constexpr uint8_t DEC = 10;
89 constexpr uint8_t HEX = 16;
90 
91 constexpr int SIZE_EXPRESSION_PLACE_HOLDER = 4;
92 constexpr int IDX_EXPRESSSION_STRING = 2;
93 constexpr int ARG_LENGTH_WATCHER = 3;
94 constexpr int ARG_LENGTH_WATCHER_CALLBACK = 3;
95 
96 constexpr char ATTR_ROOT[] = "$root"; // global.$root represents for abilitySlice
97 
98 constexpr uint32_t RGB_COLOR_VALUE_MAX = 0x1000000;
99 constexpr uint32_t TEXT_RED_COLOR_MASK = 0xff0000;
100 constexpr uint32_t TEXT_GREEN_COLOR_MASK = 0x00ff00;
101 constexpr uint32_t TEXT_BLUE_COLOR_MASK = 0x0000ff;
102 constexpr int RED_COLOR_START_BIT = 16;
103 constexpr int GREEN_COLOR_START_BIT = 8;
104 constexpr char ATTR_SRC[] = "src"; // image-animator
105 #if (FEATURE_COMPONENT_ANALOG_CLOCK == 1)
106 constexpr char CLOCK_HAND_IS_IMAGE[] = "isImage";
107 constexpr char COMMON_STYLE_OPACITY[] = "opacity";
108 constexpr char COMMON_STYLE_COLOR[] = "color";
109 #endif // FEATURE_COMPONENT_ANALOG_CLOCK
110 constexpr uint8_t DEFAULT_FONT_SIZE = 30;
111 #if (FEATURE_COMPONENT_CANVAS == 1)
112 constexpr uint8_t DEFAULT_FONT_LETTERSPACE = 2;
113 #endif // FEATURE_COMPONENT_CANVAS
114 constexpr char DEFAULT_FONT_FAMILY[] = DEFAULT_VECTOR_FONT_FILENAME;
115 
116 // common attributes
117 constexpr char ATTR_WIDTH[] = "width";   // tab-bar | image-animator
118 constexpr char ATTR_HEIGHT[] = "height"; // tab-bar | image-animator
119 constexpr char ATTR_LEFT[] = "left";     // image-animator
120 constexpr char ATTR_TOP[] = "top";       // image-animator
121 constexpr char ATTR_TYPE[] = "type";
122 constexpr char COMMON_STYLE_BACKGROUND_COLOR[] = "backgroundColor";
123 constexpr char ARG_WATCH_EL[] = "el";
124 constexpr char ARG_WATCH_ATTR[] = "attr";
125 constexpr char ATTR_ATTRS[] = "attrs";
126 constexpr char ATTR_FREEZE[] = "freeze";
127 constexpr char ABILITY_LIFECYCLE_CALLBACK_ON_CREATE[] = "onCreate";
128 constexpr char ABILITY_LIFECYCLE_CALLBACK_ON_DESTROY[] = "onDestroy";
129 #ifdef _MINI_MULTI_TASKS_
130 constexpr char ABILITY_LIFECYCLE_CALLBACK_ON_RESTORE_DATA[] = "onRestoreData";
131 constexpr char ABILITY_LIFECYCLE_CALLBACK_ON_SAVE_DATA[] = "onSaveData";
132 #endif
133 constexpr char BACK_PRESSED_NAME[] = "$backPress";
134 constexpr char ROUTER_PAGE_URI[] = "uri";
135 constexpr char ROUTER_PAGE_PARAMS[] = "params";
136 constexpr char ROUTER_PAGE_PATH[] = "path";
137 constexpr char ROUTER_PAGE[] = "$page";
138 constexpr char ATTR_REFS[] = "$refs";
139 constexpr char ATTR_SUCCESS[] = "success";
140 constexpr char ATTR_FAIL[] = "fail";
141 constexpr char ATTR_COMPLETE[] = "complete";
142 
143 // chart style
144 constexpr uint8_t POINT_SIZE = 5;
145 
146 // animation style
147 constexpr char ANIMATION_VALUE_SEP[] = ",";
148 constexpr char ATTR_APP[] = "$app";
149 constexpr char ATTR_CHILDREN[] = "children";
150 constexpr char ATTR_RENDER[] = "$render";
151 constexpr char ATTR_WATCH[] = "$watch";
152 constexpr char DESCRIPTOR_ATTR_FOR[] = "for";
153 constexpr char DESCRIPTOR_ATTR_NODES[] = "nodes";
154 constexpr char DESCRIPTOR_ATTR_RENDER[] = "render";
155 constexpr char DESCRIPTOR_ATTR_GETTER[] = "getter";
156 constexpr char TRANSITION_ROTATE[] = "rotate";
157 constexpr char TRANSITION_TRANSFORM_X[] = "translateX";
158 constexpr char TRANSITION_TRANSFORM_Y[] = "translateY";
159 constexpr char CONSTRUCTOR_VIEW_MODEL[] = "ViewModel";
160 constexpr char CONSTRUCTOR_ABILITY_SLICE[] = "AbilitySlice";
161 
162 #if (FEATURE_ROTATION_API == 1)
163 constexpr char FUNC_ROTATION_NAME[] = "rotation";
164 constexpr char ATTR_NAME_FOCUS[] = "focus";
165 #endif // FEATURE_ROTATION_API
166 
167 // get screen horizontal resolution
168 uint16_t GetHorizontalResolution();
169 // get screen vertical resolution
170 uint16_t GetVerticalResolution();
171 
172 // global functions
173 void JerrySetNamedProperty(jerry_value_t object, const char * const name, jerry_value_t propValue);
174 void JerrySetNumberProperty(jerry_value_t object, const char * const name, double value);
175 void JerrySetStringProperty(jerry_value_t object, const char * const name, const char * const value);
176 void JerrySetStringProperty(jerry_value_t object, const char * const name, const char * const value, uint32_t length);
177 // value returned should be freed by caller when it's not nullptr and won't be used any more
178 char *JerryMallocStringProperty(const jerry_value_t object, const char * const name, uint16_t &length);
179 int16_t JerryGetIntegerProperty(jerry_value_t object, const char * const name);
180 /**
181  * @brief try to parse bool property from one JS object, caller should check the returned result before using outValue.
182  * @param object the given object
183  * @param name the target property name
184  * @param outValue the target property value
185  * @return true if the parse successfully
186  */
187 bool JerryGetBoolProperty(jerry_value_t object, const char * const name, bool &outValue);
188 void JerrySetFuncProperty(jerry_value_t object, const char * const name, jerry_external_handler_t handler);
189 char *MallocStringOf(jerry_value_t source);
190 // convert one jerry string value to char*, and return the string length
191 char *MallocStringOf(jerry_value_t source, uint16_t *strLength);
192 int16_t IntegerOf(jerry_value_t source);
193 float FloatOf(jerry_value_t source);
194 bool BoolOf(jerry_value_t source);
195 
196 // relocate file name to full path of the current app path
197 char *RelocateFilePath(const char *appRootPath, const char *subPath, const char *fileName);
198 
199 // relocate file name to appRootPath/dirA/dirB/fileName
200 // e.x. /system/app/73709738-2d9d-4947-ac63-9858dcae7ccb/pages/index/right.png
201 char *RelocateFilePathRelative(const char * const appRootPath, const char * const resFileName);
202 
203 // returned value must be freed by caller
204 char *RelocateResourceFilePath(const char * const appRootPath, const char * const resFileName);
205 // returned value must be freed by caller
206 char *RelocateJSSourceFilePath(const char * const appRootPath, const char * const srcFileName);
207 // read from src file, return value must be freed by caller
208 char *ReadJSFile(const char * const appPath, const char * const jsFileName);
209 // read the given file and return the whole file content
210 char *ReadJSFile(const char * const appPath, const char * const jsFileName, uint32_t &fileSize);
211 // read the given file and return the whole file content
212 char *ReadFile(const char * const fullPath, uint32_t &fileSize, const bool binary);
213 // check whether the given file exists
214 bool IsFileExisted(const char * const filePath);
215 // Whether file existed and return the file size. 0 represents file not existed.
216 int32_t GetFileSize(const char * const filePath);
217 
218 /**
219  * @brief give out the path string from an url pattern, "url(common/logo.png)".
220  *
221  * @param url the given url
222  *
223  * @return the url itself, "common/logo.png"
224  */
225 char *CreatePathStrFromUrl(const char * const url);
226 /**
227  * @brief Call jerry_has_property and return the result
228  *
229  * @param container the target JS object to check
230  * @param property the property string value
231  */
232 bool JerryHasProperty(const jerry_value_t container, const jerry_value_t property);
233 /**
234  * @brief Call jerry_has_own_property and return the result
235  *
236  * @param container the target JS object to check
237  * @param property the property string value
238  */
239 bool HasOwnProperty(const jerry_value_t container, const jerry_value_t property);
240 // watcher callback function
241 jerry_value_t WatcherCallbackFunc(const jerry_value_t func,
242                                   const jerry_value_t context,
243                                   const jerry_value_t *args,
244                                   const jerry_length_t argsLength);
245 // print out error information from jerry value
246 void PrintErrorMessage(const jerry_value_t errorValue);
247 // wrapper function for jerry_call_function
248 jerry_value_t CallJSFunction(const jerry_value_t func,          /* function object to call */
249                              const jerry_value_t context,       /* object for 'this' binding */
250                              const jerry_value_t args[],        /* function's call arguments */
251                              const jerry_size_t argsCount);     /* number of the arguments */
252 void CallJSFunctionAutoRelease(const jerry_value_t funcObj,     /* function object to call */
253                                const jerry_value_t thisVal,     /* object for 'this' binding */
254                                const jerry_value_t args[],      /* function's call arguments */
255                                const jerry_size_t argc);        /* number of the arguments */
256 jerry_value_t CallJSFunctionOnRoot(const jerry_value_t funcObj, /* function object to call */
257                                    const jerry_value_t args[],  /* function's call arguments */
258                                    const jerry_size_t argc);    /* number of the arguments */
259 jerry_value_t CallJSWatcher(jerry_value_t arg1,
260                             jerry_value_t (*watcherCB)(const jerry_value_t func,
261                                                        const jerry_value_t context,
262                                                        const jerry_value_t *args,
263                                                        const jerry_length_t argsLength),
264                             jerry_value_t arg3);
265 void ReleaseJerryValue(jerry_value_t value, ...);
266 void InsertWatcherCommon(Watcher *&head, const jerry_value_t watcher);
267 void ClearWatchersCommon(Watcher *&head);
268 jerry_value_t ListForWatcherCallbackFunc(const jerry_value_t func,
269                                          const jerry_value_t context,
270                                          const jerry_value_t *args,
271                                          const jerry_length_t argsLength);
272 #ifdef JS_EXTRA_EVENT_SUPPORT
273 jerry_value_t *ConvertBaseEventInfo(const Event &event, const uint16_t id);
274 bool CallBaseEvent(const jerry_value_t func, const Event &event, const uint16_t id);
275 jerry_value_t *ConvertKeyEventInfo(const KeyEvent &event);
276 void ClearEventListener(const jerry_value_t args[], uint8_t argc);
277 #endif
278 JSValue GetRootAbilitySlice();
279 JSValue CallWithRootAbilitySlice(JSValue func, JSValue args[], JSSize size);
280 JSValue CallWithRootAbilitySlice(JSValue func);
281 JSValue CreateWatcher(JSValue getter, JSHandler handler, JSValue options);
282 
283 void ExpandImagePathMem(char *&imagePath, const int16_t dotPos, const int16_t suffixLen, const int16_t imagePathLen);
284 #if (OHOS_ACELITE_PRODUCT_WATCH == 1)
285 void CureImagePath(char *&imagePath);
286 #endif // OHOS_ACELITE_PRODUCT_WATCH
287 const char *ParseImageSrc(jerry_value_t source);
288 
289 /**
290  * @brief ParseKeyIdFromJSString parse key ID from JS value
291  * @param str the input JS string value
292  * @return the key ID
293  */
294 uint16_t ParseKeyIdFromJSString(const jerry_value_t str);
295 
296 constexpr char PATH_PREFIX[] = ".";
297 constexpr char PATH_DEFAULT[] = "/";
298 
299 constexpr char PREFIX_HEX_COLOR[] = "#";
300 constexpr char PREFIX_RGB_COLOR[] = "rgb";
301 constexpr char PREFIX_RGBA_COLOR[] = "rgba";
302 constexpr char LINEJOIN_MITER[] = "miter";
303 constexpr char LINEJOIN_ROUND[] = "round";
304 constexpr char LINEJOIN_BEVEL[] = "bevel";
305 constexpr char LINECAP_BUTT[] = "butt";
306 constexpr char LINECAP_SQUARE[] = "square";
307 constexpr char LINECAP_ROUND[] = "round";
308 constexpr uint8_t ALPHA_MAX = 255;
309 constexpr char BRACE_OPEN = '(';
310 constexpr char BRACE_CLOSE = ')';
311 constexpr char DELIMITER[] = ",";
312 constexpr char RESOURCE_SEPARATOR = '/';
313 
314 bool StartWith(const char *sequence, const char *prefix);
315 bool IsHexColor(const char * const target);
316 bool IsRgbColor(const char * const target);
317 bool IsRgbaColor(const char * const target);
318 bool ParseHexColor(const char * const source, uint32_t &color, uint8_t &alpha);
319 bool ParseRgbaColor(const char * const source, uint32_t &color, uint8_t &alpha);
320 bool ParseColor(const char * const source, uint32_t &color, uint8_t &alpha);
321 bool CopyFontFamily(char *&destination, const char * const fontFamily, uint32_t fontFamilyNameLen = 0);
322 int8_t ParseLineCap(const char* lineCap);
323 int8_t ParseLineJoin(const char* lineJoin);
324 
325 constexpr int16_t BUTT_VALUE = 0;
326 constexpr int16_t SQUARE_VALUE = 1;
327 constexpr int16_t ROUND_VALUE = 2;
328 constexpr int16_t LINEJOIN_MITER_VALUE = 0;
329 constexpr int16_t LINEJOIN_ROUND_VALUE = 1;
330 constexpr int16_t LINEJOIN_BEVEL_VALUE = 2;
331 
332 #if (defined(_WIN32) || defined(_WIN64))
333 constexpr char PATH_SEPARATOR = '\\';
334 constexpr char PATH_RELATIVE_WHOLE[] = "\\..\\";
335 constexpr char PATH_RELATIVE_PART[] = "..\\";
336 constexpr char SRC_SUB_FOLDER_NAME[] = "";
337 constexpr char JS_INDEX_FILE_PATH[] = "pages\\index\\index";
338 #else
339 constexpr char PATH_SEPARATOR = '/';
340 constexpr char PATH_RELATIVE_WHOLE[] = "/../";
341 constexpr char PATH_RELATIVE_PART[] = "../";
342 constexpr char SRC_SUB_FOLDER_NAME[] = "";
343 constexpr char JS_INDEX_FILE_PATH[] = "pages/index/index";
344 #endif
345 
346 // for error definition
347 enum {
348     FAILED = -1,
349     SUCCESS = 0,
350     ERROR_BASE = 0x10000,
351     ERROR_INPUT_PARAMETER,
352     ERROR_MALLOC,
353     ERROR_SECURE_USE // use secure function error
354 };
355 
356 constexpr int VA_ARG_END_FLAG = -1;
357 constexpr int INVALID_PIXEL_VALUE = -1;
358 
359 // switch on random path
360 #ifndef JS_PAGE_SPECIFIC
361 #define JS_PAGE_SPECIFIC 0
362 #endif
363 
364 #if JS_PAGE_SPECIFIC
365 constexpr char servicePrefix[] = "pages";
366 constexpr char serviceSuffix[] = "index";
367 
368 struct JSPageSpecific : public MemoryHeap {
369     char *jsIndexFilePath; // page path
JSPageSpecificJSPageSpecific370     JSPageSpecific() : jsIndexFilePath(nullptr) {}
371 };
372 
373 #endif // JS_PAGE_SPECIFIC
374 } // namespace ACELite
375 } // namespace OHOS
376 #endif // OHOS_ACELITE_JS_FWK_COMMON_H
377