2 * Copyright (c) 2019 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "include/aul_comp_context.h"
19 #include <bundle_cpp.h>
25 #include "app_request.h"
28 #include "include/aul.h"
29 #include "include/aul_error.h"
31 #include "aul/common/exception.hh"
32 #include "aul/component/component_running_context.hh"
36 struct aul_comp_context_s {
41 using namespace aul::internal;
43 ComponentRunningContext* CreateComponentRunningContext(
44 const tizen_base::Bundle& b) {
45 return ComponentRunningContext::Builder()
55 ComponentRunningContext* GetComponentRunningContext(
56 const std::string& component_id, uid_t uid) {
57 tizen_base::Bundle b { { AUL_K_COMPONENT_ID, component_id } };
58 int fd = AppRequest(COMP_CONTEXT_GET, uid)
60 .SendSimply(AUL_SOCK_ASYNC);
65 int ret = aul_sock_recv_reply_pkt(fd, &pkt);
67 THROW(aul_error_convert(fd));
69 auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
72 if (pkt->cmd != APP_GET_INFO_OK) {
73 _E("Failed to get component running context. error(%d)", pkt->cmd);
78 if (pkt->opt & AUL_SOCK_BUNDLE) {
79 kb = bundle_decode(pkt->data, pkt->len);
87 return CreateComponentRunningContext(tizen_base::Bundle(kb, false, true));
90 std::vector<std::unique_ptr<ComponentRunningContext>>
91 GetComponentRunningContexts() {
92 int fd = AppRequest(COMP_CONTEXT_FOREACH, getuid())
93 .SendSimply(AUL_SOCK_ASYNC);
97 std::vector<std::unique_ptr<ComponentRunningContext>> contexts;
98 int ret = aul_sock_recv_pkt_with_cb(fd,
99 [](app_pkt_t* pkt, void* user_data) {
100 if (pkt == nullptr) {
101 _E("pkt is nullptr");
105 if (pkt->cmd == APP_GET_INFO_ERROR) {
106 _E("Failed to get component running context");
110 bundle* kb = nullptr;
111 if (pkt->opt & AUL_SOCK_BUNDLE)
112 kb = bundle_decode(pkt->data, pkt->len);
117 tizen_base::Bundle b(kb, false, true);
118 auto* context_array =
119 static_cast<std::vector<std::unique_ptr<ComponentRunningContext>>*>(
123 context_array->emplace_back(CreateComponentRunningContext(b));
124 } catch (const Exception& e) {
125 _E("Exception occurs. error(%s)", e.what());
129 THROW(aul_error_convert(ret));
134 int SendRequest(ComponentRunningContext* context, int cmd) {
135 tizen_base::Bundle b {
136 { AUL_K_COMPONENT_ID, context->GetComponentId() },
137 { AUL_K_INSTANCE_ID, context->GetInstanceId() }
140 int ret = AppRequest(cmd, getuid())
151 extern "C" API int aul_comp_context_foreach_comp_context(
152 aul_comp_context_cb callback, void* user_data) {
153 if (callback == nullptr) {
154 _E("Invalid parameter");
159 for (auto const& context : GetComponentRunningContexts()) {
160 auto* handle = reinterpret_cast<aul_comp_context_h>(context.get());
161 if (!callback(handle, user_data))
164 } catch (const Exception& e) {
165 _E("Exception occurs. error(%d)", e.GetErrorCode());
166 return e.GetErrorCode();
172 extern "C" API int aul_comp_context_get_comp_id(aul_comp_context_h handle,
173 const char** comp_id) {
174 if (handle == nullptr || comp_id == nullptr) {
175 _E("Invalid parameter");
179 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
180 *comp_id = context->GetComponentId().c_str();
184 extern "C" API int aul_comp_context_get_instance_id(aul_comp_context_h handle,
185 const char** instance_id) {
186 if (handle == nullptr || instance_id == nullptr) {
187 _E("Invalid parameter");
191 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
192 *instance_id = context->GetInstanceId().c_str();
196 extern "C" API int aul_comp_context_get_app_id(aul_comp_context_h handle,
197 const char** app_id) {
198 if (handle == nullptr || app_id == nullptr) {
199 _E("Invalid parameter");
203 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
204 *app_id = context->GetAppId().c_str();
208 extern "C" API int aul_comp_context_get_type(aul_comp_context_h handle,
210 if (handle == nullptr || type == nullptr) {
211 _E("Invalid parameter");
215 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
216 *type = context->GetType().c_str();
220 extern "C" API int aul_comp_context_get_pid(aul_comp_context_h handle,
222 if (handle == nullptr || pid == nullptr) {
223 _E("Invalid parameter");
227 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
228 *pid = context->GetPid();
232 extern "C" API int aul_comp_context_get_status(aul_comp_context_h handle,
234 if (handle == nullptr || status == nullptr) {
235 _E("Invalid parameter");
239 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
240 *status = context->GetStatus();
244 extern "C" API int aul_comp_context_is_sub_comp(aul_comp_context_h handle,
246 if (handle == nullptr || is_sub_comp == nullptr) {
247 _E("Invalid parameter");
251 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
252 *is_sub_comp = context->IsSubComponent();
256 extern "C" API int aul_comp_context_create(const char* comp_id,
257 aul_comp_context_h* handle) {
258 return aul_comp_context_usr_create(comp_id, getuid(), handle);
261 extern "C" API int aul_comp_context_usr_create(const char* comp_id,
262 uid_t uid, aul_comp_context_h* handle) {
263 if (comp_id == nullptr || handle == nullptr) {
264 _E("Invalid parameter");
269 auto* context = GetComponentRunningContext(comp_id, uid);
270 if (context == nullptr)
273 *handle = reinterpret_cast<aul_comp_context_h>(context);
274 } catch (const Exception& e) {
275 _E("Exception occurs. error(%d)", e.GetErrorCode());
276 return e.GetErrorCode();
282 extern "C" API int aul_comp_context_destroy(aul_comp_context_h handle) {
283 if (handle == nullptr) {
284 _E("Invalid parameter");
288 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
293 extern "C" API int aul_comp_context_clone(aul_comp_context_h handle,
294 aul_comp_context_h* clone) {
295 if (handle == nullptr || clone == nullptr) {
296 _E("Invalid parameter");
300 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
301 auto* cloned_context = new (std::nothrow) ComponentRunningContext(*context);
302 if (cloned_context == nullptr) {
307 *clone = reinterpret_cast<aul_comp_context_h>(cloned_context);
311 extern "C" API int aul_comp_context_is_running(aul_comp_context_h handle,
313 if (handle == nullptr || running == nullptr) {
314 _E("Invalid parameter");
319 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
320 int ret = SendRequest(context, COMP_CONTEXT_IS_RUNNING);
321 *running = (ret == 0) ? false : true;
322 } catch (const Exception& e) {
323 _E("Exception occurs. error(%d)", e.GetErrorCode());
324 return e.GetErrorCode();
330 extern "C" int aul_comp_context_resume(aul_comp_context_h handle) {
331 if (handle == nullptr) {
332 _E("Invalid parameter");
337 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
338 SendRequest(context, COMP_CONTEXT_RESUME);
339 } catch (const Exception& e) {
340 _E("Exception occurs. error(%d)", e.GetErrorCode());
341 return e.GetErrorCode();
347 extern "C" int aul_comp_context_pause(aul_comp_context_h handle) {
348 if (handle == nullptr) {
349 _E("Invalid parameter");
354 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
355 SendRequest(context, COMP_CONTEXT_PAUSE);
356 } catch (const Exception& e) {
357 _E("Exception occurs. error(%d)", e.GetErrorCode());
358 return e.GetErrorCode();
364 extern "C" int aul_comp_context_terminate_bg_comp(aul_comp_context_h handle) {
365 if (handle == nullptr) {
366 _E("Invalid parameter");
371 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
372 SendRequest(context, COMP_CONTEXT_TERMINATE_BG_COMP);
373 } catch (const Exception& e) {
374 _E("Exception occurs. error(%d)", e.GetErrorCode());
375 return e.GetErrorCode();
381 extern "C" int aul_comp_context_terminate(aul_comp_context_h handle) {
382 if (handle == nullptr) {
383 _E("Invalid parameter");
388 auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
389 SendRequest(context, COMP_CONTEXT_TERMINATE);
390 } catch (const Exception& e) {
391 _E("Exception occurs. error(%d)", e.GetErrorCode());
392 return e.GetErrorCode();