1 /* 2 * Copyright (c) 2024 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_STARTUP_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_STARTUP_MANAGER_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <vector> 24 25 #include "app_startup_task.h" 26 #include "bundle_info.h" 27 #include "native_startup_task.h" 28 #include "preload_so_startup_task.h" 29 #include "singleton.h" 30 #include "startup_config.h" 31 #include "startup_task_manager.h" 32 33 namespace OHOS { 34 namespace AbilityRuntime { 35 struct ModuleStartupConfigInfo { 36 std::string name_; 37 std::string startupConfig_; 38 std::string hapPath_; 39 AppExecFwk::ModuleType moduleType_ = AppExecFwk::ModuleType::UNKNOWN; 40 bool esModule_; 41 42 ModuleStartupConfigInfo(std::string name, std::string startupConfig, std::string hapPath, 43 const AppExecFwk::ModuleType& moduleType, bool esModule); 44 }; 45 46 struct StartupTaskInfo { 47 std::string name; 48 std::string srcEntry; 49 std::string ohmUrl; 50 std::string moduleName; 51 std::string hapPath; 52 std::vector<std::string> dependencies; 53 bool excludeFromAutoStart = false; 54 bool callCreateOnMainThread = true; 55 bool waitOnMainThread = true; 56 bool esModule = true; 57 }; 58 59 class StartupManager : public std::enable_shared_from_this<StartupManager> { 60 DECLARE_DELAYED_SINGLETON(StartupManager) 61 62 public: 63 int32_t PreloadAppHintStartup(const AppExecFwk::BundleInfo& bundleInfo, 64 const AppExecFwk::HapModuleInfo& entryInfo, const std::string &preloadModuleName); 65 66 int32_t LoadAppStartupTaskConfig(bool &needRunAutoStartupTask); 67 68 const std::vector<StartupTaskInfo> &GetStartupTaskInfos() const; 69 70 const std::string &GetPendingConfigEntry() const; 71 72 int32_t RegisterAppStartupTask( 73 const std::string &name, const std::shared_ptr<AppStartupTask> &startupTask); 74 75 int32_t BuildAutoAppStartupTaskManager(std::shared_ptr<StartupTaskManager> &startupTaskManager); 76 77 int32_t BuildAppStartupTaskManager(const std::vector<std::string> &inputDependencies, 78 std::shared_ptr<StartupTaskManager> &startupTaskManager); 79 80 int32_t OnStartupTaskManagerComplete(uint32_t id); 81 82 void SetDefaultConfig(const std::shared_ptr<StartupConfig> &config); 83 84 const std::shared_ptr<StartupConfig>& GetDefaultConfig() const; 85 86 int32_t RemoveAllResult(); 87 88 int32_t RemoveResult(const std::string &name); 89 90 int32_t GetResult(const std::string &name, std::shared_ptr<StartupTaskResult> &result); 91 92 int32_t IsInitialized(const std::string &name, bool &isInitialized); 93 94 int32_t PostMainThreadTask(const std::function<void()> &task); 95 96 void StopAutoPreloadSoTask(); 97 98 bool HasAppStartupConfig() const; 99 100 private: 101 // read only after initialization 102 std::vector<ModuleStartupConfigInfo> moduleStartupConfigInfos_; 103 std::mutex appStartupConfigInitializationMutex_; 104 std::atomic<bool> isAppStartupConfigInited_ = false; 105 106 std::mutex startupTaskManagerMutex_; 107 uint32_t startupTaskManagerId = 0; 108 std::map<uint32_t, std::shared_ptr<StartupTaskManager>> startupTaskManagerMap_; 109 110 // read only after initialization 111 std::map<std::string, std::shared_ptr<AppStartupTask>> preloadSoStartupTasks_; 112 std::map<std::string, std::shared_ptr<AppStartupTask>> appStartupTasks_; 113 std::vector<StartupTaskInfo> pendingStartupTaskInfos_; 114 std::string pendingConfigEntry_; 115 116 std::mutex autoPreloadSoTaskManagerMutex_; 117 std::weak_ptr<StartupTaskManager> autoPreloadSoTaskManager_; 118 bool autoPreloadSoStopped_ = false; 119 120 std::shared_ptr<StartupConfig> defaultConfig_; 121 std::shared_ptr<AppExecFwk::EventHandler> mainHandler_; 122 std::shared_ptr<AppExecFwk::EventHandler> preloadHandler_; 123 124 static int32_t AddStartupTask(const std::string &name, std::map<std::string, std::shared_ptr<StartupTask>> &taskMap, 125 std::map<std::string, std::shared_ptr<AppStartupTask>> &allTasks); 126 int32_t RegisterPreloadSoStartupTask( 127 const std::string &name, const std::shared_ptr<PreloadSoStartupTask> &startupTask); 128 int32_t BuildStartupTaskManager(const std::map<std::string, std::shared_ptr<StartupTask>> &tasks, 129 std::shared_ptr<StartupTaskManager> &startupTaskManager); 130 int32_t AddAppPreloadSoTask(const std::vector<std::string> &preloadSoList, 131 std::map<std::string, std::shared_ptr<StartupTask>> ¤tStartupTasks); 132 std::shared_ptr<NativeStartupTask> CreateAppPreloadSoTask( 133 const std::map<std::string, std::shared_ptr<StartupTask>> ¤tPreloadSoTasks); 134 135 void PreloadAppHintStartupTask(); 136 int32_t AddLoadAppStartupConfigTask(std::map<std::string, std::shared_ptr<StartupTask>> &preloadAppHintTasks); 137 int32_t RunLoadAppStartupConfigTask(); 138 int32_t AddAppAutoPreloadSoTask(std::map<std::string, std::shared_ptr<StartupTask>> &preloadAppHintTasks); 139 int32_t RunAppAutoPreloadSoTask(); 140 int32_t RunAppPreloadSoTask(const std::map<std::string, std::shared_ptr<StartupTask>> &appPreloadSoTasks); 141 int32_t GetAppAutoPreloadSoTasks(std::map<std::string, std::shared_ptr<StartupTask>> &appAutoPreloadSoTasks); 142 int32_t RunAppPreloadSoTaskMainThread(const std::map<std::string, std::shared_ptr<StartupTask>> &appPreloadSoTasks, 143 std::unique_ptr<StartupTaskResultCallback> callback); 144 145 static int32_t GetStartupConfigString(const ModuleStartupConfigInfo& info, std::string& config); 146 static bool AnalyzeStartupConfig(const ModuleStartupConfigInfo& info, const std::string& startupConfig, 147 std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks, 148 std::vector<StartupTaskInfo>& pendingStartupTaskInfos, std::string& pendingConfigEntry); 149 static bool AnalyzeAppStartupTask(const ModuleStartupConfigInfo& info, nlohmann::json &startupConfigJson, 150 std::vector<StartupTaskInfo>& pendingStartupTaskInfos); 151 static bool AnalyzePreloadSoStartupTask(const ModuleStartupConfigInfo& info, nlohmann::json &startupConfigJson, 152 std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks); 153 static bool AnalyzeAppStartupTaskInner(const ModuleStartupConfigInfo& info, 154 const nlohmann::json &startupTaskJson, 155 std::vector<StartupTaskInfo>& pendingStartupTaskInfos); 156 static bool AnalyzePreloadSoStartupTaskInner(const ModuleStartupConfigInfo& info, 157 const nlohmann::json &preloadStartupTaskJson, 158 std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks); 159 static void SetOptionalParameters(const nlohmann::json& module, AppExecFwk::ModuleType moduleType, 160 StartupTaskInfo& startupTaskInfo); 161 static void SetOptionalParameters(const nlohmann::json &module, AppExecFwk::ModuleType moduleType, 162 std::shared_ptr<PreloadSoStartupTask> &task); 163 }; 164 } // namespace AbilityRuntime 165 } // namespace OHOS 166 #endif // OHOS_ABILITY_RUNTIME_STARTUP_MANAGER_H 167