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