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 "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/runtime.h"
31 #include "xwalk/runtime/browser/runtime_context.h"
32 #include "xwalk/runtime/browser/xwalk_runner.h"
33 #include "xwalk/runtime/common/xwalk_runtime_features.h"
34 #include "xwalk/runtime/common/xwalk_switches.h"
36 #if !defined(DISABLE_NACL)
37 #include "components/nacl/browser/nacl_browser.h"
38 #include "components/nacl/browser/nacl_process_host.h"
39 #include "xwalk/runtime/browser/nacl_host/nacl_browser_delegate_impl.h"
42 #if defined(USE_AURA) && defined(USE_X11)
43 #include "ui/base/ime/input_method_initializer.h"
44 #include "ui/events/x/touch_factory_x11.h"
49 // FIXME: Compare with method in startup_browser_creator.cc.
50 GURL GetURLFromCommandLine(const CommandLine& command_line) {
51 const CommandLine::StringVector& args = command_line.GetArgs();
57 if (url.is_valid() && url.has_scheme())
60 base::FilePath path(args[0]);
61 if (!path.IsAbsolute())
62 path = MakeAbsoluteFilePath(path);
64 return net::FilePathToFileURL(path);
70 // Redefine settings not exposed by content module.
71 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
76 XWalkBrowserMainParts::XWalkBrowserMainParts(
77 const content::MainFunctionParams& parameters)
78 : xwalk_runner_(XWalkRunner::GetInstance()),
79 startup_url_(url::kAboutBlankURL),
80 parameters_(parameters),
81 run_default_message_loop_(true) {
83 // FIXME: We disable the setuid sandbox on Linux because we don't ship
84 // the setuid binary. It is important to remember that the seccomp-bpf
85 // sandbox is still fully operational if supported by the kernel. See
88 // switches::kDisableSetuidSandbox is not being used here because it
89 // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
90 // content_switches.h.
91 CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
95 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
98 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
99 CommandLine* command_line = CommandLine::ForCurrentProcess();
100 command_line->AppendSwitch(switches::kEnableViewport);
101 command_line->AppendSwitch(switches::kEnableViewportMeta);
103 command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
105 // Enable multithreaded GPU compositing of web content.
106 // This also enables pinch on Tizen.
107 command_line->AppendSwitch(switches::kEnableThreadedCompositing);
109 // FIXME: Add comment why this is needed on Android and Tizen.
110 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
112 // 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);
123 startup_url_ = GetURLFromCommandLine(*command_line);
126 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
129 void XWalkBrowserMainParts::PreEarlyInitialization() {
130 #if defined(USE_AURA) && defined(USE_X11)
131 ui::InitializeInputMethodForTesting();
135 int XWalkBrowserMainParts::PreCreateThreads() {
136 return content::RESULT_CODE_NORMAL_EXIT;
139 void XWalkBrowserMainParts::RegisterExternalExtensions() {
140 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
142 #if defined(OS_TIZEN)
143 std::string value = cmd_line->GetSwitchValueASCII(
144 switches::kXWalkExternalExtensionsPath);
146 #if defined(ARCH_CPU_64_BITS)
147 const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
149 const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
153 cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
155 else if (value != tec_path)
156 VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
159 cmd_line->AppendSwitch(
160 switches::kXWalkAllowExternalExtensionsForRemoteSources);
162 if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
166 if (!cmd_line->HasSwitch(
167 switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
168 (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
169 VLOG(0) << "Unsupported scheme for external extensions: " <<
170 startup_url_.scheme();
174 base::FilePath extensions_dir =
175 cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
176 if (!base::DirectoryExists(extensions_dir)) {
177 LOG(WARNING) << "Ignoring non-existent extension directory: "
178 << extensions_dir.AsUTF8Unsafe();
182 extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
185 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
186 xwalk_runner_->PreMainMessageLoopRun();
188 extension_service_ = xwalk_runner_->extension_service();
190 if (extension_service_)
191 RegisterExternalExtensions();
193 #if !defined(DISABLE_NACL)
194 NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
195 nacl::NaClBrowser::SetDelegate(delegate);
197 content::BrowserThread::PostTask(
198 content::BrowserThread::IO,
200 base::Bind(nacl::NaClProcessHost::EarlyStartup));
203 CommandLine* command_line = CommandLine::ForCurrentProcess();
204 if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
205 std::string port_str =
206 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
208 base::StringToInt(port_str, &port);
209 xwalk_runner_->EnableRemoteDebugging(port);
212 NativeAppWindow::Initialize();
214 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
215 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
216 run_default_message_loop_ = false;
220 #if !defined(SHARED_PROCESS_MODE)
221 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
222 app_system->LaunchFromCommandLine(*command_line, startup_url_,
223 run_default_message_loop_);
224 // If the |ui_task| is specified in main function parameter, it indicates
225 // that we will run this UI task instead of running the the default main
226 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
228 if (parameters_.ui_task) {
229 parameters_.ui_task->Run();
230 delete parameters_.ui_task;
231 run_default_message_loop_ = false;
236 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
237 return !run_default_message_loop_;
240 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
241 xwalk_runner_->PostMainMessageLoopRun();
244 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
245 content::RenderProcessHost* host,
246 extensions::XWalkExtensionVector* extensions) {
249 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
250 content::RenderProcessHost* host,
251 extensions::XWalkExtensionVector* extensions) {