• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const ContinuousTaskParam &taskParam);
69 
70     /**
71      * @brief Request service to keep or stop running background for inner ability.
72      *
73      * @param taskParam Request params.
74      * @return ERR_OK if success, else fail.
75      */
76     ErrCode RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam);
77 
78     /**
79      * @brief Request service to stop running background.
80      *
81      * @param abilityName Ability name of the requester ability.
82      * @param abilityToken Ability token to mark an unique running ability instance.
83      * @return ERR_OK if success, else fail.
84      */
85     ErrCode RequestStopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken);
86 
87     /**
88      * @brief Reset proxy for background task.
89      */
90     void ResetBackgroundTaskManagerProxy();
91 
92     /**
93      * @brief Subscribes background task event.
94      *
95      * @param subscriber Subscriber token.
96      * @return ERR_OK if success, else fail.
97      */
98     ErrCode SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
99 
100     /**
101      * @brief Unsubscribes background task event.
102      *
103      * @param subscriber Subscriber token.
104      * @return ERR_OK if success, else fail.
105      */
106     ErrCode UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber);
107 
108     /**
109      * @brief Get transient task applications.
110      * @param list transient task apps.
111      * @return Returns ERR_OK if success, else failure.
112      */
113     ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list);
114 
115     /**
116      * @brief Get all continuous task running infos
117      * @param list continuous task infos.
118      * @return Returns ERR_OK if success, else failure.
119      */
120     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
121 
122     /**
123      * @brief Apply or unapply efficiency resources.
124      *
125      * @param resourceInfo Request params.
126      * @return Returns ERR_OK on success, others on failure.
127      */
128     ErrCode ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo);
129 
130     /**
131      * @brief Reset all efficiency resources.
132      *
133      * @return ERR_OK if success, else fail.
134      */
135     ErrCode ResetAllEfficiencyResources();
136 
137     /**
138      * @brief Get all effficiency resources running infos.
139      * @param appList EFficiency Resources infos of apps.
140      * @param procList  EFficiency Resources infos of processes.
141      * @return Returns ERR_OK on success, others on failure.
142      */
143     ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
144         std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList);
145 
146     /*
147      * @brief Request stop continuous task.
148      * @param uid app uid.
149      * @param pid app pid.
150      * @param taskType continuous task type.
151      * @return Returns ERR_OK if success, else failure.
152      */
153     ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType);
154 
155 private:
156     bool GetBackgroundTaskManagerProxy();
157 
158 private:
159     class BgTaskMgrDeathRecipient : public IRemoteObject::DeathRecipient {
160     public:
161         explicit BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager);
162 
163         ~BgTaskMgrDeathRecipient() override;
164 
165         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
166 
167     private:
168         BackgroundTaskManager &backgroundTaskManager_;
169     };
170 
171 private:
172     std::mutex mutex_;
173     sptr<BackgroundTaskMgr::IBackgroundTaskMgr> backgroundTaskMgrProxy_;
174     sptr<BgTaskMgrDeathRecipient> recipient_;
175 };
176 }  // namespace BackgroundTaskMgr
177 }  // namespace OHOS
178 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_INCLUDE_BACKGROUND_TASK_MANAGER_H
179