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 class BackgroundTaskMgrService; 44 class DataStorageHelper; 45 46 enum class ContinuousTaskEventTriggerType: uint32_t { 47 TASK_START, 48 TASK_CANCEL, 49 }; 50 51 struct CachedBundleInfo { 52 std::unordered_map<std::string, uint32_t> abilityBgMode_ {}; 53 std::string appName_ {""}; 54 }; 55 56 class BgContinuousTaskMgr : public DelayedSingleton<BgContinuousTaskMgr>, 57 public std::enable_shared_from_this<BgContinuousTaskMgr> { 58 public: 59 ErrCode StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam); 60 ErrCode StopBackgroundRunning(const std::string &abilityName); 61 ErrCode RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 62 ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 63 ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 64 ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 65 ErrCode GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list); 66 bool StopContinuousTaskByUser(const std::string &mapKey); 67 void OnAccountsStateChanged(int32_t id); 68 void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid); 69 void OnAbilityStateChanged(int32_t uid, const std::string &abilityName); 70 void OnProcessDied(int32_t uid, int32_t pid); 71 void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object); 72 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 73 void InitNecessaryState(); 74 void InitRequiredResourceInfo(); 75 void Clear(); 76 int32_t GetBgTaskUid(); 77 void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType); 78 void OnConfigurationChanged(const AppExecFwk::Configuration &configuration); 79 80 private: 81 ErrCode StartBackgroundRunningInner(std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecordPtr); 82 ErrCode StartBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 83 ErrCode StopBackgroundRunningInner(int32_t uid, const std::string &abilityName); 84 ErrCode StopBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam); 85 ErrCode AddSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber); 86 ErrCode RemoveSubscriberInner(const sptr<IBackgroundTaskSubscriber> &subscriber); 87 ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 88 ErrCode SendContinuousTaskNotification(std::shared_ptr<ContinuousTaskRecord> &ContinuousTaskRecordPtr); 89 ErrCode GetContinuousTaskAppsInner(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list); 90 void HandlePersistenceData(); 91 void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, 92 const std::set<std::string> &allLabels); 93 void DumpAllTaskInfo(std::vector<std::string> &dumpInfo); 94 bool DumpCheckWebviewInfo(const std::vector<std::string> &dumpOption); 95 void DumpCancelTask(const std::vector<std::string> &dumpOption, bool cleanAll); 96 bool RemoveContinuousTaskRecord(const std::string &mapKey); 97 bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo); 98 bool CheckProcessUidInfo(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t uid); 99 uint32_t GetBackgroundModeInfo(int32_t uid, const std::string &abilityName); 100 bool AddAbilityBgModeInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo); 101 bool RegisterNotificationSubscriber(); 102 bool RegisterSysCommEventListener(); 103 bool RegisterAppStateObserver(); 104 bool RegisterConfigurationObserver(); 105 bool GetNotificationPrompt(); 106 bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName); 107 void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType); 108 void RemoveSpecifiedBgTask(uint32_t taskType); 109 void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object); 110 void OnContinuousTaskChanged(const std::shared_ptr<ContinuousTaskRecord> continuousTaskInfo, 111 ContinuousTaskEventTriggerType changeEventType); 112 ErrCode CheckBgmodeType(uint32_t configuredBgMode, uint32_t requestedBgModeId, bool isNewApi, uint64_t fullTokenId); 113 ErrCode CheckBgmodeTypeForInner(uint32_t requestedBgModeId); 114 int32_t RefreshTaskRecord(); 115 void HandleAppContinuousTaskStop(int32_t uid); 116 bool checkPidCondition(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses, int32_t pid); 117 bool checkNotificationCondition(const std::set<std::string> ¬ificationLabels, const std::string &label); 118 std::shared_ptr<Global::Resource::ResourceManager> GetBundleResMgr(const AppExecFwk::BundleInfo &bundleInfo); 119 std::string GetMainAbilityLabel(const std::string &bundleName, int32_t userId); 120 121 private: 122 std::atomic<bool> isSysReady_ {false}; 123 std::string deviceType_ {""}; 124 int32_t bgTaskUid_ {-1}; 125 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 126 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap_ {}; 127 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE 128 std::shared_ptr<TaskNotificationSubscriber> subscriber_ {nullptr}; 129 #endif 130 std::shared_ptr<SystemEventObserver> systemEventListener_ {nullptr}; 131 std::shared_ptr<AppStateObserver> appStateObserver_ {nullptr}; 132 sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ {nullptr}; 133 std::list<sptr<IBackgroundTaskSubscriber>> bgTaskSubscribers_ {}; 134 std::map<sptr<IRemoteObject>, sptr<RemoteDeathRecipient>> subscriberRecipients_ {}; 135 std::unordered_map<int32_t, CachedBundleInfo> cachedBundleInfos_ {}; 136 std::vector<std::string> continuousTaskText_ {}; 137 138 DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr); 139 }; 140 } // namespace BackgroundTaskMgr 141 } // namespace OHOS 142 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_MGR_H