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 constexpr char BACK_PRESSED_NAME[] = "$backPress"; 130 constexpr char ROUTER_PAGE_URI[] = "uri"; 131 constexpr char ROUTER_PAGE_PARAMS[] = "params"; 132 constexpr char ROUTER_PAGE_PATH[] = "path"; 133 constexpr char ROUTER_PAGE[] = "$page"; 134 constexpr char ATTR_REFS[] = "$refs"; 135 constexpr char ATTR_SUCCESS[] = "success"; 136 constexpr char ATTR_FAIL[] = "fail"; 137 constexpr char ATTR_COMPLETE[] = "complete"; 138 139 // chart style 140 constexpr uint8_t POINT_SIZE = 5; 141 142 // animation style 143 constexpr char ANIMATION_VALUE_SEP[] = ","; 144 constexpr char ATTR_APP[] = "$app"; 145 constexpr char ATTR_CHILDREN[] = "children"; 146 constexpr char ATTR_RENDER[] = "$render"; 147 constexpr char ATTR_WATCH[] = "$watch"; 148 constexpr char DESCRIPTOR_ATTR_FOR[] = "for"; 149 constexpr char DESCRIPTOR_ATTR_NODES[] = "nodes"; 150 constexpr char DESCRIPTOR_ATTR_RENDER[] = "render"; 151 constexpr char DESCRIPTOR_ATTR_GETTER[] = "getter"; 152 constexpr char TRANSITION_ROTATE[] = "rotate"; 153 constexpr char TRANSITION_TRANSFORM_X[] = "translateX"; 154 constexpr char TRANSITION_TRANSFORM_Y[] = "translateY"; 155 constexpr char CONSTRUCTOR_VIEW_MODEL[] = "ViewModel"; 156 constexpr char CONSTRUCTOR_ABILITY_SLICE[] = "AbilitySlice"; 157 158 #if (FEATURE_ROTATION_API == 1) 159 constexpr char FUNC_ROTATION_NAME[] = "rotation"; 160 constexpr char ATTR_NAME_FOCUS[] = "focus"; 161 #endif // FEATURE_ROTATION_API 162 163 // get screen horizontal resolution 164 uint16_t GetHorizontalResolution(); 165 // get screen vertical resolution 166 uint16_t GetVerticalResolution(); 167 168 // global functions 169 void JerrySetNamedProperty(jerry_value_t object, const char * const name, jerry_value_t propValue); 170 void JerrySetNumberProperty(jerry_value_t object, const char * const name, double value); 171 void JerrySetStringProperty(jerry_value_t object, const char * const name, const char * const value); 172 void JerrySetStringProperty(jerry_value_t object, const char * const name, const char * const value, uint32_t length); 173 // value returned should be freed by caller when it's not nullptr and won't be used any more 174 char *JerryMallocStringProperty(const jerry_value_t object, const char * const name, uint16_t &length); 175 int16_t JerryGetIntegerProperty(jerry_value_t object, const char * const name); 176 /** 177 * @brief try to parse bool property from one JS object, caller should check the returned result before using outValue. 178 * @param object the given object 179 * @param name the target property name 180 * @param outValue the target property value 181 * @return true if the parse successfully 182 */ 183 bool JerryGetBoolProperty(jerry_value_t object, const char * const name, bool &outValue); 184 void JerrySetFuncProperty(jerry_value_t object, const char * const name, jerry_external_handler_t handler); 185 char *MallocStringOf(jerry_value_t source); 186 // convert one jerry string value to char*, and return the string length 187 char *MallocStringOf(jerry_value_t source, uint16_t *strLength); 188 int16_t IntegerOf(jerry_value_t source); 189 float FloatOf(jerry_value_t source); 190 bool BoolOf(jerry_value_t source); 191 192 // relocate file name to full path of the current app path 193 char *RelocateFilePath(const char *appRootPath, const char *subPath, const char *fileName); 194 195 // relocate file name to appRootPath/dirA/dirB/fileName 196 // e.x. /system/app/73709738-2d9d-4947-ac63-9858dcae7ccb/pages/index/right.png 197 char *RelocateFilePathRelative(const char * const appRootPath, const char * const resFileName); 198 199 // returned value must be freed by caller 200 char *RelocateResourceFilePath(const char * const appRootPath, const char * const resFileName); 201 // returned value must be freed by caller 202 char *RelocateJSSourceFilePath(const char * const appRootPath, const char * const srcFileName); 203 // read from src file, return value must be freed by caller 204 char *ReadJSFile(const char * const appPath, const char * const jsFileName); 205 // read the given file and return the whole file content 206 char *ReadJSFile(const char * const appPath, const char * const jsFileName, uint32_t &fileSize); 207 // read the given file and return the whole file content 208 char *ReadFile(const char * const fullPath, uint32_t &fileSize, const bool binary); 209 // check whether the given file exists 210 bool IsFileExisted(const char * const filePath); 211 // Whether file existed and return the file size. 0 represents file not existed. 212 int32_t GetFileSize(const char * const filePath); 213 214 /** 215 * @brief give out the path string from an url pattern, "url(common/logo.png)". 216 * 217 * @param url the given url 218 * 219 * @return the url itself, "common/logo.png" 220 */ 221 char *CreatePathStrFromUrl(const char * const url); 222 /** 223 * @brief Call jerry_has_property and return the result 224 * 225 * @param container the target JS object to check 226 * @param property the property string value 227 */ 228 bool JerryHasProperty(const jerry_value_t container, const jerry_value_t property); 229 /** 230 * @brief Call jerry_has_own_property and return the result 231 * 232 * @param container the target JS object to check 233 * @param property the property string value 234 */ 235 bool HasOwnProperty(const jerry_value_t container, const jerry_value_t property); 236 // watcher callback function 237 jerry_value_t WatcherCallbackFunc(const jerry_value_t func, 238 const jerry_value_t context, 239 const jerry_value_t *args, 240 const jerry_length_t argsLength); 241 // print out error information from jerry value 242 void PrintErrorMessage(const jerry_value_t errorValue); 243 // wrapper function for jerry_call_function 244 jerry_value_t CallJSFunction(const jerry_value_t func, /* function object to call */ 245 const jerry_value_t context, /* object for 'this' binding */ 246 const jerry_value_t args[], /* function's call arguments */ 247 const jerry_size_t argsCount); /* number of the arguments */ 248 void CallJSFunctionAutoRelease(const jerry_value_t funcObj, /* function object to call */ 249 const jerry_value_t thisVal, /* object for 'this' binding */ 250 const jerry_value_t args[], /* function's call arguments */ 251 const jerry_size_t argc); /* number of the arguments */ 252 jerry_value_t CallJSFunctionOnRoot(const jerry_value_t funcObj, /* function object to call */ 253 const jerry_value_t args[], /* function's call arguments */ 254 const jerry_size_t argc); /* number of the arguments */ 255 jerry_value_t CallJSWatcher(jerry_value_t arg1, 256 jerry_value_t (*watcherCB)(const jerry_value_t func, 257 const jerry_value_t context, 258 const jerry_value_t *args, 259 const jerry_length_t argsLength), 260 jerry_value_t arg3); 261 void ReleaseJerryValue(jerry_value_t value, ...); 262 void InsertWatcherCommon(Watcher *&head, const jerry_value_t watcher); 263 void ClearWatchersCommon(Watcher *&head); 264 jerry_value_t ListForWatcherCallbackFunc(const jerry_value_t func, 265 const jerry_value_t context, 266 const jerry_value_t *args, 267 const jerry_length_t argsLength); 268 #ifdef JS_EXTRA_EVENT_SUPPORT 269 jerry_value_t *ConvertBaseEventInfo(const Event &event, const uint16_t id); 270 bool CallBaseEvent(const jerry_value_t func, const Event &event, const uint16_t id); 271 jerry_value_t *ConvertKeyEventInfo(const KeyEvent &event); 272 void ClearEventListener(const jerry_value_t args[], uint8_t argc); 273 #endif 274 JSValue GetRootAbilitySlice(); 275 JSValue CallWithRootAbilitySlice(JSValue func, JSValue args[], JSSize size); 276 JSValue CallWithRootAbilitySlice(JSValue func); 277 JSValue CreateWatcher(JSValue getter, JSHandler handler, JSValue options); 278 279 void ExpandImagePathMem(char *&imagePath, const int16_t dotPos, const int16_t suffixLen, const int16_t imagePathLen); 280 #if (OHOS_ACELITE_PRODUCT_WATCH == 1) 281 void CureImagePath(char *&imagePath); 282 #endif // OHOS_ACELITE_PRODUCT_WATCH 283 const char *ParseImageSrc(jerry_value_t source); 284 285 /** 286 * @brief ParseKeyIdFromJSString parse key ID from JS value 287 * @param str the input JS string value 288 * @return the key ID 289 */ 290 uint16_t ParseKeyIdFromJSString(const jerry_value_t str); 291 292 constexpr char PATH_PREFIX[] = "."; 293 constexpr char PATH_DEFAULT[] = "/"; 294 295 constexpr char PREFIX_HEX_COLOR[] = "#"; 296 constexpr char PREFIX_RGB_COLOR[] = "rgb"; 297 constexpr char PREFIX_RGBA_COLOR[] = "rgba"; 298 constexpr char LINEJOIN_MITER[] = "miter"; 299 constexpr char LINEJOIN_ROUND[] = "round"; 300 constexpr char LINEJOIN_BEVEL[] = "bevel"; 301 constexpr char LINECAP_BUTT[] = "butt"; 302 constexpr char LINECAP_SQUARE[] = "square"; 303 constexpr char LINECAP_ROUND[] = "round"; 304 constexpr uint8_t ALPHA_MAX = 255; 305 constexpr char BRACE_OPEN = '('; 306 constexpr char BRACE_CLOSE = ')'; 307 constexpr char DELIMITER[] = ","; 308 constexpr char RESOURCE_SEPARATOR = '/'; 309 310 bool StartWith(const char *sequence, const char *prefix); 311 bool IsHexColor(const char * const target); 312 bool IsRgbColor(const char * const target); 313 bool IsRgbaColor(const char * const target); 314 bool ParseHexColor(const char * const source, uint32_t &color, uint8_t &alpha); 315 bool ParseRgbaColor(const char * const source, uint32_t &color, uint8_t &alpha); 316 bool ParseColor(const char * const source, uint32_t &color, uint8_t &alpha); 317 bool CopyFontFamily(char *&destination, const char * const fontFamily, uint32_t fontFamilyNameLen = 0); 318 int8_t ParseLineCap(const char* lineCap); 319 int8_t ParseLineJoin(const char* lineJoin); 320 321 constexpr int16_t BUTT_VALUE = 0; 322 constexpr int16_t SQUARE_VALUE = 1; 323 constexpr int16_t ROUND_VALUE = 2; 324 constexpr int16_t LINEJOIN_MITER_VALUE = 0; 325 constexpr int16_t LINEJOIN_ROUND_VALUE = 1; 326 constexpr int16_t LINEJOIN_BEVEL_VALUE = 2; 327 328 #if (defined(_WIN32) || defined(_WIN64)) 329 constexpr char PATH_SEPARATOR = '\\'; 330 constexpr char PATH_RELATIVE_WHOLE[] = "\\..\\"; 331 constexpr char PATH_RELATIVE_PART[] = "..\\"; 332 constexpr char SRC_SUB_FOLDER_NAME[] = ""; 333 constexpr char JS_INDEX_FILE_PATH[] = "pages\\index\\index"; 334 #else 335 constexpr char PATH_SEPARATOR = '/'; 336 constexpr char PATH_RELATIVE_WHOLE[] = "/../"; 337 constexpr char PATH_RELATIVE_PART[] = "../"; 338 constexpr char SRC_SUB_FOLDER_NAME[] = ""; 339 constexpr char JS_INDEX_FILE_PATH[] = "pages/index/index"; 340 #endif 341 342 // for error definition 343 enum { 344 FAILED = -1, 345 SUCCESS = 0, 346 ERROR_BASE = 0x10000, 347 ERROR_INPUT_PARAMETER, 348 ERROR_MALLOC, 349 ERROR_SECURE_USE // use secure function error 350 }; 351 352 constexpr int VA_ARG_END_FLAG = -1; 353 constexpr int INVALID_PIXEL_VALUE = -1; 354 355 // switch on random path 356 #ifndef JS_PAGE_SPECIFIC 357 #define JS_PAGE_SPECIFIC 0 358 #endif 359 360 #if JS_PAGE_SPECIFIC 361 constexpr char servicePrefix[] = "pages"; 362 constexpr char serviceSuffix[] = "index"; 363 364 struct JSPageSpecific : public MemoryHeap { 365 char *jsIndexFilePath; // page path JSPageSpecificJSPageSpecific366 JSPageSpecific() : jsIndexFilePath(nullptr) {} 367 }; 368 369 #endif // JS_PAGE_SPECIFIC 370 } // namespace ACELite 371 } // namespace OHOS 372 #endif // OHOS_ACELITE_JS_FWK_COMMON_H 373