1 /*
2  * Copyright (c) 2024 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 
Callback()80 Callback::Callback() {}
81 
~Callback()82 Callback::~Callback()
83 {
84     LOGI("~Callback");
85 }
86 
OnExpired()87 void Callback::OnExpired()
88 {
89     LOGI("OnExpired start");
90     std::lock_guard<std::recursive_mutex> lock(callbackLock_);
91     auto findCallback = std::find_if(callbackInstances_.begin(), callbackInstances_.end(),
92         [&](const auto& callbackInstance) { return callbackInstance.second.get() == this; }
93     );
94     if (findCallback == callbackInstances_.end()) {
95         LOGI("expired callback not found");
96         return;
97     }
98     auto callback = findCallback->second;
99     callbackInstances_.erase(findCallback);
100     LOGI("call native callback");
101     findCallback->second->ffiCallback_();
102     LOGI("OnExpired end");
103 }
104 
SetCallbackInfo(void (* callback)())105 void Callback::SetCallbackInfo(void (*callback)())
106 {
107     ffiCallback_ = callback;
108 }
109 }
110 }
111 }