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/files/file_path.h"
14 #include "base/files/file_util.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 "content/public/browser/browser_thread.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/main_function_params.h"
21 #include "content/public/common/url_constants.h"
22 #include "content/public/common/result_codes.h"
23 #include "extensions/browser/extension_system.h"
24 #include "net/base/filename_util.h"
25 #include "ui/gl/gl_switches.h"
26 #include "xwalk/application/browser/application.h"
27 #include "xwalk/application/browser/application_system.h"
28 #include "xwalk/extensions/browser/xwalk_extension_service.h"
29 #include "xwalk/extensions/common/xwalk_extension_switches.h"
30 #include "xwalk/runtime/browser/xwalk_runner.h"
31 #include "xwalk/runtime/common/xwalk_runtime_features.h"
32 #include "xwalk/runtime/common/xwalk_switches.h"
34 #if !defined(DISABLE_NACL)
35 #include "components/nacl/browser/nacl_browser.h"
36 #include "components/nacl/browser/nacl_process_host.h"
37 #include "xwalk/runtime/browser/nacl_host/nacl_browser_delegate_impl.h"
40 #if defined(USE_AURA) && defined(USE_X11)
41 #include "ui/events/devices/x11/touch_factory_x11.h"
44 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
45 #include "ui/base/ime/input_method_initializer.h"
50 // FIXME: Compare with method in startup_browser_creator.cc.
51 GURL GetURLFromCommandLine(const CommandLine& command_line) {
52 const CommandLine::StringVector& args = command_line.GetArgs();
58 if (url.is_valid() && url.has_scheme())
61 base::FilePath path(args[0]);
62 if (!path.IsAbsolute())
63 path = MakeAbsoluteFilePath(path);
65 return net::FilePathToFileURL(path);
71 // Redefine settings not exposed by content module.
72 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
77 XWalkBrowserMainParts::XWalkBrowserMainParts(
78 const content::MainFunctionParams& parameters)
79 : xwalk_runner_(XWalkRunner::GetInstance()),
80 extension_service_(NULL),
81 startup_url_(url::kAboutBlankURL),
82 parameters_(parameters),
83 run_default_message_loop_(true) {
85 // FIXME: We disable the setuid sandbox on Linux because we don't ship
86 // the setuid binary. It is important to remember that the seccomp-bpf
87 // sandbox is still fully operational if supported by the kernel. See
90 // switches::kDisableSetuidSandbox is not being used here because it
91 // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
92 // content_switches.h.
93 CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
97 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
100 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
101 CommandLine* command_line = CommandLine::ForCurrentProcess();
102 command_line->AppendSwitch(switches::kEnableViewport);
104 command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
106 // Enable multithreaded GPU compositing of web content.
107 // This also enables pinch on Tizen.
108 command_line->AppendSwitch(switches::kEnableThreadedCompositing);
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(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
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 std::string value = cmd_line->GetSwitchValueASCII(
143 switches::kXWalkExternalExtensionsPath);
145 #if defined(ARCH_CPU_64_BITS)
146 const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
148 const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
152 cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
154 else if (value != tec_path)
155 VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
158 cmd_line->AppendSwitch(
159 switches::kXWalkAllowExternalExtensionsForRemoteSources);
161 if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
165 if (!cmd_line->HasSwitch(
166 switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
167 (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
168 VLOG(0) << "Unsupported scheme for external extensions: " <<
169 startup_url_.scheme();
173 base::FilePath extensions_dir =
174 cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
175 if (!base::DirectoryExists(extensions_dir)) {
176 LOG(WARNING) << "Ignoring non-existent extension directory: "
177 << extensions_dir.AsUTF8Unsafe();
181 extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
184 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
185 xwalk_runner_->PreMainMessageLoopRun();
187 extension_service_ = xwalk_runner_->extension_service();
189 if (extension_service_)
190 RegisterExternalExtensions();
192 #if !defined(DISABLE_NACL)
193 NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
194 nacl::NaClBrowser::SetDelegate(delegate);
196 content::BrowserThread::PostTask(
197 content::BrowserThread::IO,
199 base::Bind(nacl::NaClProcessHost::EarlyStartup));
202 CommandLine* command_line = CommandLine::ForCurrentProcess();
203 if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
204 std::string port_str =
205 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
207 base::StringToInt(port_str, &port);
208 xwalk_runner_->EnableRemoteDebugging(port);
211 NativeAppWindow::Initialize();
213 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
214 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
215 run_default_message_loop_ = false;
219 if (!xwalk_runner_->shared_process_mode_enabled()) {
220 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
221 app_system->LaunchFromCommandLine(*command_line, startup_url_,
222 run_default_message_loop_);
223 // If the |ui_task| is specified in main function parameter, it indicates
224 // that we will run this UI task instead of running the the default main
225 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
227 if (parameters_.ui_task) {
228 parameters_.ui_task->Run();
229 delete parameters_.ui_task;
230 run_default_message_loop_ = false;
235 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
236 return !run_default_message_loop_;
239 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
240 xwalk_runner_->PostMainMessageLoopRun();
243 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
244 content::RenderProcessHost* host,
245 extensions::XWalkExtensionVector* extensions) {
248 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
249 content::RenderProcessHost* host,
250 extensions::XWalkExtensionVector* extensions) {