1 /* 2 * Copyright (c) 2021-2023 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 #include "task_handler_wrap.h" 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 #include "app_malloc_info.h" 41 42 namespace OHOS { 43 namespace AppExecFwk { 44 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 45 46 struct AppMgrServiceState { 47 ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START; 48 SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT; 49 }; 50 51 class AMSEventHandler; 52 53 class AppMgrService : public SystemAbility, public AppMgrStub { 54 public: 55 DECLEAR_SYSTEM_ABILITY(AppMgrService); 56 57 AppMgrService(); 58 explicit AppMgrService(const int32_t serviceId, bool runOnCreate = false); 59 virtual ~AppMgrService() override; 60 61 // the function about application 62 // attach the application to ams, then ams can control it. 63 /** 64 * AttachApplication, call AttachApplication() through proxy object, 65 * get all the information needed to start the Application (data related to the Application ). 66 * 67 * @param app, information needed to start the Application. 68 * @return 69 */ 70 virtual void AttachApplication(const sptr<IRemoteObject> &app) override; 71 72 // notify the ams update the state of an app, when it entered foreground. 73 74 /** 75 * ApplicationForegrounded, call ApplicationForegrounded() through proxy object, 76 * set the application to Foreground State. 77 * 78 * @param recordId, a unique record that identifies this Application from others. 79 * @return 80 */ 81 virtual void ApplicationForegrounded(const int32_t recordId) override; 82 83 /** 84 * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object, 85 * set the application to Backgrounded State. 86 * 87 * @param recordId, a unique record that identifies this Application from others. 88 * @return 89 */ 90 virtual void ApplicationBackgrounded(const int32_t recordId) override; 91 92 /** 93 * ApplicationTerminated, call ApplicationTerminated() through proxy object, 94 * terminate the application. 95 * 96 * @param recordId, a unique record that identifies this Application from others. 97 * @return 98 */ 99 virtual void ApplicationTerminated(const int32_t recordId) override; 100 101 /** 102 * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record. 103 * 104 * @param token, a unique record that identifies AbilityCleaned from others. 105 * @return 106 */ 107 virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override; 108 109 /** 110 * ClearUpApplicationData, call ClearUpApplicationData() through proxy project, 111 * clear the application data. 112 * 113 * @param bundleName, bundle name in Application record. 114 * @return Returns ERR_OK on success, others on failure. 115 */ 116 virtual int32_t ClearUpApplicationData(const std::string &bundleName) override; 117 118 /** 119 * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project. 120 * Obtains information about application processes that are running on the device. 121 * 122 * @param info, app name in Application record. 123 * @return Returns ERR_OK on success, others on failure. 124 */ 125 virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override; 126 127 /** 128 * GetAllRenderProcesses, call GetAllRenderProcesses() through proxy project. 129 * Obtains information about render processes that are running on the device. 130 * 131 * @param info, render process info. 132 * @return ERR_OK, return back success, others fail. 133 */ 134 virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info) override; 135 136 /** 137 * JudgeSandboxByPid, call JudgeSandboxByPid() through proxy project. 138 * Obtains information about application processes that are running on the device. 139 * 140 * @param pid, the pid of current app running record. 141 * @param isSandbox, current app is or not a sandbox. 142 * @return Returns ERR_OK on success, others on failure. 143 */ 144 virtual int32_t JudgeSandboxByPid(pid_t pid, bool &isSandbox) override; 145 146 /** 147 * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project. 148 * Obtains information about application processes that are running on the device. 149 * 150 * @param info, app name in Application record. 151 * @param userId, userId. 152 * 153 * @return Returns ERR_OK on success, others on failure. 154 */ 155 virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override; 156 157 /** 158 * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. 159 * Obtains information about current application process which is running on the device. 160 * 161 * @param info, app name in Application record. 162 * 163 * @return Returns ERR_OK on success, others on failure. 164 */ 165 virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override; 166 167 /** 168 * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. 169 * Notify applications background the current memory level. 170 * 171 * @param level, current memory level. 172 * @return Returns ERR_OK on success, others on failure. 173 */ 174 virtual int32_t NotifyMemoryLevel(int32_t level) override; 175 176 /** 177 * DumpHeapMemory, call DumpHeapMemory() through proxy project. 178 * Get the application's memory allocation info. 179 * 180 * @param pid, pid input. 181 * @param mallocInfo, dynamic storage information output. 182 * @return Returns ERR_OK on success, others on failure. 183 */ 184 virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override; 185 186 // the function about system 187 /** 188 * CheckPermission, call CheckPermission() through proxy object, check the permission. 189 * 190 * @param recordId, a unique record that identifies this Application from others. 191 * @param permission, check the permissions. 192 * @return Returns ERR_OK on success, others on failure. 193 */ 194 virtual int32_t CheckPermission(const int32_t recordId, const std::string &permission) override; 195 196 // the function about service running info 197 /** 198 * QueryServiceState, Query application service status. 199 * 200 * @return the application service status. 201 */ 202 AppMgrServiceState QueryServiceState(); 203 204 /** 205 * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance. 206 * 207 * @return sptr<IAmsMgr>, return to AMS interface instance. 208 */ 209 virtual sptr<IAmsMgr> GetAmsMgr() override; 210 211 /** 212 * Notify that the ability stage has been updated 213 * @param recordId, the app record. 214 */ 215 virtual void AddAbilityStageDone(const int32_t recordId) override; 216 217 /** 218 * Start all resident process 219 */ 220 virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override; 221 222 /** 223 * Start user test process. 224 * @param want, want object. 225 * @param observer, test observer remote object. 226 * @param bundleInfo, bundle info. 227 * @param userId the user id. 228 * @return Returns ERR_OK on success, others on failure. 229 */ 230 virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, 231 const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override; 232 233 /** 234 * @brief Finish user test. 235 * @param msg user test message. 236 * @param resultCode user test result Code. 237 * @param bundleName user test bundleName. 238 * 239 * @return Returns ERR_OK on success, others on failure. 240 */ 241 virtual int FinishUserTest( 242 const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override; 243 244 /** 245 * @brief Application hidumper. 246 * @param fd Indicates the fd. 247 * @param args Indicates the params. 248 * @return Returns the dump result. 249 */ 250 int Dump(int fd, const std::vector<std::u16string>& args) override; 251 252 virtual void ScheduleAcceptWantDone( 253 const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override; 254 255 virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override; 256 257 virtual int PreStartNWebSpawnProcess() override; 258 259 virtual int StartRenderProcess(const std::string &renderParam, 260 int32_t ipcFd, int32_t sharedFd, 261 int32_t crashFd, pid_t &renderPid) override; 262 263 virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override; 264 265 virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override; 266 267 virtual int32_t GetConfiguration(Configuration& config) override; 268 269 virtual int32_t UpdateConfiguration(const Configuration &config) override; 270 271 virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 272 273 virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 274 275 #ifdef ABILITY_COMMAND_FOR_TEST 276 /** 277 * Block app service. 278 * 279 * @return Returns ERR_OK on success, others on failure. 280 */ 281 virtual int BlockAppService() override; 282 #endif 283 284 bool GetAppRunningStateByBundleName(const std::string &bundleName) override; 285 286 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 287 288 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 289 290 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 291 292 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE 293 int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override; 294 #endif 295 296 /** 297 * @brief Check whether the shared bundle is running. 298 * 299 * @param bundleName Shared bundle name. 300 * @param versionCode Shared bundle version code. 301 * @return Returns the shared bundle running result. The result is true if running, false otherwise. 302 */ 303 virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override; 304 305 virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override; 306 307 /** 308 * Get bundleName by pid. 309 * 310 * @param pid process id. 311 * @param bundleName Output parameters, return bundleName. 312 * @param uid Output parameters, return userId. 313 * @return Returns ERR_OK on success, others on failure. 314 */ 315 virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override; 316 317 /** 318 * Notify Fault Data 319 * 320 * @param faultData the fault data. 321 * @return Returns ERR_OK on success, others on failure. 322 */ 323 int32_t NotifyAppFault(const FaultData &faultData) override; 324 325 /** 326 * Notify Fault Data By SA 327 * 328 * @param faultData the fault data notified by SA. 329 * @return Returns ERR_OK on success, others on failure. 330 */ 331 int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override; 332 333 /** 334 * get memorySize by pid. 335 * 336 * @param pid process id. 337 * @param memorySize Output parameters, return memorySize in KB. 338 * @return Returns ERR_OK on success, others on failure. 339 */ 340 virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override; 341 342 /** 343 * get application processes information list by bundleName. 344 * 345 * @param bundleName Bundle name. 346 * @param userId user Id in Application record. 347 * @param info Output parameters, return running process info list. 348 * @return Returns ERR_OK on success, others on failure. 349 */ 350 virtual int32_t GetRunningProcessInformation( 351 const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override; 352 353 private: 354 /** 355 * Init, Initialize application services. 356 * 357 * @return ERR_OK, return back success, others fail. 358 */ 359 ErrCode Init(); 360 361 // the function that overrode from SystemAbility 362 /** 363 * OnStart, Start application service. 364 * 365 * @return 366 */ 367 virtual void OnStart() override; 368 369 /** 370 * OnStop, Stop application service. 371 * 372 * @return 373 */ 374 virtual void OnStop() override; 375 376 /** 377 * @brief Judge whether the application service is ready. 378 * 379 * @return Returns true means service is ready, otherwise service is not ready. 380 */ 381 bool IsReady() const; 382 383 /** 384 * AddAppDeathRecipient, Add monitoring death application record. 385 * 386 * @param pid, the application pid. 387 * @param appDeathRecipient, Application death recipient list. 388 * 389 * @return 390 */ 391 void AddAppDeathRecipient(const pid_t pid) const; 392 393 /** 394 * SetInnerService, Setting application service Inner instance. 395 * 396 * @return 397 */ 398 void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService); 399 400 /** 401 * Register application or process state observer. 402 * @param observer, ability token. 403 * @return Returns ERR_OK on success, others on failure. 404 */ 405 virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 406 const std::vector<std::string> &bundleNameList = {}) override; 407 408 /** 409 * Unregister application or process state observer. 410 * @param observer, ability token. 411 * @return Returns ERR_OK on success, others on failure. 412 */ 413 virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override; 414 415 /** 416 * Get foreground applications. 417 * @param list, foreground apps. 418 * @return Returns ERR_OK on success, others on failure. 419 */ 420 virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override; 421 422 void Dump(const std::vector<std::u16string>& args, std::string& result) const; 423 void ShowHelp(std::string& result) const; 424 425 bool JudgeSelfCalledByRecordId(int32_t recordId); 426 427 private: 428 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_; 429 AppMgrServiceState appMgrServiceState_; 430 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_; 431 std::shared_ptr<AMSEventHandler> eventHandler_; 432 sptr<ISystemAbilityManager> systemAbilityMgr_; 433 sptr<IAmsMgr> amsMgrScheduler_; 434 435 DISALLOW_COPY_AND_MOVE(AppMgrService); 436 }; 437 } // namespace AppExecFwk 438 } // namespace OHOS 439 #endif // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 440