[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] | b3aabd34 | 2012-06-04 19:33:56 | [diff] [blame] | 10 | #include "base/hi_res_timer_manager.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/message_loop.h" |
| 13 | #include "base/metrics/field_trial.h" |
| 14 | #include "base/metrics/histogram.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 16 | #include "base/string_number_conversions.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 17 | #include "base/threading/thread_restrictions.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 18 | #include "content/browser/browser_thread_impl.h" |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 19 | #include "content/browser/download/save_file_manager.h" |
[email protected] | ac671e78 | 2012-06-05 18:48:20 | [diff] [blame] | 20 | #include "content/browser/gamepad/gamepad_service.h" |
[email protected] | 5b040e59 | 2012-02-10 02:56:10 | [diff] [blame] | 21 | #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
[email protected] | 7e34315 | 2012-09-20 21:49:53 | [diff] [blame] | 22 | #include "content/browser/gpu/gpu_data_manager_impl.h" |
[email protected] | 99af54b | 2012-03-03 01:06:50 | [diff] [blame] | 23 | #include "content/browser/gpu/gpu_process_host.h" |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 24 | #include "content/browser/gpu/gpu_process_host_ui_shim.h" |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 25 | #include "content/browser/histogram_synchronizer.h" |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 26 | #include "content/browser/in_process_webkit/webkit_thread.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 27 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
[email protected] | bff327f | 2012-03-11 22:17:19 | [diff] [blame] | 28 | #include "content/browser/net/browser_online_state_observer.h" |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 29 | #include "content/browser/plugin_service_impl.h" |
[email protected] | 5a309716 | 2012-07-06 12:03:05 | [diff] [blame] | 30 | #include "content/browser/renderer_host/media/media_stream_manager.h" |
[email protected] | 28df14d | 2012-05-16 14:51:22 | [diff] [blame] | 31 | #include "content/browser/speech/speech_recognition_manager_impl.h" |
[email protected] | 900e912 | 2012-03-13 15:43:08 | [diff] [blame] | 32 | #include "content/browser/trace_controller_impl.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 33 | #include "content/public/browser/browser_main_parts.h" |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 34 | #include "content/public/browser/browser_shutdown.h" |
[email protected] | 3220d1b | 2012-12-08 04:13:35 | [diff] [blame] | 35 | #include "content/public/browser/compositor_util.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 36 | #include "content/public/browser/content_browser_client.h" |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 37 | #include "content/public/browser/render_process_host.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 38 | #include "content/public/common/content_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 39 | #include "content/public/common/main_function_params.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 40 | #include "content/public/common/result_codes.h" |
| 41 | #include "crypto/nss_util.h" |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 42 | #include "media/audio/audio_manager.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 43 | #include "net/base/network_change_notifier.h" |
| 44 | #include "net/base/ssl_config_service.h" |
| 45 | #include "net/socket/client_socket_factory.h" |
| 46 | #include "net/socket/tcp_client_socket.h" |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 47 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 48 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 49 | #if defined(USE_AURA) |
| 50 | #include "content/browser/renderer_host/image_transport_factory.h" |
| 51 | #endif |
| 52 | |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 53 | #if defined(OS_ANDROID) |
| 54 | #include "base/android/jni_android.h" |
| 55 | #include "content/browser/android/surface_texture_peer_browser_impl.h" |
| 56 | #include "content/browser/device_orientation/data_fetcher_impl_android.h" |
| 57 | #endif |
| 58 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 59 | #if defined(OS_WIN) |
| 60 | #include <windows.h> |
| 61 | #include <commctrl.h> |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 62 | #include <shellapi.h> |
| 63 | |
[email protected] | cd1cd4c0 | 2011-11-15 01:59:49 | [diff] [blame] | 64 | #include "content/browser/system_message_window_win.h" |
[email protected] | 6a7da76 | 2012-03-31 02:12:33 | [diff] [blame] | 65 | #include "content/common/sandbox_policy.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 66 | #include "ui/base/l10n/l10n_util_win.h" |
| 67 | #include "net/base/winsock_init.h" |
| 68 | #endif |
| 69 | |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 70 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 71 | #include <glib-object.h> |
| 72 | #endif |
| 73 | |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 74 | #if defined(OS_LINUX) |
| 75 | #include "content/browser/device_monitor_linux.h" |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 76 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 77 | #include "content/browser/device_monitor_mac.h" |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 78 | #endif |
| 79 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 80 | #if defined(TOOLKIT_GTK) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 81 | #include "ui/gfx/gtk_util.h" |
| 82 | #endif |
| 83 | |
| 84 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 85 | #include <sys/stat.h> |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 86 | |
| 87 | #include "base/process_util.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 88 | #include "content/browser/renderer_host/render_sandbox_host_linux.h" |
[email protected] | 13d6b3c | 2012-07-24 01:31:31 | [diff] [blame] | 89 | #include "content/browser/zygote_host/zygote_host_impl_linux.h" |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 90 | #endif |
| 91 | |
[email protected] | 462d4a3 | 2012-01-19 00:11:04 | [diff] [blame] | 92 | #if defined(USE_X11) |
| 93 | #include <X11/Xlib.h> |
| 94 | #endif |
| 95 | |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 96 | // One of the linux specific headers defines this as a macro. |
| 97 | #ifdef DestroyAll |
| 98 | #undef DestroyAll |
| 99 | #endif |
| 100 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 101 | namespace content { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 102 | namespace { |
| 103 | |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 104 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 105 | void SetupSandbox(const CommandLine& parsed_command_line) { |
| 106 | // TODO(evanm): move this into SandboxWrapper; I'm just trying to move this |
| 107 | // code en masse out of chrome_main for now. |
| 108 | const char* sandbox_binary = NULL; |
| 109 | struct stat st; |
| 110 | |
| 111 | // In Chromium branded builds, developers can set an environment variable to |
| 112 | // use the development sandbox. See |
| 113 | // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 114 | if (stat(base::kProcSelfExe, &st) == 0 && st.st_uid == getuid()) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 115 | sandbox_binary = getenv("CHROME_DEVEL_SANDBOX"); |
| 116 | |
| 117 | #if defined(LINUX_SANDBOX_PATH) |
| 118 | if (!sandbox_binary) |
| 119 | sandbox_binary = LINUX_SANDBOX_PATH; |
| 120 | #endif |
| 121 | |
| 122 | std::string sandbox_cmd; |
| 123 | if (sandbox_binary && !parsed_command_line.HasSwitch(switches::kNoSandbox)) |
| 124 | sandbox_cmd = sandbox_binary; |
| 125 | |
| 126 | // Tickle the sandbox host and zygote host so they fork now. |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 127 | RenderSandboxHostLinux::GetInstance()->Init(sandbox_cmd); |
| 128 | ZygoteHostImpl::GetInstance()->Init(sandbox_cmd); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 129 | } |
| 130 | #endif |
| 131 | |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 132 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 133 | static void GLibLogHandler(const gchar* log_domain, |
| 134 | GLogLevelFlags log_level, |
| 135 | const gchar* message, |
| 136 | gpointer userdata) { |
| 137 | if (!log_domain) |
| 138 | log_domain = "<unknown>"; |
| 139 | if (!message) |
| 140 | message = "<no message>"; |
| 141 | |
| 142 | if (strstr(message, "Loading IM context type") || |
| 143 | strstr(message, "wrong ELF class: ELFCLASS64")) { |
| 144 | // http://crbug.com/9643 |
| 145 | // Until we have a real 64-bit build or all of these 32-bit package issues |
| 146 | // are sorted out, don't fatal on ELF 32/64-bit mismatch warnings and don't |
| 147 | // spam the user with more than one of them. |
| 148 | static bool alerted = false; |
| 149 | if (!alerted) { |
| 150 | LOG(ERROR) << "Bug 9643: " << log_domain << ": " << message; |
| 151 | alerted = true; |
| 152 | } |
[email protected] | 8a1ea19 | 2011-11-29 22:40:50 | [diff] [blame] | 153 | } else if (strstr(message, "Unable to retrieve the file info for")) { |
| 154 | LOG(ERROR) << "GTK File code error: " << message; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 155 | } else if (strstr(message, "Theme file for default has no") || |
| 156 | strstr(message, "Theme directory") || |
[email protected] | 2bdaecf | 2011-12-15 02:44:55 | [diff] [blame] | 157 | strstr(message, "theme pixmap") || |
| 158 | strstr(message, "locate theme engine")) { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 159 | LOG(ERROR) << "GTK theme error: " << message; |
[email protected] | 45d8f47 | 2012-09-20 19:29:20 | [diff] [blame] | 160 | } else if (strstr(message, "Unable to create Ubuntu Menu Proxy") && |
| 161 | strstr(log_domain, "<unknown>")) { |
| 162 | LOG(ERROR) << "GTK menu proxy create failed"; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 163 | } else if (strstr(message, "gtk_drag_dest_leave: assertion")) { |
| 164 | LOG(ERROR) << "Drag destination deleted: http://crbug.com/18557"; |
| 165 | } else if (strstr(message, "Out of memory") && |
| 166 | strstr(log_domain, "<unknown>")) { |
| 167 | LOG(ERROR) << "DBus call timeout or out of memory: " |
| 168 | << "http://crosbug.com/15496"; |
| 169 | } else if (strstr(message, "XDG_RUNTIME_DIR variable not set")) { |
| 170 | LOG(ERROR) << message << " (http://bugs.chromium.org/97293)"; |
[email protected] | 238d5c3 | 2012-11-20 02:27:40 | [diff] [blame] | 171 | } else if (strstr(message, "Attempting to store changes into") || |
| 172 | strstr(message, "Attempting to set the permissions of")) { |
| 173 | LOG(ERROR) << message << " (http://bugs.chromium.org/161366)"; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 174 | } else { |
| 175 | LOG(DFATAL) << log_domain << ": " << message; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void SetUpGLibLogHandler() { |
| 180 | // Register GLib-handled assertions to go through our logging system. |
| 181 | const char* kLogDomains[] = { NULL, "Gtk", "Gdk", "GLib", "GLib-GObject" }; |
| 182 | for (size_t i = 0; i < arraysize(kLogDomains); i++) { |
| 183 | g_log_set_handler(kLogDomains[i], |
| 184 | static_cast<GLogLevelFlags>(G_LOG_FLAG_RECURSION | |
| 185 | G_LOG_FLAG_FATAL | |
| 186 | G_LOG_LEVEL_ERROR | |
| 187 | G_LOG_LEVEL_CRITICAL | |
| 188 | G_LOG_LEVEL_WARNING), |
| 189 | GLibLogHandler, |
| 190 | NULL); |
| 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | } // namespace |
| 196 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 197 | // The currently-running BrowserMainLoop. There can be one or zero. |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 198 | BrowserMainLoop* g_current_browser_main_loop = NULL; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 199 | |
| 200 | // This is just to be able to keep ShutdownThreadsAndCleanUp out of |
| 201 | // the public interface of BrowserMainLoop. |
| 202 | class BrowserShutdownImpl { |
| 203 | public: |
| 204 | static void ImmediateShutdownAndExitProcess() { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 205 | DCHECK(g_current_browser_main_loop); |
| 206 | g_current_browser_main_loop->ShutdownThreadsAndCleanUp(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 207 | |
| 208 | #if defined(OS_WIN) |
| 209 | // At this point the message loop is still running yet we've shut everything |
| 210 | // down. If any messages are processed we'll likely crash. Exit now. |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 211 | ExitProcess(RESULT_CODE_NORMAL_EXIT); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 212 | #elif defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 213 | _exit(RESULT_CODE_NORMAL_EXIT); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 214 | #else |
| 215 | NOTIMPLEMENTED(); |
| 216 | #endif |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | void ImmediateShutdownAndExitProcess() { |
| 221 | BrowserShutdownImpl::ImmediateShutdownAndExitProcess(); |
| 222 | } |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 223 | |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 224 | // static |
[email protected] | 67dfea90 | 2012-04-03 01:49:09 | [diff] [blame] | 225 | media::AudioManager* BrowserMainLoop::GetAudioManager() { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 226 | return g_current_browser_main_loop->audio_manager_.get(); |
| 227 | } |
| 228 | |
[email protected] | 5a309716 | 2012-07-06 12:03:05 | [diff] [blame] | 229 | // static |
[email protected] | 1d232b9 | 2012-10-29 17:39:07 | [diff] [blame] | 230 | MediaStreamManager* BrowserMainLoop::GetMediaStreamManager() { |
[email protected] | 5a309716 | 2012-07-06 12:03:05 | [diff] [blame] | 231 | return g_current_browser_main_loop->media_stream_manager_.get(); |
| 232 | } |
[email protected] | e974ad29 | 2012-03-07 07:34:51 | [diff] [blame] | 233 | // BrowserMainLoop construction / destruction ============================= |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 234 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 235 | BrowserMainLoop::BrowserMainLoop(const MainFunctionParams& parameters) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 236 | : parameters_(parameters), |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 237 | parsed_command_line_(parameters.command_line), |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 238 | result_code_(RESULT_CODE_NORMAL_EXIT) { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 239 | DCHECK(!g_current_browser_main_loop); |
| 240 | g_current_browser_main_loop = this; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | BrowserMainLoop::~BrowserMainLoop() { |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 244 | DCHECK_EQ(this, g_current_browser_main_loop); |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 245 | #if !defined(OS_IOS) |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 246 | ui::Clipboard::DestroyClipboardForCurrentThread(); |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 247 | #endif // !defined(OS_IOS) |
[email protected] | 52e456b9 | 2012-02-23 17:13:18 | [diff] [blame] | 248 | g_current_browser_main_loop = NULL; |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void BrowserMainLoop::Init() { |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 252 | parts_.reset( |
| 253 | GetContentClient()->browser()->CreateBrowserMainParts(parameters_)); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // BrowserMainLoop stages ================================================== |
| 257 | |
| 258 | void BrowserMainLoop::EarlyInitialization() { |
[email protected] | 462d4a3 | 2012-01-19 00:11:04 | [diff] [blame] | 259 | #if defined(USE_X11) |
| 260 | if (parsed_command_line_.HasSwitch(switches::kSingleProcess) || |
| 261 | parsed_command_line_.HasSwitch(switches::kInProcessGPU)) { |
| 262 | if (!XInitThreads()) { |
| 263 | LOG(ERROR) << "Failed to put Xlib into threaded mode."; |
| 264 | } |
| 265 | } |
| 266 | #endif |
| 267 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 268 | if (parts_.get()) |
| 269 | parts_->PreEarlyInitialization(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 270 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 271 | #if defined(OS_WIN) |
| 272 | net::EnsureWinsockInit(); |
| 273 | #endif |
| 274 | |
[email protected] | 1648b29 | 2011-11-03 23:57:39 | [diff] [blame] | 275 | #if !defined(USE_OPENSSL) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 276 | // Use NSS for SSL by default. |
| 277 | // The default client socket factory uses NSS for SSL by default on |
| 278 | // Windows and Mac. |
| 279 | bool init_nspr = false; |
| 280 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 281 | if (parsed_command_line_.HasSwitch(switches::kUseSystemSSL)) { |
| 282 | net::ClientSocketFactory::UseSystemSSL(); |
| 283 | } else { |
| 284 | init_nspr = true; |
| 285 | } |
[email protected] | c480510 | 2012-03-02 23:34:32 | [diff] [blame] | 286 | UMA_HISTOGRAM_BOOLEAN("Chrome.CommandLineUseSystemSSL", !init_nspr); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 287 | #elif defined(USE_NSS) |
| 288 | init_nspr = true; |
| 289 | #endif |
| 290 | if (init_nspr) { |
| 291 | // We want to be sure to init NSPR on the main thread. |
| 292 | crypto::EnsureNSPRInit(); |
| 293 | } |
[email protected] | 1648b29 | 2011-11-03 23:57:39 | [diff] [blame] | 294 | #endif // !defined(USE_OPENSSL) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 295 | |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 296 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 297 | SetupSandbox(parsed_command_line_); |
| 298 | #endif |
| 299 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 300 | if (parsed_command_line_.HasSwitch(switches::kEnableSSLCachedInfo)) |
| 301 | net::SSLConfigService::EnableCachedInfo(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 302 | |
| 303 | // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't |
| 304 | // seem dependent on SSL initialization(). |
| 305 | if (parsed_command_line_.HasSwitch(switches::kEnableTcpFastOpen)) |
| 306 | net::set_tcp_fastopen_enabled(true); |
| 307 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 308 | #if !defined(OS_IOS) |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 309 | if (parsed_command_line_.HasSwitch(switches::kRendererProcessLimit)) { |
| 310 | std::string limit_string = parsed_command_line_.GetSwitchValueASCII( |
| 311 | switches::kRendererProcessLimit); |
| 312 | size_t process_limit; |
| 313 | if (base::StringToSizeT(limit_string, &process_limit)) { |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 314 | RenderProcessHost::SetMaxRendererProcessCount(process_limit); |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 315 | } |
| 316 | } |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 317 | #endif // !defined(OS_IOS) |
[email protected] | a6381cb5 | 2012-02-13 22:39:34 | [diff] [blame] | 318 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 319 | if (parts_.get()) |
| 320 | parts_->PostEarlyInitialization(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void BrowserMainLoop::MainMessageLoopStart() { |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 324 | if (parts_.get()) |
| 325 | parts_->PreMainMessageLoopStart(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 326 | |
| 327 | #if defined(OS_WIN) |
| 328 | // If we're running tests (ui_task is non-null), then the ResourceBundle |
| 329 | // has already been initialized. |
[email protected] | 716476c | 2011-12-29 00:07:03 | [diff] [blame] | 330 | if (!parameters_.ui_task) { |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 331 | // Override the configured locale with the user's preferred UI language. |
| 332 | l10n_util::OverrideLocaleWithUILanguageList(); |
| 333 | } |
| 334 | #endif |
| 335 | |
[email protected] | de88c5e | 2012-04-10 23:35:23 | [diff] [blame] | 336 | // Create a MessageLoop if one does not already exist for the current thread. |
| 337 | if (!MessageLoop::current()) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 338 | main_message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI)); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 339 | |
| 340 | InitializeMainThread(); |
| 341 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 342 | system_monitor_.reset(new base::SystemMonitor); |
| 343 | hi_res_timer_manager_.reset(new HighResolutionTimerManager); |
| 344 | network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
| 345 | audio_manager_.reset(media::AudioManager::Create()); |
| 346 | |
| 347 | #if !defined(OS_IOS) |
[email protected] | 50f3842 | 2011-11-09 19:21:33 | [diff] [blame] | 348 | // Start tracing to a file if needed. |
[email protected] | 900e912 | 2012-03-13 15:43:08 | [diff] [blame] | 349 | if (base::debug::TraceLog::GetInstance()->IsEnabled()) { |
| 350 | TraceControllerImpl::GetInstance()->InitStartupTracing( |
| 351 | parsed_command_line_); |
| 352 | } |
[email protected] | 50f3842 | 2011-11-09 19:21:33 | [diff] [blame] | 353 | |
[email protected] | bff327f | 2012-03-11 22:17:19 | [diff] [blame] | 354 | online_state_observer_.reset(new BrowserOnlineStateObserver); |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame^] | 355 | #endif // !defined(OS_IOS) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 356 | |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame^] | 357 | #if defined(ENABLE_PLUGINS) |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 358 | // Prior to any processing happening on the io thread, we create the |
| 359 | // plugin service as it is predominantly used from the io thread, |
| 360 | // but must be created on the main thread. The service ctor is |
| 361 | // inexpensive and does not invoke the io_thread() accessor. |
| 362 | PluginService::GetInstance()->Init(); |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame^] | 363 | #endif |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 364 | |
| 365 | #if defined(OS_WIN) |
| 366 | system_message_window_.reset(new SystemMessageWindowWin); |
| 367 | #endif |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 368 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 369 | if (parts_.get()) |
| 370 | parts_->PostMainMessageLoopStart(); |
[email protected] | 40bdb12 | 2012-11-10 03:02:38 | [diff] [blame] | 371 | |
| 372 | #if defined(OS_ANDROID) |
| 373 | SurfaceTexturePeer::InitInstance(new SurfaceTexturePeerBrowserImpl( |
| 374 | parameters_.command_line.HasSwitch( |
| 375 | switches::kMediaPlayerInRenderProcess))); |
| 376 | DataFetcherImplAndroid::Init(base::android::AttachCurrentThread()); |
| 377 | #endif |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 380 | void BrowserMainLoop::CreateThreads() { |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 381 | if (parts_.get()) |
[email protected] | 69479b92 | 2012-02-02 09:56:20 | [diff] [blame] | 382 | result_code_ = parts_->PreCreateThreads(); |
| 383 | |
[email protected] | dfd5365 | 2012-10-25 00:20:02 | [diff] [blame] | 384 | #if !defined(OS_IOS) && (!defined(GOOGLE_CHROME_BUILD) || defined(OS_ANDROID)) |
| 385 | // Single-process is an unsupported and not fully tested mode, so |
| 386 | // don't enable it for official Chrome builds (except on Android). |
| 387 | if (parsed_command_line_.HasSwitch(switches::kSingleProcess)) |
| 388 | RenderProcessHost::SetRunRendererInProcess(true); |
| 389 | #endif |
| 390 | |
[email protected] | 69479b92 | 2012-02-02 09:56:20 | [diff] [blame] | 391 | if (result_code_ > 0) |
| 392 | return; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 393 | |
| 394 | base::Thread::Options default_options; |
| 395 | base::Thread::Options io_message_loop_options; |
| 396 | io_message_loop_options.message_loop_type = MessageLoop::TYPE_IO; |
| 397 | base::Thread::Options ui_message_loop_options; |
| 398 | ui_message_loop_options.message_loop_type = MessageLoop::TYPE_UI; |
| 399 | |
| 400 | // Start threads in the order they occur in the BrowserThread::ID |
| 401 | // enumeration, except for BrowserThread::UI which is the main |
| 402 | // thread. |
| 403 | // |
| 404 | // Must be size_t so we can increment it. |
| 405 | for (size_t thread_id = BrowserThread::UI + 1; |
| 406 | thread_id < BrowserThread::ID_COUNT; |
| 407 | ++thread_id) { |
| 408 | scoped_ptr<BrowserProcessSubThread>* thread_to_start = NULL; |
| 409 | base::Thread::Options* options = &default_options; |
| 410 | |
| 411 | switch (thread_id) { |
| 412 | case BrowserThread::DB: |
| 413 | thread_to_start = &db_thread_; |
| 414 | break; |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 415 | case BrowserThread::WEBKIT_DEPRECATED: |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 416 | // Special case as WebKitThread is a separate |
| 417 | // type. |thread_to_start| is not used in this case. |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 418 | break; |
[email protected] | 31dbf9d | 2011-12-07 01:25:30 | [diff] [blame] | 419 | case BrowserThread::FILE_USER_BLOCKING: |
| 420 | thread_to_start = &file_user_blocking_thread_; |
| 421 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 422 | case BrowserThread::FILE: |
| 423 | thread_to_start = &file_thread_; |
| 424 | #if defined(OS_WIN) |
| 425 | // On Windows, the FILE thread needs to be have a UI message loop |
| 426 | // which pumps messages in such a way that Google Update can |
| 427 | // communicate back to us. |
| 428 | options = &ui_message_loop_options; |
| 429 | #else |
| 430 | options = &io_message_loop_options; |
| 431 | #endif |
| 432 | break; |
| 433 | case BrowserThread::PROCESS_LAUNCHER: |
| 434 | thread_to_start = &process_launcher_thread_; |
| 435 | break; |
| 436 | case BrowserThread::CACHE: |
| 437 | thread_to_start = &cache_thread_; |
| 438 | options = &io_message_loop_options; |
| 439 | break; |
| 440 | case BrowserThread::IO: |
| 441 | thread_to_start = &io_thread_; |
| 442 | options = &io_message_loop_options; |
| 443 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 444 | case BrowserThread::UI: |
| 445 | case BrowserThread::ID_COUNT: |
| 446 | default: |
| 447 | NOTREACHED(); |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); |
| 452 | |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 453 | if (thread_id == BrowserThread::WEBKIT_DEPRECATED) { |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 454 | #if !defined(OS_IOS) |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 455 | webkit_thread_.reset(new WebKitThread); |
| 456 | webkit_thread_->Initialize(); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 457 | #endif |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 458 | } else if (thread_to_start) { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 459 | (*thread_to_start).reset(new BrowserProcessSubThread(id)); |
| 460 | (*thread_to_start)->StartWithOptions(*options); |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 461 | } else { |
| 462 | NOTREACHED(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 463 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 466 | BrowserThreadsStarted(); |
| 467 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 468 | if (parts_.get()) |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 469 | parts_->PreMainMessageLoopRun(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 470 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 471 | // If the UI thread blocks, the whole UI is unresponsive. |
| 472 | // Do not allow disk IO from the UI thread. |
| 473 | base::ThreadRestrictions::SetIOAllowed(false); |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 474 | base::ThreadRestrictions::DisallowWaiting(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void BrowserMainLoop::RunMainMessageLoopParts() { |
| 478 | TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 479 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 480 | bool ran_main_loop = false; |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 481 | if (parts_.get()) |
| 482 | ran_main_loop = parts_->MainMessageLoopRun(&result_code_); |
| 483 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 484 | if (!ran_main_loop) |
| 485 | MainMessageLoopRun(); |
| 486 | |
| 487 | TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void BrowserMainLoop::ShutdownThreadsAndCleanUp() { |
| 491 | // Teardown may start in PostMainMessageLoopRun, and during teardown we |
| 492 | // need to be able to perform IO. |
| 493 | base::ThreadRestrictions::SetIOAllowed(true); |
| 494 | BrowserThread::PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 495 | BrowserThread::IO, FROM_HERE, |
[email protected] | 71cb8aa | 2011-12-29 19:14:00 | [diff] [blame] | 496 | base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
| 497 | true)); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 498 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 499 | if (parts_.get()) |
| 500 | parts_->PostMainMessageLoopRun(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 501 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 502 | #if !defined(OS_IOS) |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 503 | // Destroying the GpuProcessHostUIShims on the UI thread posts a task to |
| 504 | // delete related objects on the GPU thread. This must be done before |
| 505 | // stopping the GPU thread. The GPU thread will close IPC channels to renderer |
| 506 | // processes so this has to happen before stopping the IO thread. |
| 507 | GpuProcessHostUIShim::DestroyAll(); |
| 508 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 509 | // Cancel pending requests and prevent new requests. |
| 510 | if (resource_dispatcher_host_.get()) |
| 511 | resource_dispatcher_host_.get()->Shutdown(); |
| 512 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 513 | #if defined(USE_AURA) |
| 514 | ImageTransportFactory::Terminate(); |
| 515 | #endif |
[email protected] | 5b040e59 | 2012-02-10 02:56:10 | [diff] [blame] | 516 | BrowserGpuChannelHostFactory::Terminate(); |
| 517 | |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 518 | // The device monitors are using |system_monitor_| as dependency, so delete |
| 519 | // them before |system_monitor_| goes away. |
| 520 | // On Mac and windows, the monitor needs to be destroyed on the same thread |
| 521 | // as they were created. On Linux, the monitor will be deleted when IO thread |
| 522 | // goes away. |
| 523 | #if defined(OS_WIN) |
| 524 | system_message_window_.reset(); |
| 525 | #elif defined(OS_MACOSX) |
| 526 | device_monitor_mac_.reset(); |
| 527 | #endif |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 528 | #endif // !defined(OS_IOS) |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 529 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 530 | // Must be size_t so we can subtract from it. |
| 531 | for (size_t thread_id = BrowserThread::ID_COUNT - 1; |
| 532 | thread_id >= (BrowserThread::UI + 1); |
| 533 | --thread_id) { |
| 534 | // Find the thread object we want to stop. Looping over all valid |
| 535 | // BrowserThread IDs and DCHECKing on a missing case in the switch |
| 536 | // statement helps avoid a mismatch between this code and the |
| 537 | // BrowserThread::ID enumeration. |
| 538 | // |
| 539 | // The destruction order is the reverse order of occurrence in the |
| 540 | // BrowserThread::ID list. The rationale for the order is as |
| 541 | // follows (need to be filled in a bit): |
| 542 | // |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 543 | // |
| 544 | // - The IO thread is the only user of the CACHE thread. |
| 545 | // |
| 546 | // - The PROCESS_LAUNCHER thread must be stopped after IO in case |
| 547 | // the IO thread posted a task to terminate a process on the |
| 548 | // process launcher thread. |
| 549 | // |
| 550 | // - (Not sure why FILE needs to stop before WEBKIT.) |
| 551 | // |
| 552 | // - The WEBKIT thread (which currently is the responsibility of |
| 553 | // the embedder to stop, by destroying ResourceDispatcherHost |
| 554 | // before the DB thread is stopped) |
| 555 | // |
| 556 | // - (Not sure why DB stops last.) |
| 557 | scoped_ptr<BrowserProcessSubThread>* thread_to_stop = NULL; |
| 558 | switch (thread_id) { |
| 559 | case BrowserThread::DB: |
| 560 | thread_to_stop = &db_thread_; |
| 561 | break; |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 562 | case BrowserThread::WEBKIT_DEPRECATED: |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 563 | // Special case as WebKitThread is a separate |
| 564 | // type. |thread_to_stop| is not used in this case. |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 565 | |
| 566 | // Need to destroy ResourceDispatcherHost before PluginService |
| 567 | // and since it caches a pointer to it. |
| 568 | resource_dispatcher_host_.reset(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 569 | break; |
[email protected] | 31dbf9d | 2011-12-07 01:25:30 | [diff] [blame] | 570 | case BrowserThread::FILE_USER_BLOCKING: |
| 571 | thread_to_stop = &file_user_blocking_thread_; |
| 572 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 573 | case BrowserThread::FILE: |
| 574 | thread_to_stop = &file_thread_; |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 575 | |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 576 | #if !defined(OS_IOS) |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 577 | // Clean up state that lives on or uses the file_thread_ before |
| 578 | // it goes away. |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 579 | if (resource_dispatcher_host_.get()) |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 580 | resource_dispatcher_host_.get()->save_file_manager()->Shutdown(); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 581 | #endif // !defined(OS_IOS) |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 582 | break; |
| 583 | case BrowserThread::PROCESS_LAUNCHER: |
| 584 | thread_to_stop = &process_launcher_thread_; |
| 585 | break; |
| 586 | case BrowserThread::CACHE: |
| 587 | thread_to_stop = &cache_thread_; |
| 588 | break; |
| 589 | case BrowserThread::IO: |
| 590 | thread_to_stop = &io_thread_; |
| 591 | break; |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 592 | case BrowserThread::UI: |
| 593 | case BrowserThread::ID_COUNT: |
| 594 | default: |
| 595 | NOTREACHED(); |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); |
| 600 | |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 601 | if (id == BrowserThread::WEBKIT_DEPRECATED) { |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 602 | #if !defined(OS_IOS) |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 603 | webkit_thread_.reset(); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 604 | #endif |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 605 | } else if (thread_to_stop) { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 606 | thread_to_stop->reset(); |
[email protected] | c2b77b239 | 2011-12-02 13:00:16 | [diff] [blame] | 607 | } else { |
| 608 | NOTREACHED(); |
| 609 | } |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 612 | // Close the blocking I/O pool after the other threads. Other threads such |
| 613 | // as the I/O thread may need to schedule work like closing files or flushing |
| 614 | // data during shutdown, so the blocking pool needs to be available. There |
| 615 | // may also be slow operations pending that will blcok shutdown, so closing |
| 616 | // it here (which will block until required operations are complete) gives |
| 617 | // more head start for those operations to finish. |
| 618 | BrowserThreadImpl::ShutdownThreadPool(); |
| 619 | |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 620 | #if !defined(OS_IOS) |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 621 | // Must happen after the I/O thread is shutdown since this class lives on the |
| 622 | // I/O thread and isn't threadsafe. |
| 623 | GamepadService::GetInstance()->Terminate(); |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 624 | #endif // !defined(OS_IOS) |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 625 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 626 | if (parts_.get()) |
| 627 | parts_->PostDestroyThreads(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void BrowserMainLoop::InitializeMainThread() { |
| 631 | const char* kThreadName = "CrBrowserMain"; |
| 632 | base::PlatformThread::SetName(kThreadName); |
[email protected] | de88c5e | 2012-04-10 23:35:23 | [diff] [blame] | 633 | if (main_message_loop_.get()) |
| 634 | main_message_loop_->set_thread_name(kThreadName); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 635 | |
| 636 | // Register the main thread by instantiating it, but don't call any methods. |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 637 | main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, |
| 638 | MessageLoop::current())); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 639 | } |
| 640 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 641 | |
| 642 | void BrowserMainLoop::BrowserThreadsStarted() { |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 643 | #if !defined(OS_IOS) |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 644 | HistogramSynchronizer::GetInstance(); |
| 645 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 646 | BrowserGpuChannelHostFactory::Initialize(); |
[email protected] | c6ff6a3 | 2012-07-17 19:01:01 | [diff] [blame] | 647 | #if defined(USE_AURA) |
| 648 | ImageTransportFactory::Initialize(); |
| 649 | #endif |
| 650 | |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 651 | #if defined(OS_LINUX) |
| 652 | device_monitor_linux_.reset(new DeviceMonitorLinux()); |
[email protected] | 1462544 | 2012-08-15 12:51:42 | [diff] [blame] | 653 | #elif defined(OS_MACOSX) |
| 654 | device_monitor_mac_.reset(new DeviceMonitorMac()); |
[email protected] | 97646c9 | 2012-07-31 20:30:08 | [diff] [blame] | 655 | #endif |
| 656 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 657 | // RDH needs the IO thread to be created. |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 658 | resource_dispatcher_host_.reset(new ResourceDispatcherHostImpl()); |
[email protected] | b14b873b | 2012-05-03 03:56:09 | [diff] [blame] | 659 | |
[email protected] | 89c6eaf | 2012-11-26 18:52:27 | [diff] [blame] | 660 | // MediaStreamManager needs the IO thread to be created. |
| 661 | media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 662 | |
[email protected] | 7e34315 | 2012-09-20 21:49:53 | [diff] [blame] | 663 | // Initialize the GpuDataManager before we set up the MessageLoops because |
[email protected] | b14b873b | 2012-05-03 03:56:09 | [diff] [blame] | 664 | // otherwise we'll trigger the assertion about doing IO on the UI thread. |
[email protected] | 7e34315 | 2012-09-20 21:49:53 | [diff] [blame] | 665 | GpuDataManagerImpl::GetInstance()->Initialize(); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 666 | #endif // !OS_IOS |
| 667 | |
| 668 | #if defined(ENABLE_INPUT_SPEECH) |
[email protected] | fcb8e021 | 2012-10-29 11:57:18 | [diff] [blame] | 669 | speech_recognition_manager_.reset(new SpeechRecognitionManagerImpl()); |
[email protected] | 759a86a | 2012-09-10 14:42:54 | [diff] [blame] | 670 | #endif |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 671 | |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 672 | #if !defined(OS_IOS) |
[email protected] | 2dd3355 | 2012-09-11 16:39:55 | [diff] [blame] | 673 | // Alert the clipboard class to which threads are allowed to access the |
| 674 | // clipboard: |
| 675 | std::vector<base::PlatformThreadId> allowed_clipboard_threads; |
| 676 | // The current thread is the UI thread. |
| 677 | allowed_clipboard_threads.push_back(base::PlatformThread::CurrentId()); |
| 678 | #if defined(OS_WIN) |
| 679 | // On Windows, clipboards are also used on the File or IO threads. |
| 680 | allowed_clipboard_threads.push_back(file_thread_->thread_id()); |
| 681 | allowed_clipboard_threads.push_back(io_thread_->thread_id()); |
| 682 | #endif |
| 683 | ui::Clipboard::SetAllowedThreads(allowed_clipboard_threads); |
[email protected] | 3220d1b | 2012-12-08 04:13:35 | [diff] [blame] | 684 | |
| 685 | // When running the GPU thread in-process, avoid optimistically starting it |
| 686 | // since creating the GPU thread races against creation of the one-and-only |
| 687 | // ChildProcess instance which is created by the renderer thread. |
| 688 | if (GpuDataManagerImpl::GetInstance()->GpuAccessAllowed() && |
| 689 | content::IsForceCompositingModeEnabled() && |
| 690 | !parsed_command_line_.HasSwitch(switches::kDisableGpuProcessPrelaunch) && |
| 691 | !parsed_command_line_.HasSwitch(switches::kSingleProcess) && |
| 692 | !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) { |
| 693 | TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process"); |
| 694 | BrowserThread::PostTask( |
| 695 | BrowserThread::IO, FROM_HERE, base::Bind( |
| 696 | base::IgnoreResult(&GpuProcessHost::Get), |
| 697 | GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, |
| 698 | CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP)); |
| 699 | } |
[email protected] | a34087bd | 2012-10-11 17:05:30 | [diff] [blame] | 700 | #endif // !defined(OS_IOS) |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 703 | void BrowserMainLoop::InitializeToolkit() { |
| 704 | // TODO(evan): this function is rather subtle, due to the variety |
| 705 | // of intersecting ifdefs we have. To keep it easy to follow, there |
| 706 | // are no #else branches on any #ifs. |
| 707 | // TODO(stevenjb): Move platform specific code into platform specific Parts |
| 708 | // (Need to add InitializeToolkit stage to BrowserParts). |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 709 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 710 | // Glib type system initialization. Needed at least for gconf, |
| 711 | // used in net/proxy/proxy_config_service_linux.cc. Most likely |
| 712 | // this is superfluous as gtk_init() ought to do this. It's |
| 713 | // definitely harmless, so retained as a reminder of this |
| 714 | // requirement for gconf. |
| 715 | g_type_init(); |
| 716 | |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 717 | #if !defined(USE_AURA) |
[email protected] | 38d0b2d4 | 2012-01-18 03:37:34 | [diff] [blame] | 718 | gfx::GtkInitFromCommandLine(parsed_command_line_); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 719 | #endif |
| 720 | |
| 721 | SetUpGLibLogHandler(); |
| 722 | #endif |
| 723 | |
| 724 | #if defined(TOOLKIT_GTK) |
| 725 | // It is important for this to happen before the first run dialog, as it |
| 726 | // styles the dialog as well. |
| 727 | gfx::InitRCStyles(); |
| 728 | #endif |
| 729 | |
| 730 | #if defined(OS_WIN) |
| 731 | // Init common control sex. |
| 732 | INITCOMMONCONTROLSEX config; |
| 733 | config.dwSize = sizeof(config); |
| 734 | config.dwICC = ICC_WIN95_CLASSES; |
| 735 | if (!InitCommonControlsEx(&config)) |
| 736 | LOG_GETLASTERROR(FATAL); |
| 737 | #endif |
| 738 | |
[email protected] | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 739 | if (parts_.get()) |
| 740 | parts_->ToolkitInitialized(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void BrowserMainLoop::MainMessageLoopRun() { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 744 | #if defined(OS_ANDROID) |
[email protected] | a08029b4 | 2012-04-25 03:18:46 | [diff] [blame] | 745 | // Android's main message loop is the Java message loop. |
| 746 | NOTREACHED(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 747 | #else |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 748 | DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); |
| 749 | if (parameters_.ui_task) |
| 750 | MessageLoopForUI::current()->PostTask(FROM_HERE, *parameters_.ui_task); |
| 751 | |
| 752 | base::RunLoop run_loop; |
| 753 | run_loop.Run(); |
[email protected] | b48c918 | 2011-10-26 18:03:30 | [diff] [blame] | 754 | #endif |
| 755 | } |
| 756 | |
| 757 | } // namespace content |