• 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_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "ipc_skeleton.h"
23 #include "iremote_object.h"
24 #include "resource_manager.h"
25 #include "singleton.h"
26 
27 #include "app_state_observer.h"
28 #include "bgtaskmgr_inner_errors.h"
29 #include "bundle_info.h"
30 #include "continuous_task_callback_info.h"
31 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
32 #include "task_notification_subscriber.h"
33 #endif
34 #include "continuous_task_info.h"
35 #include "continuous_task_param.h"
36 #include "continuous_task_record.h"
37 #include "ibackground_task_subscriber.h"
38 #include "remote_death_recipient.h"
39 #include "system_event_observer.h"
40 #include "config_change_observer.h"
41 #include "want.h"
42 
43 namespace OHOS {
44 namespace BackgroundTaskMgr {
45 namespace {
46     static constexpr uint32_t SA_ID_VOIP_CALL_MANAGER = 65968;
47     static constexpr uint32_t SA_ID_HEALTH_SPORT = 9527;
48 }
49 class BackgroundTaskMgrService;
50 class DataStorageHelper;
51 
52 enum class ContinuousTaskEventTriggerType: uint32_t {
53     TASK_START,
54     TASK_UPDATE,
55     TASK_CANCEL,
56     TASK_SUSPEND,
57     TASK_ACTIVE,
58 };
59 
60 
61 struct CachedBundleInfo {
62     std::unordered_map<std::string, uint32_t> abilityBgMode_ {};
63     std::string appName_ {""};
64 };
65 
66 struct SubscriberInfo {
SubscriberInfoSubscriberInfo67     SubscriberInfo(sptr<IBackgroundTaskSubscriber> subscriber, int uid, int pid, bool isHap, uint32_t flag)
68         : subscriber_(subscriber), uid_(uid), pid_(pid), isHap_(isHap), flag_(flag) {};
69     sptr<IBackgroundTaskSubscriber> subscriber_;
70     int uid_;
71     int pid_;
72     bool isHap_ {false};
73     uint32_t flag_ {0};
74 };
75 
76 class BgContinuousTaskMgr : public DelayedSingleton<BgContinuousTaskMgr>,
77                             public std::enable_shared_from_this<BgContinuousTaskMgr> {
78 public:
79     ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam);
80     ErrCode UpdateBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam);
81     ErrCode StopBackgroundRunning(const std::string &abilityName, int32_t abilityId);
82     ErrCode GetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> &list);
83     ErrCode GetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> &list, bool includeSuspended);
84     ErrCode RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
85     ErrCode RequestGetContinuousTasksByUidForInner(int32_t uid,
86         std::vector<std::shared_ptr<ContinuousTaskInfo>> &list);
87     ErrCode AddSubscriber(const std::shared_ptr<SubscriberInfo> subscriberInfo);
88     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
89     ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
90     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list, int32_t uid = -1);
91     ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false);
92     bool StopContinuousTaskByUser(const std::string &mapKey);
93     void OnAccountsStateChanged(int32_t id);
94     void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid);
95     void OnAbilityStateChanged(int32_t uid, const std::string &abilityName, int32_t abilityId);
96     void OnAppStopped(int32_t uid);
97     void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object);
98     bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
99     void InitNecessaryState();
100     void InitRequiredResourceInfo();
101     void Clear();
102     int32_t GetBgTaskUid();
103     void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
104     void SuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key);
105     void ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key);
106     void OnConfigurationChanged(const AppExecFwk::Configuration &configuration);
107     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
108     void HandleRemoveTaskByMode(uint32_t mode);
109 
110 private:
111     ErrCode StartBackgroundRunningInner(std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecordPtr);
112     ErrCode UpdateBackgroundRunningInner(const std::string &taskInfoMapKey,
113         const sptr<ContinuousTaskParam> &taskParam);
114     ErrCode StartBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
115     ErrCode StopBackgroundRunningInner(int32_t uid, const std::string &abilityName, int32_t abilityId);
116     ErrCode StopBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
117     ErrCode GetAllContinuousTasksInner(int32_t uid, std::vector<std::shared_ptr<ContinuousTaskInfo>> &list,
118         bool includeSuspended = true);
119     ErrCode CheckIsSysReadyAndPermission(int32_t callingUid);
120     ErrCode AddSubscriberInner(const std::shared_ptr<SubscriberInfo> subscriberInfo);
121     ErrCode RemoveSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber);
122     ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
123     ErrCode SendContinuousTaskNotification(std::shared_ptr<ContinuousTaskRecord> &ContinuousTaskRecordPtr);
124     ErrCode GetContinuousTaskAppsInner(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list, int32_t uid);
125     ErrCode AVSessionNotifyUpdateNotificationInner(int32_t uid, int32_t pid, bool isPublish = false);
126     void HandlePersistenceData();
127     void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses);
128     void DumpAllTaskInfo(std::vector<std::string> &dumpInfo);
129     void DumpCancelTask(const std::vector<std::string> &dumpOption, bool cleanAll);
130     void DumpGetTask(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
131     void DumpInnerTask(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
132     bool RemoveContinuousTaskRecord(const std::string &mapKey);
133     bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
134     bool CheckProcessUidInfo(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t uid);
135     uint32_t GetBackgroundModeInfo(int32_t uid, const std::string &abilityName);
136     bool AddAbilityBgModeInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
137     bool RegisterNotificationSubscriber();
138     bool RegisterSysCommEventListener();
139     bool RegisterAppStateObserver();
140     void UnregisterAppStateObserver();
141     bool RegisterConfigurationObserver();
142     bool GetNotificationPrompt();
143     bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName);
144     void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
145     void HandleSuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key);
146     void HandleActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key);
147     void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object);
148     void OnContinuousTaskChanged(const std::shared_ptr<ContinuousTaskRecord> continuousTaskInfo,
149         ContinuousTaskEventTriggerType changeEventType);
150     ErrCode CheckBgmodeType(uint32_t configuredBgMode, uint32_t requestedBgModeId, bool isNewApi,
151         const std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord);
152     bool AllowUseTaskKeeping(const std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord);
153     ErrCode CheckBgmodeTypeForInner(uint32_t requestedBgModeId);
154     void InitRecordParam(std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord,
155         const sptr<ContinuousTaskParam> &taskParam, int32_t userId);
156     ErrCode CheckSubMode(const std::shared_ptr<AAFwk::Want> want, std::shared_ptr<ContinuousTaskRecord> record);
157     ErrCode CheckNotificationText(std::string &notificationText,
158         const std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord);
159     int32_t RefreshTaskRecord();
160     void HandleAppContinuousTaskStop(int32_t uid);
161     bool checkPidCondition(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t pid);
162     bool checkNotificationCondition(const std::set<std::string> &notificationLabels, const std::string &label);
163     std::shared_ptr<Global::Resource::ResourceManager> GetBundleResMgr(const AppExecFwk::BundleInfo &bundleInfo);
164     std::string GetMainAbilityLabel(const std::string &bundleName, int32_t userId);
165     std::string GetNotificationText(const std::shared_ptr<ContinuousTaskRecord> record);
166     void RemoveContinuousTaskRecordByUidAndMode(int32_t uid, uint32_t mode);
167     void RemoveContinuousTaskRecordByUid(int32_t uid);
168     void ReclaimProcessMemory(int32_t pid);
169     void SetReason(const std::string &mapKey, int32_t reason);
170     uint32_t GetModeNumByTypeIds(const std::vector<uint32_t> &typeIds);
171     void NotifySubscribers(ContinuousTaskEventTriggerType changeEventType,
172         const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo);
173     void NotifySubscribersTaskSuspend(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo);
174     void NotifySubscribersTaskActive(const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo);
175     void ReportHisysEvent(ContinuousTaskEventTriggerType changeEventType,
176         const std::shared_ptr<ContinuousTaskRecord> &continuousTaskInfo);
177     bool CanNotifyHap(const std::shared_ptr<SubscriberInfo> subscriberInfo,
178         const std::shared_ptr<ContinuousTaskCallbackInfo> &callbackInfo);
179     bool IsExistCallback(int32_t uid, uint32_t type);
180 private:
181     std::atomic<bool> isSysReady_ {false};
182     int32_t bgTaskUid_ {-1};
183     std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr};
184     std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap_ {};
185     std::unordered_map<int32_t, bool> avSessionNotification_ {};
186 
187 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
188     std::shared_ptr<TaskNotificationSubscriber> subscriber_ {nullptr};
189 #endif
190     std::shared_ptr<SystemEventObserver> systemEventListener_ {nullptr};
191     sptr<AppStateObserver> appStateObserver_ {nullptr};
192     sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ {nullptr};
193     std::list<std::shared_ptr<SubscriberInfo>> bgTaskSubscribers_ {};
194     sptr<RemoteDeathRecipient> susriberDeathRecipient_ {nullptr};
195     std::unordered_map<int32_t, CachedBundleInfo> cachedBundleInfos_ {};
196     std::vector<std::string> continuousTaskText_ {};
197     std::vector<std::string> continuousTaskSubText_ {};
198     int32_t continuousTaskIdIndex_ = 0;
199 
200     DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr);
201 };
202 }  // namespace BackgroundTaskMgr
203 }  // namespace OHOS
204 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H