• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_launch_data.h"
26 #include "app_startup_task.h"
27 #include "app_startup_task_matcher.h"
28 #include "bundle_info.h"
29 #include "native_startup_task.h"
30 #include "nlohmann/json.hpp"
31 #include "preload_so_startup_task.h"
32 #include "singleton.h"
33 #include "startup_config.h"
34 #include "startup_task_manager.h"
35 
36 namespace OHOS {
37 namespace AbilityRuntime {
38 struct ModuleStartupConfigInfo {
39     std::string name_;
40     std::string startupConfig_;
41     std::string hapPath_;
42     AppExecFwk::ModuleType moduleType_ = AppExecFwk::ModuleType::UNKNOWN;
43     bool esModule_;
44 
45     ModuleStartupConfigInfo(std::string name, std::string startupConfig, std::string hapPath,
46         const AppExecFwk::ModuleType& moduleType, bool esModule);
47 };
48 
49 class StartupManager : public std::enable_shared_from_this<StartupManager> {
50 DECLARE_DELAYED_SINGLETON(StartupManager)
51 
52 public:
53     int32_t PreloadAppHintStartup(const AppExecFwk::BundleInfo& bundleInfo,
54         const AppExecFwk::HapModuleInfo& entryInfo, const std::string &preloadModuleName,
55         std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData);
56 
57     int32_t LoadAppStartupTaskConfig(bool &needRunAutoStartupTask);
58 
59     const std::vector<StartupTaskInfo> GetStartupTaskInfos(const std::string &name);
60 
61     const std::string &GetPendingConfigEntry() const;
62 
63     void ClearAppStartupTask();
64 
65     int32_t RegisterAppStartupTask(
66         const std::string &name, const std::shared_ptr<AppStartupTask> &startupTask);
67 
68     int32_t BuildAutoAppStartupTaskManager(std::shared_ptr<AAFwk::Want> want,
69         std::shared_ptr<StartupTaskManager> &startupTaskManager, const std::string &moduleName);
70 
71     int32_t BuildAppStartupTaskManager(const std::vector<std::string> &inputDependencies,
72         std::shared_ptr<StartupTaskManager> &startupTaskManager, bool supportFeatureModule);
73 
74     int32_t OnStartupTaskManagerComplete(uint32_t id);
75 
76     int32_t RunLoadModuleStartupConfigTask(
77         bool &needRunAutoStartupTask, const std::shared_ptr<AppExecFwk::HapModuleInfo>& hapModuleInfo);
78 
79     void SetModuleConfig(const std::shared_ptr<StartupConfig> &config, const std::string &moduleName,
80         bool isDefaultConfig);
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     int32_t BuildStartupTaskManager(const std::map<std::string, std::shared_ptr<StartupTask>> &tasks,
101         std::shared_ptr<StartupTaskManager> &startupTaskManager);
102 
103     bool EnableLazyLoadingAppStartupTasks() const;
104 
105 private:
106     // read only after initialization
107     std::vector<ModuleStartupConfigInfo> moduleStartupConfigInfos_;
108     std::string bundleName_;
109 
110     std::mutex appStartupConfigInitializationMutex_;
111     std::atomic<bool> isAppStartupConfigInited_ = false;
112     std::atomic<bool> isAppStartupTaskRegistered_ = false;
113     std::set<std::string> isModuleStartupConfigInited_;
114 
115     std::mutex startupTaskManagerMutex_;
116     uint32_t startupTaskManagerId = 0;
117     std::map<uint32_t, std::shared_ptr<StartupTaskManager>> startupTaskManagerMap_;
118 
119     // read only after initialization
120     std::map<std::string, std::shared_ptr<AppStartupTask>> preloadSoStartupTasks_;
121     std::map<std::string, std::shared_ptr<AppStartupTask>> preloadSystemSoStartupTasks_;
122     std::map<std::string, std::shared_ptr<AppStartupTask>> appStartupTasks_;
123     std::vector<StartupTaskInfo> pendingStartupTaskInfos_;
124     std::string pendingConfigEntry_;
125 
126     std::mutex autoPreloadSoTaskManagerMutex_;
127     std::weak_ptr<StartupTaskManager> autoPreloadSoTaskManager_;
128     std::weak_ptr<StartupTaskManager> autoPreloadSystemSoTaskManager_;
129     bool autoPreloadSoStopped_ = false;
130     bool enableLazyLoadingAppStartupTasks_ = false;
131 
132     std::shared_ptr<StartupConfig> defaultConfig_;
133     std::map<std::string, std::shared_ptr<StartupConfig>> moduleConfigs_;
134     std::shared_ptr<AppExecFwk::EventHandler> mainHandler_;
135     std::shared_ptr<AppExecFwk::EventHandler> preloadHandler_;
136     std::unordered_set<std::string> preloadSystemSoAllowlist_;
137 
138     static int32_t AddStartupTask(const std::string &name, std::map<std::string, std::shared_ptr<StartupTask>> &taskMap,
139         std::map<std::string, std::shared_ptr<AppStartupTask>> &allTasks);
140     int32_t RegisterPreloadSoStartupTask(
141         const std::string &name, const std::shared_ptr<PreloadSoStartupTask> &startupTask);
142     bool FilterMatchedStartupTask(const AppStartupTaskMatcher &taskMatcher,
143         const std::map<std::string, std::shared_ptr<AppStartupTask>> &inTasks,
144         std::map<std::string, std::shared_ptr<StartupTask>> &outTasks,
145         std::set<std::string> &dependenciesSet);
146     int32_t AddAppPreloadSoTask(const std::vector<std::string> &preloadSoList,
147         std::map<std::string, std::shared_ptr<StartupTask>> &currentStartupTasks);
148     std::shared_ptr<NativeStartupTask> CreateAppPreloadSoTask(
149         const std::map<std::string, std::shared_ptr<StartupTask>> &currentPreloadSoTasks);
150 
151     void InitPreloadSystemSoAllowlist();
152     bool ReadPreloadSystemSoAllowlistFile(nlohmann::json &jsonStr);
153     bool ParsePreloadSystemSoAllowlist(const nlohmann::json &jsonStr, std::unordered_set<std::string> &allowlist);
154 
155     void PreloadAppHintStartupTask(std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData);
156     int32_t AddLoadAppStartupConfigTask(std::map<std::string, std::shared_ptr<StartupTask>> &preloadAppHintTasks);
157     int32_t RunLoadAppStartupConfigTask();
158     int32_t AddAppAutoPreloadSoTask(std::map<std::string, std::shared_ptr<StartupTask>> &preloadAppHintTasks,
159         std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData);
160     int32_t RunAppAutoPreloadSoTask(std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData);
161     int32_t RunAppAutoPreloadSystemSoTask();
162     int32_t RunAppPreloadSoTask(const std::map<std::string, std::shared_ptr<StartupTask>> &appPreloadSoTasks,
163         bool isSystemSo = false);
164     int32_t GetAppAutoPreloadSoTasks(std::map<std::string, std::shared_ptr<StartupTask>> &appAutoPreloadSoTasks,
165         std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData);
166     int32_t RunAppPreloadSoTaskMainThread(const std::map<std::string, std::shared_ptr<StartupTask>> &appPreloadSoTasks,
167         std::unique_ptr<StartupTaskResultCallback> callback);
168 
169     static int32_t GetStartupConfigString(const ModuleStartupConfigInfo& info, std::string& config);
170     bool AnalyzeStartupConfig(const ModuleStartupConfigInfo& info, const std::string& startupConfig,
171         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks,
172         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSystemSoStartupTasks,
173         std::vector<StartupTaskInfo>& pendingStartupTaskInfos, std::string& pendingConfigEntry);
174     bool AnalyzeAppStartupTask(const ModuleStartupConfigInfo& info, nlohmann::json &startupConfigJson,
175         std::vector<StartupTaskInfo>& pendingStartupTaskInfos);
176     bool AnalyzePreloadSoStartupTask(const ModuleStartupConfigInfo& info, nlohmann::json &startupConfigJson,
177         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks);
178     bool AnalyzeAppStartupTaskInner(const ModuleStartupConfigInfo& info,
179         const nlohmann::json &startupTaskJson,
180         std::vector<StartupTaskInfo>& pendingStartupTaskInfos);
181     bool AnalyzePreloadSoStartupTaskInner(const ModuleStartupConfigInfo& info,
182         const nlohmann::json &preloadStartupTaskJson,
183         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks);
184     void AnalyzePreloadSystemSoStartupTask(nlohmann::json &startupConfigJson,
185         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks);
186     void AnalyzePreloadSystemSoStartupTaskInner(const nlohmann::json &preloadStartupTaskJson,
187         std::map<std::string, std::shared_ptr<AppStartupTask>>& preloadSoStartupTasks);
188     void SetOptionalParameters(const nlohmann::json& module, AppExecFwk::ModuleType moduleType,
189         StartupTaskInfo& startupTaskInfo);
190     void SetOptionalParameters(const nlohmann::json &module, AppExecFwk::ModuleType moduleType,
191         std::shared_ptr<PreloadSoStartupTask> &task);
192     void SetMatchRules(const nlohmann::json &module, StartupTaskMatchRules &matchRules, bool isPreloadSoStartupTask);
193     static bool ParseJsonStringArray(const nlohmann::json &json, const std::string &key, std::vector<std::string> &arr);
194 };
195 } // namespace AbilityRuntime
196 } // namespace OHOS
197 #endif // OHOS_ABILITY_RUNTIME_STARTUP_MANAGER_H
198