add server/client functions
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_client_interface.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Garima Shrivastava<garima.s@samsung.com>
7  *      Jyotsna Dhumale <jyotsna.a@samsung.com>
8  *      Venkatesha Sarpangala <sarpangala.v@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <app2sd_client_interface.h>
30
31 static int app2sd_gdbus_shared_connection(GDBusConnection **connection)
32 {
33         GError *error = NULL;
34
35 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
36         g_type_init();
37 #endif
38
39         *connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
40         if (*connection == NULL) {
41                 if (error != NULL) {
42                         _E("app2sd error : failed to get "
43                                 "system dbus [%s]", error->message);
44                         g_error_free(error);
45                 }
46                 return APP2EXT_ERROR_DBUS_FAILED;
47         }
48
49         return APP2EXT_SUCCESS;
50 }
51
52 static int __app2sd_call_server_method(const gchar *method_name,
53                 GVariant *param)
54 {
55         int ret = APP2EXT_SUCCESS;
56         int result = 0;
57         GDBusConnection *conn = NULL;
58         GDBusProxy *proxy = NULL;
59         GError *error = NULL;
60         GVariant *value = NULL;
61
62         /* get gdbus connection */
63         ret = app2sd_gdbus_shared_connection(&conn);
64         if (ret) {
65                 _E("app2sd error : dbus connection error");
66                 return ret;
67         }
68
69         /* method call */
70         proxy = g_dbus_proxy_new_sync(conn,
71                 G_DBUS_PROXY_FLAGS_NONE, NULL,
72                 APP2SD_BUS_NAME, APP2SD_OBJECT_PATH, APP2SD_INTERFACE_NAME,
73                 NULL, &error);
74         if (proxy == NULL) {
75                 _E("failed to create new proxy, error(%s)", error->message);
76                 g_error_free(error);
77                 ret = APP2EXT_ERROR_DBUS_FAILED;
78                 goto out;
79         }
80
81         value = g_dbus_proxy_call_sync(proxy, method_name, param,
82                 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
83         if (error != NULL) {
84                 _E("proxy call sync error(%s)", error->message);
85                 g_error_free(error);
86                 ret = APP2EXT_ERROR_DBUS_FAILED;
87                 goto out;
88         }
89
90         g_variant_get(value, "(i)", &result);
91         g_variant_unref(value);
92
93         _D("result(%d)", result);
94         if (result)
95                 ret = result;
96
97 out:
98         if (conn)
99                 g_object_unref(conn);
100
101         return ret;
102 }
103
104 static void __app2sd_create_dir_list_builder(gpointer data, gpointer user_data)
105 {
106         app2ext_dir_details *item = (app2ext_dir_details *)data;
107         GVariantBuilder *builder = (GVariantBuilder *)user_data;
108
109         g_variant_builder_add(builder, "(si)", item->name, item->type);
110 }
111
112 int app2sd_client_usr_pre_app_install(const char *pkgid, GList* dir_list,
113                 int size, uid_t uid)
114 {
115         int ret = 0;
116         GVariantBuilder *builder = NULL;
117         GVariant *param = NULL;
118
119         /* validate the function parameter recieved */
120         if (pkgid == NULL || dir_list == NULL || size <= 0) {
121                 _E("invalid function arguments");
122                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
123         }
124
125         builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
126         g_list_foreach(dir_list, __app2sd_create_dir_list_builder, builder);
127
128         param = g_variant_new("(sia(si)i)", pkgid, size, builder, uid);
129         ret = __app2sd_call_server_method("PreAppInstall", param);
130
131         if (builder)
132                 g_variant_builder_unref(builder);
133
134         return ret;
135 }
136 int app2sd_client_pre_app_install(const char *pkgid, GList* dir_list,
137                 int size)
138 {
139         int ret = 0;
140
141         ret = app2sd_client_usr_pre_app_install(pkgid,
142                 dir_list, size, getuid());
143
144         return ret;
145 }
146
147 int app2sd_client_usr_post_app_install(const char *pkgid,
148                 app2ext_status install_status, uid_t uid)
149 {
150         int ret = 0;
151         GVariant *param = NULL;
152
153         /* validate the function parameter recieved */
154         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
155                 || install_status > APP2EXT_STATUS_SUCCESS) {
156                 _E("invalid func parameters");
157                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
158         }
159
160         param = g_variant_new("(sii)", pkgid, install_status, uid);
161         ret = __app2sd_call_server_method("PostAppInstall", param);
162
163         return ret;
164 }
165 int app2sd_client_post_app_install(const char *pkgid,
166                 app2ext_status install_status)
167 {
168         int ret = 0;
169
170         ret = app2sd_client_usr_post_app_install(pkgid,
171                 install_status, getuid());
172
173         return ret;
174 }
175
176 int app2sd_client_usr_pre_app_upgrade(const char *pkgid, GList* dir_list,
177                 int size, uid_t uid)
178 {
179         int ret = 0;
180         GVariantBuilder *builder = NULL;
181         GVariant *param = NULL;
182
183         /* validate the function parameter recieved */
184         if (pkgid == NULL || dir_list == NULL || size <= 0) {
185                 _E("invalid function arguments");
186                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
187         }
188
189         builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
190         g_list_foreach(dir_list, __app2sd_create_dir_list_builder, builder);
191
192         param = g_variant_new("(sia(si)i)", pkgid, size, builder, uid);
193         ret = __app2sd_call_server_method("PreAppUpgrade", param);
194
195         if (builder)
196                 g_variant_builder_unref(builder);
197
198         return ret;
199 }
200 int app2sd_client_pre_app_upgrade(const char *pkgid, GList* dir_list,
201                 int size)
202 {
203         int ret = 0;
204
205         ret = app2sd_client_usr_pre_app_upgrade(pkgid,
206                 dir_list, size, getuid());
207
208         return ret;
209 }
210
211 int app2sd_client_usr_post_app_upgrade(const char *pkgid,
212                 app2ext_status install_status, uid_t uid)
213 {
214         int ret = 0;
215         GVariant *param = NULL;
216
217         /* validate the function parameter recieved */
218         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
219                 || install_status > APP2EXT_STATUS_SUCCESS) {
220                 _E("invalid func parameters");
221                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
222         }
223
224         param = g_variant_new("(sii)", pkgid, install_status, uid);
225         ret = __app2sd_call_server_method("PostAppUpgrade", param);
226
227         return ret;
228 }
229 int app2sd_client_post_app_upgrade(const char *pkgid,
230                 app2ext_status install_status)
231 {
232         int ret = 0;
233
234         ret = app2sd_client_usr_post_app_upgrade(pkgid,
235                 install_status, getuid());
236
237         return ret;
238 }
239
240 int app2sd_client_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
241 {
242         int ret = 0;
243         GVariant *param = NULL;
244
245         /* validate the function parameter recieved */
246         if (pkgid == NULL) {
247                 _E("invalid func parameters");
248                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
249         }
250
251         param = g_variant_new("(si)", pkgid, uid);
252         ret = __app2sd_call_server_method("PreAppUninstall", param);
253
254         return ret;
255 }
256 int app2sd_client_pre_app_uninstall(const char *pkgid)
257 {
258         int ret = 0;
259
260         ret = app2sd_client_usr_pre_app_uninstall(pkgid,
261                 getuid());
262
263         return ret;
264 }
265
266 int app2sd_client_usr_post_app_uninstall(const char *pkgid, uid_t uid)
267 {
268         int ret = 0;
269         GVariant *param = NULL;
270
271         /* validate the function parameter recieved */
272         if (pkgid == NULL) {
273                 _E("invalid func parameters");
274                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
275         }
276
277         param = g_variant_new("(si)", pkgid, uid);
278         ret = __app2sd_call_server_method("PostAppUninstall", param);
279
280         return ret;
281 }
282 int app2sd_client_post_app_uninstall(const char *pkgid)
283 {
284         int ret = 0;
285
286         ret = app2sd_client_usr_post_app_uninstall(pkgid,
287                 getuid());
288
289         return ret;
290 }
291
292 int app2sd_client_usr_force_clean(const char *pkgid, uid_t uid)
293 {
294         int ret = 0;
295         GVariant *param = NULL;
296
297         /* validate the function parameter recieved */
298         if (pkgid == NULL) {
299                 _E("invalid func parameters");
300                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
301         }
302
303         param = g_variant_new("(si)", pkgid, uid);
304         ret = __app2sd_call_server_method("ForceClean", param);
305
306         return ret;
307 }
308 int app2sd_client_force_clean(const char *pkgid)
309 {
310         int ret = 0;
311
312         ret = app2sd_client_usr_force_clean(pkgid, getuid());
313
314         return ret;
315 }
316
317 int app2sd_client_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
318 {
319         int ret = 0;
320         GVariant *param = NULL;
321
322         /* validate the function parameter recieved */
323         if (pkgid == NULL) {
324                 _E("invalid func parameters");
325                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
326         }
327
328         param = g_variant_new("(si)", pkgid, uid);
329         ret = __app2sd_call_server_method("OndemandSetupInit", param);
330
331         return ret;
332 }
333 int app2sd_client_on_demand_setup_init(const char *pkgid)
334 {
335         int ret = 0;
336
337         ret = app2sd_client_usr_on_demand_setup_init(pkgid,
338                 getuid());
339
340         return ret;
341 }
342
343 int app2sd_client_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
344 {
345         int ret = 0;
346         GVariant *param = NULL;
347
348         /* validate the function parameter recieved */
349         if (pkgid == NULL) {
350                 _E("invalid func parameters");
351                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
352         }
353
354         param = g_variant_new("(si)", pkgid, uid);
355         ret = __app2sd_call_server_method("OndemandSetupExit", param);
356
357         return ret;
358 }
359 int app2sd_client_on_demand_setup_exit(const char *pkgid)
360 {
361         int ret = 0;
362
363         ret = app2sd_client_usr_on_demand_setup_exit(pkgid,
364                 getuid());
365
366         return ret;
367 }
368
369 int app2sd_client_usr_move_installed_app(const char *pkgid, GList* dir_list,
370                 app2ext_move_type move_type, uid_t uid)
371 {
372         int ret = 0;
373         GVariantBuilder *builder = NULL;
374         GVariant *param = NULL;
375
376         /* validate the function parameter recieved */
377         if (pkgid == NULL || dir_list == NULL
378                 || move_type < APP2EXT_MOVE_TO_EXT
379                 || move_type > APP2EXT_MOVE_TO_PHONE) {
380                 _E("invalid function arguments");
381                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
382         }
383
384         builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
385         g_list_foreach(dir_list, __app2sd_create_dir_list_builder, builder);
386
387         param = g_variant_new("(sia(si)i)", pkgid, move_type, builder, uid);
388         ret = __app2sd_call_server_method("MoveInstalledApp", param);
389
390         if (builder)
391                 g_variant_builder_unref(builder);
392
393         return ret;
394 }
395 int app2sd_client_move_installed_app(const char *pkgid, GList* dir_list,
396                 app2ext_move_type move_type)
397 {
398         int ret = 0;
399
400         ret = app2sd_client_usr_move_installed_app(pkgid,
401                 dir_list, move_type, getuid());
402
403         return ret;
404 }
405
406 void app2ext_on_load(app2ext_interface *interface)
407 {
408         /* Plug-in Binding.*/
409         interface->client_pre_install = app2sd_client_pre_app_install;
410         interface->client_post_install = app2sd_client_post_app_install;
411         interface->client_pre_upgrade = app2sd_client_pre_app_upgrade;
412         interface->client_post_upgrade = app2sd_client_post_app_upgrade;
413         interface->client_pre_uninstall = app2sd_client_pre_app_uninstall;
414         interface->client_post_uninstall = app2sd_client_post_app_uninstall;
415         interface->client_force_clean = app2sd_client_force_clean;
416         interface->client_enable = app2sd_client_on_demand_setup_init;
417         interface->client_disable = app2sd_client_on_demand_setup_exit;
418         interface->client_move = app2sd_client_move_installed_app;
419
420         interface->client_usr_pre_install = app2sd_client_usr_pre_app_install;
421         interface->client_usr_post_install = app2sd_client_usr_post_app_install;
422         interface->client_usr_pre_upgrade = app2sd_client_usr_pre_app_upgrade;
423         interface->client_usr_post_upgrade = app2sd_client_usr_post_app_upgrade;
424         interface->client_usr_pre_uninstall = app2sd_client_usr_pre_app_uninstall;
425         interface->client_usr_post_uninstall = app2sd_client_usr_post_app_uninstall;
426         interface->client_usr_force_clean = app2sd_client_usr_force_clean;
427         interface->client_usr_enable = app2sd_client_usr_on_demand_setup_init;
428         interface->client_usr_disable = app2sd_client_usr_on_demand_setup_exit;
429         interface->client_usr_move = app2sd_client_usr_move_installed_app;
430 }