Add new features into boot sequence app info
[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::Builder& AppInfo::Builder::SetSocketReadyTimestamp(
148     const tizen_base::Bundle& args) {
149   socket_ready_timestamp_ = args.GetString(AUL_K_SOCKET_READY_TIMESTAMP);
150   return *this;
151 }
152
153 AppInfo::Builder& AppInfo::Builder::SetTerminatedTimestamp(
154     const tizen_base::Bundle& args) {
155   terminated_timestamp_ = args.GetString(AUL_K_TERMINATED_TIMESTAMP);
156   return *this;
157 }
158
159 AppInfo::Builder& AppInfo::Builder::SetStatusMessage(
160     const tizen_base::Bundle& args) {
161   status_msg_ = args.GetString(AUL_K_STATUS_MSG);
162   return *this;
163 }
164
165 AppInfo* AppInfo::Builder::Build() const {
166   return new (std::nothrow) AppInfo(
167       std::move(appid_),
168       uid_,
169       priority_,
170       std::move(after_),
171       std::move(before_),
172       std::move(requires_),
173       std::move(conflicts_),
174       std::move(args_),
175       std::move(active_state_),
176       std::move(sub_state_),
177       std::move(begin_timestamp_),
178       std::move(end_timestamp_),
179       std::move(socket_ready_timestamp_),
180       std::move(terminated_timestamp_),
181       std::move(status_msg_),
182       background_launch_,
183       wait_until_ready_,
184       timeout_);
185 }
186
187 AppInfo::AppInfo(std::string appid, uid_t uid, int priority,
188     std::set<std::string> after, std::set<std::string> before,
189     std::set<std::string> requires, std::set<std::string> conflicts,
190     tizen_base::Bundle args,
191     std::string active_state, std::string sub_state,
192     std::string begin_timestamp, std::string end_timestamp,
193     std::string socket_ready_timestamp, std::string terminated_timestamp,
194     std::string status_msg,
195     bool background_launch, bool wait_until_ready, unsigned int timeout)
196     : appid_(std::move(appid)),
197       uid_(uid),
198       priority_(priority),
199       after_(std::move(after)),
200       before_(std::move(before)),
201       requires_(std::move(requires)),
202       conflicts_(std::move(conflicts)),
203       args_(std::move(args)),
204       active_state_(std::move(active_state)),
205       sub_state_(std::move(sub_state)),
206       begin_timestamp_(std::move(begin_timestamp)),
207       end_timestamp_(std::move(end_timestamp)),
208       socket_ready_timestamp_(std::move(socket_ready_timestamp)),
209       terminated_timestamp_(std::move(terminated_timestamp)),
210       background_launch_(background_launch),
211       wait_until_ready_(wait_until_ready),
212       timeout_(timeout),
213       status_msg_(std::move(status_msg)) {
214   if (background_launch_) {
215     args_.Delete(K_BG_LAUNCH);
216     args_.Add(K_BG_LAUNCH, "enable");
217   }
218 }
219
220 const std::string& AppInfo::GetAppId() const {
221   return appid_;
222 }
223
224 uid_t AppInfo::GetUid() const {
225   return uid_;
226 }
227
228 int AppInfo::GetPriority() const {
229   return priority_;
230 }
231
232 // After
233 bool AppInfo::AfterContains(const std::string& appid) {
234   return after_.find(appid) != after_.end();
235 }
236
237 const std::set<std::string>& AppInfo::AfterGet() const {
238   return after_;
239 }
240
241 // Before
242 bool AppInfo::BeforeContains(const std::string& appid) {
243   return before_.find(appid) != before_.end();
244 }
245
246 const std::set<std::string>& AppInfo::BeforeGet() const {
247   return before_;
248 }
249
250 // Requires
251 bool AppInfo::RequiresContains(const std::string& appid) {
252   return requires_.find(appid) != requires_.end();
253 }
254
255 const std::set<std::string>& AppInfo::RequiresGet() const {
256   return requires_;
257 }
258
259 // Conflicts
260 bool AppInfo::ConflictsContains(const std::string& appid) {
261   return conflicts_.find(appid) != conflicts_.end();
262 }
263
264 const std::set<std::string>& AppInfo::ConflictsGet() const {
265   return conflicts_;
266 }
267
268 const tizen_base::Bundle& AppInfo::GetArgs() const {
269   return args_;
270 }
271
272 bool AppInfo::IsBackgroundLaunch() const {
273   return background_launch_;
274 }
275
276 bool AppInfo::WaitUntilReady() const {
277   return wait_until_ready_;
278 }
279
280 unsigned int AppInfo::GetTimeout() const {
281   return timeout_;
282 }
283
284 const std::string& AppInfo::GetActiveState() const {
285   return active_state_;
286 }
287
288 const std::string& AppInfo::GetSubState() const {
289   return sub_state_;
290 }
291
292 const std::string& AppInfo::GetBeginTimestamp() const {
293   return begin_timestamp_;
294 }
295
296 const std::string& AppInfo::GetEndTimestamp() const {
297   return end_timestamp_;
298 }
299
300 const std::string& AppInfo::GetSocketReadyTimestamp() const {
301   return socket_ready_timestamp_;
302 }
303
304 const std::string& AppInfo::GetTerminatedTimestamp() const {
305   return terminated_timestamp_;
306 }
307
308 const std::string& AppInfo::GetStatusMessage() const {
309   return status_msg_;
310 }
311
312 }  // namespace boot_sequence
313 }  // namespace aul::internal