• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "get_remaining_delay_time.h"
17 
18 #include "singleton.h"
19 
20 #include "background_task_manager.h"
21 #include "transient_task_log.h"
22 
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
25 static const int32_t GET_REMAINING_DELAY_TIME_MIN_PARAMS = 1;
26 static const int32_t GET_REMAINING_DELAY_TIME_PARAMS = 2;
27 
28 struct AsyncCallbackInfoGetRemainingDelayTime {
29     napi_env env = nullptr;
30     napi_async_work asyncWork = nullptr;
31     int32_t requestId = 0;
32     int32_t delayTime = 0;
33     CallbackPromiseInfo info;
34 };
35 
36 struct GetRemainingDelayTimeParamsInfo {
37     int32_t requestId = 0;
38     napi_ref callback = nullptr;
39 };
40 
ParseParameters(const napi_env & env,const napi_callback_info & info,GetRemainingDelayTimeParamsInfo & params)41 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetRemainingDelayTimeParamsInfo &params)
42 {
43     size_t argc = GET_REMAINING_DELAY_TIME_PARAMS;
44     napi_value argv[GET_REMAINING_DELAY_TIME_PARAMS] = {nullptr};
45     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
46     NAPI_ASSERT(env, argc == GET_REMAINING_DELAY_TIME_MIN_PARAMS || argc == GET_REMAINING_DELAY_TIME_PARAMS,
47         "Wrong number of arguments");
48 
49     // argv[0] : requestId
50     if (Common::GetInt32NumberValue(env, argv[0], params.requestId) == nullptr) {
51         BGTASK_LOGE("ParseParameters failed, requestId is nullptr.");
52         return nullptr;
53     }
54 
55     // argv[1]: callback
56     if (argc == GET_REMAINING_DELAY_TIME_PARAMS) {
57         napi_valuetype valuetype = napi_undefined;
58         NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
59         NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected.");
60         napi_create_reference(env, argv[1], 1, &params.callback);
61     }
62 
63     if (params.requestId <= 0) {
64         BGTASK_LOGE("ParseParameters failed, requestId is illegal.");
65         return nullptr;
66     }
67     return Common::NapiGetNull(env);
68 }
69 
GetRemainingDelayTime(napi_env env,napi_callback_info info)70 napi_value GetRemainingDelayTime(napi_env env, napi_callback_info info)
71 {
72     GetRemainingDelayTimeParamsInfo params;
73     if (ParseParameters(env, info, params) == nullptr) {
74         return Common::JSParaError(env, params.callback);
75     }
76 
77     napi_value promise = nullptr;
78     AsyncCallbackInfoGetRemainingDelayTime *asynccallbackinfo =
79         new (std::nothrow) AsyncCallbackInfoGetRemainingDelayTime {.env = env, .asyncWork = nullptr};
80     if (!asynccallbackinfo) {
81         return Common::JSParaError(env, params.callback);
82     }
83     asynccallbackinfo->requestId = params.requestId;
84     BGTASK_LOGI(" asynccallbackinfo->requestId: %{public}d", asynccallbackinfo->requestId);
85     Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise);
86 
87     napi_value resourceName = nullptr;
88     napi_create_string_latin1(env, "GetRemainingDelayTime", NAPI_AUTO_LENGTH, &resourceName);
89 
90     napi_create_async_work(env,
91         nullptr,
92         resourceName,
93         [](napi_env env, void *data) {
94             AsyncCallbackInfoGetRemainingDelayTime *asynccallbackinfo = (AsyncCallbackInfoGetRemainingDelayTime *)data;
95             if (asynccallbackinfo != nullptr) {
96                 asynccallbackinfo->info.errorCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->
97                     GetRemainingDelayTime(asynccallbackinfo->requestId, asynccallbackinfo->delayTime);
98             }
99         },
100         [](napi_env env, napi_status status, void *data) {
101             AsyncCallbackInfoGetRemainingDelayTime *asynccallbackinfo = (AsyncCallbackInfoGetRemainingDelayTime *)data;
102             if (asynccallbackinfo != nullptr) {
103                 napi_value result = nullptr;
104                 napi_create_int32(env, asynccallbackinfo->delayTime, &result);
105                 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result);
106 
107                 if (asynccallbackinfo->info.callback != nullptr) {
108                     napi_delete_reference(env, asynccallbackinfo->info.callback);
109                 }
110 
111                 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
112                 delete asynccallbackinfo;
113                 asynccallbackinfo = nullptr;
114             }
115         },
116         (void *)asynccallbackinfo,
117         &asynccallbackinfo->asyncWork);
118 
119     NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
120 
121     if (asynccallbackinfo->info.isCallback) {
122         return Common::NapiGetNull(env);
123     } else {
124         return promise;
125     }
126 }
127 }  // namespace BackgroundTaskMgr
128 }  // namespace OHOS