Fix appcore_base_flush_memory API
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
index 00ab25e..cc311a9 100644 (file)
@@ -37,6 +37,7 @@
 #include <bundle_internal.h>
 #include <sensor_internal.h>
 #include <ttrace.h>
+#include <system_info.h>
 #include "appcore_base.h"
 #include "appcore_base_private.h"
 
@@ -44,6 +45,7 @@
 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
+#define SQLITE_FLUSH_MAX (1024 * 1024)
 
 typedef struct _appcore_base_context {
        appcore_base_ops ops;
@@ -54,6 +56,7 @@ typedef struct _appcore_base_context {
        bool suspended_state;
        bool allowed_bg;
        bool dirty;
+       guint sid;
 } appcore_base_context;
 
 typedef struct _appcore_base_event_node {
@@ -81,6 +84,46 @@ static guint __suspend_dbus_handler_initialized;
 static char *__locale_dir;
 static appcore_base_rotation __rotation;
 
+appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
+{
+       static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       char *profile_name = NULL;
+
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       system_info_get_platform_string("http://tizen.org/feature/profile",
+                       &profile_name);
+       if (profile_name == NULL)
+               return profile;
+
+       switch (*profile_name) {
+       case 'm':
+       case 'M':
+               profile = TIZEN_PROFILE_MOBILE;
+               break;
+       case 'w':
+       case 'W':
+               profile = TIZEN_PROFILE_WEARABLE;
+               break;
+       case 't':
+       case 'T':
+               profile = TIZEN_PROFILE_TV;
+               break;
+       case 'i':
+       case 'I':
+               profile = TIZEN_PROFILE_IVI;
+               break;
+       default:
+               profile = TIZEN_PROFILE_COMMON;
+               break;
+       }
+       free(profile_name);
+
+       return profile;
+}
+
+
 static void __invoke_callback(void *event, int type)
 {
        GList *iter = __events;
@@ -417,6 +460,8 @@ static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
        if (lang == NULL)
                return list;
 
+       list = g_list_append(list, strdup(lang));
+
        extract_lang = __get_string_before(lang, ".");
        if (extract_lang == NULL)
                return list;
@@ -573,9 +618,16 @@ static void __update_lang(void)
                setenv("LC_MESSAGES", lang, 1);
                r = setlocale(LC_ALL, "");
                if (r == NULL) {
-                       r = setlocale(LC_ALL, lang);
-                       if (r != NULL)
+                       r = setlocale(LC_ALL, "en_US.UTF-8");
+                       if (r != NULL) {
                                _DBG("*****appcore setlocale=%s\n", r);
+                       } else {
+                               _DBG("*****appcore setlocale=\"C\"");
+                               setenv("LC_ALL", "C", 1);
+                               r = setlocale(LC_ALL, "");
+                               if (r == NULL)
+                                       _ERR("failed to setlocale");
+                       }
                }
                free(lang);
        }
@@ -600,8 +652,15 @@ static void __update_region(void)
                setenv("LC_MEASUREMENT", region, 1);
                setenv("LC_IDENTIFICATION", region, 1);
                r = setlocale(LC_ALL, "");
-               if (r != NULL)
+               if (r != NULL) {
                        _DBG("*****appcore setlocale=%s\n", r);
+               } else {
+                       _DBG("*****appcore setlocale=\"C\"");
+                       setenv("LC_ALL", "C", 1);
+                       r = setlocale(LC_ALL, "");
+                       if (r == NULL)
+                               _ERR("failed to setlocale");
+               }
 
                free(region);
        }
@@ -611,6 +670,11 @@ static void __on_language_change(keynode_t *key, void *data)
 {
        char *val;
 
+       if (__context.sid) {
+               g_source_remove(__context.sid);
+               __context.sid = 0;
+       }
+
        val = vconf_keynode_get_str(key);
 
        __update_lang();
@@ -619,15 +683,58 @@ static void __on_language_change(keynode_t *key, void *data)
 
 static void __on_region_change(keynode_t *key, void *data)
 {
-       char *val = NULL;
+       char *val;
        const char *name;
 
        name = vconf_keynode_get_name(key);
-       if (name && !strcmp(name, VCONFKEY_REGIONFORMAT))
-               val = vconf_keynode_get_str(key);
+       if (name == NULL)
+               return;
+
+       if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
+                       strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
+               return;
+
+       val = vconf_get_str(VCONFKEY_REGIONFORMAT);
 
        __update_region();
        __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
+       free(val);
+}
+
+static gboolean __invoke_lang_change(gpointer data)
+{
+       const char *lang;
+
+       __context.sid = 0;
+
+       lang = getenv("LANG");
+       if (!lang)
+               return G_SOURCE_REMOVE;
+
+       __invoke_callback((void *)lang, APPCORE_BASE_EVENT_LANG_CHANGE);
+
+       return G_SOURCE_REMOVE;
+}
+
+static void __verify_language(void)
+{
+       char *lang;
+       const char *env_lang;
+
+       env_lang = getenv("LANG");
+       if (!env_lang)
+               return;
+
+       lang = vconf_get_str(VCONFKEY_LANGSET);
+       if (!lang)
+               return;
+
+       if (strcmp(env_lang, lang) != 0) {
+               _INFO("LANG(%s), LANGSET(%s)", env_lang, lang);
+               __context.sid = g_idle_add(__invoke_lang_change, NULL);
+       }
+
+       free(lang);
 }
 
 static gboolean __flush_memory(gpointer data)
@@ -737,6 +844,12 @@ static void __fini_suspend_dbus_handler(void)
        __bus = NULL;
 }
 
+static gboolean __init_suspend(gpointer data)
+{
+       __init_suspend_dbus_handler();
+       return G_SOURCE_REMOVE;
+}
+
 static int __get_locale_resource_dir(char *locale_dir, int size)
 {
        const char *res_path;
@@ -778,7 +891,6 @@ static int __get_app_name(const char *appid, char **name)
 static int __set_i18n(const char *domain, const char *dir)
 {
        char *r;
-       char *lan;
 
        if (domain == NULL) {
                errno = EINVAL;
@@ -795,17 +907,20 @@ static int __set_i18n(const char *domain, const char *dir)
        __update_region();
 
        r = setlocale(LC_ALL, "");
-       /* if locale is not set properly, try again to set as language base */
+       /* if locale is not set properly, try to set "en_US" again */
        if (r == NULL) {
-               lan = vconf_get_str(VCONFKEY_LANGSET);
-               if (lan != NULL) {
-                       r = setlocale(LC_ALL, lan);
+               r = setlocale(LC_ALL, "en_US.UTF-8");
+               if (r != NULL)
                        _DBG("*****appcore setlocale=%s\n", r);
-                       free(lan);
-               }
        }
-       if (r == NULL)
+       if (r == NULL) {
                _ERR("appcore: setlocale() error");
+               _DBG("*****appcore setlocale=\"C\"");
+               setenv("LC_ALL", "C", 1);
+               r = setlocale(LC_ALL, "");
+               if (r == NULL)
+                       _ERR("failed to setlocale");
+       }
 
        r = bindtextdomain(domain, dir);
        if (r == NULL)
@@ -826,8 +941,10 @@ EXPORT_API int appcore_base_on_set_i18n(void)
        char *name = NULL;
 
        r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
-       if (r < 0)
+       if (r < 0) {
+               _ERR("Failed to get application ID - pid(%d)", getpid());
                return -1;
+       }
 
        r = __get_app_name(appid, &name);
        if (r < 0)
@@ -855,6 +972,28 @@ EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_na
        return __set_i18n(domain_name, dir_name);
 }
 
+static void __set_default_events(void)
+{
+       int r;
+
+       vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
+       r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
+       if (r == 0)
+               vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
+       vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
+}
+
+static void __unset_default_events(void)
+{
+       int r;
+
+       vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
+       r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
+       if (r == 0)
+               vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
+       vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
+}
+
 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
 {
        int i;
@@ -868,10 +1007,11 @@ EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
        __context.suspended_state = false;
        __context.allowed_bg = false;
 
-       if (__context.ops.set_i18n)
-               __context.ops.set_i18n(__context.data);
+       if (__context.ops.init)
+               __context.ops.init(argc, argv, data);
 
-       __init_suspend_dbus_handler();
+       if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
+               g_idle_add(__init_suspend, NULL);
 
        if (!__context.dirty) {
                __context.dirty = true;
@@ -884,6 +1024,11 @@ EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
                }
        }
 
+       __verify_language();
+       __set_default_events();
+       if (__context.ops.set_i18n)
+               __context.ops.set_i18n(__context.data);
+
        if (__context.ops.create) {
                traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
                r = __context.ops.create(__context.data);
@@ -918,9 +1063,18 @@ EXPORT_API void appcore_base_fini(void)
                }
        }
 
+       __unset_default_events();
+
+       if (__context.sid) {
+               g_source_remove(__context.sid);
+               __context.sid = 0;
+       }
+
        g_list_free_full(__events, free);
        __events = NULL;
-       __fini_suspend_dbus_handler();
+
+       if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
+               __fini_suspend_dbus_handler();
 
        if (__locale_dir) {
                free(__locale_dir);
@@ -928,44 +1082,41 @@ EXPORT_API void appcore_base_fini(void)
        }
 
        __context.dirty = false;
+
+       if (__context.ops.finish)
+               __context.ops.finish();
 }
 
 EXPORT_API int appcore_base_flush_memory(void)
 {
+       int (*sqlite3_free_heap_memory)(int);
+
+       sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
+                       "sqlite3_release_memory");
+       if (sqlite3_free_heap_memory)
+               sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
+
        malloc_trim(0);
        return 0;
 }
 
 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
 {
-       int ret;
-       const char **tep_path;
-       int len = 0;
-       int i;
        const char *bg;
        int dummy = 0;
 
        switch (type) {
        case AUL_START:
                _DBG("[APP %d]     AUL event: AUL_START", getpid());
-               tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
-               if (tep_path) {
-                       for (i = 0; i < len; i++) {
-                               ret = aul_check_tep_mount(tep_path[i]);
-                               if (ret == -1) {
-                                       _ERR("mount request not completed within 1 sec");
-                                       exit(-1);
-                               }
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && !strcmp(bg, "ALLOWED_BG")) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               __context.allowed_bg = true;
+                               __remove_suspend_timer();
                        }
                }
 
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       __context.allowed_bg = true;
-                       __remove_suspend_timer();
-               }
-
                if (__context.ops.control) {
                        traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
                        __context.ops.control(b, __context.data);
@@ -974,11 +1125,13 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
                break;
        case AUL_RESUME:
                _DBG("[APP %d]     AUL event: AUL_RESUME", getpid());
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       __context.allowed_bg = true;
-                       __remove_suspend_timer();
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && !strcmp(bg, "ALLOWED_BG")) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               __context.allowed_bg = true;
+                               __remove_suspend_timer();
+                       }
                }
                break;
        case AUL_TERMINATE:
