• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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_param.h"
35 #include "continuous_task_record.h"
36 #include "ibackground_task_subscriber.h"
37 #include "remote_death_recipient.h"
38 #include "system_event_observer.h"
39 #include "config_change_observer.h"
40 
41 namespace OHOS {
42 namespace BackgroundTaskMgr {
43 namespace {
44     static constexpr uint32_t SA_ID_VOIP_CALL_MANAGER = 65968;
45 }
46 class BackgroundTaskMgrService;
47 class DataStorageHelper;
48 
49 enum class ContinuousTaskEventTriggerType: uint32_t {
50     TASK_START,
51     TASK_UPDATE,
52     TASK_CANCEL,
53 };
54 
55 
56 struct CachedBundleInfo {
57     std::unordered_map<std::string, uint32_t> abilityBgMode_ {};
58     std::string appName_ {""};
59 };
60 
61 struct SubscriberInfo {
SubscriberInfoSubscriberInfo62     SubscriberInfo(sptr<IBackgroundTaskSubscriber> subscriber, int uid, int pid, bool isHap)
63         : subscriber_(subscriber), uid_(uid), pid_(pid), isHap_(isHap) {};
64     sptr<IBackgroundTaskSubscriber> subscriber_;
65     int uid_;
66     int pid_;
67     bool isHap_ {false};
68 };
69 
70 class BgContinuousTaskMgr : public DelayedSingleton<BgContinuousTaskMgr>,
71                             public std::enable_shared_from_this<BgContinuousTaskMgr> {
72 public:
73     ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam);
74     ErrCode UpdateBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam);
75     ErrCode StopBackgroundRunning(const std::string &abilityName, int32_t abilityId);
76     ErrCode RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
77     ErrCode AddSubscriber(const std::shared_ptr<SubscriberInfo> subscriberInfo);
78     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
79     ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
80     ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
81     bool StopContinuousTaskByUser(const std::string &mapKey);
82     void OnAccountsStateChanged(int32_t id);
83     void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid);
84     void OnAbilityStateChanged(int32_t uid, const std::string &abilityName, int32_t abilityId);
85     void OnAppStopped(int32_t uid);
86     void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object);
87     bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
88     void InitNecessaryState();
89     void InitRequiredResourceInfo();
90     void Clear();
91     int32_t GetBgTaskUid();
92     void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
93     void OnConfigurationChanged(const AppExecFwk::Configuration &configuration);
94     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
95     void HandleVoipTaskRemove();
96 
97 private:
98     ErrCode StartBackgroundRunningInner(std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecordPtr);
99     ErrCode UpdateBackgroundRunningInner(const std::string &taskInfoMapKey,
100         const sptr<ContinuousTaskParam> &taskParam);
101     ErrCode StartBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
102     ErrCode StopBackgroundRunningInner(int32_t uid, const std::string &abilityName, int32_t abilityId);
103     ErrCode StopBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam);
104     ErrCode AddSubscriberInner(const std::shared_ptr<SubscriberInfo> subscriberInfo);
105     ErrCode RemoveSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber);
106     ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
107     ErrCode SendContinuousTaskNotification(std::shared_ptr<ContinuousTaskRecord> &ContinuousTaskRecordPtr);
108     ErrCode GetContinuousTaskAppsInner(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list);
109     void HandlePersistenceData();
110     void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses);
111     void DumpAllTaskInfo(std::vector<std::string> &dumpInfo);
112     void DumpCancelTask(const std::vector<std::string> &dumpOption, bool cleanAll);
113     bool RemoveContinuousTaskRecord(const std::string &mapKey);
114     bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
115     bool CheckProcessUidInfo(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t uid);
116     uint32_t GetBackgroundModeInfo(int32_t uid, const std::string &abilityName);
117     bool AddAbilityBgModeInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo);
118     bool RegisterNotificationSubscriber();
119     bool RegisterSysCommEventListener();
120     bool RegisterAppStateObserver();
121     void UnregisterAppStateObserver();
122     bool RegisterConfigurationObserver();
123     bool GetNotificationPrompt();
124     bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName);
125     void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key);
126     void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object);
127     void OnContinuousTaskChanged(const std::shared_ptr<ContinuousTaskRecord> continuousTaskInfo,
128         ContinuousTaskEventTriggerType changeEventType);
129     ErrCode CheckBgmodeType(uint32_t configuredBgMode, uint32_t requestedBgModeId, bool isNewApi,
130         uint64_t fullTokenId);
131     ErrCode CheckBgmodeTypeForInner(uint32_t requestedBgModeId);
132     int32_t RefreshTaskRecord();
133     void HandleAppContinuousTaskStop(int32_t uid);
134     bool checkPidCondition(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t pid);
135     bool checkNotificationCondition(const std::set<std::string> &notificationLabels, const std::string &label);
136     std::shared_ptr<Global::Resource::ResourceManager> GetBundleResMgr(const AppExecFwk::BundleInfo &bundleInfo);
137     std::string GetMainAbilityLabel(const std::string &bundleName, int32_t userId);
138     void RemoveContinuousTaskRecordByUidAndMode(int32_t uid, uint32_t mode);
139     void RemoveContinuousTaskRecordByUid(int32_t uid);
140     void ReclaimProcessMemory(int32_t pid);
141     void SetReason(const std::string &mapKey, int32_t reason);
142     uint32_t GetModeNumByTypeIds(const std::vector<uint32_t> &typeIds);
143     void NotifySubscribers(ContinuousTaskEventTriggerType changeEventType,
144         const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo);
145     void ReportHisysEvent(ContinuousTaskEventTriggerType changeEventType,
146         const std::shared_ptr<ContinuousTaskRecord> &continuousTaskInfo);
147     bool CanNotifyHap(const std::shared_ptr<SubscriberInfo> subscriberInfo,
148         const std::shared_ptr<ContinuousTaskCallbackInfo> &callbackInfo);
149 private:
150     std::atomic<bool> isSysReady_ {false};
151     std::string deviceType_ {""};
152     int32_t bgTaskUid_ {-1};
153     std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr};
154     std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap_ {};
155 
156 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
157     std::shared_ptr<TaskNotificationSubscriber> subscriber_ {nullptr};
158 #endif
159     std::shared_ptr<SystemEventObserver> systemEventListener_ {nullptr};
160     sptr<AppStateObserver> appStateObserver_ {nullptr};
161     sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ {nullptr};
162     std::list<std::shared_ptr<SubscriberInfo>> bgTaskSubscribers_ {};
163     sptr<RemoteDeathRecipient> susriberDeathRecipient_ {nullptr};
164     std::unordered_map<int32_t, CachedBundleInfo> cachedBundleInfos_ {};
165     std::vector<std::string> continuousTaskText_ {};
166     int32_t continuousTaskIdIndex_ = 0;
167 
168     DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr);
169 };
170 }  // namespace BackgroundTaskMgr
171 }  // namespace OHOS
172 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H