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