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 "xwalk/application/browser/application.h"
18 #include "xwalk/application/browser/application_system.h"
19 #include "xwalk/extensions/browser/xwalk_extension_service.h"
20 #include "xwalk/extensions/common/xwalk_extension_switches.h"
21 #include "xwalk/runtime/browser/devtools/remote_debugging_server.h"
22 #include "xwalk/runtime/browser/runtime.h"
23 #include "xwalk/runtime/browser/runtime_context.h"
24 #include "xwalk/runtime/browser/xwalk_runner.h"
25 #include "xwalk/runtime/common/xwalk_runtime_features.h"
26 #include "xwalk/runtime/common/xwalk_switches.h"
27 #include "cc/base/switches.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/main_function_params.h"
30 #include "content/public/common/url_constants.h"
31 #include "content/public/common/result_codes.h"
32 #include "net/base/net_util.h"
33 #include "ui/gl/gl_switches.h"
35 #if defined(USE_AURA) && defined(USE_X11)
36 #include "ui/base/ime/input_method_initializer.h"
37 #include "ui/events/x/touch_factory_x11.h"
42 // FIXME: Compare with method in startup_browser_creator.cc.
43 GURL GetURLFromCommandLine(const CommandLine& command_line) {
44 const CommandLine::StringVector& args = command_line.GetArgs();
50 if (url.is_valid() && url.has_scheme())
53 base::FilePath path(args[0]);
54 if (!path.IsAbsolute())
55 path = MakeAbsoluteFilePath(path);
57 return net::FilePathToFileURL(path);
63 // Redefine settings not exposed by content module.
64 const char kEnableViewport[] = "enable-viewport";
65 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
70 XWalkBrowserMainParts::XWalkBrowserMainParts(
71 const content::MainFunctionParams& parameters)
72 : xwalk_runner_(XWalkRunner::GetInstance()),
73 startup_url_(content::kAboutBlankURL),
74 parameters_(parameters),
75 run_default_message_loop_(true) {
77 // FIXME: We disable the setuid sandbox on Linux because we don't ship
78 // the setuid binary. It is important to remember that the seccomp-bpf
79 // sandbox is still fully operational if supported by the kernel. See
82 // switches::kDisableSetuidSandbox is not being used here because it
83 // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
84 // content_switches.h.
85 CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
89 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
92 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
93 CommandLine* command_line = CommandLine::ForCurrentProcess();
94 command_line->AppendSwitch(xswitches::kEnableViewport);
96 command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
98 // Enable multithreaded GPU compositing of web content.
99 // This also enables pinch on Tizen.
100 command_line->AppendSwitch(switches::kEnableThreadedCompositing);
102 // Show feedback on touch.
103 command_line->AppendSwitch(switches::kEnableGestureTapHighlight);
105 // FIXME: Add comment why this is needed on Android and Tizen.
106 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
108 // Enable SIMD.JS API by default.
109 std::string js_flags("--simd_object");
110 if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
113 command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
115 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);
117 startup_url_ = GetURLFromCommandLine(*command_line);
120 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
123 void XWalkBrowserMainParts::PreEarlyInitialization() {
124 #if defined(USE_AURA) && defined(USE_X11)
125 ui::InitializeInputMethodForTesting();
129 int XWalkBrowserMainParts::PreCreateThreads() {
130 return content::RESULT_CODE_NORMAL_EXIT;
133 void XWalkBrowserMainParts::RegisterExternalExtensions() {
134 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
136 #if defined(OS_TIZEN)
137 static const std::string tec_path = "/usr/lib/tizen-extensions-crosswalk";
138 std::string value = cmd_line->GetSwitchValueASCII(
139 switches::kXWalkExternalExtensionsPath);
142 cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
144 else if (value != tec_path)
145 VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
148 cmd_line->AppendSwitch(
149 switches::kXWalkAllowExternalExtensionsForRemoteSources);
151 if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
155 if (!cmd_line->HasSwitch(
156 switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
157 !startup_url_.SchemeIsFile()) {
158 VLOG(0) << "Unsupported scheme for external extensions: " <<
159 startup_url_.scheme();
163 base::FilePath extensions_dir =
164 cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
165 if (!base::DirectoryExists(extensions_dir)) {
166 LOG(WARNING) << "Ignoring non-existent extension directory: "
167 << extensions_dir.AsUTF8Unsafe();
171 extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
174 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
175 xwalk_runner_->PreMainMessageLoopRun();
177 extension_service_ = xwalk_runner_->extension_service();
179 if (extension_service_)
180 RegisterExternalExtensions();
182 CommandLine* command_line = CommandLine::ForCurrentProcess();
183 if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
184 std::string port_str =
185 command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
187 const char* local_ip = "0.0.0.0";
188 if (base::StringToInt(port_str, &port) && port > 0 && port < 65535) {
189 remote_debugging_server_.reset(
190 new RemoteDebuggingServer(xwalk_runner_->runtime_context(),
191 local_ip, port, std::string()));
195 NativeAppWindow::Initialize();
197 if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
198 XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
199 run_default_message_loop_ = false;
203 if (xwalk_runner_->is_running_as_service()) {
204 // In service mode, Crosswalk doesn't launch anything, just waits
205 // for external requests to launch apps.
206 VLOG(1) << "Crosswalk running as Service.";
210 application::ApplicationSystem* app_system = xwalk_runner_->app_system();
211 if (!app_system->HandleApplicationManagementCommands(*command_line,
212 startup_url_, run_default_message_loop_)) {
213 app_system->LaunchFromCommandLine(*command_line, startup_url_,
214 run_default_message_loop_);
217 // If the |ui_task| is specified in main function parameter, it indicates
218 // that we will run this UI task instead of running the the default main
219 // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
221 if (parameters_.ui_task) {
222 parameters_.ui_task->Run();
223 delete parameters_.ui_task;
224 run_default_message_loop_ = false;
228 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
229 return !run_default_message_loop_;
232 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
233 xwalk_runner_->PostMainMessageLoopRun();
236 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
237 content::RenderProcessHost* host,
238 extensions::XWalkExtensionVector* extensions) {
241 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
242 content::RenderProcessHost* host,
243 extensions::XWalkExtensionVector* extensions) {