• 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 "client_helper.h"
21 #include "js_native_api.h"
22 #include "js_native_api_types.h"
23 
24 namespace OHOS {
25 namespace UpdateEngine {
26 enum ARG_NUM {
27     ARG_NUM_ONE = 1,
28     ARG_NUM_TWO,
29     ARG_NUM_THREE,
30     ARG_NUM_FOUR,
31     MAX_ARGC
32 };
33 
34 enum CALLBACK_POSITION {
35     CALLBACK_POSITION_ONE = 1,
36     CALLBACK_POSITION_TWO,
37     CALLBACK_POSITION_THREE,
38     CALLBACK_POSITION_FOUR,
39     CALLBACK_MAX_POSITION
40 };
41 
42 template<typename T>
UnwrapJsObject(napi_env env,napi_callback_info info)43 T *UnwrapJsObject(napi_env env, napi_callback_info info)
44 {
45     size_t argc = 1;
46     napi_value argv[1] = {0};
47     napi_value thisVar = nullptr;
48     void* data = nullptr;
49     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
50 
51     T *nativeObject = nullptr;
52     napi_unwrap(env, thisVar, (void**)&nativeObject);
53     return nativeObject;
54 }
55 
56 class NapiUtil {
57 public:
58     static int32_t GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue);
59     static int32_t GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value);
60 
61     static int32_t GetString(napi_env env, napi_value arg, const std::string &attrName, std::string &strValue);
62     static int32_t GetString(napi_env env, napi_value arg, std::string &strValue);
63     static int32_t SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string);
64 
65     static int32_t SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue);
66     static int32_t SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue);
67     static int32_t SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value);
68 
69     static ClientStatus IsTypeOf(napi_env env, napi_value arg, napi_valuetype type);
70 
71     static ClientStatus CreateReference(napi_env env, napi_value arg, uint32_t refcount, napi_ref &reference);
72 };
73 } // namespace UpdateEngine
74 } // namespace OHOS
75 #endif // UPDATE_NAPI_UTIL_H