Upstream version 7.36.149.0
[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/tools/linux/xwalk_launcher_tizen.h"
13
14 enum app_event {
15   AE_UNKNOWN,
16   AE_CREATE,
17   AE_TERMINATE,
18   AE_PAUSE,
19   AE_RESUME,
20   AE_RESET,
21   AE_LOWMEM_POST,
22   AE_MEM_FLUSH,
23   AE_MAX
24 };
25
26 // Private struct from appcore-internal, necessary to get events from
27 // the system.
28 struct ui_ops {
29   void* data;
30   void (*cb_app)(enum app_event evnt, void* data, bundle* b);
31 };
32
33 static struct ui_ops appcore_ops;
34
35 static const char* event2str(enum app_event event) {
36   switch (event) {
37     case AE_UNKNOWN:
38       return "AE_UNKNOWN";
39     case AE_CREATE:
40       return "AE_CREATE";
41     case AE_TERMINATE:
42       return "AE_TERMINATE";
43     case AE_PAUSE:
44       return "AE_PAUSE";
45     case AE_RESUME:
46       return "AE_RESUME";
47     case AE_RESET:
48       return "AE_RESET";
49     case AE_LOWMEM_POST:
50       return "AE_LOWMEM_POST";
51     case AE_MEM_FLUSH:
52       return "AE_MEM_FLUSH";
53     case AE_MAX:
54       return "AE_MAX";
55   }
56
57   return "INVALID EVENT";
58 }
59
60 static void application_event_cb(enum app_event event, void* data, bundle* b) {
61   fprintf(stderr, "event '%s'\n", event2str(event));
62
63   if (event == AE_TERMINATE) {
64     exit(0);
65   }
66 }
67
68 int xwalk_appcore_init(int argc, char** argv, const char* name) {
69   appcore_ops.cb_app = application_event_cb;
70   appcore_ops.data = NULL;
71
72   return appcore_init(name, &appcore_ops, argc, argv);
73 }
74
75 int xwalk_change_cmdline(int argc, char** argv, const char* app_id) {
76   // Change /proc/<pid>/cmdline to app exec path. See XWALK-1722 for details.
77   gchar* tizen_app_id = g_strconcat("xwalk.", app_id, NULL);
78   pkgmgrinfo_appinfo_h handle;
79   char* exec_path = NULL;
80   if (pkgmgrinfo_appinfo_get_appinfo(tizen_app_id, &handle) != PMINFO_R_OK ||
81       pkgmgrinfo_appinfo_get_exec(handle, &exec_path) != PMINFO_R_OK ||
82       !exec_path) {
83     fprintf(stderr, "Couldn't find exec path for application: %s\n",
84             tizen_app_id);
85     return -1;
86   }
87
88   for (int i = 0; i < argc; ++i)
89     memset(argv[i], 0, strlen(argv[i]));
90   strncpy(argv[0], exec_path, strlen(exec_path)+1);
91   g_free(tizen_app_id);
92   pkgmgrinfo_appinfo_destroy_appinfo(handle);
93   return 0;
94 }
95
96 char* xwalk_extract_app_id(const char* tizen_app_id) {
97   // The Tizen application ID will have a format like
98   // "xwalk.crosswalk_32bytes_app_id".
99   char* app_id = NULL;
100   gchar** tokens = g_strsplit(tizen_app_id, ".", 2);
101   if (g_strv_length(tokens) != 2)
102     fprintf(stderr, "Invalid Tizen AppID, fallback to Crosswalk AppID.\n");
103   else
104     app_id = strdup(tokens[1]);
105
106   g_strfreev(tokens);
107   return app_id;
108 }