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