ff61f2e453b2f5bd6ff7c1d824f092cb6d714c37
[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   std::string js_flags("--simd_object");
114   if (command_line->HasSwitch(switches::kJavaScriptFlags)) {
115     js_flags += " ";
116     js_flags +=
117         command_line->GetSwitchValueASCII(switches::kJavaScriptFlags);
118   }
119   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags);
120
121   startup_url_ = GetURLFromCommandLine(*command_line);
122 }
123
124 void XWalkBrowserMainParts::PostMainMessageLoopStart() {
125 }
126
127 void XWalkBrowserMainParts::PreEarlyInitialization() {
128 #if defined(USE_AURA) && defined(USE_X11)
129     ui::InitializeInputMethodForTesting();
130 #endif
131 }
132
133 int XWalkBrowserMainParts::PreCreateThreads() {
134   return content::RESULT_CODE_NORMAL_EXIT;
135 }
136
137 void XWalkBrowserMainParts::RegisterExternalExtensions() {
138   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
139
140 #if defined(OS_TIZEN)
141   std::string value = cmd_line->GetSwitchValueASCII(
142       switches::kXWalkExternalExtensionsPath);
143
144 #if defined(ARCH_CPU_64_BITS)
145   const char tec_path[] = "/usr/lib64/tizen-extensions-crosswalk";
146 #else
147   const char tec_path[] = "/usr/lib/tizen-extensions-crosswalk";
148 #endif
149
150   if (value.empty())
151     cmd_line->AppendSwitchASCII(switches::kXWalkExternalExtensionsPath,
152         tec_path);
153   else if (value != tec_path)
154     VLOG(0) << "Loading Tizen extensions from " << value << " rather than " <<
155         tec_path;
156
157   cmd_line->AppendSwitch(
158         switches::kXWalkAllowExternalExtensionsForRemoteSources);
159 #else
160   if (!cmd_line->HasSwitch(switches::kXWalkExternalExtensionsPath))
161     return;
162 #endif
163
164   if (!cmd_line->HasSwitch(
165           switches::kXWalkAllowExternalExtensionsForRemoteSources) &&
166       (!startup_url_.is_empty() && !startup_url_.SchemeIsFile())) {
167     VLOG(0) << "Unsupported scheme for external extensions: " <<
168           startup_url_.scheme();
169     return;
170   }
171
172   base::FilePath extensions_dir =
173       cmd_line->GetSwitchValuePath(switches::kXWalkExternalExtensionsPath);
174   if (!base::DirectoryExists(extensions_dir)) {
175     LOG(WARNING) << "Ignoring non-existent extension directory: "
176                  << extensions_dir.AsUTF8Unsafe();
177     return;
178   }
179
180   extension_service_->RegisterExternalExtensionsForPath(extensions_dir);
181 }
182
183 void XWalkBrowserMainParts::PreMainMessageLoopRun() {
184   xwalk_runner_->PreMainMessageLoopRun();
185
186   extension_service_ = xwalk_runner_->extension_service();
187
188   if (extension_service_)
189     RegisterExternalExtensions();
190
191 #if !defined(DISABLE_NACL)
192   NaClBrowserDelegateImpl* delegate = new NaClBrowserDelegateImpl();
193   nacl::NaClBrowser::SetDelegate(delegate);
194
195   content::BrowserThread::PostTask(
196       content::BrowserThread::IO,
197       FROM_HERE,
198       base::Bind(nacl::NaClProcessHost::EarlyStartup));
199 #endif
200
201   CommandLine* command_line = CommandLine::ForCurrentProcess();
202   if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
203     std::string port_str =
204         command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort);
205     int port;
206     base::StringToInt(port_str, &port);
207     xwalk_runner_->EnableRemoteDebugging(port);
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 !defined(SHARED_PROCESS_MODE)
219   application::ApplicationSystem* app_system = xwalk_runner_->app_system();
220   app_system->LaunchFromCommandLine(*command_line, startup_url_,
221                                     run_default_message_loop_);
222   // If the |ui_task| is specified in main function parameter, it indicates
223   // that we will run this UI task instead of running the the default main
224   // message loop. See |content::BrowserTestBase::SetUp| for |ui_task| usage
225   // case.
226   if (parameters_.ui_task) {
227     parameters_.ui_task->Run();
228     delete parameters_.ui_task;
229     run_default_message_loop_ = false;
230   }
231 #endif
232 }
233
234 bool XWalkBrowserMainParts::MainMessageLoopRun(int* result_code) {
235   return !run_default_message_loop_;
236 }
237
238 void XWalkBrowserMainParts::PostMainMessageLoopRun() {
239   xwalk_runner_->PostMainMessageLoopRun();
240 }
241
242 void XWalkBrowserMainParts::CreateInternalExtensionsForUIThread(
243     content::RenderProcessHost* host,
244     extensions::XWalkExtensionVector* extensions) {
245 }
246
247 void XWalkBrowserMainParts::CreateInternalExtensionsForExtensionThread(
248     content::RenderProcessHost* host,
249     extensions::XWalkExtensionVector* extensions) {
250 }
251
252 }  // namespace xwalk