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,
70 enum cb_type { /* callback */
80 enum appcore_event sys;
85 int (*cb_pre) (void *);
87 int (*cb_post) (void *);
89 int (*vcb_pre) (void *, void *);
90 int (*vcb) (void *, void *);
91 int (*vcb_post) (void *, void *);
95 int (*callback) (void *);
99 static struct open_s open;
101 static int __app_terminate(void *data);
102 static int __app_resume(void *data);
103 static int __app_reset(void *data, bundle *k);
105 static int __sys_lowmem_post(void *data, void *evt);
106 static int __sys_lowmem(void *data, void *evt);
107 static int __sys_lowbatt(void *data, void *evt);
108 static int __sys_langchg_pre(void *data, void *evt);
109 static int __sys_langchg(void *data, void *evt);
110 static int __sys_regionchg_pre(void *data, void *evt);
111 static int __sys_regionchg(void *data, void *evt);
112 extern void aul_finalize();
115 static struct evt_ops evtops[] = {
118 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
119 .vcb_post = __sys_lowmem_post,
124 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
125 .vcb = __sys_lowbatt,
129 .key.vkey = VCONFKEY_LANGSET,
130 .vcb_pre = __sys_langchg_pre,
131 .vcb = __sys_langchg,
135 .key.vkey = VCONFKEY_REGIONFORMAT,
136 .vcb_pre = __sys_regionchg_pre,
137 .vcb = __sys_regionchg,
141 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
142 .vcb = __sys_regionchg,
146 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
147 static DBusConnection *bus = NULL;
148 static int __suspend_dbus_handler_initialized = 0;
151 static int __get_dir_name(char *dirname)
153 char pkg_name[PKGNAME_MAX];
161 if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
164 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
165 PATH_APP_ROOT, pkg_name);
168 if (access(dirname, R_OK) == 0) return 0;
169 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
170 PATH_RO_APP_ROOT, pkg_name);
177 static int __app_terminate(void *data)
179 struct appcore *ac = data;
181 _retv_if(ac == NULL || ac->ops == NULL, -1);
182 _retv_if(ac->ops->cb_app == NULL, 0);
184 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
189 static int __bgapp_terminate(void *data)
191 struct appcore *ac = data;
193 _retv_if(ac == NULL || ac->ops == NULL, -1);
194 _retv_if(ac->ops->cb_app == NULL, 0);
196 ac->ops->cb_app(AE_TERMINATE_BGAPP, ac->ops->data, NULL);
201 static gboolean __prt_ltime(gpointer data)
205 msec = appcore_measure_time_from(NULL);
207 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
212 static int __app_reset(void *data, bundle * k)
214 struct appcore *ac = data;
215 _retv_if(ac == NULL || ac->ops == NULL, -1);
216 _retv_if(ac->ops->cb_app == NULL, 0);
218 g_idle_add(__prt_ltime, ac);
220 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
225 static int __app_resume(void *data)
227 struct appcore *ac = data;
228 _retv_if(ac == NULL || ac->ops == NULL, -1);
229 _retv_if(ac->ops->cb_app == NULL, 0);
231 ac->ops->cb_app(AE_RAISE, ac->ops->data, NULL);
235 static int __app_pause(void *data)
237 struct appcore *ac = data;
238 _retv_if(ac == NULL || ac->ops == NULL, -1);
239 _retv_if(ac->ops->cb_app == NULL, 0);
241 ac->ops->cb_app(AE_LOWER, ac->ops->data, NULL);
245 static int __sys_do_default(struct appcore *ac, enum sys_event event)
251 /*r = __def_lowbatt(ac);*/
262 static int __sys_do(struct appcore *ac, void *event_info, enum sys_event event)
266 _retv_if(ac == NULL || event >= SE_MAX, -1);
268 op = &ac->sops[event];
270 if (op->func == NULL)
271 return __sys_do_default(ac, event);
273 return op->func(event_info, op->data);
276 static int __sys_lowmem_post(void *data, void *evt)
278 keynode_t *key = evt;
281 val = vconf_keynode_get_int(key);
283 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
284 #if defined(MEMORY_FLUSH_ACTIVATE)
285 struct appcore *ac = data;
286 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
294 static int __sys_lowmem(void *data, void *evt)
296 keynode_t *key = evt;
299 val = vconf_keynode_get_int(key);
301 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
302 return __sys_do(data, (void *)&val, SE_LOWMEM);
307 static int __sys_lowbatt(void *data, void *evt)
309 keynode_t *key = evt;
312 val = vconf_keynode_get_int(key);
314 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
315 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
316 return __sys_do(data, (void *)&val, SE_LOWBAT);
321 static int __sys_langchg_pre(void *data, void *evt)
327 static int __sys_langchg(void *data, void *evt)
329 keynode_t *key = evt;
332 val = vconf_keynode_get_str(key);
334 return __sys_do(data, (void *)val, SE_LANGCHG);
337 static int __sys_regionchg_pre(void *data, void *evt)
343 static int __sys_regionchg(void *data, void *evt)
345 keynode_t *key = evt;
349 name = vconf_keynode_get_name(key);
350 if (!strcmp(name, VCONFKEY_REGIONFORMAT))
351 val = vconf_keynode_get_str(key);
353 return __sys_do(data, (void *)val, SE_REGIONCHG);
356 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
361 eo->vcb_pre(data, key);
367 eo->vcb_post(data, key);
370 static void __vconf_cb(keynode_t *key, void *data)
375 name = vconf_keynode_get_name(key);
376 _ret_if(name == NULL);
378 _DBG("[APP %d] vconf changed: %s", _pid, name);
380 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
381 struct evt_ops *eo = &evtops[i];
385 if (!strcmp(name, eo->key.vkey))
386 __vconf_do(eo, key, data);
395 static int __add_vconf(struct appcore *ac)
400 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
401 struct evt_ops *eo = &evtops[i];
405 r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
417 static int __del_vconf(void)
422 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
423 struct evt_ops *eo = &evtops[i];
427 r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
438 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
439 static gboolean __flush_memory(gpointer data)
441 int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
442 struct appcore *ac = (struct appcore *)data;
444 appcore_flush_memory();
451 if (!ac->allowed_bg && !ac->suspended_state) {
452 _DBG("[__SUSPEND__] flush case");
453 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
454 ac->suspended_state = true;
460 static void __add_suspend_timer(struct appcore *ac)
462 ac->tid = g_timeout_add_seconds(5, __flush_memory, ac);
465 static void __remove_suspend_timer(struct appcore *ac)
468 g_source_remove(ac->tid);
474 static int __aul_handler(aul_type type, bundle *b, void *data)
477 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
478 const char *bg = NULL;
479 struct appcore *ac = data;
484 _DBG("[APP %d] AUL event: AUL_START", _pid);
485 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
486 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
487 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWGED_BG")) == 0) {
488 _DBG("[__SUSPEND__] allowed background");
489 ac->allowed_bg = true;
490 __remove_suspend_timer(data);
494 __app_reset(data, b);
497 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
498 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
499 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
500 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
501 _DBG("[__SUSPEND__] allowed background");
502 ac->allowed_bg = true;
503 __remove_suspend_timer(data);
508 ret = open.callback(open.cbdata);
516 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
517 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
519 __remove_suspend_timer(data);
522 __app_terminate(data);
524 case AUL_TERMINATE_BGAPP:
525 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
526 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
528 __remove_suspend_timer(data);
531 __bgapp_terminate(data);
534 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
537 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
539 _DBG("[APP %d] AUL event: AUL_WAKE", _pid);
540 if (!ac->allowed_bg && ac->suspended_state) {
541 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
542 __remove_suspend_timer(data);
543 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
544 ac->suspended_state = false;
548 _DBG("[APP %d] AUL event: AUL_SUSPEND", _pid);
549 if (!ac->allowed_bg && !ac->suspended_state) {
550 __remove_suspend_timer(data);
551 __flush_memory((gpointer)ac);
556 _DBG("[APP %d] AUL event: %d", _pid, type);
565 static void __clear(struct appcore *ac)
567 memset(ac, 0, sizeof(struct appcore));
570 void appcore_get_app_core(struct appcore **ac)
575 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
584 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
585 int (*cb) (void *, void *), void *data)
587 struct appcore *ac = &core;
591 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
592 if (event == to_ae[se])
596 if (se == SE_UNKNOWN || se >= SE_MAX) {
597 _ERR("Unregistered event");
612 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
613 int argc, char **argv)
616 char dirname[PATH_MAX];
618 if (core.state != 0) {
619 _ERR("Already in use");
624 if (ops == NULL || ops->cb_app == NULL) {
625 _ERR("ops or callback function is null");
630 r = __get_dir_name(dirname);
631 r = set_i18n(name, dirname);
632 _retv_if(r == -1, -1);
634 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
635 r = _appcore_init_suspend_dbus_handler(&core);
637 _ERR("Initailzing suspended state handler failed");
642 r = __add_vconf(&core);
644 _ERR("Add vconf callback failed");
648 r = aul_launch_init(__aul_handler, &core);
650 _ERR("Aul init failed: %d", r);
654 r = aul_launch_argv_handler(argc, argv);
656 _ERR("Aul argv handler failed: %d", r);
661 core.state = 1; /* TODO: use enum value */
663 core.suspended_state = false;
664 core.allowed_bg = false;
675 EXPORT_API void appcore_exit(void)
680 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
681 __remove_suspend_timer(&core);
687 EXPORT_API int appcore_flush_memory(void)
689 int (*flush_fn) (int);
692 struct appcore *ac = &core;
695 _ERR("Appcore not initialized");
699 _DBG("[APP %d] Flushing memory ...", _pid);
701 if (ac->ops->cb_app) {
702 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
705 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
707 size = flush_fn(SQLITE_FLUSH_MAX);
712 *Disabled - the impact of stack_trim() is unclear
716 _DBG("[APP %d] Flushing memory DONE", _pid);
721 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
722 static DBusHandlerResult __suspend_dbus_signal_filter(DBusConnection *conn,
723 DBusMessage *message, void *user_data)
726 const char *interface;
732 dbus_error_init(&error);
734 sender = dbus_message_get_sender(message);
736 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
738 interface = dbus_message_get_interface(message);
739 if (interface == NULL) {
740 _ERR("reject by security issue - no interface\n");
741 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
744 if (dbus_message_is_signal(message, interface, RESOURCED_FREEZER_SIGNAL)) {
745 if (dbus_message_get_args(message, &error, DBUS_TYPE_INT32, &state,
746 DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID) == FALSE) {
747 _ERR("Failed to get data: %s", error.message);
748 dbus_error_free(&error);
749 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
752 if (pid == getpid() && state == 0) { //thawed
753 suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
754 SECURE_LOGD("[__SUSPEND__] state: %d (0: thawed, 1: frozen), pid: %d", state, pid);
756 struct appcore *ac = (struct appcore *)user_data;
757 if (!ac->allowed_bg && ac->suspended_state) {
758 __remove_suspend_timer(ac);
759 __sys_do(user_data, &suspend, SE_SUSPENDED_STATE);
760 ac->suspended_state = false;
761 __add_suspend_timer(ac);
766 return DBUS_HANDLER_RESULT_HANDLED;
769 int _appcore_init_suspend_dbus_handler(void *data)
772 char rule[MAX_LOCAL_BUFSZ];
774 if (__suspend_dbus_handler_initialized)
777 dbus_error_init(&error);
779 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
781 _ERR("Failed to connect to the D-BUS daemon: %s", error.message);
782 dbus_error_free(&error);
786 dbus_connection_setup_with_g_main(bus, NULL);
788 snprintf(rule, MAX_LOCAL_BUFSZ,
789 "path='%s',type='signal',interface='%s'", RESOURCED_FREEZER_PATH, RESOURCED_FREEZER_INTERFACE);
790 /* listening to messages */
791 dbus_bus_add_match(bus, rule, &error);
792 if (dbus_error_is_set(&error)) {
793 _ERR("Fail to rule set: %s", error.message);
794 dbus_error_free(&error);
798 if (dbus_connection_add_filter(bus, __suspend_dbus_signal_filter, data, NULL) == FALSE) {
799 _ERR("add filter fail");
803 __suspend_dbus_handler_initialized = 1;
804 _DBG("[__SUSPEND__] suspend signal initialized");