Fix lcov option for lcov 2.0
[platform/core/appfw/librua.git] / src / rua_context.c
1 /*
2  * Copyright (c) 2019 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 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <glib.h>
24 #include <aul.h>
25 #include <aul_comp_status.h>
26
27 #include "rua_private.h"
28 #include "rua_info.h"
29 #include "rua_context.h"
30 #include "rua_manager.h"
31
32 struct rua_context_s {
33         char *app_id;
34         char *instance_id;
35         char *component_id;
36 };
37
38 static void __destroy_rua_context(struct rua_context_s *context)
39 {
40         free(context->component_id);
41         free(context->instance_id);
42         free(context->app_id);
43         free(context);
44 }
45
46 static struct rua_context_s *__create_rua_context(char *app_id,
47                 char *instance_id, char *component_id)
48 {
49         struct rua_context_s *context;
50
51         context = calloc(1, sizeof(struct rua_context_s));
52         if (!context) {
53                 _E("Out of memory");
54                 return NULL;
55         }
56
57         context->app_id = app_id;
58         context->instance_id = instance_id;
59         context->component_id = component_id;
60
61         return context;
62 }
63
64 int rua_context_create_from_rua_info(rua_info_h info,
65                 rua_context_h *context)
66 {
67         struct rua_context_s *handle;
68         char *app_id;
69         char *instance_id;
70         char *component_id;
71         int ret;
72
73         if (!info || !context) {
74                 _E("Invalid parameter");
75                 return RUA_ERROR_INVALID_PARAMETER;
76         }
77
78         ret = rua_info_get_app_id(info, &app_id);
79         if (ret != RUA_ERROR_NONE)
80                 return ret;
81
82         ret = rua_info_get_instance_id(info, &instance_id);
83         if (ret != RUA_ERROR_NONE) {
84                 free(app_id);
85                 return ret;
86         }
87
88         ret = rua_info_get_component_id(info, &component_id);
89         if (ret != RUA_ERROR_NONE) {
90                 free(instance_id);
91                 free(app_id);
92                 return ret;
93         }
94
95         handle = __create_rua_context(app_id, instance_id, component_id);
96         if (!handle) {
97                 free(component_id);
98                 free(instance_id);
99                 free(app_id);
100                 return RUA_ERROR_OUT_OF_MEMORY;
101         }
102
103         *context = handle;
104
105         return RUA_ERROR_NONE;
106 }
107
108 API int rua_context_destroy(rua_context_h context)
109 {
110         if (!context) {
111                 _E("Invalid parameter");
112                 return RUA_ERROR_INVALID_PARAMETER;
113         }
114
115         __destroy_rua_context(context);
116
117         return RUA_ERROR_NONE;
118 }
119
120 API int rua_context_get_app_id(rua_context_h context, char **app_id)
121 {
122         if (!context || !app_id) {
123                 _E("Invalid parameter");
124                 return RUA_ERROR_INVALID_PARAMETER;
125         }
126
127         *app_id = strdup(context->app_id);
128         if (*app_id == NULL) {
129                 _E("Out of memory");
130                 return RUA_ERROR_OUT_OF_MEMORY;
131         }
132
133         return RUA_ERROR_NONE;
134 }
135
136 API int rua_context_get_instance_id(rua_context_h context, char **instance_id)
137 {
138         if (!context || !instance_id) {
139                 _E("Invalid parameter");
140                 return RUA_ERROR_INVALID_PARAMETER;
141         }
142
143         if (context->instance_id) {
144                 *instance_id = strdup(context->instance_id);
145                 if (*instance_id == NULL) {
146                         _E("Out of memory");
147                         return RUA_ERROR_OUT_OF_MEMORY;
148                 }
149         } else {
150                 *instance_id = NULL;
151         }
152
153         return RUA_ERROR_NONE;
154 }
155
156 API int rua_context_get_component_id(rua_context_h context, char **component_id)
157 {
158         if (!context || !component_id) {
159                 _E("Invalid parameter");
160                 return RUA_ERROR_INVALID_PARAMETER;
161         }
162
163         if (context->component_id) {
164                 *component_id = strdup(context->component_id);
165                 if (*component_id == NULL) {
166                         _E("Out of memory");
167                         return RUA_ERROR_OUT_OF_MEMORY;
168                 }
169         } else {
170                 *component_id = NULL;
171         }
172
173         return RUA_ERROR_NONE;
174 }
175
176 static int __convert_aul_error(int error)
177 {
178         switch (error) {
179         case AUL_R_EINVAL:
180                 return RUA_ERROR_INVALID_PARAMETER;
181         case AUL_R_EILLACC:
182                 return RUA_ERROR_PERMISSION_DENIED;
183         case AUL_R_ENOAPP:
184                 return RUA_ERROR_NO_SUCH_APP;
185         default:
186                 return RUA_ERROR_IO_ERROR;
187         }
188 }
189
190 int rua_context_is_running(rua_context_h context, bool *running)
191 {
192         int ret;
193
194         if (!context || !running) {
195                 _E("Invalid parameter");
196                 return RUA_ERROR_INVALID_PARAMETER;
197         }
198
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);
204         } else {
205                 ret = aul_app_is_running(context->app_id);
206                 if (ret >= 0)
207                         *running = (bool)ret;
208         }
209
210         if (ret < 0) {
211                 _E("Failed to check running state");
212                 return __convert_aul_error(ret);
213         }
214
215         return RUA_ERROR_NONE;
216 }
217
218 int rua_context_resume(rua_context_h context)
219 {
220         int ret;
221
222         if (!context) {
223                 _E("Invalid parameter");
224                 return RUA_ERROR_INVALID_PARAMETER;
225         }
226
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);
232         } else {
233                 ret = aul_resume_app(context->app_id);
234         }
235
236         if (ret < 0) {
237                 _E("Failed to send the resume request. error(%d)", ret);
238                 return __convert_aul_error(ret);
239         }
240
241         return RUA_ERROR_NONE;
242 }
243
244 int rua_context_terminate(rua_context_h context)
245 {
246         int ret;
247
248         if (!context) {
249                 _E("Invalid parameter");
250                 return RUA_ERROR_INVALID_PARAMETER;
251         }
252
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);
258         } else {
259                 ret = aul_terminate_app(context->app_id);
260         }
261
262         if (ret < 0) {
263                 _E("Failed to send the terminate request. error(%d)", ret);
264                 return __convert_aul_error(ret);
265         }
266
267         return RUA_ERROR_NONE;
268 }