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