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.
14 #include "base/at_exit.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
19 #include "dbus/message.h"
20 #include "dbus/object_proxy.h"
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"
27 #include "xwalk/application/common/id_util.h"
28 #include "xwalk/application/tools/tizen/xwalk_tizen_user.h"
30 using xwalk::application::ApplicationData;
31 using xwalk::application::ApplicationStorage;
35 char* install_path = NULL;
36 char* uninstall_id = NULL;
37 char* reinstall_path = NULL;
38 char* operation_key = NULL;
41 gint debugging_port = -1;
42 gboolean continue_tasks = FALSE;
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,
64 #if defined(SHARED_PROCESS_MODE)
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");
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");
78 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
79 dbus::ObjectProxy* app_proxy =
82 kRunningManagerDBusPath);
86 dbus::MethodCall method_call(
87 xwalk_running_manager_iface, "EnableRemoteDebugging");
88 dbus::MessageWriter writer(&method_call);
89 writer.AppendUint32(debugging_port);
91 app_proxy->CallMethodAndBlock(&method_call, 1000);
93 if (debugging_port > 0) {
94 g_print("Remote debugging enabled at port '%d'\n", debugging_port);
96 g_print("Remote debugging has been disabled\n");
104 bool list_applications(ApplicationStorage* storage) {
105 std::vector<std::string> app_ids;
106 if (!storage->GetInstalledApplicationIDs(app_ids))
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));
115 g_print("Failed to obtain app data for xwalk id: %s\n",
116 app_ids.at(i).c_str());
119 g_print("%s %s\n", app_ids.at(i).c_str(), app_data->Name().c_str());
121 g_print("-----------------------------------------------------\n");
126 int main(int argc, char* argv[]) {
127 setlocale(LC_ALL, "");
128 GError* error = NULL;
129 GOptionContext* context;
130 bool success = false;
132 #if !GLIB_CHECK_VERSION(2, 36, 0)
133 // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
137 if (xwalk_tizen_check_user_app())
140 context = g_option_context_new("- Crosswalk Application Management");
142 g_print("g_option_context_new failed\n");
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);
152 g_option_context_free(context);
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());
162 installer->SetQuiet(static_cast<bool>(quiet));
164 installer->SetInstallationKey(operation_key);
166 if (continue_tasks) {
167 g_print("trying to continue previous unfinished tasks.\n");
168 installer->ContinueUnfinishedTasks();
170 g_print("Previous tasks have been finished.\n");
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);
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);
190 g_print("Couldn't enable remote debugging for no shared process mode!");
192 } else if (!continue_tasks) {
193 success = list_applications(storage.get());
196 return success ? 0 : 1;