The comp context API is implemented using C++ language.
Change-Id: I79c504e50d850c9ce90596a2d6a7bde849312cec
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
--- /dev/null
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "aul/component/component_running_context.hh"
+
+#include "include/aul.h"
+
+#include "aul/common/exception.hh"
+
+namespace aul {
+
+ComponentRunningContext::Builder&
+ComponentRunningContext::Builder::SetComponentId(const tizen_base::Bundle& b) {
+ component_id_ = std::move(b.GetString(AUL_K_COMPONENT_ID));
+ return *this;
+}
+
+ComponentRunningContext::Builder&
+ComponentRunningContext::Builder::SetInstanceId(const tizen_base::Bundle& b) {
+ instance_id_ = std::move(b.GetString(AUL_K_INSTANCE_ID));
+ return *this;
+}
+
+ComponentRunningContext::Builder& ComponentRunningContext::Builder::SetAppId(
+ const tizen_base::Bundle& b) {
+ app_id_ = std::move(b.GetString(AUL_K_APPID));
+ return *this;
+}
+
+ComponentRunningContext::Builder& ComponentRunningContext::Builder::SetType(
+ const tizen_base::Bundle& b) {
+ type_ = std::move(b.GetString(AUL_K_COMPONENT_TYPE));
+ return *this;
+}
+
+ComponentRunningContext::Builder& ComponentRunningContext::Builder::SetPid(
+ const tizen_base::Bundle& b) {
+ auto value = b.GetString(AUL_K_PID);
+ if (value.empty())
+ return *this;
+
+ pid_ = std::stoi(value);
+ return *this;
+}
+
+ComponentRunningContext::Builder& ComponentRunningContext::Builder::SetStatus(
+ const tizen_base::Bundle& b) {
+ auto value = b.GetString(AUL_K_STATUS);
+ if (value.empty())
+ return *this;
+
+ status_ = std::stoi(value);
+ return *this;
+}
+
+ComponentRunningContext::Builder&
+ComponentRunningContext::Builder::SetSubComponent(const tizen_base::Bundle& b) {
+ auto value = b.GetString(AUL_K_IS_SUB_COMP);
+ if (value.empty())
+ return *this;
+
+ is_sub_component_ = std::stoi(value) ? true : false;
+ return *this;
+}
+
+ComponentRunningContext::Builder::operator ComponentRunningContext*() {
+ Validate();
+ return new (std::nothrow) ComponentRunningContext(std::move(component_id_),
+ std::move(instance_id_), std::move(app_id_), std::move(type_),
+ pid_, status_, is_sub_component_);
+}
+
+void ComponentRunningContext::Builder::Validate() {
+ if (component_id_.empty() ||
+ instance_id_.empty() ||
+ app_id_.empty() ||
+ type_.empty() ||
+ pid_ < 0 ||
+ status_ < 0 )
+ THROW(AUL_R_ERROR);
+}
+
+ComponentRunningContext::ComponentRunningContext(std::string component_id,
+ std::string instance_id,
+ std::string app_id,
+ std::string type,
+ pid_t pid,
+ int status,
+ bool is_sub_component)
+ : component_id_(std::move(component_id)),
+ instance_id_(std::move(instance_id)),
+ app_id_(std::move(app_id)),
+ type_(std::move(type)),
+ pid_(pid),
+ status_(status),
+ is_sub_component_(is_sub_component) {
+}
+
+const std::string& ComponentRunningContext::GetComponentId() const {
+ return component_id_;
+}
+
+const std::string& ComponentRunningContext::GetInstanceId() const {
+ return instance_id_;
+}
+
+const std::string& ComponentRunningContext::GetAppId() const {
+ return app_id_;
+}
+
+const std::string& ComponentRunningContext::GetType() const {
+ return type_;
+}
+
+pid_t ComponentRunningContext::GetPid() const {
+ return pid_;
+}
+
+int ComponentRunningContext::GetStatus() const {
+ return status_;
+}
+
+bool ComponentRunningContext::IsSubComponent() const {
+ return is_sub_component_;
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AUL_COMPONENT_COMPONENT_RUNNING_CONTEXT_HH_
+#define AUL_COMPONENT_COMPONENT_RUNNING_CONTEXT_HH_
+
+#include <unistd.h>
+
+#include <bundle_cpp.h>
+
+#include <string>
+
+namespace aul {
+
+class ComponentRunningContext {
+ public:
+ class Builder {
+ public:
+ Builder& SetComponentId(const tizen_base::Bundle& b);
+ Builder& SetInstanceId(const tizen_base::Bundle& b);
+ Builder& SetAppId(const tizen_base::Bundle& b);
+ Builder& SetType(const tizen_base::Bundle& b);
+ Builder& SetPid(const tizen_base::Bundle& b);
+ Builder& SetStatus(const tizen_base::Bundle& b);
+ Builder& SetSubComponent(const tizen_base::Bundle& b);
+
+ operator ComponentRunningContext*();
+
+ private:
+ void Validate();
+
+ private:
+ std::string component_id_;
+ std::string instance_id_;
+ std::string app_id_;
+ std::string type_;
+ pid_t pid_ = -1;
+ int status_ = -1;
+ bool is_sub_component_ = false;
+ };
+
+ ComponentRunningContext(std::string component_id,
+ std::string instance_id,
+ std::string app_id,
+ std::string type,
+ pid_t pid,
+ int status,
+ bool is_sub_component);
+
+ const std::string& GetComponentId() const;
+ const std::string& GetInstanceId() const;
+ const std::string& GetAppId() const;
+ const std::string& GetType() const;
+ pid_t GetPid() const;
+ int GetStatus() const;
+ bool IsSubComponent() const;
+
+ private:
+ std::string component_id_;
+ std::string instance_id_;
+ std::string app_id_;
+ std::string type_;
+ pid_t pid_;
+ int status_;
+ bool is_sub_component_;
+};
+
+} // namespace aul
+
+#endif // AUL_COMPONENT_COMPONENT_RUNNING_CONTEXT_HH_
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
-#include <glib.h>
-#include <bundle.h>
-#include <bundle_internal.h>
-
-#include "aul_comp_context.h"
-#include "aul_error.h"
-#include "aul_util.h"
-#include "aul_sock.h"
-#include "aul_api.h"
-#include "aul.h"
-
-struct aul_comp_context_s {
- char *comp_id;
- char *instance_id;
- char *app_id;
- char *type;
- pid_t pid;
- int status;
- bool is_sub_comp;
-};
-
-static void __destroy_comp_context(gpointer data)
-{
- struct aul_comp_context_s *context;
-
- context = (struct aul_comp_context_s *)data;
- if (!context)
- return;
-
- if (context->comp_id)
- free(context->comp_id);
- if (context->instance_id)
- free(context->instance_id);
- if (context->app_id)
- free(context->app_id);
- if (context->type)
- free(context->type);
- free(context);
-}
-
-static struct aul_comp_context_s *__create_comp_context(bundle *b)
-{
- struct aul_comp_context_s *context;
- const char *val;
-
- context = calloc(1, sizeof(struct aul_comp_context_s));
- if (!context) {
- _E("Out of memory");
- return NULL;
- }
-
- val = bundle_get_val(b, AUL_K_PID);
- if (!val) {
- _E("Failed to get process ID");
- goto err;
- }
- context->pid = atoi(val);
-
- val = bundle_get_val(b, AUL_K_STATUS);
- if (!val) {
- _E("Failed to get component status");
- goto err;
- }
- context->status = atoi(val);
-
- val = bundle_get_val(b, AUL_K_IS_SUB_COMP);
- if (!val) {
- _E("Failed to get the flag for checking sub comp");
- goto err;
- }
- context->is_sub_comp = atoi(val);
-
- val = bundle_get_val(b, AUL_K_COMPONENT_ID);
- if (!val) {
- _E("Failed to get component ID");
- goto err;
- }
-
- context->comp_id = strdup(val);
- if (!context->comp_id) {
- _E("Failed to duplicate component ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_INSTANCE_ID);
- if (!val) {
- _E("Failed to get instance ID");
- goto err;
- }
-
- context->instance_id = strdup(val);
- if (!context->instance_id) {
- _E("Failed to duplicate instance ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_APPID);
- if (!val) {
- _E("Failed to get application ID");
- goto err;
- }
-
- context->app_id = strdup(val);
- if (!context->app_id) {
- _E("Failed to duplicate application ID");
- goto err;
- }
-
- val = bundle_get_val(b, AUL_K_COMPONENT_TYPE);
- if (!val) {
- _E("Failed to get component type");
- goto err;
- }
-
- context->type = strdup(val);
- if (!context->type) {
- _E("Failed to duplicate component type");
- goto err;
- }
-
- return context;
-
-err:
- if (context)
- __destroy_comp_context(context);
-
- return NULL;
-}
-
-static void __running_context_cb(app_pkt_t *pkt, void *user_data)
-{
- GList **list = (GList **)user_data;
- struct aul_comp_context_s *context;
- bundle *b = NULL;
-
- if (!pkt) {
- _E("Invalid parameter");
- return;
- }
-
- if (pkt->cmd == APP_GET_INFO_ERROR) {
- _E("Failed to get running component context");
- return;
- }
-
- if (pkt->opt & AUL_SOCK_BUNDLE)
- b = bundle_decode(pkt->data, pkt->len);
-
- if (!b)
- return;
-
- context = __create_comp_context(b);
- if (!context) {
- bundle_free(b);
- return;
- }
- bundle_free(b);
-
- *list = g_list_append(*list, context);
-}
-
-API int aul_comp_context_foreach_comp_context(aul_comp_context_cb callback,
- void *user_data)
-{
- struct aul_comp_context_s *context;
- GList *list = NULL;
- GList *iter;
- char buf[32];
- bundle *b;
- int ret;
- int fd;
-
- if (!callback) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- b = bundle_create();
- if (!b) {
- _E("Out of memory");
- return AUL_R_ENOMEM;
- }
-
- snprintf(buf, sizeof(buf), "%d", getuid());
- bundle_add(b, AUL_K_TARGET_UID, buf);
-
- fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), COMP_CONTEXT_FOREACH,
- b, AUL_SOCK_ASYNC);
- bundle_free(b);
- if (fd < 0)
- return aul_error_convert(fd);
-
- ret = aul_sock_recv_pkt_with_cb(fd, __running_context_cb, &list);
- if (ret < 0) {
- g_list_free_full(list, __destroy_comp_context);
- return aul_error_convert(ret);
- }
-
- iter = list;
- while (iter) {
- context = (struct aul_comp_context_s *)iter->data;
- iter = g_list_next(iter);
-
- if (!callback(context, user_data))
- break;
- }
-
- g_list_free_full(list, __destroy_comp_context);
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_comp_id(aul_comp_context_h context,
- const char **comp_id)
-{
- if (!context || !comp_id) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *comp_id = context->comp_id;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_instance_id(aul_comp_context_h context,
- const char **instance_id)
-{
- if (!context || !instance_id) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *instance_id = context->instance_id;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_app_id(aul_comp_context_h context,
- const char **app_id)
-{
- if (!context || !app_id) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *app_id = context->app_id;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_type(aul_comp_context_h context,
- const char **type)
-{
- if (!context || !type) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *type = context->type;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_pid(aul_comp_context_h context,
- pid_t *pid)
-{
- if (!context || !pid) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *pid = context->pid;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_get_status(aul_comp_context_h context,
- int *status)
-{
- if (!context || !status) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *status = context->status;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_is_sub_comp(aul_comp_context_h context,
- bool *is_sub_comp)
-{
- if (!context || !is_sub_comp) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- *is_sub_comp = context->is_sub_comp;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_create(const char *comp_id, aul_comp_context_h *handle)
-{
- return aul_comp_context_usr_create(comp_id, getuid(), handle);
-}
-
-API int aul_comp_context_usr_create(const char *comp_id, uid_t uid,
- aul_comp_context_h *handle)
-{
- struct aul_comp_context_s *context;
- app_pkt_t *pkt = NULL;
- char buf[32];
- bundle *b;
- int ret;
- int fd;
-
- if (!comp_id || !handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- b = bundle_create();
- if (!b) {
- _E("Out of memory");
- return AUL_R_ENOMEM;
- }
-
- snprintf(buf, sizeof(buf), "%d", uid);
- bundle_add(b, AUL_K_TARGET_UID, buf);
- bundle_add(b, AUL_K_COMPONENT_ID, comp_id);
-
- fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, COMP_CONTEXT_GET,
- b, AUL_SOCK_ASYNC);
- bundle_free(b);
- if (fd < 0) {
- _E("Failed to send request. error(%d)", fd);
- return aul_error_convert(fd);
- }
-
- ret = aul_sock_recv_reply_pkt(fd, &pkt);
- if (ret < 0) {
- _E("Failed to receive packet. error(%d)", ret);
- return aul_error_convert(ret);
- }
-
- if (pkt->cmd != APP_GET_INFO_OK) {
- _E("Failed to get component info. error(%d)", pkt->cmd);
- ret = pkt->cmd;
- free(pkt);
- return ret;
- }
-
- b = NULL;
- if (pkt->opt & AUL_SOCK_BUNDLE) {
- b = bundle_decode(pkt->data, pkt->len);
- if (!b) {
- _E("Failed to decode bundle");
- free(pkt);
- return AUL_R_ENOMEM;
- }
- }
-
- free(pkt);
-
- if (!b) {
- _E("Invalid packet");
- return AUL_R_ERROR;
- }
-
- context = __create_comp_context(b);
- if (!context) {
- bundle_free(b);
- return AUL_R_ENOMEM;
- }
- bundle_free(b);
-
- *handle = (aul_comp_context_h)context;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_destroy(aul_comp_context_h handle)
-{
- if (!handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- __destroy_comp_context(handle);
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_clone(aul_comp_context_h handle,
- aul_comp_context_h *clone)
-{
- struct aul_comp_context_s *context;
- struct aul_comp_context_s *new_context;
-
- if (!handle || !clone) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- context = (struct aul_comp_context_s *)handle;
- new_context = calloc(1, sizeof(struct aul_comp_context_s));
- if (!new_context) {
- _E("Out of memory");
- return AUL_R_ENOMEM;
- }
-
- new_context->comp_id = strdup(context->comp_id);
- if (!new_context->comp_id) {
- _E("Failed to duplicate component ID");
- goto err;
- }
-
- new_context->instance_id = strdup(context->instance_id);
- if (!new_context->instance_id) {
- _E("Failed to duplicate instance ID");
- goto err;
- }
-
- new_context->app_id = strdup(context->app_id);
- if (!new_context->app_id) {
- _E("Failed to duplicate application ID");
- goto err;
- }
-
- new_context->type = strdup(context->type);
- if (!new_context->type) {
- _E("Failed to duplicate component type");
- goto err;
- }
-
- new_context->pid = context->pid;
- new_context->status = context->status;
- new_context->is_sub_comp = context->is_sub_comp;
-
- *clone = (aul_comp_context_h)new_context;
-
- return AUL_R_OK;
-err:
- if (new_context)
- __destroy_comp_context(new_context);
-
- return AUL_R_ENOMEM;
-}
-
-static int __send_request(aul_comp_context_h handle, int cmd)
-{
- struct aul_comp_context_s *context;
- bundle *b;
- int ret;
-
- context = (struct aul_comp_context_s *)handle;
- b = bundle_create();
- if (!b) {
- _E("Out of memory");
- return AUL_R_ENOMEM;
- }
-
- bundle_add(b, AUL_K_COMPONENT_ID, context->comp_id);
- bundle_add(b, AUL_K_INSTANCE_ID, context->instance_id);
-
- ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), cmd,
- b, AUL_SOCK_NONE);
- bundle_free(b);
- if (ret < 0)
- return aul_error_convert(ret);
-
- return ret;
-}
-
-API int aul_comp_context_is_running(aul_comp_context_h handle,
- bool *running)
-{
- int ret;
-
- if (!handle || !running) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- ret = __send_request(handle, COMP_CONTEXT_IS_RUNNING);
- if (ret < 0) {
- _E("Failed to send request. error(%d)", ret);
- return ret;
- }
-
- *running = (bool)ret;
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_resume(aul_comp_context_h handle)
-{
- int ret;
-
- if (!handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- ret = __send_request(handle, COMP_CONTEXT_RESUME);
- if (ret < 0) {
- _E("Failed to send request. error(%d)", ret);
- return ret;
- }
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_pause(aul_comp_context_h handle)
-{
- int ret;
-
- if (!handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- ret = __send_request(handle, COMP_CONTEXT_PAUSE);
- if (ret < 0) {
- _E("Failed to send request. error(%d)", ret);
- return ret;
- }
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_terminate_bg_comp(aul_comp_context_h handle)
-{
- int ret;
-
- if (!handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- ret = __send_request(handle, COMP_CONTEXT_TERMINATE_BG_COMP);
- if (ret < 0) {
- _E("Failed to send request. error(%d)", ret);
- return ret;
- }
-
- return AUL_R_OK;
-}
-
-API int aul_comp_context_terminate(aul_comp_context_h handle)
-{
- int ret;
-
- if (!handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- ret = __send_request(handle, COMP_CONTEXT_TERMINATE);
- if (ret < 0) {
- _E("Failed to send request. error(%d)", ret);
- return ret;
- }
-
- return AUL_R_OK;
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/aul_comp_context.h"
+
+#include <bundle_cpp.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "app_request.h"
+#include "aul_api.h"
+#include "aul_util.h"
+#include "include/aul.h"
+#include "include/aul_error.h"
+
+#include "aul/common/exception.hh"
+#include "aul/component/component_running_context.hh"
+
+using namespace aul;
+
+struct aul_comp_context_s {
+ void* dummy;
+};
+
+namespace {
+using namespace aul::internal;
+
+ComponentRunningContext* CreateComponentRunningContext(
+ const tizen_base::Bundle& b) {
+ return ComponentRunningContext::Builder()
+ .SetComponentId(b)
+ .SetInstanceId(b)
+ .SetAppId(b)
+ .SetType(b)
+ .SetPid(b)
+ .SetStatus(b)
+ .SetSubComponent(b);
+}
+
+ComponentRunningContext* GetComponentRunningContext(
+ const std::string& component_id, uid_t uid) {
+ tizen_base::Bundle b { { AUL_K_COMPONENT_ID, component_id } };
+ int fd = AppRequest(COMP_CONTEXT_GET, uid)
+ .With(std::move(b))
+ .SendSimply(AUL_SOCK_ASYNC);
+ if (fd < 0)
+ THROW(aul_error_convert(fd));
+
+ app_pkt_t* pkt;
+ int ret = aul_sock_recv_reply_pkt(fd, &pkt);
+ if (ret < 0)
+ THROW(aul_error_convert(fd));
+
+ auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
+ pkt, std::free);
+
+ if (pkt->cmd != APP_GET_INFO_OK) {
+ _E("Failed to get component running context. error(%d)", pkt->cmd);
+ THROW(pkt->cmd);
+ }
+
+ bundle* kb = nullptr;
+ if (pkt->opt & AUL_SOCK_BUNDLE) {
+ kb = bundle_decode(pkt->data, pkt->len);
+ if (kb == nullptr)
+ THROW(AUL_R_ENOMEM);
+ } else {
+ _E("Wrong packet");
+ THROW(AUL_R_ERROR);
+ }
+
+ return CreateComponentRunningContext(tizen_base::Bundle(kb, false, true));
+}
+
+std::vector<std::unique_ptr<ComponentRunningContext>>
+GetComponentRunningContexts() {
+ int fd = AppRequest(COMP_CONTEXT_FOREACH, getuid())
+ .SendSimply(AUL_SOCK_ASYNC);
+ if (fd < 0)
+ THROW(aul_error_convert(fd));
+
+ std::vector<std::unique_ptr<ComponentRunningContext>> contexts;
+ int ret = aul_sock_recv_pkt_with_cb(fd,
+ [](app_pkt_t* pkt, void* user_data) {
+ if (pkt == nullptr) {
+ _E("pkt is nullptr");
+ return;
+ }
+
+ if (pkt->cmd == APP_GET_INFO_ERROR) {
+ _E("Failed to get component running context");
+ return;
+ }
+
+ bundle* kb = nullptr;
+ if (pkt->opt & AUL_SOCK_BUNDLE)
+ kb = bundle_decode(pkt->data, pkt->len);
+
+ if (kb == nullptr)
+ return;
+
+ tizen_base::Bundle b(kb, false, true);
+ auto* context_array =
+ static_cast<std::vector<std::unique_ptr<ComponentRunningContext>>*>(
+ user_data);
+
+ try {
+ context_array->emplace_back(CreateComponentRunningContext(b));
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%s)", e.what());
+ }
+ }, &contexts);
+ if (ret < 0)
+ THROW(aul_error_convert(ret));
+
+ return contexts;
+}
+
+int SendRequest(ComponentRunningContext* context, int cmd) {
+ tizen_base::Bundle b {
+ { AUL_K_COMPONENT_ID, context->GetComponentId() },
+ { AUL_K_INSTANCE_ID, context->GetInstanceId() }
+ };
+
+ int ret = AppRequest(cmd, getuid())
+ .With(std::move(b))
+ .SendSimply();
+ if (ret < 0)
+ THROW(aul_error_convert(ret));
+
+ return ret;
+}
+
+} // namespace
+
+extern "C" API int aul_comp_context_foreach_comp_context(
+ aul_comp_context_cb callback, void* user_data) {
+ if (callback == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ for (auto const& context : GetComponentRunningContexts()) {
+ auto* handle = reinterpret_cast<aul_comp_context_h>(context.get());
+ if (!callback(handle, user_data))
+ break;
+ }
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_comp_id(aul_comp_context_h handle,
+ const char** comp_id) {
+ if (handle == nullptr || comp_id == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *comp_id = context->GetComponentId().c_str();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_instance_id(aul_comp_context_h handle,
+ const char** instance_id) {
+ if (handle == nullptr || instance_id == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *instance_id = context->GetInstanceId().c_str();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_app_id(aul_comp_context_h handle,
+ const char** app_id) {
+ if (handle == nullptr || app_id == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *app_id = context->GetAppId().c_str();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_type(aul_comp_context_h handle,
+ const char** type) {
+ if (handle == nullptr || type == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *type = context->GetType().c_str();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_pid(aul_comp_context_h handle,
+ pid_t* pid) {
+ if (handle == nullptr || pid == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *pid = context->GetPid();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_get_status(aul_comp_context_h handle,
+ int* status) {
+ if (handle == nullptr || status == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *status = context->GetStatus();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_is_sub_comp(aul_comp_context_h handle,
+ bool* is_sub_comp) {
+ if (handle == nullptr || is_sub_comp == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ *is_sub_comp = context->IsSubComponent();
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_create(const char* comp_id,
+ aul_comp_context_h* handle) {
+ return aul_comp_context_usr_create(comp_id, getuid(), handle);
+}
+
+extern "C" API int aul_comp_context_usr_create(const char* comp_id,
+ uid_t uid, aul_comp_context_h* handle) {
+ if (comp_id == nullptr || handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = GetComponentRunningContext(comp_id, uid);
+ if (context == nullptr)
+ return AUL_R_ENOMEM;
+
+ *handle = reinterpret_cast<aul_comp_context_h>(context);
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_destroy(aul_comp_context_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ delete context;
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_clone(aul_comp_context_h handle,
+ aul_comp_context_h* clone) {
+ if (handle == nullptr || clone == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ auto* cloned_context = new (std::nothrow) ComponentRunningContext(*context);
+ if (cloned_context == nullptr) {
+ _E("Out of memory");
+ return AUL_R_ENOMEM;
+ }
+
+ *clone = reinterpret_cast<aul_comp_context_h>(cloned_context);
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_comp_context_is_running(aul_comp_context_h handle,
+ bool* running) {
+ if (handle == nullptr || running == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ int ret = SendRequest(context, COMP_CONTEXT_IS_RUNNING);
+ *running = (ret == 0) ? false : true;
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" int aul_comp_context_resume(aul_comp_context_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ SendRequest(context, COMP_CONTEXT_RESUME);
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" int aul_comp_context_pause(aul_comp_context_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ SendRequest(context, COMP_CONTEXT_PAUSE);
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" int aul_comp_context_terminate_bg_comp(aul_comp_context_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ SendRequest(context, COMP_CONTEXT_TERMINATE_BG_COMP);
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}
+
+extern "C" int aul_comp_context_terminate(aul_comp_context_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ try {
+ auto* context = reinterpret_cast<ComponentRunningContext*>(handle);
+ SendRequest(context, COMP_CONTEXT_TERMINATE);
+ } catch (const Exception& e) {
+ _E("Exception occurs. error(%d)", e.GetErrorCode());
+ return e.GetErrorCode();
+ }
+
+ return AUL_R_OK;
+}