• 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 #include "napi_sync_config.h"
17 #include "common.h"
18 #include "napi_common_util.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace NotificationNapi {
24 namespace {
25     constexpr int8_t SETADDITION_CONFIG_NUM = 2;
26     constexpr char KEY_NAME[] = "AGGREGATE_CONFIG";
27     constexpr char RING_LIST_KEY_NAME[] = "RING_TRUSTLIST_PKG";
28     constexpr char CTRL_LIST_KEY_NAME[] = "NOTIFICATION_CTL_LIST_PKG";
29     constexpr char CAMPAIGN_NOTIFICATION_SWITCH_LIST_PKG[] = "CAMPAIGN_NOTIFICATION_SWITCH_LIST_PKG";
30 }
31 
32 struct ConfigParams {
33     std::string key = "";
34     std::string value = "";
35     napi_ref callback = nullptr;
36 };
37 
38 struct AsyncCallbackInfoConfig {
39     napi_env env = nullptr;
40     napi_async_work asyncWork = nullptr;
41     ConfigParams params;
42     CallbackPromiseInfo info;
43 };
44 
ParseParameters(const napi_env & env,const napi_callback_info & info,ConfigParams & params)45 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ConfigParams &params)
46 {
47     ANS_LOGD("called");
48 
49     size_t argc = SETADDITION_CONFIG_NUM;
50     napi_value argv[SETADDITION_CONFIG_NUM] = {nullptr};
51     napi_value thisVar = nullptr;
52     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
53     if (argc < SETADDITION_CONFIG_NUM) {
54         ANS_LOGE("Wrong number of arguments.");
55         Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED);
56         return nullptr;
57     }
58 
59     napi_valuetype valuetype = napi_undefined;
60     // argv[0]: key: string
61     NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
62     if (valuetype != napi_string) {
63         ANS_LOGE("Argument type error. String expected.");
64         std::string msg = "Incorrect parameter types.The type of param must be string.";
65         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
66         return nullptr;
67     }
68     char keyStr[STR_MAX_SIZE] = {0};
69     size_t keyStrLen = 0;
70     NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], keyStr, STR_MAX_SIZE - 1, &keyStrLen));
71     params.key = keyStr;
72     if (std::strlen(keyStr) == 0 ||
73         (strcmp(keyStr, KEY_NAME) != 0 && strcmp(keyStr, RING_LIST_KEY_NAME) != 0
74             && strcmp(keyStr, CTRL_LIST_KEY_NAME) != 0 && strcmp(keyStr, CAMPAIGN_NOTIFICATION_SWITCH_LIST_PKG) != 0)) {
75         ANS_LOGE("Argument type error. String expected.");
76         std::string msg = "Incorrect parameter types.The type of param must be string.";
77         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
78         return nullptr;
79     }
80 
81     // argv[1]: value: string
82     NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
83     if (valuetype != napi_string) {
84         ANS_LOGE("Argument type error. String expected.");
85         std::string msg = "Incorrect parameter types.The type of param must be string.";
86         Common::NapiThrow(env, ERROR_PARAM_INVALID, msg);
87         return nullptr;
88     }
89 
90     char valueStr[LONG_LONG_STR_MAX_SIZE] = {0};
91     size_t valStrLen = 0;
92     NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], valueStr, LONG_LONG_STR_MAX_SIZE - 1, &valStrLen));
93     params.value = valueStr;
94 
95     return Common::NapiGetNull(env);
96 }
97 
AsyncSetConfigComplete(napi_env env,napi_status status,void * data)98 void AsyncSetConfigComplete(napi_env env, napi_status status, void *data)
99 {
100     ANS_LOGD("start");
101     AsyncCallbackInfoConfig *asynccallbackinfo = static_cast<AsyncCallbackInfoConfig *>(data);
102     if (asynccallbackinfo) {
103         napi_value result = nullptr;
104         napi_create_int32(env, asynccallbackinfo->info.errorCode, &result);
105         Common::CreateReturnValue(env, asynccallbackinfo->info, result);
106         if (asynccallbackinfo->info.callback != nullptr) {
107             ANS_LOGD("null callback");
108             napi_delete_reference(env, asynccallbackinfo->info.callback);
109         }
110         napi_delete_async_work(env, asynccallbackinfo->asyncWork);
111         delete asynccallbackinfo;
112         asynccallbackinfo = nullptr;
113     }
114     ANS_LOGD("end");
115 }
NapiSetAdditionConfig(napi_env env,napi_callback_info info)116 napi_value NapiSetAdditionConfig(napi_env env, napi_callback_info info)
117 {
118     ANS_LOGD("called");
119     ConfigParams paras {};
120     if (ParseParameters(env, info, paras) == nullptr) {
121         Common::NapiThrow(env, ERROR_PARAM_INVALID);
122         return Common::NapiGetUndefined(env);
123     }
124 
125     AsyncCallbackInfoConfig *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoConfig {
126         .env = env,
127         .asyncWork = nullptr,
128         .params = paras
129     };
130     if (!asynccallbackinfo) {
131         Common::NapiThrow(env, ERROR_INTERNAL_ERROR);
132         return Common::JSParaError(env, paras.callback);
133     }
134     napi_value promise = nullptr;
135     Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
136 
137     napi_value resourceName = nullptr;
138     napi_create_string_latin1(env, "setAdditionConfig", NAPI_AUTO_LENGTH, &resourceName);
139     napi_create_async_work(env,
140         nullptr,
141         resourceName,
142         [](napi_env env, void *data) {
143             ANS_LOGI("NapiSetAdditionConfig work excute.");
144             AsyncCallbackInfoConfig *asynccallbackinfo = static_cast<AsyncCallbackInfoConfig *>(data);
145             if (asynccallbackinfo) {
146                     asynccallbackinfo->info.errorCode = NotificationHelper::SetAdditionConfig(
147                         asynccallbackinfo->params.key, asynccallbackinfo->params.value);
148             }
149         }, AsyncSetConfigComplete, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork);
150 
151     napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated);
152 
153     if (asynccallbackinfo->info.isCallback) {
154         ANS_LOGD("null isCallback");
155         return Common::NapiGetNull(env);
156     } else {
157         return promise;
158     }
159 }
160 }  // namespace NotificationNapi
161 }  // namespace OHOS
162