@@ -997,18 +1150,24 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
                break;
        case AUL_WAKE:
                _DBG("[APP %d]     AUL event: AUL_WAKE", getpid());
-               if (!__context.allowed_bg && __context.suspended_state) {
-                       int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
-                       __remove_suspend_timer();
-                       __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
-                       __context.suspended_state = false;
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       if (!__context.allowed_bg &&
+                                       __context.suspended_state) {
+                               int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
+                               __remove_suspend_timer();
+                               __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
+                               __context.suspended_state = false;
+                       }
                }
                break;
        case AUL_SUSPEND:
                _DBG("[APP %d]     AUL event: AUL_SUSPEND", getpid());
-               if (!__context.allowed_bg && !__context.suspended_state) {
-                       __remove_suspend_timer();
-                       __flush_memory(NULL);
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       if (!__context.allowed_bg &&
+                                       !__context.suspended_state) {
+                               __remove_suspend_timer();
+                               __flush_memory(NULL);
+                       }
                }
                break;
        case AUL_UPDATE_REQUESTED:
@@ -1027,6 +1186,7 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
 EXPORT_API int appcore_base_on_create(void)
 {
        int r;
+
        r = aul_launch_init(__context.ops.receive, NULL);
        if (r < 0) {
                _ERR("Aul init failed: %d", r);
@@ -1050,36 +1210,19 @@ EXPORT_API int appcore_base_on_control(bundle *b)
 EXPORT_API int appcore_base_on_terminate()
 {
        aul_finalize();
-       if (__context.ops.exit)
-               __context.ops.exit(__context.data);
 
        return 0;
 }
 
 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
 {
-       int r;
-
        switch (event) {
-       case APPCORE_BASE_EVENT_LOW_MEMORY:
-               vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
-               break;
        case APPCORE_BASE_EVENT_LOW_BATTERY:
                vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
                break;
-       case APPCORE_BASE_EVENT_LANG_CHANGE:
-               vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
-               break;
        case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
                __register_rotation_changed_event();
                break;
-       case APPCORE_BASE_EVENT_REGION_CHANGE:
-               r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
-               if (r < 0)
-                       break;
-
-               vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
-               break;
        case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
                break;
 
@@ -1091,27 +1234,13 @@ EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
 
 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
 {
-       int r;
-
        switch (event) {
-       case APPCORE_BASE_EVENT_LOW_MEMORY:
-               vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
-               break;
        case APPCORE_BASE_EVENT_LOW_BATTERY:
                vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
                break;
-       case APPCORE_BASE_EVENT_LANG_CHANGE:
-               vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
-               break;
        case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
                __unregister_rotation_changed_event();
                break;
-       case APPCORE_BASE_EVENT_REGION_CHANGE:
-               r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
-               if (r < 0)
-                       break;
-               vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
-               break;
        case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
                break;
        default:
@@ -1194,6 +1323,22 @@ EXPORT_API void appcore_base_toggle_suspended_state(void)
        __context.suspended_state ^= __context.suspended_state;
 }
 
+EXPORT_API void appcore_base_exit(void)
+{
+       if (__context.ops.exit)
+               __context.ops.exit(__context.data);
+}
+
+EXPORT_API void appcore_base_add_suspend_timer(void)
+{
+       __add_suspend_timer();
+}
+
+EXPORT_API void appcore_base_remove_suspend_timer(void)
+{
+       __remove_suspend_timer();
+}
+
 static int __on_receive(aul_type type, bundle *b, void *data)
 {
        return appcore_base_on_receive(type, b);
@@ -1238,6 +1383,8 @@ EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
        ops.terminate = __on_terminate;
        ops.receive = __on_receive;
        ops.set_i18n = __on_set_i18n;
+       ops.init = NULL;
+       ops.finish = NULL;
        ops.run = NULL;
        ops.exit = NULL;
        ops.set_event = __on_set_event;
@@ -1245,5 +1392,3 @@ EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
 
        return ops;
 }
-
-