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