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 #include "napi_util.h"
17
18 #include <string>
19
20 #include "napi/native_common.h"
21
22 #include "update_define.h"
23
24 namespace OHOS {
25 namespace UpdateEngine {
26 constexpr int32_t STRING_MAX_LENGTH = 200;
27
GetInt32(napi_env env,napi_value arg,const std::string & attrName,int32_t & intValue)28 int32_t NapiUtil::GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue)
29 {
30 bool result = false;
31 napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result);
32 if (result && (status == napi_ok)) {
33 napi_value value = nullptr;
34 napi_get_named_property(env, arg, attrName.c_str(), &value);
35 napi_get_value_int32(env, value, &intValue);
36 }
37 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
38 }
39
GetBool(napi_env env,napi_value arg,const std::string & attrName,bool & value)40 int32_t NapiUtil::GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value)
41 {
42 bool result = false;
43 napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result);
44 if (result && (status == napi_ok)) {
45 napi_value obj = nullptr;
46 napi_get_named_property(env, arg, attrName.c_str(), &obj);
47 napi_get_value_bool(env, obj, &value);
48 }
49 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
50 }
51
GetString(napi_env env,napi_value arg,const std::string & attrName,std::string & strValue)52 int32_t NapiUtil::GetString(napi_env env, napi_value arg, const std::string &attrName, std::string &strValue)
53 {
54 bool result = false;
55 napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result);
56 if (result && (status == napi_ok)) {
57 napi_value value = nullptr;
58 napi_get_named_property(env, arg, attrName.c_str(), &value);
59 return GetString(env, value, strValue);
60 }
61 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
62 }
63
GetString(napi_env env,napi_value arg,std::string & strValue)64 int32_t NapiUtil::GetString(napi_env env, napi_value arg, std::string &strValue)
65 {
66 napi_valuetype valuetype;
67 napi_status status = napi_typeof(env, arg, &valuetype);
68 PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_FAIL), "Failed to napi_typeof");
69 PARAM_CHECK(valuetype == napi_string, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "Invalid type");
70
71 std::vector<char> buff(STRING_MAX_LENGTH);
72 size_t copied;
73 status = napi_get_value_string_utf8(env, arg, (char*)buff.data(), STRING_MAX_LENGTH, &copied);
74 PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "Error get string");
75 strValue.assign(buff.data(), copied);
76 return CAST_INT(ClientStatus::CLIENT_SUCCESS);
77 }
78
SetString(napi_env env,napi_value arg,const std::string & attrName,const std::string & string)79 int32_t NapiUtil::SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string)
80 {
81 napi_value value = nullptr;
82 napi_create_string_utf8(env, string.c_str(), string.length(), &value);
83 napi_set_named_property(env, arg, attrName.c_str(), value);
84 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
85 }
86
SetInt32(napi_env env,napi_value arg,const std::string & attrName,int32_t intValue)87 int32_t NapiUtil::SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue)
88 {
89 napi_value infoStatus = nullptr;
90 napi_create_int32(env, intValue, &infoStatus);
91 napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
92 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
93 }
94
SetInt64(napi_env env,napi_value arg,const std::string & attrName,int64_t intValue)95 int32_t NapiUtil::SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue)
96 {
97 napi_value infoStatus = nullptr;
98 napi_create_int64(env, intValue, &infoStatus);
99 napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
100 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
101 }
102
SetBool(napi_env env,napi_value arg,const std::string & attrName,bool value)103 int32_t NapiUtil::SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value)
104 {
105 napi_value infoStatus = nullptr;
106 napi_create_int32(env, value, &infoStatus);
107 napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
108 return static_cast<int32_t>(ClientStatus::CLIENT_SUCCESS);
109 }
110
IsTypeOf(napi_env env,napi_value arg,napi_valuetype type)111 ClientStatus NapiUtil::IsTypeOf(napi_env env, napi_value arg, napi_valuetype type)
112 {
113 napi_valuetype valueType;
114 napi_status status = napi_typeof(env, arg, &valueType);
115 PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL, "Failed to napi_typeof");
116 PARAM_CHECK(valueType == type, return ClientStatus::CLIENT_INVALID_TYPE, "Not same napi type");
117 return ClientStatus::CLIENT_SUCCESS;
118 }
119
CreateReference(napi_env env,napi_value arg,uint32_t refcount,napi_ref & reference)120 ClientStatus NapiUtil::CreateReference(napi_env env, napi_value arg, uint32_t refcount, napi_ref &reference)
121 {
122 napi_status status = napi_create_reference(env, arg, refcount, &reference);
123 PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL, "Failed to create reference");
124 return ClientStatus::CLIENT_SUCCESS;
125 }
126
CreateUint32(napi_env env,uint32_t code)127 napi_value NapiUtil::CreateUint32(napi_env env, uint32_t code)
128 {
129 napi_value value = nullptr;
130 if (napi_create_uint32(env, code, &value) != napi_ok) {
131 return nullptr;
132 }
133 return value;
134 }
135
CreateStringUtf8(napi_env env,const std::string & str)136 napi_value NapiUtil::CreateStringUtf8(napi_env env, const std::string &str)
137 {
138 napi_value value = nullptr;
139 if (napi_create_string_utf8(env, str.c_str(), strlen(str.c_str()), &value) != napi_ok) {
140 return nullptr;
141 }
142 return value;
143 }
144
CreateObject(napi_env env)145 napi_value NapiUtil::CreateObject(napi_env env)
146 {
147 napi_value object = nullptr;
148 napi_status status = napi_create_object(env, &object);
149 if (status != napi_ok) {
150 CLIENT_LOGE("CreateObject, napi_create_object fail");
151 }
152 return object;
153 }
154
CreateProperty(napi_env env,napi_value exports,const std::string & name,const std::vector<std::pair<std::string,napi_value>> & properties)155 void NapiUtil::CreateProperty(napi_env env, napi_value exports, const std::string &name,
156 const std::vector<std::pair<std::string, napi_value>> &properties)
157 {
158 napi_value objectValue = CreateObject(env);
159 if (objectValue == nullptr) {
160 return;
161 }
162 DefineProperties(env, objectValue, properties);
163 napi_status status = napi_set_named_property(env, exports, name.c_str(), objectValue);
164 if (status != napi_ok) {
165 CLIENT_LOGE("CreateProperty, napi_set_named_property fail");
166 }
167 }
168
DefineProperties(napi_env env,napi_value object,const std::vector<std::pair<std::string,napi_value>> & properties)169 void NapiUtil::DefineProperties(napi_env env, napi_value object,
170 const std::vector<std::pair<std::string, napi_value>> &properties)
171 {
172 size_t size = properties.size();
173 napi_property_descriptor descriptors[size];
174 for (size_t pos = 0; pos < size; pos++) {
175 if (properties[pos].first.empty()) {
176 continue;
177 }
178 descriptors[pos] = DECLARE_NAPI_STATIC_PROPERTY(properties[pos].first.c_str(), properties[pos].second);
179 }
180 napi_status status = napi_define_properties(env, object, size, descriptors);
181 if (status != napi_ok) {
182 CLIENT_LOGE("DefineProperties, napi_define_properties fail");
183 }
184 }
185 } // namespace UpdateEngine
186 } // namespace OHOS
187