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/shell_breakpad_client.h"
21 #include "content/shell/app/webkit_test_platform_support.h"
22 #include "content/shell/browser/shell_browser_main.h"
23 #include "content/shell/browser/shell_content_browser_client.h"
24 #include "content/shell/common/shell_switches.h"
25 #include "content/shell/renderer/shell_content_renderer_client.h"
26 #include "net/cookies/cookie_monster.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/base/ui_base_paths.h"
29 #include "ui/base/ui_base_switches.h"
30 #include "ui/events/event_switches.h"
31 #include "ui/gfx/switches.h"
32 #include "ui/gl/gl_switches.h"
34 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
36 #if defined(IPC_MESSAGE_LOG_ENABLED)
37 #define IPC_MESSAGE_MACROS_LOG_ENABLED
38 #include "content/public/common/content_ipc_logging.h"
39 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
40 content::RegisterIPCLogger(msg_id, logger)
41 #include "content/shell/common/shell_messages.h"
44 #if defined(OS_ANDROID)
45 #include "base/posix/global_descriptors.h"
46 #include "content/shell/android/shell_descriptors.h"
49 #if defined(OS_MACOSX)
50 #include "base/mac/os_crash_dumps.h"
51 #include "components/crash/app/breakpad_mac.h"
52 #include "content/shell/app/paths_mac.h"
53 #include "content/shell/app/shell_main_delegate_mac.h"
59 #include "base/logging_win.h"
60 #include "components/crash/app/breakpad_win.h"
63 #if defined(OS_POSIX) && !defined(OS_MACOSX)
64 #include "components/crash/app/breakpad_linux.h"
69 base::LazyInstance
<content::ShellBreakpadClient
>::Leaky
70 g_shell_breakpad_client
= LAZY_INSTANCE_INITIALIZER
;
73 // If "Content Shell" doesn't show up in your list of trace providers in
74 // Sawbuck, add these registry entries to your machine (NOTE the optional
75 // Wow6432Node key for x64 machines):
76 // 1. Find: HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers
77 // 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}"
78 // 3. Add these values:
79 // "default_flags"=dword:00000001
80 // "default_level"=dword:00000004
83 // {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}
84 const GUID kContentShellProviderName
= {
85 0x6a3e50a4, 0x7e15, 0x4099,
86 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } };
90 base::FilePath log_filename
;
91 PathService::Get(base::DIR_EXE
, &log_filename
);
92 log_filename
= log_filename
.AppendASCII("content_shell.log");
93 logging::LoggingSettings settings
;
94 settings
.logging_dest
= logging::LOG_TO_ALL
;
95 settings
.log_file
= log_filename
.value().c_str();
96 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
97 logging::InitLogging(settings
);
98 logging::SetLogItems(true, true, true, true);
105 ShellMainDelegate::ShellMainDelegate() {
108 ShellMainDelegate::~ShellMainDelegate() {
111 bool ShellMainDelegate::BasicStartupComplete(int* exit_code
) {
113 // Enable trace control and transport through event tracing for Windows.
114 logging::LogEventProvider::Initialize(kContentShellProviderName
);
116 #if defined(OS_MACOSX)
117 // Needs to happen before InitializeResourceBundle() and before
118 // WebKitTestPlatformInitialize() are called.
119 OverrideFrameworkBundlePath();
120 OverrideChildProcessPath();
121 EnsureCorrectResolutionSettings();
125 CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
126 if (command_line
.HasSwitch(switches::kCheckLayoutTestSysDeps
)) {
127 // If CheckLayoutSystemDeps succeeds, we don't exit early. Instead we
128 // continue and try to load the fonts in WebKitTestPlatformInitialize
129 // below, and then try to bring up the rest of the content module.
130 if (!CheckLayoutSystemDeps()) {
137 if (command_line
.HasSwitch(switches::kDumpRenderTree
)) {
138 EnableBrowserLayoutTestMode();
140 command_line
.AppendSwitch(switches::kProcessPerTab
);
141 command_line
.AppendSwitch(switches::kEnableLogging
);
142 command_line
.AppendSwitch(switches::kAllowFileAccessFromFiles
);
143 command_line
.AppendSwitchASCII(switches::kUseGL
,
144 gfx::kGLImplementationOSMesaName
);
145 command_line
.AppendSwitch(switches::kSkipGpuDataLoading
);
146 command_line
.AppendSwitchASCII(switches::kTouchEvents
,
147 switches::kTouchEventsEnabled
);
148 command_line
.AppendSwitchASCII(switches::kForceDeviceScaleFactor
, "1.0");
149 #if defined(OS_ANDROID)
150 command_line
.AppendSwitch(
151 switches::kDisableGestureRequirementForMediaPlayback
);
154 if (!command_line
.HasSwitch(switches::kStableReleaseMode
)) {
155 command_line
.AppendSwitch(
156 switches::kEnableExperimentalWebPlatformFeatures
);
159 if (!command_line
.HasSwitch(switches::kEnableThreadedCompositing
)) {
160 command_line
.AppendSwitch(switches::kDisableThreadedCompositing
);
161 command_line
.AppendSwitch(cc::switches::kDisableThreadedAnimation
);
162 command_line
.AppendSwitch(switches::kDisableImplSidePainting
);
165 command_line
.AppendSwitch(switches::kEnableInbandTextTracks
);
166 command_line
.AppendSwitch(switches::kMuteAudio
);
168 #if defined(USE_AURA) || defined(OS_ANDROID) || defined(OS_MACOSX)
169 // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer.
170 command_line
.AppendSwitch(switches::kDisableDelegatedRenderer
);
171 command_line
.AppendSwitch(cc::switches::kCompositeToMailbox
);
174 command_line
.AppendSwitch(switches::kEnableFileCookies
);
176 command_line
.AppendSwitch(switches::kEnablePreciseMemoryInfo
);
178 // Unless/until WebM files are added to the media layout tests, we need to
179 // avoid removing MP4/H264/AAC so that layout tests can run on Android.
180 #if !defined(OS_ANDROID)
181 net::RemoveProprietaryMediaTypesAndCodecsForTests();
184 if (!WebKitTestPlatformInitialize()) {
190 SetContentClient(&content_client_
);
194 void ShellMainDelegate::PreSandboxStartup() {
195 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
196 // Create an instance of the CPU class to parse /proc/cpuinfo and cache
200 if (CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kEnableCrashReporter
)) {
202 std::string process_type
=
203 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
204 switches::kProcessType
);
205 breakpad::SetBreakpadClient(g_shell_breakpad_client
.Pointer());
206 #if defined(OS_MACOSX)
207 base::mac::DisableOSCrashDumps();
208 breakpad::InitCrashReporter(process_type
);
209 breakpad::InitCrashProcessInfo(process_type
);
210 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
211 if (process_type
!= switches::kZygoteProcess
) {
212 #if defined(OS_ANDROID)
213 if (process_type
.empty())
214 breakpad::InitCrashReporter(process_type
);
216 breakpad::InitNonBrowserCrashReporterForAndroid(process_type
);
218 breakpad::InitCrashReporter(process_type
);
221 #elif defined(OS_WIN)
223 SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
| SEM_NOOPENFILEERRORBOX
;
224 UINT existing_flags
= SetErrorMode(new_flags
);
225 SetErrorMode(existing_flags
| new_flags
);
226 breakpad::InitCrashReporter(process_type
);
230 InitializeResourceBundle();
233 int ShellMainDelegate::RunProcess(
234 const std::string
& process_type
,
235 const MainFunctionParams
& main_function_params
) {
236 if (!process_type
.empty())
239 #if !defined(OS_ANDROID)
240 // Android stores the BrowserMainRunner instance as a scoped member pointer
241 // on the ShellMainDelegate class because of different object lifetime.
242 scoped_ptr
<BrowserMainRunner
> browser_runner_
;
245 browser_runner_
.reset(BrowserMainRunner::Create());
246 return ShellBrowserMain(main_function_params
, browser_runner_
);
249 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
250 void ShellMainDelegate::ZygoteForked() {
251 if (CommandLine::ForCurrentProcess()->HasSwitch(
252 switches::kEnableCrashReporter
)) {
253 std::string process_type
=
254 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
255 switches::kProcessType
);
256 breakpad::InitCrashReporter(process_type
);
261 void ShellMainDelegate::InitializeResourceBundle() {
262 #if defined(OS_ANDROID)
263 // In the Android case, the renderer runs with a different UID and can never
264 // access the file system. So we are passed a file descriptor to the
265 // ResourceBundle pak at launch time.
267 base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor
);
269 // This is clearly wrong. See crbug.com/330930
270 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
271 base::File(pak_fd
), base::MemoryMappedFile::Region::kWholeFile
);
272 ResourceBundle::GetSharedInstance().AddDataPackFromFile(
273 base::File(pak_fd
), ui::SCALE_FACTOR_100P
);
278 base::FilePath pak_file
;
279 #if defined(OS_MACOSX)
280 pak_file
= GetResourcesPakFilePath();
282 base::FilePath pak_dir
;
284 #if defined(OS_ANDROID)
285 bool got_path
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &pak_dir
);
287 pak_dir
= pak_dir
.Append(FILE_PATH_LITERAL("paks"));
289 PathService::Get(base::DIR_MODULE
, &pak_dir
);
292 pak_file
= pak_dir
.Append(FILE_PATH_LITERAL("content_shell.pak"));
294 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file
);
297 ContentBrowserClient
* ShellMainDelegate::CreateContentBrowserClient() {
298 browser_client_
.reset(new ShellContentBrowserClient
);
299 return browser_client_
.get();
302 ContentRendererClient
* ShellMainDelegate::CreateContentRendererClient() {
303 renderer_client_
.reset(new ShellContentRendererClient
);
304 return renderer_client_
.get();
307 } // namespace content