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);
102 command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
104 // Enable multithreaded GPU compositing of web content.
105 // This also enables pinch on Tizen.
106 command_line->AppendSwitch(switches::kEnableThreadedCompositing);
108 // FIXME: Add comment why this is needed on Android and Tizen.
109 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
111 // Enable SIMD.JS API by default.
112 /*std::string js_flags("--simd_object");
113 if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
116 command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
118 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);*/
120 startup_url_ = GetURLFromCommandLine(*command_line);
123 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
126 void XWalkBrowserMainParts::PreEarlyInitialization() {
127 #if defined(USE_AURA) && defined(USE_X11)
128 ui::InitializeInputMethodForTesting();
132 int XWalkBrowserMainParts::PreCreateThreads() {
133 return content::RESULT_CODE_NORMAL_EXIT;
136 void XWalkBrowserMainParts::RegisterExternalExtensions() {
137 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
139 #if defined(OS_TIZEN)
140 std::string value = cmd_line->GetSwitchValueASCII(
141 switches::kXWalkExternalExtensionsPath);
143 #if defined(ARCH_CPU_64_BITS)
144 const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
146 const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
150 cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
152 else if (value != tec_path)
153 VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
156 cmd_line->AppendSwitch(
157 switches::kXWalkAllowExternalExtensionsForRemoteSources);
159 if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
163 if (!cmd_line->HasSwitch(
164 switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
165 (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
166 VLOG(0) << "Unsupported scheme for external extensions: " <<
167 startup_url_.scheme();
171 base::FilePath extensions_dir =
172 cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
173 if (!base::DirectoryExists(extensions_dir)) {
174 LOG(WARNING) << "Ignoring non-existent extension directory: "
175 << extensions_dir.AsUTF8Unsafe();
179 extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
182 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
183 xwalk_runner_->PreMainMessageLoopRun();
185 extension_service_ = xwalk_runner_->extension_service();
187 if (extension_service_)
188 RegisterExternalExtensions();
190 #if !defined(DISABLE_NACL)
191 NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
192 nacl::NaClBrowser::SetDelegate(delegate);
194 content::BrowserThread::PostTask(
195 content::BrowserThread::IO,
197 base::Bind(nacl::NaClProcessHost::EarlyStartup));
200 CommandLine* command_line = CommandLine::ForCurrentProcess();
201 if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
202 std::string port_str =
203 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
205 base::StringToInt(port_str, &port);
206 xwalk_runner_->EnableRemoteDebugging(port);
209 NativeAppWindow::Initialize();
211 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
212 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
213 run_default_message_loop_ = false;
217 #if !defined(SHARED_PROCESS_MODE)
218 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
219 app_system->LaunchFromCommandLine(*command_line, startup_url_,
220 run_default_message_loop_);
221 // If the |ui_task| is specified in main function parameter, it indicates
222 // that we will run this UI task instead of running the the default main
223 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
225 if (parameters_.ui_task) {
226 parameters_.ui_task->Run();
227 delete parameters_.ui_task;
228 run_default_message_loop_ = false;
233 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
234 return !run_default_message_loop_;
237 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
238 xwalk_runner_->PostMainMessageLoopRun();
241 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
242 content::RenderProcessHost* host,
243 extensions::XWalkExtensionVector* extensions) {
246 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
247 content::RenderProcessHost* host,
248 extensions::XWalkExtensionVector* extensions) {