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 #include "startup_task.h"
17 #include "hilog_tag_wrapper.h"
18
19 namespace OHOS {
20 namespace AbilityRuntime {
StartupTask(const std::string & name)21 StartupTask::StartupTask(const std::string &name) : name_(name), state_(State::CREATED)
22 {}
23
24 StartupTask::~StartupTask() = default;
25
GetName() const26 const std::string& StartupTask::GetName() const
27 {
28 return name_;
29 }
30
GetDependencies() const31 std::vector<std::string> StartupTask::GetDependencies() const
32 {
33 return dependencies_;
34 }
35
GetCallCreateOnMainThread() const36 bool StartupTask::GetCallCreateOnMainThread() const
37 {
38 return callCreateOnMainThread_;
39 }
40
GetWaitOnMainThread() const41 bool StartupTask::GetWaitOnMainThread() const
42 {
43 return waitOnMainThread_;
44 }
45
GetIsExcludeFromAutoStart() const46 bool StartupTask::GetIsExcludeFromAutoStart() const
47 {
48 return isExcludeFromAutoStart_;
49 }
50
SetDependencies(const std::vector<std::string> & dependencies)51 void StartupTask::SetDependencies(const std::vector<std::string> &dependencies)
52 {
53 dependencies_ = dependencies;
54 }
55
SetCallCreateOnMainThread(bool callCreateOnMainThread)56 void StartupTask::SetCallCreateOnMainThread(bool callCreateOnMainThread)
57 {
58 callCreateOnMainThread_ = callCreateOnMainThread;
59 }
60
SetWaitOnMainThread(bool waitOnMainThread)61 void StartupTask::SetWaitOnMainThread(bool waitOnMainThread)
62 {
63 waitOnMainThread_ = waitOnMainThread;
64 }
65
SetIsExcludeFromAutoStart(bool excludeFromAutoStart)66 void StartupTask::SetIsExcludeFromAutoStart(bool excludeFromAutoStart)
67 {
68 isExcludeFromAutoStart_ = excludeFromAutoStart;
69 }
70
SaveResult(const std::shared_ptr<StartupTaskResult> & result)71 void StartupTask::SaveResult(const std::shared_ptr<StartupTaskResult> &result)
72 {
73 if (result == nullptr) {
74 TAG_LOGE(AAFwkTag::STARTUP, "startup task: %{public}s, result null", name_.c_str());
75 return;
76 }
77 TAG_LOGD(AAFwkTag::STARTUP,
78 "startup task: %{public}s, result code: %{public}d", name_.c_str(), result->GetResultCode());
79 result_ = result;
80 if (result->GetResultCode() == ERR_OK) {
81 state_ = State::INITIALIZED;
82 } else {
83 state_ = State::CREATED;
84 }
85 }
86
RemoveResult()87 int32_t StartupTask::RemoveResult()
88 {
89 if (state_ != State::INITIALIZED) {
90 TAG_LOGE(AAFwkTag::STARTUP, "%{public}s, result not init", name_.c_str());
91 return ERR_STARTUP_INTERNAL_ERROR;
92 }
93 result_ = nullptr;
94 state_ = State::CREATED;
95 return ERR_OK;
96 }
97
GetResult() const98 const std::shared_ptr<StartupTaskResult>& StartupTask::GetResult() const
99 {
100 return result_;
101 }
102
GetState() const103 StartupTask::State StartupTask::GetState() const
104 {
105 return state_;
106 }
107
DumpDependencies() const108 std::string StartupTask::DumpDependencies() const
109 {
110 if (dependencies_.empty()) {
111 return "";
112 }
113 bool isFirst = true;
114 std::string dumpResult;
115 for (const auto &iter : dependencies_) {
116 if (isFirst) {
117 dumpResult = iter;
118 isFirst = false;
119 } else {
120 dumpResult += ", " + iter;
121 }
122 }
123 return dumpResult;
124 }
125
getDependenciesCount() const126 uint32_t StartupTask::getDependenciesCount() const
127 {
128 return dependencies_.size();
129 }
130
AddExtraCallback(std::unique_ptr<StartupTaskResultCallback> callback)131 int32_t StartupTask::AddExtraCallback(std::unique_ptr<StartupTaskResultCallback> callback)
132 {
133 if (state_ != State::INITIALIZING) {
134 TAG_LOGE(AAFwkTag::STARTUP, "state not INITIALIZING");
135 return ERR_STARTUP_INTERNAL_ERROR;
136 }
137 // extra callback will called while init done
138 extraCallbacks_.emplace_back(std::move(callback));
139 return ERR_OK;
140 }
141
CallExtraCallback(const std::shared_ptr<StartupTaskResult> & result)142 void StartupTask::CallExtraCallback(const std::shared_ptr<StartupTaskResult> &result)
143 {
144 for (auto &callback : extraCallbacks_) {
145 if (callback != nullptr) {
146 callback->Call(result);
147 }
148 }
149 extraCallbacks_.clear();
150 }
151 } // namespace AbilityRuntime
152 } // namespace OHOS
153