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"
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 "cc/base/switches.h"
16 #include "content/public/browser/browser_main_runner.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/url_constants.h"
19 #include "content/public/test/layouttest_support.h"
20 #include "content/shell/app/blink_test_platform_support.h"
21 #include "content/shell/app/shell_crash_reporter_client.h"
22 #include "content/shell/browser/layout_test/layout_test_browser_main.h"
23 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h"
24 #include "content/shell/browser/shell_browser_main.h"
25 #include "content/shell/browser/shell_content_browser_client.h"
26 #include "content/shell/common/shell_switches.h"
27 #include "content/shell/renderer/layout_test/layout_test_content_renderer_client.h"
28 #include "content/shell/renderer/shell_content_renderer_client.h"
29 #include "media/base/media_switches.h"
30 #include "net/cookies/cookie_monster.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/base/ui_base_paths.h"
33 #include "ui/base/ui_base_switches.h"
34 #include "ui/events/event_switches.h"
35 #include "ui/gfx/switches.h"
36 #include "ui/gl/gl_switches.h"
38 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
40 #if defined(IPC_MESSAGE_LOG_ENABLED)
41 #define IPC_MESSAGE_MACROS_LOG_ENABLED
42 #include "content/public/common/content_ipc_logging.h"
43 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
44 content::RegisterIPCLogger(msg_id, logger)
45 #include "content/shell/common/shell_messages.h"
48 #if defined(OS_ANDROID)
49 #include "base/posix/global_descriptors.h"
50 #include "content/shell/android/shell_descriptors.h"
53 #if defined(OS_MACOSX)
54 #include "base/mac/os_crash_dumps.h"
55 #include "components/crash/app/breakpad_mac.h"
56 #include "content/shell/app/paths_mac.h"
57 #include "content/shell/app/shell_main_delegate_mac.h"
63 #include "base/logging_win.h"
64 #include "components/crash/app/breakpad_win.h"
65 #include "content/shell/common/v8_breakpad_support_win.h"
68 #if defined(OS_POSIX) && !defined(OS_MACOSX)
69 #include "components/crash/app/breakpad_linux.h"
74 base::LazyInstance
<content::ShellCrashReporterClient
>::Leaky
75 g_shell_crash_client
= LAZY_INSTANCE_INITIALIZER
;
78 // If "Content Shell" doesn't show up in your list of trace providers in
79 // Sawbuck, add these registry entries to your machine (NOTE the optional
80 // Wow6432Node key for x64 machines):
81 // 1. Find: HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers
82 // 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}"
83 // 3. Add these values:
84 // "default_flags"=dword:00000001
85 // "default_level"=dword:00000004
88 // {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}
89 const GUID kContentShellProviderName
= {
90 0x6a3e50a4, 0x7e15, 0x4099,
91 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } };
95 base::FilePath log_filename
;
96 PathService::Get(base::DIR_EXE
, &log_filename
);
97 log_filename
= log_filename
.AppendASCII("content_shell.log");
98 logging::LoggingSettings settings
;
99 settings
.logging_dest
= logging::LOG_TO_ALL
;
100 settings
.log_file
= log_filename
.value().c_str();
101 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
102 logging::InitLogging(settings
);
103 logging::SetLogItems(true, true, true, true);
110 ShellMainDelegate::ShellMainDelegate() {
113 ShellMainDelegate::~ShellMainDelegate() {
116 bool ShellMainDelegate::BasicStartupComplete(int* exit_code
) {
117 base::CommandLine
& command_line
= *base::CommandLine::ForCurrentProcess();
119 // "dump-render-tree" has been renamed to "run-layout-test", but the old
120 // flag name is still used in some places, so this check will remain until
121 // it is phased out entirely.
122 if (command_line
.HasSwitch(switches::kDumpRenderTree
))
123 command_line
.AppendSwitch(switches::kRunLayoutTest
);
126 // Enable trace control and transport through event tracing for Windows.
127 logging::LogEventProvider::Initialize(kContentShellProviderName
);
129 v8_breakpad_support::SetUp();
131 #if defined(OS_MACOSX)
132 // Needs to happen before InitializeResourceBundle() and before
133 // BlinkTestPlatformInitialize() are called.
134 OverrideFrameworkBundlePath();
135 OverrideChildProcessPath();
136 EnsureCorrectResolutionSettings();
140 if (command_line
.HasSwitch(switches::kCheckLayoutTestSysDeps
)) {
141 // If CheckLayoutSystemDeps succeeds, we don't exit early. Instead we
142 // continue and try to load the fonts in BlinkTestPlatformInitialize
143 // below, and then try to bring up the rest of the content module.
144 if (!CheckLayoutSystemDeps()) {
151 if (command_line
.HasSwitch(switches::kRunLayoutTest
)) {
152 EnableBrowserLayoutTestMode();
154 command_line
.AppendSwitch(switches::kProcessPerTab
);
155 command_line
.AppendSwitch(switches::kEnableLogging
);
156 command_line
.AppendSwitch(switches::kAllowFileAccessFromFiles
);
157 // only default to osmesa if the flag isn't already specified.
158 if (!command_line
.HasSwitch(switches::kUseGL
)) {
159 command_line
.AppendSwitchASCII(switches::kUseGL
,
160 gfx::kGLImplementationOSMesaName
);
162 command_line
.AppendSwitch(switches::kSkipGpuDataLoading
);
163 command_line
.AppendSwitchASCII(switches::kTouchEvents
,
164 switches::kTouchEventsEnabled
);
165 command_line
.AppendSwitchASCII(switches::kForceDeviceScaleFactor
, "1.0");
166 #if defined(OS_ANDROID)
167 command_line
.AppendSwitch(
168 switches::kDisableGestureRequirementForMediaPlayback
);
171 if (!command_line
.HasSwitch(switches::kStableReleaseMode
)) {
172 command_line
.AppendSwitch(
173 switches::kEnableExperimentalWebPlatformFeatures
);
176 if (!command_line
.HasSwitch(switches::kEnableThreadedCompositing
)) {
177 command_line
.AppendSwitch(switches::kDisableThreadedCompositing
);
178 command_line
.AppendSwitch(cc::switches::kDisableThreadedAnimation
);
179 // Text blobs are normally disabled when kDisableImplSidePainting is
180 // present to ensure correct LCD behavior, but for layout tests we want
181 // them on because LCD is always suppressed.
182 command_line
.AppendSwitch(switches::kForceTextBlobs
);
185 if (!command_line
.HasSwitch(switches::kEnableDisplayList2dCanvas
)) {
186 command_line
.AppendSwitch(switches::kDisableDisplayList2dCanvas
);
189 command_line
.AppendSwitch(switches::kEnableInbandTextTracks
);
190 command_line
.AppendSwitch(switches::kMuteAudio
);
192 // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer.
193 command_line
.AppendSwitch(switches::kDisableDelegatedRenderer
);
194 command_line
.AppendSwitch(cc::switches::kCompositeToMailbox
);
196 command_line
.AppendSwitch(cc::switches::kEnablePropertyTreeVerification
);
198 command_line
.AppendSwitch(switches::kEnablePreciseMemoryInfo
);
200 command_line
.AppendSwitchASCII(switches::kHostResolverRules
,
201 "MAP *.test 127.0.0.1");
203 // TODO(wfh): crbug.com/295137 Remove this when NPAPI is gone.
204 command_line
.AppendSwitch(switches::kEnableNpapi
);
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 net::RemoveProprietaryMediaTypesAndCodecsForTests();
212 if (!BlinkTestPlatformInitialize()) {
218 SetContentClient(&content_client_
);
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
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
);
244 breakpad::InitNonBrowserCrashReporterForAndroid(process_type
);
246 breakpad::InitCrashReporter(process_type
);
249 #elif defined(OS_WIN)
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
);
258 InitializeResourceBundle();
261 int ShellMainDelegate::RunProcess(
262 const std::string
& process_type
,
263 const MainFunctionParams
& main_function_params
) {
264 if (!process_type
.empty())
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_
;
273 browser_runner_
.reset(BrowserMainRunner::Create());
274 base::CommandLine
& command_line
= *base::CommandLine::ForCurrentProcess();
275 return command_line
.HasSwitch(switches::kRunLayoutTest
) ||
276 command_line
.HasSwitch(switches::kCheckLayoutTestSysDeps
)
277 ? LayoutTestBrowserMain(main_function_params
, browser_runner_
)
278 : ShellBrowserMain(main_function_params
, browser_runner_
);
281 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
282 void ShellMainDelegate::ZygoteForked() {
283 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
284 switches::kEnableCrashReporter
)) {
285 std::string process_type
=
286 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
287 switches::kProcessType
);
288 breakpad::InitCrashReporter(process_type
);
293 void ShellMainDelegate::InitializeResourceBundle() {
294 #if defined(OS_ANDROID)
295 // In the Android case, the renderer runs with a different UID and can never
296 // access the file system. So we are passed a file descriptor to the
297 // ResourceBundle pak at launch time.
299 base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor
);
301 // This is clearly wrong. See crbug.com/330930
302 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
303 base::File(pak_fd
), base::MemoryMappedFile::Region::kWholeFile
);
304 ResourceBundle::GetSharedInstance().AddDataPackFromFile(
305 base::File(pak_fd
), ui::SCALE_FACTOR_100P
);
310 base::FilePath pak_file
;
311 #if defined(OS_MACOSX)
312 pak_file
= GetResourcesPakFilePath();
314 base::FilePath pak_dir
;
316 #if defined(OS_ANDROID)
317 bool got_path
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &pak_dir
);
319 pak_dir
= pak_dir
.Append(FILE_PATH_LITERAL("paks"));
321 PathService::Get(base::DIR_MODULE
, &pak_dir
);
324 pak_file
= pak_dir
.Append(FILE_PATH_LITERAL("content_shell.pak"));
326 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file
);
329 ContentBrowserClient
* ShellMainDelegate::CreateContentBrowserClient() {
330 browser_client_
.reset(base::CommandLine::ForCurrentProcess()->HasSwitch(
331 switches::kRunLayoutTest
)
332 ? new LayoutTestContentBrowserClient
333 : new ShellContentBrowserClient
);
335 return browser_client_
.get();
338 ContentRendererClient
* ShellMainDelegate::CreateContentRendererClient() {
339 renderer_client_
.reset(base::CommandLine::ForCurrentProcess()->HasSwitch(
340 switches::kRunLayoutTest
)
341 ? new LayoutTestContentRendererClient
342 : new ShellContentRendererClient
);
344 return renderer_client_
.get();
347 } // namespace content