5649a26eb34198783a6433c11c363132c707173d
[platform/framework/web/data-provider-slave.git] / src / main.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <malloc.h>
21 #include <mcheck.h>
22
23 #include <Elementary.h>
24
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <gio/gio.h>
28 #include <Ecore.h>
29 #include <Ecore_X.h>
30 #include <app.h>
31 #include <Edje.h>
32 #include <Eina.h>
33
34 #include <dlog.h>
35 #include <bundle.h>
36 #include <heap-monitor.h>
37 #include <livebox-service.h>
38 #include <provider.h>
39 #include <vconf.h>
40 #include <livebox.h>
41
42 #include "critical_log.h"
43 #include "debug.h"
44 #include "conf.h"
45 #include "fault.h"
46 #include "update_monitor.h"
47 #include "client.h"
48 #include "util.h"
49 #include "so_handler.h"
50 #include "lb.h"
51
52 /*!
53  * \NOTE
54  * Undocumented API
55  */
56 extern void evas_common_font_flush(void);
57 extern int evas_common_font_cache_get(void);
58 extern void evas_common_font_cache_set(int size);
59
60 #define TEXT_CLASS      "tizen"
61 #define TEXT_SIZE       -100
62
63 static struct info {
64         Ecore_Event_Handler *property_handler;
65         int heap_monitor;
66 } s_info = {
67         .property_handler = NULL,
68         .heap_monitor = 0,
69 };
70
71 static Eina_Bool update_font_cb(void *data)
72 {
73         lb_system_event_all(LB_SYS_EVENT_FONT_CHANGED);
74         return ECORE_CALLBACK_CANCEL;
75 }
76
77 static Eina_Bool property_cb(void *data, int type, void *event)
78 {
79         Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
80
81         if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
82                 Eina_List *list;
83                 char *text;
84                 char *font;
85                 int cache;
86
87                 font = vconf_get_str("db/setting/accessibility/font_name");
88                 if (!font)
89                         return ECORE_CALLBACK_PASS_ON;
90
91                 cache = evas_common_font_cache_get();
92                 evas_common_font_cache_set(0);
93                 evas_common_font_flush();
94
95                 list = edje_text_class_list();
96                 EINA_LIST_FREE(list, text) {
97                         if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
98                                 edje_text_class_del(text);
99                                 edje_text_class_set(text, font, TEXT_SIZE);
100                                 DbgPrint("Update text class %s (%s, %d)\n", text, font, TEXT_SIZE);
101                         } else {
102                                 DbgPrint("Skip text class %s\n", text);
103                         }
104                 }
105
106                 evas_common_font_cache_set(cache);
107                 free(font);
108
109                 /*!
110                  * \NOTE
111                  * Try to update all liveboxes
112                  */
113                 if (!ecore_timer_add(0.1f, update_font_cb, NULL)) {
114                         ErrPrint("Failed to add timer for updating fonts\n");
115                         lb_system_event_all(LB_SYS_EVENT_FONT_CHANGED);
116                 }
117         }
118
119         return ECORE_CALLBACK_PASS_ON;
120 }
121
122 static void time_changed_cb(keynode_t *node, void *user_data)
123 {
124         if (vconf_keynode_get_int(node) != VCONFKEY_SYSMAN_STIME_CHANGED)
125                 return;
126
127         DbgPrint("Time is changed\n");
128         lb_system_event_all(LB_SYS_EVENT_TIME_CHANGED);
129 }
130
131 static bool app_create(void *data)
132 {
133         int ret;
134
135         ret = conf_loader();
136         DbgPrint("Configureation manager is initiated: %d\n", ret);
137         DbgPrint("Scale factor: %lf\n", elm_config_scale_get());
138
139         if (COM_CORE_THREAD)
140                 setenv("PROVIDER_COM_CORE_THREAD", "true", 0);
141         else
142                 setenv("PROVIDER_COM_CORE_THREAD", "false", 0);
143
144         s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
145         if (!s_info.property_handler)
146                 ErrPrint("Failed to add a property change event handler\n");
147
148         ret = livebox_service_init();
149         DbgPrint("Livebox service init: %d\n", ret);
150
151         /*
152          * \note
153          * Slave is not able to initiate system, before
154          * receive its name from the master
155          *
156          * So create callback doesn't do anything.
157          */
158         ret = fault_init();
159         DbgPrint("Crash recover is initiated: %d\n", ret);
160         ret = update_monitor_init();
161         DbgPrint("Content update monitor is initiated: %d\n", ret);
162
163         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_STIME, time_changed_cb, NULL);
164         DbgPrint("System time event callback added: %d\n", ret);
165
166         return TRUE;
167 }
168
169 static void app_terminate(void *data)
170 {
171         int ret;
172
173         ret = update_monitor_fini();
174         DbgPrint("Content update monitor is finalized: %d\n", ret);
175         ret = fault_fini();
176         DbgPrint("Crash recover is finalized: %d\n", ret);
177
178         ret = client_fini();
179         DbgPrint("client finalized: %d\n", ret); 
180
181         ret = livebox_service_fini();
182         DbgPrint("livebox service fini: %d\n", ret);
183
184         ecore_event_handler_del(s_info.property_handler);
185         s_info.property_handler = NULL;
186
187         return;
188 }
189
190 static void app_pause(void *data)
191 {
192         /* Will not be invoked */
193         return;
194 }
195
196 static void app_resume(void *data)
197 {
198         /* Will not be invoked */
199         return;
200 }
201
202 static void app_region_changed(void *data)
203 {
204         lb_system_event_all(LB_SYS_EVENT_REGION_CHANGED);
205 }
206
207 static void app_language_changed(void *data)
208 {
209         lb_system_event_all(LB_SYS_EVENT_LANG_CHANGED);
210 }
211
212 static void app_service(service_h service, void *data)
213 {
214         int ret;
215         char *name;
216         char *secured;
217         static int initialized = 0;
218
219         if (initialized) {
220                 ErrPrint("Already initialized\n");
221                 return;
222         }
223
224         ret = service_get_extra_data(service, "name", &name);
225         if (ret != SERVICE_ERROR_NONE) {
226                 ErrPrint("Name is not valid\n");
227                 return;
228         }
229
230         ret = service_get_extra_data(service, "secured", &secured);
231         if (ret != SERVICE_ERROR_NONE) {
232                 free(name);
233                 ErrPrint("Secured is not valid\n");
234                 return;
235         }
236
237         if (!!strcasecmp(secured, "true")) {
238                 if (s_info.heap_monitor) {
239                         heap_monitor_start();
240                         /* Add GUARD */
241                         // heap_monitor_add_target("/usr/apps/org.tizen."EXEC_NAME"/bin/"EXEC_NAME);
242                 }
243         } else {
244                 /* Don't use the update timer */
245                 lb_turn_secured_on();
246         }
247
248         DbgPrint("Name assigned: %s\n", name);
249         DbgPrint("Secured: %s\n", secured);
250         ret = client_init(name);
251         free(name);
252
253         initialized = 1;
254         return;
255 }
256
257 void (*__malloc_initialize_hook)(void) = heap_monitor_init;
258
259 #if defined(_ENABLE_MCHECK)
260 static inline void mcheck_cb(enum mcheck_status status)
261 {
262         char *ptr;
263
264         ptr = util_get_current_module(NULL);
265
266         switch (status) {
267         case MCHECK_DISABLED:
268                 ErrPrint("[DISABLED] Heap incosistency detected: %s\n", ptr);
269                 break;
270         case MCHECK_OK:
271                 ErrPrint("[OK] Heap incosistency detected: %s\n", ptr);
272                 break;
273         case MCHECK_HEAD:
274                 ErrPrint("[HEAD] Heap incosistency detected: %s\n", ptr);
275                 break;
276         case MCHECK_TAIL:
277                 ErrPrint("[TAIL] Heap incosistency detected: %s\n", ptr);
278                 break;
279         case MCHECK_FREE:
280                 ErrPrint("[FREE] Heap incosistency detected: %s\n", ptr);
281                 break;
282         default:
283                 break;
284         }
285 }
286 #endif
287
288 int main(int argc, char *argv[])
289 {
290         int ret;
291         app_event_callback_s event_callback;
292         const char *option;
293
294 #if defined(_ENABLE_MCHECK)
295         mcheck(mcheck_cb);
296 #endif
297         option = getenv("PROVIDER_DISABLE_CALL_OPTION");
298         if (option && !strcasecmp(option, "true"))
299                 fault_disable_call_option();
300
301         option = getenv("PROVIDER_HEAP_MONITOR_START");
302         if (option && !strcasecmp(option, "true"))
303                 s_info.heap_monitor = 1;
304
305         setenv("BUFMGR_LOCK_TYPE", "once", 0);
306         setenv("BUFMGR_MAP_CACHE", "true", 0);
307
308         critical_log_init(util_basename(argv[0]));
309         event_callback.create = app_create;
310         event_callback.terminate = app_terminate;
311         event_callback.pause = app_pause;
312         event_callback.resume = app_resume;
313         event_callback.service = app_service;
314         event_callback.low_memory = NULL;
315         event_callback.low_battery = NULL;
316         event_callback.device_orientation = NULL;
317         event_callback.language_changed = app_language_changed;
318         event_callback.region_format_changed = app_region_changed;
319         ret = app_efl_main(&argc, &argv, &event_callback, NULL);
320         critical_log_fini();
321         return ret;
322 }
323
324 HAPI int main_heap_monitor_is_enabled(void)
325 {
326         return s_info.heap_monitor;
327 }
328
329 /* End of a file */