shell_main_delegate.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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/command_line.h" 8#include "base/files/file_path.h" 9#include "base/logging.h" 10#include "base/path_service.h" 11#include "content/public/browser/browser_main_runner.h" 12#include "content/public/common/content_switches.h" 13#include "content/public/common/url_constants.h" 14#include "content/public/test/layouttest_support.h" 15#include "content/shell/common/shell_switches.h" 16#include "content/shell/renderer/shell_content_renderer_client.h" 17#include "content/shell/shell_browser_main.h" 18#include "content/shell/shell_content_browser_client.h" 19#include "content/shell/webkit_test_platform_support.h" 20#include "net/cookies/cookie_monster.h" 21#include "ui/base/resource/resource_bundle.h" 22#include "ui/base/ui_base_paths.h" 23#include "ui/base/ui_base_switches.h" 24#include "ui/gl/gl_switches.h" 25 26#include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. 27 28#if defined(IPC_MESSAGE_LOG_ENABLED) 29#define IPC_MESSAGE_MACROS_LOG_ENABLED 30#include "content/public/common/content_ipc_logging.h" 31#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ 32 content::RegisterIPCLogger(msg_id, logger) 33#include "content/shell/common/shell_messages.h" 34#endif 35 36#if defined(OS_ANDROID) 37#include "base/posix/global_descriptors.h" 38#include "content/shell/android/shell_descriptors.h" 39#endif 40 41#if defined(OS_MACOSX) 42#include "content/shell/paths_mac.h" 43#endif // OS_MACOSX 44 45#if defined(OS_WIN) 46#include <initguid.h> 47#include "base/logging_win.h" 48#endif 49 50namespace { 51 52#if defined(OS_WIN) 53// If "Content Shell" doesn't show up in your list of trace providers in 54// Sawbuck, add these registry entries to your machine (NOTE the optional 55// Wow6432Node key for x64 machines): 56// 1. Find: HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers 57// 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}" 58// 3. Add these values: 59// "default_flags"=dword:00000001 60// "default_level"=dword:00000004 61// @="Content Shell" 62 63// {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6} 64const GUID kContentShellProviderName = { 65 0x6a3e50a4, 0x7e15, 0x4099, 66 { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } }; 67#endif 68 69void InitLogging() { 70 base::FilePath log_filename; 71 PathService::Get(base::DIR_EXE, &log_filename); 72 log_filename = log_filename.AppendASCII("content_shell.log"); 73 logging::InitLogging( 74 log_filename.value().c_str(), 75 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, 76 logging::LOCK_LOG_FILE, 77 logging::DELETE_OLD_LOG_FILE, 78 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); 79 logging::SetLogItems(true, true, true, true); 80} 81 82} // namespace 83 84namespace content { 85 86ShellMainDelegate::ShellMainDelegate() { 87} 88 89ShellMainDelegate::~ShellMainDelegate() { 90} 91 92bool ShellMainDelegate::BasicStartupComplete(int* exit_code) { 93#if defined(OS_WIN) 94 // Enable trace control and transport through event tracing for Windows. 95 logging::LogEventProvider::Initialize(kContentShellProviderName); 96#endif 97#if defined(OS_MACOSX) 98 // Needs to happen before InitializeResourceBundle() and before 99 // WebKitTestPlatformInitialize() are called. 100 OverrideFrameworkBundlePath(); 101 OverrideChildProcessPath(); 102#endif // OS_MACOSX 103 104 InitLogging(); 105 CommandLine& command_line = *CommandLine::ForCurrentProcess(); 106 if (command_line.HasSwitch(switches::kDumpRenderTree)) { 107 command_line.AppendSwitch(switches::kProcessPerTab); 108 command_line.AppendSwitch(switches::kAllowFileAccessFromFiles); 109 command_line.AppendSwitchASCII( 110 switches::kUseGL, gfx::kGLImplementationOSMesaName); 111 SetAllowOSMesaImageTransportForTesting(); 112 DisableSystemDragDrop(); 113 DisableModalPopupMenus(); 114 command_line.AppendSwitch(switches::kSkipGpuDataLoading); 115 command_line.AppendSwitch(switches::kEnableExperimentalWebKitFeatures); 116 command_line.AppendSwitch(switches::kEnableCssShaders); 117 command_line.AppendSwitchASCII(switches::kTouchEvents, 118 switches::kTouchEventsEnabled); 119 if (command_line.HasSwitch(switches::kEnableSoftwareCompositing)) 120 command_line.AppendSwitch(switches::kEnableSoftwareCompositingGLAdapter); 121 122 net::CookieMonster::EnableFileScheme(); 123 if (!WebKitTestPlatformInitialize()) { 124 if (exit_code) 125 *exit_code = 1; 126 return true; 127 } 128 } 129 SetContentClient(&content_client_); 130 return false; 131} 132 133void ShellMainDelegate::PreSandboxStartup() { 134 InitializeResourceBundle(); 135} 136 137int ShellMainDelegate::RunProcess( 138 const std::string& process_type, 139 const MainFunctionParams& main_function_params) { 140 if (!process_type.empty()) 141 return -1; 142 143#if !defined(OS_ANDROID) 144 return ShellBrowserMain(main_function_params); 145#else 146 // If no process type is specified, we are creating the main browser process. 147 browser_runner_.reset(BrowserMainRunner::Create()); 148 int exit_code = browser_runner_->Initialize(main_function_params); 149 DCHECK(exit_code < 0) 150 << "BrowserRunner::Initialize failed in ShellMainDelegate"; 151 152 return exit_code; 153#endif 154} 155 156void ShellMainDelegate::InitializeResourceBundle() { 157#if defined(OS_ANDROID) 158 // In the Android case, the renderer runs with a different UID and can never 159 // access the file system. So we are passed a file descriptor to the 160 // ResourceBundle pak at launch time. 161 int pak_fd = 162 base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor); 163 if (pak_fd != base::kInvalidPlatformFileValue) { 164 ui::ResourceBundle::InitSharedInstanceWithPakFile(pak_fd, false); 165 ResourceBundle::GetSharedInstance().AddDataPackFromFile( 166 pak_fd, ui::SCALE_FACTOR_100P); 167 return; 168 } 169#endif 170 171 base::FilePath pak_file; 172#if defined(OS_MACOSX) 173 pak_file = GetResourcesPakFilePath(); 174#else 175 base::FilePath pak_dir; 176 177#if defined(OS_ANDROID) 178 bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir); 179 DCHECK(got_path); 180 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); 181#else 182 PathService::Get(base::DIR_MODULE, &pak_dir); 183#endif 184 185 pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak")); 186#endif 187 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file); 188} 189 190ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() { 191 browser_client_.reset(new ShellContentBrowserClient); 192 return browser_client_.get(); 193} 194 195ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() { 196 renderer_client_.reset(new ShellContentRendererClient); 197 return renderer_client_.get(); 198} 199 200} // namespace content 201