Update To 11.40.268.0
[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/files/file_path.h"
14 #include "base/files/file_util.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/xwalk_runner.h"
31 #include "xwalk/runtime/common/xwalk_runtime_features.h"
32 #include "xwalk/runtime/common/xwalk_switches.h"
33
34 #if !defined(DISABLE_NACL)
35 #include "components/nacl/browser/nacl_browser.h"
36 #include "components/nacl/browser/nacl_process_host.h"
37 #include "xwalk/runtime/browser/nacl_host/nacl_browser_delegate_impl.h"
38 #endif
39
40 #if defined(USE_AURA) && defined(USE_X11)
41 #include "ui/events/devices/x11/touch_factory_x11.h"
42 #endif
43
44 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
45 #include "ui/base/ime/input_method_initializer.h"
46 #endif
47
48 namespace {
49
50 // FIXME: Compare with method in startup_browser_creator.cc.
51 GURL GetURLFromCommandLine(const CommandLine& command_line) {
52   const CommandLine::StringVector& args = command_line.GetArgs();
53
54   if (args.empty())
55     return GURL();
56
57   GURL url(args[0]);
58   if (url.is_valid() && url.has_scheme())
59     return url;
60
61   base::FilePath path(args[0]);
62   if (!path.IsAbsolute())
63     path = MakeAbsoluteFilePath(path);
64
65   return net::FilePathToFileURL(path);
66 }
67
68 }  // namespace
69
70 namespace xswitches {
71 // Redefine settings not exposed by content module.
72 const char kEnableOverlayScrollbars[] = "enable-overlay-scrollbars";
73 }
74
75 namespace xwalk {
76
77 XWalkBrowserMainParts::XWalkBrowserMainParts(
78     const content::MainFunctionParams& parameters)
79     : xwalk_runner_(XWalkRunner::GetInstance()),
80       extension_service_(NULL),
81       startup_url_(url::kAboutBlankURL),
82       parameters_(parameters),
83       run_default_message_loop_(true) {
84 #if defined(OS_LINUX)
85   // FIXME: We disable the setuid sandbox on Linux because we don't ship
86   // the setuid binary. It is important to remember that the seccomp-bpf
87   // sandbox is still fully operational if supported by the kernel. See
88   // issue #496.
89   //
90   // switches::kDisableSetuidSandbox is not being used here because it
91   // doesn't have the CONTENT_EXPORT macro despite the fact it is exposed by
92   // content_switches.h.
93   CommandLine::ForCurrentProcess()->AppendSwitch("disable-setuid-sandbox");
94 #endif
95 }
96
97 XWalkBrowserMainParts::~XWalkBrowserMainParts() {
98 }
99
100 void XWalkBrowserMainParts::PreMainMessageLoopStart() {
101   CommandLine* command_line = CommandLine::ForCurrentProcess();
102   command_line->AppendSwitch(switches::kEnableViewport);
103
104   command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
105
106   // Enable multithreaded GPU compositing of web content.
107   // This also enables pinch on Tizen.
108   command_line->AppendSwitch(switches::kEnableThreadedCompositing);
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(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
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     base::StringToInt(port_str, &port);
208     xwalk_runner_->EnableRemoteDebugging(port);
209   }
210
211   NativeAppWindow::Initialize();
212
213   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
214     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
215     run_default_message_loop_ = false;
216     return;
217   }
218
219   if (!xwalk_runner_->shared_process_mode_enabled()) {
220     application::ApplicationSystem* app_system = xwalk_runner_->app_system();
221     app_system->LaunchFromCommandLine(*command_line, startup_url_,
222                                       run_default_message_loop_);
223     // If the |ui_task| is specified in main function parameter, it indicates
224     // that we will run this UI task instead of running the the default main
225     // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
226     // case.
227     if (parameters_.ui_task) {
228       parameters_.ui_task->Run();
229       delete parameters_.ui_task;
230       run_default_message_loop_ = false;
231     }
232   }
233 }
234
235 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
236   return !run_default_message_loop_;
237 }
238
239 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
240   xwalk_runner_->PostMainMessageLoopRun();
241 }
242
243 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
244     content::RenderProcessHost* host,
245     extensions::XWalkExtensionVector* extensions) {
246 }
247
248 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
249     content::RenderProcessHost* host,
250     extensions::XWalkExtensionVector* extensions) {
251 }
252
253 }  // namespace xwalk