shell_main_delegate.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 5#include "content/shell/app/shell_main_delegate.h" 6 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 "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" 33 34#include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. 35 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" 42#endif 43 44#if defined(OS_ANDROID) 45#include "base/posix/global_descriptors.h" 46#include "content/shell/android/shell_descriptors.h" 47#endif 48 49#if defined(OS_MACOSX) 50#include "base/mac/os_crash_dumps.h" 51#include "components/breakpad/app/breakpad_mac.h" 52#include "content/shell/app/paths_mac.h" 53#include "content/shell/app/shell_main_delegate_mac.h" 54#endif // OS_MACOSX 55 56#if defined(OS_WIN) 57#include <initguid.h> 58#include <windows.h> 59#include "base/logging_win.h" 60#include "components/breakpad/app/breakpad_win.h" 61#endif 62 63#if defined(OS_POSIX) && !defined(OS_MACOSX) 64#include "components/breakpad/app/breakpad_linux.h" 65#endif 66 67namespace { 68 69base::LazyInstance<content::ShellBreakpadClient>::Leaky 70 g_shell_breakpad_client = LAZY_INSTANCE_INITIALIZER; 71 72#if defined(OS_WIN) 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 81// @="Content Shell" 82 83// {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6} 84const GUID kContentShellProviderName = { 85 0x6a3e50a4, 0x7e15, 0x4099, 86 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } }; 87#endif 88 89void InitLogging() { 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); 99} 100 101} // namespace 102 103namespace content { 104 105ShellMainDelegate::ShellMainDelegate() { 106} 107 108ShellMainDelegate::~ShellMainDelegate() { 109} 110 111bool ShellMainDelegate::BasicStartupComplete(int* exit_code) { 112#if defined(OS_WIN) 113 // Enable trace control and transport through event tracing for Windows. 114 logging::LogEventProvider::Initialize(kContentShellProviderName); 115#endif 116#if defined(OS_MACOSX) 117 // Needs to happen before InitializeResourceBundle() and before 118 // WebKitTestPlatformInitialize() are called. 119 OverrideFrameworkBundlePath(); 120 OverrideChildProcessPath(); 121 EnsureCorrectResolutionSettings(); 122#endif // OS_MACOSX 123 124 InitLogging(); 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()) { 131 if (exit_code) 132 *exit_code = 1; 133 return true; 134 } 135 } 136 137 if (command_line.HasSwitch(switches::kDumpRenderTree)) { 138 EnableBrowserLayoutTestMode(); 139 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); 152#endif 153 154 if (!command_line.HasSwitch(switches::kStableReleaseMode)) { 155 command_line.AppendSwitch( 156 switches::kEnableExperimentalWebPlatformFeatures); 157 } 158 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); 163 } 164 165 command_line.AppendSwitch(switches::kEnableInbandTextTracks); 166 command_line.AppendSwitch(switches::kMuteAudio); 167 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); 172#endif 173 174 command_line.AppendSwitch(switches::kEnableFileCookies); 175 176 command_line.AppendSwitch(switches::kEnablePreciseMemoryInfo); 177 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(); 182#endif 183 184 if (!WebKitTestPlatformInitialize()) { 185 if (exit_code) 186 *exit_code = 1; 187 return true; 188 } 189 } 190 SetContentClient(&content_client_); 191 return false; 192} 193 194void 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 197 // cpu_brand info. 198 base::CPU cpu_info; 199#endif 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); 215 else 216 breakpad::InitNonBrowserCrashReporterForAndroid(process_type); 217#else 218 breakpad::InitCrashReporter(process_type); 219#endif 220 } 221#elif defined(OS_WIN) 222 UINT new_flags = 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); 227#endif 228 } 229 230 InitializeResourceBundle(); 231} 232 233int ShellMainDelegate::RunProcess( 234 const std::string& process_type, 235 const MainFunctionParams& main_function_params) { 236 if (!process_type.empty()) 237 return -1; 238 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_; 243#endif 244 245 browser_runner_.reset(BrowserMainRunner::Create()); 246 return ShellBrowserMain(main_function_params, browser_runner_); 247} 248 249#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) 250void 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); 257 } 258} 259#endif 260 261void 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. 266 int pak_fd = 267 base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor); 268 if (pak_fd >= 0) { 269 // This is clearly wrong. See crbug.com/330930 270 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion( 271 base::File(pak_fd), base::MemoryMappedFile::Region::kWholeFile, false); 272 ResourceBundle::GetSharedInstance().AddDataPackFromFile( 273 base::File(pak_fd), ui::SCALE_FACTOR_100P); 274 return; 275 } 276#endif 277 278 base::FilePath pak_file; 279#if defined(OS_MACOSX) 280 pak_file = GetResourcesPakFilePath(); 281#else 282 base::FilePath pak_dir; 283 284#if defined(OS_ANDROID) 285 bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir); 286 DCHECK(got_path); 287 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); 288#else 289 PathService::Get(base::DIR_MODULE, &pak_dir); 290#endif 291 292 pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")); 293#endif 294 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file); 295} 296 297ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() { 298 browser_client_.reset(new ShellContentBrowserClient); 299 return browser_client_.get(); 300} 301 302ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() { 303 renderer_client_.reset(new ShellContentRendererClient); 304 return renderer_client_.get(); 305} 306 307} // namespace content 308