Release version 0.5.14
[platform/core/appfw/librua.git] / src / rua_manager.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 <app_control_internal.h>
26
27 #include "rua.h"
28 #include "rua_private.h"
29 #include "rua_manager.h"
30 #include "rua_context_internal.h"
31 #include "rua_info_internal.h"
32
33 static int __convert_aul_error(int error)
34 {
35         switch (error) {
36         case AUL_R_EINVAL:
37                 return RUA_ERROR_INVALID_PARAMETER;
38         case AUL_R_EILLACC:
39                 return RUA_ERROR_PERMISSION_DENIED;
40         case AUL_R_ERROR:
41                 return RUA_ERROR_OUT_OF_MEMORY;
42         default:
43                 return RUA_ERROR_IO_ERROR;
44         }
45 }
46
47 API int rua_manager_foreach_rua_info(rua_manager_rua_info_cb callback,
48                 void *user_data)
49 {
50         return rua_info_foreach(callback, user_data);
51 }
52
53 API int rua_manager_delete_rua_info(rua_info_h info)
54 {
55         char *app_id;
56         char *instance_id;
57         int ret;
58
59         if (!info) {
60                 _E("Invalid parameter");
61                 return RUA_ERROR_INVALID_PARAMETER;
62         }
63
64         ret = rua_info_get_app_id(info, &app_id);
65         if (ret != RUA_ERROR_NONE)
66                 return ret;
67
68         ret = rua_info_get_instance_id(info, &instance_id);
69         if (ret != RUA_ERROR_NONE) {
70                 free(app_id);
71                 return ret;
72         }
73
74         if (instance_id) {
75                 ret = rua_delete_history_with_instance_id(app_id,
76                                 instance_id);
77         } else {
78                 ret = rua_delete_history_with_pkgname(app_id);
79         }
80
81         free(instance_id);
82         free(app_id);
83
84         if (ret < 0) {
85                 _E("Failed to delete RUA history. error(%d)", ret);
86                 return __convert_aul_error(ret);
87         }
88
89         return RUA_ERROR_NONE;
90 }
91
92 API int rua_manager_delete_all_rua_info(void)
93 {
94         int ret;
95
96         ret = rua_clear_history();
97         if (ret < 0) {
98                 _E("Failed to clear RUA history. error(%d)", ret);
99                 return __convert_aul_error(ret);
100         }
101
102         return RUA_ERROR_NONE;
103 }
104
105 API int rua_manager_get_app_control_from_rua_info(rua_info_h info,
106                 app_control_h *app_control)
107 {
108         app_control_h handle;
109         char *args;
110         bundle *b;
111         int ret;
112
113         if (!info || !app_control) {
114                 _E("Invalid parameter");
115                 return RUA_ERROR_INVALID_PARAMETER;
116         }
117
118         ret = rua_info_get_args(info, &args);
119         if (ret != RUA_ERROR_NONE)
120                 return ret;
121
122         b = bundle_decode((bundle_raw *)args, strlen(args));
123         free(args);
124         if (!b) {
125                 _E("Failed to decode bundle");
126                 return RUA_ERROR_OUT_OF_MEMORY;
127         }
128
129         ret = app_control_create_request(b, &handle);
130         bundle_free(b);
131         if (ret != APP_CONTROL_ERROR_NONE) {
132                 _E("Failed to create app_control handle");
133                 return RUA_ERROR_OUT_OF_MEMORY;
134         }
135
136         *app_control = handle;
137
138         return RUA_ERROR_NONE;
139 }
140
141 API int rua_manager_get_rua_context_from_rua_info(rua_info_h info,
142                 rua_context_h *context)
143 {
144         return rua_context_create_from_rua_info(info, context);
145 }
146
147 API int rua_manager_is_running(rua_context_h context, bool *running)
148 {
149         return rua_context_is_running(context, running);
150 }
151
152 API int rua_manager_resume(rua_context_h context)
153 {
154         return rua_context_resume(context);
155 }
156
157 API int rua_manager_terminate(rua_context_h context)
158 {
159         return rua_context_terminate(context);
160 }