Upstream version 9.37.195.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 <gio/gio.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <appcore/appcore-common.h>
11 #include <pkgmgr-info.h>
12
13 #include "xwalk/application/common/id_util.h"
14 #include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
15
16 enum app_event {
17   AE_UNKNOWN,
18   AE_CREATE,
19   AE_TERMINATE,
20   AE_PAUSE,
21   AE_RESUME,
22   AE_RESET,
23   AE_LOWMEM_POST,
24   AE_MEM_FLUSH,
25   AE_MAX
26 };
27
28 // Private struct from appcore-internal, necessary to get events from
29 // the system.
30 struct ui_ops {
31   void* data;
32   void (*cb_app)(enum app_event evnt, void* data, bundle* b);
33 };
34
35 static struct ui_ops appcore_ops;
36
37 static const char* event2str(enum app_event event) {
38   switch (event) {
39     case AE_UNKNOWN:
40       return "AE_UNKNOWN";
41     case AE_CREATE:
42       return "AE_CREATE";
43     case AE_TERMINATE:
44       return "AE_TERMINATE";
45     case AE_PAUSE:
46       return "AE_PAUSE";
47     case AE_RESUME:
48       return "AE_RESUME";
49     case AE_RESET:
50       return "AE_RESET";
51     case AE_LOWMEM_POST:
52       return "AE_LOWMEM_POST";
53     case AE_MEM_FLUSH:
54       return "AE_MEM_FLUSH";
55     case AE_MAX:
56       return "AE_MAX";
57   }
58
59   return "INVALID EVENT";
60 }
61
62 static void application_event_cb(enum app_event event, void* data, bundle* b) {
63   fprintf(stderr, "event '%s'\n", event2str(event));
64   GDBusProxy* app_proxy = reinterpret_cast<GDBusProxy*>(data);
65
66   if (!app_proxy) {
67     fprintf(stderr, "Invalid DBus proxy.");
68     return;
69   }
70
71   switch (event) {
72     case AE_UNKNOWN:
73     case AE_CREATE:
74       break;
75     case AE_TERMINATE:
76       exit(0);
77       break;
78     case AE_PAUSE:
79       g_dbus_proxy_call(
80           app_proxy, "Suspend", NULL,
81           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
82       break;
83     case AE_RESUME:
84       g_dbus_proxy_call(
85           app_proxy, "Resume", NULL,
86           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
87       break;
88     case AE_RESET:
89     case AE_LOWMEM_POST:
90     case AE_MEM_FLUSH:
91     case AE_MAX:
92       break;
93   }
94 }
95
96 int xwalk_appcore_init(
97     int argc, char** argv, const char* name, GDBusProxy* app_proxy) {
98   appcore_ops.cb_app = application_event_cb;
99   appcore_ops.data = app_proxy;
100
101   return appcore_init(name, &appcore_ops, argc, argv);
102 }
103
104 int xwalk_change_cmdline(int argc, char** argv, const char* app_id) {
105   // Change /proc/<pid>/cmdline to app exec path. See XWALK-1722 for details.
106   pkgmgrinfo_appinfo_h handle;
107   char* exec_path = NULL;
108   if (pkgmgrinfo_appinfo_get_appinfo(app_id, &handle) != PMINFO_R_OK ||
109       pkgmgrinfo_appinfo_get_exec(handle, &exec_path) != PMINFO_R_OK ||
110       !exec_path) {
111     fprintf(stderr, "Couldn't find exec path for application: %s\n", app_id);
112     return -1;
113   }
114
115   for (int i = 0; i < argc; ++i)
116     memset(argv[i], 0, strlen(argv[i]));
117   strncpy(argv[0], exec_path, strlen(exec_path)+1);
118   pkgmgrinfo_appinfo_destroy_appinfo(handle);
119   return 0;
120 }