• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 UPDATE_NAPI_UTIL_H
17 #define UPDATE_NAPI_UTIL_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "js_native_api.h"
23 #include "js_native_api_types.h"
24 
25 #include "client_helper.h"
26 
27 namespace OHOS {
28 namespace UpdateEngine {
29 enum ARG_NUM {
30     ARG_NUM_ONE = 1,
31     ARG_NUM_TWO,
32     ARG_NUM_THREE,
33     ARG_NUM_FOUR,
34     MAX_ARGC
35 };
36 
37 enum CALLBACK_POSITION {
38     CALLBACK_POSITION_ONE = 1,
39     CALLBACK_POSITION_TWO,
40     CALLBACK_POSITION_THREE,
41     CALLBACK_POSITION_FOUR,
42     CALLBACK_MAX_POSITION
43 };
44 
45 template<typename T>
UnwrapJsObject(napi_env env,napi_callback_info info)46 T *UnwrapJsObject(napi_env env, napi_callback_info info)
47 {
48     size_t argc = 1;
49     napi_value argv[1] = {0};
50     napi_value thisVar = nullptr;
51     void* data = nullptr;
52     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
53 
54     T *nativeObject = nullptr;
55     napi_unwrap(env, thisVar, (void**)&nativeObject);
56     return nativeObject;
57 }
58 
59 class NapiUtil {
60 public:
61     static int32_t GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue);
62     static int32_t GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value);
63 
64     static int32_t GetString(napi_env env, napi_value arg, const std::string &attrName, std::string &strValue);
65     static int32_t GetString(napi_env env, napi_value arg, std::string &strValue);
66     static int32_t SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string);
67 
68     static int32_t SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue);
69     static int32_t SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue);
70     static int32_t SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value);
71 
72     static ClientStatus IsTypeOf(napi_env env, napi_value arg, napi_valuetype type);
73 
74     static ClientStatus CreateReference(napi_env env, napi_value arg, uint32_t refcount, napi_ref &reference);
75     static napi_value CreateUint32(napi_env env, uint32_t code);
76     static napi_value CreateStringUtf8(napi_env env, const std::string &str);
77 
78     static void CreateProperty(napi_env env, napi_value exports, const std::string &name,
79                                const std::vector<std::pair<std::string, napi_value>> &properties);
80 private:
81     static napi_value CreateObject(napi_env env);
82     static void DefineProperties(napi_env env, napi_value object,
83                                  const std::vector<std::pair<std::string, napi_value>> &properties);
84 };
85 } // namespace UpdateEngine
86 } // namespace OHOS
87 #endif // UPDATE_NAPI_UTIL_H