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