Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: content/browser/browser_main.cc

Issue 7840041: Refactor some more BrowserMain code. Move core code that's required by all embedders to content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/browser_main.h" 5 #include "content/browser/browser_main.h"
6 6
7 #include "base/allocator/allocator_shim.h" 7 #include "base/allocator/allocator_shim.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/system_monitor/system_monitor.h" 13 #include "base/system_monitor/system_monitor.h"
14 #include "base/threading/thread_restrictions.h"
14 #include "content/browser/browser_thread.h" 15 #include "content/browser/browser_thread.h"
15 #include "content/browser/content_browser_client.h" 16 #include "content/browser/content_browser_client.h"
16 #include "content/common/content_switches.h" 17 #include "content/common/content_switches.h"
17 #include "content/common/hi_res_timer_manager.h" 18 #include "content/common/hi_res_timer_manager.h"
18 #include "content/common/main_function_params.h" 19 #include "content/common/main_function_params.h"
20 #include "content/common/result_codes.h"
19 #include "content/common/sandbox_policy.h" 21 #include "content/common/sandbox_policy.h"
22 #include "crypto/nss_util.h"
20 #include "net/base/network_change_notifier.h" 23 #include "net/base/network_change_notifier.h"
21 #include "net/base/ssl_config_service.h" 24 #include "net/base/ssl_config_service.h"
25 #include "net/socket/client_socket_factory.h"
22 #include "net/socket/tcp_client_socket.h" 26 #include "net/socket/tcp_client_socket.h"
23 27
24 #if defined(OS_WIN) 28 #if defined(OS_WIN)
25 #include <windows.h> 29 #include <windows.h>
26 #include <commctrl.h> 30 #include <commctrl.h>
31 #include <ole2.h>
27 #include <shellapi.h> 32 #include <shellapi.h>
28 33
34 #include "base/win/scoped_com_initializer.h"
35 #include "net/base/winsock_init.h"
29 #include "sandbox/src/sandbox.h" 36 #include "sandbox/src/sandbox.h"
37 #include "ui/base/l10n/l10n_util_win.h"
30 #endif 38 #endif
31 39
32 #if defined(OS_POSIX) && !defined(OS_MACOSX) 40 #if defined(OS_POSIX) && !defined(OS_MACOSX)
33 #include <dbus/dbus-glib.h> 41 #include <dbus/dbus-glib.h>
42 #include <sys/stat.h>
43
44 #include "content/browser/renderer_host/render_sandbox_host_linux.h"
45 #include "content/browser/zygote_host_linux.h"
34 #endif 46 #endif
35 47
36 #if defined(TOOLKIT_USES_GTK) 48 #if defined(TOOLKIT_USES_GTK)
37 #include "ui/gfx/gtk_util.h" 49 #include "ui/gfx/gtk_util.h"
38 #endif 50 #endif
39 51
40 namespace { 52 namespace {
41 53
42 // Windows-specific initialization code for the sandbox broker services. This 54 #if defined(OS_WIN)
43 // is just a NOP on non-Windows platforms to reduce ifdefs later on. 55 // Windows-specific initialization code for the sandbox broker services.
44 void InitializeBrokerServices(const MainFunctionParams& parameters, 56 void InitializeBrokerServices(const MainFunctionParams& parameters,
45 const CommandLine& parsed_command_line) { 57 const CommandLine& parsed_command_line) {
46 #if defined(OS_WIN)
47 sandbox::BrokerServices* broker_services = 58 sandbox::BrokerServices* broker_services =
48 parameters.sandbox_info_.BrokerServices(); 59 parameters.sandbox_info_.BrokerServices();
49 if (broker_services) { 60 if (broker_services) {
50 sandbox::InitBrokerServices(broker_services); 61 sandbox::InitBrokerServices(broker_services);
51 if (!parsed_command_line.HasSwitch(switches::kNoSandbox)) { 62 if (!parsed_command_line.HasSwitch(switches::kNoSandbox)) {
52 bool use_winsta = !parsed_command_line.HasSwitch( 63 bool use_winsta = !parsed_command_line.HasSwitch(
53 switches::kDisableAltWinstation); 64 switches::kDisableAltWinstation);
54 // Precreate the desktop and window station used by the renderers. 65 // Precreate the desktop and window station used by the renderers.
55 sandbox::TargetPolicy* policy = broker_services->CreatePolicy(); 66 sandbox::TargetPolicy* policy = broker_services->CreatePolicy();
56 sandbox::ResultCode result = policy->CreateAlternateDesktop(use_winsta); 67 sandbox::ResultCode result = policy->CreateAlternateDesktop(use_winsta);
57 CHECK(sandbox::SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION != result); 68 CHECK(sandbox::SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION != result);
58 policy->Release(); 69 policy->Release();
59 } 70 }
60 } 71 }
72 }
73 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
74 void SetupSandbox(const CommandLine& parsed_command_line) {
75 // TODO(evanm): move this into SandboxWrapper; I'm just trying to move this
76 // code en masse out of chrome_main for now.
77 const char* sandbox_binary = NULL;
78 struct stat st;
79
80 // In Chromium branded builds, developers can set an environment variable to
81 // use the development sandbox. See
82 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
83 if (stat("/proc/self/exe", &st) == 0 && st.st_uid == getuid())
84 sandbox_binary = getenv("CHROME_DEVEL_SANDBOX");
85
86 #if defined(LINUX_SANDBOX_PATH)
87 if (!sandbox_binary)
88 sandbox_binary = LINUX_SANDBOX_PATH;
61 #endif 89 #endif
90
91 std::string sandbox_cmd;
92 if (sandbox_binary && !parsed_command_line.HasSwitch(switches::kNoSandbox))
93 sandbox_cmd = sandbox_binary;
94
95 // Tickle the sandbox host and zygote host so they fork now.
96 RenderSandboxHostLinux* shost = RenderSandboxHostLinux::GetInstance();
97 shost->Init(sandbox_cmd);
98 ZygoteHost* zhost = ZygoteHost::GetInstance();
99 zhost->Init(sandbox_cmd);
62 } 100 }
101 #endif
63 102
64 #if defined(TOOLKIT_USES_GTK) 103 #if defined(TOOLKIT_USES_GTK)
65 static void GLibLogHandler(const gchar* log_domain, 104 static void GLibLogHandler(const gchar* log_domain,
66 GLogLevelFlags log_level, 105 GLogLevelFlags log_level,
67 const gchar* message, 106 const gchar* message,
68 gpointer userdata) { 107 gpointer userdata) {
69 if (!log_domain) 108 if (!log_domain)
70 log_domain = "<unknown>"; 109 log_domain = "<unknown>";
71 if (!message) 110 if (!message)
72 message = "<no message>"; 111 message = "<no message>";
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 } 151 }
113 } 152 }
114 #endif 153 #endif
115 154
116 } // namespace 155 } // namespace
117 156
118 namespace content { 157 namespace content {
119 158
120 BrowserMainParts::BrowserMainParts(const MainFunctionParams& parameters) 159 BrowserMainParts::BrowserMainParts(const MainFunctionParams& parameters)
121 : parameters_(parameters), 160 : parameters_(parameters),
122 parsed_command_line_(parameters.command_line_) { 161 parsed_command_line_(parameters.command_line_),
162 result_code_(content::RESULT_CODE_NORMAL_EXIT) {
123 } 163 }
124 164
125 BrowserMainParts::~BrowserMainParts() { 165 BrowserMainParts::~BrowserMainParts() {
126 } 166 }
127 167
128 void BrowserMainParts::EarlyInitialization() { 168 void BrowserMainParts::EarlyInitialization() {
129 PreEarlyInitialization(); 169 PreEarlyInitialization();
130 170
171 #if defined(OS_WIN)
172 net::EnsureWinsockInit();
173 #endif
174
175 // Use NSS for SSL by default.
176 // The default client socket factory uses NSS for SSL by default on
177 // Windows and Mac.
178 #if defined(OS_WIN) || defined(OS_MACOSX)
179 if (parsed_command_line().HasSwitch(switches::kUseSystemSSL)) {
180 net::ClientSocketFactory::UseSystemSSL();
181 } else {
182 #elif defined(USE_NSS)
183 if (true) {
184 #else
185 if (false) {
186 #endif
187 // We want to be sure to init NSPR on the main thread.
188 crypto::EnsureNSPRInit();
189 }
190
191 #if defined(OS_POSIX) && !defined(OS_MACOSX)
192 SetupSandbox(parsed_command_line());
193 #endif
194
131 if (parsed_command_line().HasSwitch(switches::kEnableBenchmarking)) 195 if (parsed_command_line().HasSwitch(switches::kEnableBenchmarking))
132 base::FieldTrial::EnableBenchmarking(); 196 base::FieldTrial::EnableBenchmarking();
133
134 InitializeSSL();
135
136 if (parsed_command_line().HasSwitch(switches::kDisableSSLFalseStart)) 197 if (parsed_command_line().HasSwitch(switches::kDisableSSLFalseStart))
137 net::SSLConfigService::DisableFalseStart(); 198 net::SSLConfigService::DisableFalseStart();
138 if (parsed_command_line().HasSwitch(switches::kEnableSSLCachedInfo)) 199 if (parsed_command_line().HasSwitch(switches::kEnableSSLCachedInfo))
139 net::SSLConfigService::EnableCachedInfo(); 200 net::SSLConfigService::EnableCachedInfo();
140 if (parsed_command_line().HasSwitch(switches::kEnableOriginBoundCerts)) 201 if (parsed_command_line().HasSwitch(switches::kEnableOriginBoundCerts))
141 net::SSLConfigService::EnableOriginBoundCerts(); 202 net::SSLConfigService::EnableOriginBoundCerts();
142 if (parsed_command_line().HasSwitch( 203 if (parsed_command_line().HasSwitch(
143 switches::kEnableDNSCertProvenanceChecking)) { 204 switches::kEnableDNSCertProvenanceChecking)) {
144 net::SSLConfigService::EnableDNSCertProvenanceChecking(); 205 net::SSLConfigService::EnableDNSCertProvenanceChecking();
145 } 206 }
146 207
147 // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't 208 // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't
148 // seem dependent on InitializeSSL(). 209 // seem dependent on SSL initialization().
149 if (parsed_command_line().HasSwitch(switches::kEnableTcpFastOpen)) 210 if (parsed_command_line().HasSwitch(switches::kEnableTcpFastOpen))
150 net::set_tcp_fastopen_enabled(true); 211 net::set_tcp_fastopen_enabled(true);
151 212
152 PostEarlyInitialization(); 213 PostEarlyInitialization();
153 } 214 }
154 215
155 void BrowserMainParts::MainMessageLoopStart() { 216 void BrowserMainParts::MainMessageLoopStart() {
156 PreMainMessageLoopStart(); 217 PreMainMessageLoopStart();
157 218
219 #if defined(OS_WIN)
220 OleInitialize(NULL);
221
222 // If we're running tests (ui_task is non-null), then the ResourceBundle
223 // has already been initialized.
224 if (!parameters().ui_task) {
225 // Override the configured locale with the user's preferred UI language.
226 l10n_util::OverrideLocaleWithUILanguageList();
227 }
228 #endif
229
158 main_message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI)); 230 main_message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI));
159 231
160 // TODO(viettrungluu): should these really go before setting the thread name? 232 // TODO(viettrungluu): should these really go before setting the thread name?
161 system_monitor_.reset(new base::SystemMonitor); 233 system_monitor_.reset(new base::SystemMonitor);
162 hi_res_timer_manager_.reset(new HighResolutionTimerManager); 234 hi_res_timer_manager_.reset(new HighResolutionTimerManager);
163 235
164 InitializeMainThread(); 236 InitializeMainThread();
165 237
166 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); 238 network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
167 239
168 PostMainMessageLoopStart(); 240 PostMainMessageLoopStart();
169 } 241 }
170 242
243 void BrowserMainParts::MainMessageLoopRunWrapper() {
244 PreMainMessageLoopRun();
245
246 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
247 {
jam 2011/09/08 16:38:01 I got rid of this unnecessary scope
248 // If the UI thread blocks, the whole UI is unresponsive.
249 // Do not allow disk IO from the UI thread.
250 base::ThreadRestrictions::SetIOAllowed(false);
251 MainMessageLoopRun();
252 }
253 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
254
255 PostMainMessageLoopRun();
256 }
257
171 void BrowserMainParts::InitializeMainThread() { 258 void BrowserMainParts::InitializeMainThread() {
172 const char* kThreadName = "CrBrowserMain"; 259 const char* kThreadName = "CrBrowserMain";
173 base::PlatformThread::SetName(kThreadName); 260 base::PlatformThread::SetName(kThreadName);
174 main_message_loop().set_thread_name(kThreadName); 261 main_message_loop().set_thread_name(kThreadName);
175 262
176 // Register the main thread by instantiating it, but don't call any methods. 263 // Register the main thread by instantiating it, but don't call any methods.
177 main_thread_.reset(new BrowserThread(BrowserThread::UI, 264 main_thread_.reset(new BrowserThread(BrowserThread::UI,
178 MessageLoop::current())); 265 MessageLoop::current()));
179 } 266 }
180 267
(...skipping 26 matching lines...) Expand all
207 SetUpGLibLogHandler(); 294 SetUpGLibLogHandler();
208 #endif 295 #endif
209 296
210 #if defined(TOOLKIT_GTK) 297 #if defined(TOOLKIT_GTK)
211 // It is important for this to happen before the first run dialog, as it 298 // It is important for this to happen before the first run dialog, as it
212 // styles the dialog as well. 299 // styles the dialog as well.
213 gfx::InitRCStyles(); 300 gfx::InitRCStyles();
214 #endif 301 #endif
215 302
216 #if defined(OS_WIN) 303 #if defined(OS_WIN)
217 // Init common control sex.
218 INITCOMMONCONTROLSEX config; 304 INITCOMMONCONTROLSEX config;
219 config.dwSize = sizeof(config); 305 config.dwSize = sizeof(config);
220 config.dwICC = ICC_WIN95_CLASSES; 306 config.dwICC = ICC_WIN95_CLASSES;
221 if (!InitCommonControlsEx(&config)) 307 if (!InitCommonControlsEx(&config))
222 LOG_GETLASTERROR(FATAL); 308 LOG_GETLASTERROR(FATAL);
223 #endif 309 #endif
224 310
225 ToolkitInitialized(); 311 ToolkitInitialized();
226 } 312 }
227 313
228 void BrowserMainParts::PreEarlyInitialization() { 314 void BrowserMainParts::PreEarlyInitialization() {
229 } 315 }
230 316
231 void BrowserMainParts::PostEarlyInitialization() { 317 void BrowserMainParts::PostEarlyInitialization() {
232 } 318 }
233 319
234 void BrowserMainParts::PreMainMessageLoopStart() { 320 void BrowserMainParts::PreMainMessageLoopStart() {
235 } 321 }
236 322
237 void BrowserMainParts::PostMainMessageLoopStart() { 323 void BrowserMainParts::PostMainMessageLoopStart() {
238 } 324 }
239 325
240 void BrowserMainParts::InitializeSSL() { 326 void BrowserMainParts::PreMainMessageLoopRun() {
327 }
328
329 void BrowserMainParts::MainMessageLoopRun() {
330 #if defined(OS_MACOSX)
331 MessageLoopForUI::current()->Run();
332 #else
333 MessageLoopForUI::current()->Run(NULL);
334 #endif
335 }
336
337 void BrowserMainParts::PostMainMessageLoopRun() {
241 } 338 }
242 339
243 void BrowserMainParts::ToolkitInitialized() { 340 void BrowserMainParts::ToolkitInitialized() {
244 } 341 }
245 342
246 int BrowserMainParts::TemporaryContinue() {
247 return 0;
248 }
249
250 } // namespace content 343 } // namespace content
251 344
252 // Main routine for running as the Browser process. 345 // Main routine for running as the Browser process.
253 int BrowserMain(const MainFunctionParams& parameters) { 346 int BrowserMain(const MainFunctionParams& parameters) {
254 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); 347 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, "");
255 348
256 scoped_ptr<content::BrowserMainParts> parts( 349 scoped_ptr<content::BrowserMainParts> parts(
257 content::GetContentClient()->browser()->CreateBrowserMainParts( 350 content::GetContentClient()->browser()->CreateBrowserMainParts(
258 parameters)); 351 parameters));
259 352
(...skipping 21 matching lines...) Expand all
281 // your code at that point? 374 // your code at that point?
282 // - If you need to create a (persistent) object, heap allocate it and keep a 375 // - If you need to create a (persistent) object, heap allocate it and keep a
283 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise 376 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise
284 // I'll have to convert your code when I refactor. 377 // I'll have to convert your code when I refactor.
285 // - Unless your new code is just a couple of lines, factor it out into a 378 // - Unless your new code is just a couple of lines, factor it out into a
286 // function with a well-defined purpose. Do NOT just add it inline in 379 // function with a well-defined purpose. Do NOT just add it inline in
287 // |BrowserMain()|. 380 // |BrowserMain()|.
288 // Thanks! 381 // Thanks!
289 382
290 // TODO(viettrungluu): put the remainder into BrowserMainParts 383 // TODO(viettrungluu): put the remainder into BrowserMainParts
291 const CommandLine& parsed_command_line = parameters.command_line_;
292 384
293 #if defined(OS_WIN) && !defined(NO_TCMALLOC) 385 #if defined(OS_WIN)
386 #if !defined(NO_TCMALLOC)
294 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic 387 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
295 // allocator selection is not supported. 388 // allocator selection is not supported.
296 389
297 // Make this call before going multithreaded, or spawning any subprocesses. 390 // Make this call before going multithreaded, or spawning any subprocesses.
298 base::allocator::SetupSubprocessAllocator(); 391 base::allocator::SetupSubprocessAllocator();
299 #endif // OS_WIN 392 #endif
300
301 // The broker service initialization needs to run early because it will 393 // The broker service initialization needs to run early because it will
302 // initialize the sandbox broker, which requires the process to swap its 394 // initialize the sandbox broker, which requires the process to swap its
303 // window station. During this time all the UI will be broken. This has to 395 // window station. During this time all the UI will be broken. This has to
304 // run before threads and windows are created. 396 // run before threads and windows are created.
305 InitializeBrokerServices(parameters, parsed_command_line); 397 InitializeBrokerServices(parameters, parameters.command_line_);
398
399 base::win::ScopedCOMInitializer com_initializer;
400 #endif // OS_WIN
306 401
307 // Initialize histogram statistics gathering system. 402 // Initialize histogram statistics gathering system.
308 base::StatisticsRecorder statistics; 403 base::StatisticsRecorder statistics;
309 404
310 // TODO(jam): bring the content parts from this chrome function here. 405 parts->MainMessageLoopRunWrapper();
311 int result_code = parts->TemporaryContinue();
312
313 // Release BrowserMainParts here, before shutting down CrosLibrary, since
314 // some of the classes initialized there have CrosLibrary dependencies.
315 parts.reset(NULL);
316 406
317 TRACE_EVENT_END_ETW("BrowserMain", 0, 0); 407 TRACE_EVENT_END_ETW("BrowserMain", 0, 0);
318 return result_code; 408 return parts->result_code();
319 } 409 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698