• 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 "napi_template.h"
17 
18 #include "ans_inner_errors.h"
19 #include "ans_template.h"
20 
21 namespace OHOS {
22 namespace NotificationNapi {
NapiIsSupportTemplate(napi_env env,napi_callback_info info)23 napi_value NapiIsSupportTemplate(napi_env env, napi_callback_info info)
24 {
25     ANS_LOGD("called");
26     TemplateName params;
27     if (ParseParameters(env, info, params) == nullptr) {
28         Common::NapiThrow(env, ERROR_PARAM_INVALID);
29         return Common::NapiGetUndefined(env);
30     }
31 
32     AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow)
33         AsyncCallbackInfoTemplate {.env = env, .asyncWork = nullptr, .params = params};
34     if (!asyncCallbackinfo) {
35         return Common::JSParaError(env, params.callback);
36     }
37     napi_value promise = nullptr;
38     Common::PaddingCallbackPromiseInfo(env, params.callback, asyncCallbackinfo->info, promise);
39 
40     napi_value resourceName = nullptr;
41     napi_create_string_latin1(env, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName);
42     // Asynchronous function call
43     napi_create_async_work(env,
44         nullptr,
45         resourceName,
46         [](napi_env env, void *data) {
47             ANS_LOGD("NapiIsSupportTemplate work excute.");
48             AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast<AsyncCallbackInfoTemplate *>(data);
49 
50             if (asyncCallbackinfo) {
51                 asyncCallbackinfo->info.errorCode = NotificationHelper::IsSupportTemplate(
52                     asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support);
53             }
54         },
55         [](napi_env env, napi_status status, void *data) {
56             ANS_LOGD("NapiIsSupportTemplate work complete.");
57             AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast<AsyncCallbackInfoTemplate *>(data);
58             if (asyncCallbackinfo) {
59                 napi_value result = nullptr;
60                 napi_get_boolean(env, asyncCallbackinfo->params.support, &result);
61                 Common::CreateReturnValue(env, asyncCallbackinfo->info, result);
62                 if (asyncCallbackinfo->info.callback != nullptr) {
63                     ANS_LOGD("null callback");
64                     napi_delete_reference(env, asyncCallbackinfo->info.callback);
65                 }
66                 napi_delete_async_work(env, asyncCallbackinfo->asyncWork);
67                 delete asyncCallbackinfo;
68                 asyncCallbackinfo = nullptr;
69             }
70         },
71         (void *)asyncCallbackinfo,
72         &asyncCallbackinfo->asyncWork);
73 
74     bool isCallback = asyncCallbackinfo->info.isCallback;
75     napi_queue_async_work_with_qos(env, asyncCallbackinfo->asyncWork, napi_qos_user_initiated);
76 
77     if (isCallback) {
78         return Common::NapiGetNull(env);
79     } else {
80         return promise;
81     }
82 }
83 }  // namespace NotificationNapi
84 }  // namespace OHOS
85