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