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_TASK_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_STARTUP_TASK_MANAGER_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 23 #include "event_handler.h" 24 #include "startup_config.h" 25 #include "startup_task.h" 26 #include "startup_task_dispatcher.h" 27 #include "startup_utils.h" 28 29 namespace OHOS { 30 namespace AbilityRuntime { 31 class StartupTaskManager : public std::enable_shared_from_this<StartupTaskManager> { 32 public: 33 explicit StartupTaskManager(uint32_t startupTaskManagerId, 34 std::map<std::string, std::shared_ptr<StartupTask>> autoStartupTasks); 35 36 ~StartupTaskManager(); 37 38 int32_t AddTask(const std::shared_ptr<StartupTask> &task); 39 40 void SetConfig(const std::shared_ptr<StartupConfig> &config); 41 42 size_t GetStartupTaskCount() const; 43 44 int32_t Prepare(); 45 46 int32_t Run(const std::shared_ptr<OnCompletedCallback> &mainThreadAwaitCallback); 47 48 void TimeoutStop(); 49 50 void OnTimeout(); 51 52 private: 53 uint32_t startupTaskManagerId_ = 0; 54 std::shared_ptr<StartupConfig> config_; 55 std::shared_ptr<StartupTaskDispatcher> dispatcher_; 56 std::map<std::string, std::shared_ptr<StartupTask>> tasks_; 57 std::shared_ptr<AppExecFwk::EventHandler> mainHandler_; 58 59 void CallListenerOnCompleted(int32_t result, const std::string &resultMessage = ""); 60 void AddAsyncTimeoutTimer(); 61 void CancelAsyncTimeoutTimer(); 62 }; 63 } // namespace AbilityRuntime 64 } // namespace OHOS 65 #endif // OHOS_ABILITY_RUNTIME_STARTUP_TASK_MANAGER_H 66