8ea14b781e841eec493342b0a08fbadb83eeb251
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_launcher_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 <glib.h>
7
8 #include <memory>
9
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_pump.h"
12 #include "base/message_loop/message_pump_glib.h"
13 #include "base/run_loop.h"
14
15 #include "xwalk/application/tools/linux/xwalk_launcher.h"
16 #if defined(OS_TIZEN)
17 #include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
18 #endif
19
20 namespace {
21
22 int g_argc;
23 char** g_argv;
24 gboolean query_running = FALSE;
25 gboolean fullscreen = FALSE;
26 gboolean remote_debugging = FALSE;
27 gchar** cmd_appid_or_url;
28 char* application_object_path;
29
30 }  // namespace
31
32 static const GOptionEntry entries[] {
33   { "running", 'r', 0, G_OPTION_ARG_NONE, &query_running,
34     "Check whether the application is running", nullptr },
35   { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen,
36     "Run the application as fullscreen", nullptr },
37   { "debugging_port", 'd', 0, G_OPTION_ARG_NONE, &remote_debugging,
38     "Enable remote debugging for the application", nullptr },
39   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
40     &cmd_appid_or_url,
41     "ID of the application to be launched or URL to open", nullptr },
42   { nullptr }
43 };
44
45 int main(int argc, char** argv) {
46 #if !GLIB_CHECK_VERSION(2, 36, 0)
47   // g_type_init() is deprecated on GLib since 2.36.
48   g_type_init();
49 #endif
50
51 #if defined(OS_TIZEN)
52   if (xwalk_tizen_check_group_users())
53     return 1;
54 #endif
55   base::MessageLoop msg_loop(
56       make_scoped_ptr<base::MessagePump>(new base::MessagePumpGlib()));
57
58   g_argc = argc;
59   g_argv = argv;
60
61   GError* error = nullptr;
62   GOptionContext* context =
63       g_option_context_new("- Crosswalk Application Launcher");
64   g_option_context_add_main_entries(context, entries, nullptr);
65   if (!g_option_context_parse(context, &argc, &argv, &error)) {
66     LOG(ERROR) << "Option parsing failed: " << error->message;
67     exit(1);
68   }
69
70   std::string appid_or_url;
71   if (!strcmp(basename(g_argv[0]), "xwalk-launcher")) {
72     if (!cmd_appid_or_url) {
73       LOG(ERROR) << "No AppID informed, nothing to do.";
74       exit(1);
75     }
76     appid_or_url = std::string(cmd_appid_or_url[0]);
77   } else {
78     appid_or_url = std::string(basename(g_argv[0]));
79   }
80
81   std::unique_ptr<XWalkLauncher> launcher;
82 #if defined(OS_TIZEN)
83   launcher.reset(new XWalkLauncherTizen(query_running, &msg_loop));
84 #else
85   launcher.reset(new XWalkLauncher(query_running, &msg_loop));
86 #endif
87   int result = launcher->Launch(appid_or_url, fullscreen, remote_debugging,
88                                 argc, argv);
89   if (!result)
90     msg_loop.Run();
91   return result;
92 }