• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     ErrCode GetAllTransientTasks(int32_t &remainingQuota,
67         std::vector<std::shared_ptr<DelaySuspendInfo>> &list) override;
68     // notificationId和continuousTaskId为idl化整改工具能力暂不具备而添加,在proxy中使用
69     ErrCode StartBackgroundRunning(const ContinuousTaskParam &taskParam, int32_t& notificationId,
70         int32_t& continuousTaskId) override;
71     ErrCode UpdateBackgroundRunning(const ContinuousTaskParam &taskParam, int32_t& notificationId,
72         int32_t& continuousTaskId) override;
73     ErrCode RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam) override;
74     ErrCode RequestGetContinuousTasksByUidForInner(int32_t uid, std::vector<ContinuousTaskInfo> &list) override;
75     ErrCode StopBackgroundRunning(const std::string &abilityName, const sptr<IRemoteObject> &abilityToken,
76         int32_t abilityId) override;
77     ErrCode GetAllContinuousTasks(std::vector<ContinuousTaskInfo> &list) override;
78     ErrCode GetAllContinuousTasks(
79         std::vector<std::shared_ptr<ContinuousTaskInfo>> &list, bool includeSuspended) override;
80     ErrCode SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber, uint32_t flag) override;
81     ErrCode UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber) override;
82     ErrCode GetTransientTaskApps(std::vector<TransientTaskAppInfo> &list) override;
83     ErrCode PauseTransientTaskTimeForInner(int32_t uid) override;
84     ErrCode StartTransientTaskTimeForInner(int32_t uid) override;
85     ErrCode GetContinuousTaskApps(std::vector<ContinuousTaskCallbackInfo> &list) override;
86     ErrCode ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo) override;
87     ErrCode ResetAllEfficiencyResources() override;
88     ErrCode GetEfficiencyResourcesInfos(std::vector<ResourceCallbackInfo> &appList,
89         std::vector<ResourceCallbackInfo> &procList) override;
90     ErrCode GetAllEfficiencyResources(std::vector<EfficiencyResourceInfo> &resourceInfoList) override;
91     ErrCode StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key) override;
92     ErrCode SuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key) override;
93     ErrCode ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key) override;
94     ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false) override;
95     ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override;
96     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
97 
98     void ForceCancelSuspendDelay(int32_t requestId);
99     void HandleRequestExpired(const int32_t requestId);
100     void HandleExpiredCallbackDeath(const wptr<IRemoteObject>& remote);
101     void HandleSubscriberDeath(const wptr<IRemoteObject>& remote);
102 
103 private:
104     void Init();
105     void DumpUsage(std::string &result);
106     bool AllowDump();
107     bool CheckCallingToken();
108     bool CheckHapCalling(bool &isHap);
109     bool CheckCallingProcess();
110     bool CheckAtomicService();
111     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
112     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
113 
114 private:
115     ServiceRunningState state_ {ServiceRunningState::STATE_NOT_START};
116     std::shared_ptr<AppExecFwk::EventRunner> runner_ {nullptr};
117     std::mutex readyMutex_;
118     uint32_t dependsReady_ {0};
119 };
120 }  // namespace BackgroundTaskMgr
121 }  // namespace OHOS
122 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_MGR_SERVICE_H