Improve aulctl tool
[platform/core/appfw/aul-1.git] / tool / aulctl / boot_sequence / app_info.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "app_info.hh"
18
19 #include <bundle_internal.h>
20
21 #include "include/aul_key.h"
22 #include "src/aul_util.h"
23
24 namespace aul::internal {
25 namespace boot_sequence {
26 namespace {
27
28 constexpr const char METADATA_KEY_ONBOOT_PRIORITY[] =
29     "http://tizen.org/metadata/on-boot/priority";
30 constexpr const int MIN_PRIORITY = 1;
31 constexpr const int MAX_PRIORITY = 99;
32 constexpr const char K_BG_LAUNCH[] = "__K_BG_LAUNCH";
33 constexpr const unsigned int DEFAULT_TIMEOUT = 5000;
34
35 }  // namespace
36
37 AppInfo::Builder& AppInfo::Builder::SetAppId(const tizen_base::Bundle& args) {
38   appid_ = args.GetString(AUL_K_APPID);
39   return *this;
40 }
41
42 AppInfo::Builder& AppInfo::Builder::SetUid(const tizen_base::Bundle& args) {
43   uid_ = static_cast<uid_t>(std::stoi(args.GetString(AUL_K_TARGET_UID)));
44   return *this;
45 }
46
47 AppInfo::Builder& AppInfo::Builder::SetPriority(
48     const tizen_base::Bundle& args) {
49   priority_ = std::stoi(args.GetString(AUL_K_BOOT_SEQUENCE_PRIORITY));
50   return *this;
51 }
52
53 AppInfo::Builder& AppInfo::Builder::SetAfter(const tizen_base::Bundle& args) {
54   auto apps = args.GetStringArray(AUL_K_BOOT_SEQUENCE_AFTER);
55   after_.insert(apps.begin(), apps.end());
56   return *this;
57 }
58
59 AppInfo::Builder& AppInfo::Builder::SetBefore(const tizen_base::Bundle& args) {
60   auto apps = args.GetStringArray(AUL_K_BOOT_SEQUENCE_BEFORE);
61   before_.insert(apps.begin(), apps.end());
62   return *this;
63 }
64
65 AppInfo::Builder& AppInfo::Builder::SetRequires(
66     const tizen_base::Bundle& args) {
67   auto apps = args.GetStringArray(AUL_K_BOOT_SEQUENCE_REQUIRES);
68   requires_.insert(apps.begin(), apps.end());
69   return *this;
70 }
71
72 AppInfo::Builder& AppInfo::Builder::SetConflicts(
73     const tizen_base::Bundle& args) {
74   auto apps = args.GetStringArray(AUL_K_BOOT_SEQUENCE_CONFLICTS);
75   conflicts_.insert(apps.begin(), apps.end());
76   return *this;
77 }
78
79 AppInfo::Builder& AppInfo::Builder::SetArgs(
80     const tizen_base::Bundle& args) {
81   static const std::vector<std::string> internal_keys{
82     "__AUL_APPID__",
83     "__AUL_STARTTIME__",
84     "__K_BG_LAUNCH"
85   };
86   auto arg_json = args.GetString(AUL_K_ARGS);
87   bundle* b = nullptr;
88   if (bundle_from_json(arg_json.c_str(), &b) != BUNDLE_ERROR_NONE) {
89     _E("bundle_from_json() is failed");
90   } else {
91     args_ = tizen_base::Bundle(b, false, true);
92     for (auto& key : internal_keys)
93       args_.Delete(key);
94   }
95
96   return *this;
97 }
98
99 AppInfo::Builder& AppInfo::Builder::SetBackgroundLaunch(
100     const tizen_base::Bundle& args) {
101   background_launch_ = (args.GetString(AUL_K_ALLOWED_BG) == "true");
102   return *this;
103 }
104
105 AppInfo::Builder& AppInfo::Builder::SetWaitUntilReady(
106     const tizen_base::Bundle& args) {
107   wait_until_ready_ =
108       (args.GetString(AUL_K_BOOT_SEQUENCE_READY_WAIT) == "true");
109   return *this;
110 }
111
112 AppInfo::Builder& AppInfo::Builder::SetTimeout(
113     const tizen_base::Bundle& args) {
114   auto timeout_str = args.GetString(AUL_K_TIMEOUT);
115   if (isdigit(timeout_str[0]))
116     timeout_ = static_cast<unsigned int>(std::stoi(timeout_str));
117   else
118     timeout_ = DEFAULT_TIMEOUT;
119
120   return *this;
121 }
122
123 AppInfo::Builder& AppInfo::Builder::SetActiveState(
124     const tizen_base::Bundle& args) {
125   active_state_ = args.GetString(AUL_K_STATUS);
126   return *this;
127 }
128
129 AppInfo::Builder& AppInfo::Builder::SetSubState(
130     const tizen_base::Bundle& args) {
131   sub_state_ = args.GetString(AUL_K_SUB_STATUS);
132   return *this;
133 }
134
135 AppInfo::Builder& AppInfo::Builder::SetBeginTimestamp(
136     const tizen_base::Bundle& args) {
137   begin_timestamp_ = args.GetString(AUL_K_BEGIN_TIMESTAMP);
138   return *this;
139 }
140
141 AppInfo::Builder& AppInfo::Builder::SetEndTimestamp(
142     const tizen_base::Bundle& args) {
143   end_timestamp_ = args.GetString(AUL_K_END_TIMESTAMP);
144   return *this;
145 }
146
147 AppInfo* AppInfo::Builder::Build() const {
148   return new (std::nothrow) AppInfo(
149       std::move(appid_),
150       uid_,
151       priority_,
152       std::move(after_),
153       std::move(before_),
154       std::move(requires_),
155       std::move(conflicts_),
156       std::move(args_),
157       std::move(active_state_),
158       std::move(sub_state_),
159       std::move(begin_timestamp_),
160       std::move(end_timestamp_),
161       background_launch_,
162       wait_until_ready_,
163       timeout_);
164 }
165
166 AppInfo::AppInfo(std::string appid, uid_t uid, int priority,
167     std::set<std::string> after, std::set<std::string> before,
168     std::set<std::string> requires, std::set<std::string> conflicts,
169     tizen_base::Bundle args,
170     std::string active_state, std::string sub_state,
171     std::string begin_timestamp, std::string end_timestamp,
172     bool background_launch, bool wait_until_ready, unsigned int timeout)
173     : appid_(std::move(appid)),
174       uid_(uid),
175       priority_(priority),
176       after_(std::move(after)),
177       before_(std::move(before)),
178       requires_(std::move(requires)),
179       conflicts_(std::move(conflicts)),
180       args_(std::move(args)),
181       active_state_(std::move(active_state)),
182       sub_state_(std::move(sub_state)),
183       begin_timestamp_(std::move(begin_timestamp)),
184       end_timestamp_(std::move(end_timestamp)),
185       background_launch_(background_launch),
186       wait_until_ready_(wait_until_ready),
187       timeout_(timeout) {
188   if (background_launch_) {
189     args_.Delete(K_BG_LAUNCH);
190     args_.Add(K_BG_LAUNCH, "enable");
191   }
192 }
193
194 const std::string& AppInfo::GetAppId() const {
195   return appid_;
196 }
197
198 uid_t AppInfo::GetUid() const {
199   return uid_;
200 }
201
202 int AppInfo::GetPriority() const {
203   return priority_;
204 }
205
206 // After
207 bool AppInfo::AfterContains(const std::string& appid) {
208   return after_.find(appid) != after_.end();
209 }
210
211 const std::set<std::string>& AppInfo::AfterGet() const {
212   return after_;
213 }
214
215 // Before
216 bool AppInfo::BeforeContains(const std::string& appid) {
217   return before_.find(appid) != before_.end();
218 }
219
220 const std::set<std::string>& AppInfo::BeforeGet() const {
221   return before_;
222 }
223
224 // Requires
225 bool AppInfo::RequiresContains(const std::string& appid) {
226   return requires_.find(appid) != requires_.end();
227 }
228
229 const std::set<std::string>& AppInfo::RequiresGet() const {
230   return requires_;
231 }
232
233 // Conflicts
234 bool AppInfo::ConflictsContains(const std::string& appid) {
235   return conflicts_.find(appid) != conflicts_.end();
236 }
237
238 const std::set<std::string>& AppInfo::ConflictsGet() const {
239   return conflicts_;
240 }
241
242 const tizen_base::Bundle& AppInfo::GetArgs() const {
243   return args_;
244 }
245
246 bool AppInfo::IsBackgroundLaunch() const {
247   return background_launch_;
248 }
249
250 bool AppInfo::WaitUntilReady() const {
251   return wait_until_ready_;
252 }
253
254 unsigned int AppInfo::GetTimeout() const {
255   return timeout_;
256 }
257
258 const std::string& AppInfo::GetActiveState() const {
259   return active_state_;
260 }
261
262 const std::string& AppInfo::GetSubState() const {
263   return sub_state_;
264 }
265
266 const std::string& AppInfo::GetBeginTimestamp() const {
267   return begin_timestamp_;
268 }
269
270 const std::string& AppInfo::GetEndTimestamp() const {
271   return end_timestamp_;
272 }
273
274 const std::string& AppInfo::GetStatusMessage() const {
275   return status_msg_;
276 }
277
278 }  // namespace boot_sequence
279 }  // namespace aul::internal