Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalkctl_main.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 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9
10 #include <glib.h>
11 #include <gio/gio.h>
12 #include <locale.h>
13
14 #include "base/at_exit.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17
18 #include "dbus/bus.h"
19 #include "dbus/message.h"
20 #include "dbus/object_proxy.h"
21
22 #include "xwalk/application/common/application_storage.h"
23 #include "xwalk/application/tools/linux/dbus_connection.h"
24 #include "xwalk/application/tools/tizen/xwalk_package_installer.h"
25 #include "xwalk/runtime/common/xwalk_paths.h"
26
27 #include "xwalk/application/common/id_util.h"
28 #include "xwalk/application/tools/tizen/xwalk_tizen_user.h"
29
30 using xwalk::application::ApplicationData;
31 using xwalk::application::ApplicationStorage;
32
33 namespace {
34
35 char* install_path = NULL;
36 char* uninstall_id = NULL;
37 char* reinstall_path = NULL;
38 char* operation_key = NULL;
39 int quiet = 0;
40
41 gint debugging_port = -1;
42 gboolean continue_tasks = FALSE;
43
44 GOptionEntry entries[] = {
45   { "install", 'i', 0, G_OPTION_ARG_STRING, &install_path,
46     "Path of the application to be installed/updated", "PATH" },
47   { "uninstall", 'u', 0, G_OPTION_ARG_STRING, &uninstall_id,
48     "Uninstall the application with this appid/pkgid", "ID" },
49   { "debugging_port", 'd', 0, G_OPTION_ARG_INT, &debugging_port,
50     "Enable remote debugging, port number 0 means to disable", NULL },
51   { "continue", 'c' , 0, G_OPTION_ARG_NONE, &continue_tasks,
52     "Continue the previous unfinished tasks.", NULL},
53   { "reinstall", 'r', 0, G_OPTION_ARG_STRING, &reinstall_path,
54     "Reinstall the application with path", "PATH" },
55   { "key", 'k', 0, G_OPTION_ARG_STRING, &operation_key,
56     "Unique operation key", "KEY" },
57   { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
58     "Quiet mode", NULL },
59   { NULL }
60 };
61
62 }  // namespace
63
64 #if defined(SHARED_PROCESS_MODE)
65 namespace {
66
67 const char xwalk_service_name[] = "org.crosswalkproject.Runtime1";
68 const char xwalk_running_manager_iface[] =
69     "org.crosswalkproject.Running.Manager1";
70 const dbus::ObjectPath kRunningManagerDBusPath("/running1");
71
72 bool enable_remote_debugging(gint debugging_port) {
73   dbus::Bus::Options options;
74 #if defined(OS_TIZEN_MOBILE)
75   options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
76   options.address.assign("unix:path=/run/user/app/dbus/user_bus_socket");
77 #endif
78   scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
79   dbus::ObjectProxy* app_proxy =
80       bus->GetObjectProxy(
81           xwalk_service_name,
82           kRunningManagerDBusPath);
83   if (!app_proxy)
84     return false;
85
86   dbus::MethodCall method_call(
87       xwalk_running_manager_iface, "EnableRemoteDebugging");
88   dbus::MessageWriter writer(&method_call);
89   writer.AppendUint32(debugging_port);
90
91   app_proxy->CallMethodAndBlock(&method_call, 1000);
92
93   if (debugging_port > 0) {
94     g_print("Remote debugging enabled at port '%d'\n", debugging_port);
95   } else {
96     g_print("Remote debugging has been disabled\n");
97   }
98   return true;
99 }
100
101 }  // namespace
102 #endif
103
104 bool list_applications(ApplicationStorage* storage) {
105   std::vector<std::string> app_ids;
106   if (!storage->GetInstalledApplicationIDs(app_ids))
107     return false;
108
109   g_print("Application ID                       Application Name\n");
110   g_print("-----------------------------------------------------\n");
111   for (unsigned i = 0; i < app_ids.size(); ++i) {
112     scoped_refptr<ApplicationData> app_data =
113         storage->GetApplicationData(app_ids.at(i));
114     if (!app_data) {
115       g_print("Failed to obtain app data for xwalk id: %s\n",
116               app_ids.at(i).c_str());
117       continue;
118     }
119     g_print("%s  %s\n", app_ids.at(i).c_str(), app_data->Name().c_str());
120   }
121   g_print("-----------------------------------------------------\n");
122
123   return true;
124 }
125
126 int main(int argc, char* argv[]) {
127   setlocale(LC_ALL, "");
128   GError* error = NULL;
129   GOptionContext* context;
130   bool success = false;
131
132 #if !GLIB_CHECK_VERSION(2, 36, 0)
133   // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
134   g_type_init();
135 #endif
136
137   if (xwalk_tizen_check_user_app())
138     exit(1);
139
140   context = g_option_context_new("- Crosswalk Application Management");
141   if (!context) {
142     g_print("g_option_context_new failed\n");
143     exit(1);
144   }
145   g_option_context_add_main_entries(context, entries, NULL);
146   if (!g_option_context_parse(context, &argc, &argv, &error)) {
147     g_print("option parsing failed: %s\n", error->message);
148     g_option_context_free(context);
149     exit(1);
150   }
151
152   g_option_context_free(context);
153
154   base::AtExitManager at_exit;
155   base::FilePath data_path;
156   xwalk::RegisterPathProvider();
157   PathService::Get(xwalk::DIR_DATA_PATH, &data_path);
158   scoped_ptr<ApplicationStorage> storage(new ApplicationStorage(data_path));
159   scoped_ptr<PackageInstaller> installer =
160       PackageInstaller::Create(storage.get());
161
162   installer->SetQuiet(static_cast<bool>(quiet));
163   if (operation_key)
164     installer->SetInstallationKey(operation_key);
165
166   if (continue_tasks) {
167     g_print("trying to continue previous unfinished tasks.\n");
168     installer->ContinueUnfinishedTasks();
169     success = true;
170     g_print("Previous tasks have been finished.\n");
171   }
172
173   if (install_path) {
174     std::string app_id;
175     const base::FilePath& path = base::FilePath(install_path);
176     success = installer->Install(path, &app_id);
177     if (!success && storage->Contains(app_id)) {
178       g_print("trying to update %s\n", app_id.c_str());
179       success = installer->Update(app_id, path);
180     }
181   } else if (uninstall_id) {
182     success = installer->Uninstall(uninstall_id);
183   } else if (reinstall_path) {
184     success = installer->Reinstall(base::FilePath(reinstall_path));
185   } else if (debugging_port >= 0) {
186 #if defined(SHARED_PROCESS_MODE)
187     // Deal with the case "xwalkctl -d PORT_NUMBER"
188     success = enable_remote_debugging(debugging_port);
189 #else
190     g_print("Couldn't enable remote debugging for no shared process mode!");
191 #endif
192   } else if (!continue_tasks) {
193     success = list_applications(storage.get());
194   }
195
196   return success ? 0 : 1;
197 }