Add widget app restart logic
[platform/core/appfw/appcore-widget.git] / src / widget_app_internal.c
1 /*
2  * Copyright (c) 2016 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 #include <stdlib.h>
18
19 #include <glib.h>
20 #include <glib-object.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include <bundle.h>
26 #include <bundle_internal.h>
27 #include <aul.h>
28 #include <aul_app_com.h>
29 #include <dlog.h>
30
31 #include "widget_app.h"
32 #include "widget-log.h"
33 #include "widget-private.h"
34 #include "widget_app_internal.h"
35 #include "widget-private.h"
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40
41 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
42
43 static char *viewer_endpoint = NULL;
44 static GList *contexts = NULL;
45
46 EXPORT_API int widget_app_restart()
47 {
48         int ret;
49         int status = AUL_WIDGET_INSTANCE_EVENT_APP_RESTART_REQUEST;
50         bundle *kb;
51         widget_context_s *wc;
52         char *classid;
53
54         if (contexts == NULL) {
55                 _E("no widget");
56                 return WIDGET_ERROR_IO_ERROR;
57         }
58         wc = (widget_context_s *)contexts->data;
59         classid = wc->provider->classid;
60         _D("restart widget classid : %s", classid);
61
62         kb = bundle_create();
63         bundle_add_str(kb, AUL_K_WIDGET_ID, classid);
64         bundle_add_byte(kb, AUL_K_WIDGET_STATUS, &status, sizeof(int));
65         ret = aul_app_com_send(viewer_endpoint, kb);
66         bundle_free(kb);
67         if (ret != AUL_R_OK) {
68                 _E("failed to kill app");
69                 return WIDGET_ERROR_IO_ERROR;
70         }
71         return WIDGET_ERROR_NONE;
72 }
73
74 GList *_widget_app_get_contexts()
75 {
76         return contexts;
77 }
78
79 int _widget_app_add_context(widget_context_s *wc)
80 {
81         contexts = g_list_append(contexts, wc);
82         return WIDGET_ERROR_NONE;
83 }
84
85 int _widget_app_remove_context(widget_context_s *wc)
86 {
87         contexts = g_list_remove(contexts, wc);
88         return WIDGET_ERROR_NONE;
89 }
90
91 int _widget_app_set_viewer_endpoint(char *endpoint)
92 {
93         if (endpoint == NULL)
94                 return WIDGET_ERROR_INVALID_PARAMETER;
95
96         viewer_endpoint = strdup(endpoint);
97         if (viewer_endpoint == NULL)
98                 return WIDGET_ERROR_OUT_OF_MEMORY;
99
100         return WIDGET_ERROR_NONE;
101 }
102
103 char *_widget_app_get_viewer_endpoint()
104 {
105         return viewer_endpoint;
106 }
107
108 int _widget_app_free_viewer_endpoint()
109 {
110         if (viewer_endpoint)
111                 free(viewer_endpoint);
112
113         return WIDGET_ERROR_NONE;
114 }