Upstream version 11.39.256.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalkctl_main.cc
1 // Copyright (c) 2014 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 <glib.h>
6 #include <gio/gio.h>
7
8 #include <limits>
9
10 #include "base/at_exit.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14
15 #include "xwalk/application/common/tizen/application_storage.h"
16 #include "xwalk/runtime/common/xwalk_paths.h"
17
18 #include "dbus/bus.h"
19 #include "dbus/message.h"
20 #include "dbus/object_proxy.h"
21
22 #if defined(OS_TIZEN)
23 #include "xwalk/application/tools/tizen/xwalk_tizen_user.h"
24 #endif
25
26 using xwalk::application::ApplicationData;
27 using xwalk::application::ApplicationStorage;
28
29 namespace {
30
31 const gint debugging_port_not_set = std::numeric_limits<gint>::min();
32
33 gint debugging_port = debugging_port_not_set;
34 GOptionEntry entries[] = {
35   { "debugging_port", 'd', 0, G_OPTION_ARG_INT, &debugging_port,
36     "Enable remote debugging, port number 0 means to disable", NULL },
37   { NULL }
38 };
39
40 const char kServiceName[] = "org.crosswalkproject.Runtime1";
41 const char kRunningManagerIface[] =
42     "org.crosswalkproject.Running.Manager1";
43 const dbus::ObjectPath kRunningManagerDBusPath("/running1");
44
45 bool EnableRemoteDebugging(int port) {
46   if (port < 0) {
47     g_print("Remote debugging port cannot be negative\n");
48     return false;
49   }
50   if (port >= 65535) {
51     DLOG(ERROR) << "Invalid http debugger port number " << port;
52     return false;
53   }
54
55   dbus::Bus::Options options;
56 #if defined(OS_TIZEN_MOBILE)
57   options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
58   options.address.assign("unix:path=/run/user/app/dbus/user_bus_socket");
59 #endif
60   scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
61   dbus::ObjectProxy* app_proxy =
62       bus->GetObjectProxy(
63           kServiceName,
64           kRunningManagerDBusPath);
65   if (!app_proxy) {
66     DLOG(ERROR) << "Failed to get application proxy.";
67     return false;
68   }
69
70   dbus::MethodCall method_call(
71       kRunningManagerIface, "EnableRemoteDebugging");
72   dbus::MessageWriter writer(&method_call);
73   writer.AppendUint32(port);
74
75   app_proxy->CallMethodAndBlock(&method_call, 1000);
76
77   if (!port)
78     g_print("Remote debugging has been disabled \n");
79   else
80     g_print("Remote debugging enabled at port %d \n", port);
81   return true;
82 }
83
84 bool list_applications(ApplicationStorage* storage) {
85   std::vector<std::string> app_ids;
86   if (!storage->GetInstalledApplicationIDs(app_ids))
87     return false;
88
89   g_print("Application ID                       Application Name\n");
90   g_print("-----------------------------------------------------\n");
91   for (auto id : app_ids) {
92     scoped_refptr<ApplicationData> app_data = storage->GetApplicationData(id);
93     if (!app_data.get()) {
94       g_print("Failed to obtain app data for xwalk id: %s\n",
95               id.c_str());
96       continue;
97     }
98     g_print("%s  %s\n", id.c_str(), app_data->Name().c_str());
99   }
100   g_print("-----------------------------------------------------\n");
101
102   return true;
103 }
104
105 }  // namespace
106
107 int main(int argc, char* argv[]) {
108 #if defined(OS_TIZEN)
109   if (xwalk_tizen_check_group_users())
110     return 1;
111 #endif
112   GOptionContext* context = g_option_context_new("- Crosswalk Setter");
113   g_option_context_add_main_entries(context, entries, NULL);
114   GError* error = nullptr;
115   if (!g_option_context_parse(context, &argc, &argv, &error)) {
116     g_print("option parsing failed: %s\n", error->message);
117     g_option_context_free(context);
118     return 1;
119   }
120   g_option_context_free(context);
121   base::AtExitManager at_exit;
122
123   if (debugging_port != debugging_port_not_set) {
124     if (!EnableRemoteDebugging(static_cast<int>(debugging_port)))
125       return 1;
126   } else {
127     // FIXME : there should be a way to get application Id from platform, so
128     // the below code should not be needed.
129     xwalk::RegisterPathProvider();
130     base::FilePath data_path;
131     PathService::Get(xwalk::DIR_DATA_PATH, &data_path);
132     scoped_ptr<ApplicationStorage> storage(new ApplicationStorage(data_path));
133     if (!list_applications(storage.get()))
134       return 1;
135   }
136
137   return 0;
138 }