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_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H 18 19 #include <ctime> 20 #include <list> 21 #include <memory> 22 #include <mutex> 23 24 #include "event_handler.h" 25 #include "event_runner.h" 26 #include "refbase.h" 27 #include "singleton.h" 28 #include "system_ability.h" 29 #include "system_ability_definition.h" 30 31 #include "background_task_mgr_stub.h" 32 #include "bg_continuous_task_mgr.h" 33 #include "bg_transient_task_mgr.h" 34 #include "bgtaskmgr_inner_errors.h" 35 #include "bg_efficiency_resources_mgr.h" 36 37 namespace OHOS { 38 namespace BackgroundTaskMgr { 39 enum class ServiceRunningState { 40 STATE_NOT_START, 41 STATE_RUNNING 42 }; 43 44 enum ServiceReadyState { 45 TRANSIENT_SERVICE_READY = 1 << 0, 46 CONTINUOUS_SERVICE_READY = 1 << 1, 47 EFFICIENCY_RESOURCES_SERVICE_READY = 1 << 2, 48 ALL_READY = TRANSIENT_SERVICE_READY | CONTINUOUS_SERVICE_READY | EFFICIENCY_RESOURCES_SERVICE_READY 49 }; 50 51 class BackgroundTaskMgrService final : public SystemAbility, public BackgroundTaskMgrStub, 52 public std::enable_shared_from_this<BackgroundTaskMgrService> { 53 DISALLOW_COPY_AND_MOVE(BackgroundTaskMgrService); 54 DECLARE_SYSTEM_ABILITY(BackgroundTaskMgrService); 55 DECLARE_DELAYED_SINGLETON(BackgroundTaskMgrService); 56 public: 57 BackgroundTaskMgrService(const int32_t systemAbilityId, bool runOnCreate); 58 void OnStart() final; 59 void OnStop() final; 60 void SetReady(uint32_t flag); 61 62 ErrCode RequestSuspendDelay(const std::u16string& reason, 63 const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo) override; 64 ErrCode CancelSuspendDelay(int32_t requestId) override; 65 ErrCode GetRemainingDelayTime(int32_t requestId, int32_t &delayTime) override; 66 ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam) override; 67 ErrCode UpdateBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam) override; 68 ErrCode RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam) override; 69 ErrCode StopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken, 70 int32_t abilityId) override; 71 ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override; 72 ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override; 73 ErrCode GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list) override; 74 ErrCode PauseTransientTaskTimeForInner(int32_t uid) override; 75 ErrCode StartTransientTaskTimeForInner(int32_t uid) override; 76 ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list) override; 77 ErrCode ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo) override; 78 ErrCode ResetAllEfficiencyResources() override; 79 ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList, 80 std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList) override; 81 ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key) override; 82 ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override; 83 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 84 85 void ForceCancelSuspendDelay(int32_t requestId); 86 void HandleRequestExpired(const int32_t requestId); 87 void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote); 88 void HandleSubscriberDeath(const wptr<IRemoteObject>& remote); 89 90 private: 91 void Init(); 92 void DumpUsage(std::string &result); 93 bool AllowDump(); 94 bool CheckCallingToken(); 95 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 96 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 97 98 private: 99 ServiceRunningState state_ {ServiceRunningState::STATE_NOT_START}; 100 std::shared_ptr<AppExecFwk::EventRunner> runner_ {nullptr}; 101 std::mutex readyMutex_; 102 uint32_t dependsReady_ {0}; 103 }; 104 } // namespace BackgroundTaskMgr 105 } // namespace OHOS 106 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H