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

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: review comments 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
« no previous file with comments | « content/browser/browser_main.h ('k') | content/common/content_switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::RunMainMessageLoopParts() {
244 PreMainMessageLoopRun();
245
246 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
247 // If the UI thread blocks, the whole UI is unresponsive.
248 // Do not allow disk IO from the UI thread.
249 base::ThreadRestrictions::SetIOAllowed(false);
250 MainMessageLoopRun();
251 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
252
253 PostMainMessageLoopRun();
254 }
255
171 void BrowserMainParts::InitializeMainThread() { 256 void BrowserMainParts::InitializeMainThread() {
172 const char* kThreadName = "CrBrowserMain"; 257 const char* kThreadName = "CrBrowserMain";
173 base::PlatformThread::SetName(kThreadName); 258 base::PlatformThread::SetName(kThreadName);
174 main_message_loop().set_thread_name(kThreadName); 259 main_message_loop().set_thread_name(kThreadName);
175 260
176 // Register the main thread by instantiating it, but don't call any methods. 261 // Register the main thread by instantiating it, but don't call any methods.
177 main_thread_.reset(new BrowserThread(BrowserThread::UI, 262 main_thread_.reset(new BrowserThread(BrowserThread::UI,
178 MessageLoop::current())); 263 MessageLoop::current()));
179 } 264 }
180 265
(...skipping 26 matching lines...) Expand all
207 SetUpGLibLogHandler(); 292 SetUpGLibLogHandler();
208 #endif 293 #endif
209 294
210 #if defined(TOOLKIT_GTK) 295 #if defined(TOOLKIT_GTK)
211 // It is important for this to happen before the first run dialog, as it 296 // It is important for this to happen before the first run dialog, as it
212 // styles the dialog as well. 297 // styles the dialog as well.
213 gfx::InitRCStyles(); 298 gfx::InitRCStyles();
214 #endif 299 #endif
215 300
216 #if defined(OS_WIN) 301 #if defined(OS_WIN)
217 // Init common control sex.
218 INITCOMMONCONTROLSEX config; 302 INITCOMMONCONTROLSEX config;
219 config.dwSize = sizeof(config); 303 config.dwSize = sizeof(config);
220 config.dwICC = ICC_WIN95_CLASSES; 304 config.dwICC = ICC_WIN95_CLASSES;
221 if (!InitCommonControlsEx(&config)) 305 if (!InitCommonControlsEx(&config))
222 LOG_GETLASTERROR(FATAL); 306 LOG_GETLASTERROR(FATAL);
223 #endif 307 #endif
224 308
225 ToolkitInitialized(); 309 ToolkitInitialized();
226 } 310 }
227 311
228 void BrowserMainParts::PreEarlyInitialization() { 312 void BrowserMainParts::PreEarlyInitialization() {
229 } 313 }
230 314
231 void BrowserMainParts::PostEarlyInitialization() { 315 void BrowserMainParts::PostEarlyInitialization() {
232 } 316 }
233 317
234 void BrowserMainParts::PreMainMessageLoopStart() { 318 void BrowserMainParts::PreMainMessageLoopStart() {
235 } 319 }
236 320
237 void BrowserMainParts::PostMainMessageLoopStart() { 321 void BrowserMainParts::PostMainMessageLoopStart() {
238 } 322 }
239 323
240 void BrowserMainParts::InitializeSSL() { 324 void BrowserMainParts::PreMainMessageLoopRun() {
325 }
326
327 void BrowserMainParts::MainMessageLoopRun() {
328 #if defined(OS_MACOSX)
329 MessageLoopForUI::current()->Run();
330 #else
331 MessageLoopForUI::current()->Run(NULL);
332 #endif
333 }
334
335 void BrowserMainParts::PostMainMessageLoopRun() {
241 } 336 }
242 337
243 void BrowserMainParts::ToolkitInitialized() { 338 void BrowserMainParts::ToolkitInitialized() {
244 } 339 }
245 340
246 int BrowserMainParts::TemporaryContinue() {
247 return 0;
248 }
249
250 } // namespace content 341 } // namespace content
251 342
252 // Main routine for running as the Browser process. 343 // Main routine for running as the Browser process.
253 int BrowserMain(const MainFunctionParams& parameters) { 344 int BrowserMain(const MainFunctionParams& parameters) {
254 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); 345 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, "");
255 346
256 scoped_ptr<content::BrowserMainParts> parts( 347 scoped_ptr<content::BrowserMainParts> parts(
257 content::GetContentClient()->browser()->CreateBrowserMainParts( 348 content::GetContentClient()->browser()->CreateBrowserMainParts(
258 parameters)); 349 parameters));
259 350
(...skipping 21 matching lines...) Expand all
281 // your code at that point? 372 // your code at that point?
282 // - If you need to create a (persistent) object, heap allocate it and keep a 373 // - 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 374 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise
284 // I'll have to convert your code when I refactor. 375 // 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 376 // - 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 377 // function with a well-defined purpose. Do NOT just add it inline in
287 // |BrowserMain()|. 378 // |BrowserMain()|.
288 // Thanks! 379 // Thanks!
289 380
290 // TODO(viettrungluu): put the remainder into BrowserMainParts 381 // TODO(viettrungluu): put the remainder into BrowserMainParts
291 const CommandLine& parsed_command_line = parameters.command_line_;
292 382
293 #if defined(OS_WIN) && !defined(NO_TCMALLOC) 383 #if defined(OS_WIN)
384 #if !defined(NO_TCMALLOC)
294 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic 385 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
295 // allocator selection is not supported. 386 // allocator selection is not supported.
296 387
297 // Make this call before going multithreaded, or spawning any subprocesses. 388 // Make this call before going multithreaded, or spawning any subprocesses.
298 base::allocator::SetupSubprocessAllocator(); 389 base::allocator::SetupSubprocessAllocator();
299 #endif // OS_WIN 390 #endif
300
301 // The broker service initialization needs to run early because it will 391 // The broker service initialization needs to run early because it will
302 // initialize the sandbox broker, which requires the process to swap its 392 // 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 393 // window station. During this time all the UI will be broken. This has to
304 // run before threads and windows are created. 394 // run before threads and windows are created.
305 InitializeBrokerServices(parameters, parsed_command_line); 395 InitializeBrokerServices(parameters, parameters.command_line_);
396
397 base::win::ScopedCOMInitializer com_initializer;
398 #endif // OS_WIN
306 399
307 // Initialize histogram statistics gathering system. 400 // Initialize histogram statistics gathering system.
308 base::StatisticsRecorder statistics; 401 base::StatisticsRecorder statistics;
309 402
310 // TODO(jam): bring the content parts from this chrome function here. 403 parts->RunMainMessageLoopParts();
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 404
317 TRACE_EVENT_END_ETW("BrowserMain", 0, 0); 405 TRACE_EVENT_END_ETW("BrowserMain", 0, 0);
318 return result_code; 406 return parts->result_code();
319 } 407 }
OLDNEW
« no previous file with comments | « content/browser/browser_main.h ('k') | content/common/content_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698