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_AAFWK_APP_SCHEDULER_H 17 #define OHOS_AAFWK_APP_SCHEDULER_H 18 19 #include <memory> 20 #include <unordered_set> 21 22 #include "ability_info.h" 23 #include "appmgr/app_mgr_client.h" 24 #include "appmgr/app_state_callback_host.h" 25 #include "appmgr/start_specified_ability_response_stub.h" 26 #include "application_info.h" 27 #include "bundle_info.h" 28 #include "iremote_object.h" 29 #include "refbase.h" 30 #include "singleton.h" 31 #include "system_memory_attr.h" 32 #include "running_process_info.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 class Configuration; 37 } 38 namespace AAFwk { 39 /** 40 * @enum AppAbilityState 41 * AppAbilityState defines the life cycle state of app ability. 42 */ 43 enum class AppAbilityState { 44 ABILITY_STATE_UNDEFINED = 0, 45 ABILITY_STATE_FOREGROUND, 46 ABILITY_STATE_BACKGROUND, 47 ABILITY_STATE_END, 48 }; 49 50 enum class AppState { 51 BEGIN = 0, 52 READY, 53 FOREGROUND, 54 BACKGROUND, 55 SUSPENDED, 56 TERMINATED, 57 END, 58 }; 59 60 struct AppData { 61 std::string appName; 62 int32_t uid; 63 }; 64 65 struct AppInfo { 66 std::vector<AppData> appData; 67 std::string processName; 68 AppState state; 69 }; 70 /** 71 * @class AppStateCallback 72 * AppStateCallback. 73 */ 74 class AppStateCallback { 75 public: AppStateCallback()76 AppStateCallback() 77 {} ~AppStateCallback()78 virtual ~AppStateCallback() 79 {} 80 81 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) = 0; 82 83 virtual void OnAppStateChanged(const AppInfo &info) = 0; 84 }; 85 86 class StartSpecifiedAbilityResponse : public AppExecFwk::StartSpecifiedAbilityResponseStub { 87 public: 88 StartSpecifiedAbilityResponse() = default; 89 virtual ~StartSpecifiedAbilityResponse() = default; 90 91 virtual void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag) override; 92 virtual void OnTimeoutResponse(const AAFwk::Want &want) override; 93 }; 94 95 /** 96 * @class AppScheduler 97 * AppScheduler , access app manager service. 98 */ 99 class AppScheduler : virtual RefBase, public AppExecFwk::AppStateCallbackHost { 100 101 DECLARE_DELAYED_SINGLETON(AppScheduler) 102 public: 103 /** 104 * init app scheduler. 105 * @param callback, app state call back. 106 * @return true on success ,false on failure. 107 */ 108 bool Init(const std::weak_ptr<AppStateCallback> &callback); 109 110 /** 111 * load ability with token, ability info and application info. 112 * 113 * @param token, the token of ability. 114 * @param preToken, the token of ability's caller. 115 * @param abilityinfo, ability info. 116 * @param application, application info. 117 * @param want ability want 118 * @return true on success ,false on failure. 119 */ 120 int LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 121 const AppExecFwk::AbilityInfo &abilityinfo, const AppExecFwk::ApplicationInfo &application, 122 const Want &want); 123 124 /** 125 * terminate ability with token. 126 * 127 * @param token, the token of ability. 128 * @return true on success ,false on failure. 129 */ 130 int TerminateAbility(const sptr<IRemoteObject> &token); 131 132 /** 133 * move ability to forground. 134 * 135 * @param token, the token of ability. 136 */ 137 void MoveToForground(const sptr<IRemoteObject> &token); 138 139 /** 140 * move ability to background. 141 * 142 * @param token, the token of ability. 143 */ 144 void MoveToBackground(const sptr<IRemoteObject> &token); 145 146 /** 147 * Update ability state. 148 * 149 * @param token, the token of ability. 150 * @param state, ability state. 151 */ 152 void UpdateAbilityState(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state); 153 154 /** 155 * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status. 156 * 157 * @param token, the unique identification to update the extension. 158 * @param state, extension status that needs to be updated. 159 * @return 160 */ 161 void UpdateExtensionState(const sptr<IRemoteObject> &token, const AppExecFwk::ExtensionState state); 162 163 /** 164 * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization. 165 * 166 * @param token, the unique identification to start the ability. 167 * @param preToken, the unique identification to call the ability. 168 * @param visibility, the visibility information about windows info. 169 * @param perceptibility, the Perceptibility information about windows info. 170 * @param connectionState, the service ability connection state. 171 * @return Returns RESULT_OK on success, others on failure. 172 */ 173 void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 174 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState); 175 176 /** 177 * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object, 178 * kill the process by ability token. 179 * 180 * @param token, the unique identification to the ability. 181 */ 182 void KillProcessByAbilityToken(const sptr<IRemoteObject> &token); 183 184 /** 185 * KillProcessesByUserId, call KillProcessesByUserId() through proxy object, 186 * kill the process by user id. 187 * 188 * @param userId, the user id. 189 */ 190 void KillProcessesByUserId(int32_t userId); 191 192 /** 193 * convert ability state to app ability state. 194 * 195 * @param state, the state of ability. 196 */ 197 AppAbilityState ConvertToAppAbilityState(const int32_t state); 198 199 /** 200 * get ability state. 201 * 202 * @return state, the state of app ability. 203 */ 204 AppAbilityState GetAbilityState() const; 205 206 /** 207 * kill the application 208 * 209 * @param bundleName. 210 */ 211 int KillApplication(const std::string &bundleName); 212 213 /** 214 * kill the application by uid 215 * 216 * @param bundleName name of bundle. 217 * @param uid uid of bundle. 218 * @return 0 if success. 219 */ 220 int KillApplicationByUid(const std::string &bundleName, int32_t uid); 221 222 /** 223 * clear the application data 224 * 225 * @param bundleName. 226 */ 227 int ClearUpApplicationData(const std::string &bundleName); 228 229 void AttachTimeOut(const sptr<IRemoteObject> &token); 230 231 void PrepareTerminate(const sptr<IRemoteObject> &token); 232 233 /** 234 * Get system memory information. 235 * @param SystemMemoryAttr, memory information. 236 */ 237 void GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &memoryInfo, std::string &strConfig); 238 239 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 240 /** 241 * Start a resident process 242 */ 243 void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos); 244 245 void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo); 246 int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info); 247 248 /** 249 * Start a user test 250 */ 251 int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer, const AppExecFwk::BundleInfo &bundleInfo, 252 int32_t userId); 253 254 /** 255 * @brief Finish user test. 256 * @param msg user test message. 257 * @param resultCode user test result Code. 258 * @param bundleName user test bundleName. 259 * 260 * @return Returns ERR_OK on success, others on failure. 261 */ 262 int FinishUserTest(const std::string &msg, const int &resultCode, const std::string &bundleName); 263 264 int GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> &info, int32_t userId); 265 std::string ConvertAppState(const AppState &state); 266 267 /** 268 * ANotify application update system environment changes. 269 * 270 * @param config System environment change parameters. 271 * @return Returns ERR_OK on success, others on failure. 272 */ 273 int UpdateConfiguration(const AppExecFwk::Configuration &config); 274 275 int GetConfiguration(AppExecFwk::Configuration &config); 276 277 /** 278 * Get the token of ability records by process ID. 279 * 280 * @param pid The process id. 281 * @param tokens The token of ability records. 282 * @return Returns true on success, others on failure. 283 */ 284 int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens); 285 286 /** 287 * Post a task to the not response process. 288 * 289 * @param pid, the not response process id. 290 */ 291 void PostANRTaskByProcessID(const pid_t pid); 292 293 protected: 294 /** 295 * OnAbilityRequestDone, app manager service call this interface after ability request done. 296 * 297 * @param token,ability's token. 298 * @param state,the state of ability lift cycle. 299 */ 300 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state) override; 301 302 /** 303 * Application state changed callback. 304 * 305 * @param appProcessData Process data 306 */ 307 virtual void OnAppStateChanged(const AppExecFwk::AppProcessData &appData) override; 308 309 private: 310 std::recursive_mutex lock_; 311 bool isInit_ {false}; 312 std::weak_ptr<AppStateCallback> callback_; 313 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_; 314 AppAbilityState appAbilityState_ = AppAbilityState::ABILITY_STATE_UNDEFINED; 315 sptr<StartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_; 316 }; 317 } // namespace AAFwk 318 } // namespace OHOS 319 #endif // OHOS_AAFWK_APP_SCHEDULER_H 320