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_DISPATCHER_H
17 #define OHOS_ABILITY_RUNTIME_STARTUP_TASK_DISPATCHER_H
18 
19 #include <deque>
20 
21 #include "startup_sort_result.h"
22 #include "startup_task_result.h"
23 #include "startup_utils.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class StartupTaskDispatcher : public std::enable_shared_from_this<StartupTaskDispatcher> {
28 public:
29     StartupTaskDispatcher(const std::map<std::string, std::shared_ptr<StartupTask>> &tasks,
30         const std::shared_ptr<StartupSortResult> &sortResult);
31 
32     ~StartupTaskDispatcher();
33 
34     int32_t Run(const std::shared_ptr<OnCompletedCallback> &completedCallback,
35         const std::shared_ptr<OnCompletedCallback> &mainThreadAwaitCallback);
36 
37 private:
38     const std::map<std::string, std::shared_ptr<StartupTask>> &tasks_;
39     std::shared_ptr<StartupSortResult> sortResult_;
40     std::map<std::string, std::uint32_t> inDegreeMap_;
41     uint32_t mainThreadAwaitCount_ = 0;
42     uint32_t tasksCount_ = 0;
43     std::shared_ptr<OnCompletedCallback> completedCallback_;
44     std::shared_ptr<OnCompletedCallback> mainThreadAwaitCallback_;
45 
46     void Dispatch(const std::string &name, const std::shared_ptr<StartupTaskResult> &result);
47     int32_t NotifyChildren(const std::string &name, const std::shared_ptr<StartupTaskResult> &result);
48     int32_t RunTaskInit(const std::string &name, const std::shared_ptr<StartupTask> &task);
49     void OnError(const std::string &name, const std::shared_ptr<StartupTaskResult> &result);
50     void OnError(int32_t errorCode, const std::string &errorMessage);
51 };
52 } // namespace AbilityRuntime
53 } // namespace OHOS
54 #endif // OHOS_ABILITY_RUNTIME_STARTUP_TASK_DISPATCHER_H
55