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_MGR_SERVICE_H 17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 18 19 #include <list> 20 #include <string> 21 #include <vector> 22 23 #include "if_system_ability_manager.h" 24 #include "nocopyable.h" 25 #include "system_ability.h" 26 27 #include "ability_info.h" 28 #include "ability_running_record.h" 29 #include "appexecfwk_errors.h" 30 #include "application_info.h" 31 #include "app_mgr_constants.h" 32 #include "app_mgr_stub.h" 33 #include "app_mgr_service_event_handler.h" 34 #include "app_mgr_service_inner.h" 35 #include "app_record_id.h" 36 #include "app_running_record.h" 37 #include "app_scheduler_proxy.h" 38 #include "ams_mgr_scheduler.h" 39 #include "ams_mgr_scheduler.h" 40 41 namespace OHOS { 42 namespace AppExecFwk { 43 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 44 45 struct AppMgrServiceState { 46 ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START; 47 SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT; 48 }; 49 50 class AMSEventHandler; 51 52 class AppMgrService : public SystemAbility, public AppMgrStub { 53 public: 54 DECLEAR_SYSTEM_ABILITY(AppMgrService); 55 56 AppMgrService(); 57 AppMgrService(const int32_t serviceId, bool runOnCreate = false); 58 virtual ~AppMgrService() override; 59 60 // the function about application 61 // attach the application to ams, then ams can control it. 62 /** 63 * AttachApplication, call AttachApplication() through proxy object, 64 * get all the information needed to start the Application (data related to the Application ). 65 * 66 * @param app, information needed to start the Application. 67 * @return 68 */ 69 virtual void AttachApplication(const sptr<IRemoteObject> &app) override; 70 71 // notify the ams update the state of an app, when it entered foreground. 72 73 /** 74 * ApplicationForegrounded, call ApplicationForegrounded() through proxy object, 75 * set the application to Foreground State. 76 * 77 * @param recordId, a unique record that identifies this Application from others. 78 * @return 79 */ 80 virtual void ApplicationForegrounded(const int32_t recordId) override; 81 82 /** 83 * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object, 84 * set the application to Backgrounded State. 85 * 86 * @param recordId, a unique record that identifies this Application from others. 87 * @return 88 */ 89 virtual void ApplicationBackgrounded(const int32_t recordId) override; 90 91 /** 92 * ApplicationTerminated, call ApplicationTerminated() through proxy object, 93 * terminate the application. 94 * 95 * @param recordId, a unique record that identifies this Application from others. 96 * @return 97 */ 98 virtual void ApplicationTerminated(const int32_t recordId) override; 99 100 /** 101 * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record. 102 * 103 * @param token, a unique record that identifies AbilityCleaned from others. 104 * @return 105 */ 106 virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override; 107 108 /** 109 * ClearUpApplicationData, call ClearUpApplicationData() through proxy project, 110 * clear the application data. 111 * 112 * @param bundleName, bundle name in Application record. 113 * @return Returns ERR_OK on success, others on failure. 114 */ 115 virtual int32_t ClearUpApplicationData(const std::string &bundleName) override; 116 117 /** 118 * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project. 119 * Obtains information about application processes that are running on the device. 120 * 121 * @param info, app name in Application record. 122 * @return Returns ERR_OK on success, others on failure. 123 */ 124 virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override; 125 126 /** 127 * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project. 128 * Obtains information about application processes that are running on the device. 129 * 130 * @param info, app name in Application record. 131 * @param userId, userId. 132 * 133 * @return Returns ERR_OK on success, others on failure. 134 */ 135 virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override; 136 137 /** 138 * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. 139 * Obtains information about current application process which is running on the device. 140 * 141 * @param info, app name in Application record. 142 * 143 * @return Returns ERR_OK on success, others on failure. 144 */ 145 virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override; 146 147 /** 148 * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. 149 * Notify applications background the current memory level. 150 * 151 * @param level, current memory level. 152 * @return Returns ERR_OK on success, others on failure. 153 */ 154 virtual int32_t NotifyMemoryLevel(int32_t level) override; 155 156 // the function about system 157 /** 158 * CheckPermission, call CheckPermission() through proxy object, check the permission. 159 * 160 * @param recordId, a unique record that identifies this Application from others. 161 * @param permission, check the permissions. 162 * @return Returns ERR_OK on success, others on failure. 163 */ 164 virtual int32_t CheckPermission(const int32_t recordId, const std::string &permission) override; 165 166 // the function about service running info 167 /** 168 * QueryServiceState, Query application service status. 169 * 170 * @return the application service status. 171 */ 172 AppMgrServiceState QueryServiceState(); 173 174 /** 175 * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance. 176 * 177 * @return sptr<IAmsMgr>, return to AMS interface instance. 178 */ 179 virtual sptr<IAmsMgr> GetAmsMgr() override; 180 181 /** 182 * Notify that the ability stage has been updated 183 * @param recordId, the app record. 184 */ 185 virtual void AddAbilityStageDone(const int32_t recordId) override; 186 187 /** 188 * Start all resident process 189 */ 190 virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override; 191 192 /** 193 * Start user test process. 194 * @param want, want object. 195 * @param observer, test observer remote object. 196 * @param bundleInfo, bundle info. 197 * @param userId the user id. 198 * @return Returns ERR_OK on success, others on failure. 199 */ 200 virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, 201 const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override; 202 203 /** 204 * @brief Finish user test. 205 * @param msg user test message. 206 * @param resultCode user test result Code. 207 * @param bundleName user test bundleName. 208 * 209 * @return Returns ERR_OK on success, others on failure. 210 */ 211 virtual int FinishUserTest( 212 const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override; 213 214 /** 215 * @brief Application hidumper. 216 * @param fd Indicates the fd. 217 * @param args Indicates the params. 218 * @return Returns the dump result. 219 */ 220 int Dump(int fd, const std::vector<std::u16string>& args) override; 221 222 virtual void ScheduleAcceptWantDone( 223 const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override; 224 225 virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override; 226 227 virtual int PreStartNWebSpawnProcess() override; 228 229 virtual int StartRenderProcess(const std::string &renderParam, int32_t ipcFd, 230 int32_t sharedFd, pid_t &renderPid) override; 231 232 virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override; 233 234 virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override; 235 236 virtual int32_t GetConfiguration(Configuration& config) override; 237 238 virtual int32_t UpdateConfiguration(const Configuration &config) override; 239 240 virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 241 242 virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 243 244 #ifdef ABILITY_COMMAND_FOR_TEST 245 /** 246 * Block app service. 247 * 248 * @return Returns ERR_OK on success, others on failure. 249 */ 250 virtual int BlockAppService() override; 251 #endif 252 253 bool GetAppRunningStateByBundleName(const std::string &bundleName) override; 254 255 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 256 257 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 258 259 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 260 261 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE 262 int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override; 263 #endif 264 265 private: 266 /** 267 * Init, Initialize application services. 268 * 269 * @return ERR_OK, return back success, others fail. 270 */ 271 ErrCode Init(); 272 273 // the function that overrode from SystemAbility 274 /** 275 * OnStart, Start application service. 276 * 277 * @return 278 */ 279 virtual void OnStart() override; 280 281 /** 282 * OnStop, Stop application service. 283 * 284 * @return 285 */ 286 virtual void OnStop() override; 287 288 /** 289 * @brief Judge whether the application service is ready. 290 * 291 * @return Returns true means service is ready, otherwise service is not ready. 292 */ 293 bool IsReady() const; 294 295 /** 296 * AddAppDeathRecipient, Add monitoring death application record. 297 * 298 * @param pid, the application pid. 299 * @param appDeathRecipient, Application death recipient list. 300 * 301 * @return 302 */ 303 void AddAppDeathRecipient(const pid_t pid) const; 304 305 /** 306 * SetInnerService, Setting application service Inner instance. 307 * 308 * @return 309 */ 310 void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService); 311 312 /** 313 * Register application or process state observer. 314 * @param observer, ability token. 315 * @return Returns ERR_OK on success, others on failure. 316 */ 317 virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 318 const std::vector<std::string> &bundleNameList = {}) override; 319 320 /** 321 * Unregister application or process state observer. 322 * @param observer, ability token. 323 * @return Returns ERR_OK on success, others on failure. 324 */ 325 virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override; 326 327 /** 328 * Get foreground applications. 329 * @param list, foreground apps. 330 * @return Returns ERR_OK on success, others on failure. 331 */ 332 virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override; 333 334 void Dump(const std::vector<std::u16string>& args, std::string& result) const; 335 void ShowHelp(std::string& result) const; 336 337 bool JudgeSelfCalledByRecordId(int32_t recordId); 338 339 private: 340 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_; 341 AppMgrServiceState appMgrServiceState_; 342 std::shared_ptr<EventRunner> runner_; 343 std::shared_ptr<AMSEventHandler> handler_; 344 sptr<ISystemAbilityManager> systemAbilityMgr_; 345 sptr<IAmsMgr> amsMgrScheduler_; 346 347 DISALLOW_COPY_AND_MOVE(AppMgrService); 348 }; 349 } // namespace AppExecFwk 350 } // namespace OHOS 351 #endif // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 352