7b9c9737460a13211cdc8fbd7edd74755c692159
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / ubrowser / main.cc
1 // Copyright 2014 Samsung Electronics. 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 <ewk_chromium.h>
6 #include <ewk_main.h>
7 #include <Elementary.h>
8 #include <getopt.h>
9 #include <unistd.h>
10 #include <vector>
11 #if defined(OS_TIZEN)
12 #include <appfw/app.h>
13 #endif // OS_TIZEN
14
15 #include "browser.h"
16 #include "logger.h"
17
18 // Default command line flags added by default when
19 // ubrowser is started with --mobile|-m flag.
20 const char* kMobileFlags[] = {
21     "use-mobile-user-agent",
22     "enable-overlay-scrollbar",
23     "enable-viewport",
24     "enable-prefer-compositing-to-lcd-text",
25     "ewk-enable-mobile-features-for-desktop",
26 };
27
28 extern int logger_show_trace;
29 extern int logger_use_color;
30 #if defined(OS_TIZEN)
31 int desktop_ui = 0;
32 #else
33 int desktop_ui = 1;
34 #endif
35
36 struct AppData {
37   AppData() : browser(nullptr), ewk_initialized(false) { }
38
39   std::vector<std::string> urls;
40   Browser* browser;
41   bool ewk_initialized;
42 };
43
44 void show_help_and_exit(const char* app_name) {
45   printf("Usage: %s [OPTION]... [URL]...\n\n", app_name);
46   printf("Available options:\n");
47   printf("  -v, --verbose   Print verbose application logs\n");
48   printf("  -n, --no-color  Don't use colors in application logs\n");
49   printf("  -d, --desktop   Run application UI in desktop mode\n");
50   printf("  -m, --mobile    Run application UI in mobile mode\n");
51   printf("  -h, --help      Show this help message\n");
52   exit(0);
53 }
54
55 void parse_options(int argc, char** argv) {
56   int show_help = 0;
57   int c;
58
59   while (1) {
60     static struct option long_options[] = {
61       {"verbose",  no_argument, &logger_show_trace, 1},
62       {"no-color", no_argument, &logger_use_color,  0},
63       {"desktop",  no_argument, &desktop_ui, 1},
64       {"mobile",   no_argument, &desktop_ui, 0},
65       {"help",     no_argument, &show_help, 1},
66       {0, 0, 0, 0}
67     };
68
69     int option_index = 0;
70     c = getopt_long (argc, argv, "vndmh", long_options, &option_index);
71
72     if (c == -1)
73       break;
74
75     switch (c) {
76     case 'v':
77       logger_show_trace = 1;
78       break;
79     case 'n':
80       logger_use_color = 0;
81       break;
82     case 'd':
83       desktop_ui = 1;
84       break;
85     case 'm':
86       desktop_ui = 0;
87       break;
88     case 'h':
89       show_help = 1;
90       break;
91     default:
92       // Ignore EFL or chromium specific options,
93       // ewk_set_arguments should handle them
94       continue;
95     }
96   }
97
98   if (show_help)
99     show_help_and_exit(argv[0]);
100 }
101
102 static bool app_create(void* data) {
103   AppData* app_data = static_cast<AppData*>(data);
104
105   if (!ewk_init())
106     return false;
107
108   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
109
110   app_data->browser = new Browser(desktop_ui);
111   std::vector<std::string>::iterator it = app_data->urls.begin();
112   for (;it != app_data->urls.end(); ++it)
113     app_data->browser->CreateWindow().LoadURL(*it);
114   app_data->urls.clear();
115   app_data->ewk_initialized = true;
116   return true;
117 }
118
119 static void app_terminate(void* data) {
120   AppData* app_data = static_cast<AppData*>(data);
121   if (app_data->browser)
122     delete app_data->browser;
123   if (app_data->ewk_initialized)
124     ewk_shutdown();
125 }
126
127 #if defined(OS_TIZEN) && defined(TIZEN_APP)
128 static void app_pause(void* data) {
129   log_debug("Tizen uBrowser pause");
130 }
131
132 static void app_resume(void* data) {
133   log_debug("Tizen uBrowser resume");
134 }
135 #endif // OS_TIZEN
136
137 int main(int argc, char** argv) {
138   AppData data;
139
140   for (int i = 1; i < argc; ++i)
141     if (argv[i][0] != '-')
142       data.urls.push_back(std::string(argv[i]));
143
144   if (data.urls.empty())
145     data.urls.push_back("http://www.google.com");
146
147   ewk_set_arguments(argc, argv);
148   elm_init(argc, argv);
149   elm_config_accel_preference_set("opengl");
150   parse_options(argc, argv);
151
152 // Do not propagate mobile flags for all TIZEN builds.
153 // They are controlled by command_line_efl.cc.
154 #if !defined(OS_TIZEN)
155   if (!desktop_ui) {
156     int size = sizeof(kMobileFlags) / sizeof(kMobileFlags[0]);
157     for (int i = 0; i < size; ++i)
158       ewk_chromium_append_command_line_flag(kMobileFlags[i]);
159   }
160 #endif
161
162   int ret = 0;
163 // Enable the section below once ubrowser is ready
164 // to replace mini_browser. For the code below to work
165 // proper manifest needs to be added to the rpm package
166 #if defined(OS_TIZEN) && defined(TIZEN_APP)
167   ui_app_lifecycle_callback_s ops;
168
169   memset(&ops, 0, sizeof(ops));
170   ops.create = app_create;
171   ops.terminate = app_terminate;
172   ops.pause = app_pause;
173   ops.resume = app_resume;
174
175   ret = ui_app_main(argc, argv, &ops, &data)
176 #else
177   if (!app_create(&data))
178     return EXIT_FAILURE;
179
180   elm_run();
181
182   app_terminate(&data);
183 #endif
184
185   return ret;
186 }