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