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_MAIN_THREAD_H 17 #define OHOS_ABILITY_RUNTIME_MAIN_THREAD_H 18 19 #include <string> 20 #include <mutex> 21 #include "event_handler.h" 22 #include "extension_config_mgr.h" 23 #include "idle_time.h" 24 #include "inner_event.h" 25 #include "app_scheduler_host.h" 26 #include "app_mgr_interface.h" 27 #include "ability_record_mgr.h" 28 #include "application_impl.h" 29 #include "common_event_subscriber.h" 30 #include "resource_manager.h" 31 #include "foundation/ability/ability_runtime/interfaces/inner_api/runtime/include/runtime.h" 32 #include "ipc_singleton.h" 33 #include "js_runtime.h" 34 #include "native_engine/native_engine.h" 35 #include "overlay_event_subscriber.h" 36 #include "watchdog.h" 37 #include "app_malloc_info.h" 38 #define ABILITY_LIBRARY_LOADER 39 40 class Runtime; 41 namespace OHOS { 42 namespace AppExecFwk { 43 using namespace OHOS::Global; 44 using OHOS::AbilityRuntime::Runtime; 45 struct BaseSharedBundleInfo; 46 using HspList = std::vector<BaseSharedBundleInfo>; 47 enum class MainThreadState { INIT, ATTACH, READY, RUNNING }; 48 struct BundleInfo; 49 class ContextDeal; 50 // class Global::Resource::ResourceManager; 51 class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient { 52 public: 53 /** 54 * 55 * @brief Notify the AppMgrDeathRecipient that the remote is dead. 56 * 57 * @param remote The remote which is dead. 58 */ 59 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 60 AppMgrDeathRecipient() = default; 61 ~AppMgrDeathRecipient() override = default; 62 }; 63 64 class MainThread : public AppSchedulerHost { 65 DECLARE_DELAYED_IPCSINGLETON(MainThread); 66 67 public: 68 /** 69 * 70 * @brief Get the current MainThreadState. 71 * 72 * @return Returns the current MainThreadState. 73 */ 74 MainThreadState GetMainThreadState() const; 75 76 /** 77 * 78 * @brief Get the runner state of mainthread. 79 * 80 * @return Returns the runner state of mainthread. 81 */ 82 bool GetRunnerStarted() const; 83 84 /** 85 * 86 * @brief Get the newThreadId. 87 * 88 * @return Returns the newThreadId. 89 */ 90 int GetNewThreadId(); 91 92 /** 93 * 94 * @brief Get the application. 95 * 96 * @return Returns the application. 97 */ 98 std::shared_ptr<OHOSApplication> GetApplication() const; 99 100 /** 101 * 102 * @brief Get the applicationInfo. 103 * 104 * @return Returns the applicationInfo. 105 */ 106 std::shared_ptr<ApplicationInfo> GetApplicationInfo() const; 107 108 /** 109 * 110 * @brief Get the applicationImpl. 111 * 112 * @return Returns the applicationImpl. 113 */ 114 std::shared_ptr<ApplicationImpl> GetApplicationImpl(); 115 116 /** 117 * 118 * @brief Get the eventHandler of mainthread. 119 * 120 * @return Returns the eventHandler of mainthread. 121 */ 122 std::shared_ptr<EventHandler> GetMainHandler() const; 123 124 /** 125 * 126 * @brief Schedule the foreground lifecycle of application. 127 * 128 */ 129 void ScheduleForegroundApplication() override; 130 131 /** 132 * 133 * @brief Schedule the background lifecycle of application. 134 * 135 */ 136 void ScheduleBackgroundApplication() override; 137 138 /** 139 * 140 * @brief Schedule the terminate lifecycle of application. 141 * 142 */ 143 void ScheduleTerminateApplication() override; 144 145 /** 146 * 147 * @brief Shrink the memory which used by application. 148 * 149 * @param level Indicates the memory trim level, which shows the current memory usage status. 150 */ 151 void ScheduleShrinkMemory(const int level) override; 152 153 /** 154 * 155 * @brief Notify the current memory. 156 * 157 * @param level Indicates the memory trim level, which shows the current memory usage status. 158 */ 159 void ScheduleMemoryLevel(const int level) override; 160 161 /** 162 * 163 * @brief Get the application's memory allocation info. 164 * 165 * @param pid, pid input. 166 * @param mallocInfo, dynamic storage information output. 167 */ 168 void ScheduleHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override; 169 170 /** 171 * 172 * @brief Low the memory which used by application. 173 * 174 */ 175 void ScheduleLowMemory() override; 176 177 /** 178 * 179 * @brief Launch the application. 180 * 181 * @param data The launchdata of the application witch launced. 182 * 183 */ 184 void ScheduleLaunchApplication(const AppLaunchData &data, const Configuration &config) override; 185 186 /** 187 * 188 * @brief update the application info after new module installed. 189 * 190 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 191 * 192 */ 193 void ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo) override; 194 195 /** 196 * Notify application to launch ability stage. 197 * 198 * @param The resident process data value. 199 */ 200 void ScheduleAbilityStage(const HapModuleInfo &abilityStage) override; 201 202 void ScheduleLaunchAbility(const AbilityInfo &info, const sptr<IRemoteObject> &token, 203 const std::shared_ptr<AAFwk::Want> &want) override; 204 205 /** 206 * 207 * @brief clean the ability by token. 208 * 209 * @param token The token belong to the ability which want to be cleaned. 210 * 211 */ 212 void ScheduleCleanAbility(const sptr<IRemoteObject> &token) override; 213 214 /** 215 * 216 * @brief send the new profile. 217 * 218 * @param profile The updated profile. 219 * 220 */ 221 void ScheduleProfileChanged(const Profile &profile) override; 222 223 /** 224 * 225 * @brief send the new config to the application. 226 * 227 * @param config The updated config. 228 * 229 */ 230 void ScheduleConfigurationUpdated(const Configuration &config) override; 231 232 /** 233 * 234 * @brief Starts the mainthread. 235 * 236 */ 237 static void Start(); 238 239 /** 240 * 241 * @brief Preload extensions in appspawn. 242 * 243 */ 244 static void PreloadExtensionPlugin(); 245 246 /** 247 * 248 * @brief Schedule the application process exit safely. 249 * 250 */ 251 void ScheduleProcessSecurityExit() override; 252 253 void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName) override; 254 255 /** 256 * 257 * @brief Check the App main thread state. 258 * 259 */ 260 void CheckMainThreadIsAlive(); 261 262 int32_t ScheduleNotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback, 263 const int32_t recordId) override; 264 265 int32_t ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId) override; 266 267 int32_t ScheduleNotifyUnLoadRepairPatch(const std::string &bundleName, 268 const sptr<IQuickFixCallback> &callback, const int32_t recordId) override; 269 270 int32_t ScheduleNotifyAppFault(const FaultData &faultData) override; 271 272 private: 273 /** 274 * 275 * @brief Terminate the application but don't notify ams. 276 * 277 */ 278 void HandleTerminateApplicationLocal(); 279 280 void HandleScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName); 281 282 /** 283 * 284 * @brief Schedule the application process exit safely. 285 * 286 */ 287 void HandleProcessSecurityExit(); 288 289 /** 290 * 291 * @brief Clean the ability but don't notify ams. 292 * 293 * @param token The token which belongs to the ability launched. 294 * 295 */ 296 void HandleCleanAbilityLocal(const sptr<IRemoteObject> &token); 297 298 /** 299 * 300 * @brief Launch the application. 301 * 302 * @param appLaunchData The launchdata of the application witch launced. 303 * 304 */ 305 void HandleLaunchApplication(const AppLaunchData &appLaunchData, const Configuration &config); 306 307 /** 308 * 309 * @brief update the application info after new module installed. 310 * 311 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 312 * 313 */ 314 void HandleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo); 315 316 /** 317 * 318 * @brief Launch the application. 319 * 320 * @param appLaunchData The launchdata of the application witch launced. 321 * 322 */ 323 void HandleAbilityStage(const HapModuleInfo &abilityStage); 324 325 /** 326 * 327 * @brief Launch the ability. 328 * 329 * @param abilityRecord The abilityRecord which belongs to the ability launched. 330 * 331 */ 332 void HandleLaunchAbility(const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 333 334 /** 335 * 336 * @brief Clean the ability. 337 * 338 * @param token The token which belongs to the ability launched. 339 * 340 */ 341 void HandleCleanAbility(const sptr<IRemoteObject> &token); 342 343 /** 344 * 345 * @brief Foreground the application. 346 * 347 */ 348 void HandleForegroundApplication(); 349 350 /** 351 * 352 * @brief Background the application. 353 * 354 */ 355 void HandleBackgroundApplication(); 356 357 /** 358 * 359 * @brief Terminate the application. 360 * 361 */ 362 void HandleTerminateApplication(); 363 364 /** 365 * 366 * @brief Shrink the memory which used by application. 367 * 368 * @param level Indicates the memory trim level, which shows the current memory usage status. 369 * 370 */ 371 void HandleShrinkMemory(const int level); 372 373 /** 374 * 375 * @brief Notify the memory. 376 * 377 * @param level Indicates the memory trim level, which shows the current memory usage status. 378 * 379 */ 380 void HandleMemoryLevel(int level); 381 382 /** 383 * 384 * @brief send the new config to the application. 385 * 386 * @param config The updated config. 387 * 388 */ 389 void HandleConfigurationUpdated(const Configuration &config); 390 391 /** 392 * 393 * @brief remove the deathRecipient from appMgr. 394 * 395 */ 396 void RemoveAppMgrDeathRecipient(); 397 398 /** 399 * 400 * @brief Attach the mainthread to the AppMgr. 401 * 402 */ 403 void Attach(); 404 405 /** 406 * 407 * @brief Set the runner state of mainthread. 408 * 409 * @param runnerStart whether the runner is started. 410 */ 411 void SetRunnerStarted(bool runnerStart); 412 413 /** 414 * 415 * @brief Connect the mainthread to the AppMgr. 416 * 417 */ 418 bool ConnectToAppMgr(); 419 420 /** 421 * 422 * @brief Check whether the appLaunchData is legal. 423 * 424 * @param appLaunchData The appLaunchData should be checked. 425 * 426 * @return if the appLaunchData is legal, return true. else return false. 427 */ 428 bool CheckLaunchApplicationParam(const AppLaunchData &appLaunchData) const; 429 430 /** 431 * 432 * @brief Check whether the record is legal. 433 * 434 * @param record The record should be checked. 435 * 436 * @return if the record is legal, return true. else return false. 437 */ 438 bool CheckAbilityItem(const std::shared_ptr<AbilityLocalRecord> &record) const; 439 440 /** 441 * 442 * @brief Init the mainthread. 443 * 444 * @param runner the runner belong to the mainthread. 445 * 446 */ 447 void Init(const std::shared_ptr<EventRunner> &runner); 448 449 /** 450 * 451 * @brief Task in event handler timeout detected. 452 * 453 * @param runner the runner belong to the mainthread. 454 * 455 */ 456 void TaskTimeoutDetected(const std::shared_ptr<EventRunner>& runner); 457 458 /** 459 * 460 * @brief Check whether the OHOSApplication is ready. 461 * 462 * @return if the OHOSApplication is ready, return true. else return false. 463 * 464 */ 465 bool IsApplicationReady() const; 466 467 /** 468 * @brief Load all extension so 469 * 470 * @param nativeEngine nativeEngine instance 471 */ 472 void LoadAllExtensions(NativeEngine &nativeEngine); 473 474 /** 475 * 476 * @brief Ability Delegator Prepare. 477 * 478 * @param record User Test info. 479 * 480 */ 481 bool PrepareAbilityDelegator(const std::shared_ptr<UserTestRecord> &record, bool isStageBased, 482 const AppExecFwk::HapModuleInfo &entryHapModuleInfo); 483 484 /** 485 * @brief Set current process extension type 486 * 487 * @param abilityRecord current running ability record 488 */ 489 void SetProcessExtensionType(const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 490 491 /** 492 * @brief Add Extension block item 493 * 494 * @param extensionName extension name 495 * @param type extension type 496 */ 497 void AddExtensionBlockItem(const std::string &extensionName, int32_t type); 498 499 /** 500 * @brief Update runtime module checker 501 * 502 * @param runtime runtime the ability runtime 503 */ 504 void UpdateRuntimeModuleChecker(const std::unique_ptr<AbilityRuntime::Runtime> &runtime); 505 506 static void HandleDumpHeap(bool isPrivate); 507 static void HandleSignal(int signal); 508 509 void NotifyAppFault(const FaultData &faultData); 510 511 void OnOverlayChanged(const EventFwk::CommonEventData &data, 512 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName, 513 const std::string &moduleName, const std::string &loadPath); 514 515 void HandleOnOverlayChanged(const EventFwk::CommonEventData &data, 516 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName, 517 const std::string &moduleName, const std::string &loadPath); 518 519 int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName, 520 std::vector<OverlayModuleInfo> &overlayModuleInfos) const; 521 522 std::vector<std::string> GetAddOverlayPaths(const std::vector<OverlayModuleInfo> &overlayModuleInfos); 523 524 std::vector<std::string> GetRemoveOverlayPaths(const std::vector<OverlayModuleInfo> &overlayModuleInfos); 525 526 class MainHandler : public EventHandler { 527 public: 528 MainHandler(const std::shared_ptr<EventRunner> &runner, const sptr<MainThread> &thread); 529 virtual ~MainHandler() = default; 530 531 /** 532 * 533 * @brief Process the event. 534 * 535 * @param event the event want to be processed. 536 * 537 */ 538 void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) override; 539 540 private: 541 wptr<MainThread> mainThreadObj_ = nullptr; 542 }; 543 544 bool isRunnerStarted_ = false; 545 int newThreadId_ = -1; 546 std::shared_ptr<ApplicationInfo> applicationInfo_ = nullptr; 547 std::shared_ptr<ProcessInfo> processInfo_ = nullptr; 548 std::shared_ptr<OHOSApplication> application_ = nullptr; 549 std::shared_ptr<ApplicationImpl> applicationImpl_ = nullptr; 550 static std::shared_ptr<MainHandler> mainHandler_; 551 std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr; 552 std::shared_ptr<Watchdog> watchdog_ = nullptr; 553 std::unique_ptr<AbilityRuntime::ExtensionConfigMgr> extensionConfigMgr_ = nullptr; 554 MainThreadState mainThreadState_ = MainThreadState::INIT; 555 sptr<IAppMgr> appMgr_ = nullptr; // appMgrService Handler 556 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 557 std::string aceApplicationName_ = "AceApplication"; 558 std::string pathSeparator_ = "/"; 559 std::string abilityLibraryType_ = ".so"; 560 static std::shared_ptr<EventHandler> signalHandler_; 561 static std::weak_ptr<OHOSApplication> applicationForDump_; 562 563 #ifdef ABILITY_LIBRARY_LOADER 564 /** 565 * 566 * @brief Load the ability library. 567 * 568 * @param libraryPaths the library paths. 569 * 570 */ 571 void LoadAbilityLibrary(const std::vector<std::string> &libraryPaths); 572 573 void CalcNativeLiabraryEntries(const BundleInfo &bundleInfo, std::string &nativeLibraryPath); 574 void LoadNativeLiabrary(const BundleInfo &bundleInfo, std::string &nativeLibraryPath); 575 576 void LoadAppDetailAbilityLibrary(std::string &nativeLibraryPath); 577 578 void LoadAppLibrary(); 579 580 void ChangeToLocalPath(const std::string &bundleName, 581 const std::vector<std::string> &sourceDirs, std::vector<std::string> &localPath); 582 583 void ChangeToLocalPath(const std::string &bundleName, 584 const std::string &sourcDir, std::string &localPath); 585 586 bool ScanDir(const std::string &dirPath, std::vector<std::string> &files); 587 588 /** 589 * 590 * @brief Check the fileType. 591 * 592 * @param fileName The fileName of the lib. 593 * @param extensionName The extensionName of the lib. 594 * 595 * @return if the FileType is legal, return true. else return false. 596 * 597 */ 598 bool CheckFileType(const std::string &fileName, const std::string &extensionName); 599 600 bool InitCreate(std::shared_ptr<ContextDeal> &contextDeal, ApplicationInfo &appInfo, ProcessInfo &processInfo); 601 bool CheckForHandleLaunchApplication(const AppLaunchData &appLaunchData); 602 bool InitResourceManager(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 603 const AppExecFwk::HapModuleInfo &entryHapModuleInfo, const std::string &bundleName, 604 bool multiProjects, const Configuration &config); 605 606 bool GetHqfFileAndHapPath(const std::string &bundleName, 607 std::vector<std::pair<std::string, std::string>> &fileMap); 608 void GetNativeLibPath(const BundleInfo &bundleInfo, const HspList &hspList, AppLibPathMap &appLibPaths); 609 610 std::vector<std::string> fileEntries_; 611 std::vector<std::string> nativeFileEntries_; 612 std::vector<void *> handleAbilityLib_; // the handler of ACE Library. 613 std::shared_ptr<IdleTime> idleTime_ = nullptr; 614 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_; 615 #endif // ABILITY_LIBRARY_LOADER 616 #ifdef APPLICATION_LIBRARY_LOADER 617 void *handleAppLib_ = nullptr; // the handler of ACE Library. 618 constexpr static std::string applicationLibraryPath = "/hos/lib/libapplication_native.z.so"; 619 #endif // APPLICATION_LIBRARY_LOADER 620 DISALLOW_COPY_AND_MOVE(MainThread); 621 }; 622 } // namespace AppExecFwk 623 } // namespace OHOS 624 #endif // OHOS_ABILITY_RUNTIME_MAIN_THREAD_H 625