Merge branch 'tizen_3.0' into tizen
[platform/core/appfw/app-core.git] / src / appcore.c
index 31ce92f..20fb3c4 100644 (file)
@@ -30,7 +30,6 @@
 #include <dlfcn.h>
 #include <vconf.h>
 #include <aul.h>
-#include <tzplatform_config.h>
 #include <bundle_internal.h>
 #include "appcore-internal.h"
 
 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
+
+int __appcore_init_suspend_dbus_handler(void *data);
+void __appcore_fini_suspend_dbus_handler(void);
 #endif
 
 #define SQLITE_FLUSH_MAX               (1024*1024)
 
-#define PKGNAME_MAX 256
-#define PATH_APP_ROOT tzplatform_getenv(TZ_USER_APP)
-#define PATH_SYS_RO_APP_ROOT tzplatform_getenv(TZ_SYS_RO_APP)
-#define PATH_SYS_RW_APP_ROOT tzplatform_getenv(TZ_SYS_RW_APP)
-#define PATH_RES "/res"
-#define PATH_LOCALE "/locale"
+#define PATH_LOCALE "locale"
 
 static struct appcore core;
 static pid_t _pid;
@@ -61,6 +58,7 @@ static enum appcore_event to_ae[SE_MAX] = {
        APPCORE_EVENT_LANG_CHANGE,      /* SE_LANGCGH */
        APPCORE_EVENT_REGION_CHANGE,
        APPCORE_EVENT_SUSPENDED_STATE_CHANGE,
+       APPCORE_EVENT_UPDATE_REQUESTED,
 };
 
 static int appcore_event_initialized[SE_MAX] = {0,};
@@ -142,38 +140,22 @@ static struct evt_ops evtops[] = {
 };
 
 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-static GDBusConnection *bus = NULL;
-static guint __suspend_dbus_handler_initialized = 0;
+static GDBusConnection *bus;
+static guint __suspend_dbus_handler_initialized;
 #endif
 
-static int __get_dir_name(char *dirname)
+static int __get_locale_resource_dir(char *locale_dir, int size)
 {
-       char pkg_name[PKGNAME_MAX];
-       int r;
-       int pid;
+       const char *res_path;
 
-       pid = getpid();
-       if (pid < 0)
+       res_path = aul_get_app_resource_path();
+       if (res_path == NULL) {
+               _ERR("Failed to get resource path");
                return -1;
+       }
 
-       if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
-               return -1;
-
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_APP_ROOT, pkg_name);
-       if (r < 0)
-               return -1;
-       if (access(dirname, R_OK) == 0)
-               return 0;
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_SYS_RO_APP_ROOT, pkg_name);
-       if (r < 0)
-               return -1;
-       if (access(dirname, R_OK) == 0)
-               return 0;
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_SYS_RW_APP_ROOT, pkg_name);
-       if (r < 0)
+       snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
+       if (access(locale_dir, R_OK) != 0)
                return -1;
 
        return 0;
@@ -247,6 +229,17 @@ static int __app_pause(void *data)
        return 0;
 }
 
+static int __app_update_requested(void *data)
+{
+       struct appcore *ac = data;
+
+       _retv_if(ac == NULL || ac->ops == NULL, -1);
+       _retv_if(ac->ops->cb_app == NULL, 0);
+       ac->ops->cb_app(AE_UPDATE_REQUESTED, ac->ops->data, NULL);
+
+       return 0;
+}
+
 static int __sys_do_default(struct appcore *ac, enum sys_event event)
 {
        int r;
@@ -512,21 +505,26 @@ static void __remove_suspend_timer(struct appcore *ac)
 static int __aul_handler(aul_type type, bundle *b, void *data)
 {
        int ret;
-       const char *tep_path = NULL;
+       const char **tep_path = NULL;
+       int len = 0;
+       int i;
 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
        const char *bg = NULL;
        struct appcore *ac = data;
+       int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
 #endif
 
        switch (type) {
        case AUL_START:
                _DBG("[APP %d]     AUL event: AUL_START", _pid);
-               tep_path = bundle_get_val(b, AUL_TEP_PATH);
+               tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
                if (tep_path) {
-                       ret = aul_check_tep_mount(tep_path);
-                       if (ret == -1) {
-                               _ERR("mount request not completed within 1 sec");
-                               exit(-1);
+                       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);
+                               }
                        }
                }
 
@@ -586,20 +584,32 @@ static int __aul_handler(aul_type type, bundle *b, void *data)
        case AUL_WAKE:
                _DBG("[APP %d]     AUL event: AUL_WAKE", _pid);
                if (!ac->allowed_bg && ac->suspended_state) {
-                       int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
                        __remove_suspend_timer(data);
                        __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
                        ac->suspended_state = false;
                }
+
+               if (b) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && strcmp(bg, "ALLOWED_BG") == 0) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               ac->allowed_bg = true;
+                       }
+               }
                break;
        case AUL_SUSPEND:
                _DBG("[APP %d]     AUL event: AUL_SUSPEND", _pid);
-               if (!ac->allowed_bg && !ac->suspended_state) {
+               ac->allowed_bg = false;
+               if (!ac->suspended_state) {
                        __remove_suspend_timer(data);
                        __flush_memory((gpointer)ac);
                }
                break;
 #endif
+       case AUL_UPDATE_REQUESTED:
+               _DBG("[APP %d]     AUL event: AUL_UPDATE_REQUESTED", _pid);
+               __app_update_requested(data);
+               break;
        default:
                _DBG("[APP %d]     AUL event: %d", _pid, type);
                /* do nothing */
@@ -675,7 +685,7 @@ static gboolean __init_suspend(gpointer data)
 {
        int r;
 
-       r = _appcore_init_suspend_dbus_handler(&core);
+       r = __appcore_init_suspend_dbus_handler(&core);
        if (r == -1) {
                _ERR("Initailzing suspended state handler failed");
        }
@@ -688,7 +698,7 @@ EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
                            int argc, char **argv)
 {
        int r;
-       char dirname[PATH_MAX];
+       char locale_dir[PATH_MAX];
 
        if (core.state != 0) {
                _ERR("Already in use");
@@ -702,8 +712,8 @@ EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
                return -1;
        }
 
-       r = __get_dir_name(dirname);
-       r = set_i18n(name, dirname);
+       r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
+       r = set_i18n(name, locale_dir);
        _retv_if(r == -1, -1);
 
        r = aul_launch_init(__aul_handler, &core);
@@ -744,6 +754,7 @@ EXPORT_API void appcore_exit(void)
                __clear(&core);
 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
                __remove_suspend_timer(&core);
+               __appcore_fini_suspend_dbus_handler();
 #endif
        }
        aul_finalize();
@@ -807,7 +818,7 @@ static void __suspend_dbus_signal_handler(GDBusConnection *connection,
        }
 }
 
-int _appcore_init_suspend_dbus_handler(void *data)
+int __appcore_init_suspend_dbus_handler(void *data)
 {
        GError *err = NULL;
 
@@ -844,4 +855,20 @@ int _appcore_init_suspend_dbus_handler(void *data)
 
        return 0;
 }
+
+void __appcore_fini_suspend_dbus_handler(void)
+{
+       if (bus == NULL)
+               return;
+
+       if (__suspend_dbus_handler_initialized) {
+               g_dbus_connection_signal_unsubscribe(bus,
+                               __suspend_dbus_handler_initialized);
+               __suspend_dbus_handler_initialized = 0;
+       }
+
+       g_object_unref(bus);
+       bus = NULL;
+}
 #endif
+