• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_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_LOGI("enter");
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_LOGI("IsSupportTemplate napi_create_async_work start");
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_LOGI("IsSupportTemplate napi_create_async_work end");
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                     napi_delete_reference(env, asyncCallbackinfo->info.callback);
64                 }
65                 napi_delete_async_work(env, asyncCallbackinfo->asyncWork);
66                 delete asyncCallbackinfo;
67                 asyncCallbackinfo = nullptr;
68             }
69         },
70         (void *)asyncCallbackinfo,
71         &asyncCallbackinfo->asyncWork);
72 
73     NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackinfo->asyncWork));
74     if (asyncCallbackinfo->info.isCallback) {
75         return Common::NapiGetNull(env);
76     } else {
77         return promise;
78     }
79 }
80 }  // namespace NotificationNapi
81 }  // namespace OHOS