4fecd9e8c5fb4a5b937fe488bf54129a9f1464bc
[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/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"
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_(url::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   std::string value = cmd_line->GetSwitchValueASCII(
143       switches::kXWalkExternalExtensionsPath);
144
145 #if defined(ARCH_CPU_64_BITS)
146   const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
147 #else
148   const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
149 #endif
150
151   if (value.empty())
152     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
153         tec_path);
154   else if (value != tec_path)
155     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
156         tec_path;
157
158   cmd_line->AppendSwitch(
159         switches::kXWalkAllowExternalExtensionsForRemoteSources);
160 #else
161   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
162     return;
163 #endif
164
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();
170     return;
171   }
172
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();
178     return;
179   }
180
181   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
182 }
183
184 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
185   xwalk_runner_->PreMainMessageLoopRun();
186
187   extension_service_ = xwalk_runner_->extension_service();
188
189   if (extension_service_)
190     RegisterExternalExtensions();
191
192 #if !defined(DISABLE_NACL)
193   NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
194   nacl::NaClBrowser::SetDelegate(delegate);
195
196   content::BrowserThread::PostTask(
197       content::BrowserThread::IO,
198       FROM_HERE,
199       base::Bind(nacl::NaClProcessHost::EarlyStartup));
200 #endif
201
202   CommandLine* command_line = CommandLine::ForCurrentProcess();
203   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
204     std::string port_str =
205         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
206     int port;
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()));
212     }
213   }
214
215   NativeAppWindow::Initialize();
216
217   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
218     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
219     run_default_message_loop_ = false;
220     return;
221   }
222
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
230   // case.
231   if (parameters_.ui_task) {
232     parameters_.ui_task->Run();
233     delete parameters_.ui_task;
234     run_default_message_loop_ = false;
235   }
236 #endif
237 }
238
239 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
240   return !run_default_message_loop_;
241 }
242
243 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
244   xwalk_runner_->PostMainMessageLoopRun();
245 }
246
247 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
248     content::RenderProcessHost* host,
249     extensions::XWalkExtensionVector* extensions) {
250 }
251
252 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
253     content::RenderProcessHost* host,
254     extensions::XWalkExtensionVector* extensions) {
255 }
256
257 }  // namespace xwalk