62ffc5557678113e4c00c62c2c0c1ab59b942ff8
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_browser_main_parts.cc
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.
4
5 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
6
7 #include <stdlib.h>
8
9 #include <set>
10
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/net_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"
39
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"
43 #endif
44
45 namespace {
46
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();
50
51   if (args.empty())
52     return GURL();
53
54   GURL url(args[0]);
55   if (url.is_valid() && url.has_scheme())
56     return url;
57
58   base::FilePath path(args[0]);
59   if (!path.IsAbsolute())
60     path = MakeAbsoluteFilePath(path);
61
62   return net::FilePathToFileURL(path);
63 }
64
65 }  // namespace
66
67 namespace xswitches {
68 // Redefine settings not exposed by content module.
69 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
70 }
71
72 namespace xwalk {
73
74 XWalkBrowserMainParts::XWalkBrowserMainParts(
75     const content::MainFunctionParams& parameters)
76     : xwalk_runner_(XWalkRunner::GetInstance()),
77       startup_url_(content::kAboutBlankURL),
78       parameters_(parameters),
79       run_default_message_loop_(true) {
80 #if defined(OS_LINUX)
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
84   // issue #496.
85   //
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");
90 #endif
91 }
92
93 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
94 }
95
96 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
97   CommandLine* command_line = CommandLine::ForCurrentProcess();
98   command_line->AppendSwitch(switches::kEnableViewport);
99   command_line->AppendSwitch(switches::kEnableViewportMeta);
100
101   command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
102
103   // Enable multithreaded GPU compositing of web content.
104   // This also enables pinch on Tizen.
105   command_line->AppendSwitch(switches::kEnableThreadedCompositing);
106
107   // Show feedback on touch.
108   command_line->AppendSwitch(switches::kEnableGestureTapHighlight);
109
110   // FIXME: Add comment why this is needed on Android and Tizen.
111   command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
112
113   // Enable SIMD.JS API by default.
114   std::string js_flags("--simd_object");
115   if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
116     js_flags += " ";
117     js_flags +=
118         command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
119   }
120   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);
121
122   startup_url_ = GetURLFromCommandLine(*command_line);
123 }
124
125 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
126 }
127
128 void XWalkBrowserMainParts::PreEarlyInitialization() {
129 #if defined(USE_AURA) && defined(USE_X11)
130     ui::InitializeInputMethodForTesting();
131 #endif
132 }
133
134 int XWalkBrowserMainParts::PreCreateThreads() {
135   return content::RESULT_CODE_NORMAL_EXIT;
136 }
137
138 void XWalkBrowserMainParts::RegisterExternalExtensions() {
139   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
140
141 #if defined(OS_TIZEN)
142   static const std::string tec_path = "/usr/lib/tizen-extensions-crosswalk";
143   std::string value = cmd_line->GetSwitchValueASCII(
144       switches::kXWalkExternalExtensionsPath);
145
146   if (value.empty())
147     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
148         tec_path);
149   else if (value != tec_path)
150     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
151         tec_path;
152
153   cmd_line->AppendSwitch(
154         switches::kXWalkAllowExternalExtensionsForRemoteSources);
155 #else
156   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
157     return;
158 #endif
159
160   if (!cmd_line->HasSwitch(
161           switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
162       (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
163     VLOG(0) << "Unsupported scheme for external extensions: " <<
164           startup_url_.scheme();
165     return;
166   }
167
168   base::FilePath extensions_dir =
169       cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
170   if (!base::DirectoryExists(extensions_dir)) {
171     LOG(WARNING) << "Ignoring non-existent extension directory: "
172                  << extensions_dir.AsUTF8Unsafe();
173     return;
174   }
175
176   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
177 }
178
179 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
180   xwalk_runner_->PreMainMessageLoopRun();
181
182   extension_service_ = xwalk_runner_->extension_service();
183
184   if (extension_service_)
185     RegisterExternalExtensions();
186
187 #if !defined(DISABLE_NACL)
188   NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
189   nacl::NaClBrowser::SetDelegate(delegate);
190
191   content::BrowserThread::PostTask(
192       content::BrowserThread::IO,
193       FROM_HERE,
194       base::Bind(nacl::NaClProcessHost::EarlyStartup));
195 #endif
196
197   CommandLine* command_line = CommandLine::ForCurrentProcess();
198   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
199     std::string port_str =
200         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
201     int port;
202     const char* local_ip = "0.0.0.0";
203     if (base::StringToInt(port_str, &port) && port > 0 && port < 65535) {
204       remote_debugging_server_.reset(
205           new RemoteDebuggingServer(xwalk_runner_->runtime_context(),
206               local_ip, port, std::string()));
207     }
208   }
209
210   NativeAppWindow::Initialize();
211
212   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
213     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
214     run_default_message_loop_ = false;
215     return;
216   }
217
218   if (xwalk_runner_->is_running_as_service()) {
219     // In service mode, Crosswalk doesn't launch anything, just waits
220     // for external requests to launch apps.
221     VLOG(1) << "Crosswalk running as Service.";
222     return;
223   }
224
225   application::ApplicationSystem* app_system = xwalk_runner_->app_system();
226   if (!app_system->HandleApplicationManagementCommands(*command_line,
227       startup_url_, run_default_message_loop_)) {
228     app_system->LaunchFromCommandLine(*command_line, startup_url_,
229                                       run_default_message_loop_);
230   }
231
232   // If the |ui_task| is specified in main function parameter, it indicates
233   // that we will run this UI task instead of running the the default main
234   // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
235   // case.
236   if (parameters_.ui_task) {
237     parameters_.ui_task->Run();
238     delete parameters_.ui_task;
239     run_default_message_loop_ = false;
240   }
241 }
242
243 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
244   return !run_default_message_loop_;
245 }
246
247 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
248   xwalk_runner_->PostMainMessageLoopRun();
249 }
250
251 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
252     content::RenderProcessHost* host,
253     extensions::XWalkExtensionVector* extensions) {
254 }
255
256 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
257     content::RenderProcessHost* host,
258     extensions::XWalkExtensionVector* extensions) {
259 }
260
261 }  // namespace xwalk