• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H
17 #define FOUNDATION_ACE_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "ui/base/macros.h"
25 #include "frameworks/core/components/common/properties/color.h"
26 #include "frameworks/core/components/theme/theme_constants.h"
27 
28 namespace OHOS::Ace {
29 class ACE_FORCE_EXPORT NapiAsyncEvent {
30 public:
31     NapiAsyncEvent(napi_env env, napi_value callback);
32     ~NapiAsyncEvent();
33     napi_value Call(int32_t argc, napi_value* argv);
34     napi_env GetEnv();
35 
36 private:
37     napi_env env_;
38     napi_ref ref_;
39 };
40 
41 class ACE_FORCE_EXPORT ExtNapiUtils {
42 public:
43     static napi_value CreateInt32(napi_env env, int32_t code);
44     static napi_value CreateObject(napi_env env);
45     static napi_value CreateDouble(napi_env env, double value);
46     static napi_value CreateFunction(napi_env env,
47                                         const char* utf8name, size_t length,
48                                         napi_callback cb,
49                                         void* data);
50     static int32_t GetCInt32(napi_env env, napi_value value);
51     static int64_t GetCInt64(napi_env env, napi_value value);
52     static double GetDouble(napi_env env, napi_value value);
53     static napi_value CreateNull(napi_env env);
54     static bool GetBool(napi_env env, napi_value value);
55     static napi_valuetype GetValueType(napi_env env, napi_value value);
56     static std::string GetStringFromValueUtf8(napi_env env, napi_value value);
57     static bool CheckTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType);
58     static void SetNamedProperty(napi_env env, napi_value object, const std::string& propertyName, napi_value value);
59     static bool ParseColorFromResource(napi_env env, napi_value value, Color& result);
60     static bool ParseColor(napi_env env, napi_value value, Color& result);
61     static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string& propertyName);
62     static bool IsArray(napi_env env, napi_value value);
63     static napi_value CreateUndefined(napi_env env);
64     static RefPtr<ThemeConstants> GetThemeConstants(napi_env env, napi_value value);
65     static std::unique_ptr<JsonValue> PutJsonValue(napi_env env, napi_value value, std::string& key);
66     static bool ParseLengthMetrics(napi_env env, napi_value param, CalcDimension& result);
67     static bool ParseColorMetrics(napi_env env, napi_value param, Color& result);
68 };
69 } // namespace OHOS::Ace
70 #endif // FOUNDATION_ACE_COMPONENTEXT_EXT_COMMON_EXT_NAPI_UTILS_H