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_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H 17 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include <iremote_object.h> 27 #include <system_ability.h> 28 29 #include "ability_manager_interface.h" 30 #include "system_ability_status_change_stub.h" 31 #include "work_sched_service_stub.h" 32 #include "work_status.h" 33 #include "work_event_handler.h" 34 #include "delayed_sp_singleton.h" 35 #include "work_standby_state_change_callback.h" 36 37 namespace OHOS { 38 namespace WorkScheduler { 39 class WorkQueueManager; 40 class WorkPolicyManager; 41 class WorkBundleGroupChangeCallback; 42 class SchedulerBgTaskSubscriber; 43 class WorkSchedulerService final : public SystemAbility, public WorkSchedServiceStub, 44 public std::enable_shared_from_this<WorkSchedulerService> { 45 DISALLOW_COPY_AND_MOVE(WorkSchedulerService); 46 DECLARE_SYSTEM_ABILITY(WorkSchedulerService); 47 DECLARE_DELAYED_SP_SINGLETON(WorkSchedulerService); 48 public: 49 WorkSchedulerService(const int32_t systemAbilityId, bool runOnCreate); 50 51 /** 52 * @brief The OnStart callback. 53 */ 54 void OnStart() override; 55 /** 56 * @brief The OnStop callback. 57 */ 58 void OnStop() override; 59 /** 60 * @brief Start work. 61 * 62 * @param workInfo The info of work. 63 * @return error code, ERR_OK if success. 64 */ 65 int32_t StartWork(WorkInfo& workInfo) override; 66 /** 67 * @brief Stop work. 68 * 69 * @param workInfo The info of work. 70 * @return error code, ERR_OK if success. 71 */ 72 int32_t StopWork(WorkInfo& workInfo) override; 73 /** 74 * @brief Stop and cancel work. 75 * 76 * @param workInfo The info of work. 77 * @return error code, ERR_OK if success. 78 */ 79 int32_t StopAndCancelWork(WorkInfo& workInfo) override; 80 /** 81 * @brief Stop and clear works. 82 * 83 * @return error code, ERR_OK if success. 84 */ 85 int32_t StopAndClearWorks() override; 86 /** 87 * @brief The last work time out. 88 * 89 * @param workId The id of work. 90 * @param result True if the work executed time out, else false. 91 * @return error code, ERR_OK if success. 92 */ 93 int32_t IsLastWorkTimeout(int32_t workId, bool &result) override; 94 /** 95 * @brief Obtain all works. 96 * 97 * @param uid The uid. 98 * @param pid The pid. 99 * @param workInfos The infos of work. 100 * @return error code, ERR_OK if success. 101 */ 102 int32_t ObtainAllWorks(int32_t &uid, int32_t &pid, 103 std::list<std::shared_ptr<WorkInfo>>& workInfos) override; 104 /** 105 * @brief Get the status of work. 106 * 107 * @param uid The uid. 108 * @param workId The id of work. 109 * @param workInfo The info of work. 110 * @return error code, ERR_OK if success. 111 */ 112 int32_t GetWorkStatus(int32_t &uid, int32_t &workId, std::shared_ptr<WorkInfo>& workInfo) override; 113 /** 114 * @brief Get the Running Work Scheduler Work object 115 * 116 * @param workInfos The infos of work. 117 * @return ErrCode ERR_OK on success, others on failure 118 */ 119 int32_t GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos) override; 120 121 /** 122 * @brief Dump. 123 * 124 * @param fd The file descriptor. 125 * @param args The dump args. 126 * @return Status code, ERR_OK if success. 127 */ 128 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 129 130 /** 131 * @brief Init persisted. 132 */ 133 void InitPersisted(); 134 /** 135 * @brief Refresh persisted works. 136 */ 137 void RefreshPersistedWorks(); 138 /** 139 * @brief Stop and clear works by uid. 140 * 141 * @param uid The uid. 142 * @return True if success,else false. 143 */ 144 bool StopAndClearWorksByUid(int32_t uid); 145 /** 146 * @brief Create node dir. 147 * 148 * @param dir The dir. 149 * @return ERR_OK. 150 */ 151 int32_t CreateNodeDir(std::string dir); 152 /** 153 * @brief Create node file. 154 * 155 * @param filePath The file path. 156 * @return ERR_OK. 157 */ 158 int32_t CreateNodeFile(std::string filePath); 159 /** 160 * @brief Update work before real start. 161 * 162 * @param work The work. 163 */ 164 void UpdateWorkBeforeRealStart(std::shared_ptr<WorkStatus> work); 165 /** 166 * @brief The OnConditionReady callback. 167 * 168 * @param workStatusVector The work status vector. 169 */ 170 void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 171 /** 172 * @brief Watchdog time out. 173 * 174 * @param workStatus The status of work. 175 */ 176 void WatchdogTimeOut(std::shared_ptr<WorkStatus> workStatus); 177 /** 178 * @brief Init. 179 * 180 * @return True if success,else false. 181 */ 182 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 183 184 /** 185 * @brief Get handler. 186 * 187 * @return Handler. 188 */ GetHandler()189 std::shared_ptr<WorkEventHandler> GetHandler() 190 { 191 return handler_; 192 } 193 194 /** 195 * @brief Get work queue manager. 196 * 197 * @return Work queue manager. 198 */ GetWorkQueueManager()199 std::shared_ptr<WorkQueueManager> GetWorkQueueManager() 200 { 201 return workQueueManager_; 202 } 203 204 /** 205 * @brief Get work policy manager. 206 * 207 * @return work policy manager. 208 */ GetWorkPolicyManager()209 std::shared_ptr<WorkPolicyManager> GetWorkPolicyManager() 210 { 211 return workPolicyManager_; 212 } 213 214 /** 215 * @brief add uid to the whitelist or delete uid from the whitelist. 216 * 217 * @param uid uid of the app. 218 * @param isAdd true if add name, else delete. 219 */ 220 void UpdateEffiResApplyInfo(int32_t uid, bool isAdd); 221 222 /** 223 * @brief init background task subscriber, subscribe self to bgtask service 224 * 225 * @return true seccess to init subscriber 226 * @return false fail to init subscriber 227 */ 228 bool InitBgTaskSubscriber(); 229 230 /** 231 * @brief check uid has work_scheduler resources or not 232 * 233 * @param uid the uid of application 234 * @return true uid has work_scheduler resources or not 235 * @return false uid does not have work_scheduler resources or not 236 */ 237 bool CheckEffiResApplyInfo(int32_t uid); 238 239 /** 240 * @brief Get the efficiency resources uid List object 241 * 242 * @return std::string string consists of uid 243 */ 244 std::string GetEffiResApplyUid(); 245 246 /** 247 * @brief Get the Efficiency Resources Infos object 248 * 249 * @return ErrCode ERR_OK if succeed, others if failed 250 */ 251 ErrCode QueryResAppliedUid(); 252 /** 253 * @brief check bundleName has device_standby allow permission or not. 254 * 255 * @param bundleName bundleName of the application. 256 * @return true mean the application has device_stadnby allow permission, false or not. 257 */ 258 bool CheckStandbyApplyInfo(std::string& bundleName); 259 private: 260 std::set<int32_t> whitelist_; 261 std::mutex whitelistMutex_; 262 #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE 263 std::shared_ptr<SchedulerBgTaskSubscriber> subscriber_; 264 #endif 265 266 private: 267 std::shared_ptr<WorkQueueManager> workQueueManager_; 268 std::shared_ptr<WorkPolicyManager> workPolicyManager_; 269 std::mutex mutex_; 270 std::mutex observerMutex_; 271 std::map<std::string, std::shared_ptr<WorkInfo>> persistedMap_; 272 bool ready_ {false}; 273 std::shared_ptr<WorkEventHandler> handler_; 274 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 275 bool checkBundle_ {true}; 276 #ifdef DEVICE_USAGE_STATISTICS_ENABLE 277 sptr<WorkBundleGroupChangeCallback> groupObserver_; 278 #endif 279 #ifdef DEVICE_STANDBY_ENABLE 280 sptr<WorkStandbyStateChangeCallback> standbyStateObserver_; 281 #endif 282 void RegisterStandbyStateObserver(); 283 void WorkQueueManagerInit(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 284 bool WorkPolicyManagerInit(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 285 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 286 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 287 #ifdef DEVICE_USAGE_STATISTICS_ENABLE 288 void GroupObserverInit(); 289 #endif 290 std::list<std::shared_ptr<WorkInfo>> ReadPersistedWorks(); 291 void InitPersistedWork(WorkInfo& workInfo); 292 void DumpAllInfo(std::string &result); 293 bool CheckWorkInfo(WorkInfo &workInfo, int32_t &uid); 294 bool StopWorkInner(std::shared_ptr<WorkStatus> workStatus, int32_t uid, const bool needCancel, bool isTimeOut); 295 bool CheckCondition(WorkInfo& workInfo); 296 bool IsBaseAbilityReady(); 297 void DumpUsage(std::string &result); 298 void DumpParamSet(std::string &key, std::string &value, std::string &result); 299 }; 300 } // namespace WorkScheduler 301 } // namespace OHOS 302 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H