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 class BgContinuousTaskMgr : public DelayedSingleton<BgContinuousTaskMgr>, 62 public std::enable_shared_from_this<BgContinuousTaskMgr> { 63 public: 64 ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam); 65 ErrCode UpdateBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam); 66 ErrCode StopBackgroundRunning(const std::string &abilityName, int32_t abilityId); 67 ErrCode RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 68 ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 69 ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 70 ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 71 ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list); 72 bool StopContinuousTaskByUser(const std::string &mapKey); 73 void OnAccountsStateChanged(int32_t id); 74 void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid); 75 void OnAbilityStateChanged(int32_t uid, const std::string &abilityName, int32_t abilityId); 76 void OnAppStopped(int32_t uid); 77 void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object); 78 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 79 void InitNecessaryState(); 80 void InitRequiredResourceInfo(); 81 void Clear(); 82 int32_t GetBgTaskUid(); 83 void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key); 84 void OnConfigurationChanged(const AppExecFwk::Configuration &configuration); 85 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 86 void HandleVoipTaskRemove(); 87 88 private: 89 ErrCode StartBackgroundRunningInner(std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecordPtr); 90 ErrCode UpdateBackgroundRunningInner(const std::string &taskInfoMapKey, 91 const sptr<ContinuousTaskParam> &taskParam); 92 ErrCode StartBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 93 ErrCode StopBackgroundRunningInner(int32_t uid, const std::string &abilityName, int32_t abilityId); 94 ErrCode StopBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 95 ErrCode AddSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber); 96 ErrCode RemoveSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber); 97 ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 98 ErrCode SendContinuousTaskNotification(std::shared_ptr<ContinuousTaskRecord> &ContinuousTaskRecordPtr); 99 ErrCode GetContinuousTaskAppsInner(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list); 100 void HandlePersistenceData(); 101 void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses); 102 void DumpAllTaskInfo(std::vector<std::string> &dumpInfo); 103 void DumpCancelTask(const std::vector<std::string> &dumpOption, bool cleanAll); 104 bool RemoveContinuousTaskRecord(const std::string &mapKey); 105 bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo); 106 bool CheckProcessUidInfo(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t uid); 107 uint32_t GetBackgroundModeInfo(int32_t uid, const std::string &abilityName); 108 bool AddAbilityBgModeInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo); 109 bool RegisterNotificationSubscriber(); 110 bool RegisterSysCommEventListener(); 111 bool RegisterAppStateObserver(); 112 void UnregisterAppStateObserver(); 113 bool RegisterConfigurationObserver(); 114 bool GetNotificationPrompt(); 115 bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName); 116 void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key); 117 void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object); 118 void OnContinuousTaskChanged(const std::shared_ptr<ContinuousTaskRecord> continuousTaskInfo, 119 ContinuousTaskEventTriggerType changeEventType); 120 ErrCode CheckBgmodeType(uint32_t configuredBgMode, uint32_t requestedBgModeId, bool isNewApi, 121 uint64_t fullTokenId); 122 ErrCode CheckBgmodeTypeForInner(uint32_t requestedBgModeId); 123 int32_t RefreshTaskRecord(); 124 void HandleAppContinuousTaskStop(int32_t uid); 125 bool checkPidCondition(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t pid); 126 bool checkNotificationCondition(const std::set<std::string> ¬ificationLabels, const std::string &label); 127 std::shared_ptr<Global::Resource::ResourceManager> GetBundleResMgr(const AppExecFwk::BundleInfo &bundleInfo); 128 std::string GetMainAbilityLabel(const std::string &bundleName, int32_t userId); 129 void RemoveContinuousTaskRecordByUidAndMode(int32_t uid, uint32_t mode); 130 void RemoveContinuousTaskRecordByUid(int32_t uid); 131 void ReclaimProcessMemory(int32_t pid); 132 void SetReason(const std::string &mapKey, int32_t reason); 133 uint32_t GetModeNumByTypeIds(const std::vector<uint32_t> &typeIds); 134 void NotifySubscribers(ContinuousTaskEventTriggerType changeEventType, 135 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo); 136 void ReportHisysEvent(ContinuousTaskEventTriggerType changeEventType, 137 const std::shared_ptr<ContinuousTaskRecord> &continuousTaskInfo); 138 private: 139 std::atomic<bool> isSysReady_ {false}; 140 std::string deviceType_ {""}; 141 int32_t bgTaskUid_ {-1}; 142 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 143 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap_ {}; 144 145 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE 146 std::shared_ptr<TaskNotificationSubscriber> subscriber_ {nullptr}; 147 #endif 148 std::shared_ptr<SystemEventObserver> systemEventListener_ {nullptr}; 149 sptr<AppStateObserver> appStateObserver_ {nullptr}; 150 sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ {nullptr}; 151 std::list<sptr<IBackgroundTaskSubscriber>> bgTaskSubscribers_ {}; 152 std::map<sptr<IRemoteObject>, sptr<RemoteDeathRecipient>> subscriberRecipients_ {}; 153 std::unordered_map<int32_t, CachedBundleInfo> cachedBundleInfos_ {}; 154 std::vector<std::string> continuousTaskText_ {}; 155 156 DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr); 157 }; 158 } // namespace BackgroundTaskMgr 159 } // namespace OHOS 160 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H