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_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 #include <atomic> 26 27 #include <iremote_object.h> 28 #include <system_ability.h> 29 30 #include "ability_manager_interface.h" 31 #include "system_ability_status_change_stub.h" 32 #include "work_sched_service_stub.h" 33 #include "work_status.h" 34 #include "work_event_handler.h" 35 #include "singleton.h" 36 #include "work_standby_state_change_callback.h" 37 #include "ffrt.h" 38 39 namespace OHOS { 40 namespace WorkScheduler { 41 class WorkQueueManager; 42 class WorkPolicyManager; 43 class WorkBundleGroupChangeCallback; 44 class SchedulerBgTaskSubscriber; 45 class WorkSchedulerService final : public SystemAbility, public WorkSchedServiceStub, 46 public std::enable_shared_from_this<WorkSchedulerService> { 47 DISALLOW_COPY_AND_MOVE(WorkSchedulerService); 48 DECLARE_SYSTEM_ABILITY(WorkSchedulerService); 49 DECLARE_DELAYED_SINGLETON(WorkSchedulerService); 50 public: 51 WorkSchedulerService(const int32_t systemAbilityId, bool runOnCreate); 52 53 /** 54 * @brief The OnStart callback. 55 */ 56 void OnStart() override; 57 /** 58 * @brief The OnStop callback. 59 */ 60 void OnStop() override; 61 /** 62 * @brief Start work. 63 * 64 * @param workInfo The info of work. 65 * @return error code, ERR_OK if success. 66 */ 67 int32_t StartWork(const WorkInfo& workInfo) override; 68 /** 69 * @brief Start work for inner. 70 * 71 * @param workInfo The info of work. 72 * @return error code, ERR_OK if success. 73 */ 74 int32_t StartWorkForInner(const WorkInfo& workInfo) override; 75 /** 76 * @brief Stop work. 77 * 78 * @param workInfo The info of work. 79 * @return error code, ERR_OK if success. 80 */ 81 int32_t StopWork(const WorkInfo& workInfo) override; 82 /** 83 * @brief Stop work for inner. 84 * 85 * @param workInfo The info of work. 86 * @param needCancel True if force stop the work that is being executed, else false. 87 * @return error code, ERR_OK if success. 88 */ 89 int32_t StopWorkForInner(const WorkInfo& workInfo, bool needCancel) override; 90 /** 91 * @brief Stop and cancel work. 92 * 93 * @param workInfo The info of work. 94 * @return error code, ERR_OK if success. 95 */ 96 int32_t StopAndCancelWork(const WorkInfo& workInfo) override; 97 /** 98 * @brief Stop and clear works. 99 * 100 * @return error code, ERR_OK if success. 101 */ 102 int32_t StopAndClearWorks() override; 103 /** 104 * @brief The last work time out. 105 * 106 * @param workId The id of work. 107 * @param result True if the work executed time out, else false. 108 * @return error code, ERR_OK if success. 109 */ 110 int32_t IsLastWorkTimeout(int32_t workId, bool &result) override; 111 /** 112 * @brief Obtain all works. 113 * 114 * @param workInfos The infos of work. 115 * @return error code, ERR_OK if success. 116 */ 117 int32_t ObtainAllWorks(std::vector<WorkInfo>& workInfos) override; 118 /** 119 * @brief Obtain works by uid and workId for inner. 120 * @param uid The uid. 121 * @param workInfos The infos of work. 122 * @param workId The id of work. 123 * @return error code, ERR_OK if success. 124 */ 125 int32_t ObtainWorksByUidAndWorkIdForInner(int32_t uid, std::vector<WorkInfo>& workInfos, int32_t workId) override; 126 /** 127 * @brief Get the status of work. 128 * 129 * @param workId The id of work. 130 * @param workInfo The info of work. 131 * @return error code, ERR_OK if success. 132 */ 133 int32_t GetWorkStatus(int32_t workId, WorkInfo& workInfo) override; 134 /** 135 * @brief Get the Running Work Scheduler Work object 136 * 137 * @param workInfos The infos of work. 138 * @return ErrCode ERR_OK on success, others on failure 139 */ 140 int32_t GetAllRunningWorks(std::vector<WorkInfo>& workInfos) override; 141 142 /** 143 * @brief Pause Running Works. 144 * 145 * @param uid The uid. 146 * @return The errcode. ERR_OK on success, others on failure. 147 */ 148 int32_t PauseRunningWorks(int32_t uid) override; 149 150 /** 151 * @brief Resume Paused works. 152 * 153 * @param uid The uid. 154 * @return ErrCode ERR_OK on success, others on failure 155 */ 156 int32_t ResumePausedWorks(int32_t uid) override; 157 158 /** 159 * @brief Dump. 160 * 161 * @param fd The file descriptor. 162 * @param args The dump args. 163 * @return Status code, ERR_OK if success. 164 */ 165 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 166 /** 167 * @brief Refresh persisted works. 168 */ 169 void RefreshPersistedWorks(); 170 /** 171 * @brief Stop and clear works by uid. 172 * 173 * @param uid The uid. 174 * @return True if success,else false. 175 */ 176 bool StopAndClearWorksByUid(int32_t uid); 177 /** 178 * @brief Create node dir. 179 * 180 * @param dir The dir. 181 * @return ERR_OK. 182 */ 183 int32_t CreateNodeDir(std::string dir); 184 /** 185 * @brief Create node file. 186 * 187 * @return ERR_OK. 188 */ 189 int32_t CreateNodeFile(); 190 /** 191 * @brief Update work before real start. 192 * 193 * @param work The work. 194 */ 195 void UpdateWorkBeforeRealStart(std::shared_ptr<WorkStatus> work); 196 /** 197 * @brief The OnConditionReady callback. 198 * 199 * @param workStatusVector The work status vector. 200 */ 201 void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 202 /** 203 * @brief Watchdog time out. 204 * 205 * @param workStatus The status of work. 206 */ 207 void WatchdogTimeOut(std::shared_ptr<WorkStatus> workStatus); 208 /** 209 * @brief Init. 210 * 211 * @return True if success,else false. 212 */ 213 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 214 215 /** 216 * @brief Get handler. 217 * 218 * @return Handler. 219 */ GetHandler()220 std::shared_ptr<WorkEventHandler> GetHandler() 221 { 222 return handler_; 223 } 224 225 /** 226 * @brief Get work queue manager. 227 * 228 * @return Work queue manager. 229 */ GetWorkQueueManager()230 std::shared_ptr<WorkQueueManager> GetWorkQueueManager() 231 { 232 return workQueueManager_; 233 } 234 235 /** 236 * @brief Get work policy manager. 237 * 238 * @return work policy manager. 239 */ GetWorkPolicyManager()240 std::shared_ptr<WorkPolicyManager> GetWorkPolicyManager() 241 { 242 return workPolicyManager_; 243 } 244 245 /** 246 * @brief add uid to the whitelist or delete uid from the whitelist. 247 * 248 * @param uid uid of the app. 249 * @param isAdd true if add name, else delete. 250 */ 251 void UpdateEffiResApplyInfo(int32_t uid, bool isAdd); 252 253 /** 254 * @brief init background task subscriber, subscribe self to bgtask service 255 * 256 * @return true seccess to init subscriber 257 * @return false fail to init subscriber 258 */ 259 bool InitBgTaskSubscriber(); 260 261 /** 262 * @brief check uid has work_scheduler resources or not 263 * 264 * @param uid the uid of application 265 * @return true uid has work_scheduler resources or not 266 * @return false uid does not have work_scheduler resources or not 267 */ 268 bool CheckEffiResApplyInfo(int32_t uid); 269 270 /** 271 * @brief Get the efficiency resources uid List object 272 * 273 * @return std::string string consists of uid 274 */ 275 std::string DumpEffiResApplyUid(); 276 277 /** 278 * @brief Get the Efficiency Resources Infos object 279 * 280 * @return ErrCode ERR_OK if succeed, others if failed 281 */ 282 ErrCode QueryResAppliedUid(); 283 /** 284 * @brief init the preinstalled work. 285 */ 286 void InitPreinstalledWork(); 287 void TriggerWorkIfConditionReady(); 288 /** 289 * @brief stop deepIdle works. 290 * 291 * @return success or fail. 292 */ 293 int32_t StopDeepIdleWorks(); 294 /** 295 * @brief load SA. 296 * @return success or fail. 297 */ 298 bool LoadSa(std::shared_ptr<WorkStatus> workStatus, const std::string& action); 299 /** 300 * @brief Handle DeepIdle callback Msg. 301 */ 302 void HandleDeepIdleMsg(); 303 /** 304 * @brief Check If The bundle is in the whitelist. 305 * 306 * @param bundle Need Check bundleName 307 * @return true or false. 308 */ 309 bool IsExemptionBundle(const std::string& checkBundleName); 310 311 /** 312 * @brief Set work scheduler config. 313 * 314 * @param configData config param. 315 * @param sourceType data source. 316 * @return ErrCode ERR_OK on success, others on failure 317 */ 318 int32_t SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType) override; 319 void InitDeviceStandyWhitelist(); 320 void InitDeviceStandyRestrictlist(); 321 bool IsPreinstalledBundle(const std::string& checkBundleName); 322 /** 323 * @brief Stop SA. 324 * 325 * @param saId SA id. 326 * @return ErrCode ERR_OK on success, others on failure 327 */ 328 int32_t StopWorkForSA(int32_t saId) override; 329 bool StopWorkInner(std::shared_ptr<WorkStatus> workStatus, int32_t uid, const bool needCancel, bool isTimeOut); 330 private: 331 void RegisterStandbyStateObserver(); 332 void WorkQueueManagerInit(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 333 bool WorkPolicyManagerInit(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 334 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 335 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 336 #ifdef DEVICE_USAGE_STATISTICS_ENABLE 337 void GroupObserverInit(); 338 #endif 339 std::list<std::shared_ptr<WorkInfo>> ReadPersistedWorks(); 340 void DumpAllInfo(std::string& result); 341 bool CheckWorkInfo(WorkInfo& workInfo, int32_t& uid); 342 int32_t StartWorkInner(const WorkInfo& workInfo, int32_t uid); 343 bool CheckCondition(WorkInfo& workInfo); 344 bool IsBaseAbilityReady(); 345 void DumpUsage(std::string& result); 346 void DumpParamSet(std::string& key, std::string& value, std::string& result); 347 void DumpProcessWorks(const std::string& bundleName, const std::string& abilityName, std::string& result); 348 void DumpRunningWorks(const std::string& uidStr, const std::string& option, std::string& result); 349 bool IsDebugApp(const std::string& bundleName); 350 bool AllowDump(); 351 void DumpProcessForEngMode(std::vector<std::string>& argsInStr, std::string& result); 352 void DumpProcessForUserMode(std::vector<std::string>& argsInStr, std::string& result); 353 bool GetJsonFromFile(const char* filePath, nlohmann::json& root); 354 bool GetUidByBundleName(const std::string& bundleName, int32_t& uid); 355 void InitWorkInner(); 356 void AddWorkInner(WorkInfo& workInfo); 357 std::list<std::shared_ptr<WorkInfo>> ReadPreinstalledWorks(); 358 void LoadWorksFromFile(const char *path, std::list<std::shared_ptr<WorkInfo>> &workInfos); 359 void LoadExemptionBundlesFromFile(const char *path); 360 void InitPersistedWork(); 361 bool CheckProcessName(); 362 bool CheckCallingServiceName(); 363 bool GetAppIndexAndBundleNameByUid(int32_t uid, int32_t &appIndex, std::string &bundleName); 364 bool CheckExtensionInfos(WorkInfo &workInfo, int32_t uid); 365 void DumpLoadSaWorks(const std::string &saIdStr, const std::string &uidStr, std::string &result); 366 void DumpGetWorks(const std::string &uidStr, const std::string &workIdStr, std::string &result); 367 std::string DumpExemptionBundles(); 368 void LoadMinRepeatTimeFromFile(const char *path); 369 int32_t SetTimer(); 370 void CancelTimer(int32_t id); 371 bool CheckCallingToken(); 372 void DumpTriggerWork(const std::string& uIdStr, const std::string& workIdStr, std::string& result); 373 void ReportUserDataSizeEvent(); 374 375 private: 376 std::set<int32_t> whitelist_; 377 ffrt::mutex whitelistMutex_; 378 #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE 379 std::shared_ptr<SchedulerBgTaskSubscriber> subscriber_; 380 #endif 381 std::shared_ptr<WorkQueueManager> workQueueManager_; 382 std::shared_ptr<WorkPolicyManager> workPolicyManager_; 383 ffrt::mutex mutex_; 384 ffrt::mutex observerMutex_; 385 std::map<std::string, std::shared_ptr<WorkInfo>> persistedMap_; 386 bool ready_ {false}; 387 std::shared_ptr<WorkEventHandler> handler_; 388 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 389 bool checkBundle_ {true}; 390 std::set<std::string> exemptionBundles_; 391 std::set<std::string> preinstalledBundles_; 392 #ifdef DEVICE_USAGE_STATISTICS_ENABLE 393 sptr<WorkBundleGroupChangeCallback> groupObserver_; 394 #endif 395 #ifdef DEVICE_STANDBY_ENABLE 396 sptr<WorkStandbyStateChangeCallback> standbyStateObserver_; 397 #endif 398 uint32_t minTimeCycle_ = 20 * 60 * 1000; 399 uint32_t minCheckTime_ = 0; 400 ffrt::mutex specialMutex_; 401 std::map<std::string, uint32_t> specialMap_; 402 }; 403 } // namespace WorkScheduler 404 } // namespace OHOS 405 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H 406