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::string& reason, 63 const sptr<IExpiredCallback>& callback, DelaySuspendInfo &delayInfo) override; 64 ErrCode CancelSuspendDelay(int32_t requestId) override; 65 ErrCode GetRemainingDelayTime(int32_t requestId, int32_t &delayTime) override; 66 // notificationId和continuousTaskId为idl化整改工具能力暂不具备而添加,在proxy中使用 67 ErrCode StartBackgroundRunning(const ContinuousTaskParam &taskParam, int32_t& notificationId, 68 int32_t& continuousTaskId) override; 69 ErrCode UpdateBackgroundRunning(const ContinuousTaskParam &taskParam, int32_t& notificationId, 70 int32_t& continuousTaskId) override; 71 ErrCode RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam) override; 72 ErrCode StopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken, 73 int32_t abilityId) override; 74 ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override; 75 ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override; 76 ErrCode GetTransientTaskApps(std::vector<TransientTaskAppInfo> &list) override; 77 ErrCode PauseTransientTaskTimeForInner(int32_t uid) override; 78 ErrCode StartTransientTaskTimeForInner(int32_t uid) override; 79 ErrCode GetContinuousTaskApps(std::vector<ContinuousTaskCallbackInfo> &list) override; 80 ErrCode ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo) override; 81 ErrCode ResetAllEfficiencyResources() override; 82 ErrCode GetEfficiencyResourcesInfos(std::vector<ResourceCallbackInfo> &appList, 83 std::vector<ResourceCallbackInfo> &procList) override; 84 ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key) override; 85 ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override; 86 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 87 88 void ForceCancelSuspendDelay(int32_t requestId); 89 void HandleRequestExpired(const int32_t requestId); 90 void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote); 91 void HandleSubscriberDeath(const wptr<IRemoteObject>& remote); 92 93 private: 94 void Init(); 95 void DumpUsage(std::string &result); 96 bool AllowDump(); 97 bool CheckCallingToken(); 98 bool CheckHapCalling(bool &isHap); 99 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 100 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 101 102 private: 103 ServiceRunningState state_ {ServiceRunningState::STATE_NOT_START}; 104 std::shared_ptr<AppExecFwk::EventRunner> runner_ {nullptr}; 105 std::mutex readyMutex_; 106 uint32_t dependsReady_ {0}; 107 }; 108 } // namespace BackgroundTaskMgr 109 } // namespace OHOS 110 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H