blob: d9cf97dd5c04b59037cde2c14950016d74e23fdb [file] [log] [blame]
[email protected]38d0b2d42012-01-18 03:37:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b48c9182011-10-26 18:03:302// 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/browser/browser_main_loop.h"
6
[email protected]820de972012-01-03 18:23:047#include "base/bind.h"
[email protected]b48c9182011-10-26 18:03:308#include "base/command_line.h"
9#include "base/debug/trace_event.h"
[email protected]b3aabd342012-06-04 19:33:5610#include "base/hi_res_timer_manager.h"
[email protected]b48c9182011-10-26 18:03:3011#include "base/logging.h"
12#include "base/message_loop.h"
13#include "base/metrics/field_trial.h"
14#include "base/metrics/histogram.h"
[email protected]a6381cb52012-02-13 22:39:3415#include "base/string_number_conversions.h"
[email protected]b48c9182011-10-26 18:03:3016#include "base/threading/thread_restrictions.h"
[email protected]c38831a12011-10-28 12:44:4917#include "content/browser/browser_thread_impl.h"
[email protected]99907362012-01-11 05:41:4018#include "content/browser/download/download_file_manager.h"
19#include "content/browser/download/save_file_manager.h"
[email protected]5b040e592012-02-10 02:56:1020#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
[email protected]99af54b2012-03-03 01:06:5021#include "content/browser/gpu/gpu_process_host.h"
[email protected]64d69de42012-02-06 00:19:5422#include "content/browser/gpu/gpu_process_host_ui_shim.h"
[email protected]c2b77b2392011-12-02 13:00:1623#include "content/browser/in_process_webkit/webkit_thread.h"
[email protected]bff327f2012-03-11 22:17:1924#include "content/browser/net/browser_online_state_observer.h"
[email protected]99907362012-01-11 05:41:4025#include "content/browser/plugin_service_impl.h"
[email protected]ea114722012-03-12 01:11:2526#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]28df14d2012-05-16 14:51:2227#include "content/browser/speech/speech_recognition_manager_impl.h"
[email protected]900e9122012-03-13 15:43:0828#include "content/browser/trace_controller_impl.h"
[email protected]b48c9182011-10-26 18:03:3029#include "content/public/browser/browser_main_parts.h"
[email protected]2e5b60a22011-11-28 15:56:4130#include "content/public/browser/browser_shutdown.h"
[email protected]b48c9182011-10-26 18:03:3031#include "content/public/browser/content_browser_client.h"
[email protected]b14b873b2012-05-03 03:56:0932#include "content/public/browser/gpu_data_manager.h"
[email protected]a6381cb52012-02-13 22:39:3433#include "content/public/browser/render_process_host.h"
[email protected]b48c9182011-10-26 18:03:3034#include "content/public/common/content_switches.h"
[email protected]4573fbd2011-10-31 20:25:1835#include "content/public/common/main_function_params.h"
[email protected]b48c9182011-10-26 18:03:3036#include "content/public/common/result_codes.h"
37#include "crypto/nss_util.h"
[email protected]52e456b92012-02-23 17:13:1838#include "media/audio/audio_manager.h"
[email protected]b48c9182011-10-26 18:03:3039#include "net/base/network_change_notifier.h"
40#include "net/base/ssl_config_service.h"
41#include "net/socket/client_socket_factory.h"
42#include "net/socket/tcp_client_socket.h"
43
[email protected]894e8fc2012-02-24 13:29:5044#if defined(USE_AURA)
45#include "content/browser/renderer_host/image_transport_factory.h"
46#endif
47
[email protected]b48c9182011-10-26 18:03:3048#if defined(OS_WIN)
49#include <windows.h>
50#include <commctrl.h>
[email protected]b48c9182011-10-26 18:03:3051#include <shellapi.h>
52
[email protected]cd1cd4c02011-11-15 01:59:4953#include "content/browser/system_message_window_win.h"
[email protected]6a7da762012-03-31 02:12:3354#include "content/common/sandbox_policy.h"
[email protected]b48c9182011-10-26 18:03:3055#include "ui/base/l10n/l10n_util_win.h"
56#include "net/base/winsock_init.h"
57#endif
58
[email protected]e60c0232011-11-11 19:56:3559#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]b48c9182011-10-26 18:03:3060#include <glib-object.h>
61#endif
62
63#if defined(OS_CHROMEOS)
64#include <dbus/dbus-glib.h>
65#endif
66
[email protected]a13283cc2012-04-05 00:21:2267#if defined(TOOLKIT_GTK)
[email protected]b48c9182011-10-26 18:03:3068#include "ui/gfx/gtk_util.h"
69#endif
70
71#if defined(OS_POSIX) && !defined(OS_MACOSX)
72#include <sys/stat.h>
73#include "content/browser/renderer_host/render_sandbox_host_linux.h"
[email protected]c2c68b1f2012-02-25 00:29:1574#include "content/browser/zygote_host_impl_linux.h"
[email protected]b48c9182011-10-26 18:03:3075#endif
76
[email protected]462d4a32012-01-19 00:11:0477#if defined(USE_X11)
78#include <X11/Xlib.h>
79#endif
80
[email protected]64d69de42012-02-06 00:19:5481// One of the linux specific headers defines this as a macro.
82#ifdef DestroyAll
83#undef DestroyAll
84#endif
85
[email protected]900e9122012-03-13 15:43:0886using content::TraceControllerImpl;
87
[email protected]b48c9182011-10-26 18:03:3088namespace {
89
[email protected]a08029b42012-04-25 03:18:4690#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:3091void SetupSandbox(const CommandLine& parsed_command_line) {
92 // TODO(evanm): move this into SandboxWrapper; I'm just trying to move this
93 // code en masse out of chrome_main for now.
94 const char* sandbox_binary = NULL;
95 struct stat st;
96
97 // In Chromium branded builds, developers can set an environment variable to
98 // use the development sandbox. See
99 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
100 if (stat("/proc/self/exe", &st) == 0 && st.st_uid == getuid())
101 sandbox_binary = getenv("CHROME_DEVEL_SANDBOX");
102
103#if defined(LINUX_SANDBOX_PATH)
104 if (!sandbox_binary)
105 sandbox_binary = LINUX_SANDBOX_PATH;
106#endif
107
108 std::string sandbox_cmd;
109 if (sandbox_binary && !parsed_command_line.HasSwitch(switches::kNoSandbox))
110 sandbox_cmd = sandbox_binary;
111
112 // Tickle the sandbox host and zygote host so they fork now.
[email protected]c2c68b1f2012-02-25 00:29:15113 RenderSandboxHostLinux::GetInstance()->Init(sandbox_cmd);
114 ZygoteHostImpl::GetInstance()->Init(sandbox_cmd);
[email protected]b48c9182011-10-26 18:03:30115}
116#endif
117
[email protected]e60c0232011-11-11 19:56:35118#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]b48c9182011-10-26 18:03:30119static void GLibLogHandler(const gchar* log_domain,
120 GLogLevelFlags log_level,
121 const gchar* message,
122 gpointer userdata) {
123 if (!log_domain)
124 log_domain = "<unknown>";
125 if (!message)
126 message = "<no message>";
127
128 if (strstr(message, "Loading IM context type") ||
129 strstr(message, "wrong ELF class: ELFCLASS64")) {
130 // http://crbug.com/9643
131 // Until we have a real 64-bit build or all of these 32-bit package issues
132 // are sorted out, don't fatal on ELF 32/64-bit mismatch warnings and don't
133 // spam the user with more than one of them.
134 static bool alerted = false;
135 if (!alerted) {
136 LOG(ERROR) << "Bug 9643: " << log_domain << ": " << message;
137 alerted = true;
138 }
[email protected]8a1ea192011-11-29 22:40:50139 } else if (strstr(message, "Unable to retrieve the file info for")) {
140 LOG(ERROR) << "GTK File code error: " << message;
[email protected]b48c9182011-10-26 18:03:30141 } else if (strstr(message, "Theme file for default has no") ||
142 strstr(message, "Theme directory") ||
[email protected]2bdaecf2011-12-15 02:44:55143 strstr(message, "theme pixmap") ||
144 strstr(message, "locate theme engine")) {
[email protected]b48c9182011-10-26 18:03:30145 LOG(ERROR) << "GTK theme error: " << message;
146 } else if (strstr(message, "gtk_drag_dest_leave: assertion")) {
147 LOG(ERROR) << "Drag destination deleted: http://crbug.com/18557";
148 } else if (strstr(message, "Out of memory") &&
149 strstr(log_domain, "<unknown>")) {
150 LOG(ERROR) << "DBus call timeout or out of memory: "
151 << "http://crosbug.com/15496";
152 } else if (strstr(message, "XDG_RUNTIME_DIR variable not set")) {
153 LOG(ERROR) << message << " (http://bugs.chromium.org/97293)";
154 } else {
155 LOG(DFATAL) << log_domain << ": " << message;
156 }
157}
158
159static void SetUpGLibLogHandler() {
160 // Register GLib-handled assertions to go through our logging system.
161 const char* kLogDomains[] = { NULL, "Gtk", "Gdk", "GLib", "GLib-GObject" };
162 for (size_t i = 0; i < arraysize(kLogDomains); i++) {
163 g_log_set_handler(kLogDomains[i],
164 static_cast<GLogLevelFlags>(G_LOG_FLAG_RECURSION |
165 G_LOG_FLAG_FATAL |
166 G_LOG_LEVEL_ERROR |
167 G_LOG_LEVEL_CRITICAL |
168 G_LOG_LEVEL_WARNING),
169 GLibLogHandler,
170 NULL);
171 }
172}
173#endif
174
175} // namespace
176
177namespace content {
178
[email protected]2e5b60a22011-11-28 15:56:41179// The currently-running BrowserMainLoop. There can be one or zero.
[email protected]52e456b92012-02-23 17:13:18180BrowserMainLoop* g_current_browser_main_loop = NULL;
[email protected]2e5b60a22011-11-28 15:56:41181
182// This is just to be able to keep ShutdownThreadsAndCleanUp out of
183// the public interface of BrowserMainLoop.
184class BrowserShutdownImpl {
185 public:
186 static void ImmediateShutdownAndExitProcess() {
[email protected]52e456b92012-02-23 17:13:18187 DCHECK(g_current_browser_main_loop);
188 g_current_browser_main_loop->ShutdownThreadsAndCleanUp();
[email protected]2e5b60a22011-11-28 15:56:41189
190#if defined(OS_WIN)
191 // At this point the message loop is still running yet we've shut everything
192 // down. If any messages are processed we'll likely crash. Exit now.
193 ExitProcess(content::RESULT_CODE_NORMAL_EXIT);
194#elif defined(OS_POSIX) && !defined(OS_MACOSX)
195 _exit(content::RESULT_CODE_NORMAL_EXIT);
196#else
197 NOTIMPLEMENTED();
198#endif
199 }
200};
201
202void ImmediateShutdownAndExitProcess() {
203 BrowserShutdownImpl::ImmediateShutdownAndExitProcess();
204}
[email protected]b48c9182011-10-26 18:03:30205
[email protected]52e456b92012-02-23 17:13:18206// static
[email protected]67dfea902012-04-03 01:49:09207media::AudioManager* BrowserMainLoop::GetAudioManager() {
[email protected]52e456b92012-02-23 17:13:18208 return g_current_browser_main_loop->audio_manager_.get();
209}
210
[email protected]e974ad292012-03-07 07:34:51211// BrowserMainLoop construction / destruction =============================
[email protected]b48c9182011-10-26 18:03:30212
[email protected]4573fbd2011-10-31 20:25:18213BrowserMainLoop::BrowserMainLoop(const content::MainFunctionParams& parameters)
[email protected]b48c9182011-10-26 18:03:30214 : parameters_(parameters),
[email protected]badf5cf2011-10-29 03:44:44215 parsed_command_line_(parameters.command_line),
[email protected]b48c9182011-10-26 18:03:30216 result_code_(content::RESULT_CODE_NORMAL_EXIT) {
[email protected]52e456b92012-02-23 17:13:18217 DCHECK(!g_current_browser_main_loop);
218 g_current_browser_main_loop = this;
[email protected]b48c9182011-10-26 18:03:30219}
220
221BrowserMainLoop::~BrowserMainLoop() {
[email protected]52e456b92012-02-23 17:13:18222 DCHECK_EQ(this, g_current_browser_main_loop);
223 g_current_browser_main_loop = NULL;
[email protected]b48c9182011-10-26 18:03:30224}
225
226void BrowserMainLoop::Init() {
[email protected]50462bf02011-11-21 19:13:31227 parts_.reset(
228 GetContentClient()->browser()->CreateBrowserMainParts(parameters_));
[email protected]b48c9182011-10-26 18:03:30229}
230
231// BrowserMainLoop stages ==================================================
232
233void BrowserMainLoop::EarlyInitialization() {
[email protected]462d4a32012-01-19 00:11:04234#if defined(USE_X11)
235 if (parsed_command_line_.HasSwitch(switches::kSingleProcess) ||
236 parsed_command_line_.HasSwitch(switches::kInProcessGPU)) {
237 if (!XInitThreads()) {
238 LOG(ERROR) << "Failed to put Xlib into threaded mode.";
239 }
240 }
241#endif
242
[email protected]50462bf02011-11-21 19:13:31243 if (parts_.get())
244 parts_->PreEarlyInitialization();
[email protected]b48c9182011-10-26 18:03:30245
[email protected]b48c9182011-10-26 18:03:30246#if defined(OS_WIN)
247 net::EnsureWinsockInit();
248#endif
249
[email protected]1648b292011-11-03 23:57:39250#if !defined(USE_OPENSSL)
[email protected]b48c9182011-10-26 18:03:30251 // Use NSS for SSL by default.
252 // The default client socket factory uses NSS for SSL by default on
253 // Windows and Mac.
254 bool init_nspr = false;
255#if defined(OS_WIN) || defined(OS_MACOSX)
256 if (parsed_command_line_.HasSwitch(switches::kUseSystemSSL)) {
257 net::ClientSocketFactory::UseSystemSSL();
258 } else {
259 init_nspr = true;
260 }
[email protected]c4805102012-03-02 23:34:32261 UMA_HISTOGRAM_BOOLEAN("Chrome.CommandLineUseSystemSSL", !init_nspr);
[email protected]b48c9182011-10-26 18:03:30262#elif defined(USE_NSS)
263 init_nspr = true;
264#endif
265 if (init_nspr) {
266 // We want to be sure to init NSPR on the main thread.
267 crypto::EnsureNSPRInit();
268 }
[email protected]1648b292011-11-03 23:57:39269#endif // !defined(USE_OPENSSL)
[email protected]b48c9182011-10-26 18:03:30270
[email protected]a08029b42012-04-25 03:18:46271#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:30272 SetupSandbox(parsed_command_line_);
273#endif
274
[email protected]b48c9182011-10-26 18:03:30275 if (parsed_command_line_.HasSwitch(switches::kEnableSSLCachedInfo))
276 net::SSLConfigService::EnableCachedInfo();
[email protected]b48c9182011-10-26 18:03:30277
278 // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't
279 // seem dependent on SSL initialization().
280 if (parsed_command_line_.HasSwitch(switches::kEnableTcpFastOpen))
281 net::set_tcp_fastopen_enabled(true);
282
[email protected]a6381cb52012-02-13 22:39:34283 if (parsed_command_line_.HasSwitch(switches::kRendererProcessLimit)) {
284 std::string limit_string = parsed_command_line_.GetSwitchValueASCII(
285 switches::kRendererProcessLimit);
286 size_t process_limit;
287 if (base::StringToSizeT(limit_string, &process_limit)) {
288 content::RenderProcessHost::SetMaxRendererProcessCount(process_limit);
289 }
290 }
291
[email protected]50462bf02011-11-21 19:13:31292 if (parts_.get())
293 parts_->PostEarlyInitialization();
[email protected]b48c9182011-10-26 18:03:30294}
295
296void BrowserMainLoop::MainMessageLoopStart() {
[email protected]50462bf02011-11-21 19:13:31297 if (parts_.get())
298 parts_->PreMainMessageLoopStart();
[email protected]b48c9182011-10-26 18:03:30299
300#if defined(OS_WIN)
301 // If we're running tests (ui_task is non-null), then the ResourceBundle
302 // has already been initialized.
[email protected]716476c2011-12-29 00:07:03303 if (!parameters_.ui_task) {
[email protected]b48c9182011-10-26 18:03:30304 // Override the configured locale with the user's preferred UI language.
305 l10n_util::OverrideLocaleWithUILanguageList();
306 }
307#endif
308
[email protected]de88c5e2012-04-10 23:35:23309 // Create a MessageLoop if one does not already exist for the current thread.
310 if (!MessageLoop::current())
[email protected]f573ed6b2012-02-10 15:58:52311 main_message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI));
[email protected]b48c9182011-10-26 18:03:30312
313 InitializeMainThread();
314
[email protected]50f38422011-11-09 19:21:33315 // Start tracing to a file if needed.
[email protected]900e9122012-03-13 15:43:08316 if (base::debug::TraceLog::GetInstance()->IsEnabled()) {
317 TraceControllerImpl::GetInstance()->InitStartupTracing(
318 parsed_command_line_);
319 }
[email protected]50f38422011-11-09 19:21:33320
[email protected]b48c9182011-10-26 18:03:30321 system_monitor_.reset(new base::SystemMonitor);
322 hi_res_timer_manager_.reset(new HighResolutionTimerManager);
323
324 network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
[email protected]67dfea902012-04-03 01:49:09325 audio_manager_.reset(media::AudioManager::Create());
[email protected]bff327f2012-03-11 22:17:19326 online_state_observer_.reset(new BrowserOnlineStateObserver);
[email protected]b48c9182011-10-26 18:03:30327
[email protected]cd1cd4c02011-11-15 01:59:49328#if defined(OS_WIN)
329 system_message_window_.reset(new SystemMessageWindowWin);
330#endif
331
[email protected]99907362012-01-11 05:41:40332 // Prior to any processing happening on the io thread, we create the
333 // plugin service as it is predominantly used from the io thread,
334 // but must be created on the main thread. The service ctor is
335 // inexpensive and does not invoke the io_thread() accessor.
336 PluginService::GetInstance()->Init();
337
[email protected]50462bf02011-11-21 19:13:31338 if (parts_.get())
339 parts_->PostMainMessageLoopStart();
[email protected]b48c9182011-10-26 18:03:30340}
341
[email protected]f573ed6b2012-02-10 15:58:52342void BrowserMainLoop::CreateThreads() {
[email protected]50462bf02011-11-21 19:13:31343 if (parts_.get())
[email protected]69479b922012-02-02 09:56:20344 result_code_ = parts_->PreCreateThreads();
345
346 if (result_code_ > 0)
347 return;
[email protected]2e5b60a22011-11-28 15:56:41348
349 base::Thread::Options default_options;
350 base::Thread::Options io_message_loop_options;
351 io_message_loop_options.message_loop_type = MessageLoop::TYPE_IO;
352 base::Thread::Options ui_message_loop_options;
353 ui_message_loop_options.message_loop_type = MessageLoop::TYPE_UI;
354
355 // Start threads in the order they occur in the BrowserThread::ID
356 // enumeration, except for BrowserThread::UI which is the main
357 // thread.
358 //
359 // Must be size_t so we can increment it.
360 for (size_t thread_id = BrowserThread::UI + 1;
361 thread_id < BrowserThread::ID_COUNT;
362 ++thread_id) {
363 scoped_ptr<BrowserProcessSubThread>* thread_to_start = NULL;
364 base::Thread::Options* options = &default_options;
365
366 switch (thread_id) {
367 case BrowserThread::DB:
368 thread_to_start = &db_thread_;
369 break;
[email protected]e1dd5622011-12-20 12:28:58370 case BrowserThread::WEBKIT_DEPRECATED:
[email protected]c2b77b2392011-12-02 13:00:16371 // Special case as WebKitThread is a separate
372 // type. |thread_to_start| is not used in this case.
[email protected]2e5b60a22011-11-28 15:56:41373 break;
[email protected]31dbf9d2011-12-07 01:25:30374 case BrowserThread::FILE_USER_BLOCKING:
375 thread_to_start = &file_user_blocking_thread_;
376 break;
[email protected]2e5b60a22011-11-28 15:56:41377 case BrowserThread::FILE:
378 thread_to_start = &file_thread_;
379#if defined(OS_WIN)
380 // On Windows, the FILE thread needs to be have a UI message loop
381 // which pumps messages in such a way that Google Update can
382 // communicate back to us.
383 options = &ui_message_loop_options;
384#else
385 options = &io_message_loop_options;
386#endif
387 break;
388 case BrowserThread::PROCESS_LAUNCHER:
389 thread_to_start = &process_launcher_thread_;
390 break;
391 case BrowserThread::CACHE:
392 thread_to_start = &cache_thread_;
393 options = &io_message_loop_options;
394 break;
395 case BrowserThread::IO:
396 thread_to_start = &io_thread_;
397 options = &io_message_loop_options;
398 break;
[email protected]2e5b60a22011-11-28 15:56:41399 case BrowserThread::UI:
400 case BrowserThread::ID_COUNT:
401 default:
402 NOTREACHED();
403 break;
404 }
405
406 BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id);
407
[email protected]e1dd5622011-12-20 12:28:58408 if (thread_id == BrowserThread::WEBKIT_DEPRECATED) {
[email protected]c2b77b2392011-12-02 13:00:16409 webkit_thread_.reset(new WebKitThread);
410 webkit_thread_->Initialize();
411 } else if (thread_to_start) {
[email protected]2e5b60a22011-11-28 15:56:41412 (*thread_to_start).reset(new BrowserProcessSubThread(id));
413 (*thread_to_start)->StartWithOptions(*options);
[email protected]c2b77b2392011-12-02 13:00:16414 } else {
415 NOTREACHED();
[email protected]2e5b60a22011-11-28 15:56:41416 }
[email protected]2e5b60a22011-11-28 15:56:41417 }
418
[email protected]5b040e592012-02-10 02:56:10419 BrowserGpuChannelHostFactory::Initialize();
[email protected]894e8fc2012-02-24 13:29:50420#if defined(USE_AURA)
421 ImageTransportFactory::Initialize();
422#endif
[email protected]5b040e592012-02-10 02:56:10423
[email protected]99907362012-01-11 05:41:40424 BrowserThreadsStarted();
425
[email protected]2e5b60a22011-11-28 15:56:41426 if (parts_.get())
[email protected]50462bf02011-11-21 19:13:31427 parts_->PreMainMessageLoopRun();
[email protected]b48c9182011-10-26 18:03:30428
[email protected]b48c9182011-10-26 18:03:30429 // If the UI thread blocks, the whole UI is unresponsive.
430 // Do not allow disk IO from the UI thread.
431 base::ThreadRestrictions::SetIOAllowed(false);
[email protected]3a7b66d2012-04-26 16:34:16432 base::ThreadRestrictions::DisallowWaiting();
[email protected]f573ed6b2012-02-10 15:58:52433}
434
435void BrowserMainLoop::RunMainMessageLoopParts() {
436 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
[email protected]b48c9182011-10-26 18:03:30437
[email protected]b48c9182011-10-26 18:03:30438 bool ran_main_loop = false;
[email protected]50462bf02011-11-21 19:13:31439 if (parts_.get())
440 ran_main_loop = parts_->MainMessageLoopRun(&result_code_);
441
[email protected]b48c9182011-10-26 18:03:30442 if (!ran_main_loop)
443 MainMessageLoopRun();
444
445 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
[email protected]2e5b60a22011-11-28 15:56:41446}
447
448void BrowserMainLoop::ShutdownThreadsAndCleanUp() {
449 // Teardown may start in PostMainMessageLoopRun, and during teardown we
450 // need to be able to perform IO.
451 base::ThreadRestrictions::SetIOAllowed(true);
452 BrowserThread::PostTask(
[email protected]8f5a7e492012-01-01 02:14:47453 BrowserThread::IO, FROM_HERE,
[email protected]71cb8aa2011-12-29 19:14:00454 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
455 true));
[email protected]2e5b60a22011-11-28 15:56:41456
[email protected]50462bf02011-11-21 19:13:31457 if (parts_.get())
458 parts_->PostMainMessageLoopRun();
[email protected]2e5b60a22011-11-28 15:56:41459
[email protected]64d69de42012-02-06 00:19:54460 // Destroying the GpuProcessHostUIShims on the UI thread posts a task to
461 // delete related objects on the GPU thread. This must be done before
462 // stopping the GPU thread. The GPU thread will close IPC channels to renderer
463 // processes so this has to happen before stopping the IO thread.
464 GpuProcessHostUIShim::DestroyAll();
465
[email protected]99907362012-01-11 05:41:40466 // Cancel pending requests and prevent new requests.
467 if (resource_dispatcher_host_.get())
468 resource_dispatcher_host_.get()->Shutdown();
469
[email protected]28df14d2012-05-16 14:51:22470 speech_recognition_manager_.reset();
471
[email protected]894e8fc2012-02-24 13:29:50472#if defined(USE_AURA)
473 ImageTransportFactory::Terminate();
474#endif
[email protected]5b040e592012-02-10 02:56:10475 BrowserGpuChannelHostFactory::Terminate();
476
[email protected]2e5b60a22011-11-28 15:56:41477 // Must be size_t so we can subtract from it.
478 for (size_t thread_id = BrowserThread::ID_COUNT - 1;
479 thread_id >= (BrowserThread::UI + 1);
480 --thread_id) {
481 // Find the thread object we want to stop. Looping over all valid
482 // BrowserThread IDs and DCHECKing on a missing case in the switch
483 // statement helps avoid a mismatch between this code and the
484 // BrowserThread::ID enumeration.
485 //
486 // The destruction order is the reverse order of occurrence in the
487 // BrowserThread::ID list. The rationale for the order is as
488 // follows (need to be filled in a bit):
489 //
[email protected]2e5b60a22011-11-28 15:56:41490 //
491 // - The IO thread is the only user of the CACHE thread.
492 //
493 // - The PROCESS_LAUNCHER thread must be stopped after IO in case
494 // the IO thread posted a task to terminate a process on the
495 // process launcher thread.
496 //
497 // - (Not sure why FILE needs to stop before WEBKIT.)
498 //
499 // - The WEBKIT thread (which currently is the responsibility of
500 // the embedder to stop, by destroying ResourceDispatcherHost
501 // before the DB thread is stopped)
502 //
503 // - (Not sure why DB stops last.)
504 scoped_ptr<BrowserProcessSubThread>* thread_to_stop = NULL;
505 switch (thread_id) {
506 case BrowserThread::DB:
507 thread_to_stop = &db_thread_;
508 break;
[email protected]e1dd5622011-12-20 12:28:58509 case BrowserThread::WEBKIT_DEPRECATED:
[email protected]c2b77b2392011-12-02 13:00:16510 // Special case as WebKitThread is a separate
511 // type. |thread_to_stop| is not used in this case.
[email protected]99907362012-01-11 05:41:40512
513 // Need to destroy ResourceDispatcherHost before PluginService
514 // and since it caches a pointer to it.
515 resource_dispatcher_host_.reset();
[email protected]2e5b60a22011-11-28 15:56:41516 break;
[email protected]31dbf9d2011-12-07 01:25:30517 case BrowserThread::FILE_USER_BLOCKING:
518 thread_to_stop = &file_user_blocking_thread_;
519 break;
[email protected]2e5b60a22011-11-28 15:56:41520 case BrowserThread::FILE:
521 thread_to_stop = &file_thread_;
[email protected]99907362012-01-11 05:41:40522
523 // Clean up state that lives on or uses the file_thread_ before
524 // it goes away.
525 if (resource_dispatcher_host_.get()) {
526 resource_dispatcher_host_.get()->download_file_manager()->Shutdown();
527 resource_dispatcher_host_.get()->save_file_manager()->Shutdown();
528 }
[email protected]2e5b60a22011-11-28 15:56:41529 break;
530 case BrowserThread::PROCESS_LAUNCHER:
531 thread_to_stop = &process_launcher_thread_;
532 break;
533 case BrowserThread::CACHE:
534 thread_to_stop = &cache_thread_;
535 break;
536 case BrowserThread::IO:
537 thread_to_stop = &io_thread_;
538 break;
[email protected]2e5b60a22011-11-28 15:56:41539 case BrowserThread::UI:
540 case BrowserThread::ID_COUNT:
541 default:
542 NOTREACHED();
543 break;
544 }
545
546 BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id);
547
[email protected]e1dd5622011-12-20 12:28:58548 if (id == BrowserThread::WEBKIT_DEPRECATED) {
[email protected]c2b77b2392011-12-02 13:00:16549 webkit_thread_.reset();
550 } else if (thread_to_stop) {
[email protected]2e5b60a22011-11-28 15:56:41551 thread_to_stop->reset();
[email protected]c2b77b2392011-12-02 13:00:16552 } else {
553 NOTREACHED();
554 }
[email protected]2e5b60a22011-11-28 15:56:41555 }
556
[email protected]3189013e2012-01-19 04:11:57557 // Close the blocking I/O pool after the other threads. Other threads such
558 // as the I/O thread may need to schedule work like closing files or flushing
559 // data during shutdown, so the blocking pool needs to be available. There
560 // may also be slow operations pending that will blcok shutdown, so closing
561 // it here (which will block until required operations are complete) gives
562 // more head start for those operations to finish.
563 BrowserThreadImpl::ShutdownThreadPool();
564
[email protected]2e5b60a22011-11-28 15:56:41565 if (parts_.get())
566 parts_->PostDestroyThreads();
[email protected]b48c9182011-10-26 18:03:30567}
568
569void BrowserMainLoop::InitializeMainThread() {
570 const char* kThreadName = "CrBrowserMain";
571 base::PlatformThread::SetName(kThreadName);
[email protected]de88c5e2012-04-10 23:35:23572 if (main_message_loop_.get())
573 main_message_loop_->set_thread_name(kThreadName);
[email protected]b48c9182011-10-26 18:03:30574
575 // Register the main thread by instantiating it, but don't call any methods.
[email protected]c38831a12011-10-28 12:44:49576 main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
577 MessageLoop::current()));
[email protected]b48c9182011-10-26 18:03:30578}
579
[email protected]99907362012-01-11 05:41:40580
581void BrowserMainLoop::BrowserThreadsStarted() {
582 // RDH needs the IO thread to be created.
[email protected]ea114722012-03-12 01:11:25583 resource_dispatcher_host_.reset(new ResourceDispatcherHostImpl());
[email protected]b14b873b2012-05-03 03:56:09584
[email protected]28df14d2012-05-16 14:51:22585#if defined(ENABLE_INPUT_SPEECH)
586 speech_recognition_manager_.reset(new speech::SpeechRecognitionManagerImpl());
587#endif
588
[email protected]b13afb72012-05-30 07:38:17589 // When running the GPU thread in-process, avoid optimistically starting it
590 // since creating the GPU thread races against creation of the one-and-only
591 // ChildProcess instance which is created by the renderer thread.
592 if (!parsed_command_line_.HasSwitch(switches::kDisableGpuProcessPrelaunch) &&
593 !parsed_command_line_.HasSwitch(switches::kSingleProcess) &&
594 !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) {
595 TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process");
596 BrowserThread::PostTask(
597 BrowserThread::IO, FROM_HERE, base::Bind(
598 base::IgnoreResult(&GpuProcessHost::Get),
599 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED,
600 content::CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP));
601 }
602
[email protected]b14b873b2012-05-03 03:56:09603 // Start the GpuDataManager before we set up the MessageLoops because
604 // otherwise we'll trigger the assertion about doing IO on the UI thread.
605 content::GpuDataManager::GetInstance();
[email protected]99907362012-01-11 05:41:40606}
607
[email protected]b48c9182011-10-26 18:03:30608void BrowserMainLoop::InitializeToolkit() {
609 // TODO(evan): this function is rather subtle, due to the variety
610 // of intersecting ifdefs we have. To keep it easy to follow, there
611 // are no #else branches on any #ifs.
612 // TODO(stevenjb): Move platform specific code into platform specific Parts
613 // (Need to add InitializeToolkit stage to BrowserParts).
[email protected]e60c0232011-11-11 19:56:35614#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]b48c9182011-10-26 18:03:30615 // Glib type system initialization. Needed at least for gconf,
616 // used in net/proxy/proxy_config_service_linux.cc. Most likely
617 // this is superfluous as gtk_init() ought to do this. It's
618 // definitely harmless, so retained as a reminder of this
619 // requirement for gconf.
620 g_type_init();
621
[email protected]b48c9182011-10-26 18:03:30622#if !defined(USE_AURA)
[email protected]38d0b2d42012-01-18 03:37:34623 gfx::GtkInitFromCommandLine(parsed_command_line_);
[email protected]b48c9182011-10-26 18:03:30624#endif
625
626 SetUpGLibLogHandler();
627#endif
628
629#if defined(TOOLKIT_GTK)
630 // It is important for this to happen before the first run dialog, as it
631 // styles the dialog as well.
632 gfx::InitRCStyles();
633#endif
634
635#if defined(OS_WIN)
636 // Init common control sex.
637 INITCOMMONCONTROLSEX config;
638 config.dwSize = sizeof(config);
639 config.dwICC = ICC_WIN95_CLASSES;
640 if (!InitCommonControlsEx(&config))
641 LOG_GETLASTERROR(FATAL);
642#endif
643
[email protected]50462bf02011-11-21 19:13:31644 if (parts_.get())
645 parts_->ToolkitInitialized();
[email protected]b48c9182011-10-26 18:03:30646}
647
648void BrowserMainLoop::MainMessageLoopRun() {
[email protected]716476c2011-12-29 00:07:03649 if (parameters_.ui_task)
[email protected]71cb8aa2011-12-29 19:14:00650 MessageLoopForUI::current()->PostTask(FROM_HERE, *parameters_.ui_task);
[email protected]b48c9182011-10-26 18:03:30651
652#if defined(OS_MACOSX)
653 MessageLoopForUI::current()->Run();
[email protected]a08029b42012-04-25 03:18:46654#elif defined(OS_ANDROID)
655 // Android's main message loop is the Java message loop.
656 NOTREACHED();
[email protected]b48c9182011-10-26 18:03:30657#else
658 MessageLoopForUI::current()->RunWithDispatcher(NULL);
659#endif
660}
661
662} // namespace content