• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "ans_template.h"
17 
18 namespace OHOS {
19 namespace NotificationNapi {
20 const int IS_TEMPLATE_MAX_PARA = 2;
21 
ParseParameters(const napi_env & env,const napi_callback_info & info,TemplateName & params)22 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, TemplateName& params)
23 {
24     ANS_LOGI("enter");
25 
26     size_t argc = IS_TEMPLATE_MAX_PARA;
27     napi_value argv[IS_TEMPLATE_MAX_PARA] = {nullptr};
28     napi_value thisVar = nullptr;
29     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
30     if (argc < IS_TEMPLATE_MAX_PARA - 1) {
31         ANS_LOGE("Wrong number of arguments");
32         return nullptr;
33     }
34 
35     napi_valuetype valuetype = napi_undefined;
36     // argv[0]: name: string
37     NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
38     if (valuetype != napi_string && valuetype != napi_number && valuetype != napi_boolean) {
39         ANS_LOGE("Wrong argument type. String number boolean expected.");
40         return nullptr;
41     }
42     if (valuetype == napi_string) {
43         char str[STR_MAX_SIZE] = {0};
44         size_t strLen = 0;
45         NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], str, STR_MAX_SIZE - 1, &strLen));
46         params.templateName = str;
47     } else if (valuetype == napi_number) {
48         int64_t number = 0;
49         NAPI_CALL(env, napi_get_value_int64(env, argv[0], &number));
50         params.templateName = std::to_string(number);
51     } else {
52         bool result = false;
53         NAPI_CALL(env, napi_get_value_bool(env, argv[0], &result));
54         params.templateName = std::to_string(result);
55     }
56 
57     // argv[1]: callback
58     if (argc >= IS_TEMPLATE_MAX_PARA) {
59         NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
60         if (valuetype != napi_function) {
61             ANS_LOGE("Callback is not function excute promise.");
62             return Common::NapiGetNull(env);
63         }
64         napi_create_reference(env, argv[1], 1, &params.callback);
65     }
66 
67     return Common::NapiGetNull(env);
68 }
69 
IsSupportTemplate(napi_env env,napi_callback_info info)70 napi_value IsSupportTemplate(napi_env env, napi_callback_info info)
71 {
72     ANS_LOGI("enter");
73 
74     TemplateName params;
75     if (ParseParameters(env, info, params) == nullptr) {
76         return Common::NapiGetUndefined(env);
77     }
78 
79     AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow)
80         AsyncCallbackInfoTemplate {.env = env, .asyncWork = nullptr, .params = params};
81     if (!asyncCallbackinfo) {
82         return Common::JSParaError(env, params.callback);
83     }
84     napi_value promise = nullptr;
85     Common::PaddingCallbackPromiseInfo(env, params.callback, asyncCallbackinfo->info, promise);
86 
87     napi_value resourceName = nullptr;
88     napi_create_string_latin1(env, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName);
89     // Asynchronous function call
90     napi_create_async_work(env,
91         nullptr,
92         resourceName,
93         [](napi_env env, void *data) {
94             ANS_LOGI("IsSupportTemplate napi_create_async_work start");
95             AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast<AsyncCallbackInfoTemplate *>(data);
96 
97             if (asyncCallbackinfo) {
98                 asyncCallbackinfo->info.errorCode = NotificationHelper::IsSupportTemplate(
99                     asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support);
100             }
101         },
102         [](napi_env env, napi_status status, void *data) {
103             ANS_LOGI("IsSupportTemplate napi_create_async_work end");
104             AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast<AsyncCallbackInfoTemplate *>(data);
105             if (asyncCallbackinfo) {
106                 napi_value result = nullptr;
107                 napi_get_boolean(env, asyncCallbackinfo->params.support, &result);
108                 Common::ReturnCallbackPromise(env, asyncCallbackinfo->info, result);
109                 if (asyncCallbackinfo->info.callback != nullptr) {
110                     napi_delete_reference(env, asyncCallbackinfo->info.callback);
111                 }
112                 napi_delete_async_work(env, asyncCallbackinfo->asyncWork);
113                 delete asyncCallbackinfo;
114                 asyncCallbackinfo = nullptr;
115             }
116         },
117         (void *)asyncCallbackinfo,
118         &asyncCallbackinfo->asyncWork);
119 
120     napi_status status = napi_queue_async_work_with_qos(env, asyncCallbackinfo->asyncWork, napi_qos_user_initiated);
121     if (status != napi_ok) {
122         ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
123         if (asyncCallbackinfo->info.callback != nullptr) {
124             napi_delete_reference(env, asyncCallbackinfo->info.callback);
125         }
126         napi_delete_async_work(env, asyncCallbackinfo->asyncWork);
127         delete asyncCallbackinfo;
128         asyncCallbackinfo = nullptr;
129         return Common::JSParaError(env, params.callback);
130     }
131 
132     if (asyncCallbackinfo->info.isCallback) {
133         return Common::NapiGetNull(env);
134     } else {
135         return promise;
136     }
137 }
138 }  // namespace NotificationNapi
139 }  // namespace OHOS