• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_INCLUDE_BACKGROUND_TASK_MANAGER_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_INCLUDE_BACKGROUND_TASK_MANAGER_H
18 
19 #include "background_task_subscriber.h"
20 #include "expired_callback.h"
21 #include "ibackground_task_mgr.h"
22 #include "iremote_object.h"
23 #include "want_agent.h"
24 #include "efficiency_resource_info.h"
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 class BackgroundTaskManager {
29 public:
30     BackgroundTaskManager();
31 
32     virtual ~BackgroundTaskManager();
33 
34     /**
35      * @brief Cancel delay suspend of background task.
36      *
37      * @param requestId Id of the requested background task.
38      * @return ERR_OK if success, else fail.
39      */
40     ErrCode CancelSuspendDelay(int32_t requestId);
41 
42     /**
43      * @brief Request delay suspend for background task.
44      *
45      * @param reason Reason of requesting delay suspend.
46      * @param callback Called back to notify the application.
47      * @param delayInfo Info of background task which request delay suspend.
48      * @return ERR_OK if success, else fail.
49      */
50     ErrCode RequestSuspendDelay(const std::u16string &reason,
51         const ExpiredCallback &callback, std::shared_ptr<DelaySuspendInfo> &delayInfo);
52 
53     /**
54      * @brief Get the time remaining before the background tasks enter the suspended state.
55      *
56      * @param requestId Id of the requested background task.
57      * @param delayTime Remaining time.
58      * @return ERR_OK if success, else fail.
59      */
60     ErrCode GetRemainingDelayTime(int32_t requestId, int32_t &delayTime);
61 
62     /**
63      * @brief Request service to get all transient tasks.
64      *
65      * @param remainingQuota remain quota for the day.
66      * @param list transient tasks.
67      * @return ERR_OK if success, else fail.
68      */
69     ErrCode GetAllTransientTasks(int32_t &remainingQuota, std::vector<std::shared_ptr<DelaySuspendInfo>> &list);
70 
71     /**
72      * @brief Request service to keep running background.
73      *
74      * @param taskParam Request params.
75      * @return ERR_OK if success, else fail.
76      */
77     ErrCode RequestStartBackgroundRunning(ContinuousTaskParam &taskParam);
78 
79     /**
80      * @brief Request service to update running background.
81      *
82      * @param taskParam Request params.
83      * @return ERR_OK if success, else fail.
84      */
85     ErrCode RequestUpdateBackgroundRunning(ContinuousTaskParam &taskParam);
86 
87     /**
88      * @brief Request service to keep or stop running background for inner ability.
89      *
90      * @param taskParam Request params.
91      * @return ERR_OK if success, else fail.
92      */
93     ErrCode RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam);
94 
95     /**
96      * @brief Request service to get continuous task info by uid for inner ability.
97      *
98      * @param uid app uid.
99      * @param list continuous task info list.
100      * @return ERR_OK if success, else fail.
101      */
102     ErrCode RequestGetContinuousTasksByUidForInner(int32_t uid,
103         std::vector<std::shared_ptr<ContinuousTaskInfo>> &list);
104 
105     /**
106      * @brief Request service to stop running background.
107      *
108      * @param abilityName Ability name of the requester ability.
109      * @param abilityToken Ability token to mark an unique running ability instance.
110      * @param abilityId Ability identity.
111      * @return ERR_OK if success, else fail.
112      */
113     ErrCode RequestStopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken,
114         int32_t abilityId);
115 
116     /**
117      * @brief Request service to get all continuous tasks.
118      *
119      * @param list continuous task info.
120      * @return ERR_OK if success, else fail.
121      */
122     ErrCode RequestGetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> &list);
123 
124     /**
125      * @brief Request service to get all continuous tasks.
126      *
127      * @param list continuous task info.
128      * @param includeSuspended whether include suspended continuous task.
129      * @return ERR_OK if success, else fail.
130      */
131     ErrCode RequestGetAllContinuousTasks(
132         std::vector<std::shared_ptr<ContinuousTaskInfo>> &list, bool includeSuspended);
133 
134     /**
135      * @brief Reset proxy for background task.
136      */
137     void ResetBackgroundTaskManagerProxy();
138 
139     /**
140      * @brief Subscribes background task event.
141      *
142      * @param subscriber Subscriber token.
143      * @return ERR_OK if success, else fail.
144      */
145     ErrCode SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
146 
147     /**
148      * @brief Unsubscribes background task event.
149      *
150      * @param subscriber Subscriber token.
151      * @return ERR_OK if success, else fail.
152      */
153     ErrCode UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
154 
155     /**
156      * @brief Get transient task applications.
157      * @param list transient task apps.
158      * @return Returns ERR_OK if success, else failure.
159      */
160     ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list);
161 
162     /**
163      * @brief Pause transient task time by uid for inner ability.
164      * @param uid app uid.
165      * @return Returns ERR_OK if success, else failure.
166      */
167     ErrCode PauseTransientTaskTimeForInner(int32_t uid);
168 
169     /**
170      * @brief Start transient task time by uid for inner ability.
171      * @param uid app uid.
172      * @return Returns ERR_OK if success, else failure.
173      */
174     ErrCode StartTransientTaskTimeForInner(int32_t uid);
175 
176     /**
177      * @brief Get all continuous task running infos
178      * @param list continuous task infos.
179      * @return Returns ERR_OK if success, else failure.
180      */
181     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
182 
183     /**
184      * @brief Apply or unapply efficiency resources.
185      *
186      * @param resourceInfo Request params.
187      * @return Returns ERR_OK on success, others on failure.
188      */
189     ErrCode ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo);
190 
191     /**
192      * @brief Reset all efficiency resources.
193      *
194      * @return ERR_OK if success, else fail.
195      */
196     ErrCode ResetAllEfficiencyResources();
197 
198     /**
199      * @brief Get all efficiency resources
200      *
201      * @param resourceInfoList efficiency resource infos.
202      * @return ERR_OK if success, else fail.
203      */
204     ErrCode GetAllEfficiencyResources(std::vector<std::shared_ptr<EfficiencyResourceInfo>> &resourceInfoList);
205 
206     /**
207      * @brief Get all effficiency resources running infos.
208      * @param appList EFficiency Resources infos of apps.
209      * @param procList  EFficiency Resources infos of processes.
210      * @return Returns ERR_OK on success, others on failure.
211      */
212     ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
213         std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList);
214 
215     /*
216      * @brief Request stop continuous task.
217      * @param uid app uid.
218      * @param pid app pid.
219      * @param taskType continuous task type.
220      * @param key continuous task key.
221      * @return Returns ERR_OK if success, else failure.
222      */
223     ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
224 
225     /*
226      * @brief Request suspend continuous task.
227      * @param uid app uid.
228      * @param pid app pid.
229      * @param reason continuous task suspend reason.
230      * @param key continuous task key.
231      * @return Returns ERR_OK if success, else failure.
232      */
233     ErrCode SuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key);
234 
235     /*
236      * @brief Request active continuous task.
237      * @param uid app uid.
238      * @param pid app pid.
239      * @param key continuous task key.
240      * @return Returns ERR_OK if success, else failure.
241      */
242     ErrCode ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key);
243 
244     /**
245      * @brief AVsession notify update notification.
246      * @param uid app uid.
247      * @param pid app pid.
248      * @param isPublish isPublish notification.
249      * @return Returns ERR_OK if success, else failure.
250      */
251     ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false);
252 
253     /**
254      * @brief Set background task config.
255      * @param configData config param.
256      * @param sourceType data source.
257      * @return Returns ERR_OK if success, else failure.
258      */
259     ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType);
260 
261 private:
262     bool GetBackgroundTaskManagerProxy();
263 
264 private:
265     class BgTaskMgrDeathRecipient : public IRemoteObject::DeathRecipient {
266     public:
267         explicit BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager);
268 
269         ~BgTaskMgrDeathRecipient() override;
270 
271         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
272 
273     private:
274         BackgroundTaskManager &backgroundTaskManager_;
275     };
276 
277 private:
278     std::mutex mutex_;
279     sptr<BackgroundTaskMgr::IBackgroundTaskMgr> proxy_;
280     sptr<BgTaskMgrDeathRecipient> recipient_;
281 };
282 }  // namespace BackgroundTaskMgr
283 }  // namespace OHOS
284 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_INCLUDE_BACKGROUND_TASK_MANAGER_H
285