1 /* 2 * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <regex> 22 23 #include "iremote_object.h" 24 #include "refbase.h" 25 26 #include "app_running_record.h" 27 #include "ability_info.h" 28 #include "application_info.h" 29 #include "app_state_data.h" 30 #include "record_query_result.h" 31 #include "running_process_info.h" 32 #include "bundle_info.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 class AppRunningManager { 37 public: 38 AppRunningManager(); 39 virtual ~AppRunningManager(); 40 /** 41 * CreateAppRunningRecord, Get or create application record information. 42 * 43 * @param token, the unique identification to start the ability. 44 * @param abilityInfo, ability information. 45 * @param appInfo, app information. 46 * @param processName, app process name. 47 * @param uid, app uid in Application record. 48 * @param result, If error occurs, error code is in |result|. 49 * 50 * @return AppRunningRecord pointer if success get or create. 51 */ 52 std::shared_ptr<AppRunningRecord> CreateAppRunningRecord( 53 const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName, const BundleInfo &bundleInfo); 54 55 /** 56 * CheckAppRunningRecordIsExist, Get process record by application name and process Name. 57 * 58 * @param appName, the application name. 59 * @param processName, the process name. 60 * @param uid, the process uid. 61 * 62 * @return process record. 63 */ 64 std::shared_ptr<AppRunningRecord> CheckAppRunningRecordIsExist(const std::string &appName, 65 const std::string &processName, const int uid, const BundleInfo &bundleInfo); 66 67 /** 68 * GetAppRunningRecordByPid, Get process record by application pid. 69 * 70 * @param pid, the application pid. 71 * 72 * @return process record. 73 */ 74 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid); 75 76 /** 77 * GetAppRunningRecordByAbilityToken, Get process record by ability token. 78 * 79 * @param abilityToken, the ability token. 80 * 81 * @return process record. 82 */ 83 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken); 84 85 /** 86 * OnRemoteDied, Equipment death notification. 87 * 88 * @param remote, Death client. 89 * @return 90 */ 91 std::shared_ptr<AppRunningRecord> OnRemoteDied(const wptr<IRemoteObject> &remote); 92 93 /** 94 * GetAppRunningRecordMap, Get application record list. 95 * 96 * @return the application record list. 97 */ 98 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> GetAppRunningRecordMap(); 99 100 /** 101 * RemoveAppRunningRecordById, Remove application information through application id. 102 * 103 * @param recordId, the application id. 104 * @return 105 */ 106 void RemoveAppRunningRecordById(const int32_t recordId); 107 108 /** 109 * ClearAppRunningRecordMap, Clear application record list. 110 * 111 * @return 112 */ 113 void ClearAppRunningRecordMap(); 114 115 /** 116 * Get the pid of a non-resident process. 117 * 118 * @return Return true if found, otherwise return false. 119 */ 120 bool ProcessExitByBundleName(const std::string &bundleName, std::list<pid_t> &pids); 121 /** 122 * Get Foreground Applications. 123 * 124 * @return Foreground Applications. 125 */ 126 void GetForegroundApplications(std::vector<AppStateData> &list); 127 128 /* 129 * ANotify application update system environment changes. 130 * 131 * @param config System environment change parameters. 132 * @return Returns ERR_OK on success, others on failure. 133 */ 134 int32_t UpdateConfiguration(const Configuration &config); 135 136 /* 137 * Notify application background of current memory level. 138 * 139 * @param level current memory level. 140 * @return Returns ERR_OK on success, others on failure. 141 */ 142 int32_t NotifyMemoryLevel(int32_t level); 143 144 void HandleTerminateTimeOut(int64_t eventId); 145 void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token); 146 std::shared_ptr<AppRunningRecord> GetAppRunningRecord(const int64_t eventId); 147 void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag, 148 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner); 149 150 /** 151 * 152 * @brief update the application info after new module installed. 153 * 154 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 155 * 156 */ 157 int32_t ProcessUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo); 158 159 bool ProcessExitByBundleNameAndUid(const std::string &bundleName, const int uid, std::list<pid_t> &pids); 160 bool GetPidsByUserId(int32_t userId, std::list<pid_t> &pids); 161 162 void PrepareTerminate(const sptr<IRemoteObject> &token); 163 164 std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &abilityToken); 165 166 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 167 void GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info); 168 169 void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr); 170 void HandleAddAbilityStageTimeOut(const int64_t eventId); 171 void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId); 172 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByRenderPid(const pid_t pid); 173 std::shared_ptr<RenderRecord> OnRemoteRenderDied(const wptr<IRemoteObject> &remote); 174 bool ProcessExitByPid(pid_t pid); 175 bool GetAppRunningStateByBundleName(const std::string &bundleName); 176 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 177 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 178 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 179 bool IsApplicationFirstForeground(const AppRunningRecord &foregroundingRecord); 180 bool IsApplicationBackground(const std::string &bundleName); 181 bool IsApplicationFirstFocused(const AppRunningRecord &foregroundingRecord); 182 bool IsApplicationUnfocused(const std::string &bundleName); 183 private: 184 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId); 185 void AssignRunningProcessInfoByAppRecord( 186 std::shared_ptr<AppRunningRecord> appRecord, AppExecFwk::RunningProcessInfo &info) const; 187 188 private: 189 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> appRunningRecordMap_; 190 std::map<const std::string, int> processRestartRecord_; 191 std::recursive_mutex lock_; 192 }; 193 } // namespace AppExecFwk 194 } // namespace OHOS 195 196 #endif // OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H 197