Upstream version 8.37.183.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalkctl_main.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 <stdlib.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8
9 #include <glib.h>
10 #include <gio/gio.h>
11 #include <locale.h>
12
13 #include "base/at_exit.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16
17 #include "dbus/bus.h"
18 #include "dbus/message.h"
19 #include "dbus/object_proxy.h"
20
21 #include "xwalk/application/common/application_storage.h"
22 #include "xwalk/application/common/installer/package_installer.h"
23 #include "xwalk/application/tools/linux/dbus_connection.h"
24 #include "xwalk/runtime/common/xwalk_paths.h"
25 #if defined(OS_TIZEN)
26 #include "xwalk/application/common/id_util.h"
27 #include "xwalk/application/tools/linux/xwalk_tizen_user.h"
28 #endif
29
30 using xwalk::application::ApplicationData;
31 using xwalk::application::ApplicationStorage;
32 using xwalk::application::PackageInstaller;
33
34 static char* install_path;
35 static char* uninstall_appid;
36
37 static GOptionEntry entries[] = {
38   { "install", 'i', 0, G_OPTION_ARG_STRING, &install_path,
39     "Path of the application to be installed/updated", "PATH" },
40   { "uninstall", 'u', 0, G_OPTION_ARG_STRING, &uninstall_appid,
41     "Uninstall the application with this appid", "APPID" },
42   { NULL }
43 };
44
45 #if defined(SHARED_PROCESS_MODE)
46 namespace {
47
48 const char xwalk_service_name[] = "org.crosswalkproject.Runtime1";
49 const char xwalk_running_manager_iface[] =
50     "org.crosswalkproject.Running.Manager1";
51 const dbus::ObjectPath kRunningManagerDBusPath("/running1");
52
53 }  // namespace
54
55 static void TerminateIfRunning(const std::string& app_id) {
56   dbus::Bus::Options options;
57 #if defined(OS_TIZEN_MOBILE)
58   options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
59   options.address.assign("unix:path=/run/user/app/dbus/user_bus_socket");
60 #endif
61   scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
62   dbus::ObjectProxy* app_proxy =
63       bus->GetObjectProxy(xwalk_service_name, kRunningManagerDBusPath);
64   if (!app_proxy)
65     return;
66
67   dbus::MethodCall method_call(
68       xwalk_running_manager_iface, "TerminateIfRunning");
69   dbus::MessageWriter writer(&method_call);
70   writer.AppendString(app_id);
71
72   app_proxy->CallMethodAndBlock(&method_call, 1000);
73 }
74 #endif
75
76 bool list_applications(ApplicationStorage* storage) {
77   std::vector<std::string> app_ids;
78   if (!storage->GetInstalledApplicationIDs(app_ids))
79     return false;
80
81   g_print("Application ID                       Application Name\n");
82   g_print("-----------------------------------------------------\n");
83   for (unsigned i = 0; i < app_ids.size(); ++i) {
84     scoped_refptr<ApplicationData> app_data =
85         storage->GetApplicationData(app_ids.at(i));
86     if (!app_data) {
87       g_print("Failed to obtain app data for xwalk id: %s\n",
88               app_ids.at(i).c_str());
89       continue;
90     }
91 #if defined(OS_TIZEN)
92     g_print("%s  %s\n",
93             GetTizenAppId(app_data).c_str(),
94             app_data->Name().c_str());
95 #else
96     g_print("%s  %s\n", app_data->ID().c_str(), app_data->Name().c_str());
97 #endif
98   }
99   g_print("-----------------------------------------------------\n");
100
101   return true;
102 }
103
104 int main(int argc, char* argv[]) {
105   setlocale(LC_ALL, "");
106   GError* error = NULL;
107   GOptionContext* context;
108   bool success = false;
109
110 #if !GLIB_CHECK_VERSION(2, 36, 0)
111   // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
112   g_type_init();
113 #endif
114
115 #if defined(OS_TIZEN)
116   if (xwalk_tizen_check_user_app())
117     exit(1);
118 #endif
119
120   context = g_option_context_new("- Crosswalk Application Management");
121   g_option_context_add_main_entries(context, entries, NULL);
122   if (!g_option_context_parse(context, &argc, &argv, &error)) {
123     g_print("option parsing failed: %s\n", error->message);
124     exit(1);
125   }
126
127   base::AtExitManager at_exit;
128   base::FilePath data_path;
129   xwalk::RegisterPathProvider();
130   PathService::Get(xwalk::DIR_DATA_PATH, &data_path);
131   scoped_ptr<ApplicationStorage> storage(new ApplicationStorage(data_path));
132   scoped_ptr<PackageInstaller> installer =
133       PackageInstaller::Create(storage.get());
134
135   if (install_path) {
136     std::string app_id;
137     const base::FilePath& path = base::FilePath(install_path);
138     success = installer->Install(path, &app_id);
139     if (!success && storage->Contains(app_id)) {
140       g_print("trying to update %s\n", app_id.c_str());
141       success = installer->Update(app_id, path);
142     }
143   } else if (uninstall_appid) {
144 #if defined(SHARED_PROCESS_MODE)
145     TerminateIfRunning(uninstall_appid);
146 #endif
147 #if defined(OS_TIZEN)
148     std::string crosswalk_app_id =
149         xwalk::application::RawAppIdToCrosswalkAppId(uninstall_appid);
150     uninstall_appid = strdup(crosswalk_app_id.c_str());
151 #endif
152     success = installer->Uninstall(uninstall_appid);
153   } else {
154     success = list_applications(storage.get());
155   }
156
157   return success ? 0 : 1;
158 }