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/filename_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_(url::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 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 const char* local_ip = "0.0.0.0";
208 if (base::StringToInt(port_str, &port) && port > 0 && port < 65535) {
209 remote_debugging_server_.reset(
210 new RemoteDebuggingServer(xwalk_runner_->runtime_context(),
211 local_ip, port, std::string()));
215 NativeAppWindow::Initialize();
217 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
218 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
219 run_default_message_loop_ = false;
223 #if !defined(SHARED_PROCESS_MODE)
224 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
225 app_system->LaunchFromCommandLine(*command_line, startup_url_,
226 run_default_message_loop_);
227 // If the |ui_task| is specified in main function parameter, it indicates
228 // that we will run this UI task instead of running the the default main
229 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
231 if (parameters_.ui_task) {
232 parameters_.ui_task->Run();
233 delete parameters_.ui_task;
234 run_default_message_loop_ = false;
239 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
240 return !run_default_message_loop_;
243 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
244 xwalk_runner_->PostMainMessageLoopRun();
247 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
248 content::RenderProcessHost* host,
249 extensions::XWalkExtensionVector* extensions) {
252 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
253 content::RenderProcessHost* host,
254 extensions::XWalkExtensionVector* extensions) {