6319e4eeedcc9646255483cd3f9dc87f05f7119c
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_launcher_tizen.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <glib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <appcore/appcore-common.h>
10 #include <pkgmgr-info.h>
11
12 #include "xwalk/application/common/id_util.h"
13 #include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
14
15 enum app_event {
16   AE_UNKNOWN,
17   AE_CREATE,
18   AE_TERMINATE,
19   AE_PAUSE,
20   AE_RESUME,
21   AE_RESET,
22   AE_LOWMEM_POST,
23   AE_MEM_FLUSH,
24   AE_MAX
25 };
26
27 // Private struct from appcore-internal, necessary to get events from
28 // the system.
29 struct ui_ops {
30   void* data;
31   void (*cb_app)(enum app_event evnt, void* data, bundle* b);
32 };
33
34 static struct ui_ops appcore_ops;
35
36 static const char* event2str(enum app_event event) {
37   switch (event) {
38     case AE_UNKNOWN:
39       return "AE_UNKNOWN";
40     case AE_CREATE:
41       return "AE_CREATE";
42     case AE_TERMINATE:
43       return "AE_TERMINATE";
44     case AE_PAUSE:
45       return "AE_PAUSE";
46     case AE_RESUME:
47       return "AE_RESUME";
48     case AE_RESET:
49       return "AE_RESET";
50     case AE_LOWMEM_POST:
51       return "AE_LOWMEM_POST";
52     case AE_MEM_FLUSH:
53       return "AE_MEM_FLUSH";
54     case AE_MAX:
55       return "AE_MAX";
56   }
57
58   return "INVALID EVENT";
59 }
60
61 static void application_event_cb(enum app_event event, void* data, bundle* b) {
62   fprintf(stderr, "event '%s'\n", event2str(event));
63
64   if (event == AE_TERMINATE) {
65     exit(0);
66   }
67 }
68
69 int xwalk_appcore_init(int argc, char** argv, const char* name) {
70   appcore_ops.cb_app = application_event_cb;
71   appcore_ops.data = NULL;
72
73   return appcore_init(name, &appcore_ops, argc, argv);
74 }
75
76 int xwalk_change_cmdline(int argc, char** argv, const char* app_id) {
77   // Change /proc/<pid>/cmdline to app exec path. See XWALK-1722 for details.
78   char* app_id_for_db = strdup(
79       xwalk::application::RawAppIdToAppIdForTizenPkgmgrDB(app_id).c_str());
80   pkgmgrinfo_appinfo_h handle;
81   char* exec_path = NULL;
82   if (pkgmgrinfo_appinfo_get_appinfo(app_id_for_db, &handle) != PMINFO_R_OK ||
83       pkgmgrinfo_appinfo_get_exec(handle, &exec_path) != PMINFO_R_OK ||
84       !exec_path) {
85     fprintf(stderr, "Couldn't find exec path for application: %s\n",
86             app_id_for_db);
87     return -1;
88   }
89
90   for (int i = 0; i < argc; ++i)
91     memset(argv[i], 0, strlen(argv[i]));
92   strncpy(argv[0], exec_path, strlen(exec_path)+1);
93   g_free(app_id_for_db);
94   pkgmgrinfo_appinfo_destroy_appinfo(handle);
95   return 0;
96 }