2 * Copyright (c) 2019 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.
25 #include <aul_comp_status.h>
27 #include "rua_private.h"
29 #include "rua_context.h"
30 #include "rua_manager.h"
32 struct rua_context_s {
38 static void __destroy_rua_context(struct rua_context_s *context)
40 free(context->component_id);
41 free(context->instance_id);
42 free(context->app_id);
46 static struct rua_context_s *__create_rua_context(char *app_id,
47 char *instance_id, char *component_id)
49 struct rua_context_s *context;
51 context = calloc(1, sizeof(struct rua_context_s));
57 context->app_id = app_id;
58 context->instance_id = instance_id;
59 context->component_id = component_id;
64 int rua_context_create_from_rua_info(rua_info_h info,
65 rua_context_h *context)
67 struct rua_context_s *handle;
73 if (!info || !context) {
74 _E("Invalid parameter");
75 return RUA_ERROR_INVALID_PARAMETER;
78 ret = rua_info_get_app_id(info, &app_id);
79 if (ret != RUA_ERROR_NONE)
82 ret = rua_info_get_instance_id(info, &instance_id);
83 if (ret != RUA_ERROR_NONE) {
88 ret = rua_info_get_component_id(info, &component_id);
89 if (ret != RUA_ERROR_NONE) {
95 handle = __create_rua_context(app_id, instance_id, component_id);
100 return RUA_ERROR_OUT_OF_MEMORY;
105 return RUA_ERROR_NONE;
108 API int rua_context_destroy(rua_context_h context)
111 _E("Invalid parameter");
112 return RUA_ERROR_INVALID_PARAMETER;
115 __destroy_rua_context(context);
117 return RUA_ERROR_NONE;
120 API int rua_context_get_app_id(rua_context_h context, char **app_id)
122 if (!context || !app_id) {
123 _E("Invalid parameter");
124 return RUA_ERROR_INVALID_PARAMETER;
127 *app_id = strdup(context->app_id);
128 if (*app_id == NULL) {
130 return RUA_ERROR_OUT_OF_MEMORY;
133 return RUA_ERROR_NONE;
136 API int rua_context_get_instance_id(rua_context_h context, char **instance_id)
138 if (!context || !instance_id) {
139 _E("Invalid parameter");
140 return RUA_ERROR_INVALID_PARAMETER;
143 if (context->instance_id) {
144 *instance_id = strdup(context->instance_id);
145 if (*instance_id == NULL) {
147 return RUA_ERROR_OUT_OF_MEMORY;
153 return RUA_ERROR_NONE;
156 API int rua_context_get_component_id(rua_context_h context, char **component_id)
158 if (!context || !component_id) {
159 _E("Invalid parameter");
160 return RUA_ERROR_INVALID_PARAMETER;
163 if (context->component_id) {
164 *component_id = strdup(context->component_id);
165 if (*component_id == NULL) {
167 return RUA_ERROR_OUT_OF_MEMORY;
170 *component_id = NULL;
173 return RUA_ERROR_NONE;
176 static int __convert_aul_error(int error)
180 return RUA_ERROR_INVALID_PARAMETER;
182 return RUA_ERROR_PERMISSION_DENIED;
184 return RUA_ERROR_NO_SUCH_APP;
186 return RUA_ERROR_IO_ERROR;
190 int rua_context_is_running(rua_context_h context, bool *running)
194 if (!context || !running) {
195 _E("Invalid parameter");
196 return RUA_ERROR_INVALID_PARAMETER;
199 if (context->component_id) {
200 ret = aul_comp_is_running(context->instance_id, running);
201 } else if (context->instance_id) {
202 ret = aul_app_is_running_with_instance_id(context->app_id,
203 context->instance_id, running);
205 ret = aul_app_is_running(context->app_id);
207 *running = (bool)ret;
211 _E("Failed to check running state");
212 return __convert_aul_error(ret);
215 return RUA_ERROR_NONE;
218 int rua_context_resume(rua_context_h context)
223 _E("Invalid parameter");
224 return RUA_ERROR_INVALID_PARAMETER;
227 if (context->component_id) {
228 ret = aul_comp_resume(context->instance_id);
229 } else if (context->instance_id) {
230 ret = aul_resume_app_by_instance_id(context->app_id,
231 context->instance_id);
233 ret = aul_resume_app(context->app_id);
237 _E("Failed to send the resume request. error(%d)", ret);
238 return __convert_aul_error(ret);
241 return RUA_ERROR_NONE;
244 int rua_context_terminate(rua_context_h context)
249 _E("Invalid parameter");
250 return RUA_ERROR_INVALID_PARAMETER;
253 if (context->component_id) {
254 ret = aul_comp_terminate(context->instance_id);
255 } else if (context->instance_id) {
256 ret = aul_terminate_app_with_instance_id(context->app_id,
257 context->instance_id);
259 ret = aul_terminate_app(context->app_id);
263 _E("Failed to send the terminate request. error(%d)", ret);
264 return __convert_aul_error(ret);
267 return RUA_ERROR_NONE;