1 /* 2 * Copyright (c) 2023 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAPI_COMMON_UTILS_COMMON_NAPI_UTILS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAPI_COMMON_UTILS_COMMON_NAPI_UTILS_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 #include "base/utils/macros.h" 25 #include "core/components/common/properties/color.h" 26 #include "core/components/theme/theme_constants.h" 27 #include "core/gestures/gesture_info.h" 28 29 namespace OHOS::Ace { 30 class ACE_FORCE_EXPORT NapiAsyncEvnet { 31 public: 32 NapiAsyncEvnet(napi_env env, napi_value callback); 33 ~NapiAsyncEvnet(); 34 napi_value Call(int32_t argc, napi_value* argv); 35 napi_env GetEnv(); 36 37 private: 38 napi_env env_; 39 napi_ref ref_; 40 }; 41 42 class ACE_FORCE_EXPORT CommonNapiUtils { 43 public: 44 static napi_value CreateInt32(napi_env env, int32_t code); 45 static int32_t GetCInt32(napi_value value, napi_env env); 46 static int64_t GetCInt64(napi_value value, napi_env env); 47 static napi_value CreateBoolean(napi_env env, bool value); 48 static bool GetBool(napi_env env, napi_value value); 49 static napi_value CreateDouble(napi_env env, double value); 50 static double GetDouble(napi_env env, napi_value value); 51 static size_t GetCString(napi_value value, napi_env env, char* buf, size_t size); 52 static napi_value CreateStringUtf8(napi_env env, const std::string& str); 53 static std::string GetStringFromValueUtf8(napi_env env, napi_value value); 54 static napi_value CreateNull(napi_env env); 55 static napi_value CreateUndefined(napi_env env); 56 static napi_valuetype GetValueType(napi_env env, napi_value value); 57 static napi_value CreateObject(napi_env env); 58 static void DefineProperties( 59 napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor>& properties); 60 static void DefineClass(napi_env env, napi_value exports, 61 const std::initializer_list<napi_property_descriptor>& properties, const std::string& className); 62 static void SetNamedProperty(napi_env env, napi_value object, const std::string& propertyName, napi_value value); 63 static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string& propertyName); 64 static bool HasNamedProperty(napi_env env, napi_value object, const std::string& propertyName); 65 static bool GetPropertyNames(napi_env env, napi_value object, std::vector<std::string>& nameList); 66 static bool IsArray(napi_env env, napi_value value); 67 static void SetSelementToArray(napi_env env, napi_value array, int index, napi_value value); 68 static napi_value CreateArray(napi_env env); 69 static bool ParseColorFromResource(napi_env env, napi_value value, Color& result); 70 static bool ParseColor(napi_env env, napi_value value, Color& result); 71 static std::unique_ptr<JsonValue> PutJsonValue(napi_env env, napi_value value, std::string& key); 72 static RefPtr<ThemeConstants> GetThemeConstants(napi_env env, napi_value value); 73 static bool GetDimensionResult(napi_env env, napi_value value, CalcDimension& result); 74 }; 75 } // namespace OHOS::Ace 76 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAPI_COMMON_UTILS_COMMON_NAPI_UTILS_H 77