shell_main_delegate.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/files/file.h" 10#include "base/files/file_path.h" 11#include "base/lazy_instance.h" 12#include "base/logging.h" 13#include "base/path_service.h" 14#include "cc/base/switches.h" 15#include "content/public/browser/browser_main_runner.h" 16#include "content/public/common/content_switches.h" 17#include "content/public/common/url_constants.h" 18#include "content/public/test/layouttest_support.h" 19#include "content/shell/app/shell_breakpad_client.h" 20#include "content/shell/app/webkit_test_platform_support.h" 21#include "content/shell/browser/shell_browser_main.h" 22#include "content/shell/browser/shell_content_browser_client.h" 23#include "content/shell/common/shell_switches.h" 24#include "content/shell/renderer/shell_content_renderer_client.h" 25#include "net/cookies/cookie_monster.h" 26#include "ui/base/resource/resource_bundle.h" 27#include "ui/base/ui_base_paths.h" 28#include "ui/base/ui_base_switches.h" 29#include "ui/events/event_switches.h" 30#include "ui/gfx/switches.h" 31#include "ui/gl/gl_switches.h" 32 33#include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. 34 35#if defined(IPC_MESSAGE_LOG_ENABLED) 36#define IPC_MESSAGE_MACROS_LOG_ENABLED 37#include "content/public/common/content_ipc_logging.h" 38#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ 39 content::RegisterIPCLogger(msg_id, logger) 40#include "content/shell/common/shell_messages.h" 41#endif 42 43#if defined(OS_ANDROID) 44#include "base/posix/global_descriptors.h" 45#include "content/shell/android/shell_descriptors.h" 46#endif 47 48#if defined(OS_MACOSX) 49#include "base/mac/os_crash_dumps.h" 50#include "components/breakpad/app/breakpad_mac.h" 51#include "content/shell/app/paths_mac.h" 52#include "content/shell/app/shell_main_delegate_mac.h" 53#endif // OS_MACOSX 54 55#if defined(OS_WIN) 56#include <initguid.h> 57#include <windows.h> 58#include "base/logging_win.h" 59#include "components/breakpad/app/breakpad_win.h" 60#endif 61 62#if defined(OS_POSIX) && !defined(OS_MACOSX) 63#include "components/breakpad/app/breakpad_linux.h" 64#endif 65 66namespace { 67 68base::LazyInstance<content::ShellBreakpadClient>::Leaky 69 g_shell_breakpad_client = LAZY_INSTANCE_INITIALIZER; 70 71#if defined(OS_WIN) 72// If "Content Shell" doesn't show up in your list of trace providers in 73// Sawbuck, add these registry entries to your machine (NOTE the optional 74// Wow6432Node key for x64 machines): 75// 1. Find: HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers 76// 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}" 77// 3. Add these values: 78// "default_flags"=dword:00000001 79// "default_level"=dword:00000004 80// @="Content Shell" 81 82// {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6} 83const GUID kContentShellProviderName = { 84 0x6a3e50a4, 0x7e15, 0x4099, 85 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } }; 86#endif 87 88void InitLogging() { 89 base::FilePath log_filename; 90 PathService::Get(base::DIR_EXE, &log_filename); 91 log_filename = log_filename.AppendASCII("content_shell.log"); 92 logging::LoggingSettings settings; 93 settings.logging_dest = logging::LOG_TO_ALL; 94 settings.log_file = log_filename.value().c_str(); 95 settings.delete_old = logging::DELETE_OLD_LOG_FILE; 96 logging::InitLogging(settings); 97 logging::SetLogItems(true, true, true, true); 98} 99 100} // namespace 101 102namespace content { 103 104ShellMainDelegate::ShellMainDelegate() { 105} 106 107ShellMainDelegate::~ShellMainDelegate() { 108} 109 110bool ShellMainDelegate::BasicStartupComplete(int* exit_code) { 111#if defined(OS_WIN) 112 // Enable trace control and transport through event tracing for Windows. 113 logging::LogEventProvider::Initialize(kContentShellProviderName); 114#endif 115#if defined(OS_MACOSX) 116 // Needs to happen before InitializeResourceBundle() and before 117 // WebKitTestPlatformInitialize() are called. 118 OverrideFrameworkBundlePath(); 119 OverrideChildProcessPath(); 120 EnsureCorrectResolutionSettings(); 121#endif // OS_MACOSX 122 123 InitLogging(); 124 CommandLine& command_line = *CommandLine::ForCurrentProcess(); 125 if (command_line.HasSwitch(switches::kCheckLayoutTestSysDeps)) { 126 if (!CheckLayoutSystemDeps()) { 127 if (exit_code) 128 *exit_code = 1; 129 return true; 130 } 131 } 132 133 if (command_line.HasSwitch(switches::kDumpRenderTree)) { 134 EnableBrowserLayoutTestMode(); 135 136 command_line.AppendSwitch(switches::kProcessPerTab); 137 command_line.AppendSwitch(switches::kEnableLogging); 138 command_line.AppendSwitch(switches::kAllowFileAccessFromFiles); 139#if !defined(OS_ANDROID) 140 // OSMesa is not yet available for Android. http://crbug.com/248925 141 command_line.AppendSwitchASCII( 142 switches::kUseGL, gfx::kGLImplementationOSMesaName); 143#endif 144 command_line.AppendSwitch(switches::kSkipGpuDataLoading); 145 command_line.AppendSwitchASCII(switches::kTouchEvents, 146 switches::kTouchEventsEnabled); 147 command_line.AppendSwitch(switches::kEnableGestureTapHighlight); 148 command_line.AppendSwitchASCII(switches::kForceDeviceScaleFactor, "1.0"); 149#if defined(OS_ANDROID) 150 command_line.AppendSwitch( 151 switches::kDisableGestureRequirementForMediaPlayback); 152 // Capturing pixel results does not yet work when implementation-side 153 // painting is enabled. See http://crbug.com/250777 154 command_line.AppendSwitch(cc::switches::kDisableImplSidePainting); 155#endif 156 157 if (!command_line.HasSwitch(switches::kStableReleaseMode)) { 158 command_line.AppendSwitch( 159 switches::kEnableExperimentalWebPlatformFeatures); 160 } 161 162 if (!command_line.HasSwitch(switches::kEnableThreadedCompositing)) { 163 command_line.AppendSwitch(switches::kDisableThreadedCompositing); 164 command_line.AppendSwitch(cc::switches::kDisableThreadedAnimation); 165 } 166 167 command_line.AppendSwitch(switches::kEnableInbandTextTracks); 168 command_line.AppendSwitch(switches::kMuteAudio); 169 170#if defined(USE_AURA) 171 // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer. 172 command_line.AppendSwitch(switches::kDisableDelegatedRenderer); 173#endif 174 175 command_line.AppendSwitch(switches::kEnableFileCookies); 176 177 // Unless/until WebM files are added to the media layout tests, we need to 178 // avoid removing MP4/H264/AAC so that layout tests can run on Android. 179#if !defined(OS_ANDROID) 180 net::RemoveProprietaryMediaTypesAndCodecsForTests(); 181#endif 182 183 if (!WebKitTestPlatformInitialize()) { 184 if (exit_code) 185 *exit_code = 1; 186 return true; 187 } 188 } 189 SetContentClient(&content_client_); 190 return false; 191} 192 193void ShellMainDelegate::PreSandboxStartup() { 194 if (CommandLine::ForCurrentProcess()->HasSwitch( 195 switches::kEnableCrashReporter)) { 196 std::string process_type = 197 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 198 switches::kProcessType); 199 breakpad::SetBreakpadClient(g_shell_breakpad_client.Pointer()); 200#if defined(OS_MACOSX) 201 base::mac::DisableOSCrashDumps(); 202 breakpad::InitCrashReporter(process_type); 203 breakpad::InitCrashProcessInfo(process_type); 204#elif defined(OS_POSIX) && !defined(OS_MACOSX) 205 if (process_type != switches::kZygoteProcess) { 206#if defined(OS_ANDROID) 207 if (process_type.empty()) 208 breakpad::InitCrashReporter(process_type); 209 else 210 breakpad::InitNonBrowserCrashReporterForAndroid(process_type); 211#else 212 breakpad::InitCrashReporter(process_type); 213#endif 214 } 215#elif defined(OS_WIN) 216 UINT new_flags = 217 SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX; 218 UINT existing_flags = SetErrorMode(new_flags); 219 SetErrorMode(existing_flags | new_flags); 220 breakpad::InitCrashReporter(process_type); 221#endif 222 } 223 224 InitializeResourceBundle(); 225} 226 227int ShellMainDelegate::RunProcess( 228 const std::string& process_type, 229 const MainFunctionParams& main_function_params) { 230 if (!process_type.empty()) 231 return -1; 232 233#if !defined(OS_ANDROID) 234 // Android stores the BrowserMainRunner instance as a scoped member pointer 235 // on the ShellMainDelegate class because of different object lifetime. 236 scoped_ptr<BrowserMainRunner> browser_runner_; 237#endif 238 239 browser_runner_.reset(BrowserMainRunner::Create()); 240 return ShellBrowserMain(main_function_params, browser_runner_); 241} 242 243#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) 244void ShellMainDelegate::ZygoteForked() { 245 if (CommandLine::ForCurrentProcess()->HasSwitch( 246 switches::kEnableCrashReporter)) { 247 std::string process_type = 248 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 249 switches::kProcessType); 250 breakpad::InitCrashReporter(process_type); 251 } 252} 253#endif 254 255void ShellMainDelegate::InitializeResourceBundle() { 256#if defined(OS_ANDROID) 257 // In the Android case, the renderer runs with a different UID and can never 258 // access the file system. So we are passed a file descriptor to the 259 // ResourceBundle pak at launch time. 260 int pak_fd = 261 base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor); 262 if (pak_fd != base::kInvalidPlatformFileValue) { 263 ui::ResourceBundle::InitSharedInstanceWithPakFile(base::File(pak_fd), 264 false); 265 ResourceBundle::GetSharedInstance().AddDataPackFromFile( 266 base::File(pak_fd), ui::SCALE_FACTOR_100P); 267 return; 268 } 269#endif 270 271 base::FilePath pak_file; 272#if defined(OS_MACOSX) 273 pak_file = GetResourcesPakFilePath(); 274#else 275 base::FilePath pak_dir; 276 277#if defined(OS_ANDROID) 278 bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir); 279 DCHECK(got_path); 280 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); 281#else 282 PathService::Get(base::DIR_MODULE, &pak_dir); 283#endif 284 285 pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")); 286#endif 287 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file); 288} 289 290ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() { 291 browser_client_.reset(new ShellContentBrowserClient); 292 return browser_client_.get(); 293} 294 295ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() { 296 renderer_client_.reset(new ShellContentRendererClient); 297 return renderer_client_.get(); 298} 299 300} // namespace content 301