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 <string>
22 
23 #include "singleton.h"
24 #include "startup_config.h"
25 #include "startup_task.h"
26 #include "startup_task_manager.h"
27 #include "startup_utils.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
31 class StartupManager : public std::enable_shared_from_this<StartupManager> {
32 DECLARE_DELAYED_SINGLETON(StartupManager)
33 
34 public:
35     int32_t RegisterStartupTask(const std::string &name, const std::shared_ptr<StartupTask> &startupTask);
36 
37     int32_t BuildAutoStartupTaskManager(std::shared_ptr<StartupTaskManager> &startupTaskManager);
38 
39     int32_t BuildStartupTaskManager(const std::vector<std::string> &inputDependencies,
40         std::shared_ptr<StartupTaskManager> &startupTaskManager);
41 
42     int32_t OnStartupTaskManagerComplete(uint32_t id);
43 
44     void SetDefaultConfig(const std::shared_ptr<StartupConfig> &config);
45 
46     const std::shared_ptr<StartupConfig>& GetDefaultConfig() const;
47 
48     int32_t RemoveAllResult();
49 
50     int32_t RemoveResult(const std::string &name);
51 
52     int32_t GetResult(const std::string &name, std::shared_ptr<StartupTaskResult> &result);
53 
54     int32_t IsInitialized(const std::string &name, bool &isInitialized);
55 
56     int32_t PostMainThreadTask(const std::function<void()> &task);
57 
58 private:
59     uint32_t startupTaskManagerId = 0;
60     std::map<uint32_t, std::shared_ptr<StartupTaskManager>> startupTaskManagerMap_;
61     // read only after initialization
62     std::map<std::string, std::shared_ptr<StartupTask>> startupTasks_;
63     std::shared_ptr<StartupConfig> defaultConfig_;
64     std::shared_ptr<AppExecFwk::EventHandler> mainHandler_;
65 
66     int32_t AddStartupTask(const std::string &name,
67         std::map<std::string, std::shared_ptr<StartupTask>> &taskMap);
68 };
69 } // namespace AbilityRuntime
70 } // namespace OHOS
71 #endif // OHOS_ABILITY_RUNTIME_STARTUP_MANAGER_H
72