blob: 75897f22c2549109d3454dc419cf9a0ca2a0c34f [file] [log] [blame]
[email protected]3a80ea332012-01-09 19:53:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/browser_process_impl.h"
6
[email protected]38489a32012-05-11 02:24:547#include <algorithm>
[email protected]3cdacd42010-04-30 18:55:538#include <map>
[email protected]68f88b992011-05-07 02:01:399#include <vector>
[email protected]3cdacd42010-04-30 18:55:5310
[email protected]b3cc05442011-11-19 22:43:4411#include "base/bind.h"
12#include "base/bind_helpers.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/command_line.h"
[email protected]6b4e6852012-02-14 04:02:4814#include "base/debug/alias.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/path_service.h"
[email protected]03b9b4e2012-10-22 20:01:5216#include "base/prefs/json_pref_store.h"
[email protected]3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_registry_simple.h"
18#include "base/prefs/pref_service.h"
[email protected]985655a2011-02-23 09:54:2519#include "base/synchronization/waitable_event.h"
[email protected]34b99632011-01-01 01:01:0620#include "base/threading/thread.h"
21#include "base/threading/thread_restrictions.h"
[email protected]e13ad79b2010-07-22 21:36:5022#include "chrome/browser/automation/automation_provider_list.h"
[email protected]a07676b22011-06-17 16:36:5323#include "chrome/browser/background/background_mode_manager.h"
[email protected]c38831a12011-10-28 12:44:4924#include "chrome/browser/chrome_browser_main.h"
[email protected]572f40c9b2012-07-06 20:35:3325#include "chrome/browser/chrome_content_browser_client.h"
[email protected]fdf40f3e2013-07-11 23:55:4626#include "chrome/browser/chrome_notification_types.h"
[email protected]c1adf5a2011-08-03 22:11:3727#include "chrome/browser/component_updater/component_updater_configurator.h"
28#include "chrome/browser/component_updater/component_updater_service.h"
[email protected]9f108f22013-04-09 00:39:5629#include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
[email protected]195c99c2012-10-31 06:24:5130#include "chrome/browser/defaults.h"
[email protected]70019152012-12-19 11:44:1931#include "chrome/browser/devtools/remote_debugging_server.h"
[email protected]254ed742011-08-16 18:45:2732#include "chrome/browser/download/download_request_limiter.h"
[email protected]75e51b52012-02-04 16:57:5433#include "chrome/browser/download/download_status_updater.h"
[email protected]5a38dfd2012-07-23 23:22:1034#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]d5570942013-01-24 01:42:4335#include "chrome/browser/extensions/extension_renderer_state.h"
[email protected]815856722011-04-13 17:19:1936#include "chrome/browser/first_run/upgrade_util.h"
[email protected]a9830b52012-12-17 23:30:3637#include "chrome/browser/gpu/gl_string_manager.h"
[email protected]e6933d2a2013-03-12 22:32:2138#include "chrome/browser/gpu/gpu_mode_manager.h"
[email protected]dcefa302009-05-20 00:24:3939#include "chrome/browser/icon_manager.h"
[email protected]91901092013-01-31 01:53:5940#include "chrome/browser/idle.h"
[email protected]c4ff4952010-01-08 19:12:4741#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2742#include "chrome/browser/io_thread.h"
[email protected]2e6389f2012-05-18 19:41:2543#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]dc6f4962009-02-13 01:25:5044#include "chrome/browser/metrics/metrics_service.h"
[email protected]0b565182011-03-02 18:11:1545#include "chrome/browser/metrics/thread_watcher.h"
[email protected]cf265dc02012-08-15 01:01:1646#include "chrome/browser/metrics/variations/variations_service.h"
[email protected]b2fcd0e2010-12-01 15:19:4047#include "chrome/browser/net/chrome_net_log.h"
[email protected]ca167a32011-09-30 15:19:1148#include "chrome/browser/net/crl_set_fetcher.h"
[email protected]d393a0fd2009-05-13 23:32:0149#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]29672ab2009-10-30 03:44:0350#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]c2295f72013-01-03 22:18:5651#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
[email protected]de75c702012-09-25 23:06:0252#include "chrome/browser/plugins/plugin_finder.h"
[email protected]21d3a882012-05-31 14:41:5553#include "chrome/browser/policy/policy_service.h"
[email protected]caf63aea2011-04-26 11:04:1054#include "chrome/browser/prefs/browser_prefs.h"
[email protected]5b199522012-12-22 17:24:4455#include "chrome/browser/prefs/chrome_pref_service_factory.h"
[email protected]1459fb62011-05-25 19:03:2756#include "chrome/browser/prerender/prerender_tracker.h"
[email protected]68f88b992011-05-07 02:01:3957#include "chrome/browser/printing/background_printing_manager.h"
[email protected]e06131d2010-02-10 18:40:3358#include "chrome/browser/printing/print_job_manager.h"
[email protected]d8ce31e2012-12-19 05:09:2159#include "chrome/browser/printing/print_preview_dialog_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3360#include "chrome/browser/profiles/profile_manager.h"
[email protected]8bcdf072011-06-03 16:52:1561#include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
[email protected]81218f42009-02-05 18:48:0862#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]e450fa62011-02-01 12:52:5663#include "chrome/browser/shell_integration.h"
[email protected]9e7f015f2011-05-28 00:24:2564#include "chrome/browser/status_icons/status_tray.h"
[email protected]8b60ca722013-07-19 01:29:0265#include "chrome/browser/storage_monitor/storage_monitor.h"
[email protected]94e385322012-10-08 20:21:3366#include "chrome/browser/thumbnails/render_widget_snapshot_taker.h"
[email protected]195c99c2012-10-31 06:24:5167#include "chrome/browser/ui/bookmarks/bookmark_prompt_controller.h"
[email protected]0665ebe2013-02-13 09:53:1968#include "chrome/browser/ui/browser_finder.h"
[email protected]ed304482013-01-04 04:45:3269#include "chrome/browser/web_resource/promo_resource_service.h"
[email protected]4ef795df2010-02-03 02:35:0870#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2971#include "chrome/common/chrome_paths.h"
72#include "chrome/common/chrome_switches.h"
[email protected]d592b1bd2013-05-06 06:40:4773#include "chrome/common/extensions/chrome_manifest_handlers.h"
[email protected]f1b6de22010-03-06 12:13:4774#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]e676f8f2013-04-04 09:04:3775#include "chrome/common/extensions/permissions/chrome_api_permissions.h"
76#include "chrome/common/extensions/permissions/permissions_info.h"
initial.commit09911bf2008-07-26 23:55:2977#include "chrome/common/pref_names.h"
[email protected]5c08f222010-09-22 09:37:2178#include "chrome/common/switch_utils.h"
[email protected]985655a2011-02-23 09:54:2579#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4280#include "chrome/installer/util/google_update_constants.h"
[email protected]c38831a12011-10-28 12:44:4981#include "content/public/browser/browser_thread.h"
[email protected]b9535422012-02-09 01:47:5982#include "content/public/browser/child_process_security_policy.h"
[email protected]ad50def52011-10-19 23:17:0783#include "content/public/browser/notification_details.h"
[email protected]3a5180ae2011-12-21 02:39:3884#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:3085#include "content/public/browser/render_process_host.h"
[email protected]ea114722012-03-12 01:11:2586#include "content/public/browser/resource_dispatcher_host.h"
[email protected]885c0e92012-11-13 20:27:4287#include "extensions/common/constants.h"
[email protected]12c84e22011-07-11 09:35:4588#include "net/socket/client_socket_pool_manager.h"
[email protected]abe2c032011-03-31 18:49:3489#include "net/url_request/url_request_context_getter.h"
[email protected]c051a1b2011-01-21 23:30:1790#include "ui/base/l10n/l10n_util.h"
[email protected]fb1aac14e2013-05-14 05:30:2491#include "ui/message_center/message_center.h"
[email protected]b112a4c2009-02-01 20:24:0192
[email protected]3b19e8e2012-10-17 19:15:4993#if defined(ENABLE_CONFIGURATION_POLICY)
94#include "chrome/browser/policy/browser_policy_connector.h"
95#else
[email protected]21d3a882012-05-31 14:41:5596#include "chrome/browser/policy/policy_service_stub.h"
97#endif // defined(ENABLE_CONFIGURATION_POLICY)
98
[email protected]b112a4c2009-02-01 20:24:0199#if defined(OS_WIN)
[email protected]770c6d82012-09-06 22:21:32100#include "base/win/windows_version.h"
[email protected]477ae052011-11-18 23:53:57101#include "ui/views/focus/view_storage.h"
[email protected]03d8d3e92011-09-20 06:07:11102#elif defined(OS_MACOSX)
103#include "chrome/browser/chrome_browser_main_mac.h"
[email protected]86230b92009-11-23 20:38:38104#endif
105
[email protected]4189bc222012-03-29 23:11:47106#if defined(USE_AURA)
107#include "ui/aura/env.h"
108#endif
109
[email protected]8b60ca722013-07-19 01:29:02110#if !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]e8262a52013-04-24 02:02:51111#include "chrome/browser/media_galleries/media_file_system_registry.h"
112#endif
113
[email protected]2366a3a2012-10-02 20:41:14114#if defined(ENABLE_PLUGIN_INSTALLATION)
[email protected]97eddfe2013-02-12 19:16:30115#include "chrome/browser/plugins/plugins_resource_service.h"
[email protected]2366a3a2012-10-02 20:41:14116#endif
117
[email protected]acd2121c2013-03-18 05:54:50118#if defined(OS_MACOSX)
119#include "apps/app_shim/app_shim_host_manager_mac.h"
[email protected]5538b312013-05-21 08:27:07120#include "chrome/browser/ui/app_list/app_list_service.h"
[email protected]acd2121c2013-03-18 05:54:50121#endif
122
[email protected]bbf32c52013-06-03 14:05:52123#if defined(ENABLE_WEBRTC)
124#include "chrome/browser/media/webrtc_log_uploader.h"
125#endif
126
[email protected]3cdacd42010-04-30 18:55:53127#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
128// How often to check if the persistent instance of Chrome needs to restart
129// to install an update.
130static const int kUpdateCheckIntervalHours = 6;
131#endif
132
[email protected]af391f02011-09-15 06:13:35133#if defined(OS_WIN)
134// Attest to the fact that the call to the file thread to save preferences has
135// run, and it is safe to terminate. This avoids the potential of some other
136// task prematurely terminating our waiting message loop by posting a
137// QuitTask().
138static bool g_end_session_file_thread_has_completed = false;
139#endif
140
[email protected]c6032e82010-09-13 20:06:05141#if defined(USE_X11)
142// How long to wait for the File thread to complete during EndSession, on
143// Linux. We have a timeout here because we're unable to run the UI messageloop
144// and there's some deadlock risk. Our only option is to exit anyway.
145static const int kEndSessionTimeoutSeconds = 10;
146#endif
147
[email protected]631bb742011-11-02 11:29:39148using content::BrowserThread;
[email protected]b9535422012-02-09 01:47:59149using content::ChildProcessSecurityPolicy;
[email protected]3a5180ae2011-12-21 02:39:38150using content::PluginService;
[email protected]ea114722012-03-12 01:11:25151using content::ResourceDispatcherHost;
[email protected]631bb742011-11-02 11:29:39152
[email protected]0de615a2012-11-08 04:40:59153BrowserProcessImpl::BrowserProcessImpl(
154 base::SequencedTaskRunner* local_state_task_runner,
155 const CommandLine& command_line)
[email protected]99907362012-01-11 05:41:40156 : created_metrics_service_(false),
[email protected]0b565182011-03-02 18:11:15157 created_watchdog_thread_(false),
[email protected]663831822012-05-23 10:19:21158 created_browser_policy_connector_(false),
initial.commit09911bf2008-07-26 23:55:29159 created_profile_manager_(false),
160 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:01161 created_icon_manager_(false),
[email protected]29672ab2009-10-30 03:44:03162 created_notification_ui_manager_(false),
[email protected]462a0ff2011-06-02 17:15:34163 created_safe_browsing_service_(false),
initial.commit09911bf2008-07-26 23:55:29164 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20165 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13166 checked_for_new_frames_(false),
[email protected]7a0e72f2011-09-20 19:59:11167 using_new_frames_(false),
[email protected]94e385322012-10-08 20:21:33168 render_widget_snapshot_taker_(new RenderWidgetSnapshotTaker),
[email protected]0de615a2012-11-08 04:40:59169 download_status_updater_(new DownloadStatusUpdater),
170 local_state_task_runner_(local_state_task_runner) {
initial.commit09911bf2008-07-26 23:55:29171 g_browser_process = this;
[email protected]7cad6b0d2013-04-25 20:29:32172 platform_part_.reset(new BrowserProcessPlatformPart());
initial.commit09911bf2008-07-26 23:55:29173
[email protected]658677f2012-06-09 06:04:02174#if defined(ENABLE_PRINTING)
initial.commit09911bf2008-07-26 23:55:29175 // Must be created after the NotificationService.
176 print_job_manager_.reset(new printing::PrintJobManager);
[email protected]058e5732012-03-01 22:48:03177#endif
initial.commit09911bf2008-07-26 23:55:29178
[email protected]b2fcd0e2010-12-01 15:19:40179 net_log_.reset(new ChromeNetLog);
[email protected]3ce02412011-03-01 12:01:15180
[email protected]8add5412011-10-01 21:02:14181 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
[email protected]885c0e92012-11-13 20:27:42182 extensions::kExtensionScheme);
[email protected]b23b1b42012-05-18 19:09:16183 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
184 chrome::kExtensionResourceScheme);
[email protected]672c8c12013-03-07 12:30:06185 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
186 chrome::kChromeSearchScheme);
[email protected]8add5412011-10-01 21:02:14187
[email protected]91901092013-01-31 01:53:59188#if defined(OS_MACOSX)
189 InitIdleMonitor();
190#endif
191
[email protected]e676f8f2013-04-04 09:04:37192 extensions::PermissionsInfo::GetInstance()->InitializeWithDelegate(
193 extensions::ChromeAPIPermissions());
[email protected]d592b1bd2013-05-06 06:40:47194 extensions::RegisterChromeManifestHandlers();
[email protected]5a38dfd2012-07-23 23:22:10195 extension_event_router_forwarder_ = new extensions::EventRouterForwarder;
[email protected]d5570942013-01-24 01:42:43196 ExtensionRendererState::GetInstance()->Init();
[email protected]356530722013-03-05 19:10:19197
[email protected]356530722013-03-05 19:10:19198 message_center::MessageCenter::Initialize();
initial.commit09911bf2008-07-26 23:55:29199}
200
201BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]2e5b60a22011-11-28 15:56:41202 tracked_objects::ThreadData::EnsureCleanupWasCalled(4);
203
204 g_browser_process = NULL;
205}
206
207void BrowserProcessImpl::StartTearDown() {
[email protected]2e22e2f2012-03-15 21:53:10208#if defined(ENABLE_AUTOMATION)
initial.commit09911bf2008-07-26 23:55:29209 // Delete the AutomationProviderList before NotificationService,
210 // since it may try to unregister notifications
211 // Both NotificationService and AutomationProvider are singleton instances in
212 // the BrowserProcess. Since AutomationProvider may have some active
213 // notification observers, it is essential that it gets destroyed before the
214 // NotificationService. NotificationService won't be destroyed until after
215 // this destructor is run.
216 automation_provider_list_.reset();
[email protected]2e22e2f2012-03-15 21:53:10217#endif
initial.commit09911bf2008-07-26 23:55:29218
[email protected]d393a0fd2009-05-13 23:32:01219 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
220 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
221 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
222 // any pending URLFetchers, and avoid creating any more.
[email protected]cf956b982011-12-02 12:00:45223 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
224 base::Bind(&SdchDictionaryFetcher::Shutdown));
[email protected]d393a0fd2009-05-13 23:32:01225
[email protected]54e26c122012-05-17 15:36:21226 // We need to destroy the MetricsService, VariationsService,
[email protected]ed304482013-01-04 04:45:32227 // IntranetRedirectDetector, PromoResourceService, and SafeBrowsing
228 // ClientSideDetectionService (owned by the SafeBrowsingService) before the
229 // io_thread_ gets destroyed, since their destructors can call the URLFetcher
230 // destructor, which does a PostDelayedTask operation on the IO thread. (The
231 // IO thread will handle that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29232 metrics_service_.reset();
[email protected]54e26c122012-05-17 15:36:21233 variations_service_.reset();
[email protected]c4ff4952010-01-08 19:12:47234 intranet_redirect_detector_.reset();
[email protected]6c521fed2012-11-29 17:00:03235#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
[email protected]ed304482013-01-04 04:45:32236 if (safe_browsing_service_.get())
[email protected]9efb46692011-08-23 12:56:05237 safe_browsing_service()->ShutDown();
[email protected]9efb46692011-08-23 12:56:05238#endif
initial.commit09911bf2008-07-26 23:55:29239
[email protected]d8a899c2010-09-21 04:50:33240 // Need to clear the desktop notification balloons before the io_thread_ and
241 // before the profiles, since if there are any still showing we will access
242 // those things during teardown.
243 notification_ui_manager_.reset();
244
initial.commit09911bf2008-07-26 23:55:29245 // Need to clear profiles (download managers) before the io_thread_.
246 profile_manager_.reset();
247
[email protected]81054f812012-08-30 00:47:09248#if !defined(OS_ANDROID)
initial.commit09911bf2008-07-26 23:55:29249 // Debugger must be cleaned up before IO thread and NotificationService.
[email protected]4475d232011-07-27 15:29:20250 remote_debugging_server_.reset();
[email protected]81054f812012-08-30 00:47:09251#endif
[email protected]4475d232011-07-27 15:29:20252
[email protected]d5570942013-01-24 01:42:43253 ExtensionRendererState::GetInstance()->Shutdown();
[email protected]fc4facd2011-03-22 23:18:50254
[email protected]8b60ca722013-07-19 01:29:02255#if !defined(OS_ANDROID) && !defined(OS_IOS)
256 media_file_system_registry_.reset();
257 // Delete |storage_monitor_| now. Otherwise the FILE thread would be gone
258 // when we try to release it in the dtor and Valgrind would report a
259 // leak on almost every single browser_test.
260 // TODO(gbillock): Make this unnecessary.
261 storage_monitor_.reset();
262#endif
263
[email protected]356530722013-03-05 19:10:19264 message_center::MessageCenter::Shutdown();
[email protected]356530722013-03-05 19:10:19265
[email protected]3b19e8e2012-10-17 19:15:49266#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]985655a2011-02-23 09:54:25267 // The policy providers managed by |browser_policy_connector_| need to shut
268 // down while the IO and FILE threads are still alive.
[email protected]3b19e8e2012-10-17 19:15:49269 if (browser_policy_connector_)
270 browser_policy_connector_->Shutdown();
271#endif
[email protected]f2a893c2011-01-05 09:38:07272
[email protected]8fd11832011-07-14 20:01:13273 // Stop the watchdog thread before stopping other threads.
274 watchdog_thread_.reset();
[email protected]4189bc222012-03-29 23:11:47275
276#if defined(USE_AURA)
277 // Delete aura after the metrics service has been deleted as it accesses
278 // monitor information.
279 aura::Env::DeleteInstance();
280#endif
[email protected]acd2121c2013-03-18 05:54:50281
282#if defined(OS_MACOSX)
283 app_shim_host_manager_.reset();
284#endif
[email protected]7cad6b0d2013-04-25 20:29:32285
286 platform_part()->StartTearDown();
[email protected]2e5b60a22011-11-28 15:56:41287}
[email protected]8fd11832011-07-14 20:01:13288
[email protected]99907362012-01-11 05:41:40289void BrowserProcessImpl::PostDestroyThreads() {
290 // With the file_thread_ flushed, we can release any icon resources.
291 icon_manager_.reset();
initial.commit09911bf2008-07-26 23:55:29292
[email protected]99907362012-01-11 05:41:40293 // Reset associated state right after actual thread is stopped,
294 // as io_thread_.global_ cleanup happens in CleanUp on the IO
295 // thread, i.e. as the thread exits its message loop.
296 //
297 // This is important also because in various places, the
298 // IOThread object being NULL is considered synonymous with the
299 // IO thread having stopped.
300 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29301}
302
[email protected]c6032e82010-09-13 20:06:05303#if defined(OS_WIN)
[email protected]af391f02011-09-15 06:13:35304// Send a QuitTask to the given MessageLoop when the (file) thread has processed
305// our (other) recent requests (to save preferences).
306// Change the boolean so that the receiving thread will know that we did indeed
307// send the QuitTask that terminated the message loop.
[email protected]b3a25092013-05-28 22:08:16308static void PostQuit(base::MessageLoop* message_loop) {
[email protected]af391f02011-09-15 06:13:35309 g_end_session_file_thread_has_completed = true;
[email protected]b3a25092013-05-28 22:08:16310 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
[email protected]295039bd2008-08-15 04:32:57311}
[email protected]c6032e82010-09-13 20:06:05312#elif defined(USE_X11)
313static void Signal(base::WaitableEvent* event) {
314 event->Signal();
315}
316#endif
initial.commit09911bf2008-07-26 23:55:29317
[email protected]b443cb042009-12-15 22:05:09318unsigned int BrowserProcessImpl::AddRefModule() {
319 DCHECK(CalledOnValidThread());
[email protected]6b4e6852012-02-14 04:02:48320
321 // CHECK(!IsShuttingDown());
322 if (IsShuttingDown()) {
323 // Copy the stacktrace which released the final reference onto our stack so
324 // it will be available in the crash report for inspection.
325 base::debug::StackTrace callstack = release_last_reference_callstack_;
326 base::debug::Alias(&callstack);
327 CHECK(false);
328 }
329
[email protected]afd20c022010-06-10 00:48:20330 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09331 module_ref_count_++;
332 return module_ref_count_;
333}
334
335unsigned int BrowserProcessImpl::ReleaseModule() {
336 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18337 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09338 module_ref_count_--;
339 if (0 == module_ref_count_) {
[email protected]6b4e6852012-02-14 04:02:48340 release_last_reference_callstack_ = base::debug::StackTrace();
341
[email protected]658677f2012-06-09 06:04:02342#if defined(ENABLE_PRINTING)
[email protected]6869771f2012-05-19 00:30:57343 // Wait for the pending print jobs to finish. Don't do this later, since
344 // this might cause a nested message loop to run, and we don't want pending
345 // tasks to run once teardown has started.
346 print_job_manager_->OnQuit();
347 print_job_manager_.reset();
348#endif
349
[email protected]b3a25092013-05-28 22:08:16350 CHECK(base::MessageLoop::current()->is_running());
[email protected]03d8d3e92011-09-20 06:07:11351
352#if defined(OS_MACOSX)
[email protected]b3a25092013-05-28 22:08:16353 base::MessageLoop::current()->PostTask(
[email protected]03d8d3e92011-09-20 06:07:11354 FROM_HERE,
[email protected]b3cc05442011-11-19 22:43:44355 base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop));
[email protected]03d8d3e92011-09-20 06:07:11356#endif
[email protected]b3a25092013-05-28 22:08:16357 base::MessageLoop::current()->Quit();
[email protected]b443cb042009-12-15 22:05:09358 }
359 return module_ref_count_;
360}
361
initial.commit09911bf2008-07-26 23:55:29362void BrowserProcessImpl::EndSession() {
initial.commit09911bf2008-07-26 23:55:29363 // Mark all the profiles as clean.
364 ProfileManager* pm = profile_manager();
[email protected]844a1002011-04-19 11:37:21365 std::vector<Profile*> profiles(pm->GetLoadedProfiles());
366 for (size_t i = 0; i < profiles.size(); ++i)
[email protected]6c0ca7fc2012-10-05 16:27:22367 profiles[i]->SetExitType(Profile::EXIT_SESSION_ENDED);
initial.commit09911bf2008-07-26 23:55:29368
369 // Tell the metrics service it was cleanly shutdown.
370 MetricsService* metrics = g_browser_process->metrics_service();
371 if (metrics && local_state()) {
initial.commit09911bf2008-07-26 23:55:29372 metrics->RecordStartOfSessionEnd();
373
374 // MetricsService lazily writes to prefs, force it to write now.
[email protected]fbe17c8a2011-12-27 16:41:48375 local_state()->CommitPendingWrite();
initial.commit09911bf2008-07-26 23:55:29376 }
377
[email protected]79a75b02012-04-26 18:03:58378 // http://crbug.com/125207
379 base::ThreadRestrictions::ScopedAllowWait allow_wait;
380
initial.commit09911bf2008-07-26 23:55:29381 // We must write that the profile and metrics service shutdown cleanly,
382 // otherwise on startup we'll think we crashed. So we block until done and
383 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05384#if defined(USE_X11)
385 // Can't run a local loop on linux. Instead create a waitable event.
[email protected]0bfbcdb2011-09-15 21:28:04386 scoped_ptr<base::WaitableEvent> done_writing(
387 new base::WaitableEvent(false, false));
[email protected]d04e7662010-10-10 22:24:48388 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]b3cc05442011-11-19 22:43:44389 base::Bind(Signal, done_writing.get()));
[email protected]0bfbcdb2011-09-15 21:28:04390 // If all file writes haven't cleared in the timeout, leak the WaitableEvent
391 // so that there's no race to reference it in Signal().
392 if (!done_writing->TimedWait(
[email protected]ed304482013-01-04 04:45:32393 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds))) {
[email protected]0bfbcdb2011-09-15 21:28:04394 ignore_result(done_writing.release());
[email protected]ed304482013-01-04 04:45:32395 }
[email protected]0bfbcdb2011-09-15 21:28:04396
[email protected]c6032e82010-09-13 20:06:05397#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48398 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]b3a25092013-05-28 22:08:16399 base::Bind(PostQuit, base::MessageLoop::current()));
[email protected]af391f02011-09-15 06:13:35400 int quits_received = 0;
401 do {
[email protected]b3a25092013-05-28 22:08:16402 base::MessageLoop::current()->Run();
[email protected]af391f02011-09-15 06:13:35403 ++quits_received;
404 } while (!g_end_session_file_thread_has_completed);
405 // If we did get extra quits, then we should re-post them to the message loop.
[email protected]b3a25092013-05-28 22:08:16406 while (--quits_received > 0) {
407 base::MessageLoop::current()->PostTask(FROM_HERE,
408 base::MessageLoop::QuitClosure());
409 }
[email protected]c6032e82010-09-13 20:06:05410#else
411 NOTIMPLEMENTED();
412#endif
initial.commit09911bf2008-07-26 23:55:29413}
414
[email protected]e13ad79b2010-07-22 21:36:50415MetricsService* BrowserProcessImpl::metrics_service() {
416 DCHECK(CalledOnValidThread());
417 if (!created_metrics_service_)
418 CreateMetricsService();
419 return metrics_service_.get();
420}
421
422IOThread* BrowserProcessImpl::io_thread() {
423 DCHECK(CalledOnValidThread());
[email protected]2e5b60a22011-11-28 15:56:41424 DCHECK(io_thread_.get());
[email protected]e13ad79b2010-07-22 21:36:50425 return io_thread_.get();
426}
427
[email protected]0b565182011-03-02 18:11:15428WatchDogThread* BrowserProcessImpl::watchdog_thread() {
429 DCHECK(CalledOnValidThread());
430 if (!created_watchdog_thread_)
431 CreateWatchdogThread();
432 DCHECK(watchdog_thread_.get() != NULL);
433 return watchdog_thread_.get();
434}
435
[email protected]e13ad79b2010-07-22 21:36:50436ProfileManager* BrowserProcessImpl::profile_manager() {
437 DCHECK(CalledOnValidThread());
438 if (!created_profile_manager_)
439 CreateProfileManager();
440 return profile_manager_.get();
441}
442
[email protected]b1de2c72013-02-06 02:45:47443PrefService* BrowserProcessImpl::local_state() {
[email protected]e13ad79b2010-07-22 21:36:50444 DCHECK(CalledOnValidThread());
445 if (!created_local_state_)
446 CreateLocalState();
447 return local_state_.get();
448}
449
[email protected]abe2c032011-03-31 18:49:34450net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
[email protected]db0e86dd2011-03-16 14:47:21451 DCHECK(CalledOnValidThread());
452 return io_thread()->system_url_request_context_getter();
453}
454
[email protected]04811242012-07-06 18:04:29