Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_browser_main_parts.cc
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.
4
5 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
6
7 #include <stdlib.h>
8
9 #include <set>
10
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "xwalk/application/browser/application.h"
18 #include "xwalk/application/browser/application_system.h"
19 #include "xwalk/extensions/browser/xwalk_extension_service.h"
20 #include "xwalk/extensions/common/xwalk_extension_switches.h"
21 #include "xwalk/runtime/browser/devtools/remote_debugging_server.h"
22 #include "xwalk/runtime/browser/runtime.h"
23 #include "xwalk/runtime/browser/runtime_context.h"
24 #include "xwalk/runtime/browser/xwalk_runner.h"
25 #include "xwalk/runtime/common/xwalk_runtime_features.h"
26 #include "xwalk/runtime/common/xwalk_switches.h"
27 #include "cc/base/switches.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/main_function_params.h"
30 #include "content/public/common/url_constants.h"
31 #include "content/public/common/result_codes.h"
32 #include "net/base/net_util.h"
33 #include "ui/gl/gl_switches.h"
34
35 #if defined(USE_AURA) && defined(USE_X11)
36 #include "ui/base/ime/input_method_initializer.h"
37 #include "ui/events/x/touch_factory_x11.h"
38 #endif
39
40 namespace {
41
42 // FIXME: Compare with method in startup_browser_creator.cc.
43 GURL GetURLFromCommandLine(const CommandLine& command_line) {
44   const CommandLine::StringVector& args = command_line.GetArgs();
45
46   if (args.empty())
47     return GURL();
48
49   GURL url(args[0]);
50   if (url.is_valid() && url.has_scheme())
51     return url;
52
53   base::FilePath path(args[0]);
54   if (!path.IsAbsolute())
55     path = MakeAbsoluteFilePath(path);
56
57   return net::FilePathToFileURL(path);
58 }
59
60 }  // namespace
61
62 namespace xswitches {
63 // Redefine settings not exposed by content module.
64 const char kEnableViewport[] = "enable-viewport";
65 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
66 }
67
68 namespace xwalk {
69
70 XWalkBrowserMainParts::XWalkBrowserMainParts(
71     const content::MainFunctionParams& parameters)
72     : xwalk_runner_(XWalkRunner::GetInstance()),
73       startup_url_(content::kAboutBlankURL),
74       parameters_(parameters),
75       run_default_message_loop_(true) {
76 #if defined(OS_LINUX)
77   // FIXME: We disable the setuid sandbox on Linux because we don't ship
78   // the setuid binary. It is important to remember that the seccomp-bpf
79   // sandbox is still fully operational if supported by the kernel. See
80   // issue #496.
81   //
82   // switches::kDisableSetuidSandbox is not being used here because it
83   // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
84   // content_switches.h.
85   CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
86 #endif
87 }
88
89 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
90 }
91
92 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
93   CommandLine* command_line = CommandLine::ForCurrentProcess();
94   command_line->AppendSwitch(xswitches::kEnableViewport);
95
96   command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
97
98   // Enable multithreaded GPU compositing of web content.
99   // This also enables pinch on Tizen.
100   command_line->AppendSwitch(switches::kEnableThreadedCompositing);
101
102   // Show feedback on touch.
103   command_line->AppendSwitch(switches::kEnableGestureTapHighlight);
104
105   // FIXME: Add comment why this is needed on Android and Tizen.
106   command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
107
108   // Enable SIMD.JS API by default.
109   std::string js_flags("--simd_object");
110   if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
111     js_flags += " ";
112     js_flags +=
113         command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
114   }
115   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);
116
117   startup_url_ = GetURLFromCommandLine(*command_line);
118 }
119
120 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
121 }
122
123 void XWalkBrowserMainParts::PreEarlyInitialization() {
124 #if defined(USE_AURA) && defined(USE_X11)
125     ui::InitializeInputMethodForTesting();
126 #endif
127 }
128
129 int XWalkBrowserMainParts::PreCreateThreads() {
130   return content::RESULT_CODE_NORMAL_EXIT;
131 }
132
133 void XWalkBrowserMainParts::RegisterExternalExtensions() {
134   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
135
136 #if defined(OS_TIZEN)
137   static const std::string tec_path = "/usr/lib/tizen-extensions-crosswalk";
138   std::string value = cmd_line->GetSwitchValueASCII(
139       switches::kXWalkExternalExtensionsPath);
140
141   if (value.empty())
142     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
143         tec_path);
144   else if (value != tec_path)
145     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
146         tec_path;
147
148   cmd_line->AppendSwitch(
149         switches::kXWalkAllowExternalExtensionsForRemoteSources);
150 #else
151   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
152     return;
153 #endif
154
155   if (!cmd_line->HasSwitch(
156           switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
157       !startup_url_.SchemeIsFile()) {
158     VLOG(0) << "Unsupported scheme for external extensions: " <<
159           startup_url_.scheme();
160     return;
161   }
162
163   base::FilePath extensions_dir =
164       cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
165   if (!base::DirectoryExists(extensions_dir)) {
166     LOG(WARNING) << "Ignoring non-existent extension directory: "
167                  << extensions_dir.AsUTF8Unsafe();
168     return;
169   }
170
171   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
172 }
173
174 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
175   xwalk_runner_->PreMainMessageLoopRun();
176
177   extension_service_ = xwalk_runner_->extension_service();
178
179   if (extension_service_)
180     RegisterExternalExtensions();
181
182   CommandLine* command_line = CommandLine::ForCurrentProcess();
183   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
184     std::string port_str =
185         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
186     int port;
187     const char* local_ip = "0.0.0.0";
188     if (base::StringToInt(port_str, &port) && port > 0 && port < 65535) {
189       remote_debugging_server_.reset(
190           new RemoteDebuggingServer(xwalk_runner_->runtime_context(),
191               local_ip, port, std::string()));
192     }
193   }
194
195   NativeAppWindow::Initialize();
196
197   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
198     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
199     run_default_message_loop_ = false;
200     return;
201   }
202
203   if (xwalk_runner_->is_running_as_service()) {
204     // In service mode, Crosswalk doesn't launch anything, just waits
205     // for external requests to launch apps.
206     VLOG(1) << "Crosswalk running as Service.";
207     return;
208   }
209
210   application::ApplicationSystem* app_system = xwalk_runner_->app_system();
211   if (!app_system->HandleApplicationManagementCommands(*command_line,
212       startup_url_, run_default_message_loop_)) {
213     app_system->LaunchFromCommandLine(*command_line, startup_url_,
214                                       run_default_message_loop_);
215   }
216
217   // If the |ui_task| is specified in main function parameter, it indicates
218   // that we will run this UI task instead of running the the default main
219   // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
220   // case.
221   if (parameters_.ui_task) {
222     parameters_.ui_task->Run();
223     delete parameters_.ui_task;
224     run_default_message_loop_ = false;
225   }
226 }
227
228 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
229   return !run_default_message_loop_;
230 }
231
232 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
233   xwalk_runner_->PostMainMessageLoopRun();
234 }
235
236 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
237     content::RenderProcessHost* host,
238     extensions::XWalkExtensionVector* extensions) {
239 }
240
241 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
242     content::RenderProcessHost* host,
243     extensions::XWalkExtensionVector* extensions) {
244 }
245
246 }  // namespace xwalk