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.
5 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
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 "cc/base/switches.h"
18 #include "components/nacl/browser/nacl_browser.h"
19 #include "components/nacl/browser/nacl_process_host.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/main_function_params.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/common/result_codes.h"
25 #include "extensions/browser/extension_system.h"
26 #include "net/base/net_util.h"
27 #include "ui/gl/gl_switches.h"
28 #include "xwalk/application/browser/application.h"
29 #include "xwalk/application/browser/application_system.h"
30 #include "xwalk/extensions/browser/xwalk_extension_service.h"
31 #include "xwalk/extensions/common/xwalk_extension_switches.h"
32 #include "xwalk/runtime/browser/devtools/remote_debugging_server.h"
33 #include "xwalk/runtime/browser/nacl_host/nacl_browser_delegate_impl.h"
34 #include "xwalk/runtime/browser/runtime.h"
35 #include "xwalk/runtime/browser/runtime_context.h"
36 #include "xwalk/runtime/browser/xwalk_runner.h"
37 #include "xwalk/runtime/common/xwalk_runtime_features.h"
38 #include "xwalk/runtime/common/xwalk_switches.h"
40 #if defined(USE_AURA) && defined(USE_X11)
41 #include "ui/base/ime/input_method_initializer.h"
42 #include "ui/events/x/touch_factory_x11.h"
47 // FIXME: Compare with method in startup_browser_creator.cc.
48 GURL GetURLFromCommandLine(const CommandLine& command_line) {
49 const CommandLine::StringVector& args = command_line.GetArgs();
55 if (url.is_valid() && url.has_scheme())
58 base::FilePath path(args[0]);
59 if (!path.IsAbsolute())
60 path = MakeAbsoluteFilePath(path);
62 return net::FilePathToFileURL(path);
68 // Redefine settings not exposed by content module.
69 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
74 XWalkBrowserMainParts::XWalkBrowserMainParts(
75 const content::MainFunctionParams& parameters)
76 : xwalk_runner_(XWalkRunner::GetInstance()),
77 startup_url_(content::kAboutBlankURL),
78 parameters_(parameters),
79 run_default_message_loop_(true) {
81 // FIXME: We disable the setuid sandbox on Linux because we don't ship
82 // the setuid binary. It is important to remember that the seccomp-bpf
83 // sandbox is still fully operational if supported by the kernel. See
86 // switches::kDisableSetuidSandbox is not being used here because it
87 // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
88 // content_switches.h.
89 CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
93 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
96 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
97 CommandLine* command_line = CommandLine::ForCurrentProcess();
98 command_line->AppendSwitch(switches::kEnableViewport);
99 command_line->AppendSwitch(switches::kEnableViewportMeta);
101 command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
103 // Enable multithreaded GPU compositing of web content.
104 // This also enables pinch on Tizen.
105 command_line->AppendSwitch(switches::kEnableThreadedCompositing);
107 // Show feedback on touch.
108 command_line->AppendSwitch(switches::kEnableGestureTapHighlight);
110 // FIXME: Add comment why this is needed on Android and Tizen.
111 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
113 // Enable SIMD.JS API by default.
114 std::string js_flags("--simd_object");
115 if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
118 command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
120 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);
122 startup_url_ = GetURLFromCommandLine(*command_line);
125 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
128 void XWalkBrowserMainParts::PreEarlyInitialization() {
129 #if defined(USE_AURA) && defined(USE_X11)
130 ui::InitializeInputMethodForTesting();
134 int XWalkBrowserMainParts::PreCreateThreads() {
135 return content::RESULT_CODE_NORMAL_EXIT;
138 void XWalkBrowserMainParts::RegisterExternalExtensions() {
139 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
141 #if defined(OS_TIZEN)
142 static const std::string tec_path = "/usr/lib/tizen-extensions-crosswalk";
143 std::string value = cmd_line->GetSwitchValueASCII(
144 switches::kXWalkExternalExtensionsPath);
147 cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
149 else if (value != tec_path)
150 VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
153 cmd_line->AppendSwitch(
154 switches::kXWalkAllowExternalExtensionsForRemoteSources);
156 if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
160 if (!cmd_line->HasSwitch(
161 switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
162 (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
163 VLOG(0) << "Unsupported scheme for external extensions: " <<
164 startup_url_.scheme();
168 base::FilePath extensions_dir =
169 cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
170 if (!base::DirectoryExists(extensions_dir)) {
171 LOG(WARNING) << "Ignoring non-existent extension directory: "
172 << extensions_dir.AsUTF8Unsafe();
176 extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
179 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
180 xwalk_runner_->PreMainMessageLoopRun();
182 extension_service_ = xwalk_runner_->extension_service();
184 if (extension_service_)
185 RegisterExternalExtensions();
187 #if !defined(DISABLE_NACL)
188 NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
189 nacl::NaClBrowser::SetDelegate(delegate);
191 content::BrowserThread::PostTask(
192 content::BrowserThread::IO,
194 base::Bind(nacl::NaClProcessHost::EarlyStartup));
197 CommandLine* command_line = CommandLine::ForCurrentProcess();
198 if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
199 std::string port_str =
200 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
202 const char* local_ip = "0.0.0.0";
203 if (base::StringToInt(port_str, &port) && port > 0 && port < 65535) {
204 remote_debugging_server_.reset(
205 new RemoteDebuggingServer(xwalk_runner_->runtime_context(),
206 local_ip, port, std::string()));
210 NativeAppWindow::Initialize();
212 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
213 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
214 run_default_message_loop_ = false;
218 if (xwalk_runner_->is_running_as_service()) {
219 // In service mode, Crosswalk doesn't launch anything, just waits
220 // for external requests to launch apps.
221 VLOG(1) << "Crosswalk running as Service.";
225 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
226 if (!app_system->HandleApplicationManagementCommands(*command_line,
227 startup_url_, run_default_message_loop_)) {
228 app_system->LaunchFromCommandLine(*command_line, startup_url_,
229 run_default_message_loop_);
232 // If the |ui_task| is specified in main function parameter, it indicates
233 // that we will run this UI task instead of running the the default main
234 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
236 if (parameters_.ui_task) {
237 parameters_.ui_task->Run();
238 delete parameters_.ui_task;
239 run_default_message_loop_ = false;
243 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
244 return !run_default_message_loop_;
247 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
248 xwalk_runner_->PostMainMessageLoopRun();
251 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
252 content::RenderProcessHost* host,
253 extensions::XWalkExtensionVector* extensions) {
256 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
257 content::RenderProcessHost* host,
258 extensions::XWalkExtensionVector* extensions) {