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_H
17 #define OHOS_ABILITY_RUNTIME_STARTUP_TASK_H
18 
19 #include <string>
20 #include <vector>
21 #include <memory>
22 
23 #include "startup_task_result.h"
24 #include "startup_utils.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 class StartupTask : public std::enable_shared_from_this<StartupTask> {
29 public:
30     enum class State {
31         INVALID,
32         CREATED,
33         INITIALIZING,
34         INITIALIZED,
35     };
36 
37     explicit StartupTask(const std::string &name);
38 
39     virtual ~StartupTask();
40 
41     const std::string& GetName() const;
42 
43     std::vector<std::string> GetDependencies() const;
44 
45     bool GetCallCreateOnMainThread() const;
46 
47     bool GetWaitOnMainThread() const;
48 
49     bool GetIsExcludeFromAutoStart() const;
50 
51     void SetDependencies(const std::vector<std::string> &dependencies);
52 
53     void SetCallCreateOnMainThread(bool callCreateOnMainThread);
54 
55     void SetWaitOnMainThread(bool waitOnMainThread);
56 
57     void SetIsExcludeFromAutoStart(bool excludeFromAutoStart);
58 
59     uint32_t getDependenciesCount() const;
60 
61     void SaveResult(const std::shared_ptr<StartupTaskResult> &result);
62 
63     int32_t RemoveResult();
64 
65     const std::shared_ptr<StartupTaskResult>& GetResult() const;
66 
67     virtual int32_t RunTaskInit(std::unique_ptr<StartupTaskResultCallback> callback) = 0;
68 
69     virtual int32_t RunTaskOnDependencyCompleted(const std::string &name,
70         const std::shared_ptr<StartupTaskResult> &result) = 0;
71 
72     State GetState() const;
73 
74     int32_t AddExtraCallback(std::unique_ptr<StartupTaskResultCallback> callback);
75 
76     void CallExtraCallback(const std::shared_ptr<StartupTaskResult> &result);
77 
78     virtual void OnAsyncTaskCompleted(const std::shared_ptr<StartupTaskResult> &result) = 0;
79 
80 protected:
81     std::string name_;
82     std::vector<std::string> dependencies_;
83     bool callCreateOnMainThread_ = true;
84     bool waitOnMainThread_ = true;
85     bool isExcludeFromAutoStart_ = false;
86     std::shared_ptr<StartupTaskResult> result_;
87     State state_ = State::INVALID;
88     std::vector<std::unique_ptr<StartupTaskResultCallback>> extraCallbacks_;
89 
90     std::string DumpDependencies() const;
91 };
92 } // namespace AbilityRuntime
93 } // namespace OHOS
94 #endif // OHOS_ABILITY_RUNTIME_STARTUP_TASK_H
95