1666b6068b6f04d7bcb31f555d27e859b9747f39
[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 "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"
35
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"
40 #endif
41
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"
45 #endif
46
47 namespace {
48
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();
52
53   if (args.empty())
54     return GURL();
55
56   GURL url(args[0]);
57   if (url.is_valid() && url.has_scheme())
58     return url;
59
60   base::FilePath path(args[0]);
61   if (!path.IsAbsolute())
62     path = MakeAbsoluteFilePath(path);
63
64   return net::FilePathToFileURL(path);
65 }
66
67 }  // namespace
68
69 namespace xswitches {
70 // Redefine settings not exposed by content module.
71 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
72 }
73
74 namespace xwalk {
75
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) {
82 #if defined(OS_LINUX)
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
86   // issue #496.
87   //
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");
92 #endif
93 }
94
95 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
96 }
97
98 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
99   CommandLine* command_line = CommandLine::ForCurrentProcess();
100   command_line->AppendSwitch(switches::kEnableViewport);
101
102   command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
103
104   // Enable multithreaded GPU compositing of web content.
105   // This also enables pinch on Tizen.
106   command_line->AppendSwitch(switches::kEnableThreadedCompositing);
107
108   // FIXME: Add comment why this is needed on Android and Tizen.
109   command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
110
111   // Enable SIMD.JS API by default.
112   /*std::string js_flags("--simd_object");
113   if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
114     js_flags += " ";
115     js_flags +=
116         command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
117   }
118   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);*/
119
120   startup_url_ = GetURLFromCommandLine(*command_line);
121 }
122
123 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
124 }
125
126 void XWalkBrowserMainParts::PreEarlyInitialization() {
127 #if defined(USE_AURA) && defined(USE_X11)
128     ui::InitializeInputMethodForTesting();
129 #endif
130 }
131
132 int XWalkBrowserMainParts::PreCreateThreads() {
133   return content::RESULT_CODE_NORMAL_EXIT;
134 }
135
136 void XWalkBrowserMainParts::RegisterExternalExtensions() {
137   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
138
139 #if defined(OS_TIZEN)
140   std::string value = cmd_line->GetSwitchValueASCII(
141       switches::kXWalkExternalExtensionsPath);
142
143 #if defined(ARCH_CPU_64_BITS)
144   const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
145 #else
146   const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
147 #endif
148
149   if (value.empty())
150     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
151         tec_path);
152   else if (value != tec_path)
153     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
154         tec_path;
155
156   cmd_line->AppendSwitch(
157         switches::kXWalkAllowExternalExtensionsForRemoteSources);
158 #else
159   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
160     return;
161 #endif
162
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();
168     return;
169   }
170
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();
176     return;
177   }
178
179   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
180 }
181
182 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
183   xwalk_runner_->PreMainMessageLoopRun();
184
185   extension_service_ = xwalk_runner_->extension_service();
186
187   if (extension_service_)
188     RegisterExternalExtensions();
189
190 #if !defined(DISABLE_NACL)
191   NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
192   nacl::NaClBrowser::SetDelegate(delegate);
193
194   content::BrowserThread::PostTask(
195       content::BrowserThread::IO,
196       FROM_HERE,
197       base::Bind(nacl::NaClProcessHost::EarlyStartup));
198 #endif
199
200   CommandLine* command_line = CommandLine::ForCurrentProcess();
201   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
202     std::string port_str =
203         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
204     int port;
205     base::StringToInt(port_str, &port);
206     xwalk_runner_->EnableRemoteDebugging(port);
207   }
208
209   NativeAppWindow::Initialize();
210
211   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
212     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
213     run_default_message_loop_ = false;
214     return;
215   }
216
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
224   // case.
225   if (parameters_.ui_task) {
226     parameters_.ui_task->Run();
227     delete parameters_.ui_task;
228     run_default_message_loop_ = false;
229   }
230 #endif
231 }
232
233 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
234   return !run_default_message_loop_;
235 }
236
237 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
238   xwalk_runner_->PostMainMessageLoopRun();
239 }
240
241 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
242     content::RenderProcessHost* host,
243     extensions::XWalkExtensionVector* extensions) {
244 }
245
246 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
247     content::RenderProcessHost* host,
248     extensions::XWalkExtensionVector* extensions) {
249 }
250
251 }  // namespace xwalk