• 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 "cancel_suspend_delay.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 CANCEL_SUSPEND_DELAY_PARAMS = 1;
26 
ParseParameters(const napi_env & env,const napi_callback_info & info,int32_t & requestId)27 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, int32_t &requestId)
28 {
29     size_t argc = CANCEL_SUSPEND_DELAY_PARAMS;
30     napi_value argv[CANCEL_SUSPEND_DELAY_PARAMS] = {nullptr};
31     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
32     NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
33 
34     // argv[0] :requestId
35     if (Common::GetInt32NumberValue(env, argv[0], requestId) == nullptr) {
36         BGTASK_LOGE("ParseParameters failed, requestId is nullptr.");
37         return nullptr;
38     }
39     if (requestId <= 0) {
40         BGTASK_LOGE("ParseParameters failed, requestId is illegal.");
41         return nullptr;
42     }
43     return Common::NapiGetNull(env);
44 }
45 
CancelSuspendDelay(napi_env env,napi_callback_info info)46 napi_value CancelSuspendDelay(napi_env env, napi_callback_info info)
47 {
48     int32_t requestId;
49     if (ParseParameters(env, info, requestId) == nullptr) {
50         return Common::NapiGetNull(env);
51     }
52 
53     DelayedSingleton<BackgroundTaskManager>::GetInstance()->CancelSuspendDelay(requestId);
54     return Common::NapiGetNull(env);
55 }
56 }  // namespace BackgroundTaskMgr
57 }  // namespace OHOS