Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalk_package_helper.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 // On Tizen installing applications requires super-user powers primarily because
7 // some pieces of information of the application are put on globally accessible
8 // locations. This helper will run with super-user powers (via suid) and will
9 // be called by Crosswalk (now running as a normal user) so all the activities
10 // that required 'root' access are done by a small code base.
11
12 #include <glib.h>
13 #include <gio/gio.h>
14
15 #include <string>
16
17 #include "base/files/file_path.h"
18 #include "base/file_util.h"
19 #include "xwalk/application/tools/tizen/xwalk_package_installer_helper.h"
20
21 namespace {
22
23 char* install_option = NULL;
24 char* update_option = NULL;
25 char* uninstall_option = NULL;
26 char* reinstall_option = NULL;
27 char* operation_key = NULL;
28 char* xml_path = NULL;
29 char* icon_path = NULL;
30 int quiet = 0;
31
32 GOptionEntry entries[] = {
33   { "install", 'i', 0, G_OPTION_ARG_STRING, &install_option,
34     "Path of the application to be installed", "APPID" },
35   { "update", 'u', 0, G_OPTION_ARG_STRING, &update_option,
36     "Path of the application to be updated", "APPID" },
37   { "uninstall", 'd', 0, G_OPTION_ARG_STRING, &uninstall_option,
38     "Uninstall the application with this appid", "APPID" },
39   { "reinstall", 'r', 0, G_OPTION_ARG_STRING, &reinstall_option,
40     "Path of the application to be reinstalled", "APPID" },
41   { "key", 'k', 0, G_OPTION_ARG_STRING, &operation_key,
42     "Unique operation key", "KEY" },
43   { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
44     "Quiet mode", NULL },
45   { "xml", 'x', 0, G_OPTION_ARG_STRING, &xml_path,
46     "Xml file", "XML_FILE" },
47   { "icon", 'y', 0, G_OPTION_ARG_STRING, &icon_path,
48     "Icon file", "ICON_FILE" },
49   { NULL }
50 };
51
52 }  // namespace
53
54 int main(int argc, char *argv[]) {
55   GError* error = NULL;
56   GOptionContext* context;
57
58 #if !GLIB_CHECK_VERSION(2, 36, 0)
59   // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
60   g_type_init();
61 #endif
62
63   context = g_option_context_new(
64         " - Crosswalk Tizen Application Installation helper");
65   if (!context) {
66     fprintf(stderr, "g_option_context_new failed\n");
67     exit(1);
68   }
69   g_option_context_add_main_entries(context, entries, NULL);
70   if (!g_option_context_parse(context, &argc, &argv, &error)) {
71     fprintf(stderr, "option parsing failed: %s\n", error->message);
72     g_option_context_free(context);
73     exit(1);
74   }
75   g_option_context_free(context);
76
77   bool result = false;
78
79   // args for pkgmgr
80   const char* pkgmgr_argv[5];
81   pkgmgr_argv[2] = "-k";
82   pkgmgr_argv[3] = operation_key;
83   pkgmgr_argv[4] = "-q";
84
85   char* appId = NULL;
86   if (install_option) {
87     appId = install_option;
88   } else if (uninstall_option) {
89     appId = uninstall_option;
90   } else if (update_option) {
91     appId = update_option;
92   } else if (reinstall_option) {
93     appId = reinstall_option;
94   } else {
95     fprintf(stderr, "Use: --install, --uninstall, --update or --reinstall\n");
96     exit(1);
97   }
98
99   PackageInstallerHelper helper(appId);
100
101   if (install_option) {
102     if (!xml_path || !icon_path) {
103       fprintf(stdout, "missing --xml or --icon option\n");
104       exit(1);
105     }
106
107      if (operation_key) {
108       pkgmgr_argv[0] = "-i";
109       pkgmgr_argv[1] = install_option;  // this value is ignored by pkgmgr
110       helper.InitializePkgmgrSignal((quiet ? 5 : 4), pkgmgr_argv);
111     }
112
113     result = helper.InstallApplication(xml_path, icon_path);
114   } else if (uninstall_option) {
115     if (operation_key) {
116       pkgmgr_argv[0] = "-d";
117       pkgmgr_argv[1] = uninstall_option;  // this value is ignored by pkgmgr
118       helper.InitializePkgmgrSignal((quiet ? 5 : 4), pkgmgr_argv);
119     }
120
121     result = helper.UninstallApplication();
122   } else if (update_option) {
123     if (!xml_path || !icon_path) {
124       fprintf(stderr, "missing --xml or --icon option\n");
125       exit(1);
126     }
127
128     if (operation_key) {
129       pkgmgr_argv[0] = "-i";
130       pkgmgr_argv[1] = update_option;  // this value is ignored by pkgmgr
131       helper.InitializePkgmgrSignal((quiet ? 5 : 4), pkgmgr_argv);
132     }
133
134     result = helper.UpdateApplication(xml_path, icon_path);
135   } else if (reinstall_option) {
136     if (operation_key) {
137       pkgmgr_argv[0] = "-r";
138       pkgmgr_argv[1] = reinstall_option;  // this value is ignored by pkgmgr
139       helper.InitializePkgmgrSignal((quiet ? 5 : 4), pkgmgr_argv);
140     }
141
142     result = helper.ReinstallApplication();
143   }
144
145   // Convention is to return 0 on success.
146   return result ? 0 : 1;
147 }