[email protected] | 38d0b2d4 | 2012-01-18 03:37:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/browser_main_loop.h" |
| 6 | |
[email protected] | 820de97 | 2012-01-03 18:23:04 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 8 | #include "base/command_line.h" |
| 9 | #include "base/debug/trace_event.h" |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 95f861e | 2013-07-18 02:07:17 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 13 | #include "base/metrics/field_trial.h" |
| 14 | #include "base/metrics/histogram.h" |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | 1d88aea9 | 2013-02-03 08:16:24 | [diff] [blame] | 16 | #include "base/pending_task.h" |
[email protected] | 6c5905b7 | 2013-04-03 19:06:51 | [diff] [blame] | 17 | #include "base/power_monitor/power_monitor.h" |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 18 | #include "base/power_monitor/power_monitor_device_source.h" |
[email protected] | fa20e00 | 2013-07-23 21:20:54 | [diff] [blame] | 19 | #include "base/process/process_metrics.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 20 | #include "base/run_loop.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
| 22 | #include "base/system_monitor/system_monitor.h" |
[email protected] | e5a631a | 2013-09-18 07:33:27 | [diff] [blame] | 23 | #include "base/thread_task_runner_handle.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 24 | #include "base/threading/thread_restrictions.h" |
[email protected] | 89bf27e | 2013-06-27 18:04:56 | [diff] [blame] | 25 | #include "base/timer/hi_res_timer_manager.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 26 | #include "content/browser/browser_thread_impl.h" |
[email protected] | 436c24cd | 2013-08-22 13:02:00 | [diff] [blame] | 27 | #include "content/browser/device_orientation/device_inertial_sensor_service.h" |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 28 | #include "content/browser/download/save_file_manager.h" |
[email protected] | ac671e78 | 2012-06-05 18:48:20 | [diff] [blame] | 29 | #include "content/browser/gamepad/gamepad_service.h" |
[email protected] | 5b040e59 | 2012-02-10 02:56:10 | [diff] [blame] | 30 | #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
[email protected] | 1515493 | 2013-08-21 03:27:24 | [diff] [blame] | 31 | #include "content/browser/gpu/compositor_util.h" |
[email protected] | 7e34315 | 2012-09-20 21:49:53 | [diff] [blame] | 32 | #include "content/browser/gpu/gpu_data_manager_impl.h" |
[email protected] | 99af54b | 2012-03-03 01:06:50 | [diff] [blame] | 33 | #include "content/browser/gpu/gpu_process_host.h" |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 34 | #include "content/browser/gpu/gpu_process_host_ui_shim.h" |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 35 | #include "content/browser/histogram_synchronizer.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 36 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
[email protected] | bff327f | 2012-03-11 22:17:19 | [diff] [blame] | 37 | #include "content/browser/net/browser_online_state_observer.h" |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 38 | #include "content/browser/plugin_service_impl.h" |
[email protected] | f8d1737 | 2013-01-11 09:31:47 | [diff] [blame] | 39 | #include "content/browser/renderer_host/media/audio_mirroring_manager.h" |
[email protected] | 5a309716 | 2012-07-06 12:03:05 | [diff] [blame] | 40 | #include "content/browser/renderer_host/media/media_stream_manager.h" |
[email protected] | 28df14d | 2012-05-16 14:51:22 | [diff] [blame] | 41 | #include "content/browser/speech/speech_recognition_manager_impl.h" |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 42 | #include "content/browser/startup_task_runner.h" |
[email protected] | 3a85b1f | 2013-02-01 04:47:40 | [diff] [blame] | 43 | #include "content/browser/tracing/trace_controller_impl.h" |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 44 | #include "content/browser/webui/content_web_ui_controller_factory.h" |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 45 | #include "content/browser/webui/url_data_manager.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 46 | #include "content/public/browser/browser_main_parts.h" |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 47 | #include "content/public/browser/browser_shutdown.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 48 | #include "content/public/browser/content_browser_client.h" |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 49 | #include "content/public/browser/render_process_host.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 50 | #include "content/public/common/content_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 51 | #include "content/public/common/main_function_params.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 52 | #include "content/public/common/result_codes.h" |
| 53 | #include "crypto/nss_util.h" |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 54 | #include "media/audio/audio_manager.h" |
[email protected] | 8e15369c | 2013-05-10 00:30:15 | [diff] [blame] | 55 | #include "media/base/media.h" |
[email protected] | 61f697f | 2013-08-15 22:02:40 | [diff] [blame] | 56 | #include "media/base/user_input_monitor.h" |
[email protected] | a987515 | 2013-06-22 04:03:03 | [diff] [blame] | 57 | #include "media/midi/midi_manager.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 58 | #include "net/base/network_change_notifier.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 59 | #include "net/socket/client_socket_factory.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 60 | #include "net/ssl/ssl_config_service.h" |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 61 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 62 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 63 | #if defined(USE_AURA) |
[email protected] | bc90af6b | 2013-07-29 20:32:39 | [diff] [blame] | 64 | #include "content/browser/aura/image_transport_factory.h" |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 65 | #endif |
| 66 | |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 67 | #if defined(OS_ANDROID) |
| 68 | #include "base/android/jni_android.h" |
[email protected] | ddb4b20 | 2013-08-20 22:55:54 | [diff] [blame] | 69 | #include "content/browser/android/browser_startup_controller.h" |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 70 | #include "content/browser/android/surface_texture_peer_browser_impl.h" |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 71 | #endif |
| 72 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 73 | #if defined(OS_WIN) |
| 74 | #include <windows.h> |
| 75 | #include <commctrl.h> |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 76 | #include <shellapi.h> |
| 77 | |
[email protected] | 73ddc32b | 2013-05-23 00:33:38 | [diff] [blame] | 78 | #include "base/win/text_services_message_filter.h" |
[email protected] | cd1cd4c0 | 2011-11-15 01:59:49 | [diff] [blame] | 79 | #include "content/browser/system_message_window_win.h" |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 80 | #include "content/common/sandbox_win.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 81 | #include "net/base/winsock_init.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 82 | #include "ui/base/l10n/l10n_util_win.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 83 | #endif |
| 84 | |
[email protected] | a220b593 | 2013-09-21 03:47:44 | [diff] [blame] | 85 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 86 | #include <glib-object.h> |
| 87 | #endif |
| 88 | |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 89 | #if defined(OS_LINUX) |
| 90 | #include "content/browser/device_monitor_linux.h" |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 91 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 92 | #include "content/browser/device_monitor_mac.h" |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 93 | #endif |
| 94 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 95 | #if defined(TOOLKIT_GTK) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 96 | #include "ui/gfx/gtk_util.h" |
| 97 | #endif |
| 98 | |
| 99 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 100 | #include <sys/stat.h> |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 101 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 102 | #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
[email protected] | 13d6b3c | 2012-07-24 01:31:31 | [diff] [blame] | 103 | #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 104 | #endif |
| 105 | |
[email protected] | 111494e | 2013-07-24 18:38:29 | [diff] [blame] | 106 | #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) |
| 107 | #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 108 | #endif |
| 109 | |
[email protected] | 462d4a3 | 2012-01-19 00:11:04 | [diff] [blame] | 110 | #if defined(USE_X11) |
| 111 | #include <X11/Xlib.h> |
| 112 | #endif |
| 113 | |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 114 | // One of the linux specific headers defines this as a macro. |
| 115 | #ifdef DestroyAll |
| 116 | #undef DestroyAll |
| 117 | #endif |
| 118 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 119 | namespace content { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 120 | namespace { |
| 121 | |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 122 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 123 | void SetupSandbox(const CommandLine& parsed_command_line) { |
[email protected] | 3061010 | 2013-07-04 08:41:38 | [diff] [blame] | 124 | TRACE_EVENT0("startup", "SetupSandbox"); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 125 | // 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] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 127 | base::FilePath sandbox_binary; |
| 128 | bool env_chrome_devel_sandbox_set = false; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 129 | struct stat st; |
| 130 | |
[email protected] | b098ef89 | 2013-06-13 17:55:18 | [diff] [blame] | 131 | const bool want_setuid_sandbox = |
| 132 | !parsed_command_line.HasSwitch(switches::kNoSandbox) && |
| 133 | !parsed_command_line.HasSwitch(switches::kDisableSetuidSandbox); |
| 134 | |
[email protected] | dbf253d4 | 2013-06-23 18:53:02 | [diff] [blame] | 135 | if (want_setuid_sandbox) { |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 136 | 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] | dbf253d4 | 2013-06-23 18:53:02 | [diff] [blame] | 155 | static const char no_suid_error[] = "Running without the SUID sandbox! See " |
[email protected] | b098ef89 | 2013-06-13 17:55:18 | [diff] [blame] | 156 | "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment " |
[email protected] | dbf253d4 | 2013-06-23 18:53:02 | [diff] [blame] | 157 | "for more information on developing with the sandbox on."; |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 158 | 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] | a6cbc803 | 2013-08-02 16:16:48 | [diff] [blame] | 164 | |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 165 | // 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] | b098ef89 | 2013-06-13 17:55:18 | [diff] [blame] | 170 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 171 | |
| 172 | // Tickle the sandbox host and zygote host so they fork now. |
[email protected] | 4741d01 | 2013-08-09 20:43:37 | [diff] [blame] | 173 | RenderSandboxHostLinux::GetInstance()->Init(sandbox_binary.value()); |
| 174 | ZygoteHostImpl::GetInstance()->Init(sandbox_binary.value()); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | |
[email protected] | a220b593 | 2013-09-21 03:47:44 | [diff] [blame] | 178 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 179 | static 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] | 8a1ea19 | 2011-11-29 22:40:50 | [diff] [blame] | 199 | } else if (strstr(message, "Unable to retrieve the file info for")) { |
| 200 | LOG(ERROR) << "GTK File code error: " << message; |
[email protected] | d13332030 | 2013-05-23 16:58:21 | [diff] [blame] | 201 | } else if (strstr(message, "Could not find the icon") && |
| 202 | strstr(log_domain, "Gtk")) { |
| 203 | LOG(ERROR) << "GTK icon error: " << message; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 204 | } else if (strstr(message, "Theme file for default has no") || |
| 205 | strstr(message, "Theme directory") || |
[email protected] | 2bdaecf | 2011-12-15 02:44:55 | [diff] [blame] | 206 | strstr(message, "theme pixmap") || |
| 207 | strstr(message, "locate theme engine")) { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 208 | LOG(ERROR) << "GTK theme error: " << message; |
[email protected] | 45d8f47 | 2012-09-20 19:29:20 | [diff] [blame] | 209 | } 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] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 212 | } 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] | 0ed4301 | 2013-03-05 04:02:29 | [diff] [blame] | 218 | } 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] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 222 | } else if (strstr(message, "XDG_RUNTIME_DIR variable not set")) { |
| 223 | LOG(ERROR) << message << " (http://bugs.chromium.org/97293)"; |
[email protected] | 238d5c3 | 2012-11-20 02:27:40 | [diff] [blame] | 224 | } 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] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 227 | } else { |
| 228 | LOG(DFATAL) << log_domain << ": " << message; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static 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] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 250 | // The currently-running BrowserMainLoop. There can be one or zero. |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 251 | BrowserMainLoop* g_current_browser_main_loop = NULL; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 252 | |
| 253 | // This is just to be able to keep ShutdownThreadsAndCleanUp out of |
| 254 | // the public interface of BrowserMainLoop. |
| 255 | class BrowserShutdownImpl { |
| 256 | public: |
| 257 | static void ImmediateShutdownAndExitProcess() { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 258 | DCHECK(g_current_browser_main_loop); |
| 259 | g_current_browser_main_loop->ShutdownThreadsAndCleanUp(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 260 | |
| 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] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 264 | ExitProcess(RESULT_CODE_NORMAL_EXIT); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 265 | #elif defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 266 | _exit(RESULT_CODE_NORMAL_EXIT); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 267 | #else |
| 268 | NOTIMPLEMENTED(); |
| 269 | #endif |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | void ImmediateShutdownAndExitProcess() { |
| 274 | BrowserShutdownImpl::ImmediateShutdownAndExitProcess(); |
| 275 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 276 | |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 277 | // For measuring memory usage after each task. Behind a command line flag. |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 278 | class BrowserMainLoop::MemoryObserver : public base::MessageLoop::TaskObserver { |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 279 | public: |
| 280 | MemoryObserver() {} |
| 281 | virtual ~MemoryObserver() {} |
| 282 | |
[email protected] | 1d88aea9 | 2013-02-03 08:16:24 | [diff] [blame] | 283 | virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE { |
| 284 | } |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 285 | |
[email protected] | 1d88aea9 | 2013-02-03 08:16:24 | [diff] [blame] | 286 | virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE { |
[email protected] | 66ad307 | 2013-02-25 08:53:34 | [diff] [blame] | 287 | #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] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 298 | #endif |
| 299 | } |
| 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(MemoryObserver); |
| 302 | }; |
| 303 | |
| 304 | |
[email protected] | e974ad29 | 2012-03-07 07:34:51 | [diff] [blame] | 305 | // BrowserMainLoop construction / destruction ============================= |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 306 | |
[email protected] | aa44546 | 2013-06-21 17:12:36 | [diff] [blame] | 307 | BrowserMainLoop* BrowserMainLoop::GetInstance() { |
| 308 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 309 | return g_current_browser_main_loop; |
| 310 | } |
| 311 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 312 | BrowserMainLoop::BrowserMainLoop(const MainFunctionParams& parameters) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 313 | : parameters_(parameters), |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 314 | parsed_command_line_(parameters.command_line), |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 315 | result_code_(RESULT_CODE_NORMAL_EXIT), |
| 316 | created_threads_(false) { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 317 | DCHECK(!g_current_browser_main_loop); |
| 318 | g_current_browser_main_loop = this; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | BrowserMainLoop::~BrowserMainLoop() { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 322 | DCHECK_EQ(this, g_current_browser_main_loop); |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 323 | #if !defined(OS_IOS) |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 324 | ui::Clipboard::DestroyClipboardForCurrentThread(); |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 325 | #endif // !defined(OS_IOS) |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 326 | g_current_browser_main_loop = NULL; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void BrowserMainLoop::Init() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 330 | TRACE_EVENT0("startup", "BrowserMainLoop::Init") |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 331 | parts_.reset( |
| 332 | GetContentClient()->browser()->CreateBrowserMainParts(parameters_)); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // BrowserMainLoop stages ================================================== |
| 336 | |
| 337 | void BrowserMainLoop::EarlyInitialization() { |
[email protected] | 3061010 | 2013-07-04 08:41:38 | [diff] [blame] | 338 | TRACE_EVENT0("startup", "BrowserMainLoop::EarlyInitialization"); |
[email protected] | 462d4a3 | 2012-01-19 00:11:04 | [diff] [blame] | 339 | #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] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 348 | if (parts_) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 349 | parts_->PreEarlyInitialization(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 350 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 351 | #if defined(OS_WIN) |
| 352 | net::EnsureWinsockInit(); |
| 353 | #endif |
| 354 | |
[email protected] | 1648b29 | 2011-11-03 23:57:39 | [diff] [blame] | 355 | #if !defined(USE_OPENSSL) |
[email protected] | 8cbcabf | 2013-01-16 03:08:54 | [diff] [blame] | 356 | // We want to be sure to init NSPR on the main thread. |
| 357 | crypto::EnsureNSPRInit(); |
[email protected] | 1648b29 | 2011-11-03 23:57:39 | [diff] [blame] | 358 | #endif // !defined(USE_OPENSSL) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 359 | |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 360 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 361 | SetupSandbox(parsed_command_line_); |
| 362 | #endif |
| 363 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 364 | if (parsed_command_line_.HasSwitch(switches::kEnableSSLCachedInfo)) |
| 365 | net::SSLConfigService::EnableCachedInfo(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 366 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 367 | #if !defined(OS_IOS) |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 368 | 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] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 373 | RenderProcessHost::SetMaxRendererProcessCount(process_limit); |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 374 | } |
| 375 | } |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 376 | #endif // !defined(OS_IOS) |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 377 | |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 378 | if (parts_) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 379 | parts_->PostEarlyInitialization(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void BrowserMainLoop::MainMessageLoopStart() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 383 | TRACE_EVENT0("startup", "BrowserMainLoop::MainMessageLoopStart") |
| 384 | if (parts_) { |
| 385 | TRACE_EVENT0("startup", |
| 386 | "BrowserMainLoop::MainMessageLoopStart:PreMainMessageLoopStart"); |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 387 | parts_->PreMainMessageLoopStart(); |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 388 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 389 | |
| 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] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 393 | if (!parameters_.ui_task) { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 394 | // Override the configured locale with the user's preferred UI language. |
| 395 | l10n_util::OverrideLocaleWithUILanguageList(); |
| 396 | } |
| 397 | #endif |
| 398 | |
[email protected] | de88c5e | 2012-04-10 23:35:23 | [diff] [blame] | 399 | // Create a MessageLoop if one does not already exist for the current thread. |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 400 | if (!base::MessageLoop::current()) |
| 401 | main_message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI)); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 402 | |
| 403 | InitializeMainThread(); |
| 404 | |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 405 | { |
| 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] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 411 | scoped_ptr<base::PowerMonitorSource> power_monitor_source( |
| 412 | new base::PowerMonitorDeviceSource()); |
| 413 | power_monitor_.reset(new base::PowerMonitor(power_monitor_source.Pass())); |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 414 | } |
| 415 | { |
| 416 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:HighResTimerManager") |
[email protected] | 89bf27e | 2013-06-27 18:04:56 | [diff] [blame] | 417 | hi_res_timer_manager_.reset(new base::HighResolutionTimerManager); |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 418 | } |
| 419 | { |
| 420 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:NetworkChangeNotifier") |
| 421 | network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
| 422 | } |
[email protected] | 8e15369c | 2013-05-10 00:30:15 | [diff] [blame] | 423 | |
[email protected] | f13401b | 2013-08-28 00:24:32 | [diff] [blame] | 424 | #if !defined(OS_IOS) |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 425 | { |
| 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] | a987515 | 2013-06-22 04:03:03 | [diff] [blame] | 433 | { |
| 434 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:MIDIManager") |
| 435 | midi_manager_.reset(media::MIDIManager::Create()); |
| 436 | } |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 437 | { |
| 438 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:ContentWebUIController") |
| 439 | WebUIControllerFactory::RegisterFactory( |
| 440 | ContentWebUIControllerFactory::GetInstance()); |
| 441 | } |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 442 | |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 443 | { |
| 444 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:AudioMirroringManager") |
| 445 | audio_mirroring_manager_.reset(new AudioMirroringManager()); |
| 446 | } |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 447 | |
[email protected] | 50f3842 | 2011-11-09 19:21:33 | [diff] [blame] | 448 | // Start tracing to a file if needed. |
[email protected] | 900e912 | 2012-03-13 15:43:08 | [diff] [blame] | 449 | if (base::debug::TraceLog::GetInstance()->IsEnabled()) { |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 450 | TRACE_EVENT0("startup", "BrowserMainLoop::InitStartupTracing") |
[email protected] | 900e912 | 2012-03-13 15:43:08 | [diff] [blame] | 451 | TraceControllerImpl::GetInstance()->InitStartupTracing( |
| 452 | parsed_command_line_); |
| 453 | } |
[email protected] | 50f3842 | 2011-11-09 19:21:33 | [diff] [blame] | 454 | |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 455 | { |
| 456 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:OnlineStateObserver") |
| 457 | online_state_observer_.reset(new BrowserOnlineStateObserver); |
| 458 | } |
[email protected] | e5a631a | 2013-09-18 07:33:27 | [diff] [blame] | 459 | |
| 460 | { |
| 461 | system_stats_monitor_.reset(new base::debug::TraceEventSystemStatsMonitor( |
| 462 | base::ThreadTaskRunnerHandle::Get())); |
| 463 | } |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 464 | #endif // !defined(OS_IOS) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 465 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 466 | #if defined(OS_WIN) |
| 467 | system_message_window_.reset(new SystemMessageWindowWin); |
[email protected] | 73ddc32b | 2013-05-23 00:33:38 | [diff] [blame] | 468 | |
| 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] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 475 | base::MessageLoopForUI::current()->SetMessageFilter( |
| 476 | tsf_message_filter.PassAs<base::MessageLoopForUI::MessageFilter>()); |
[email protected] | 73ddc32b | 2013-05-23 00:33:38 | [diff] [blame] | 477 | } |
| 478 | } |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 479 | #endif |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 480 | |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 481 | if (parts_) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 482 | parts_->PostMainMessageLoopStart(); |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 483 | |
| 484 | #if defined(OS_ANDROID) |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 485 | { |
| 486 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:SurfaceTexturePeer") |
| 487 | SurfaceTexturePeer::InitInstance(new SurfaceTexturePeerBrowserImpl()); |
| 488 | } |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 489 | #endif |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 490 | |
| 491 | if (parsed_command_line_.HasSwitch(switches::kMemoryMetrics)) { |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 492 | TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:MemoryObserver") |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 493 | memory_observer_.reset(new MemoryObserver()); |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 494 | base::MessageLoop::current()->AddTaskObserver(memory_observer_.get()); |
[email protected] | ee57f33 | 2013-01-31 18:13:54 | [diff] [blame] | 495 | } |
[email protected] | 111494e | 2013-07-24 18:38:29 | [diff] [blame] | 496 | |
| 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] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 506 | int BrowserMainLoop::PreCreateThreads() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 507 | |
| 508 | if (parts_) { |
| 509 | TRACE_EVENT0("startup", |
[email protected] | 73852e0 | 2013-06-21 06:51:53 | [diff] [blame] | 510 | "BrowserMainLoop::CreateThreads:PreCreateThreads"); |
[email protected] | 69479b92 | 2012-02-02 09:56:20 | [diff] [blame] | 511 | result_code_ = parts_->PreCreateThreads(); |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 512 | } |
[email protected] | 69479b92 | 2012-02-02 09:56:20 | [diff] [blame] | 513 | |
[email protected] | 73852e0 | 2013-06-21 06:51:53 | [diff] [blame] | 514 | #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] | dfd5365 | 2012-10-25 00:20:02 | [diff] [blame] | 525 | #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] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 531 | return result_code_; |
| 532 | } |
[email protected] | dfd5365 | 2012-10-25 00:20:02 | [diff] [blame] | 533 | |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 534 | void BrowserMainLoop::CreateStartupTasks() { |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 535 | 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] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 563 | |
| 564 | #if defined(OS_ANDROID) |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 565 | if (BrowserMayStartAsynchronously()) { |
| 566 | startup_task_runner_->StartRunningTasksAsync(); |
| 567 | } |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 568 | #endif |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 569 | } |
| 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] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | int BrowserMainLoop::CreateThreads() { |
| 585 | TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 586 | |
| 587 | base::Thread::Options default_options; |
| 588 | base::Thread::Options io_message_loop_options; |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 589 | io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 590 | base::Thread::Options ui_message_loop_options; |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 591 | ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 592 | |
| 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] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 606 | TRACE_EVENT_BEGIN1("startup", |
| 607 | "BrowserMainLoop::CreateThreads:start", |
| 608 | "Thread", "BrowserThread::DB"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 609 | thread_to_start = &db_thread_; |
| 610 | break; |
[email protected] | 31dbf9d | 2011-12-07 01:25:30 | [diff] [blame] | 611 | case BrowserThread::FILE_USER_BLOCKING: |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 612 | TRACE_EVENT_BEGIN1("startup", |
| 613 | "BrowserMainLoop::CreateThreads:start", |
| 614 | "Thread", "BrowserThread::FILE_USER_BLOCKING"); |
[email protected] | 31dbf9d | 2011-12-07 01:25:30 | [diff] [blame] | 615 | thread_to_start = &file_user_blocking_thread_; |
| 616 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 617 | case BrowserThread::FILE: |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 618 | TRACE_EVENT_BEGIN1("startup", |
| 619 | "BrowserMainLoop::CreateThreads:start", |
| 620 | "Thread", "BrowserThread::FILE"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 621 | 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] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 632 | TRACE_EVENT_BEGIN1("startup", |
| 633 | "BrowserMainLoop::CreateThreads:start", |
| 634 | "Thread", "BrowserThread::PROCESS_LAUNCHER"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 635 | thread_to_start = &process_launcher_thread_; |
| 636 | break; |
| 637 | case BrowserThread::CACHE: |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 638 | TRACE_EVENT_BEGIN1("startup", |
| 639 | "BrowserMainLoop::CreateThreads:start", |
| 640 | "Thread", "BrowserThread::CACHE"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 641 | thread_to_start = &cache_thread_; |
| 642 | options = &io_message_loop_options; |
| 643 | break; |
| 644 | case BrowserThread::IO: |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 645 | TRACE_EVENT_BEGIN1("startup", |
| 646 | "BrowserMainLoop::CreateThreads:start", |
| 647 | "Thread", "BrowserThread::IO"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 648 | thread_to_start = &io_thread_; |
| 649 | options = &io_message_loop_options; |
| 650 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 651 | 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] | 08dda5c | 2013-07-19 21:55:53 | [diff] [blame] | 660 | if (thread_to_start) { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 661 | (*thread_to_start).reset(new BrowserProcessSubThread(id)); |
| 662 | (*thread_to_start)->StartWithOptions(*options); |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 663 | } else { |
| 664 | NOTREACHED(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 665 | } |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 666 | |
| 667 | TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThreads:start"); |
| 668 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 669 | } |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 670 | created_threads_ = true; |
| 671 | return result_code_; |
| 672 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 673 | |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 674 | int BrowserMainLoop::PreMainMessageLoopRun() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 675 | if (parts_) { |
| 676 | TRACE_EVENT0("startup", |
| 677 | "BrowserMainLoop::CreateThreads:PreMainMessageLoopRun"); |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 678 | parts_->PreMainMessageLoopRun(); |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 679 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 680 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 681 | // 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] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 684 | base::ThreadRestrictions::DisallowWaiting(); |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 685 | return result_code_; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void BrowserMainLoop::RunMainMessageLoopParts() { |
| 689 | TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 690 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 691 | bool ran_main_loop = false; |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 692 | if (parts_) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 693 | ran_main_loop = parts_->MainMessageLoopRun(&result_code_); |
| 694 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 695 | if (!ran_main_loop) |
| 696 | MainMessageLoopRun(); |
| 697 | |
| 698 | TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | void BrowserMainLoop::ShutdownThreadsAndCleanUp() { |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 702 | if (!created_threads_) { |
| 703 | // Called early, nothing to do |
| 704 | return; |
| 705 | } |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 706 | TRACE_EVENT0("shutdown", "BrowserMainLoop::ShutdownThreadsAndCleanUp") |
| 707 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 708 | // 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] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 712 | BrowserThread::IO, FROM_HERE, |
[email protected] | 71cb8aa | 2011-12-29 19:14:00 | [diff] [blame] | 713 | base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
| 714 | true)); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 715 | |
[email protected] | 7fff586 | 2013-10-15 20:39:01 | [diff] [blame] | 716 | if (RenderProcessHost::run_renderer_in_process() && |
| 717 | !RenderProcessHost::AllHostsIterator().IsAtEnd()) { |
| 718 | delete RenderProcessHost::AllHostsIterator().GetCurrentValue(); |
| 719 | } |
| 720 | |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 721 | if (parts_) { |
| 722 | TRACE_EVENT0("shutdown", |
| 723 | "BrowserMainLoop::Subsystem:PostMainMessageLoopRun"); |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 724 | parts_->PostMainMessageLoopRun(); |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 725 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 726 | |
[email protected] | 111494e | 2013-07-24 18:38:29 | [diff] [blame] | 727 | trace_memory_controller_.reset(); |
[email protected] | e5a631a | 2013-09-18 07:33:27 | [diff] [blame] | 728 | system_stats_monitor_.reset(); |
[email protected] | 111494e | 2013-07-24 18:38:29 | [diff] [blame] | 729 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 730 | #if !defined(OS_IOS) |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 731 | // 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] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 735 | { |
| 736 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUProcessHostShim"); |
| 737 | GpuProcessHostUIShim::DestroyAll(); |
| 738 | } |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 739 | // Cancel pending requests and prevent new requests. |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 740 | if (resource_dispatcher_host_) { |
| 741 | TRACE_EVENT0("shutdown", |
| 742 | "BrowserMainLoop::Subsystem:ResourceDispatcherHost"); |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 743 | resource_dispatcher_host_.get()->Shutdown(); |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 744 | } |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 745 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 746 | #if defined(USE_AURA) |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 747 | { |
| 748 | TRACE_EVENT0("shutdown", |
| 749 | "BrowserMainLoop::Subsystem:ImageTransportFactory"); |
| 750 | ImageTransportFactory::Terminate(); |
| 751 | } |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 752 | #endif |
[email protected] | 5b040e59 | 2012-02-10 02:56:10 | [diff] [blame] | 753 | |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 754 | // 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] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 764 | #endif // !defined(OS_IOS) |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 765 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 766 | // 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] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 779 | // |
| 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] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 786 | // - (Not sure why DB stops last.) |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 787 | switch (thread_id) { |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 788 | case BrowserThread::DB: { |
| 789 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:DBThread"); |
| 790 | db_thread_.reset(); |
| 791 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 792 | break; |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 793 | case BrowserThread::FILE_USER_BLOCKING: { |
| 794 | TRACE_EVENT0("shutdown", |
| 795 | "BrowserMainLoop::Subsystem:FileUserBlockingThread"); |
| 796 | file_user_blocking_thread_.reset(); |
| 797 | } |
[email protected] | 31dbf9d | 2011-12-07 01:25:30 | [diff] [blame] | 798 | break; |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 799 | case BrowserThread::FILE: { |
| 800 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:FileThread"); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 801 | #if !defined(OS_IOS) |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 802 | // 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] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 806 | #endif // !defined(OS_IOS) |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 807 | file_thread_.reset(); |
| 808 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 809 | break; |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 810 | case BrowserThread::PROCESS_LAUNCHER: { |
| 811 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:LauncherThread"); |
| 812 | process_launcher_thread_.reset(); |
| 813 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 814 | break; |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 815 | case BrowserThread::CACHE: { |
| 816 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:CacheThread"); |
| 817 | cache_thread_.reset(); |
| 818 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 819 | break; |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 820 | case BrowserThread::IO: { |
| 821 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IOThread"); |
| 822 | io_thread_.reset(); |
| 823 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 824 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 825 | case BrowserThread::UI: |
| 826 | case BrowserThread::ID_COUNT: |
| 827 | default: |
| 828 | NOTREACHED(); |
| 829 | break; |
| 830 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 831 | } |
| 832 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 833 | #if !defined(OS_IOS) |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 834 | { |
| 835 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IndexedDBThread"); |
| 836 | indexed_db_thread_.reset(); |
| 837 | } |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 838 | #endif |
| 839 | |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 840 | // 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] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 846 | { |
| 847 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:ThreadPool"); |
| 848 | BrowserThreadImpl::ShutdownThreadPool(); |
| 849 | } |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 850 | |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 851 | #if !defined(OS_IOS) |
[email protected] | 33fee2e5 | 2013-01-12 17:16:24 | [diff] [blame] | 852 | // Must happen after the IO thread is shutdown since this may be accessed from |
| 853 | // it. |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 854 | { |
| 855 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUChannelFactory"); |
[email protected] | 96ab016c | 2013-10-23 00:50:29 | [diff] [blame] | 856 | if (BrowserGpuChannelHostFactory::instance()) |
| 857 | BrowserGpuChannelHostFactory::Terminate(); |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 858 | } |
[email protected] | 33fee2e5 | 2013-01-12 17:16:24 | [diff] [blame] | 859 | |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 860 | // 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] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 862 | { |
| 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] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 874 | #endif // !defined(OS_IOS) |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 875 | |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 876 | if (parts_) { |
| 877 | TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:PostDestroyThreads"); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 878 | parts_->PostDestroyThreads(); |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 879 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void BrowserMainLoop::InitializeMainThread() { |
[email protected] | 5be5851 | 2013-05-31 00:22:24 | [diff] [blame] | 883 | TRACE_EVENT0("startup", "BrowserMainLoop::InitializeMainThread") |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 884 | const char* kThreadName = "CrBrowserMain"; |
| 885 | base::PlatformThread::SetName(kThreadName); |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 886 | if (main_message_loop_) |
[email protected] | de88c5e | 2012-04-10 23:35:23 | [diff] [blame] | 887 | main_message_loop_->set_thread_name(kThreadName); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 888 | |
| 889 | // Register the main thread by instantiating it, but don't call any methods. |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 890 | main_thread_.reset( |
| 891 | new BrowserThreadImpl(BrowserThread::UI, base::MessageLoop::current())); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 892 | } |
| 893 | |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 894 | int BrowserMainLoop::BrowserThreadsStarted() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 895 | TRACE_EVENT0("startup", "BrowserMainLoop::BrowserThreadsStarted") |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 896 | |
| 897 | #if !defined(OS_IOS) |
| 898 | indexed_db_thread_.reset(new base::Thread("IndexedDB")); |
| 899 | indexed_db_thread_->Start(); |
| 900 | #endif |
| 901 | |
[email protected] | e9875de7 | 2013-01-24 01:55:06 | [diff] [blame] | 902 | #if defined(OS_ANDROID) |
[email protected] | b38864d | 2013-05-24 14:42:36 | [diff] [blame] | 903 | // 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] | e9875de7 | 2013-01-24 01:55:06 | [diff] [blame] | 909 | #endif |
| 910 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 911 | #if !defined(OS_IOS) |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 912 | HistogramSynchronizer::GetInstance(); |
| 913 | |
[email protected] | 96ab016c | 2013-10-23 00:50:29 | [diff] [blame] | 914 | // 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] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 925 | #if defined(USE_AURA) |
[email protected] | 96ab016c | 2013-10-23 00:50:29 | [diff] [blame] | 926 | if (!GpuDataManagerImpl::GetInstance()->CanUseGpuBrowserCompositor()) { |
| 927 | established_gpu_channel = always_uses_gpu = false; |
| 928 | } |
| 929 | BrowserGpuChannelHostFactory::Initialize(established_gpu_channel); |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 930 | ImageTransportFactory::Initialize(); |
[email protected] | 96ab016c | 2013-10-23 00:50:29 | [diff] [blame] | 931 | #elif defined(OS_ANDROID) |
| 932 | BrowserGpuChannelHostFactory::Initialize(established_gpu_channel); |
| 933 | #endif |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 934 | #endif |
| 935 | |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 936 | #if defined(OS_LINUX) |
| 937 | device_monitor_linux_.reset(new DeviceMonitorLinux()); |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 938 | #elif defined(OS_MACOSX) |
| 939 | device_monitor_mac_.reset(new DeviceMonitorMac()); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 940 | #endif |
| 941 | |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 942 | // 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] | b14b873b | 2012-05-03 03:56:09 | [diff] [blame] | 948 | |
[email protected] | 89c6eaf | 2012-11-26 18:52:27 | [diff] [blame] | 949 | // MediaStreamManager needs the IO thread to be created. |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 950 | { |
| 951 | TRACE_EVENT0("startup", |
| 952 | "BrowserMainLoop::BrowserThreadsStarted:InitMediaStreamManager"); |
| 953 | media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 954 | } |
[email protected] | 89c6eaf | 2012-11-26 18:52:27 | [diff] [blame] | 955 | |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 956 | { |
| 957 | TRACE_EVENT0("startup", |
| 958 | "BrowserMainLoop::BrowserThreadsStarted:InitSpeechRecognition"); |
[email protected] | aa44546 | 2013-06-21 17:12:36 | [diff] [blame] | 959 | speech_recognition_manager_.reset(new SpeechRecognitionManagerImpl( |
| 960 | audio_manager_.get(), media_stream_manager_.get())); |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 961 | } |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 962 | |
[email protected] | 61f697f | 2013-08-15 22:02:40 | [diff] [blame] | 963 | { |
| 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] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 971 | // 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] | 3220d1b | 2012-12-08 04:13:35 | [diff] [blame] | 982 | |
| 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] | a040138 | 2013-05-07 00:12:55 | [diff] [blame] | 986 | if (GpuDataManagerImpl::GetInstance()->GpuAccessAllowed(NULL) && |
[email protected] | 96ab016c | 2013-10-23 00:50:29 | [diff] [blame] | 987 | !established_gpu_channel && |
[email protected] | 234edc0 | 2013-08-20 12:03:13 | [diff] [blame] | 988 | always_uses_gpu && |
[email protected] | 3220d1b | 2012-12-08 04:13:35 | [diff] [blame] | 989 | !parsed_command_line_.HasSwitch(switches::kDisableGpuProcessPrelaunch) && |
| 990 | !parsed_command_line_.HasSwitch(switches::kSingleProcess) && |
| 991 | !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) { |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 992 | TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process", |
| 993 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | 3220d1b | 2012-12-08 04:13:35 | [diff] [blame] | 994 | 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] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 1000 | #endif // !defined(OS_IOS) |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 1001 | return result_code_; |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 1002 | } |
| 1003 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1004 | void BrowserMainLoop::InitializeToolkit() { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 1005 | TRACE_EVENT0("startup", "BrowserMainLoop::InitializeToolkit") |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1006 | // 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] | a220b593 | 2013-09-21 03:47:44 | [diff] [blame] | 1011 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 04827728 | 2013-01-24 04:39:33 | [diff] [blame] | 1012 | // 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] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1016 | // 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] | 04827728 | 2013-01-24 04:39:33 | [diff] [blame] | 1022 | #endif |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1023 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1024 | #if !defined(USE_AURA) |
[email protected] | 38d0b2d4 | 2012-01-18 03:37:34 | [diff] [blame] | 1025 | gfx::GtkInitFromCommandLine(parsed_command_line_); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1026 | #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] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 1046 | if (parts_) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 1047 | parts_->ToolkitInitialized(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | void BrowserMainLoop::MainMessageLoopRun() { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1051 | #if defined(OS_ANDROID) |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 1052 | // Android's main message loop is the Java message loop. |
| 1053 | NOTREACHED(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1054 | #else |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 1055 | DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1056 | if (parameters_.ui_task) |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 1057 | base::MessageLoopForUI::current()->PostTask(FROM_HERE, |
| 1058 | *parameters_.ui_task); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 1059 | |
| 1060 | base::RunLoop run_loop; |
| 1061 | run_loop.Run(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 1062 | #endif |
| 1063 | } |
| 1064 | |
| 1065 | } // namespace content |