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