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 #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 keep running background.
64      *
65      * @param taskParam Request params.
66      * @return ERR_OK if success, else fail.
67      */
68     ErrCode RequestStartBackgroundRunning(ContinuousTaskParam &taskParam);
69 
70     /**
71      * @brief Request service to update running background.
72      *
73      * @param taskParam Request params.
74      * @return ERR_OK if success, else fail.
75      */
76     ErrCode RequestUpdateBackgroundRunning(ContinuousTaskParam &taskParam);
77 
78     /**
79      * @brief Request service to keep or stop running background for inner ability.
80      *
81      * @param taskParam Request params.
82      * @return ERR_OK if success, else fail.
83      */
84     ErrCode RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam);
85 
86     /**
87      * @brief Request service to stop running background.
88      *
89      * @param abilityName Ability name of the requester ability.
90      * @param abilityToken Ability token to mark an unique running ability instance.
91      * @param abilityId Ability identity.
92      * @return ERR_OK if success, else fail.
93      */
94     ErrCode RequestStopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken,
95         int32_t abilityId);
96 
97     /**
98      * @brief Reset proxy for background task.
99      */
100     void ResetBackgroundTaskManagerProxy();
101 
102     /**
103      * @brief Subscribes background task event.
104      *
105      * @param subscriber Subscriber token.
106      * @return ERR_OK if success, else fail.
107      */
108     ErrCode SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
109 
110     /**
111      * @brief Unsubscribes background task event.
112      *
113      * @param subscriber Subscriber token.
114      * @return ERR_OK if success, else fail.
115      */
116     ErrCode UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
117 
118     /**
119      * @brief Get transient task applications.
120      * @param list transient task apps.
121      * @return Returns ERR_OK if success, else failure.
122      */
123     ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list);
124 
125     /**
126      * @brief Pause transient task time by uid for inner ability.
127      * @param uid app uid.
128      * @return Returns ERR_OK if success, else failure.
129      */
130     ErrCode PauseTransientTaskTimeForInner(int32_t uid);
131 
132     /**
133      * @brief Start transient task time by uid for inner ability.
134      * @param uid app uid.
135      * @return Returns ERR_OK if success, else failure.
136      */
137     ErrCode StartTransientTaskTimeForInner(int32_t uid);
138 
139     /**
140      * @brief Get all continuous task running infos
141      * @param list continuous task infos.
142      * @return Returns ERR_OK if success, else failure.
143      */
144     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
145 
146     /**
147      * @brief Apply or unapply efficiency resources.
148      *
149      * @param resourceInfo Request params.
150      * @return Returns ERR_OK on success, others on failure.
151      */
152     ErrCode ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo);
153 
154     /**
155      * @brief Reset all efficiency resources.
156      *
157      * @return ERR_OK if success, else fail.
158      */
159     ErrCode ResetAllEfficiencyResources();
160 
161     /**
162      * @brief Get all effficiency resources running infos.
163      * @param appList EFficiency Resources infos of apps.
164      * @param procList  EFficiency Resources infos of processes.
165      * @return Returns ERR_OK on success, others on failure.
166      */
167     ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
168         std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList);
169 
170     /*
171      * @brief Request stop continuous task.
172      * @param uid app uid.
173      * @param pid app pid.
174      * @param taskType continuous task type.
175      * @param key continuous task key.
176      * @return Returns ERR_OK if success, else failure.
177      */
178     ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
179 
180     /**
181      * @brief Set background task config.
182      * @param configData config param.
183      * @param sourceType data source.
184      * @return Returns ERR_OK if success, else failure.
185      */
186     ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType);
187 
188 private:
189     bool GetBackgroundTaskManagerProxy();
190 
191 private:
192     class BgTaskMgrDeathRecipient : public IRemoteObject::DeathRecipient {
193     public:
194         explicit BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager);
195 
196         ~BgTaskMgrDeathRecipient() override;
197 
198         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
199 
200     private:
201         BackgroundTaskManager &backgroundTaskManager_;
202     };
203 
204 private:
205     std::mutex mutex_;
206     sptr<BackgroundTaskMgr::IBackgroundTaskMgr> proxy_;
207     sptr<BgTaskMgrDeathRecipient> recipient_;
208 };
209 }  // namespace BackgroundTaskMgr
210 }  // namespace OHOS
211 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_INCLUDE_BACKGROUND_TASK_MANAGER_H
212