[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/browser_process_impl.h" |
| 6 | |
[email protected] | 90f3990 | 2009-10-03 04:25:37 | [diff] [blame] | 7 | #include "app/clipboard/clipboard.h" |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 8 | #include "app/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 10 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 12 | #include "base/thread.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 13 | #include "base/waitable_event.h" |
[email protected] | 0082d7e0d | 2010-02-27 14:34:12 | [diff] [blame] | 14 | #include "chrome/browser/appcache/chrome_appcache_service.h" |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 15 | #include "chrome/browser/browser_main.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 16 | #include "chrome/browser/browser_process_sub_thread.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 17 | #include "chrome/browser/browser_trial.h" |
[email protected] | dcddcdb0 | 2010-02-23 09:10:13 | [diff] [blame] | 18 | #include "chrome/browser/child_process_host.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4ab4b0f | 2009-02-10 18:54:50 | [diff] [blame] | 20 | #include "chrome/browser/debugger/debugger_wrapper.h" |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 21 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 22 | #include "chrome/browser/download/download_file.h" |
[email protected] | 5ba0a2c | 2009-02-19 01:19:34 | [diff] [blame] | 23 | #include "chrome/browser/download/save_file_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/browser/google_url_tracker.h" |
[email protected] | dcefa30 | 2009-05-20 00:24:39 | [diff] [blame] | 25 | #include "chrome/browser/icon_manager.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 26 | #include "chrome/browser/in_process_webkit/dom_storage_context.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 27 | #include "chrome/browser/intranet_redirect_detector.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 28 | #include "chrome/browser/io_thread.h" |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 29 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 30 | #include "chrome/browser/net/dns_global.h" |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 31 | #include "chrome/browser/net/sdch_dictionary_fetcher.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 32 | #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
[email protected] | 29672ab | 2009-10-30 03:44:03 | [diff] [blame] | 33 | #include "chrome/browser/notifications/notification_ui_manager.h" |
[email protected] | fd49e2d | 2009-02-20 17:21:30 | [diff] [blame] | 34 | #include "chrome/browser/plugin_service.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 35 | #include "chrome/browser/pref_service.h" |
[email protected] | e06131d | 2010-02-10 18:40:33 | [diff] [blame] | 36 | #include "chrome/browser/printing/print_job_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 38 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 81218f4 | 2009-02-05 18:48:08 | [diff] [blame] | 39 | #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 40 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 41 | #include "chrome/browser/status_icons/status_tray_manager.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 42 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | #include "chrome/common/chrome_paths.h" |
| 44 | #include "chrome/common/chrome_switches.h" |
[email protected] | f1b6de2 | 2010-03-06 12:13:47 | [diff] [blame] | 45 | #include "chrome/common/extensions/extension_l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | #include "chrome/common/notification_service.h" |
| 47 | #include "chrome/common/pref_names.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 48 | #include "chrome/common/url_constants.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 49 | #include "ipc/ipc_logging.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 50 | #include "webkit/database/database_tracker.h" |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 51 | |
| 52 | #if defined(OS_WIN) |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 53 | #include "views/focus/view_storage.h" |
[email protected] | 86230b9 | 2009-11-23 20:38:38 | [diff] [blame] | 54 | #endif |
| 55 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 56 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 57 | #include "chrome/common/plugin_messages.h" |
| 58 | #include "chrome/common/render_messages.h" |
| 59 | #endif |
| 60 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 61 | BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | : created_resource_dispatcher_host_(false), |
| 63 | created_metrics_service_(false), |
| 64 | created_io_thread_(false), |
| 65 | created_file_thread_(false), |
| 66 | created_db_thread_(false), |
[email protected] | 91451171 | 2009-11-23 19:42:33 | [diff] [blame] | 67 | created_process_launcher_thread_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | created_profile_manager_(false), |
| 69 | created_local_state_(false), |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 70 | created_icon_manager_(false), |
| 71 | created_debugger_wrapper_(false), |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 72 | created_devtools_manager_(false), |
[email protected] | 29672ab | 2009-10-30 03:44:03 | [diff] [blame] | 73 | created_notification_ui_manager_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | module_ref_count_(0), |
[email protected] | 1b2db1a | 2008-08-08 17:46:13 | [diff] [blame] | 75 | checked_for_new_frames_(false), |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 76 | using_new_frames_(false), |
| 77 | have_inspector_files_(true) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | g_browser_process = this; |
[email protected] | 1b8d02f1 | 2009-05-05 04:14:11 | [diff] [blame] | 79 | clipboard_.reset(new Clipboard); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | main_notification_service_.reset(new NotificationService); |
| 81 | |
| 82 | // Must be created after the NotificationService. |
| 83 | print_job_manager_.reset(new printing::PrintJobManager); |
| 84 | |
[email protected] | b797e15 | 2009-01-23 16:06:14 | [diff] [blame] | 85 | shutdown_event_.reset(new base::WaitableEvent(true, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | BrowserProcessImpl::~BrowserProcessImpl() { |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 89 | FilePath profile_path; |
| 90 | bool clear_local_state_on_exit; |
| 91 | |
| 92 | // Store the profile path for clearing local state data on exit. |
| 93 | clear_local_state_on_exit = ShouldClearLocalState(&profile_path); |
| 94 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | // Delete the AutomationProviderList before NotificationService, |
| 96 | // since it may try to unregister notifications |
| 97 | // Both NotificationService and AutomationProvider are singleton instances in |
| 98 | // the BrowserProcess. Since AutomationProvider may have some active |
| 99 | // notification observers, it is essential that it gets destroyed before the |
| 100 | // NotificationService. NotificationService won't be destroyed until after |
| 101 | // this destructor is run. |
| 102 | automation_provider_list_.reset(); |
| 103 | |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 104 | // We need to shutdown the SdchDictionaryFetcher as it regularly holds |
| 105 | // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do |
| 106 | // a PostDelayedTask onto the IO thread. This shutdown call will both discard |
| 107 | // any pending URLFetchers, and avoid creating any more. |
| 108 | SdchDictionaryFetcher::Shutdown(); |
| 109 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 110 | // We need to destroy the MetricsService, GoogleURLTracker, and |
| 111 | // IntranetRedirectDetector before the io_thread_ gets destroyed, since their |
| 112 | // destructors can call the URLFetcher destructor, which does a |
| 113 | // PostDelayedTask operation on the IO thread. (The IO thread will handle |
| 114 | // that URLFetcher operation before going away.) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | metrics_service_.reset(); |
| 116 | google_url_tracker_.reset(); |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 117 | intranet_redirect_detector_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | |
| 119 | // Need to clear profiles (download managers) before the io_thread_. |
| 120 | profile_manager_.reset(); |
| 121 | |
| 122 | // Debugger must be cleaned up before IO thread and NotificationService. |
| 123 | debugger_wrapper_ = NULL; |
| 124 | |
| 125 | if (resource_dispatcher_host_.get()) { |
| 126 | // Need to tell Safe Browsing Service that the IO thread is going away |
| 127 | // since it cached a pointer to it. |
| 128 | if (resource_dispatcher_host()->safe_browsing_service()) |
| 129 | resource_dispatcher_host()->safe_browsing_service()->ShutDown(); |
| 130 | |
| 131 | // Cancel pending requests and prevent new requests. |
| 132 | resource_dispatcher_host()->Shutdown(); |
| 133 | } |
| 134 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 135 | #if defined(USE_X11) |
[email protected] | 4c3cd741 | 2009-04-22 17:56:06 | [diff] [blame] | 136 | // The IO thread must outlive the BACKGROUND_X11 thread. |
| 137 | background_x11_thread_.reset(); |
| 138 | #endif |
| 139 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | // Need to stop io_thread_ before resource_dispatcher_host_, since |
| 141 | // io_thread_ may still deref ResourceDispatcherHost and handle resource |
| 142 | // request before going away. |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 143 | io_thread_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
[email protected] | 91451171 | 2009-11-23 19:42:33 | [diff] [blame] | 145 | // Stop the process launcher thread after the IO thread, in case the IO thread |
| 146 | // posted a task to terminate a process on the process launcher thread. |
| 147 | process_launcher_thread_.reset(); |
| 148 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | // Clean up state that lives on the file_thread_ before it goes away. |
| 150 | if (resource_dispatcher_host_.get()) { |
| 151 | resource_dispatcher_host()->download_file_manager()->Shutdown(); |
| 152 | resource_dispatcher_host()->save_file_manager()->Shutdown(); |
| 153 | } |
| 154 | |
| 155 | // Need to stop the file_thread_ here to force it to process messages in its |
| 156 | // message loop from the previous call to shutdown the DownloadFileManager, |
| 157 | // SaveFileManager and SessionService. |
| 158 | file_thread_.reset(); |
| 159 | |
| 160 | // With the file_thread_ flushed, we can release any icon resources. |
| 161 | icon_manager_.reset(); |
| 162 | |
| 163 | // Need to destroy ResourceDispatcherHost before PluginService and |
[email protected] | 49f28bc7 | 2010-02-04 00:10:01 | [diff] [blame] | 164 | // SafeBrowsingService, since it caches a pointer to it. This also |
| 165 | // causes the webkit thread to terminate. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | resource_dispatcher_host_.reset(); |
| 167 | |
| 168 | // Wait for the pending print jobs to finish. |
| 169 | print_job_manager_->OnQuit(); |
| 170 | print_job_manager_.reset(); |
| 171 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | // Now OK to destroy NotificationService. |
| 173 | main_notification_service_.reset(); |
| 174 | |
[email protected] | 49f28bc7 | 2010-02-04 00:10:01 | [diff] [blame] | 175 | // Prior to clearing local state, we want to complete tasks pending |
| 176 | // on the db thread too. |
| 177 | db_thread_.reset(); |
| 178 | |
| 179 | // At this point, no render process exist and the file, io, db, and |
| 180 | // webkit threads in this process have all terminated, so it's safe |
| 181 | // to access local state data such as cookies, database, or local storage. |
| 182 | if (clear_local_state_on_exit) |
| 183 | ClearLocalState(profile_path); |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | g_browser_process = NULL; |
| 186 | } |
| 187 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 188 | // Send a QuitTask to the given MessageLoop. |
| 189 | static void PostQuit(MessageLoop* message_loop) { |
| 190 | message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 191 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 193 | unsigned int BrowserProcessImpl::AddRefModule() { |
| 194 | DCHECK(CalledOnValidThread()); |
| 195 | module_ref_count_++; |
| 196 | return module_ref_count_; |
| 197 | } |
| 198 | |
| 199 | unsigned int BrowserProcessImpl::ReleaseModule() { |
| 200 | DCHECK(CalledOnValidThread()); |
| 201 | DCHECK(0 != module_ref_count_); |
| 202 | module_ref_count_--; |
| 203 | if (0 == module_ref_count_) { |
| 204 | MessageLoop::current()->PostTask( |
[email protected] | 34f73fb | 2010-03-24 20:50:34 | [diff] [blame^] | 205 | FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop)); |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 206 | MessageLoop::current()->Quit(); |
| 207 | } |
| 208 | return module_ref_count_; |
| 209 | } |
| 210 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | void BrowserProcessImpl::EndSession() { |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 212 | #if defined(OS_WIN) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 213 | // Notify we are going away. |
[email protected] | b797e15 | 2009-01-23 16:06:14 | [diff] [blame] | 214 | ::SetEvent(shutdown_event_->handle()); |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 215 | #endif |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 216 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | // Mark all the profiles as clean. |
| 218 | ProfileManager* pm = profile_manager(); |
| 219 | for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i) |
| 220 | (*i)->MarkAsCleanShutdown(); |
| 221 | |
| 222 | // Tell the metrics service it was cleanly shutdown. |
| 223 | MetricsService* metrics = g_browser_process->metrics_service(); |
| 224 | if (metrics && local_state()) { |
| 225 | metrics->RecordCleanShutdown(); |
| 226 | |
| 227 | metrics->RecordStartOfSessionEnd(); |
| 228 | |
| 229 | // MetricsService lazily writes to prefs, force it to write now. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 230 | local_state()->SavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // We must write that the profile and metrics service shutdown cleanly, |
| 234 | // otherwise on startup we'll think we crashed. So we block until done and |
| 235 | // then proceed with normal shutdown. |
| 236 | g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE, |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 237 | NewRunnableFunction(PostQuit, MessageLoop::current())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | MessageLoop::current()->Run(); |
| 239 | } |
| 240 | |
| 241 | printing::PrintJobManager* BrowserProcessImpl::print_job_manager() { |
| 242 | // TODO(abarth): DCHECK(CalledOnValidThread()); |
| 243 | // See <http://b/1287209>. |
| 244 | // print_job_manager_ is initialized in the constructor and destroyed in the |
| 245 | // destructor, so it should always be valid. |
| 246 | DCHECK(print_job_manager_.get()); |
| 247 | return print_job_manager_.get(); |
| 248 | } |
| 249 | |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 250 | void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) { |
| 251 | SQLitePersistentCookieStore::ClearLocalState(profile_path.Append( |
| 252 | chrome::kCookieFilename)); |
| 253 | DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme); |
[email protected] | 72cfd90f | 2010-02-06 03:08:04 | [diff] [blame] | 254 | webkit_database::DatabaseTracker::ClearLocalState(profile_path); |
[email protected] | 10c93405 | 2010-02-04 19:58:31 | [diff] [blame] | 255 | ChromeAppCacheService::ClearLocalState(profile_path); |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) { |
| 259 | FilePath user_data_dir; |
| 260 | Profile* profile; |
| 261 | |
[email protected] | ed0cf56 | 2010-02-05 22:50:41 | [diff] [blame] | 262 | // Check for the existance of a profile manager. When quitting early, |
| 263 | // e.g. because another chrome instance is running, or when invoked with |
| 264 | // options such as --uninstall or --try-chrome-again=0, the profile manager |
| 265 | // does not exist yet. |
| 266 | if (!profile_manager_.get()) |
| 267 | return false; |
| 268 | |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 269 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 270 | profile = profile_manager_->GetDefaultProfile(user_data_dir); |
[email protected] | 7bfc67432 | 2010-03-05 00:20:57 | [diff] [blame] | 271 | if (!profile) |
| 272 | return false; |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 273 | *profile_path = profile->GetPath(); |
| 274 | return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit); |
| 275 | } |
| 276 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | void BrowserProcessImpl::CreateResourceDispatcherHost() { |
| 278 | DCHECK(!created_resource_dispatcher_host_ && |
| 279 | resource_dispatcher_host_.get() == NULL); |
| 280 | created_resource_dispatcher_host_ = true; |
| 281 | |
[email protected] | 79084c2d | 2009-11-03 23:12:42 | [diff] [blame] | 282 | resource_dispatcher_host_.reset(new ResourceDispatcherHost()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | resource_dispatcher_host_->Initialize(); |
| 284 | } |
| 285 | |
| 286 | void BrowserProcessImpl::CreateMetricsService() { |
| 287 | DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL); |
| 288 | created_metrics_service_ = true; |
| 289 | |
| 290 | metrics_service_.reset(new MetricsService); |
| 291 | } |
| 292 | |
| 293 | void BrowserProcessImpl::CreateIOThread() { |
| 294 | DCHECK(!created_io_thread_ && io_thread_.get() == NULL); |
| 295 | created_io_thread_ = true; |
| 296 | |
| 297 | // Prior to starting the io thread, we create the plugin service as |
| 298 | // it is predominantly used from the io thread, but must be created |
| 299 | // on the main thread. The service ctor is inexpensive and does not |
| 300 | // invoke the io_thread() accessor. |
| 301 | PluginService::GetInstance(); |
| 302 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 303 | #if defined(USE_X11) |
[email protected] | 4c3cd741 | 2009-04-22 17:56:06 | [diff] [blame] | 304 | // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so |
| 305 | // we start it now. |
| 306 | scoped_ptr<base::Thread> background_x11_thread( |
| 307 | new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11)); |
| 308 | if (!background_x11_thread->Start()) |
| 309 | return; |
| 310 | background_x11_thread_.swap(background_x11_thread); |
| 311 | #endif |
| 312 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 313 | scoped_ptr<IOThread> thread(new IOThread); |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 314 | base::Thread::Options options; |
| 315 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 316 | if (!thread->StartWithOptions(options)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | return; |
| 318 | io_thread_.swap(thread); |
| 319 | } |
| 320 | |
| 321 | void BrowserProcessImpl::CreateFileThread() { |
| 322 | DCHECK(!created_file_thread_ && file_thread_.get() == NULL); |
| 323 | created_file_thread_ = true; |
| 324 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 325 | scoped_ptr<base::Thread> thread( |
| 326 | new BrowserProcessSubThread(ChromeThread::FILE)); |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 327 | base::Thread::Options options; |
[email protected] | 9e549b58 | 2009-02-05 21:13:39 | [diff] [blame] | 328 | #if defined(OS_WIN) |
| 329 | // On Windows, the FILE thread needs to be have a UI message loop which pumps |
| 330 | // messages in such a way that Google Update can communicate back to us. |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 331 | options.message_loop_type = MessageLoop::TYPE_UI; |
[email protected] | 9e549b58 | 2009-02-05 21:13:39 | [diff] [blame] | 332 | #else |
| 333 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 334 | #endif |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 335 | if (!thread->StartWithOptions(options)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | return; |
| 337 | file_thread_.swap(thread); |
| 338 | } |
| 339 | |
| 340 | void BrowserProcessImpl::CreateDBThread() { |
| 341 | DCHECK(!created_db_thread_ && db_thread_.get() == NULL); |
| 342 | created_db_thread_ = true; |
| 343 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 344 | scoped_ptr<base::Thread> thread( |
| 345 | new BrowserProcessSubThread(ChromeThread::DB)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | if (!thread->Start()) |
| 347 | return; |
| 348 | db_thread_.swap(thread); |
| 349 | } |
| 350 | |
[email protected] | 91451171 | 2009-11-23 19:42:33 | [diff] [blame] | 351 | void BrowserProcessImpl::CreateProcessLauncherThread() { |
| 352 | DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get()); |
| 353 | created_process_launcher_thread_ = true; |
| 354 | |
| 355 | scoped_ptr<base::Thread> thread( |
| 356 | new BrowserProcessSubThread(ChromeThread::PROCESS_LAUNCHER)); |
| 357 | if (!thread->Start()) |
| 358 | return; |
| 359 | process_launcher_thread_.swap(thread); |
| 360 | } |
| 361 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | void BrowserProcessImpl::CreateProfileManager() { |
| 363 | DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL); |
| 364 | created_profile_manager_ = true; |
| 365 | |
| 366 | profile_manager_.reset(new ProfileManager()); |
| 367 | } |
| 368 | |
| 369 | void BrowserProcessImpl::CreateLocalState() { |
| 370 | DCHECK(!created_local_state_ && local_state_.get() == NULL); |
| 371 | created_local_state_ = true; |
| 372 | |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 373 | FilePath local_state_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 375 | local_state_.reset(new PrefService(local_state_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | } |
| 377 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | void BrowserProcessImpl::CreateIconManager() { |
| 379 | DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL); |
| 380 | created_icon_manager_ = true; |
| 381 | icon_manager_.reset(new IconManager); |
| 382 | } |
| 383 | |
| 384 | void BrowserProcessImpl::CreateDebuggerWrapper(int port) { |
| 385 | DCHECK(debugger_wrapper_.get() == NULL); |
| 386 | created_debugger_wrapper_ = true; |
| 387 | |
| 388 | debugger_wrapper_ = new DebuggerWrapper(port); |
| 389 | } |
| 390 | |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 391 | void BrowserProcessImpl::CreateDevToolsManager() { |
[email protected] | 73ee0152 | 2009-06-05 10:13:44 | [diff] [blame] | 392 | DCHECK(devtools_manager_.get() == NULL); |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 393 | created_devtools_manager_ = true; |
[email protected] | 73ee0152 | 2009-06-05 10:13:44 | [diff] [blame] | 394 | devtools_manager_ = new DevToolsManager(); |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 395 | } |
| 396 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | void BrowserProcessImpl::CreateGoogleURLTracker() { |
| 398 | DCHECK(google_url_tracker_.get() == NULL); |
| 399 | scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker); |
| 400 | google_url_tracker_.swap(google_url_tracker); |
| 401 | } |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 402 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 403 | void BrowserProcessImpl::CreateIntranetRedirectDetector() { |
| 404 | DCHECK(intranet_redirect_detector_.get() == NULL); |
| 405 | scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector( |
| 406 | new IntranetRedirectDetector); |
| 407 | intranet_redirect_detector_.swap(intranet_redirect_detector); |
| 408 | } |
| 409 | |
[email protected] | 29672ab | 2009-10-30 03:44:03 | [diff] [blame] | 410 | void BrowserProcessImpl::CreateNotificationUIManager() { |
| 411 | DCHECK(notification_ui_manager_.get() == NULL); |
| 412 | notification_ui_manager_.reset(NotificationUIManager::Create()); |
| 413 | created_notification_ui_manager_ = true; |
| 414 | } |
| 415 | |
[email protected] | f1b6de2 | 2010-03-06 12:13:47 | [diff] [blame] | 416 | void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) { |
| 417 | locale_ = locale; |
| 418 | extension_l10n_util::SetProcessLocale(locale); |
| 419 | } |
| 420 | |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 421 | void BrowserProcessImpl::CreateStatusTrayManager() { |
| 422 | DCHECK(status_tray_manager_.get() == NULL); |
| 423 | status_tray_manager_.reset(new StatusTrayManager()); |
| 424 | } |
| 425 | |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 426 | // The BrowserProcess object must outlive the file thread so we use traits |
| 427 | // which don't do any management. |
| 428 | template <> |
| 429 | struct RunnableMethodTraits<BrowserProcessImpl> { |
[email protected] | ee5e379 | 2009-10-13 23:23:47 | [diff] [blame] | 430 | void RetainCallee(BrowserProcessImpl* process) {} |
| 431 | void ReleaseCallee(BrowserProcessImpl* process) {} |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | void BrowserProcessImpl::CheckForInspectorFiles() { |
| 435 | file_thread()->message_loop()->PostTask |
| 436 | (FROM_HERE, |
| 437 | NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck)); |
| 438 | } |
| 439 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 440 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 441 | |
| 442 | void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) { |
| 443 | // First enable myself. |
| 444 | if (enable) |
| 445 | IPC::Logging::current()->Enable(); |
| 446 | else |
| 447 | IPC::Logging::current()->Disable(); |
| 448 | |
| 449 | // Now tell subprocesses. Messages to ChildProcess-derived |
| 450 | // processes must be done on the IO thread. |
| 451 | io_thread()->message_loop()->PostTask |
| 452 | (FROM_HERE, |
| 453 | NewRunnableMethod( |
| 454 | this, |
| 455 | &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses, |
| 456 | enable)); |
| 457 | |
| 458 | // Finally, tell the renderers which don't derive from ChildProcess. |
| 459 | // Messages to the renderers must be done on the UI (main) thread. |
[email protected] | 019191a | 2009-10-02 20:37:27 | [diff] [blame] | 460 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 461 | !i.IsAtEnd(); i.Advance()) |
| 462 | i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable)); |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Helper for SetIPCLoggingEnabled. |
| 466 | void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) { |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 467 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 468 | |
| 469 | ChildProcessHost::Iterator i; // default constr references a singleton |
[email protected] | 34cf97d | 2009-09-29 22:46:11 | [diff] [blame] | 470 | while (!i.Done()) { |
| 471 | i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled)); |
| 472 | ++i; |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | #endif // IPC_MESSAGE_LOG_ENABLED |
| 477 | |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 478 | void BrowserProcessImpl::DoInspectorFilesCheck() { |
| 479 | // Runs on FILE thread. |
| 480 | DCHECK(file_thread_->message_loop() == MessageLoop::current()); |
| 481 | bool result = false; |
| 482 | |
| 483 | FilePath inspector_dir; |
| 484 | if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| 485 | result = file_util::PathExists(inspector_dir); |
| 486 | } |
| 487 | |
[email protected] | 308080d | 2009-11-11 19:19:02 | [diff] [blame] | 488 | have_inspector_files_ = result; |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 489 | } |