Check invalid parameter
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 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
18 #include <stdlib.h>
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <aul.h>
23 #include <dlog.h>
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <stdlib.h>
27 #include <app_control.h>
28 #include <app_control_internal.h>
29 #include <Elementary.h>
30 #include <widget_errno.h>
31 #include <widget_instance.h>
32 #include <widget_service_internal.h>
33 #include <aul_app_com.h>
34 #include <Ecore_Wayland.h>
35
36 #include "widget_app.h"
37 #include "widget-log.h"
38 #include "widget-private.h"
39 #include "widget_app_internal.h"
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44
45 #define STR_MAX_BUF 128
46 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
47 #define K_REASON    "__WC_K_REASON__"
48
49 typedef enum _widget_obj_state_e {
50         WC_READY = 0,
51         WC_RUNNING = 1,
52         WC_PAUSED = 2,
53         WC_TERMINATED = 3
54 } widget_obj_state_e;
55
56 struct app_event_handler {
57         app_event_type_e type;
58         app_event_cb cb;
59         void *data;
60 };
61
62 struct app_event_info {
63         app_event_type_e type;
64         void *value;
65 };
66
67 struct _widget_class {
68         void *user_data;
69         widget_instance_lifecycle_callback_s ops;
70         char *classid;
71         struct _widget_class *next;
72         struct _widget_class *prev;
73 };
74
75 struct _widget_context {
76         char *id;
77         struct _widget_class *provider;
78         int state;
79         void *tag;
80         Evas_Object *win;
81         bundle *content;
82         widget_instance_lifecycle_callback_s ops;
83 };
84
85 typedef struct _widget_class widget_class_s;
86 typedef struct _widget_context widget_context_s;
87
88 static int caller_pid = 0;
89 static widget_app_lifecycle_callback_s *app_ops;
90 static void *app_user_data = NULL;
91 static char *appid = NULL;
92 static widget_class_h class_provider = NULL;
93 static GList *contexts = NULL;
94 static char *viewer_endpoint = NULL;
95
96 static gint __comp_by_id(gconstpointer a, gconstpointer b)
97 {
98         widget_context_s *wc = (widget_context_s*)a;
99
100         return strcmp(wc->id, (const char*)b);
101 }
102
103 static widget_context_s* __find_context_by_id(const char *id)
104 {
105         GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
106
107         if (ret == NULL)
108                 return NULL;
109
110         return ret->data;
111 }
112
113 static int __send_update_status(const char *class_id, const char *instance_id,
114         int status, bundle *extra, int internal_only)
115 {
116         bundle *b = extra;
117
118         if (b == NULL)
119                 b = bundle_create();
120
121         bundle_add_str(b, WIDGET_K_ID, class_id);
122         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
123         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
124
125         aul_app_com_send(viewer_endpoint, b);
126
127         if (extra == NULL)
128                 bundle_free(b);
129
130         return 0;
131 }
132
133 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
134 {
135         widget_context_s *wc = NULL;
136         int w = 0, h = 0;
137         char *w_str = NULL, *h_str = NULL;
138         char *remain = NULL;
139         int ret = 0;
140
141         wc = (widget_context_s *)malloc(sizeof(widget_context_s));
142         if (!wc)
143                 return WIDGET_ERROR_OUT_OF_MEMORY;
144
145         wc->state = WC_READY;
146         wc->id = g_strdup(id);
147         wc->provider = handle;
148         wc->win = NULL;
149
150         wc->content = bundle_dup(b);
151         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
152         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
153
154         if (w_str) {
155                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
156         }
157
158         if (h_str) {
159                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
160         }
161
162         contexts = g_list_append(contexts, wc);
163
164         handle->ops.create(wc, b, w, h, handle->user_data);
165
166         ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
167
168         return ret;
169 }
170
171 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
172 {
173         widget_context_s *wc = __find_context_by_id(id);
174         int ret = 0;
175
176         if (wc) {
177                 wc->state = WC_TERMINATED;
178                 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
179
180                 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
181
182                 contexts = g_list_remove(contexts, wc);
183
184                 if (wc->id)
185                         free(wc->id);
186                 free(wc);
187         } else {
188                 _E("could not find widget obj: %s", id);
189                 ret = WIDGET_ERROR_INVALID_PARAMETER;
190         }
191
192         return ret;
193 }
194
195 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
196 {
197         if (!class_id || !handle)
198                 return NULL;
199
200         widget_class_h head = handle;
201
202         while (head) {
203                 if (head->classid && strcmp(head->classid, class_id) == 0)
204                         return head;
205
206                 head = head->next;
207         }
208
209         return NULL;
210 }
211
212 static void __control(bundle *b)
213 {
214         char *class_id = NULL;
215         char *id = NULL;
216         char *operation = NULL;
217         char *reason = NULL;
218         char *remain = NULL;
219         int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
220
221         widget_class_h handle = NULL;
222         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
223         /* for previous version compatibility, use appid for default class id */
224         if (class_id == NULL)
225                 class_id = appid;
226
227         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
228         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
229
230         handle = __find_class_handler(class_id, class_provider);
231         if (!handle) {
232                 _E("no handle provided: %s", class_id);
233                 goto error;
234         }
235
236         if (!operation) {
237                 _E("no operation provided");
238                 goto error;
239         }
240
241         if (strcmp(operation, "create") == 0) {
242                 __instance_create(handle, id, b);
243         } else if (strcmp(operation, "resize") == 0) {
244                 /* TODO */
245         } else if (strcmp(operation, "update") == 0) {
246                 /* TODO */
247         } else if (strcmp(operation, "destroy") == 0) {
248                 bundle_get_str(b, WIDGET_K_REASON, &reason);
249                 if (reason)
250                         destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
251
252                 __instance_destroy(handle, id, destroy_type, b);
253         } else if (strcmp(operation, "resume") == 0) {
254                 /* TODO */
255         } else if (strcmp(operation, "pause") == 0) {
256                 /* TODO */
257         }
258
259         return;
260 error:
261         LOGD("error on control");
262         return;
263 }
264
265 static void __show_all()
266 {
267         LOGD("resume");
268 }
269
270 static int __aul_handler(aul_type type, bundle *b, void *data)
271 {
272         char *caller = NULL;
273         char *remain = NULL;
274
275         switch (type) {
276         case AUL_START:
277                 if (b) {
278                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
279                         if (caller)
280                                 caller_pid = g_ascii_strtoll(caller, &remain, 10);
281                         else {
282                                 /* using caller appid and query pid using caller appid? */
283                                 _E("no caller pid");
284                         }
285                 }
286
287                 __control(b);
288                 break;
289         case AUL_RESUME:
290                 __show_all();
291                 break;
292         case AUL_TERMINATE:
293                 widget_app_exit();
294                 break;
295         default:
296                 break;
297         }
298
299         return 0;
300 }
301
302
303 static int __before_loop(int argc, char **argv)
304 {
305         int r;
306         bundle *kb = NULL;
307         char *wayland_display = NULL;
308         char *xdg_runtime_dir = NULL;
309
310 #if !(GLIB_CHECK_VERSION(2, 36, 0))
311         g_type_init();
312 #endif
313
314         kb = bundle_import_from_argv(argc, argv);
315         if (kb) {
316                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
317                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
318                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
319
320                 if (xdg_runtime_dir)
321                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
322
323                 if (wayland_display)
324                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
325
326                 bundle_free(kb);
327                 kb = NULL;
328         } else {
329                 _E("failed to get launch argv");
330         }
331
332         elm_init(argc, argv);
333
334         r = aul_launch_init(__aul_handler, NULL);
335         if (r < 0) {
336                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
337                                         "Fail to call the aul_launch_init");
338         }
339
340         r = aul_launch_argv_handler(argc, argv);
341         if (r < 0) {
342                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
343                                         "Fail to call the aul_launch_argv_handler");
344         }
345
346         r = app_get_id(&appid);
347         if (r != APP_ERROR_NONE)
348                 return r;
349
350         class_provider = app_ops->create(app_user_data);
351         if (class_provider == NULL) {
352                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
353                                         "widget_class is NULL");
354         }
355
356         return WIDGET_ERROR_NONE;
357 }
358
359 static void __after_loop()
360 {
361         if (app_ops->terminate)
362                 app_ops->terminate(app_user_data);
363
364         elm_shutdown();
365 }
366
367 EXPORT_API int widget_app_main(int argc, char **argv,
368                                 widget_app_lifecycle_callback_s *callback, void *user_data)
369 {
370         int r;
371
372         if (argc <= 0 || argv == NULL || callback == NULL)
373                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
374
375         if (callback->create == NULL)
376                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
377
378         app_ops = callback;
379         app_user_data = user_data;
380         r = __before_loop(argc, argv);
381         if (r < 0)
382                 return r;
383
384         ecore_main_loop_begin();
385         aul_status_update(STATUS_DYING);
386         __after_loop();
387
388         return WIDGET_ERROR_NONE;
389 }
390
391 EXPORT_API int widget_app_exit(void)
392 {
393         ecore_main_loop_quit();
394
395         return WIDGET_ERROR_NONE;
396 }
397
398 static gboolean __finish_event_cb(gpointer user_data)
399 {
400         if (user_data == NULL)
401                 return FALSE;
402
403         widget_context_s *wc = (widget_context_s *)user_data;
404
405         switch (wc->state) {
406         case WC_READY:
407
408                 break;
409         case WC_RUNNING:
410
411                 break;
412         case WC_PAUSED:
413
414                 break;
415         default:
416                 break;
417         }
418
419
420         return FALSE;
421 }
422
423 EXPORT_API int widget_app_terminate_context(widget_context_h context)
424 {
425         if (context == NULL)
426                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
427
428         g_idle_add(__finish_event_cb, context);
429         return WIDGET_ERROR_NONE;
430 }
431
432 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
433 {
434         if (!cb)
435                 return WIDGET_ERROR_INVALID_PARAMETER;
436
437         return WIDGET_ERROR_NONE;
438 }
439
440 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
441                                         app_event_type_e event_type, app_event_cb callback,
442                                         void *user_data)
443 {
444         /* TODO */
445         if (!event_handler || !callback)
446                 return WIDGET_ERROR_INVALID_PARAMETER;
447
448         switch (event_type) {
449         case APP_EVENT_LOW_MEMORY:
450         case APP_EVENT_LOW_BATTERY:
451         case APP_EVENT_LANGUAGE_CHANGED:
452         case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
453         case APP_EVENT_REGION_FORMAT_CHANGED:
454         case APP_EVENT_SUSPENDED_STATE_CHANGED:
455
456                 break;
457         default:
458                 return WIDGET_ERROR_INVALID_PARAMETER;
459         }
460
461         return WIDGET_ERROR_NONE;
462 }
463
464 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
465                                                 event_handler)
466 {
467         /* TODO */
468         if (!event_handler)
469                 return WIDGET_ERROR_INVALID_PARAMETER;
470
471         return WIDGET_ERROR_NONE;
472 }
473
474 EXPORT_API const char* widget_app_get_id(widget_context_h context)
475 {
476         if (!context) {
477                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
478                 return NULL;
479         }
480
481         return context->id;
482 }
483
484 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
485                                         Evas_Object **win)
486 {
487         if (context == NULL || win == NULL)
488                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
489
490         widget_context_s *cxt = (widget_context_s*)context;
491         Evas_Object *ret_win;
492         Ecore_Wl_Window *wl_win;
493
494         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
495         if (ret_win == NULL) {
496                 _E("failed to create window");
497                 return WIDGET_ERROR_FAULT;
498         }
499
500         wl_win = elm_win_wl_window_get(ret_win);
501         if (wl_win == NULL) {
502                 _E("failed to get wayland window");
503                 evas_object_del(ret_win);
504                 return WIDGET_ERROR_FAULT;
505         }
506
507         ecore_wl_window_class_name_set(wl_win, cxt->id);
508
509         *win = ret_win;
510         cxt->win = ret_win;
511
512         return WIDGET_ERROR_NONE;
513 }
514
515 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
516                 widget_instance_lifecycle_callback_s callback, void *user_data)
517 {
518         widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
519
520         if (wc == NULL) {
521                 _E("failed to malloc : %s", __FUNCTION__);
522                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
523                 return NULL;
524         }
525
526         wc->classid = strdup(class_id);
527         wc->user_data = user_data;
528         wc->ops = callback;
529         wc->next = prev;
530         wc->prev = NULL;
531
532         set_last_result(WIDGET_ERROR_NONE);
533
534         if (prev)
535                 prev->prev = wc;
536
537         return wc;
538 }
539
540 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
541                 widget_instance_lifecycle_callback_s callback, void *user_data)
542 {
543         if (class_id == NULL) {
544                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
545                 return NULL;
546         }
547
548         return _widget_class_create(widget_class, class_id, callback, user_data);
549 }
550
551 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
552 {
553         return _widget_class_create(class_provider, appid, callback, user_data);
554 }
555
556 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
557 {
558         if (context == NULL)
559                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
560
561         context->tag = tag;
562
563         return WIDGET_ERROR_NONE;
564 }
565
566 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
567 {
568         if (context == NULL || tag == NULL)
569                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
570
571         *tag = context->tag;
572
573         return WIDGET_ERROR_NONE;
574 }
575
576 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
577 {
578         const char *class_id = NULL;
579         int ret = 0;
580         if (context == NULL || content_info == NULL)
581                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
582
583         if (context->provider == NULL)
584                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
585
586         class_id = context->provider->classid;
587
588         if (class_id == NULL)
589                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
590
591         ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
592
593         if (ret < 0) {
594                 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
595                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
596         }
597
598         return WIDGET_ERROR_NONE;
599 }
600
601 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
602 {
603         /* TODO
604          call elm_win_title_set()
605          */
606
607         if (!context || !title)
608                 return WIDGET_ERROR_INVALID_PARAMETER;
609
610         return WIDGET_ERROR_NONE;
611 }
612