Upstream version 9.38.198.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/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   command_line->AppendSwitch(switches::kEnableViewportMeta);
102
103   command_line->AppendSwitch(xswitches::kEnableOverlayScrollbars);
104
105   // Enable multithreaded GPU compositing of web content.
106   // This also enables pinch on Tizen.
107   command_line->AppendSwitch(switches::kEnableThreadedCompositing);
108
109   // FIXME: Add comment why this is needed on Android and Tizen.
110   command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
111
112   // Enable SIMD.JS API by default.
113   /*
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
123   startup_url_ = GetURLFromCommandLine(*command_line);
124 }
125
126 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
127 }
128
129 void XWalkBrowserMainParts::PreEarlyInitialization() {
130 #if defined(USE_AURA) && defined(USE_X11)
131     ui::InitializeInputMethodForTesting();
132 #endif
133 }
134
135 int XWalkBrowserMainParts::PreCreateThreads() {
136   return content::RESULT_CODE_NORMAL_EXIT;
137 }
138
139 void XWalkBrowserMainParts::RegisterExternalExtensions() {
140   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
141
142 #if defined(OS_TIZEN)
143   std::string value = cmd_line->GetSwitchValueASCII(
144       switches::kXWalkExternalExtensionsPath);
145
146 #if defined(ARCH_CPU_64_BITS)
147   const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
148 #else
149   const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
150 #endif
151
152   if (value.empty())
153     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
154         tec_path);
155   else if (value != tec_path)
156     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
157         tec_path;
158
159   cmd_line->AppendSwitch(
160         switches::kXWalkAllowExternalExtensionsForRemoteSources);
161 #else
162   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
163     return;
164 #endif
165
166   if (!cmd_line->HasSwitch(
167           switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
168       (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
169     VLOG(0) << "Unsupported scheme for external extensions: " <<
170           startup_url_.scheme();
171     return;
172   }
173
174   base::FilePath extensions_dir =
175       cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
176   if (!base::DirectoryExists(extensions_dir)) {
177     LOG(WARNING) << "Ignoring non-existent extension directory: "
178                  << extensions_dir.AsUTF8Unsafe();
179     return;
180   }
181
182   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
183 }
184
185 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
186   xwalk_runner_->PreMainMessageLoopRun();
187
188   extension_service_ = xwalk_runner_->extension_service();
189
190   if (extension_service_)
191     RegisterExternalExtensions();
192
193 #if !defined(DISABLE_NACL)
194   NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
195   nacl::NaClBrowser::SetDelegate(delegate);
196
197   content::BrowserThread::PostTask(
198       content::BrowserThread::IO,
199       FROM_HERE,
200       base::Bind(nacl::NaClProcessHost::EarlyStartup));
201 #endif
202
203   CommandLine* command_line = CommandLine::ForCurrentProcess();
204   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
205     std::string port_str =
206         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
207     int port;
208     base::StringToInt(port_str, &port);
209     xwalk_runner_->EnableRemoteDebugging(port);
210   }
211
212   NativeAppWindow::Initialize();
213
214   if (command_line->HasSwitch(switches::kListFeaturesFlags)) {
215     XWalkRuntimeFeatures::GetInstance()->DumpFeaturesFlags();
216     run_default_message_loop_ = false;
217     return;
218   }
219
220 #if !defined(SHARED_PROCESS_MODE)
221   application::ApplicationSystem* app_system = xwalk_runner_->app_system();
222   app_system->LaunchFromCommandLine(*command_line, startup_url_,
223                                     run_default_message_loop_);
224   // If the |ui_task| is specified in main function parameter, it indicates
225   // that we will run this UI task instead of running the the default main
226   // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
227   // case.
228   if (parameters_.ui_task) {
229     parameters_.ui_task->Run();
230     delete parameters_.ui_task;
231     run_default_message_loop_ = false;
232   }
233 #endif
234 }
235
236 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
237   return !run_default_message_loop_;
238 }
239
240 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
241   xwalk_runner_->PostMainMessageLoopRun();
242 }
243
244 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
245     content::RenderProcessHost* host,
246     extensions::XWalkExtensionVector* extensions) {
247 }
248
249 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
250     content::RenderProcessHost* host,
251     extensions::XWalkExtensionVector* extensions) {
252 }
253
254 }  // namespace xwalk