Add ICU message format support
[chromium-blink-merge.git] / content / shell / app / shell_main_delegate.cc
blob63f3f298bac2eced5055193bc41ad6b38e10daaf
1 // Copyright (c) 2012 The Chromium Authors. 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.
5 #include "content/shell/app/shell_main_delegate.h"
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/cpu.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "build/build_config.h"
16 #include "cc/base/switches.h"
17 #include "content/public/browser/browser_main_runner.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/url_constants.h"
20 #include "content/public/test/layouttest_support.h"
21 #include "content/shell/app/blink_test_platform_support.h"
22 #include "content/shell/app/shell_crash_reporter_client.h"
23 #include "content/shell/browser/layout_test/layout_test_browser_main.h"
24 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h"
25 #include "content/shell/browser/shell_browser_main.h"
26 #include "content/shell/browser/shell_content_browser_client.h"
27 #include "content/shell/common/shell_switches.h"
28 #include "content/shell/renderer/layout_test/layout_test_content_renderer_client.h"
29 #include "content/shell/renderer/shell_content_renderer_client.h"
30 #include "content/shell/utility/shell_content_utility_client.h"
31 #include "media/base/media_switches.h"
32 #include "media/base/mime_util.h"
33 #include "net/cookies/cookie_monster.h"
34 #include "ui/base/resource/resource_bundle.h"
35 #include "ui/base/ui_base_paths.h"
36 #include "ui/base/ui_base_switches.h"
37 #include "ui/events/event_switches.h"
38 #include "ui/gfx/switches.h"
39 #include "ui/gl/gl_switches.h"
41 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
43 #if defined(IPC_MESSAGE_LOG_ENABLED)
44 #define IPC_MESSAGE_MACROS_LOG_ENABLED
45 #include "content/public/common/content_ipc_logging.h"
46 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
47 content::RegisterIPCLogger(msg_id, logger)
48 #include "content/shell/common/shell_messages.h"
49 #endif
51 #if defined(OS_ANDROID)
52 #include "base/android/apk_assets.h"
53 #include "base/posix/global_descriptors.h"
54 #include "content/shell/android/shell_descriptors.h"
55 #endif
57 #if defined(OS_MACOSX)
58 #include "base/mac/os_crash_dumps.h"
59 #include "components/crash/app/breakpad_mac.h"
60 #include "content/shell/app/paths_mac.h"
61 #include "content/shell/app/shell_main_delegate_mac.h"
62 #endif // OS_MACOSX
64 #if defined(OS_WIN)
65 #include <initguid.h>
66 #include <windows.h>
67 #include "base/logging_win.h"
68 #include "components/crash/app/breakpad_win.h"
69 #include "content/shell/common/v8_breakpad_support_win.h"
70 #endif
72 #if defined(OS_POSIX) && !defined(OS_MACOSX)
73 #include "components/crash/app/breakpad_linux.h"
74 #endif
76 namespace {
78 base::LazyInstance<content::ShellCrashReporterClient>::Leaky
79 g_shell_crash_client = LAZY_INSTANCE_INITIALIZER;
81 #if defined(OS_WIN)
82 // If "Content Shell" doesn't show up in your list of trace providers in
83 // Sawbuck, add these registry entries to your machine (NOTE the optional
84 // Wow6432Node key for x64 machines):
85 // 1. Find: HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers
86 // 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}"
87 // 3. Add these values:
88 // "default_flags"=dword:00000001
89 // "default_level"=dword:00000004
90 // @="Content Shell"
92 // {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}
93 const GUID kContentShellProviderName = {
94 0x6a3e50a4, 0x7e15, 0x4099,
95 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } };
96 #endif
98 void InitLogging() {
99 base::FilePath log_filename;
100 PathService::Get(base::DIR_EXE, &log_filename);
101 log_filename = log_filename.AppendASCII("content_shell.log");
102 logging::LoggingSettings settings;
103 settings.logging_dest = logging::LOG_TO_ALL;
104 settings.log_file = log_filename.value().c_str();
105 settings.delete_old = logging::DELETE_OLD_LOG_FILE;
106 logging::InitLogging(settings);
107 logging::SetLogItems(true, true, true, true);
110 } // namespace
112 namespace content {
114 ShellMainDelegate::ShellMainDelegate() {
117 ShellMainDelegate::~ShellMainDelegate() {
120 bool ShellMainDelegate::BasicStartupComplete(int* exit_code) {
121 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
123 // "dump-render-tree" has been renamed to "run-layout-test", but the old
124 // flag name is still used in some places, so this check will remain until
125 // it is phased out entirely.
126 if (command_line.HasSwitch(switches::kDumpRenderTree))
127 command_line.AppendSwitch(switches::kRunLayoutTest);
129 #if defined(OS_WIN)
130 // Enable trace control and transport through event tracing for Windows.
131 logging::LogEventProvider::Initialize(kContentShellProviderName);
133 v8_breakpad_support::SetUp();
134 #endif
135 #if defined(OS_MACOSX)
136 // Needs to happen before InitializeResourceBundle() and before
137 // BlinkTestPlatformInitialize() are called.
138 OverrideFrameworkBundlePath();
139 OverrideChildProcessPath();
140 EnsureCorrectResolutionSettings();
141 #endif // OS_MACOSX
143 InitLogging();
144 if (command_line.HasSwitch(switches::kCheckLayoutTestSysDeps)) {
145 // If CheckLayoutSystemDeps succeeds, we don't exit early. Instead we
146 // continue and try to load the fonts in BlinkTestPlatformInitialize
147 // below, and then try to bring up the rest of the content module.
148 if (!CheckLayoutSystemDeps()) {
149 if (exit_code)
150 *exit_code = 1;
151 return true;
155 if (command_line.HasSwitch(switches::kRunLayoutTest)) {
156 EnableBrowserLayoutTestMode();
158 command_line.AppendSwitch(switches::kProcessPerTab);
159 command_line.AppendSwitch(switches::kEnableLogging);
160 command_line.AppendSwitch(switches::kAllowFileAccessFromFiles);
161 // only default to osmesa if the flag isn't already specified.
162 if (!command_line.HasSwitch(switches::kUseGL)) {
163 command_line.AppendSwitchASCII(switches::kUseGL,
164 gfx::kGLImplementationOSMesaName);
166 command_line.AppendSwitch(switches::kSkipGpuDataLoading);
167 command_line.AppendSwitchASCII(switches::kTouchEvents,
168 switches::kTouchEventsEnabled);
169 command_line.AppendSwitchASCII(switches::kForceDeviceScaleFactor, "1.0");
170 #if defined(OS_ANDROID)
171 command_line.AppendSwitch(
172 switches::kDisableGestureRequirementForMediaPlayback);
173 #endif
175 if (!command_line.HasSwitch(switches::kStableReleaseMode)) {
176 command_line.AppendSwitch(
177 switches::kEnableExperimentalWebPlatformFeatures);
178 // Only enable WebBluetooth during Layout Tests in non release mode.
179 command_line.AppendSwitch(switches::kEnableWebBluetooth);
182 if (!command_line.HasSwitch(switches::kEnableThreadedCompositing)) {
183 command_line.AppendSwitch(switches::kDisableThreadedCompositing);
184 command_line.AppendSwitch(cc::switches::kDisableThreadedAnimation);
187 if (!command_line.HasSwitch(switches::kEnableDisplayList2dCanvas)) {
188 command_line.AppendSwitch(switches::kDisableDisplayList2dCanvas);
191 command_line.AppendSwitch(switches::kEnableInbandTextTracks);
192 command_line.AppendSwitch(switches::kMuteAudio);
194 // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer.
195 command_line.AppendSwitch(switches::kDisableDelegatedRenderer);
196 command_line.AppendSwitch(cc::switches::kCompositeToMailbox);
198 command_line.AppendSwitch(cc::switches::kEnablePropertyTreeVerification);
200 command_line.AppendSwitch(switches::kEnablePreciseMemoryInfo);
202 command_line.AppendSwitchASCII(switches::kHostResolverRules,
203 "MAP *.test 127.0.0.1");
205 // Unless/until WebM files are added to the media layout tests, we need to
206 // avoid removing MP4/H264/AAC so that layout tests can run on Android.
207 #if !defined(OS_ANDROID)
208 media::RemoveProprietaryMediaTypesAndCodecsForTests();
209 #endif
211 if (!BlinkTestPlatformInitialize()) {
212 if (exit_code)
213 *exit_code = 1;
214 return true;
217 SetContentClient(&content_client_);
218 return false;
221 void ShellMainDelegate::PreSandboxStartup() {
222 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
223 // Create an instance of the CPU class to parse /proc/cpuinfo and cache
224 // cpu_brand info.
225 base::CPU cpu_info;
226 #endif
227 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
228 switches::kEnableCrashReporter)) {
229 std::string process_type =
230 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
231 switches::kProcessType);
232 crash_reporter::SetCrashReporterClient(g_shell_crash_client.Pointer());
233 #if defined(OS_MACOSX)
234 base::mac::DisableOSCrashDumps();
235 breakpad::InitCrashReporter(process_type);
236 breakpad::InitCrashProcessInfo(process_type);
237 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
238 if (process_type != switches::kZygoteProcess) {
239 #if defined(OS_ANDROID)
240 if (process_type.empty())
241 breakpad::InitCrashReporter(process_type);
242 else
243 breakpad::InitNonBrowserCrashReporterForAndroid(process_type);
244 #else
245 breakpad::InitCrashReporter(process_type);
246 #endif
248 #elif defined(OS_WIN)
249 UINT new_flags =
250 SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
251 UINT existing_flags = SetErrorMode(new_flags);
252 SetErrorMode(existing_flags | new_flags);
253 breakpad::InitCrashReporter(process_type);
254 #endif
257 InitializeResourceBundle();
260 int ShellMainDelegate::RunProcess(
261 const std::string& process_type,
262 const MainFunctionParams& main_function_params) {
263 if (!process_type.empty())
264 return -1;
266 #if !defined(OS_ANDROID)
267 // Android stores the BrowserMainRunner instance as a scoped member pointer
268 // on the ShellMainDelegate class because of different object lifetime.
269 scoped_ptr<BrowserMainRunner> browser_runner_;
270 #endif
272 browser_runner_.reset(BrowserMainRunner::Create());
273 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
274 return command_line.HasSwitch(switches::kRunLayoutTest) ||
275 command_line.HasSwitch(switches::kCheckLayoutTestSysDeps)
276 ? LayoutTestBrowserMain(main_function_params, browser_runner_)
277 : ShellBrowserMain(main_function_params, browser_runner_);
280 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
281 void ShellMainDelegate::ZygoteForked() {
282 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
283 switches::kEnableCrashReporter)) {
284 std::string process_type =
285 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
286 switches::kProcessType);
287 breakpad::InitCrashReporter(process_type);
290 #endif
292 void ShellMainDelegate::InitializeResourceBundle() {
293 #if defined(OS_ANDROID)
294 // On Android, the renderer runs with a different UID and can never access
295 // the file system. Use the file descriptor passed in at launch time.
296 auto* global_descriptors = base::GlobalDescriptors::GetInstance();
297 int pak_fd = global_descriptors->MaybeGet(kShellPakDescriptor);
298 base::MemoryMappedFile::Region pak_region;
299 if (pak_fd >= 0) {
300 pak_region = global_descriptors->GetRegion(kShellPakDescriptor);
301 } else {
302 pak_fd =
303 base::android::OpenApkAsset("assets/content_shell.pak", &pak_region);
304 // Loaded from disk for browsertests.
305 if (pak_fd < 0) {
306 base::FilePath pak_file;
307 bool r = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
308 DCHECK(r);
309 pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
310 pak_file = pak_file.Append(FILE_PATH_LITERAL("content_shell.pak"));
311 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
312 pak_fd = base::File(pak_file, flags).TakePlatformFile();
313 pak_region = base::MemoryMappedFile::Region::kWholeFile;
315 global_descriptors->Set(kShellPakDescriptor, pak_fd, pak_region);
317 DCHECK_GE(pak_fd, 0);
318 // This is clearly wrong. See crbug.com/330930
319 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
320 pak_region);
321 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
322 base::File(pak_fd), pak_region, ui::SCALE_FACTOR_100P);
323 #else // defined(OS_ANDROID)
324 #if defined(OS_MACOSX)
325 base::FilePath pak_file = GetResourcesPakFilePath();
326 #else
327 base::FilePath pak_file;
328 bool r = PathService::Get(base::DIR_MODULE, &pak_file);
329 DCHECK(r);
330 pak_file = pak_file.Append(FILE_PATH_LITERAL("content_shell.pak"));
331 #endif // defined(OS_MACOSX)
332 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
333 #endif // defined(OS_ANDROID)
336 ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() {
337 browser_client_.reset(base::CommandLine::ForCurrentProcess()->HasSwitch(
338 switches::kRunLayoutTest)
339 ? new LayoutTestContentBrowserClient
340 : new ShellContentBrowserClient);
342 return browser_client_.get();
345 ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() {
346 renderer_client_.reset(base::CommandLine::ForCurrentProcess()->HasSwitch(
347 switches::kRunLayoutTest)
348 ? new LayoutTestContentRendererClient
349 : new ShellContentRendererClient);
351 return renderer_client_.get();
354 ContentUtilityClient* ShellMainDelegate::CreateContentUtilityClient() {
355 utility_client_.reset(new ShellContentUtilityClient);
356 return utility_client_.get();
359 } // namespace content