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.
13 #include "base/at_exit.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
18 #include "dbus/message.h"
19 #include "dbus/object_proxy.h"
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"
26 #include "xwalk/application/common/id_util.h"
27 #include "xwalk/application/tools/linux/xwalk_tizen_user.h"
30 using xwalk::application::ApplicationData;
31 using xwalk::application::ApplicationStorage;
32 using xwalk::application::PackageInstaller;
34 static char* install_path;
35 static char* uninstall_appid;
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" },
45 #if defined(SHARED_PROCESS_MODE)
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");
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");
61 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
62 dbus::ObjectProxy* app_proxy =
63 bus->GetObjectProxy(xwalk_service_name, kRunningManagerDBusPath);
67 dbus::MethodCall method_call(
68 xwalk_running_manager_iface, "TerminateIfRunning");
69 dbus::MessageWriter writer(&method_call);
70 writer.AppendString(app_id);
72 app_proxy->CallMethodAndBlock(&method_call, 1000);
76 bool list_applications(ApplicationStorage* storage) {
77 std::vector<std::string> app_ids;
78 if (!storage->GetInstalledApplicationIDs(app_ids))
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));
87 g_print("Failed to obtain app data for xwalk id: %s\n",
88 app_ids.at(i).c_str());
93 GetTizenAppId(app_data).c_str(),
94 app_data->Name().c_str());
96 g_print("%s %s\n", app_data->ID().c_str(), app_data->Name().c_str());
99 g_print("-----------------------------------------------------\n");
104 int main(int argc, char* argv[]) {
105 setlocale(LC_ALL, "");
106 GError* error = NULL;
107 GOptionContext* context;
108 bool success = false;
110 #if !GLIB_CHECK_VERSION(2, 36, 0)
111 // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
115 #if defined(OS_TIZEN)
116 if (xwalk_tizen_check_user_app())
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);
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());
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);
143 } else if (uninstall_appid) {
144 #if defined(SHARED_PROCESS_MODE)
145 TerminateIfRunning(uninstall_appid);
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());
152 success = installer->Uninstall(uninstall_appid);
154 success = list_applications(storage.get());
157 return success ? 0 : 1;