1 /*
2 * Copyright (c) 2024-2025 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 "transient_task_api.h"
17 #include "singleton.h"
18 #include "background_task_manager.h"
19 #include "transient_task_log.h"
20 #include "bgtaskmgr_inner_errors.h"
21 #include "background_task_mgr_helper.h"
22 #include "string_ex.h"
23 #include "callback.h"
24
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 std::map<int32_t, std::shared_ptr<Callback>> callbackInstances_;
28 std::recursive_mutex callbackLock_;
29 const int32_t INNER_ERROR_SHIFT = 100;
30
31 extern "C" {
OH_BackgroundTaskManager_RequestSuspendDelay(const char * reason,TransientTask_Callback callback,TransientTask_DelaySuspendInfo * info)32 int32_t OH_BackgroundTaskManager_RequestSuspendDelay(const char* reason,
33 TransientTask_Callback callback, TransientTask_DelaySuspendInfo *info)
34 {
35 if (!callback || !info) {
36 LOGI("OH_BackgroundTaskManager_RequestSuspendDelay param is null");
37 return ERR_TRANSIENT_TASK_INVALID_PARAM;
38 }
39 LOGI("OH_BackgroundTaskManager_RequestSuspendDelay start");
40 std::string tmp(reason);
41 std::u16string reasonu16(Str8ToStr16(tmp));
42 std::shared_ptr<DelaySuspendInfo> delaySuspendInfo {nullptr};
43 std::shared_ptr<Callback> expiredCallback = std::make_shared<Callback>();
44 expiredCallback->Init();
45 expiredCallback->SetCallbackInfo(callback);
46
47 ErrCode errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->
48 RequestSuspendDelay(reasonu16, *expiredCallback, delaySuspendInfo);
49 if (errCode != 0) {
50 return errCode / INNER_ERROR_SHIFT;
51 }
52 info->requestId = delaySuspendInfo->GetRequestId();
53 info->actualDelayTime = delaySuspendInfo->GetActualDelayTime();
54 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
55 callbackInstances_[delaySuspendInfo->GetRequestId()] = expiredCallback;
56 return ERR_TRANSIENT_TASK_OK;
57 }
58
OH_BackgroundTaskManager_GetRemainingDelayTime(int32_t requestId,int32_t * delayTime)59 int32_t OH_BackgroundTaskManager_GetRemainingDelayTime(int32_t requestId, int32_t *delayTime)
60 {
61 auto errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->
62 GetRemainingDelayTime(requestId, *delayTime);
63 return errCode / INNER_ERROR_SHIFT;
64 }
65
OH_BackgroundTaskManager_CancelSuspendDelay(int32_t requestId)66 int32_t OH_BackgroundTaskManager_CancelSuspendDelay(int32_t requestId)
67 {
68 auto errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->CancelSuspendDelay(requestId);
69 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
70 auto findCallback = callbackInstances_.find(requestId);
71 if (findCallback != callbackInstances_.end()) {
72 LOGI("CancelSuspendDelay erase");
73 callbackInstances_.erase(findCallback);
74 LOGI("CancelSuspendDelay erase ok");
75 }
76
77 return errCode / INNER_ERROR_SHIFT;
78 }
79
OH_BackgroundTaskManager_GetTransientTaskInfo(TransientTask_TransientTaskInfo * info)80 int32_t OH_BackgroundTaskManager_GetTransientTaskInfo(TransientTask_TransientTaskInfo *info)
81 {
82 int32_t remainingQuotaValue = 0;
83 std::vector<std::shared_ptr<DelaySuspendInfo>> listValue;
84 auto errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->
85 GetAllTransientTasks(remainingQuotaValue, listValue);
86 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
87 if (errCode != 0) {
88 return errCode / INNER_ERROR_SHIFT;
89 }
90 info->remainingQuota = remainingQuotaValue;
91 int index = 0;
92 for (const auto &transientTasksRecord : listValue) {
93 info->transientTasks[index].requestId = transientTasksRecord->GetRequestId();
94 info->transientTasks[index].actualDelayTime = transientTasksRecord->GetActualDelayTime();
95 index++;
96 }
97 return ERR_TRANSIENT_TASK_OK;
98 }
99
Callback()100 Callback::Callback() {}
101
~Callback()102 Callback::~Callback()
103 {
104 LOGI("~Callback");
105 }
106
OnExpired()107 void Callback::OnExpired()
108 {
109 LOGI("OnExpired start");
110 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
111 auto findCallback = std::find_if(callbackInstances_.begin(), callbackInstances_.end(),
112 [&](const auto& callbackInstance) { return callbackInstance.second.get() == this; }
113 );
114 if (findCallback == callbackInstances_.end()) {
115 LOGI("expired callback not found");
116 return;
117 }
118 auto callback = findCallback->second;
119 callbackInstances_.erase(findCallback);
120 LOGI("call native callback");
121 findCallback->second->ffiCallback_();
122 LOGI("OnExpired end");
123 }
124
SetCallbackInfo(void (* callback)())125 void Callback::SetCallbackInfo(void (*callback)())
126 {
127 ffiCallback_ = callback;
128 }
129 }
130 }
131 }