[email protected] | 3a80ea33 | 2012-01-09 19:53:29 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 3cdacd4 | 2010-04-30 18:55:53 | [diff] [blame] | 7 | #include <map> |
[email protected] | 68f88b99 | 2011-05-07 02:01:39 | [diff] [blame] | 8 | #include <set> |
| 9 | #include <vector> |
[email protected] | 3cdacd4 | 2010-04-30 18:55:53 | [diff] [blame] | 10 | |
[email protected] | b3cc0544 | 2011-11-19 22:43:44 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 14 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 16 | #include "base/synchronization/waitable_event.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
| 18 | #include "base/threading/thread_restrictions.h" |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 19 | #include "chrome/browser/automation/automation_provider_list.h" |
[email protected] | a07676b2 | 2011-06-17 16:36:53 | [diff] [blame] | 20 | #include "chrome/browser/background/background_mode_manager.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 21 | #include "chrome/browser/browser_trial.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 22 | #include "chrome/browser/chrome_browser_main.h" |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 23 | #include "chrome/browser/chrome_plugin_service_filter.h" |
[email protected] | c1adf5a | 2011-08-03 22:11:37 | [diff] [blame] | 24 | #include "chrome/browser/component_updater/component_updater_configurator.h" |
| 25 | #include "chrome/browser/component_updater/component_updater_service.h" |
[email protected] | 4475d23 | 2011-07-27 15:29:20 | [diff] [blame] | 26 | #include "chrome/browser/debugger/remote_debugging_server.h" |
[email protected] | 254ed74 | 2011-08-16 18:45:27 | [diff] [blame] | 27 | #include "chrome/browser/download/download_request_limiter.h" |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 28 | #include "chrome/browser/download/download_status_updater.h" |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 29 | #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
[email protected] | fc4facd | 2011-03-22 23:18:50 | [diff] [blame] | 30 | #include "chrome/browser/extensions/extension_tab_id_map.h" |
[email protected] | b6cf240f | 2011-10-15 22:09:53 | [diff] [blame] | 31 | #include "chrome/browser/extensions/network_delay_listener.h" |
[email protected] | 21453884 | 2011-04-01 18:47:24 | [diff] [blame] | 32 | #include "chrome/browser/extensions/user_script_listener.h" |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 33 | #include "chrome/browser/first_run/upgrade_util.h" |
[email protected] | f7578f5 | 2010-08-30 22:22:49 | [diff] [blame] | 34 | #include "chrome/browser/google/google_url_tracker.h" |
[email protected] | dcefa30 | 2009-05-20 00:24:39 | [diff] [blame] | 35 | #include "chrome/browser/icon_manager.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 36 | #include "chrome/browser/intranet_redirect_detector.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 37 | #include "chrome/browser/io_thread.h" |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 38 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | 0b56518 | 2011-03-02 18:11:15 | [diff] [blame] | 39 | #include "chrome/browser/metrics/thread_watcher.h" |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 40 | #include "chrome/browser/net/chrome_net_log.h" |
[email protected] | ca167a3 | 2011-09-30 15:19:11 | [diff] [blame] | 41 | #include "chrome/browser/net/crl_set_fetcher.h" |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 42 | #include "chrome/browser/net/sdch_dictionary_fetcher.h" |
[email protected] | 29672ab | 2009-10-30 03:44:03 | [diff] [blame] | 43 | #include "chrome/browser/notifications/notification_ui_manager.h" |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 44 | #include "chrome/browser/policy/browser_policy_connector.h" |
[email protected] | caf63aea | 2011-04-26 11:04:10 | [diff] [blame] | 45 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 46 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 1459fb6 | 2011-05-25 19:03:27 | [diff] [blame] | 47 | #include "chrome/browser/prerender/prerender_tracker.h" |
[email protected] | 68f88b99 | 2011-05-07 02:01:39 | [diff] [blame] | 48 | #include "chrome/browser/printing/background_printing_manager.h" |
[email protected] | e06131d | 2010-02-10 18:40:33 | [diff] [blame] | 49 | #include "chrome/browser/printing/print_job_manager.h" |
[email protected] | dbeebd5 | 2010-11-16 20:34:16 | [diff] [blame] | 50 | #include "chrome/browser/printing/print_preview_tab_controller.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 51 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 8bcdf07 | 2011-06-03 16:52:15 | [diff] [blame] | 52 | #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h" |
[email protected] | 81218f4 | 2009-02-05 18:48:08 | [diff] [blame] | 53 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
[email protected] | e450fa6 | 2011-02-01 12:52:56 | [diff] [blame] | 54 | #include "chrome/browser/shell_integration.h" |
[email protected] | 9e7f015f | 2011-05-28 00:24:25 | [diff] [blame] | 55 | #include "chrome/browser/status_icons/status_tray.h" |
[email protected] | 0b4d338 | 2010-07-14 16:13:04 | [diff] [blame] | 56 | #include "chrome/browser/tab_closeable_state_watcher.h" |
[email protected] | 7a0e72f | 2011-09-20 19:59:11 | [diff] [blame] | 57 | #include "chrome/browser/tab_contents/thumbnail_generator.h" |
[email protected] | 71b73f0 | 2011-04-06 15:57:29 | [diff] [blame] | 58 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 4ef795df | 2010-02-03 02:35:08 | [diff] [blame] | 59 | #include "chrome/common/chrome_constants.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 60 | #include "chrome/common/chrome_notification_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | #include "chrome/common/chrome_paths.h" |
| 62 | #include "chrome/common/chrome_switches.h" |
[email protected] | f1b6de2 | 2010-03-06 12:13:47 | [diff] [blame] | 63 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 64 | #include "chrome/common/extensions/extension_resource.h" |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 65 | #include "chrome/common/json_pref_store.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | #include "chrome/common/pref_names.h" |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 67 | #include "chrome/common/switch_utils.h" |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 68 | #include "chrome/common/url_constants.h" |
[email protected] | bd48c2b0 | 2010-04-09 20:32:42 | [diff] [blame] | 69 | #include "chrome/installer/util/google_update_constants.h" |
[email protected] | 8f6a3b85 | 2011-07-19 16:48:56 | [diff] [blame] | 70 | #include "content/browser/download/mhtml_generation_manager.h" |
[email protected] | 6eac57a | 2011-07-12 21:15:09 | [diff] [blame] | 71 | #include "content/browser/net/browser_online_state_observer.h" |
[email protected] | a01efd2 | 2011-03-01 00:38:32 | [diff] [blame] | 72 | #include "content/browser/renderer_host/resource_dispatcher_host.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 73 | #include "content/public/browser/browser_thread.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame^] | 74 | #include "content/public/browser/child_process_security_policy.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 75 | #include "content/public/browser/notification_details.h" |
[email protected] | 3a5180ae | 2011-12-21 02:39:38 | [diff] [blame] | 76 | #include "content/public/browser/plugin_service.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 77 | #include "content/public/browser/render_process_host.h" |
[email protected] | 2e77cb3 | 2011-12-12 21:50:00 | [diff] [blame] | 78 | #include "media/audio/audio_manager.h" |
[email protected] | 12c84e2 | 2011-07-11 09:35:45 | [diff] [blame] | 79 | #include "net/socket/client_socket_pool_manager.h" |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 80 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 81 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 82 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 83 | |
| 84 | #if defined(OS_WIN) |
[email protected] | 477ae05 | 2011-11-18 23:53:57 | [diff] [blame] | 85 | #include "ui/views/focus/view_storage.h" |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 86 | #elif defined(OS_MACOSX) |
| 87 | #include "chrome/browser/chrome_browser_main_mac.h" |
[email protected] | 86230b9 | 2009-11-23 20:38:38 | [diff] [blame] | 88 | #endif |
| 89 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 90 | #if defined(OS_CHROMEOS) |
[email protected] | 5b86996 | 2011-09-20 19:49:33 | [diff] [blame] | 91 | #include "chrome/browser/oom_priority_manager.h" |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 92 | #endif // defined(OS_CHROMEOS) |
| 93 | |
[email protected] | 3cdacd4 | 2010-04-30 18:55:53 | [diff] [blame] | 94 | #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
| 95 | // How often to check if the persistent instance of Chrome needs to restart |
| 96 | // to install an update. |
| 97 | static const int kUpdateCheckIntervalHours = 6; |
| 98 | #endif |
| 99 | |
[email protected] | af391f0 | 2011-09-15 06:13:35 | [diff] [blame] | 100 | #if defined(OS_WIN) |
| 101 | // Attest to the fact that the call to the file thread to save preferences has |
| 102 | // run, and it is safe to terminate. This avoids the potential of some other |
| 103 | // task prematurely terminating our waiting message loop by posting a |
| 104 | // QuitTask(). |
| 105 | static bool g_end_session_file_thread_has_completed = false; |
| 106 | #endif |
| 107 | |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 108 | #if defined(USE_X11) |
| 109 | // How long to wait for the File thread to complete during EndSession, on |
| 110 | // Linux. We have a timeout here because we're unable to run the UI messageloop |
| 111 | // and there's some deadlock risk. Our only option is to exit anyway. |
| 112 | static const int kEndSessionTimeoutSeconds = 10; |
| 113 | #endif |
| 114 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 115 | using content::BrowserThread; |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame^] | 116 | using content::ChildProcessSecurityPolicy; |
[email protected] | 3a5180ae | 2011-12-21 02:39:38 | [diff] [blame] | 117 | using content::PluginService; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 118 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 119 | BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line) |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 120 | : created_metrics_service_(false), |
[email protected] | 0b56518 | 2011-03-02 18:11:15 | [diff] [blame] | 121 | created_watchdog_thread_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | created_profile_manager_(false), |
| 123 | created_local_state_(false), |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 124 | created_icon_manager_(false), |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 125 | created_browser_policy_connector_(false), |
[email protected] | 29672ab | 2009-10-30 03:44:03 | [diff] [blame] | 126 | created_notification_ui_manager_(false), |
[email protected] | 462a0ff | 2011-06-02 17:15:34 | [diff] [blame] | 127 | created_safe_browsing_service_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | module_ref_count_(0), |
[email protected] | afd20c02 | 2010-06-10 00:48:20 | [diff] [blame] | 129 | did_start_(false), |
[email protected] | 1b2db1a | 2008-08-08 17:46:13 | [diff] [blame] | 130 | checked_for_new_frames_(false), |
[email protected] | 7a0e72f | 2011-09-20 19:59:11 | [diff] [blame] | 131 | using_new_frames_(false), |
| 132 | thumbnail_generator_(new ThumbnailGenerator), |
| 133 | download_status_updater_(new DownloadStatusUpdater) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | g_browser_process = this; |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 135 | clipboard_.reset(new ui::Clipboard); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | |
| 137 | // Must be created after the NotificationService. |
| 138 | print_job_manager_.reset(new printing::PrintJobManager); |
| 139 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 140 | net_log_.reset(new ChromeNetLog); |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 141 | |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 142 | ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme( |
| 143 | chrome::kExtensionScheme); |
| 144 | |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 145 | extension_event_router_forwarder_ = new ExtensionEventRouterForwarder; |
[email protected] | fc4facd | 2011-03-22 23:18:50 | [diff] [blame] | 146 | |
| 147 | ExtensionTabIdMap::GetInstance()->Init(); |
[email protected] | 6eac57a | 2011-07-12 21:15:09 | [diff] [blame] | 148 | |
| 149 | online_state_observer_.reset(new BrowserOnlineStateObserver); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | BrowserProcessImpl::~BrowserProcessImpl() { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 153 | // Wait for the pending print jobs to finish. |
| 154 | print_job_manager_->OnQuit(); |
| 155 | print_job_manager_.reset(); |
| 156 | |
| 157 | tracked_objects::ThreadData::EnsureCleanupWasCalled(4); |
| 158 | |
| 159 | g_browser_process = NULL; |
| 160 | } |
| 161 | |
| 162 | void BrowserProcessImpl::StartTearDown() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | // Delete the AutomationProviderList before NotificationService, |
| 164 | // since it may try to unregister notifications |
| 165 | // Both NotificationService and AutomationProvider are singleton instances in |
| 166 | // the BrowserProcess. Since AutomationProvider may have some active |
| 167 | // notification observers, it is essential that it gets destroyed before the |
| 168 | // NotificationService. NotificationService won't be destroyed until after |
| 169 | // this destructor is run. |
| 170 | automation_provider_list_.reset(); |
| 171 | |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 172 | // We need to shutdown the SdchDictionaryFetcher as it regularly holds |
| 173 | // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do |
| 174 | // a PostDelayedTask onto the IO thread. This shutdown call will both discard |
| 175 | // any pending URLFetchers, and avoid creating any more. |
[email protected] | cf956b98 | 2011-12-02 12:00:45 | [diff] [blame] | 176 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 177 | base::Bind(&SdchDictionaryFetcher::Shutdown)); |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 178 | |
[email protected] | 51ac04076 | 2011-01-20 02:01:04 | [diff] [blame] | 179 | // We need to destroy the MetricsService, GoogleURLTracker, |
| 180 | // IntranetRedirectDetector, and SafeBrowsing ClientSideDetectionService |
[email protected] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 181 | // (owned by the SafeBrowsingService) before the io_thread_ gets destroyed, |
| 182 | // since their destructors can call the URLFetcher destructor, which does a |
| 183 | // PostDelayedTask operation on the IO thread. |
| 184 | // (The IO thread will handle that URLFetcher operation before going away.) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | metrics_service_.reset(); |
| 186 | google_url_tracker_.reset(); |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 187 | intranet_redirect_detector_.reset(); |
[email protected] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 188 | #if defined(ENABLE_SAFE_BROWSING) |
| 189 | if (safe_browsing_service_.get()) { |
| 190 | safe_browsing_service()->ShutDown(); |
| 191 | } |
| 192 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
[email protected] | d8a899c | 2010-09-21 04:50:33 | [diff] [blame] | 194 | // Need to clear the desktop notification balloons before the io_thread_ and |
| 195 | // before the profiles, since if there are any still showing we will access |
| 196 | // those things during teardown. |
| 197 | notification_ui_manager_.reset(); |
| 198 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | // Need to clear profiles (download managers) before the io_thread_. |
| 200 | profile_manager_.reset(); |
| 201 | |
| 202 | // Debugger must be cleaned up before IO thread and NotificationService. |
[email protected] | 4475d23 | 2011-07-27 15:29:20 | [diff] [blame] | 203 | remote_debugging_server_.reset(); |
| 204 | |
[email protected] | fc4facd | 2011-03-22 23:18:50 | [diff] [blame] | 205 | ExtensionTabIdMap::GetInstance()->Shutdown(); |
| 206 | |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 207 | // The policy providers managed by |browser_policy_connector_| need to shut |
| 208 | // down while the IO and FILE threads are still alive. |
| 209 | browser_policy_connector_.reset(); |
[email protected] | f2a893c | 2011-01-05 09:38:07 | [diff] [blame] | 210 | |
[email protected] | 8fd1183 | 2011-07-14 20:01:13 | [diff] [blame] | 211 | // Stop the watchdog thread before stopping other threads. |
| 212 | watchdog_thread_.reset(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 213 | } |
[email protected] | 8fd1183 | 2011-07-14 20:01:13 | [diff] [blame] | 214 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 215 | void BrowserProcessImpl::PostDestroyThreads() { |
| 216 | // With the file_thread_ flushed, we can release any icon resources. |
| 217 | icon_manager_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 219 | // Reset associated state right after actual thread is stopped, |
| 220 | // as io_thread_.global_ cleanup happens in CleanUp on the IO |
| 221 | // thread, i.e. as the thread exits its message loop. |
| 222 | // |
| 223 | // This is important also because in various places, the |
| 224 | // IOThread object being NULL is considered synonymous with the |
| 225 | // IO thread having stopped. |
| 226 | io_thread_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 229 | #if defined(OS_WIN) |
[email protected] | af391f0 | 2011-09-15 06:13:35 | [diff] [blame] | 230 | // Send a QuitTask to the given MessageLoop when the (file) thread has processed |
| 231 | // our (other) recent requests (to save preferences). |
| 232 | // Change the boolean so that the receiving thread will know that we did indeed |
| 233 | // send the QuitTask that terminated the message loop. |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 234 | static void PostQuit(MessageLoop* message_loop) { |
[email protected] | af391f0 | 2011-09-15 06:13:35 | [diff] [blame] | 235 | g_end_session_file_thread_has_completed = true; |
[email protected] | a778709f | 2011-12-10 00:28:17 | [diff] [blame] | 236 | message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 237 | } |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 238 | #elif defined(USE_X11) |
| 239 | static void Signal(base::WaitableEvent* event) { |
| 240 | event->Signal(); |
| 241 | } |
| 242 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 244 | unsigned int BrowserProcessImpl::AddRefModule() { |
| 245 | DCHECK(CalledOnValidThread()); |
[email protected] | 63b9d96 | 2011-09-14 20:32:31 | [diff] [blame] | 246 | CHECK(!IsShuttingDown()); |
[email protected] | afd20c02 | 2010-06-10 00:48:20 | [diff] [blame] | 247 | did_start_ = true; |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 248 | module_ref_count_++; |
| 249 | return module_ref_count_; |
| 250 | } |
| 251 | |
| 252 | unsigned int BrowserProcessImpl::ReleaseModule() { |
| 253 | DCHECK(CalledOnValidThread()); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 254 | DCHECK_NE(0u, module_ref_count_); |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 255 | module_ref_count_--; |
| 256 | if (0 == module_ref_count_) { |
[email protected] | e6244c18 | 2011-11-01 22:06:58 | [diff] [blame] | 257 | CHECK(MessageLoop::current()->is_running()); |
[email protected] | 7d038c3 | 2010-12-14 00:40:00 | [diff] [blame] | 258 | // Allow UI and IO threads to do blocking IO on shutdown, since we do a lot |
| 259 | // of it on shutdown for valid reasons. |
| 260 | base::ThreadRestrictions::SetIOAllowed(true); |
[email protected] | 0a53082e | 2011-09-16 09:31:25 | [diff] [blame] | 261 | CHECK(!BrowserList::GetLastActive()); |
[email protected] | dd48370 | 2011-12-02 14:47:42 | [diff] [blame] | 262 | BrowserThread::PostTask( |
| 263 | BrowserThread::IO, |
[email protected] | 7d038c3 | 2010-12-14 00:40:00 | [diff] [blame] | 264 | FROM_HERE, |
[email protected] | dcde767 | 2012-01-06 02:37:17 | [diff] [blame] | 265 | base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
| 266 | true)); |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 267 | |
| 268 | #if defined(OS_MACOSX) |
[email protected] | 8d2f91e | 2011-09-15 22:38:04 | [diff] [blame] | 269 | MessageLoop::current()->PostTask( |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 270 | FROM_HERE, |
[email protected] | b3cc0544 | 2011-11-19 22:43:44 | [diff] [blame] | 271 | base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop)); |
[email protected] | 03d8d3e9 | 2011-09-20 06:07:11 | [diff] [blame] | 272 | #endif |
[email protected] | b443cb04 | 2009-12-15 22:05:09 | [diff] [blame] | 273 | MessageLoop::current()->Quit(); |
| 274 | } |
| 275 | return module_ref_count_; |
| 276 | } |
| 277 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | void BrowserProcessImpl::EndSession() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | // Mark all the profiles as clean. |
| 280 | ProfileManager* pm = profile_manager(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 281 | std::vector<Profile*> profiles(pm->GetLoadedProfiles()); |
| 282 | for (size_t i = 0; i < profiles.size(); ++i) |
| 283 | profiles[i]->MarkAsCleanShutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | |
| 285 | // Tell the metrics service it was cleanly shutdown. |
| 286 | MetricsService* metrics = g_browser_process->metrics_service(); |
| 287 | if (metrics && local_state()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | metrics->RecordStartOfSessionEnd(); |
| 289 | |
| 290 | // MetricsService lazily writes to prefs, force it to write now. |
[email protected] | fbe17c8a | 2011-12-27 16:41:48 | [diff] [blame] | 291 | local_state()->CommitPendingWrite(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // We must write that the profile and metrics service shutdown cleanly, |
| 295 | // otherwise on startup we'll think we crashed. So we block until done and |
| 296 | // then proceed with normal shutdown. |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 297 | #if defined(USE_X11) |
| 298 | // Can't run a local loop on linux. Instead create a waitable event. |
[email protected] | 0bfbcdb | 2011-09-15 21:28:04 | [diff] [blame] | 299 | scoped_ptr<base::WaitableEvent> done_writing( |
| 300 | new base::WaitableEvent(false, false)); |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 301 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | b3cc0544 | 2011-11-19 22:43:44 | [diff] [blame] | 302 | base::Bind(Signal, done_writing.get())); |
[email protected] | 0bfbcdb | 2011-09-15 21:28:04 | [diff] [blame] | 303 | // If all file writes haven't cleared in the timeout, leak the WaitableEvent |
| 304 | // so that there's no race to reference it in Signal(). |
| 305 | if (!done_writing->TimedWait( |
| 306 | base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds))) |
| 307 | ignore_result(done_writing.release()); |
| 308 | |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 309 | #elif defined(OS_WIN) |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 310 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | b3cc0544 | 2011-11-19 22:43:44 | [diff] [blame] | 311 | base::Bind(PostQuit, MessageLoop::current())); |
[email protected] | af391f0 | 2011-09-15 06:13:35 | [diff] [blame] | 312 | int quits_received = 0; |
| 313 | do { |
| 314 | MessageLoop::current()->Run(); |
| 315 | ++quits_received; |
| 316 | } while (!g_end_session_file_thread_has_completed); |
| 317 | // If we did get extra quits, then we should re-post them to the message loop. |
| 318 | while (--quits_received > 0) |
[email protected] | a778709f | 2011-12-10 00:28:17 | [diff] [blame] | 319 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | c6032e8 | 2010-09-13 20:06:05 | [diff] [blame] | 320 | #else |
| 321 | NOTIMPLEMENTED(); |
| 322 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 325 | MetricsService* BrowserProcessImpl::metrics_service() { |
| 326 | DCHECK(CalledOnValidThread()); |
| 327 | if (!created_metrics_service_) |
| 328 | CreateMetricsService(); |
| 329 | return metrics_service_.get(); |
| 330 | } |
| 331 | |
| 332 | IOThread* BrowserProcessImpl::io_thread() { |
| 333 | DCHECK(CalledOnValidThread()); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 334 | DCHECK(io_thread_.get()); |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 335 | return io_thread_.get(); |
| 336 | } |
| 337 | |
[email protected] | 0b56518 | 2011-03-02 18:11:15 | [diff] [blame] | 338 | WatchDogThread* BrowserProcessImpl::watchdog_thread() { |
| 339 | DCHECK(CalledOnValidThread()); |
| 340 | if (!created_watchdog_thread_) |
| 341 | CreateWatchdogThread(); |
| 342 | DCHECK(watchdog_thread_.get() != NULL); |
| 343 | return watchdog_thread_.get(); |
| 344 | } |
| 345 | |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 346 | ProfileManager* BrowserProcessImpl::profile_manager() { |
| 347 | DCHECK(CalledOnValidThread()); |
| 348 | if (!created_profile_manager_) |
| 349 | CreateProfileManager(); |
| 350 | return profile_manager_.get(); |
| 351 | } |
| 352 | |
| 353 | PrefService* BrowserProcessImpl::local_state() { |
| 354 | DCHECK(CalledOnValidThread()); |
| 355 | if (!created_local_state_) |
| 356 | CreateLocalState(); |
| 357 | return local_state_.get(); |
| 358 | } |
| 359 | |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 360 | ui::Clipboard* BrowserProcessImpl::clipboard() { |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 361 | DCHECK(CalledOnValidThread()); |
| 362 | return clipboard_.get(); |
| 363 | } |
| 364 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 365 | net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() { |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 366 | DCHECK(CalledOnValidThread()); |
| 367 | return io_thread()->system_url_request_context_getter(); |
| 368 | } |
| 369 | |
| 370 | #if defined(OS_CHROMEOS) |
[email protected] | 5b86996 | 2011-09-20 19:49:33 | [diff] [blame] | 371 | browser::OomPriorityManager* BrowserProcessImpl::oom_priority_manager() { |
| 372 | DCHECK(CalledOnValidThread()); |
| 373 | if (!oom_priority_manager_.get()) |
| 374 | oom_priority_manager_.reset(new browser::OomPriorityManager()); |
| 375 | return oom_priority_manager_.get(); |
| 376 | } |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 377 | #endif // defined(OS_CHROMEOS) |
| 378 | |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 379 | ExtensionEventRouterForwarder* |
| 380 | BrowserProcessImpl::extension_event_router_forwarder() { |
| 381 | return extension_event_router_forwarder_.get(); |
| 382 | } |
| 383 | |
[email protected] | e13ad79b | 2010-07-22 21:36:50 | [diff] [blame] | 384 | NotificationUIManager* BrowserProcessImpl::notification_ui_manager() { |
| 385 | DCHECK(CalledOnValidThread()); |
| 386 | if (!created_notification_ui_manager_) |
| 387 | CreateNotificationUIManager(); |
| 388 | return notification_ui_manager_.get(); |
| 389 | } |
| 390 | |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 391 | policy::BrowserPolicyConnector* BrowserProcessImpl::browser_policy_connector() { |
[email protected] | f2a893c | 2011-01-05 09:38:07 | [diff] [blame] | 392 | DCHECK(CalledOnValidThread()); |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] | 393 | if (!created_browser_policy_connector_) { |
| 394 | DCHECK(browser_policy_connector_.get() == NULL); |
| 395 | created_browser_policy_connector_ = true; |
[email protected] | f31e2e5 | 2011-07-14 16:01:19 | [diff] [blame] | 396 | #if defined(ENABLE_CONFIGURATION_POLICY) |
[email protected] | 5fe0458 | 2011-11-21 19:25:57 | [diff] [blame] | 397 | browser_policy_connector_.reset(new policy::BrowserPolicyConnector()); |
| 398 | browser_policy_connector_->Init(); |
[email protected] | f31e2e5 | 2011-07-14 16:01:19 | [diff] [blame] | 399 | #endif |
[email protected] | f2a893c | 2011-01-05 09:38:07 | [diff] [blame] | 400 | } |
[email protected] | 985655a | 2011-02-23 09:54:25 | [diff] [blame] |
|