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 #include "napi_domain_server_manager.h"
17
18 #include <uv.h>
19 #include "account_log_wrapper.h"
20 #include "domain_account_client.h"
21 #include "napi_account_common.h"
22 #include "napi_account_error.h"
23 #include "napi_common.h"
24 #include "napi_domain_account_common.h"
25 #include "string_wrapper.h"
26
27 namespace OHOS {
28 namespace AccountJsKit {
29 using namespace OHOS::AccountSA;
30 namespace {
31 const size_t ARG_SIZE_ONE = 1;
32 }
33
Init(napi_env env,napi_value exports)34 napi_value NapiDomainServerConfigManager::Init(napi_env env, napi_value exports)
35 {
36 napi_property_descriptor clzDes[] = {
37 DECLARE_NAPI_STATIC_FUNCTION("addServerConfig", AddServerConfig),
38 DECLARE_NAPI_STATIC_FUNCTION("removeServerConfig", RemoveServerConfig),
39 DECLARE_NAPI_STATIC_FUNCTION("getAccountServerConfig", GetAccountServerConfig),
40 DECLARE_NAPI_FUNCTION("addServerConfig", AddServerConfig),
41 DECLARE_NAPI_FUNCTION("removeServerConfig", RemoveServerConfig),
42 DECLARE_NAPI_FUNCTION("getAccountServerConfig", GetAccountServerConfig),
43 };
44 napi_value cons;
45 NAPI_CALL(env, napi_define_class(env, "DomainServerConfigManager", NAPI_AUTO_LENGTH, JsConstructor,
46 nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
47 NAPI_CALL(env, napi_set_named_property(env, exports, "DomainServerConfigManager", cons));
48 return exports;
49 }
50
JsConstructor(napi_env env,napi_callback_info info)51 napi_value NapiDomainServerConfigManager::JsConstructor(napi_env env, napi_callback_info info)
52 {
53 if (!IsSystemApp(env)) {
54 return nullptr;
55 }
56 napi_value thisVar = nullptr;
57 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
58 return thisVar;
59 }
60
ParseContextFoAddServerConfig(napi_env env,napi_callback_info info,AddServerConfigAsyncContext * context)61 static bool ParseContextFoAddServerConfig(
62 napi_env env, napi_callback_info info, AddServerConfigAsyncContext *context)
63 {
64 size_t argc = ARG_SIZE_ONE;
65 napi_value argv[ARG_SIZE_ONE] = {0};
66 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
67 if (argc < ARG_SIZE_ONE) {
68 ACCOUNT_LOGE("The parameter of number should be at least one.");
69 return false;
70 }
71 if (!JsObjectToNativeString(env, argv[0], context->parameters)) {
72 ACCOUNT_LOGE("Get parameters failed.");
73 return false;
74 }
75 return true;
76 }
77
AddServerConfigExecuteCB(napi_env env,void * data)78 static void AddServerConfigExecuteCB(napi_env env, void *data)
79 {
80 AddServerConfigAsyncContext *asyncContext = reinterpret_cast<AddServerConfigAsyncContext *>(data);
81 if (asyncContext == nullptr) {
82 ACCOUNT_LOGE("In AddServerConfigExecuteCB asyncContext is nullptr.");
83 return;
84 }
85 asyncContext->errCode = DomainAccountClient::GetInstance().AddServerConfig(
86 asyncContext->parameters, asyncContext->domainServerConfig);
87 }
88
ServerConfigToJs(napi_env env,const DomainServerConfig & config,napi_value & configJs)89 static void ServerConfigToJs(napi_env env, const DomainServerConfig& config, napi_value &configJs)
90 {
91 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &configJs));
92 napi_value idJs;
93 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, config.id_.c_str(), NAPI_AUTO_LENGTH, &idJs));
94 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, configJs, "id", idJs));
95 napi_value parametersJs = NativeStringToJsObject(env, config.parameters_);
96 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, configJs, "parameters", parametersJs));
97 napi_value domainJs;
98 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, config.domain_.c_str(), NAPI_AUTO_LENGTH, &domainJs));
99 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, configJs, "domain", domainJs));
100 }
101
AddServerConfigCompletedCB(napi_env env,napi_status status,void * data)102 static void AddServerConfigCompletedCB(napi_env env, napi_status status, void *data)
103 {
104 ACCOUNT_LOGD("napi_create_async_work complete");
105 AddServerConfigAsyncContext *asyncContext = reinterpret_cast<AddServerConfigAsyncContext *>(data);
106 if (asyncContext == nullptr) {
107 ACCOUNT_LOGE("In AddServerConfigCompletedCB asyncContext is nullptr.");
108 return;
109 }
110 napi_value errJs = nullptr;
111 napi_value dataJs = nullptr;
112 if (asyncContext->errCode == 0) {
113 errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
114 ServerConfigToJs(env, asyncContext->domainServerConfig, dataJs);
115 } else {
116 errJs = GenerateBusinessError(env, asyncContext->errCode);
117 napi_get_null(env, &dataJs);
118 }
119 ReturnCallbackOrPromise(env, asyncContext, errJs, dataJs);
120 delete asyncContext;
121 }
122
AddServerConfig(napi_env env,napi_callback_info info)123 napi_value NapiDomainServerConfigManager::AddServerConfig(napi_env env, napi_callback_info info)
124 {
125 auto context = std::make_unique<AddServerConfigAsyncContext>(env);
126
127 if (!ParseContextFoAddServerConfig(env, info, context.get())) {
128 std::string errMsg = "The type of parameter is error.";
129 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
130 return nullptr;
131 }
132 napi_value result = nullptr;
133 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
134 napi_value resource = nullptr;
135 NAPI_CALL(env, napi_create_string_utf8(env, "AddServerConfig", NAPI_AUTO_LENGTH, &resource));
136 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, AddServerConfigExecuteCB, AddServerConfigCompletedCB,
137 reinterpret_cast<void *>(context.get()), &context->work));
138 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_default));
139 context.release();
140 return result;
141 }
142
ParseContextFoRemoveServerConfig(napi_env env,napi_callback_info info,RemoveServerConfigAsyncContext * context)143 static bool ParseContextFoRemoveServerConfig(
144 napi_env env, napi_callback_info info, RemoveServerConfigAsyncContext *context)
145 {
146 size_t argc = ARG_SIZE_ONE;
147 napi_value argv[ARG_SIZE_ONE] = {0};
148 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
149 if (argc < ARG_SIZE_ONE) {
150 ACCOUNT_LOGE("The parameter of number should be at least one.");
151 return false;
152 }
153 if (!GetStringProperty(env, argv[0], context->configId)) {
154 ACCOUNT_LOGE("Get removeServerconfig's configId failed");
155 return false;
156 }
157 return true;
158 }
159
RemoveServerConfigExecuteCB(napi_env env,void * data)160 static void RemoveServerConfigExecuteCB(napi_env env, void *data)
161 {
162 RemoveServerConfigAsyncContext *asyncContext = reinterpret_cast<RemoveServerConfigAsyncContext *>(data);
163 if (asyncContext == nullptr) {
164 ACCOUNT_LOGE("In RemoveServerConfigExecuteCB asyncContext is nullptr.");
165 return;
166 }
167 asyncContext->errCode = DomainAccountClient::GetInstance().RemoveServerConfig(asyncContext->configId);
168 }
169
RemoveServerConfigCompletedCB(napi_env env,napi_status status,void * data)170 static void RemoveServerConfigCompletedCB(napi_env env, napi_status status, void *data)
171 {
172 RemoveServerConfigAsyncContext *asyncContext = reinterpret_cast<RemoveServerConfigAsyncContext *>(data);
173 if (asyncContext == nullptr) {
174 ACCOUNT_LOGE("AsyncContext is nullptr.");
175 return;
176 }
177 napi_value errJs = nullptr;
178 napi_value dataJs = nullptr;
179 if (asyncContext->errCode == 0) {
180 napi_get_null(env, &errJs);
181 } else {
182 errJs = GenerateBusinessError(env, asyncContext->errCode);
183 }
184 napi_get_null(env, &dataJs);
185 ReturnCallbackOrPromise(env, asyncContext, errJs, dataJs);
186 delete asyncContext;
187 }
188
RemoveServerConfig(napi_env env,napi_callback_info info)189 napi_value NapiDomainServerConfigManager::RemoveServerConfig(napi_env env, napi_callback_info info)
190 {
191 auto context = std::make_unique<RemoveServerConfigAsyncContext>(env);
192 if (!ParseContextFoRemoveServerConfig(env, info, context.get())) {
193 std::string errMsg = "The type of parameter is error.";
194 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
195 return nullptr;
196 }
197 napi_value result = nullptr;
198 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
199 napi_value resource = nullptr;
200 NAPI_CALL(env, napi_create_string_utf8(env, "RemoveServerConfig", NAPI_AUTO_LENGTH, &resource));
201 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, RemoveServerConfigExecuteCB,
202 RemoveServerConfigCompletedCB, reinterpret_cast<void *>(context.get()), &context->work));
203 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_default));
204 context.release();
205 return result;
206 }
207
ParseContextFoGetAccountServerConfig(napi_env env,napi_callback_info info,GetAccountServerConfigAsyncContext * context)208 static bool ParseContextFoGetAccountServerConfig(
209 napi_env env, napi_callback_info info, GetAccountServerConfigAsyncContext *context)
210 {
211 size_t argc = ARG_SIZE_ONE;
212 napi_value argv[ARG_SIZE_ONE] = {0};
213 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
214 if (argc < ARG_SIZE_ONE) {
215 ACCOUNT_LOGE("The parameter of number should be at least one.");
216 return false;
217 }
218 if (!ParseDomainAccountInfo(env, argv[0], context->domainAccountInfo)) {
219 ACCOUNT_LOGE("Get domainInfo failed");
220 return false;
221 }
222 return true;
223 }
224
GetAccountServerConfigExecuteCB(napi_env env,void * data)225 static void GetAccountServerConfigExecuteCB(napi_env env, void *data)
226 {
227 GetAccountServerConfigAsyncContext *asyncContext = reinterpret_cast<GetAccountServerConfigAsyncContext *>(data);
228 if (asyncContext == nullptr) {
229 ACCOUNT_LOGE("In GetAccountServerConfigExecuteCB asyncContext is nullptr.");
230 return;
231 }
232 asyncContext->errCode = DomainAccountClient::GetInstance().GetAccountServerConfig(asyncContext->domainAccountInfo,
233 asyncContext->domainServerConfig);
234 }
235
236
GetAccountServerConfigCompletedCB(napi_env env,napi_status status,void * data)237 static void GetAccountServerConfigCompletedCB(napi_env env, napi_status status, void *data)
238 {
239 GetAccountServerConfigAsyncContext *asyncContext = reinterpret_cast<GetAccountServerConfigAsyncContext *>(data);
240 if (asyncContext == nullptr) {
241 ACCOUNT_LOGE("AsyncContext is nullptr.");
242 return;
243 }
244 napi_value errJs = nullptr;
245 napi_value dataJs = nullptr;
246 if (asyncContext->errCode == 0) {
247 errJs = GenerateBusinessSuccess(env, asyncContext->throwErr);
248 ServerConfigToJs(env, asyncContext->domainServerConfig, dataJs);
249 } else {
250 errJs = GenerateBusinessError(env, asyncContext->errCode);
251 napi_get_null(env, &dataJs);
252 }
253 ReturnCallbackOrPromise(env, asyncContext, errJs, dataJs);
254 delete asyncContext;
255 }
256
GetAccountServerConfig(napi_env env,napi_callback_info info)257 napi_value NapiDomainServerConfigManager::GetAccountServerConfig(napi_env env, napi_callback_info info)
258 {
259 auto context = std::make_unique<GetAccountServerConfigAsyncContext>(env);
260 if (!ParseContextFoGetAccountServerConfig(env, info, context.get())) {
261 std::string errMsg = "The type of parameter is error.";
262 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
263 return nullptr;
264 }
265 napi_value result = nullptr;
266 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
267 napi_value resource = nullptr;
268 NAPI_CALL(env, napi_create_string_utf8(env, "GetAccountServerConfig", NAPI_AUTO_LENGTH, &resource));
269 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetAccountServerConfigExecuteCB,
270 GetAccountServerConfigCompletedCB, reinterpret_cast<void *>(context.get()), &context->work));
271 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_default));
272 context.release();
273 return result;
274 }
275
276 } // namespace AccountJsKit
277 } // namespace OHOS
278