blob: ee2abf7f10da2b0953a3c1fad7197e239345548f [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]4741d012013-08-09 20:43:3710#include "base/file_util.h"
[email protected]b48c9182011-10-26 18:03:3011#include "base/logging.h"
[email protected]95f861e2013-07-18 02:07:1712#include "base/message_loop/message_loop.h"
[email protected]b48c9182011-10-26 18:03:3013#include "base/metrics/field_trial.h"
14#include "base/metrics/histogram.h"
[email protected]4741d012013-08-09 20:43:3715#include "base/path_service.h"
[email protected]1d88aea92013-02-03 08:16:2416#include "base/pending_task.h"
[email protected]6c5905b72013-04-03 19:06:5117#include "base/power_monitor/power_monitor.h"
[email protected]9dd90152013-08-02 22:09:1318#include "base/power_monitor/power_monitor_device_source.h"
[email protected]fa20e002013-07-23 21:20:5419#include "base/process/process_metrics.h"
[email protected]8e937c1e2012-06-28 22:57:3020#include "base/run_loop.h"
[email protected]10994d132013-06-11 07:16:1821#include "base/strings/string_number_conversions.h"
22#include "base/system_monitor/system_monitor.h"
[email protected]e5a631a2013-09-18 07:33:2723#include "base/thread_task_runner_handle.h"
[email protected]b48c9182011-10-26 18:03:3024#include "base/threading/thread_restrictions.h"
[email protected]89bf27e2013-06-27 18:04:5625#include "base/timer/hi_res_timer_manager.h"
[email protected]c38831a12011-10-28 12:44:4926#include "content/browser/browser_thread_impl.h"
[email protected]436c24cd2013-08-22 13:02:0027#include "content/browser/device_orientation/device_inertial_sensor_service.h"
[email protected]99907362012-01-11 05:41:4028#include "content/browser/download/save_file_manager.h"
[email protected]ac671e782012-06-05 18:48:2029#include "content/browser/gamepad/gamepad_service.h"
[email protected]5b040e592012-02-10 02:56:1030#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
[email protected]15154932013-08-21 03:27:2431#include "content/browser/gpu/compositor_util.h"
[email protected]7e343152012-09-20 21:49:5332#include "content/browser/gpu/gpu_data_manager_impl.h"
[email protected]99af54b2012-03-03 01:06:5033#include "content/browser/gpu/gpu_process_host.h"
[email protected]64d69de42012-02-06 00:19:5434#include "content/browser/gpu/gpu_process_host_ui_shim.h"
[email protected]c6ff6a32012-07-17 19:01:0135#include "content/browser/histogram_synchronizer.h"
[email protected]678c0362012-12-05 08:02:4436#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]bff327f2012-03-11 22:17:1937#include "content/browser/net/browser_online_state_observer.h"
[email protected]99907362012-01-11 05:41:4038#include "content/browser/plugin_service_impl.h"
[email protected]f8d17372013-01-11 09:31:4739#include "content/browser/renderer_host/media/audio_mirroring_manager.h"
[email protected]5a3097162012-07-06 12:03:0540#include "content/browser/renderer_host/media/media_stream_manager.h"
[email protected]28df14d2012-05-16 14:51:2241#include "content/browser/speech/speech_recognition_manager_impl.h"
[email protected]57624ab2013-08-01 16:01:5142#include "content/browser/startup_task_runner.h"
[email protected]3a85b1f2013-02-01 04:47:4043#include "content/browser/tracing/trace_controller_impl.h"
[email protected]64ccac32013-01-29 21:40:0444#include "content/browser/webui/content_web_ui_controller_factory.h"
[email protected]33c1c26a2013-01-24 21:56:2645#include "content/browser/webui/url_data_manager.h"
[email protected]b48c9182011-10-26 18:03:3046#include "content/public/browser/browser_main_parts.h"
[email protected]2e5b60a22011-11-28 15:56:4147#include "content/public/browser/browser_shutdown.h"
[email protected]b48c9182011-10-26 18:03:3048#include "content/public/browser/content_browser_client.h"
[email protected]a6381cb52012-02-13 22:39:3449#include "content/public/browser/render_process_host.h"
[email protected]b48c9182011-10-26 18:03:3050#include "content/public/common/content_switches.h"
[email protected]4573fbd2011-10-31 20:25:1851#include "content/public/common/main_function_params.h"
[email protected]b48c9182011-10-26 18:03:3052#include "content/public/common/result_codes.h"
53#include "crypto/nss_util.h"
[email protected]52e456b92012-02-23 17:13:1854#include "media/audio/audio_manager.h"
[email protected]8e15369c2013-05-10 00:30:1555#include "media/base/media.h"
[email protected]61f697f2013-08-15 22:02:4056#include "media/base/user_input_monitor.h"
[email protected]a9875152013-06-22 04:03:0357#include "media/midi/midi_manager.h"
[email protected]b48c9182011-10-26 18:03:3058#include "net/base/network_change_notifier.h"
[email protected]b48c9182011-10-26 18:03:3059#include "net/socket/client_socket_factory.h"
[email protected]536fd0b2013-03-14 17:41:5760#include "net/ssl/ssl_config_service.h"
[email protected]2dd33552012-09-11 16:39:5561#include "ui/base/clipboard/clipboard.h"
[email protected]b48c9182011-10-26 18:03:3062
[email protected]894e8fc2012-02-24 13:29:5063#if defined(USE_AURA)
[email protected]bc90af6b2013-07-29 20:32:3964#include "content/browser/aura/image_transport_factory.h"
[email protected]894e8fc2012-02-24 13:29:5065#endif
66
[email protected]40bdb122012-11-10 03:02:3867#if defined(OS_ANDROID)
68#include "base/android/jni_android.h"
[email protected]ddb4b202013-08-20 22:55:5469#include "content/browser/android/browser_startup_controller.h"
[email protected]40bdb122012-11-10 03:02:3870#include "content/browser/android/surface_texture_peer_browser_impl.h"
[email protected]40bdb122012-11-10 03:02:3871#endif
72
[email protected]b48c9182011-10-26 18:03:3073#if defined(OS_WIN)
74#include <windows.h>
75#include <commctrl.h>
[email protected]b48c9182011-10-26 18:03:3076#include <shellapi.h>
77
[email protected]73ddc32b2013-05-23 00:33:3878#include "base/win/text_services_message_filter.h"
[email protected]cd1cd4c02011-11-15 01:59:4979#include "content/browser/system_message_window_win.h"
[email protected]34f48682013-03-20 00:30:1880#include "content/common/sandbox_win.h"
[email protected]b48c9182011-10-26 18:03:3081#include "net/base/winsock_init.h"
[email protected]536fd0b2013-03-14 17:41:5782#include "ui/base/l10n/l10n_util_win.h"
[email protected]b48c9182011-10-26 18:03:3083#endif
84
[email protected]a220b5932013-09-21 03:47:4485#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:3086#include <glib-object.h>
87#endif
88
[email protected]97646c92012-07-31 20:30:0889#if defined(OS_LINUX)
90#include "content/browser/device_monitor_linux.h"
[email protected]a34087bd2012-10-11 17:05:3091#elif defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]14625442012-08-15 12:51:4292#include "content/browser/device_monitor_mac.h"
[email protected]97646c92012-07-31 20:30:0893#endif
94
[email protected]a13283cc2012-04-05 00:21:2295#if defined(TOOLKIT_GTK)
[email protected]b48c9182011-10-26 18:03:3096#include "ui/gfx/gtk_util.h"
97#endif
98
99#if defined(OS_POSIX) && !defined(OS_MACOSX)
100#include <sys/stat.h>
[email protected]094797b72012-09-15 10:51:05101
[email protected]b48c9182011-10-26 18:03:30102#include "content/browser/renderer_host/render_sandbox_host_linux.h"
[email protected]13d6b3c2012-07-24 01:31:31103#include "content/browser/zygote_host/zygote_host_impl_linux.h"
[email protected]b48c9182011-10-26 18:03:30104#endif
105
[email protected]111494e2013-07-24 18:38:29106#if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
107#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
108#endif
109
[email protected]462d4a32012-01-19 00:11:04110#if defined(USE_X11)
111#include <X11/Xlib.h>
112#endif
113
[email protected]64d69de42012-02-06 00:19:54114// One of the linux specific headers defines this as a macro.
115#ifdef DestroyAll
116#undef DestroyAll
117#endif
118
[email protected]130757672012-10-24 00:26:19119namespace content {
[email protected]b48c9182011-10-26 18:03:30120namespace {
121
[email protected]a08029b42012-04-25 03:18:46122#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:30123void SetupSandbox(const CommandLine& parsed_command_line) {
[email protected]30610102013-07-04 08:41:38124 TRACE_EVENT0("startup", "SetupSandbox");
[email protected]b48c9182011-10-26 18:03:30125 // TODO(evanm): move this into SandboxWrapper; I'm just trying to move this
126 // code en masse out of chrome_main for now.
[email protected]4741d012013-08-09 20:43:37127 base::FilePath sandbox_binary;
128 bool env_chrome_devel_sandbox_set = false;
[email protected]b48c9182011-10-26 18:03:30129 struct stat st;
130
[email protected]b098ef892013-06-13 17:55:18131 const bool want_setuid_sandbox =
132 !parsed_command_line.HasSwitch(switches::kNoSandbox) &&
133 !parsed_command_line.HasSwitch(switches::kDisableSetuidSandbox);
134
[email protected]dbf253d42013-06-23 18:53:02135 if (want_setuid_sandbox) {
[email protected]4741d012013-08-09 20:43:37136 base::FilePath exe_dir;
137 if (PathService::Get(base::DIR_EXE, &exe_dir)) {
138 base::FilePath sandbox_candidate = exe_dir.AppendASCII("chrome-sandbox");
139 if (base::PathExists(sandbox_candidate))
140 sandbox_binary = sandbox_candidate;
141 }
142
143 // In user-managed builds, including development builds, an environment
144 // variable is required to enable the sandbox. See
145 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
146 if (sandbox_binary.empty() &&
147 stat(base::kProcSelfExe, &st) == 0 && st.st_uid == getuid()) {
148 const char* devel_sandbox_path = getenv("CHROME_DEVEL_SANDBOX");
149 if (devel_sandbox_path) {
150 env_chrome_devel_sandbox_set = true;
151 sandbox_binary = base::FilePath(devel_sandbox_path);
152 }
153 }
154
[email protected]dbf253d42013-06-23 18:53:02155 static const char no_suid_error[] = "Running without the SUID sandbox! See "
[email protected]b098ef892013-06-13 17:55:18156 "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment "
[email protected]dbf253d42013-06-23 18:53:02157 "for more information on developing with the sandbox on.";
[email protected]4741d012013-08-09 20:43:37158 if (sandbox_binary.empty()) {
159 if (!env_chrome_devel_sandbox_set) {
160 // This needs to be fatal. Talk to [email protected] if you feel
161 // otherwise.
162 LOG(FATAL) << no_suid_error;
163 }
[email protected]a6cbc8032013-08-02 16:16:48164
[email protected]4741d012013-08-09 20:43:37165 // TODO(jln): an empty CHROME_DEVEL_SANDBOX environment variable (as
166 // opposed to a non existing one) is not fatal yet. This is needed
167 // because of existing bots and scripts. Fix it (crbug.com/245376).
168 LOG(ERROR) << no_suid_error;
169 }
[email protected]b098ef892013-06-13 17:55:18170 }
[email protected]b48c9182011-10-26 18:03:30171
172 // Tickle the sandbox host and zygote host so they fork now.
[email protected]4741d012013-08-09 20:43:37173 RenderSandboxHostLinux::GetInstance()->Init(sandbox_binary.value());
174 ZygoteHostImpl::GetInstance()->Init(sandbox_binary.value());
[email protected]b48c9182011-10-26 18:03:30175}
176#endif
177
[email protected]a220b5932013-09-21 03:47:44178#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:30179static void GLibLogHandler(const gchar* log_domain,
180 GLogLevelFlags log_level,
181 const gchar* message,
182 gpointer userdata) {
183 if (!log_domain)
184 log_domain = "<unknown>";
185 if (!message)
186 message = "<no message>";
187
188 if (strstr(message, "Loading IM context type") ||
189 strstr(message, "wrong ELF class: ELFCLASS64")) {
190 // http://crbug.com/9643
191 // Until we have a real 64-bit build or all of these 32-bit package issues
192 // are sorted out, don't fatal on ELF 32/64-bit mismatch warnings and don't
193 // spam the user with more than one of them.
194 static bool alerted = false;
195 if (!alerted) {
196 LOG(ERROR) << "Bug 9643: " << log_domain << ": " << message;
197 alerted = true;
198 }
[email protected]8a1ea192011-11-29 22:40:50199 } else if (strstr(message, "Unable to retrieve the file info for")) {
200 LOG(ERROR) << "GTK File code error: " << message;
[email protected]d133320302013-05-23 16:58:21201 } else if (strstr(message, "Could not find the icon") &&
202 strstr(log_domain, "Gtk")) {
203 LOG(ERROR) << "GTK icon error: " << message;
[email protected]b48c9182011-10-26 18:03:30204 } else if (strstr(message, "Theme file for default has no") ||
205 strstr(message, "Theme directory") ||
[email protected]2bdaecf2011-12-15 02:44:55206 strstr(message, "theme pixmap") ||
207 strstr(message, "locate theme engine")) {
[email protected]b48c9182011-10-26 18:03:30208 LOG(ERROR) << "GTK theme error: " << message;
[email protected]45d8f472012-09-20 19:29:20209 } else if (strstr(message, "Unable to create Ubuntu Menu Proxy") &&
210 strstr(log_domain, "<unknown>")) {
211 LOG(ERROR) << "GTK menu proxy create failed";
[email protected]b48c9182011-10-26 18:03:30212 } else if (strstr(message, "gtk_drag_dest_leave: assertion")) {
213 LOG(ERROR) << "Drag destination deleted: http://crbug.com/18557";
214 } else if (strstr(message, "Out of memory") &&
215 strstr(log_domain, "<unknown>")) {
216 LOG(ERROR) << "DBus call timeout or out of memory: "
217 << "http://crosbug.com/15496";
[email protected]0ed43012013-03-05 04:02:29218 } else if (strstr(message, "Could not connect: Connection refused") &&
219 strstr(log_domain, "<unknown>")) {
220 LOG(ERROR) << "DConf settings backend could not connect to session bus: "
221 << "http://crbug.com/179797";
[email protected]b48c9182011-10-26 18:03:30222 } else if (strstr(message, "XDG_RUNTIME_DIR variable not set")) {
223 LOG(ERROR) << message << " (http://bugs.chromium.org/97293)";
[email protected]238d5c32012-11-20 02:27:40224 } else if (strstr(message, "Attempting to store changes into") ||
225 strstr(message, "Attempting to set the permissions of")) {
226 LOG(ERROR) << message << " (http://bugs.chromium.org/161366)";
[email protected]b48c9182011-10-26 18:03:30227 } else {
228 LOG(DFATAL) << log_domain << ": " << message;
229 }
230}
231
232static void SetUpGLibLogHandler() {
233 // Register GLib-handled assertions to go through our logging system.
234 const char* kLogDomains[] = { NULL, "Gtk", "Gdk", "GLib", "GLib-GObject" };
235 for (size_t i = 0; i < arraysize(kLogDomains); i++) {
236 g_log_set_handler(kLogDomains[i],
237 static_cast<GLogLevelFlags>(G_LOG_FLAG_RECURSION |
238 G_LOG_FLAG_FATAL |
239 G_LOG_LEVEL_ERROR |
240 G_LOG_LEVEL_CRITICAL |
241 G_LOG_LEVEL_WARNING),
242 GLibLogHandler,
243 NULL);
244 }
245}
246#endif
247
248} // namespace
249
[email protected]2e5b60a22011-11-28 15:56:41250// The currently-running BrowserMainLoop. There can be one or zero.
[email protected]52e456b92012-02-23 17:13:18251BrowserMainLoop* g_current_browser_main_loop = NULL;
[email protected]2e5b60a22011-11-28 15:56:41252
253// This is just to be able to keep ShutdownThreadsAndCleanUp out of
254// the public interface of BrowserMainLoop.
255class BrowserShutdownImpl {
256 public:
257 static void ImmediateShutdownAndExitProcess() {
[email protected]52e456b92012-02-23 17:13:18258 DCHECK(g_current_browser_main_loop);
259 g_current_browser_main_loop->ShutdownThreadsAndCleanUp();
[email protected]2e5b60a22011-11-28 15:56:41260
261#if defined(OS_WIN)
262 // At this point the message loop is still running yet we've shut everything
263 // down. If any messages are processed we'll likely crash. Exit now.
[email protected]130757672012-10-24 00:26:19264 ExitProcess(RESULT_CODE_NORMAL_EXIT);
[email protected]2e5b60a22011-11-28 15:56:41265#elif defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]130757672012-10-24 00:26:19266 _exit(RESULT_CODE_NORMAL_EXIT);
[email protected]2e5b60a22011-11-28 15:56:41267#else
268 NOTIMPLEMENTED();
269#endif
270 }
271};
272
273void ImmediateShutdownAndExitProcess() {
274 BrowserShutdownImpl::ImmediateShutdownAndExitProcess();
275}
[email protected]b48c9182011-10-26 18:03:30276
[email protected]ee57f332013-01-31 18:13:54277// For measuring memory usage after each task. Behind a command line flag.
[email protected]dd32b1272013-05-04 14:17:11278class BrowserMainLoop::MemoryObserver : public base::MessageLoop::TaskObserver {
[email protected]ee57f332013-01-31 18:13:54279 public:
280 MemoryObserver() {}
281 virtual ~MemoryObserver() {}
282
[email protected]1d88aea92013-02-03 08:16:24283 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
284 }
[email protected]ee57f332013-01-31 18:13:54285
[email protected]1d88aea92013-02-03 08:16:24286 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
[email protected]66ad3072013-02-25 08:53:34287#if !defined(OS_IOS) // No ProcessMetrics on IOS.
288 scoped_ptr<base::ProcessMetrics> process_metrics(
289 base::ProcessMetrics::CreateProcessMetrics(
290#if defined(OS_MACOSX)
291 base::GetCurrentProcessHandle(), NULL));
292#else
293 base::GetCurrentProcessHandle()));
294#endif
295 size_t private_bytes;
296 process_metrics->GetMemoryBytes(&private_bytes, NULL);
297 HISTOGRAM_MEMORY_KB("Memory.BrowserUsed", private_bytes >> 10);
[email protected]ee57f332013-01-31 18:13:54298#endif
299 }
300 private:
301 DISALLOW_COPY_AND_ASSIGN(MemoryObserver);
302};
303
304
[email protected]e974ad292012-03-07 07:34:51305// BrowserMainLoop construction / destruction =============================
[email protected]b48c9182011-10-26 18:03:30306
[email protected]aa445462013-06-21 17:12:36307BrowserMainLoop* BrowserMainLoop::GetInstance() {
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
309 return g_current_browser_main_loop;
310}
311
[email protected]130757672012-10-24 00:26:19312BrowserMainLoop::BrowserMainLoop(const MainFunctionParams& parameters)
[email protected]b48c9182011-10-26 18:03:30313 : parameters_(parameters),
[email protected]badf5cf2011-10-29 03:44:44314 parsed_command_line_(parameters.command_line),
[email protected]57624ab2013-08-01 16:01:51315 result_code_(RESULT_CODE_NORMAL_EXIT),
316 created_threads_(false) {
[email protected]52e456b92012-02-23 17:13:18317 DCHECK(!g_current_browser_main_loop);
318 g_current_browser_main_loop = this;
[email protected]b48c9182011-10-26 18:03:30319}
320
321BrowserMainLoop::~BrowserMainLoop() {
[email protected]52e456b92012-02-23 17:13:18322 DCHECK_EQ(this, g_current_browser_main_loop);
[email protected]a34087bd2012-10-11 17:05:30323#if !defined(OS_IOS)
[email protected]2dd33552012-09-11 16:39:55324 ui::Clipboard::DestroyClipboardForCurrentThread();
[email protected]a34087bd2012-10-11 17:05:30325#endif // !defined(OS_IOS)
[email protected]52e456b92012-02-23 17:13:18326 g_current_browser_main_loop = NULL;
[email protected]b48c9182011-10-26 18:03:30327}
328
329void BrowserMainLoop::Init() {
[email protected]aa328c72013-05-03 10:55:43330 TRACE_EVENT0("startup", "BrowserMainLoop::Init")
[email protected]50462bf02011-11-21 19:13:31331 parts_.reset(
332 GetContentClient()->browser()->CreateBrowserMainParts(parameters_));
[email protected]b48c9182011-10-26 18:03:30333}
334
335// BrowserMainLoop stages ==================================================
336
337void BrowserMainLoop::EarlyInitialization() {
[email protected]30610102013-07-04 08:41:38338 TRACE_EVENT0("startup", "BrowserMainLoop::EarlyInitialization");
[email protected]462d4a32012-01-19 00:11:04339#if defined(USE_X11)
340 if (parsed_command_line_.HasSwitch(switches::kSingleProcess) ||
341 parsed_command_line_.HasSwitch(switches::kInProcessGPU)) {
342 if (!XInitThreads()) {
343 LOG(ERROR) << "Failed to put Xlib into threaded mode.";
344 }
345 }
346#endif
347
[email protected]59383c782013-04-17 16:43:27348 if (parts_)
[email protected]50462bf02011-11-21 19:13:31349 parts_->PreEarlyInitialization();
[email protected]b48c9182011-10-26 18:03:30350
[email protected]b48c9182011-10-26 18:03:30351#if defined(OS_WIN)
352 net::EnsureWinsockInit();
353#endif
354
[email protected]1648b292011-11-03 23:57:39355#if !defined(USE_OPENSSL)
[email protected]8cbcabf2013-01-16 03:08:54356 // We want to be sure to init NSPR on the main thread.
357 crypto::EnsureNSPRInit();
[email protected]1648b292011-11-03 23:57:39358#endif // !defined(USE_OPENSSL)
[email protected]b48c9182011-10-26 18:03:30359
[email protected]a08029b42012-04-25 03:18:46360#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]b48c9182011-10-26 18:03:30361 SetupSandbox(parsed_command_line_);
362#endif
363
[email protected]b48c9182011-10-26 18:03:30364 if (parsed_command_line_.HasSwitch(switches::kEnableSSLCachedInfo))
365 net::SSLConfigService::EnableCachedInfo();
[email protected]b48c9182011-10-26 18:03:30366
[email protected]759a86a2012-09-10 14:42:54367#if !defined(OS_IOS)
[email protected]a6381cb52012-02-13 22:39:34368 if (parsed_command_line_.HasSwitch(switches::kRendererProcessLimit)) {
369 std::string limit_string = parsed_command_line_.GetSwitchValueASCII(
370 switches::kRendererProcessLimit);
371 size_t process_limit;
372 if (base::StringToSizeT(limit_string, &process_limit)) {
[email protected]130757672012-10-24 00:26:19373 RenderProcessHost::SetMaxRendererProcessCount(process_limit);
[email protected]a6381cb52012-02-13 22:39:34374 }
375 }
[email protected]759a86a2012-09-10 14:42:54376#endif // !defined(OS_IOS)
[email protected]a6381cb52012-02-13 22:39:34377
[email protected]59383c782013-04-17 16:43:27378 if (parts_)
[email protected]50462bf02011-11-21 19:13:31379 parts_->PostEarlyInitialization();
[email protected]b48c9182011-10-26 18:03:30380}
381
382void BrowserMainLoop::MainMessageLoopStart() {
[email protected]aa328c72013-05-03 10:55:43383 TRACE_EVENT0("startup", "BrowserMainLoop::MainMessageLoopStart")
384 if (parts_) {
385 TRACE_EVENT0("startup",
386 "BrowserMainLoop::MainMessageLoopStart:PreMainMessageLoopStart");
[email protected]50462bf02011-11-21 19:13:31387 parts_->PreMainMessageLoopStart();
[email protected]aa328c72013-05-03 10:55:43388 }
[email protected]b48c9182011-10-26 18:03:30389
390#if defined(OS_WIN)
391 // If we're running tests (ui_task is non-null), then the ResourceBundle
392 // has already been initialized.
[email protected]716476c2011-12-29 00:07:03393 if (!parameters_.ui_task) {
[email protected]b48c9182011-10-26 18:03:30394 // Override the configured locale with the user's preferred UI language.
395 l10n_util::OverrideLocaleWithUILanguageList();
396 }
397#endif
398
[email protected]de88c5e2012-04-10 23:35:23399 // Create a MessageLoop if one does not already exist for the current thread.
[email protected]dd32b1272013-05-04 14:17:11400 if (!base::MessageLoop::current())
401 main_message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI));
[email protected]b48c9182011-10-26 18:03:30402
403 InitializeMainThread();
404
[email protected]5be58512013-05-31 00:22:24405 {
406 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:SystemMonitor")
407 system_monitor_.reset(new base::SystemMonitor);
408 }
409 {
410 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:PowerMonitor")
[email protected]9dd90152013-08-02 22:09:13411 scoped_ptr<base::PowerMonitorSource> power_monitor_source(
412 new base::PowerMonitorDeviceSource());
413 power_monitor_.reset(new base::PowerMonitor(power_monitor_source.Pass()));
[email protected]5be58512013-05-31 00:22:24414 }
415 {
416 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:HighResTimerManager")
[email protected]89bf27e2013-06-27 18:04:56417 hi_res_timer_manager_.reset(new base::HighResolutionTimerManager);
[email protected]5be58512013-05-31 00:22:24418 }
419 {
420 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:NetworkChangeNotifier")
421 network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
422 }
[email protected]8e15369c2013-05-10 00:30:15423
[email protected]f13401b2013-08-28 00:24:32424#if !defined(OS_IOS)
[email protected]5be58512013-05-31 00:22:24425 {
426 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:MediaFeatures")
427 media::InitializeCPUSpecificMediaFeatures();
428 }
429 {
430 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:AudioMan")
431 audio_manager_.reset(media::AudioManager::Create());
432 }
[email protected]a9875152013-06-22 04:03:03433 {
434 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:MIDIManager")
435 midi_manager_.reset(media::MIDIManager::Create());
436 }
[email protected]5be58512013-05-31 00:22:24437 {
438 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:ContentWebUIController")
439 WebUIControllerFactory::RegisterFactory(
440 ContentWebUIControllerFactory::GetInstance());
441 }
[email protected]64ccac32013-01-29 21:40:04442
[email protected]5be58512013-05-31 00:22:24443 {
444 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:AudioMirroringManager")
445 audio_mirroring_manager_.reset(new AudioMirroringManager());
446 }
[email protected]64ccac32013-01-29 21:40:04447
[email protected]50f38422011-11-09 19:21:33448 // Start tracing to a file if needed.
[email protected]900e9122012-03-13 15:43:08449 if (base::debug::TraceLog::GetInstance()->IsEnabled()) {
[email protected]5be58512013-05-31 00:22:24450 TRACE_EVENT0("startup", "BrowserMainLoop::InitStartupTracing")
[email protected]900e9122012-03-13 15:43:08451 TraceControllerImpl::GetInstance()->InitStartupTracing(
452 parsed_command_line_);
453 }
[email protected]50f38422011-11-09 19:21:33454
[email protected]5be58512013-05-31 00:22:24455 {
456 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:OnlineStateObserver")
457 online_state_observer_.reset(new BrowserOnlineStateObserver);
458 }
[email protected]e5a631a2013-09-18 07:33:27459
460 {
461 system_stats_monitor_.reset(new base::debug::TraceEventSystemStatsMonitor(
462 base::ThreadTaskRunnerHandle::Get()));
463 }
[email protected]ebd71962012-12-20 02:56:55464#endif // !defined(OS_IOS)
[email protected]b48c9182011-10-26 18:03:30465
[email protected]759a86a2012-09-10 14:42:54466#if defined(OS_WIN)
467 system_message_window_.reset(new SystemMessageWindowWin);
[email protected]73ddc32b2013-05-23 00:33:38468
469 if (base::win::IsTSFAwareRequired()) {
470 // Create a TSF message filter for the message loop. MessageLoop takes
471 // ownership of the filter.
472 scoped_ptr<base::win::TextServicesMessageFilter> tsf_message_filter(
473 new base::win::TextServicesMessageFilter);
474 if (tsf_message_filter->Init()) {
[email protected]9e7154122013-05-30 23:11:04475 base::MessageLoopForUI::current()->SetMessageFilter(
476 tsf_message_filter.PassAs<base::MessageLoopForUI::MessageFilter>());
[email protected]73ddc32b2013-05-23 00:33:38477 }
478 }
[email protected]759a86a2012-09-10 14:42:54479#endif
[email protected]99907362012-01-11 05:41:40480
[email protected]59383c782013-04-17 16:43:27481 if (parts_)
[email protected]50462bf02011-11-21 19:13:31482 parts_->PostMainMessageLoopStart();
[email protected]40bdb122012-11-10 03:02:38483
484#if defined(OS_ANDROID)
[email protected]5be58512013-05-31 00:22:24485 {
486 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:SurfaceTexturePeer")
487 SurfaceTexturePeer::InitInstance(new SurfaceTexturePeerBrowserImpl());
488 }
[email protected]40bdb122012-11-10 03:02:38489#endif
[email protected]ee57f332013-01-31 18:13:54490
491 if (parsed_command_line_.HasSwitch(switches::kMemoryMetrics)) {
[email protected]5be58512013-05-31 00:22:24492 TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:MemoryObserver")
[email protected]ee57f332013-01-31 18:13:54493 memory_observer_.reset(new MemoryObserver());
[email protected]dd32b1272013-05-04 14:17:11494 base::MessageLoop::current()->AddTaskObserver(memory_observer_.get());
[email protected]ee57f332013-01-31 18:13:54495 }
[email protected]111494e2013-07-24 18:38:29496
497#if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
498 trace_memory_controller_.reset(new base::debug::TraceMemoryController(
499 base::MessageLoop::current()->message_loop_proxy(),
500 ::HeapProfilerWithPseudoStackStart,
501 ::HeapProfilerStop,
502 ::GetHeapProfile));
503#endif
[email protected]b48c9182011-10-26 18:03:30504}
505
[email protected]57624ab2013-08-01 16:01:51506int BrowserMainLoop::PreCreateThreads() {
[email protected]aa328c72013-05-03 10:55:43507
508 if (parts_) {
509 TRACE_EVENT0("startup",
[email protected]73852e02013-06-21 06:51:53510 "BrowserMainLoop::CreateThreads:PreCreateThreads");
[email protected]69479b922012-02-02 09:56:20511 result_code_ = parts_->PreCreateThreads();
[email protected]aa328c72013-05-03 10:55:43512 }
[email protected]69479b922012-02-02 09:56:20513
[email protected]73852e02013-06-21 06:51:53514#if defined(ENABLE_PLUGINS)
515 // Prior to any processing happening on the io thread, we create the
516 // plugin service as it is predominantly used from the io thread,
517 // but must be created on the main thread. The service ctor is
518 // inexpensive and does not invoke the io_thread() accessor.
519 {
520 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads:PluginService")
521 PluginService::GetInstance()->Init();
522 }
523#endif
524
[email protected]dfd53652012-10-25 00:20:02525#if !defined(OS_IOS) && (!defined(GOOGLE_CHROME_BUILD) || defined(OS_ANDROID))
526 // Single-process is an unsupported and not fully tested mode, so
527 // don't enable it for official Chrome builds (except on Android).
528 if (parsed_command_line_.HasSwitch(switches::kSingleProcess))
529 RenderProcessHost::SetRunRendererInProcess(true);
530#endif
[email protected]57624ab2013-08-01 16:01:51531 return result_code_;
532}
[email protected]dfd53652012-10-25 00:20:02533
[email protected]57624ab2013-08-01 16:01:51534void BrowserMainLoop::CreateStartupTasks() {
[email protected]232e09d2013-08-27 15:29:56535 TRACE_EVENT0("startup", "BrowserMainLoop::CreateStartupTasks");
536
537 // First time through, we really want to create all the tasks
538 if (!startup_task_runner_.get()) {
539#if defined(OS_ANDROID)
540 startup_task_runner_ = make_scoped_ptr(new StartupTaskRunner(
541 base::Bind(&BrowserStartupComplete),
542 base::MessageLoop::current()->message_loop_proxy()));
543#else
544 startup_task_runner_ = make_scoped_ptr(new StartupTaskRunner(
545 base::Callback<void(int)>(),
546 base::MessageLoop::current()->message_loop_proxy()));
547#endif
548 StartupTask pre_create_threads =
549 base::Bind(&BrowserMainLoop::PreCreateThreads, base::Unretained(this));
550 startup_task_runner_->AddTask(pre_create_threads);
551
552 StartupTask create_threads =
553 base::Bind(&BrowserMainLoop::CreateThreads, base::Unretained(this));
554 startup_task_runner_->AddTask(create_threads);
555
556 StartupTask browser_thread_started = base::Bind(
557 &BrowserMainLoop::BrowserThreadsStarted, base::Unretained(this));
558 startup_task_runner_->AddTask(browser_thread_started);
559
560 StartupTask pre_main_message_loop_run = base::Bind(
561 &BrowserMainLoop::PreMainMessageLoopRun, base::Unretained(this));
562 startup_task_runner_->AddTask(pre_main_message_loop_run);
[email protected]57624ab2013-08-01 16:01:51563
564#if defined(OS_ANDROID)
[email protected]232e09d2013-08-27 15:29:56565 if (BrowserMayStartAsynchronously()) {
566 startup_task_runner_->StartRunningTasksAsync();
567 }
[email protected]57624ab2013-08-01 16:01:51568#endif
[email protected]232e09d2013-08-27 15:29:56569 }
570#if defined(OS_ANDROID)
571 if (!BrowserMayStartAsynchronously()) {
572 // A second request for asynchronous startup can be ignored, so
573 // StartupRunningTasksAsync is only called first time through. If, however,
574 // this is a request for synchronous startup then it must override any
575 // previous call for async startup, so we call RunAllTasksNow()
576 // unconditionally.
577 startup_task_runner_->RunAllTasksNow();
578 }
579#else
580 startup_task_runner_->RunAllTasksNow();
581#endif
[email protected]57624ab2013-08-01 16:01:51582}
583
584int BrowserMainLoop::CreateThreads() {
585 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads");
[email protected]2e5b60a22011-11-28 15:56:41586
587 base::Thread::Options default_options;
588 base::Thread::Options io_message_loop_options;
[email protected]dd32b1272013-05-04 14:17:11589 io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]2e5b60a22011-11-28 15:56:41590 base::Thread::Options ui_message_loop_options;
[email protected]dd32b1272013-05-04 14:17:11591 ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI;
[email protected]2e5b60a22011-11-28 15:56:41592
593 // Start threads in the order they occur in the BrowserThread::ID
594 // enumeration, except for BrowserThread::UI which is the main
595 // thread.
596 //
597 // Must be size_t so we can increment it.
598 for (size_t thread_id = BrowserThread::UI + 1;
599 thread_id < BrowserThread::ID_COUNT;
600 ++thread_id) {
601 scoped_ptr<BrowserProcessSubThread>* thread_to_start = NULL;
602 base::Thread::Options* options = &default_options;
603
604 switch (thread_id) {
605 case BrowserThread::DB:
[email protected]aa328c72013-05-03 10:55:43606 TRACE_EVENT_BEGIN1("startup",
607 "BrowserMainLoop::CreateThreads:start",
608 "Thread", "BrowserThread::DB");
[email protected]2e5b60a22011-11-28 15:56:41609 thread_to_start = &db_thread_;
610 break;
[email protected]31dbf9d2011-12-07 01:25:30611 case BrowserThread::FILE_USER_BLOCKING:
[email protected]aa328c72013-05-03 10:55:43612 TRACE_EVENT_BEGIN1("startup",
613 "BrowserMainLoop::CreateThreads:start",
614 "Thread", "BrowserThread::FILE_USER_BLOCKING");
[email protected]31dbf9d2011-12-07 01:25:30615 thread_to_start = &file_user_blocking_thread_;
616 break;
[email protected]2e5b60a22011-11-28 15:56:41617 case BrowserThread::FILE:
[email protected]aa328c72013-05-03 10:55:43618 TRACE_EVENT_BEGIN1("startup",
619 "BrowserMainLoop::CreateThreads:start",
620 "Thread", "BrowserThread::FILE");
[email protected]2e5b60a22011-11-28 15:56:41621 thread_to_start = &file_thread_;
622#if defined(OS_WIN)
623 // On Windows, the FILE thread needs to be have a UI message loop
624 // which pumps messages in such a way that Google Update can
625 // communicate back to us.
626 options = &ui_message_loop_options;
627#else
628 options = &io_message_loop_options;
629#endif
630 break;
631 case BrowserThread::PROCESS_LAUNCHER:
[email protected]aa328c72013-05-03 10:55:43632 TRACE_EVENT_BEGIN1("startup",
633 "BrowserMainLoop::CreateThreads:start",
634 "Thread", "BrowserThread::PROCESS_LAUNCHER");
[email protected]2e5b60a22011-11-28 15:56:41635 thread_to_start = &process_launcher_thread_;
636 break;
637 case BrowserThread::CACHE:
[email protected]aa328c72013-05-03 10:55:43638 TRACE_EVENT_BEGIN1("startup",
639 "BrowserMainLoop::CreateThreads:start",
640 "Thread", "BrowserThread::CACHE");
[email protected]2e5b60a22011-11-28 15:56:41641 thread_to_start = &cache_thread_;
642 options = &io_message_loop_options;
643 break;
644 case BrowserThread::IO:
[email protected]aa328c72013-05-03 10:55:43645 TRACE_EVENT_BEGIN1("startup",
646 "BrowserMainLoop::CreateThreads:start",
647 "Thread", "BrowserThread::IO");
[email protected]2e5b60a22011-11-28 15:56:41648 thread_to_start = &io_thread_;
649 options = &io_message_loop_options;
650 break;
[email protected]2e5b60a22011-11-28 15:56:41651 case BrowserThread::UI:
652 case BrowserThread::ID_COUNT:
653 default:
654 NOTREACHED();
655 break;
656 }
657
658 BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id);
659
[email protected]08dda5c2013-07-19 21:55:53660 if (thread_to_start) {
[email protected]2e5b60a22011-11-28 15:56:41661 (*thread_to_start).reset(new BrowserProcessSubThread(id));
662 (*thread_to_start)->StartWithOptions(*options);
[email protected]c2b77b2392011-12-02 13:00:16663 } else {
664 NOTREACHED();
[email protected]2e5b60a22011-11-28 15:56:41665 }
[email protected]aa328c72013-05-03 10:55:43666
667 TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThreads:start");
668
[email protected]2e5b60a22011-11-28 15:56:41669 }
[email protected]57624ab2013-08-01 16:01:51670 created_threads_ = true;
671 return result_code_;
672}
[email protected]2e5b60a22011-11-28 15:56:41673
[email protected]57624ab2013-08-01 16:01:51674int BrowserMainLoop::PreMainMessageLoopRun() {
[email protected]aa328c72013-05-03 10:55:43675 if (parts_) {
676 TRACE_EVENT0("startup",
677 "BrowserMainLoop::CreateThreads:PreMainMessageLoopRun");
[email protected]50462bf02011-11-21 19:13:31678 parts_->PreMainMessageLoopRun();
[email protected]aa328c72013-05-03 10:55:43679 }
[email protected]b48c9182011-10-26 18:03:30680
[email protected]b48c9182011-10-26 18:03:30681 // If the UI thread blocks, the whole UI is unresponsive.
682 // Do not allow disk IO from the UI thread.
683 base::ThreadRestrictions::SetIOAllowed(false);
[email protected]3a7b66d2012-04-26 16:34:16684 base::ThreadRestrictions::DisallowWaiting();
[email protected]57624ab2013-08-01 16:01:51685 return result_code_;
[email protected]f573ed6b2012-02-10 15:58:52686}
687
688void BrowserMainLoop::RunMainMessageLoopParts() {
689 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
[email protected]b48c9182011-10-26 18:03:30690
[email protected]b48c9182011-10-26 18:03:30691 bool ran_main_loop = false;
[email protected]59383c782013-04-17 16:43:27692 if (parts_)
[email protected]50462bf02011-11-21 19:13:31693 ran_main_loop = parts_->MainMessageLoopRun(&result_code_);
694
[email protected]b48c9182011-10-26 18:03:30695 if (!ran_main_loop)
696 MainMessageLoopRun();
697
698 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, "");
[email protected]2e5b60a22011-11-28 15:56:41699}
700
701void BrowserMainLoop::ShutdownThreadsAndCleanUp() {
[email protected]57624ab2013-08-01 16:01:51702 if (!created_threads_) {
703 // Called early, nothing to do
704 return;
705 }
[email protected]89af4002013-09-06 07:47:07706 TRACE_EVENT0("shutdown", "BrowserMainLoop::ShutdownThreadsAndCleanUp")
707
[email protected]2e5b60a22011-11-28 15:56:41708 // Teardown may start in PostMainMessageLoopRun, and during teardown we
709 // need to be able to perform IO.
710 base::ThreadRestrictions::SetIOAllowed(true);
711 BrowserThread::PostTask(
[email protected]8f5a7e492012-01-01 02:14:47712 BrowserThread::IO, FROM_HERE,
[email protected]71cb8aa2011-12-29 19:14:00713 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
714 true));
[email protected]2e5b60a22011-11-28 15:56:41715
[email protected]7fff5862013-10-15 20:39:01716 if (RenderProcessHost::run_renderer_in_process() &&
717 !RenderProcessHost::AllHostsIterator().IsAtEnd()) {
718 delete RenderProcessHost::AllHostsIterator().GetCurrentValue();
719 }
720
[email protected]89af4002013-09-06 07:47:07721 if (parts_) {
722 TRACE_EVENT0("shutdown",
723 "BrowserMainLoop::Subsystem:PostMainMessageLoopRun");
[email protected]50462bf02011-11-21 19:13:31724 parts_->PostMainMessageLoopRun();
[email protected]89af4002013-09-06 07:47:07725 }
[email protected]2e5b60a22011-11-28 15:56:41726
[email protected]111494e2013-07-24 18:38:29727 trace_memory_controller_.reset();
[email protected]e5a631a2013-09-18 07:33:27728 system_stats_monitor_.reset();
[email protected]111494e2013-07-24 18:38:29729
[email protected]759a86a2012-09-10 14:42:54730#if !defined(OS_IOS)
[email protected]64d69de42012-02-06 00:19:54731 // Destroying the GpuProcessHostUIShims on the UI thread posts a task to
732 // delete related objects on the GPU thread. This must be done before
733 // stopping the GPU thread. The GPU thread will close IPC channels to renderer
734 // processes so this has to happen before stopping the IO thread.
[email protected]89af4002013-09-06 07:47:07735 {
736 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUProcessHostShim");
737 GpuProcessHostUIShim::DestroyAll();
738 }
[email protected]99907362012-01-11 05:41:40739 // Cancel pending requests and prevent new requests.
[email protected]89af4002013-09-06 07:47:07740 if (resource_dispatcher_host_) {
741 TRACE_EVENT0("shutdown",
742 "BrowserMainLoop::Subsystem:ResourceDispatcherHost");
[email protected]99907362012-01-11 05:41:40743 resource_dispatcher_host_.get()->Shutdown();
[email protected]89af4002013-09-06 07:47:07744 }
[email protected]99907362012-01-11 05:41:40745
[email protected]894e8fc2012-02-24 13:29:50746#if defined(USE_AURA)
[email protected]89af4002013-09-06 07:47:07747 {
748 TRACE_EVENT0("shutdown",
749 "BrowserMainLoop::Subsystem:ImageTransportFactory");
750 ImageTransportFactory::Terminate();
751 }
[email protected]894e8fc2012-02-24 13:29:50752#endif
[email protected]5b040e592012-02-10 02:56:10753
[email protected]14625442012-08-15 12:51:42754 // The device monitors are using |system_monitor_| as dependency, so delete
755 // them before |system_monitor_| goes away.
756 // On Mac and windows, the monitor needs to be destroyed on the same thread
757 // as they were created. On Linux, the monitor will be deleted when IO thread
758 // goes away.
759#if defined(OS_WIN)
760 system_message_window_.reset();
761#elif defined(OS_MACOSX)
762 device_monitor_mac_.reset();
763#endif
[email protected]759a86a2012-09-10 14:42:54764#endif // !defined(OS_IOS)
[email protected]14625442012-08-15 12:51:42765
[email protected]2e5b60a22011-11-28 15:56:41766 // Must be size_t so we can subtract from it.
767 for (size_t thread_id = BrowserThread::ID_COUNT - 1;
768 thread_id >= (BrowserThread::UI + 1);
769 --thread_id) {
770 // Find the thread object we want to stop. Looping over all valid
771 // BrowserThread IDs and DCHECKing on a missing case in the switch
772 // statement helps avoid a mismatch between this code and the
773 // BrowserThread::ID enumeration.
774 //
775 // The destruction order is the reverse order of occurrence in the
776 // BrowserThread::ID list. The rationale for the order is as
777 // follows (need to be filled in a bit):
778 //
[email protected]2e5b60a22011-11-28 15:56:41779 //
780 // - The IO thread is the only user of the CACHE thread.
781 //
782 // - The PROCESS_LAUNCHER thread must be stopped after IO in case
783 // the IO thread posted a task to terminate a process on the
784 // process launcher thread.
785 //
[email protected]2e5b60a22011-11-28 15:56:41786 // - (Not sure why DB stops last.)
[email protected]2e5b60a22011-11-28 15:56:41787 switch (thread_id) {
[email protected]89af4002013-09-06 07:47:07788 case BrowserThread::DB: {
789 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:DBThread");
790 db_thread_.reset();
791 }
[email protected]2e5b60a22011-11-28 15:56:41792 break;
[email protected]89af4002013-09-06 07:47:07793 case BrowserThread::FILE_USER_BLOCKING: {
794 TRACE_EVENT0("shutdown",
795 "BrowserMainLoop::Subsystem:FileUserBlockingThread");
796 file_user_blocking_thread_.reset();
797 }
[email protected]31dbf9d2011-12-07 01:25:30798 break;
[email protected]89af4002013-09-06 07:47:07799 case BrowserThread::FILE: {
800 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:FileThread");
[email protected]759a86a2012-09-10 14:42:54801#if !defined(OS_IOS)
[email protected]89af4002013-09-06 07:47:07802 // Clean up state that lives on or uses the file_thread_ before
803 // it goes away.
804 if (resource_dispatcher_host_)
805 resource_dispatcher_host_.get()->save_file_manager()->Shutdown();
[email protected]759a86a2012-09-10 14:42:54806#endif // !defined(OS_IOS)
[email protected]89af4002013-09-06 07:47:07807 file_thread_.reset();
808 }
[email protected]2e5b60a22011-11-28 15:56:41809 break;
[email protected]89af4002013-09-06 07:47:07810 case BrowserThread::PROCESS_LAUNCHER: {
811 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:LauncherThread");
812 process_launcher_thread_.reset();
813 }
[email protected]2e5b60a22011-11-28 15:56:41814 break;
[email protected]89af4002013-09-06 07:47:07815 case BrowserThread::CACHE: {
816 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:CacheThread");
817 cache_thread_.reset();
818 }
[email protected]2e5b60a22011-11-28 15:56:41819 break;
[email protected]89af4002013-09-06 07:47:07820 case BrowserThread::IO: {
821 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IOThread");
822 io_thread_.reset();
823 }
[email protected]2e5b60a22011-11-28 15:56:41824 break;
[email protected]2e5b60a22011-11-28 15:56:41825 case BrowserThread::UI:
826 case BrowserThread::ID_COUNT:
827 default:
828 NOTREACHED();
829 break;
830 }
[email protected]2e5b60a22011-11-28 15:56:41831 }
832
[email protected]89acda82013-06-25 20:52:50833#if !defined(OS_IOS)
[email protected]89af4002013-09-06 07:47:07834 {
835 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IndexedDBThread");
836 indexed_db_thread_.reset();
837 }
[email protected]89acda82013-06-25 20:52:50838#endif
839
[email protected]3189013e2012-01-19 04:11:57840 // Close the blocking I/O pool after the other threads. Other threads such
841 // as the I/O thread may need to schedule work like closing files or flushing
842 // data during shutdown, so the blocking pool needs to be available. There
843 // may also be slow operations pending that will blcok shutdown, so closing
844 // it here (which will block until required operations are complete) gives
845 // more head start for those operations to finish.
[email protected]89af4002013-09-06 07:47:07846 {
847 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:ThreadPool");
848 BrowserThreadImpl::ShutdownThreadPool();
849 }
[email protected]3189013e2012-01-19 04:11:57850
[email protected]a34087bd2012-10-11 17:05:30851#if !defined(OS_IOS)
[email protected]33fee2e52013-01-12 17:16:24852 // Must happen after the IO thread is shutdown since this may be accessed from
853 // it.
[email protected]89af4002013-09-06 07:47:07854 {
855 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUChannelFactory");
[email protected]96ab016c2013-10-23 00:50:29856 if (BrowserGpuChannelHostFactory::instance())
857 BrowserGpuChannelHostFactory::Terminate();
[email protected]89af4002013-09-06 07:47:07858 }
[email protected]33fee2e52013-01-12 17:16:24859
[email protected]00c0d042012-09-10 07:06:39860 // Must happen after the I/O thread is shutdown since this class lives on the
861 // I/O thread and isn't threadsafe.
[email protected]89af4002013-09-06 07:47:07862 {
863 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GamepadService");
864 GamepadService::GetInstance()->Terminate();
865 }
866 {
867 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:SensorService");
868 DeviceInertialSensorService::GetInstance()->Shutdown();
869 }
870 {
871 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:DeleteDataSources");
872 URLDataManager::DeleteDataSources();
873 }
[email protected]a34087bd2012-10-11 17:05:30874#endif // !defined(OS_IOS)
[email protected]00c0d042012-09-10 07:06:39875
[email protected]89af4002013-09-06 07:47:07876 if (parts_) {
877 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:PostDestroyThreads");
[email protected]2e5b60a22011-11-28 15:56:41878 parts_->PostDestroyThreads();
[email protected]89af4002013-09-06 07:47:07879 }
[email protected]b48c9182011-10-26 18:03:30880}
881
882void BrowserMainLoop::InitializeMainThread() {
[email protected]5be58512013-05-31 00:22:24883 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeMainThread")
[email protected]b48c9182011-10-26 18:03:30884 const char* kThreadName = "CrBrowserMain";
885 base::PlatformThread::SetName(kThreadName);
[email protected]59383c782013-04-17 16:43:27886 if (main_message_loop_)
[email protected]de88c5e2012-04-10 23:35:23887 main_message_loop_->set_thread_name(kThreadName);
[email protected]b48c9182011-10-26 18:03:30888
889 // Register the main thread by instantiating it, but don't call any methods.
[email protected]dd32b1272013-05-04 14:17:11890 main_thread_.reset(
891 new BrowserThreadImpl(BrowserThread::UI, base::MessageLoop::current()));
[email protected]b48c9182011-10-26 18:03:30892}
893
[email protected]57624ab2013-08-01 16:01:51894int BrowserMainLoop::BrowserThreadsStarted() {
[email protected]aa328c72013-05-03 10:55:43895 TRACE_EVENT0("startup", "BrowserMainLoop::BrowserThreadsStarted")
[email protected]57624ab2013-08-01 16:01:51896
897#if !defined(OS_IOS)
898 indexed_db_thread_.reset(new base::Thread("IndexedDB"));
899 indexed_db_thread_->Start();
900#endif
901
[email protected]e9875de72013-01-24 01:55:06902#if defined(OS_ANDROID)
[email protected]b38864d2013-05-24 14:42:36903 // Up the priority of anything that touches with display tasks
904 // (this thread is UI thread, and io_thread_ is for IPCs).
905 io_thread_->SetPriority(base::kThreadPriority_Display);
906 base::PlatformThread::SetThreadPriority(
907 base::PlatformThread::CurrentHandle(),
908 base::kThreadPriority_Display);
[email protected]e9875de72013-01-24 01:55:06909#endif
910
[email protected]759a86a2012-09-10 14:42:54911#if !defined(OS_IOS)
[email protected]c6ff6a32012-07-17 19:01:01912 HistogramSynchronizer::GetInstance();
913
[email protected]96ab016c2013-10-23 00:50:29914 // Initialize the GpuDataManager before we set up the MessageLoops because
915 // otherwise we'll trigger the assertion about doing IO on the UI thread.
916 GpuDataManagerImpl::GetInstance()->Initialize();
917
918 bool always_uses_gpu = IsForceCompositingModeEnabled();
919 bool established_gpu_channel = false;
920#if defined(USE_AURA) || defined(OS_ANDROID)
921 established_gpu_channel =
922 !parsed_command_line_.HasSwitch(switches::kDisableGpuProcessPrelaunch) ||
923 parsed_command_line_.HasSwitch(switches::kSingleProcess) ||
924 parsed_command_line_.HasSwitch(switches::kInProcessGPU);
[email protected]c6ff6a32012-07-17 19:01:01925#if defined(USE_AURA)
[email protected]96ab016c2013-10-23 00:50:29926 if (!GpuDataManagerImpl::GetInstance()->CanUseGpuBrowserCompositor()) {
927 established_gpu_channel = always_uses_gpu = false;
928 }
929 BrowserGpuChannelHostFactory::Initialize(established_gpu_channel);
[email protected]c6ff6a32012-07-17 19:01:01930 ImageTransportFactory::Initialize();
[email protected]96ab016c2013-10-23 00:50:29931#elif defined(OS_ANDROID)
932 BrowserGpuChannelHostFactory::Initialize(established_gpu_channel);
933#endif
[email protected]c6ff6a32012-07-17 19:01:01934#endif
935
[email protected]97646c92012-07-31 20:30:08936#if defined(OS_LINUX)
937 device_monitor_linux_.reset(new DeviceMonitorLinux());
[email protected]14625442012-08-15 12:51:42938#elif defined(OS_MACOSX)
939 device_monitor_mac_.reset(new DeviceMonitorMac());
[email protected]97646c92012-07-31 20:30:08940#endif
941
[email protected]aa328c72013-05-03 10:55:43942 // RDH needs the IO thread to be created
943 {
944 TRACE_EVENT0("startup",
945 "BrowserMainLoop::BrowserThreadsStarted:InitResourceDispatcherHost");
946 resource_dispatcher_host_.reset(new ResourceDispatcherHostImpl());
947 }
[email protected]b14b873b2012-05-03 03:56:09948
[email protected]89c6eaf2012-11-26 18:52:27949 // MediaStreamManager needs the IO thread to be created.
[email protected]aa328c72013-05-03 10:55:43950 {
951 TRACE_EVENT0("startup",
952 "BrowserMainLoop::BrowserThreadsStarted:InitMediaStreamManager");
953 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
954 }
[email protected]89c6eaf2012-11-26 18:52:27955
[email protected]aa328c72013-05-03 10:55:43956 {
957 TRACE_EVENT0("startup",
958 "BrowserMainLoop::BrowserThreadsStarted:InitSpeechRecognition");
[email protected]aa445462013-06-21 17:12:36959 speech_recognition_manager_.reset(new SpeechRecognitionManagerImpl(
960 audio_manager_.get(), media_stream_manager_.get()));
[email protected]aa328c72013-05-03 10:55:43961 }
[email protected]2dd33552012-09-11 16:39:55962
[email protected]61f697f2013-08-15 22:02:40963 {
964 TRACE_EVENT0(
965 "startup",
966 "BrowserMainLoop::BrowserThreadsStarted::InitUserInputMonitor");
967 user_input_monitor_ = media::UserInputMonitor::Create(
968 io_thread_->message_loop_proxy(), main_thread_->message_loop_proxy());
969 }
970
[email protected]2dd33552012-09-11 16:39:55971 // Alert the clipboard class to which threads are allowed to access the
972 // clipboard:
973 std::vector<base::PlatformThreadId> allowed_clipboard_threads;
974 // The current thread is the UI thread.
975 allowed_clipboard_threads.push_back(base::PlatformThread::CurrentId());
976#if defined(OS_WIN)
977 // On Windows, clipboards are also used on the File or IO threads.
978 allowed_clipboard_threads.push_back(file_thread_->thread_id());
979 allowed_clipboard_threads.push_back(io_thread_->thread_id());
980#endif
981 ui::Clipboard::SetAllowedThreads(allowed_clipboard_threads);
[email protected]3220d1b2012-12-08 04:13:35982
983 // When running the GPU thread in-process, avoid optimistically starting it
984 // since creating the GPU thread races against creation of the one-and-only
985 // ChildProcess instance which is created by the renderer thread.
[email protected]a0401382013-05-07 00:12:55986 if (GpuDataManagerImpl::GetInstance()->GpuAccessAllowed(NULL) &&
[email protected]96ab016c2013-10-23 00:50:29987 !established_gpu_channel &&
[email protected]234edc02013-08-20 12:03:13988 always_uses_gpu &&
[email protected]3220d1b2012-12-08 04:13:35989 !parsed_command_line_.HasSwitch(switches::kDisableGpuProcessPrelaunch) &&
990 !parsed_command_line_.HasSwitch(switches::kSingleProcess) &&
991 !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) {
[email protected]c76faea2013-03-26 07:42:42992 TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process",
993 TRACE_EVENT_SCOPE_THREAD);
[email protected]3220d1b2012-12-08 04:13:35994 BrowserThread::PostTask(
995 BrowserThread::IO, FROM_HERE, base::Bind(
996 base::IgnoreResult(&GpuProcessHost::Get),
997 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED,
998 CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP));
999 }
[email protected]a34087bd2012-10-11 17:05:301000#endif // !defined(OS_IOS)
[email protected]57624ab2013-08-01 16:01:511001 return result_code_;
[email protected]99907362012-01-11 05:41:401002}
1003
[email protected]b48c9182011-10-26 18:03:301004void BrowserMainLoop::InitializeToolkit() {
[email protected]aa328c72013-05-03 10:55:431005 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeToolkit")
[email protected]b48c9182011-10-26 18:03:301006 // TODO(evan): this function is rather subtle, due to the variety
1007 // of intersecting ifdefs we have. To keep it easy to follow, there
1008 // are no #else branches on any #ifs.
1009 // TODO(stevenjb): Move platform specific code into platform specific Parts
1010 // (Need to add InitializeToolkit stage to BrowserParts).
[email protected]a220b5932013-09-21 03:47:441011#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]048277282013-01-24 04:39:331012 // g_type_init will be deprecated in 2.36. 2.35 is the development
1013 // version for 2.36, hence do not call g_type_init starting 2.35.
1014 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init
1015#if !GLIB_CHECK_VERSION(2, 35, 0)
[email protected]b48c9182011-10-26 18:03:301016 // Glib type system initialization. Needed at least for gconf,
1017 // used in net/proxy/proxy_config_service_linux.cc. Most likely
1018 // this is superfluous as gtk_init() ought to do this. It's
1019 // definitely harmless, so retained as a reminder of this
1020 // requirement for gconf.
1021 g_type_init();
[email protected]048277282013-01-24 04:39:331022#endif
[email protected]b48c9182011-10-26 18:03:301023
[email protected]b48c9182011-10-26 18:03:301024#if !defined(USE_AURA)
[email protected]38d0b2d42012-01-18 03:37:341025 gfx::GtkInitFromCommandLine(parsed_command_line_);
[email protected]b48c9182011-10-26 18:03:301026#endif
1027
1028 SetUpGLibLogHandler();
1029#endif
1030
1031#if defined(TOOLKIT_GTK)
1032 // It is important for this to happen before the first run dialog, as it
1033 // styles the dialog as well.
1034 gfx::InitRCStyles();
1035#endif
1036
1037#if defined(OS_WIN)
1038 // Init common control sex.
1039 INITCOMMONCONTROLSEX config;
1040 config.dwSize = sizeof(config);
1041 config.dwICC = ICC_WIN95_CLASSES;
1042 if (!InitCommonControlsEx(&config))
1043 LOG_GETLASTERROR(FATAL);
1044#endif
1045
[email protected]59383c782013-04-17 16:43:271046 if (parts_)
[email protected]50462bf02011-11-21 19:13:311047 parts_->ToolkitInitialized();
[email protected]b48c9182011-10-26 18:03:301048}
1049
1050void BrowserMainLoop::MainMessageLoopRun() {
[email protected]8e937c1e2012-06-28 22:57:301051#if defined(OS_ANDROID)
[email protected]a08029b42012-04-25 03:18:461052 // Android's main message loop is the Java message loop.
1053 NOTREACHED();
[email protected]b48c9182011-10-26 18:03:301054#else
[email protected]dd32b1272013-05-04 14:17:111055 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type());
[email protected]8e937c1e2012-06-28 22:57:301056 if (parameters_.ui_task)
[email protected]dd32b1272013-05-04 14:17:111057 base::MessageLoopForUI::current()->PostTask(FROM_HERE,
1058 *parameters_.ui_task);
[email protected]8e937c1e2012-06-28 22:57:301059
1060 base::RunLoop run_loop;
1061 run_loop.Run();
[email protected]b48c9182011-10-26 18:03:301062#endif
1063}
1064
1065} // namespace content