4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <sys/types.h>
32 #include <linux/limits.h>
38 #include <tzplatform_config.h>
39 #include "appcore-internal.h"
41 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
42 #include <dbus/dbus.h>
43 #include <dbus/dbus-glib-lowlevel.h>
45 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
46 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
47 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
50 #define SQLITE_FLUSH_MAX (1024*1024)
52 #define PKGNAME_MAX 256
53 #define PATH_APP_ROOT tzplatform_getenv(TZ_USER_APP)
54 #define PATH_RO_APP_ROOT tzplatform_getenv(TZ_SYS_RO_APP)
55 #define PATH_RES "/res"
56 #define PATH_LOCALE "/locale"
58 static struct appcore core;
61 static enum appcore_event to_ae[SE_MAX] = {
62 APPCORE_EVENT_UNKNOWN, /* SE_UNKNOWN */
63 APPCORE_EVENT_LOW_MEMORY, /* SE_LOWMEM */
64 APPCORE_EVENT_LOW_BATTERY, /* SE_LOWBAT */
65 APPCORE_EVENT_LANG_CHANGE, /* SE_LANGCGH */
66 APPCORE_EVENT_REGION_CHANGE,
67 APPCORE_EVENT_SUSPENDED_STATE_CHANGE,
70 static int appcore_event_initialized[SE_MAX] = {0,};
72 enum cb_type { /* callback */
82 enum appcore_event sys;
87 int (*cb_pre) (void *);
89 int (*cb_post) (void *);
91 int (*vcb_pre) (void *, void *);
92 int (*vcb) (void *, void *);
93 int (*vcb_post) (void *, void *);
97 int (*callback) (void *);
101 static struct open_s open;
103 static int __app_terminate(void *data);
104 static int __app_resume(void *data);
105 static int __app_reset(void *data, bundle *k);
107 static int __sys_lowmem_post(void *data, void *evt);
108 static int __sys_lowmem(void *data, void *evt);
109 static int __sys_lowbatt(void *data, void *evt);
110 static int __sys_langchg_pre(void *data, void *evt);
111 static int __sys_langchg(void *data, void *evt);
112 static int __sys_regionchg_pre(void *data, void *evt);
113 static int __sys_regionchg(void *data, void *evt);
114 extern void aul_finalize();
117 static struct evt_ops evtops[] = {
120 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
121 .vcb_post = __sys_lowmem_post,
126 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
127 .vcb = __sys_lowbatt,
131 .key.vkey = VCONFKEY_LANGSET,
132 .vcb_pre = __sys_langchg_pre,
133 .vcb = __sys_langchg,
137 .key.vkey = VCONFKEY_REGIONFORMAT,
138 .vcb_pre = __sys_regionchg_pre,
139 .vcb = __sys_regionchg,
143 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
144 .vcb = __sys_regionchg,
148 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
149 static DBusConnection *bus = NULL;
150 static int __suspend_dbus_handler_initialized = 0;
153 static int __get_dir_name(char *dirname)
155 char pkg_name[PKGNAME_MAX];
163 if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
166 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
167 PATH_APP_ROOT, pkg_name);
170 if (access(dirname, R_OK) == 0) return 0;
171 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
172 PATH_RO_APP_ROOT, pkg_name);
179 static int __app_terminate(void *data)
181 struct appcore *ac = data;
183 _retv_if(ac == NULL || ac->ops == NULL, -1);
184 _retv_if(ac->ops->cb_app == NULL, 0);
186 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
191 static int __bgapp_terminate(void *data)
193 struct appcore *ac = data;
195 _retv_if(ac == NULL || ac->ops == NULL, -1);
196 _retv_if(ac->ops->cb_app == NULL, 0);
198 ac->ops->cb_app(AE_TERMINATE_BGAPP, ac->ops->data, NULL);
203 static gboolean __prt_ltime(gpointer data)
207 msec = appcore_measure_time_from(NULL);
209 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
214 static int __app_reset(void *data, bundle * k)
216 struct appcore *ac = data;
217 _retv_if(ac == NULL || ac->ops == NULL, -1);
218 _retv_if(ac->ops->cb_app == NULL, 0);
220 g_idle_add(__prt_ltime, ac);
222 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
227 static int __app_resume(void *data)
229 struct appcore *ac = data;
230 _retv_if(ac == NULL || ac->ops == NULL, -1);
231 _retv_if(ac->ops->cb_app == NULL, 0);
233 ac->ops->cb_app(AE_RAISE, ac->ops->data, NULL);
237 static int __app_pause(void *data)
239 struct appcore *ac = data;
240 _retv_if(ac == NULL || ac->ops == NULL, -1);
241 _retv_if(ac->ops->cb_app == NULL, 0);
243 ac->ops->cb_app(AE_LOWER, ac->ops->data, NULL);
247 static int __sys_do_default(struct appcore *ac, enum sys_event event)
253 /*r = __def_lowbatt(ac);*/
264 static int __sys_do(struct appcore *ac, void *event_info, enum sys_event event)
268 _retv_if(ac == NULL || event >= SE_MAX, -1);
270 op = &ac->sops[event];
272 if (op->func == NULL)
273 return __sys_do_default(ac, event);
275 return op->func(event_info, op->data);
278 static int __sys_lowmem_post(void *data, void *evt)
280 keynode_t *key = evt;
283 val = vconf_keynode_get_int(key);
285 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
286 #if defined(MEMORY_FLUSH_ACTIVATE)
287 struct appcore *ac = data;
288 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
296 static int __sys_lowmem(void *data, void *evt)
298 keynode_t *key = evt;
301 val = vconf_keynode_get_int(key);
303 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
304 return __sys_do(data, (void *)&val, SE_LOWMEM);
309 static int __sys_lowbatt(void *data, void *evt)
311 keynode_t *key = evt;
314 val = vconf_keynode_get_int(key);
316 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
317 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
318 return __sys_do(data, (void *)&val, SE_LOWBAT);
323 static int __sys_langchg_pre(void *data, void *evt)
329 static int __sys_langchg(void *data, void *evt)
331 keynode_t *key = evt;
334 val = vconf_keynode_get_str(key);
336 return __sys_do(data, (void *)val, SE_LANGCHG);
339 static int __sys_regionchg_pre(void *data, void *evt)
345 static int __sys_regionchg(void *data, void *evt)
347 keynode_t *key = evt;
351 name = vconf_keynode_get_name(key);
352 if (!strcmp(name, VCONFKEY_REGIONFORMAT))
353 val = vconf_keynode_get_str(key);
355 return __sys_do(data, (void *)val, SE_REGIONCHG);
358 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
363 eo->vcb_pre(data, key);
369 eo->vcb_post(data, key);
372 static void __vconf_cb(keynode_t *key, void *data)
377 name = vconf_keynode_get_name(key);
378 _ret_if(name == NULL);
380 _DBG("[APP %d] vconf changed: %s", _pid, name);
382 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
383 struct evt_ops *eo = &evtops[i];
387 if (!strcmp(name, eo->key.vkey))
388 __vconf_do(eo, key, data);
397 static int __add_vconf(struct appcore *ac, enum sys_event se)
403 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb, ac);
406 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb, ac);
409 r = vconf_notify_key_changed(VCONFKEY_LANGSET, __vconf_cb, ac);
412 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb, ac);
416 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb, ac);
426 static int __del_vconf(enum sys_event se)
432 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb);
435 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb);
438 r = vconf_ignore_key_changed(VCONFKEY_LANGSET, __vconf_cb);
441 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb);
445 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb);
455 static int __del_vconf_list(void)
460 for (se = SE_LOWMEM; se < SE_MAX; se++) {
461 if (appcore_event_initialized[se]) {
464 _ERR("Delete vconf callback failed");
466 appcore_event_initialized[se] = 0;
473 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
474 static gboolean __flush_memory(gpointer data)
476 int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
477 struct appcore *ac = (struct appcore *)data;
479 appcore_flush_memory();
486 if (!ac->allowed_bg && !ac->suspended_state) {
487 _DBG("[__SUSPEND__] flush case");
488 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
489 ac->suspended_state = true;
495 static void __add_suspend_timer(struct appcore *ac)
497 ac->tid = g_timeout_add_seconds(5, __flush_memory, ac);
500 static void __remove_suspend_timer(struct appcore *ac)
503 g_source_remove(ac->tid);
509 static int __aul_handler(aul_type type, bundle *b, void *data)
512 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
513 const char *bg = NULL;
514 struct appcore *ac = data;
519 _DBG("[APP %d] AUL event: AUL_START", _pid);
520 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
521 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
522 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWGED_BG")) == 0) {
523 _DBG("[__SUSPEND__] allowed background");
524 ac->allowed_bg = true;
525 __remove_suspend_timer(data);
529 __app_reset(data, b);
532 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
533 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
534 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
535 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
536 _DBG("[__SUSPEND__] allowed background");
537 ac->allowed_bg = true;
538 __remove_suspend_timer(data);
543 ret = open.callback(open.cbdata);
551 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
552 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
554 __remove_suspend_timer(data);
557 __app_terminate(data);
559 case AUL_TERMINATE_BGAPP:
560 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
561 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
563 __remove_suspend_timer(data);
566 __bgapp_terminate(data);
569 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
572 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
574 _DBG("[APP %d] AUL event: AUL_WAKE", _pid);
575 if (!ac->allowed_bg && ac->suspended_state) {
576 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
577 __remove_suspend_timer(data);
578 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
579 ac->suspended_state = false;
583 _DBG("[APP %d] AUL event: AUL_SUSPEND", _pid);
584 if (!ac->allowed_bg && !ac->suspended_state) {
585 __remove_suspend_timer(data);
586 __flush_memory((gpointer)ac);
591 _DBG("[APP %d] AUL event: %d", _pid, type);
600 static void __clear(struct appcore *ac)
602 memset(ac, 0, sizeof(struct appcore));
605 void appcore_get_app_core(struct appcore **ac)
610 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
619 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
620 int (*cb) (void *, void *), void *data)
622 struct appcore *ac = &core;
627 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
628 if (event == to_ae[se])
632 if (se == SE_UNKNOWN || se >= SE_MAX) {
633 _ERR("Unregistered event");
643 if (op->func && !appcore_event_initialized[se]) {
644 r = __add_vconf(ac, se);
646 _ERR("Add vconf callback failed");
648 appcore_event_initialized[se] = 1;
649 } else if (!op->func && appcore_event_initialized[se]) {
652 _ERR("Delete vconf callback failed");
654 appcore_event_initialized[se] = 0;
660 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
661 int argc, char **argv)
664 char dirname[PATH_MAX];
666 if (core.state != 0) {
667 _ERR("Already in use");
672 if (ops == NULL || ops->cb_app == NULL) {
673 _ERR("ops or callback function is null");
678 r = __get_dir_name(dirname);
679 r = set_i18n(name, dirname);
680 _retv_if(r == -1, -1);
682 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
683 r = _appcore_init_suspend_dbus_handler(&core);
685 _ERR("Initailzing suspended state handler failed");
690 r = aul_launch_init(__aul_handler, &core);
692 _ERR("Aul init failed: %d", r);
696 r = aul_launch_argv_handler(argc, argv);
698 _ERR("Aul argv handler failed: %d", r);
703 core.state = 1; /* TODO: use enum value */
705 core.suspended_state = false;
706 core.allowed_bg = false;
717 EXPORT_API void appcore_exit(void)
722 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
723 __remove_suspend_timer(&core);
729 EXPORT_API int appcore_flush_memory(void)
731 int (*flush_fn) (int);
733 struct appcore *ac = &core;
736 _ERR("Appcore not initialized");
740 _DBG("[APP %d] Flushing memory ...", _pid);
743 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
745 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
747 flush_fn(SQLITE_FLUSH_MAX);
751 *Disabled - the impact of stack_trim() is unclear
755 _DBG("[APP %d] Flushing memory DONE", _pid);
760 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
761 static DBusHandlerResult __suspend_dbus_signal_filter(DBusConnection *conn,
762 DBusMessage *message, void *user_data)
765 const char *interface;
771 dbus_error_init(&error);
773 sender = dbus_message_get_sender(message);
775 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
777 interface = dbus_message_get_interface(message);
778 if (interface == NULL) {
779 _ERR("reject by security issue - no interface\n");
780 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
783 if (dbus_message_is_signal(message, interface, RESOURCED_FREEZER_SIGNAL)) {
784 if (dbus_message_get_args(message, &error, DBUS_TYPE_INT32, &state,
785 DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID) == FALSE) {
786 _ERR("Failed to get data: %s", error.message);
787 dbus_error_free(&error);
788 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
791 if (pid == getpid() && state == 0) { /* thawed */
792 suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
793 SECURE_LOGD("[__SUSPEND__] state: %d (0: thawed, 1: frozen), pid: %d", state, pid);
795 struct appcore *ac = (struct appcore *)user_data;
796 if (!ac->allowed_bg && ac->suspended_state) {
797 __remove_suspend_timer(ac);
798 __sys_do(user_data, &suspend, SE_SUSPENDED_STATE);
799 ac->suspended_state = false;
800 __add_suspend_timer(ac);
805 return DBUS_HANDLER_RESULT_HANDLED;
808 int _appcore_init_suspend_dbus_handler(void *data)
811 char rule[MAX_LOCAL_BUFSZ];
813 if (__suspend_dbus_handler_initialized)
816 dbus_error_init(&error);
818 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
820 _ERR("Failed to connect to the D-BUS daemon: %s", error.message);
821 dbus_error_free(&error);
825 dbus_connection_setup_with_g_main(bus, NULL);
827 snprintf(rule, MAX_LOCAL_BUFSZ,
828 "path='%s',type='signal',interface='%s'", RESOURCED_FREEZER_PATH, RESOURCED_FREEZER_INTERFACE);
829 /* listening to messages */
830 dbus_bus_add_match(bus, rule, &error);
831 if (dbus_error_is_set(&error)) {
832 _ERR("Fail to rule set: %s", error.message);
833 dbus_error_free(&error);
837 if (dbus_connection_add_filter(bus, __suspend_dbus_signal_filter, data, NULL) == FALSE) {
838 _ERR("add filter fail");
842 __suspend_dbus_handler_initialized = 1;
843 _DBG("[__SUSPEND__] suspend signal initialized");