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 "request_suspend_delay.h"
17
18 #include <uv.h>
19
20 #include "singleton.h"
21 #ifdef SUPPORT_JSSTACK
22 #include "xpower_event_js.h"
23 #endif
24
25 #include "background_task_manager.h"
26 #include "hitrace_meter.h"
27 #include "transient_task_log.h"
28
29 namespace OHOS {
30 namespace BackgroundTaskMgr {
31 std::map<int32_t, std::shared_ptr<ExpiredCallback>> callbackInstances_;
32 std::mutex callbackLock_;
33 static const uint32_t REQUEST_SUSPEND_DELAY_PARAMS = 2;
34
35 struct CallbackReceiveDataWorker {
36 napi_env env = nullptr;
37 napi_ref ref = nullptr;
38 std::shared_ptr<ExpiredCallback> callback = nullptr;
39 };
40
CallbackInstance()41 CallbackInstance::CallbackInstance() {}
42
~CallbackInstance()43 CallbackInstance::~CallbackInstance()
44 {
45 if (expiredCallbackInfo_.ref == nullptr) {
46 return;
47 }
48 DeleteNapiRef();
49 }
50
DeleteNapiRef()51 void CallbackInstance::DeleteNapiRef()
52 {
53 std::shared_ptr<CallbackReceiveDataWorker> dataWorker = std::make_shared<CallbackReceiveDataWorker>();
54 if (dataWorker == nullptr) {
55 BGTASK_LOGE("DeleteNapiRef new dataWorker failed");
56 return;
57 }
58
59 dataWorker->env = expiredCallbackInfo_.env;
60 dataWorker->ref = expiredCallbackInfo_.ref;
61
62 auto task = [dataWorker]() {
63 napi_delete_reference(dataWorker->env, dataWorker->ref);
64 };
65 if (napi_status::napi_ok != napi_send_event(expiredCallbackInfo_.env, task, napi_eprio_high)) {
66 BGTASK_LOGE("DeleteNapiRef: Failed to SendEvent");
67 dataWorker = nullptr;
68 }
69 }
70
OnExpired()71 __attribute__((no_sanitize("cfi"))) void CallbackInstance::OnExpired()
72 {
73 std::lock_guard<std::mutex> lock(callbackLock_);
74 auto findCallback = std::find_if(callbackInstances_.begin(), callbackInstances_.end(),
75 [&](const auto& callbackInstance) { return callbackInstance.second.get() == this; }
76 );
77 if (findCallback == callbackInstances_.end()) {
78 BGTASK_LOGI("expired callback is not found");
79 return;
80 }
81
82 if (expiredCallbackInfo_.ref == nullptr) {
83 BGTASK_LOGE("expired callback unset");
84 callbackInstances_.erase(findCallback);
85 return;
86 }
87
88 std::shared_ptr<CallbackReceiveDataWorker> dataWorker = std::make_shared<CallbackReceiveDataWorker>();
89 if (dataWorker == nullptr) {
90 BGTASK_LOGE("OnExpired new dataWorker failed");
91 callbackInstances_.erase(findCallback);
92 return;
93 }
94
95 dataWorker->env = expiredCallbackInfo_.env;
96 dataWorker->ref = expiredCallbackInfo_.ref;
97 dataWorker->callback = shared_from_this();
98
99 auto task = [dataWorker]() {
100 BGTASK_LOGD("OnExpired start");
101 Common::SetCallback(dataWorker->env, dataWorker->ref, Common::NapiGetNull(dataWorker->env));
102 std::lock_guard<std::mutex> lock(callbackLock_);
103 auto findCallback = std::find_if(callbackInstances_.begin(), callbackInstances_.end(),
104 [&](const auto& callbackInstance) { return callbackInstance.second == dataWorker->callback; }
105 );
106 if (findCallback != callbackInstances_.end()) {
107 callbackInstances_.erase(findCallback);
108 }
109 };
110 if (napi_status::napi_ok != napi_send_event(expiredCallbackInfo_.env, task, napi_eprio_high)) {
111 BGTASK_LOGE("OnExpired: Failed to SendEvent");
112 dataWorker = nullptr;
113 callbackInstances_.erase(findCallback);
114 }
115 }
116
SetCallbackInfo(const napi_env & env,const napi_ref & ref)117 void CallbackInstance::SetCallbackInfo(const napi_env &env, const napi_ref &ref)
118 {
119 expiredCallbackInfo_.env = env;
120 expiredCallbackInfo_.ref = ref;
121 }
122
GetExpiredCallback(const napi_env & env,const napi_value & value,std::shared_ptr<CallbackInstance> & callback)123 napi_value GetExpiredCallback(
124 const napi_env &env, const napi_value &value, std::shared_ptr<CallbackInstance> &callback)
125 {
126 napi_ref result = nullptr;
127 callback = std::make_shared<CallbackInstance>();
128 callback->Init();
129
130 napi_create_reference(env, value, 1, &result);
131 callback->SetCallbackInfo(env, result);
132
133 return Common::NapiGetNull(env);
134 }
135
ParseParameters(const napi_env & env,const napi_callback_info & info,std::u16string & reason,std::shared_ptr<CallbackInstance> & callback,bool isThrow)136 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info,
137 std::u16string &reason, std::shared_ptr<CallbackInstance> &callback, bool isThrow)
138 {
139 size_t argc = REQUEST_SUSPEND_DELAY_PARAMS;
140 napi_value argv[REQUEST_SUSPEND_DELAY_PARAMS] = {nullptr};
141 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
142 if (argc != REQUEST_SUSPEND_DELAY_PARAMS) {
143 Common::HandleParamErr(env, ERR_PARAM_NUMBER_ERR, isThrow);
144 return nullptr;
145 }
146
147 // argv[0] : reason
148 if (Common::GetU16StringValue(env, argv[0], reason) == nullptr) {
149 BGTASK_LOGE("ParseParameters failed, reason is nullptr.");
150 Common::HandleParamErr(env, ERR_REASON_NULL_OR_TYPE_ERR, isThrow);
151 return nullptr;
152 }
153
154 // arg[1] : callback
155 napi_valuetype valuetype = napi_undefined;
156 NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype));
157 if (valuetype != napi_function) {
158 Common::HandleParamErr(env, ERR_CALLBACK_NULL_OR_TYPE_ERR, isThrow);
159 return nullptr;
160 }
161
162 if (GetExpiredCallback(env, argv[1], callback) == nullptr) {
163 BGTASK_LOGE("ExpiredCallback parse failed");
164 Common::HandleParamErr(env, ERR_CALLBACK_NULL_OR_TYPE_ERR, isThrow);
165 return nullptr;
166 }
167 return Common::NapiGetNull(env);
168 }
169
RequestSuspendDelay(napi_env env,napi_callback_info info,bool isThrow)170 napi_value RequestSuspendDelay(napi_env env, napi_callback_info info, bool isThrow)
171 {
172 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
173 "BackgroundTaskManager::TransientTask::Napi::RequestSuspendDelay");
174
175 #ifdef SUPPORT_JSSTACK
176 HiviewDFX::ReportXPowerJsStackSysEvent(env, "TRANSIENT_TASK_APPLY");
177 #endif
178 std::shared_ptr<CallbackInstance> callback = nullptr;
179 std::u16string reason;
180 if (ParseParameters(env, info, reason, callback, isThrow) == nullptr) {
181 return Common::NapiGetNull(env);
182 }
183
184 std::shared_ptr<DelaySuspendInfo> delaySuspendInfo {nullptr};
185 ErrCode errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->
186 RequestSuspendDelay(reason, *callback, delaySuspendInfo);
187 Common::HandleErrCode(env, errCode, isThrow);
188 if (!delaySuspendInfo) {
189 return Common::NapiGetNull(env);
190 }
191 {
192 std::lock_guard<std::mutex> lock(callbackLock_);
193 callbackInstances_[delaySuspendInfo->GetRequestId()] = callback;
194 }
195
196 napi_value result = nullptr;
197 napi_create_object(env, &result);
198 if (!Common::SetDelaySuspendInfo(env, delaySuspendInfo, result)) {
199 BGTASK_LOGW("Set DelaySuspendInfo object failed");
200 }
201 return result;
202 }
203
RequestSuspendDelay(napi_env env,napi_callback_info info)204 napi_value RequestSuspendDelay(napi_env env, napi_callback_info info)
205 {
206 return RequestSuspendDelay(env, info, false);
207 }
208
RequestSuspendDelayThrow(napi_env env,napi_callback_info info)209 napi_value RequestSuspendDelayThrow(napi_env env, napi_callback_info info)
210 {
211 return RequestSuspendDelay(env, info, true);
212 }
213 } // namespace BackgroundTaskMgr
214 } // namespace OHOS