• 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 "request_suspend_delay.h"
22 #include "transient_task_log.h"
23 
24 namespace OHOS {
25 namespace BackgroundTaskMgr {
26 static const uint32_t CANCEL_SUSPEND_DELAY_PARAMS = 1;
27 
ParseParameters(const napi_env & env,const napi_callback_info & info,int32_t & requestId,bool isThrow)28 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, int32_t &requestId, bool isThrow)
29 {
30     size_t argc = CANCEL_SUSPEND_DELAY_PARAMS;
31     napi_value argv[CANCEL_SUSPEND_DELAY_PARAMS] = {nullptr};
32     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
33     if (argc != CANCEL_SUSPEND_DELAY_PARAMS) {
34         Common::HandleParamErr(env, ERR_PARAM_NUMBER_ERR, isThrow);
35         return nullptr;
36     }
37 
38     // argv[0] :requestId
39     if (Common::GetInt32NumberValue(env, argv[0], requestId) == nullptr) {
40         BGTASK_LOGE("ParseParameters failed, requestId is nullptr.");
41         Common::HandleParamErr(env, ERR_REQUESTID_NULL_OR_ID_TYPE_ERR, isThrow);
42         return nullptr;
43     }
44     if (requestId <= 0) {
45         BGTASK_LOGI("ParseParameters failed, requestId is illegal.");
46         Common::HandleParamErr(env, ERR_REQUESTID_ILLEGAL, isThrow);
47         return nullptr;
48     }
49     return Common::NapiGetNull(env);
50 }
51 
CancelSuspendDelay(napi_env env,napi_callback_info info,bool isThrow)52 napi_value CancelSuspendDelay(napi_env env, napi_callback_info info, bool isThrow)
53 {
54     int32_t requestId;
55     if (ParseParameters(env, info, requestId, isThrow) == nullptr) {
56         return Common::NapiGetNull(env);
57     }
58 
59     ErrCode errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->CancelSuspendDelay(requestId);
60     Common::HandleErrCode(env, errCode, isThrow);
61     std::lock_guard<std::mutex> lock(callbackLock_);
62     auto findCallback = callbackInstances_.find(requestId);
63     if (findCallback != callbackInstances_.end()) {
64         callbackInstances_.erase(findCallback);
65     }
66     return Common::NapiGetNull(env);
67 }
68 
CancelSuspendDelay(napi_env env,napi_callback_info info)69 napi_value CancelSuspendDelay(napi_env env, napi_callback_info info)
70 {
71     return CancelSuspendDelay(env, info, false);
72 }
73 
CancelSuspendDelayThrow(napi_env env,napi_callback_info info)74 napi_value CancelSuspendDelayThrow(napi_env env, napi_callback_info info)
75 {
76     return CancelSuspendDelay(env, info, true);
77 }
78 }  // namespace BackgroundTaskMgr
79 }  // namespace OHOS