blob: c379704ad6f35baba5ba9319cc9a0d1650a7700b [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]7a6af7e2014-07-08 20:11:1311#include "base/atomic_ref_count.h"
[email protected]b3cc0542011-11-19 22:43:4412#include "base/bind.h"
13#include "base/bind_helpers.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/command_line.h"
[email protected]6b4e6852012-02-14 04:02:4815#include "base/debug/alias.h"
[email protected]69c019472013-10-15 05:03:4716#include "base/debug/leak_annotations.h"
[email protected]c027d6d2014-06-05 15:25:4017#include "base/files/file_path.h"
[email protected]cc2ebc72014-07-15 23:57:3618#include "base/metrics/field_trial.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]03b9b4e2012-10-22 20:01:5220#include "base/prefs/json_pref_store.h"
[email protected]3853a4c2013-02-11 17:15:5721#include "base/prefs/pref_registry_simple.h"
22#include "base/prefs/pref_service.h"
[email protected]985655a2011-02-23 09:54:2523#include "base/synchronization/waitable_event.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread.h"
25#include "base/threading/thread_restrictions.h"
[email protected]72f93712014-05-19 15:10:3926#include "base/time/default_tick_clock.h"
[email protected]a07676b22011-06-17 16:36:5327#include "chrome/browser/background/background_mode_manager.h"
[email protected]c38831a12011-10-28 12:44:4928#include "chrome/browser/chrome_browser_main.h"
[email protected]572f40c9b2012-07-06 20:35:3329#include "chrome/browser/chrome_content_browser_client.h"
[email protected]fdf40f3e2013-07-11 23:55:4630#include "chrome/browser/chrome_notification_types.h"
[email protected]de0fdca22014-08-19 05:26:0931#include "chrome/browser/component_updater/chrome_component_updater_configurator.h"
[email protected]9f108f22013-04-09 00:39:5632#include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
[email protected]195c99c2012-10-31 06:24:5133#include "chrome/browser/defaults.h"
[email protected]70019152012-12-19 11:44:1934#include "chrome/browser/devtools/remote_debugging_server.h"
[email protected]254ed742011-08-16 18:45:2735#include "chrome/browser/download/download_request_limiter.h"
[email protected]75e51b52012-02-04 16:57:5436#include "chrome/browser/download/download_status_updater.h"
[email protected]815856722011-04-13 17:19:1937#include "chrome/browser/first_run/upgrade_util.h"
[email protected]a9830b52012-12-17 23:30:3638#include "chrome/browser/gpu/gl_string_manager.h"
[email protected]e6933d2a2013-03-12 22:32:2139#include "chrome/browser/gpu/gpu_mode_manager.h"
[email protected]dcefa302009-05-20 00:24:3940#include "chrome/browser/icon_manager.h"
[email protected]91901092013-01-31 01:53:5941#include "chrome/browser/idle.h"
[email protected]c4ff4952010-01-08 19:12:4742#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2743#include "chrome/browser/io_thread.h"
[email protected]2e6389f2012-05-18 19:41:2544#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]c3cac952014-05-09 01:51:1845#include "chrome/browser/metrics/metrics_services_manager.h"
[email protected]0b565182011-03-02 18:11:1546#include "chrome/browser/metrics/thread_watcher.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]29672ab2009-10-30 03:44:0349#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]8f4b69e2014-06-26 00:01:3150#include "chrome/browser/omaha_query_params/chrome_omaha_query_params_delegate.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]caf63aea2011-04-26 11:04:1053#include "chrome/browser/prefs/browser_prefs.h"
[email protected]5b199522012-12-22 17:24:4454#include "chrome/browser/prefs/chrome_pref_service_factory.h"
[email protected]1459fb62011-05-25 19:03:2755#include "chrome/browser/prerender/prerender_tracker.h"
[email protected]68f88b992011-05-07 02:01:3956#include "chrome/browser/printing/background_printing_manager.h"
[email protected]e06131d2010-02-10 18:40:3357#include "chrome/browser/printing/print_job_manager.h"
[email protected]d8ce31e2012-12-19 05:09:2158#include "chrome/browser/printing/print_preview_dialog_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3359#include "chrome/browser/profiles/profile_manager.h"
[email protected]8bcdf072011-06-03 16:52:1560#include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
[email protected]81218f42009-02-05 18:48:0861#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]e450fa62011-02-01 12:52:5662#include "chrome/browser/shell_integration.h"
[email protected]9e7f015f2011-05-28 00:24:2563#include "chrome/browser/status_icons/status_tray.h"
[email protected]ed100e32014-08-05 06:38:2064#include "chrome/browser/ui/apps/chrome_apps_client.h"
[email protected]94840bb2013-12-17 19:43:3865#include "chrome/browser/ui/browser_dialogs.h"
[email protected]0665ebe2013-02-13 09:53:1966#include "chrome/browser/ui/browser_finder.h"
[email protected]ed304482013-01-04 04:45:3267#include "chrome/browser/web_resource/promo_resource_service.h"
[email protected]4ef795df2010-02-03 02:35:0868#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2969#include "chrome/common/chrome_paths.h"
70#include "chrome/common/chrome_switches.h"
[email protected]ad8b4ba2013-08-09 19:52:4471#include "chrome/common/extensions/chrome_extensions_client.h"
initial.commit09911bf2008-07-26 23:55:2972#include "chrome/common/pref_names.h"
[email protected]5c08f222010-09-22 09:37:2173#include "chrome/common/switch_utils.h"
[email protected]985655a2011-02-23 09:54:2574#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4275#include "chrome/installer/util/google_update_constants.h"
[email protected]48ff2c7f2014-05-23 09:57:4576#include "chrome/installer/util/google_update_settings.h"
[email protected]de0fdca22014-08-19 05:26:0977#include "components/component_updater/component_updater_service.h"
[email protected]c027d6d2014-06-05 15:25:4078#include "components/gcm_driver/gcm_driver.h"
[email protected]d6147bd2014-06-11 01:58:1979#include "components/metrics/metrics_service.h"
[email protected]d0ab865b2014-06-04 19:41:3480#include "components/network_time/network_time_tracker.h"
[email protected]8f4b69e2014-06-26 00:01:3181#include "components/omaha_query_params/omaha_query_params.h"
[email protected]76b4b152013-12-08 21:10:0482#include "components/policy/core/common/policy_service.h"
[email protected]1c799ff2014-04-16 01:44:5283#include "components/signin/core/common/profile_management_switches.h"
[email protected]bb1c86cd2014-01-29 18:22:1884#include "components/translate/core/browser/translate_download_manager.h"
[email protected]c38831a12011-10-28 12:44:4985#include "content/public/browser/browser_thread.h"
[email protected]b9535422012-02-09 01:47:5986#include "content/public/browser/child_process_security_policy.h"
[email protected]ad50def52011-10-19 23:17:0787#include "content/public/browser/notification_details.h"
[email protected]3a5180ae2011-12-21 02:39:3888#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:3089#include "content/public/browser/render_process_host.h"
[email protected]ea114722012-03-12 01:11:2590#include "content/public/browser/resource_dispatcher_host.h"
[email protected]294bdb32014-05-30 07:01:5491#include "content/public/browser/service_worker_context.h"
92#include "content/public/browser/storage_partition.h"
[email protected]885c0e92012-11-13 20:27:4293#include "extensions/common/constants.h"
[email protected]6668e5d2014-04-08 23:32:5294#include "extensions/common/extension_l10n_util.h"
[email protected]12c84e22011-07-11 09:35:4595#include "net/socket/client_socket_pool_manager.h"
[email protected]abe2c032011-03-31 18:49:3496#include "net/url_request/url_request_context_getter.h"
[email protected]c051a1b2011-01-21 23:30:1797#include "ui/base/l10n/l10n_util.h"
[email protected]fb1aac12013-05-14 05:30:2498#include "ui/message_center/message_center.h"
[email protected]b112a4c2009-02-01 20:24:0199
100#if defined(OS_WIN)
[email protected]770c6d82012-09-06 22:21:32101#include "base/win/windows_version.h"
[email protected]477ae052011-11-18 23:53:57102#include "ui/views/focus/view_storage.h"
[email protected]03d8d3e92011-09-20 06:07:11103#elif defined(OS_MACOSX)
104#include "chrome/browser/chrome_browser_main_mac.h"
[email protected]86230b92009-11-23 20:38:38105#endif
106
[email protected]cb6341e2014-06-17 03:32:02107#if defined(OS_ANDROID)
108#include "components/gcm_driver/gcm_driver_android.h"
109#else
110#include "chrome/browser/services/gcm/gcm_desktop_utils.h"
111#include "components/gcm_driver/gcm_client_factory.h"
[email protected]4189bc222012-03-29 23:11:47112#endif
113
[email protected]cb6341e2014-06-17 03:32:02114#if defined(USE_AURA)
115#include "ui/aura/env.h"
116#endif
117
118#if defined(ENABLE_CONFIGURATION_POLICY)
119#include "components/policy/core/browser/browser_policy_connector.h"
[email protected]c027d6d2014-06-05 15:25:40120#else
[email protected]cb6341e2014-06-17 03:32:02121#include "components/policy/core/common/policy_service_stub.h"
122#endif // defined(ENABLE_CONFIGURATION_POLICY)
123
[email protected]84b7a552014-07-19 04:52:06124#if defined(ENABLE_EXTENSIONS)
[email protected]d5a74be2014-08-08 01:01:27125#include "chrome/browser/extensions/chrome_extensions_browser_client.h"
[email protected]84b7a552014-07-19 04:52:06126#include "chrome/browser/extensions/event_router_forwarder.h"
127#include "chrome/browser/extensions/extension_renderer_state.h"
128#include "chrome/browser/media_galleries/media_file_system_registry.h"
129#include "components/storage_monitor/storage_monitor.h"
130#endif
131
[email protected]cb6341e2014-06-17 03:32:02132#if defined(ENABLE_PLUGIN_INSTALLATION)
133#include "chrome/browser/plugins/plugins_resource_service.h"
134#endif
135
136#if defined(ENABLE_WEBRTC)
137#include "chrome/browser/media/webrtc_log_uploader.h"
[email protected]c027d6d2014-06-05 15:25:40138#endif
139
[email protected]3cdacd42010-04-30 18:55:53140#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
141// How often to check if the persistent instance of Chrome needs to restart
142// to install an update.
143static const int kUpdateCheckIntervalHours = 6;
144#endif
145
[email protected]6ef01ac2014-02-19 02:06:40146#if defined(USE_X11) || defined(OS_WIN)
147// How long to wait for the File thread to complete during EndSession, on Linux
148// and Windows. We have a timeout here because we're unable to run the UI
149// messageloop and there's some deadlock risk. Our only option is to exit
150// anyway.
[email protected]c6032e82010-09-13 20:06:05151static const int kEndSessionTimeoutSeconds = 10;
152#endif
153
[email protected]631bb742011-11-02 11:29:39154using content::BrowserThread;
[email protected]b9535422012-02-09 01:47:59155using content::ChildProcessSecurityPolicy;
[email protected]3a5180ae2011-12-21 02:39:38156using content::PluginService;
[email protected]ea114722012-03-12 01:11:25157using content::ResourceDispatcherHost;
[email protected]631bb742011-11-02 11:29:39158
[email protected]0de615a2012-11-08 04:40:59159BrowserProcessImpl::BrowserProcessImpl(
160 base::SequencedTaskRunner* local_state_task_runner,
161 const CommandLine& command_line)
[email protected]c3cac952014-05-09 01:51:18162 : created_watchdog_thread_(false),
[email protected]663831822012-05-23 10:19:21163 created_browser_policy_connector_(false),
initial.commit09911bf2008-07-26 23:55:29164 created_profile_manager_(false),
165 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:01166 created_icon_manager_(false),
[email protected]29672ab2009-10-30 03:44:03167 created_notification_ui_manager_(false),
[email protected]462a0ff2011-06-02 17:15:34168 created_safe_browsing_service_(false),
initial.commit09911bf2008-07-26 23:55:29169 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20170 did_start_(false),
[email protected]0de615a2012-11-08 04:40:59171 download_status_updater_(new DownloadStatusUpdater),
[email protected]ad9cfab2014-05-30 10:17:03172 local_state_task_runner_(local_state_task_runner) {
initial.commit09911bf2008-07-26 23:55:29173 g_browser_process = this;
[email protected]7cad6b0d2013-04-25 20:29:32174 platform_part_.reset(new BrowserProcessPlatformPart());
initial.commit09911bf2008-07-26 23:55:29175
[email protected]658677f2012-06-09 06:04:02176#if defined(ENABLE_PRINTING)
initial.commit09911bf2008-07-26 23:55:29177 // Must be created after the NotificationService.
178 print_job_manager_.reset(new printing::PrintJobManager);
[email protected]058e5732012-03-01 22:48:03179#endif
initial.commit09911bf2008-07-26 23:55:29180
[email protected]b2fcd0e2010-12-01 15:19:40181 net_log_.reset(new ChromeNetLog);
[email protected]3ce02412011-03-01 12:01:15182
[email protected]8add5412011-10-01 21:02:14183 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
[email protected]885c0e92012-11-13 20:27:42184 extensions::kExtensionScheme);
[email protected]b23b1b42012-05-18 19:09:16185 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
[email protected]2f3cf692013-12-19 00:51:24186 extensions::kExtensionResourceScheme);
[email protected]672c8c12013-03-07 12:30:06187 ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
188 chrome::kChromeSearchScheme);
[email protected]8add5412011-10-01 21:02:14189
[email protected]91901092013-01-31 01:53:59190#if defined(OS_MACOSX)
191 InitIdleMonitor();
192#endif
193
[email protected]cb6341e2014-06-17 03:32:02194#if defined(ENABLE_EXTENSIONS)
[email protected]01996a592013-09-13 06:55:04195 apps::AppsClient::Set(ChromeAppsClient::GetInstance());
[email protected]84b7a552014-07-19 04:52:06196
197 extension_event_router_forwarder_ = new extensions::EventRouterForwarder;
198 ExtensionRendererState::GetInstance()->Init();
[email protected]cb6341e2014-06-17 03:32:02199
[email protected]ad8b4ba2013-08-09 19:52:44200 extensions::ExtensionsClient::Set(
201 extensions::ChromeExtensionsClient::GetInstance());
[email protected]aa3dd492013-11-05 17:09:09202
203 extensions_browser_client_.reset(
204 new extensions::ChromeExtensionsBrowserClient);
205 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
[email protected]d5a74be2014-08-08 01:01:27206#endif
[email protected]aa3dd492013-11-05 17:09:09207
[email protected]356530722013-03-05 19:10:19208 message_center::MessageCenter::Initialize();
[email protected]8f4b69e2014-06-26 00:01:31209
210 omaha_query_params::OmahaQueryParams::SetDelegate(
211 ChromeOmahaQueryParamsDelegate::GetInstance());
initial.commit09911bf2008-07-26 23:55:29212}
213
214BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]2e5b60a22011-11-28 15:56:41215 tracked_objects::ThreadData::EnsureCleanupWasCalled(4);
216
217 g_browser_process = NULL;
218}
219
220void BrowserProcessImpl::StartTearDown() {
[email protected]89af4002013-09-06 07:47:07221 TRACE_EVENT0("shutdown", "BrowserProcessImpl::StartTearDown");
[email protected]c3cac952014-05-09 01:51:18222 // We need to destroy the MetricsServicesManager, IntranetRedirectDetector,
223 // PromoResourceService, and SafeBrowsing ClientSideDetectionService (owned by
224 // the SafeBrowsingService) before the io_thread_ gets destroyed, since their
225 // destructors can call the URLFetcher destructor, which does a
226 // PostDelayedTask operation on the IO thread. (The IO thread will handle that
227 // URLFetcher operation before going away.)
228 metrics_services_manager_.reset();
[email protected]c4ff4952010-01-08 19:12:47229 intranet_redirect_detector_.reset();
[email protected]6c521fed2012-11-29 17:00:03230#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
[email protected]ed304482013-01-04 04:45:32231 if (safe_browsing_service_.get())
[email protected]9efb46692011-08-23 12:56:05232 safe_browsing_service()->ShutDown();
[email protected]9efb46692011-08-23 12:56:05233#endif
initial.commit09911bf2008-07-26 23:55:29234
[email protected]d8a899c2010-09-21 04:50:33235 // Need to clear the desktop notification balloons before the io_thread_ and
236 // before the profiles, since if there are any still showing we will access
237 // those things during teardown.
238 notification_ui_manager_.reset();
239
initial.commit09911bf2008-07-26 23:55:29240 // Need to clear profiles (download managers) before the io_thread_.
[email protected]89af4002013-09-06 07:47:07241 {
242 TRACE_EVENT0("shutdown",
243 "BrowserProcessImpl::StartTearDown:ProfileManager");
[email protected]94840bb2013-12-17 19:43:38244 // The desktop User Manager needs to be closed before the guest profile
245 // can be destroyed.
[email protected]c9335412014-07-28 17:47:25246 if (switches::IsNewAvatarMenu())
[email protected]94840bb2013-12-17 19:43:38247 chrome::HideUserManager();
[email protected]89af4002013-09-06 07:47:07248 profile_manager_.reset();
249 }
initial.commit09911bf2008-07-26 23:55:29250
[email protected]81054f812012-08-30 00:47:09251#if !defined(OS_ANDROID)
initial.commit09911bf2008-07-26 23:55:29252 // Debugger must be cleaned up before IO thread and NotificationService.
[email protected]4475d232011-07-27 15:29:20253 remote_debugging_server_.reset();
[email protected]81054f812012-08-30 00:47:09254#endif
[email protected]4475d232011-07-27 15:29:20255
[email protected]84b7a552014-07-19 04:52:06256#if defined(ENABLE_EXTENSIONS)
[email protected]d5570942013-01-24 01:42:43257 ExtensionRendererState::GetInstance()->Shutdown();
[email protected]fc4facd2011-03-22 23:18:50258
[email protected]8b60ca722013-07-19 01:29:02259 media_file_system_registry_.reset();
[email protected]e4c5f97b2014-02-17 18:57:17260 // Remove the global instance of the Storage Monitor now. Otherwise the
261 // FILE thread would be gone when we try to release it in the dtor and
262 // Valgrind would report a leak on almost every single browser_test.
[email protected]8b60ca722013-07-19 01:29:02263 // TODO(gbillock): Make this unnecessary.
[email protected]7bfe94602014-02-25 10:22:39264 storage_monitor::StorageMonitor::Destroy();
[email protected]8b60ca722013-07-19 01:29:02265#endif
266
[email protected]356530722013-03-05 19:10:19267 message_center::MessageCenter::Shutdown();
[email protected]356530722013-03-05 19:10:19268
[email protected]3b19e8e2012-10-17 19:15:49269#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]985655a2011-02-23 09:54:25270 // The policy providers managed by |browser_policy_connector_| need to shut
271 // down while the IO and FILE threads are still alive.
[email protected]3b19e8e2012-10-17 19:15:49272 if (browser_policy_connector_)
273 browser_policy_connector_->Shutdown();
274#endif
[email protected]f2a893c2011-01-05 09:38:07275
[email protected]c027d6d2014-06-05 15:25:40276 // The |gcm_driver_| must shut down while the IO thread is still alive.
277 if (gcm_driver_)
278 gcm_driver_->Shutdown();
279
[email protected]8fd11832011-07-14 20:01:13280 // Stop the watchdog thread before stopping other threads.
281 watchdog_thread_.reset();
[email protected]4189bc222012-03-29 23:11:47282
283#if defined(USE_AURA)
284 // Delete aura after the metrics service has been deleted as it accesses
285 // monitor information.
286 aura::Env::DeleteInstance();
287#endif
[email protected]acd2121c2013-03-18 05:54:50288
[email protected]7cad6b0d2013-04-25 20:29:32289 platform_part()->StartTearDown();
[email protected]ab8973da2013-11-25 19:32:38290
[email protected]f3724cf2014-03-04 11:21:10291#if defined(ENABLE_WEBRTC)
[email protected]969ebea2014-03-28 16:50:26292 // Cancel any uploads to release the system url request context references.
293 if (webrtc_log_uploader_)
294 webrtc_log_uploader_->StartShutdown();
[email protected]f3724cf2014-03-04 11:21:10295#endif
296
[email protected]b4a269b2014-01-14 04:48:48297 if (local_state())
298 local_state()->CommitPendingWrite();
[email protected]2e5b60a22011-11-28 15:56:41299}
[email protected]8fd11832011-07-14 20:01:13300
[email protected]99907362012-01-11 05:41:40301void BrowserProcessImpl::PostDestroyThreads() {
302 // With the file_thread_ flushed, we can release any icon resources.
303 icon_manager_.reset();
initial.commit09911bf2008-07-26 23:55:29304
[email protected]969ebea2014-03-28 16:50:26305#if defined(ENABLE_WEBRTC)
306 // Must outlive the file thread.
307 webrtc_log_uploader_.reset();
308#endif
309
[email protected]99907362012-01-11 05:41:40310 // Reset associated state right after actual thread is stopped,
311 // as io_thread_.global_ cleanup happens in CleanUp on the IO
312 // thread, i.e. as the thread exits its message loop.
313 //
314 // This is important also because in various places, the
315 // IOThread object being NULL is considered synonymous with the
316 // IO thread having stopped.
317 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29318}
319
[email protected]b443cb042009-12-15 22:05:09320unsigned int BrowserProcessImpl::AddRefModule() {
321 DCHECK(CalledOnValidThread());
[email protected]6b4e6852012-02-14 04:02:48322
323 // CHECK(!IsShuttingDown());
324 if (IsShuttingDown()) {
325 // Copy the stacktrace which released the final reference onto our stack so
326 // it will be available in the crash report for inspection.
327 base::debug::StackTrace callstack = release_last_reference_callstack_;
328 base::debug::Alias(&callstack);
329 CHECK(false);
330 }
331
[email protected]afd20c022010-06-10 00:48:20332 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09333 module_ref_count_++;
334 return module_ref_count_;
335}
336
[email protected]294bdb32014-05-30 07:01:54337static void ShutdownServiceWorkerContext(content::StoragePartition* partition) {
338 partition->GetServiceWorkerContext()->Terminate();
339}
340
[email protected]b443cb042009-12-15 22:05:09341unsigned int BrowserProcessImpl::ReleaseModule() {
342 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18343 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09344 module_ref_count_--;
345 if (0 == module_ref_count_) {
[email protected]6b4e6852012-02-14 04:02:48346 release_last_reference_callstack_ = base::debug::StackTrace();
347
[email protected]294bdb32014-05-30 07:01:54348 // Stop service workers
349 ProfileManager* pm = profile_manager();
350 std::vector<Profile*> profiles(pm->GetLoadedProfiles());
351 for (size_t i = 0; i < profiles.size(); ++i) {
352 content::BrowserContext::ForEachStoragePartition(
353 profiles[i], base::Bind(ShutdownServiceWorkerContext));
354 }
355
[email protected]658677f2012-06-09 06:04:02356#if defined(ENABLE_PRINTING)
[email protected]6869771f2012-05-19 00:30:57357 // Wait for the pending print jobs to finish. Don't do this later, since
358 // this might cause a nested message loop to run, and we don't want pending
359 // tasks to run once teardown has started.
[email protected]3222c3262013-09-18 10:26:14360 print_job_manager_->Shutdown();
[email protected]6869771f2012-05-19 00:30:57361#endif
362
[email protected]69c019472013-10-15 05:03:47363#if defined(LEAK_SANITIZER)
364 // Check for memory leaks now, before we start shutting down threads. Doing
365 // this early means we won't report any shutdown-only leaks (as they have
366 // not yet happened at this point).
367 // If leaks are found, this will make the process exit immediately.
368 __lsan_do_leak_check();
369#endif
370
[email protected]b3a25092013-05-28 22:08:16371 CHECK(base::MessageLoop::current()->is_running());
[email protected]03d8d3e92011-09-20 06:07:11372
373#if defined(OS_MACOSX)
[email protected]b3a25092013-05-28 22:08:16374 base::MessageLoop::current()->PostTask(
[email protected]03d8d3e92011-09-20 06:07:11375 FROM_HERE,
[email protected]b3cc0542011-11-19 22:43:44376 base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop));
[email protected]03d8d3e92011-09-20 06:07:11377#endif
[email protected]b3a25092013-05-28 22:08:16378 base::MessageLoop::current()->Quit();
[email protected]b443cb042009-12-15 22:05:09379 }
380 return module_ref_count_;
381}
382
[email protected]7a6af7e2014-07-08 20:11:13383namespace {
384
385// Used at the end of session to block the UI thread for completion of sentinel
386// tasks on the set of threads used to persist profile data and local state.
387// This is done to ensure that the data has been persisted to disk before
388// continuing.
389class RundownTaskCounter :
390 public base::RefCountedThreadSafe<RundownTaskCounter> {
391 public:
392 RundownTaskCounter();
393
394 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number
395 // of times before calling TimedWait.
396 void Post(base::SequencedTaskRunner* task_runner);
397
398 // Waits until the count is zero or |max_time| has passed.
399 // This can only be called once per instance.
400 bool TimedWait(const base::TimeDelta& max_time);
401
402 private:
403 friend class base::RefCountedThreadSafe<RundownTaskCounter>;
404 ~RundownTaskCounter() {}
405
406 // Decrements the counter and releases the waitable event on transition to
407 // zero.
408 void Decrement();
409
410 // The count starts at one to defer the possibility of one->zero transitions
411 // until TimedWait is called.
412 base::AtomicRefCount count_;
413 base::WaitableEvent waitable_event_;
414
415 DISALLOW_COPY_AND_ASSIGN(RundownTaskCounter);
416};
417
418RundownTaskCounter::RundownTaskCounter()
419 : count_(1), waitable_event_(true, false) {
420}
421
422void RundownTaskCounter::Post(base::SequencedTaskRunner* task_runner) {
423 // As the count starts off at one, it should never get to zero unless
424 // TimedWait has been called.
425 DCHECK(!base::AtomicRefCountIsZero(&count_));
426
427 base::AtomicRefCountInc(&count_);
428
[email protected]ab968172014-08-07 20:03:32429 // The task must be non-nestable to guarantee that it runs after all tasks
430 // currently scheduled on |task_runner| have completed.
431 task_runner->PostNonNestableTask(FROM_HERE,
[email protected]7a6af7e2014-07-08 20:11:13432 base::Bind(&RundownTaskCounter::Decrement, this));
433}
434
435void RundownTaskCounter::Decrement() {
436 if (!base::AtomicRefCountDec(&count_))
437 waitable_event_.Signal();
438}
439
440bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) {
441 // Decrement the excess count from the constructor.
442 Decrement();
443
444 return waitable_event_.TimedWait(max_time);
445}
446
[email protected]cc2ebc72014-07-15 23:57:36447bool ExperimentUseBrokenSynchronization() {
448 // The logoff behavior used to have a race, whereby it would perform profile
449 // IO writes on the blocking thread pool, but would sycnhronize to the FILE
450 // thread. Windows feels free to terminate any process that's hidden or
451 // destroyed all it's windows, and sometimes Chrome would be terminated
452 // with pending profile IO due to this mis-synchronization.
453 // Under the "WindowsLogoffRace" experiment group, the broken behavior is
454 // emulated, in order to allow measuring what fraction of unclean shutdowns
455 // are due to this bug.
456 const std::string group_name =
457 base::FieldTrialList::FindFullName("WindowsLogoffRace");
458 return group_name == "BrokenSynchronization";
459}
460
[email protected]7a6af7e2014-07-08 20:11:13461} // namespace
462
initial.commit09911bf2008-07-26 23:55:29463void BrowserProcessImpl::EndSession() {
[email protected]cc2ebc72014-07-15 23:57:36464 bool use_broken_synchronization = ExperimentUseBrokenSynchronization();
465
initial.commit09911bf2008-07-26 23:55:29466 // Mark all the profiles as clean.
467 ProfileManager* pm = profile_manager();
[email protected]844a1002011-04-19 11:37:21468 std::vector<Profile*> profiles(pm->GetLoadedProfiles());
[email protected]7a6af7e2014-07-08 20:11:13469 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter());
470 for (size_t i = 0; i < profiles.size(); ++i) {
471 Profile* profile = profiles[i];
472 profile->SetExitType(Profile::EXIT_SESSION_ENDED);
473
[email protected]cc2ebc72014-07-15 23:57:36474 if (!use_broken_synchronization)
475 rundown_counter->Post(profile->GetIOTaskRunner());
[email protected]7a6af7e2014-07-08 20:11:13476 }
initial.commit09911bf2008-07-26 23:55:29477
478 // Tell the metrics service it was cleanly shutdown.
479 MetricsService* metrics = g_browser_process->metrics_service();
480 if (metrics && local_state()) {
initial.commit09911bf2008-07-26 23:55:29481 metrics->RecordStartOfSessionEnd();
[email protected]a6aa7542013-10-19 10:24:35482#if !defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29483 // MetricsService lazily writes to prefs, force it to write now.
[email protected]a6aa7542013-10-19 10:24:35484 // On ChromeOS, chrome gets killed when hangs, so no need to
[email protected]d6147bd2014-06-11 01:58:19485 // commit metrics::prefs::kStabilitySessionEndCompleted change immediately.
[email protected]fbe17c8a2011-12-27 16:41:48486 local_state()->CommitPendingWrite();
[email protected]7a6af7e2014-07-08 20:11:13487
[email protected]cc2ebc72014-07-15 23:57:36488 if (!use_broken_synchronization)
489 rundown_counter->Post(local_state_task_runner_);
[email protected]a6aa7542013-10-19 10:24:35490#endif
initial.commit09911bf2008-07-26 23:55:29491 }
492
[email protected]79a75b02012-04-26 18:03:58493 // http://crbug.com/125207
494 base::ThreadRestrictions::ScopedAllowWait allow_wait;
495
initial.commit09911bf2008-07-26 23:55:29496 // We must write that the profile and metrics service shutdown cleanly,
497 // otherwise on startup we'll think we crashed. So we block until done and
498 // then proceed with normal shutdown.
[email protected]7a6af7e2014-07-08 20:11:13499 //
500 // If you change the condition here, be sure to also change
501 // ProfileBrowserTests to match.
[email protected]6ef01ac2014-02-19 02:06:40502#if defined(USE_X11) || defined(OS_WIN)
[email protected]cc2ebc72014-07-15 23:57:36503 if (use_broken_synchronization) {
504 rundown_counter->Post(
505 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
506 }
507
[email protected]7a6af7e2014-07-08 20:11:13508 // Do a best-effort wait on the successful countdown of rundown tasks. Note
509 // that if we don't complete "quickly enough", Windows will terminate our
510 // process.
[email protected]6ef01ac2014-02-19 02:06:40511 //
512 // On Windows, we previously posted a message to FILE and then ran a nested
513 // message loop, waiting for that message to be processed until quitting.
514 // However, doing so means that other messages will also be processed. In
515 // particular, if the GPU process host notices that the GPU has been killed
516 // during shutdown, it races exiting the nested loop with the process host
517 // blocking the message loop attempting to re-establish a connection to the
518 // GPU process synchronously. Because the system may not be allowing
519 // processes to launch, this can result in a hang. See
520 // http://crbug.com/318527.
[email protected]7a6af7e2014-07-08 20:11:13521 rundown_counter->TimedWait(
522 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
[email protected]c6032e82010-09-13 20:06:05523#else
524 NOTIMPLEMENTED();
525#endif
initial.commit09911bf2008-07-26 23:55:29526}
527
[email protected]544246e2014-06-06 11:22:28528MetricsServicesManager* BrowserProcessImpl::GetMetricsServicesManager() {
529 DCHECK(CalledOnValidThread());
530 if (!metrics_services_manager_)
531 metrics_services_manager_.reset(new MetricsServicesManager(local_state()));
532 return metrics_services_manager_.get();
533}
534
[email protected]e13ad79b2010-07-22 21:36:50535MetricsService* BrowserProcessImpl::metrics_service() {
536 DCHECK(CalledOnValidThread());
[email protected]c3cac952014-05-09 01:51:18537 return GetMetricsServicesManager()->GetMetricsService();
[email protected]e13ad79b2010-07-22 21:36:50538}
539
[email protected]2a172e42014-02-21 04:06:10540rappor::RapporService* BrowserProcessImpl::rappor_service() {
541 DCHECK(CalledOnValidThread());
[email protected]c3cac952014-05-09 01:51:18542 return GetMetricsServicesManager()->GetRapporService();
[email protected]2a172e42014-02-21 04:06:10543}
544
[email protected]e13ad79b2010-07-22 21:36:50545IOThread* BrowserProcessImpl::io_thread() {
546 DCHECK(CalledOnValidThread());
[email protected]2e5b60a22011-11-28 15:56:41547 DCHECK(io_thread_.get());
[email protected]e13ad79b2010-07-22 21:36:50548 return io_thread_.get();
549}
550
[email protected]0b565182011-03-02 18:11:15551WatchDogThread* BrowserProcessImpl::watchdog_thread() {
552 DCHECK(CalledOnValidThread());
553 if (!created_watchdog_thread_)
554 CreateWatchdogThread();
555 DCHECK(watchdog_thread_.get() != NULL);
556 return watchdog_thread_.get();
557}
558
[email protected]e13ad79b2010-07-22 21:36:50559ProfileManager* BrowserProcessImpl::profile_manager() {
560 DCHECK(CalledOnValidThread());
561 if (!created_profile_manager_)
562 CreateProfileManager();
563 return profile_manager_.get();
564}
565
[email protected]b1de2c72013-02-06 02:45:47566PrefService* BrowserProcessImpl::local_state() {
[email protected]e13ad79b2010-07-22 21:36:50567 DCHECK(CalledOnValidThread());
568 if (!created_local_state_)
569 CreateLocalState();
570 return local_state_.get();
571}
572
[email protected]abe2c032011-03-31 18:49:34573net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
[email protected]db0e86dd2011-03-16 14:47:21574 DCHECK(CalledOnValidThread());
575 return io_thread()->system_url_request_context_getter();
576}
577
[email protected]04811242012-07-06 18:04:29578chrome_variations::VariationsService* BrowserProcessImpl::variations_service() {
[email protected]54e26c122012-05-17 15:36:21579 DCHECK(CalledOnValidThread());
[email protected]c3cac952014-05-09 01:51:18580 return GetMetricsServicesManager()->GetVariationsService();
[email protected]54e26c122012-05-17 15:36:21581}
582
[email protected]7cad6b0d2013-04-25 20:29:32583BrowserProcessPlatformPart* BrowserProcessImpl::platform_part() {
584 return platform_part_.get();
[email protected]5b869962011-09-20 19:49:33585}
[email protected]db0e86dd2011-03-16 14:47:21586
[email protected]5a38dfd2012-07-23 23:22:10587extensions::EventRouterForwarder*
[email protected]3ce02412011-03-01 12:01:15588BrowserProcessImpl::extension_event_router_forwarder() {
[email protected]84b7a552014-07-19 04:52:06589#if defined(ENABLE_EXTENSIONS)
[email protected]3ce02412011-03-01 12:01:15590 return extension_event_router_forwarder_.get();
[email protected]84b7a552014-07-19 04:52:06591#else
592 return NULL;
593#endif
[email protected]3ce02412011-03-01 12:01:15594}
595
[email protected]e13ad79b2010-07-22 21:36:50596NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
597 DCHECK(CalledOnValidThread());
598 if (!created_notification_ui_manager_)
599 CreateNotificationUIManager();
600 return notification_ui_manager_.get();
601}
602
[email protected]8393aa62013-01-23 19:40:10603message_center::MessageCenter* BrowserProcessImpl::message_center() {
604 DCHECK(CalledOnValidThread());
[email protected]356530722013-03-05 19:10:19605 return message_center::MessageCenter::Get();
[email protected]8393aa62013-01-23 19:40:10606}
[email protected]8393aa62013-01-23 19:40:10607
[email protected]985655a2011-02-23 09:54:25608policy::BrowserPolicyConnector* BrowserProcessImpl::browser_policy_connector() {
[email protected]f2a893c2011-01-05 09:38:07609 DCHECK(CalledOnValidThread());
[email protected]f31e2e52011-07-14 16:01:19610#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]3b19e8e2012-10-17 19:15:49611 if (!created_browser_policy_connector_) {
[email protected]3b19e8e2012-10-17 19:15:49612 DCHECK(!browser_policy_connector_);
[email protected]97275822014-01-21 19:30:36613 browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
[email protected]371e3ed2012-05-23 18:04:28614 created_browser_policy_connector_ = true;
[email protected]f2a893c2011-01-05 09:38:07615 }
[email protected]985655a2011-02-23 09:54:25616 return browser_policy_connector_.get();
[email protected]3b19e8e2012-10-17 19:15:49617#else
618 return NULL;
619#endif
[email protected]f2a893c2011-01-05 09:38:07620}
621
[email protected]a4179c2a2012-02-09 18:14:21622policy::PolicyService* BrowserProcessImpl::policy_service() {
623#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]3b19e8e2012-10-17 19:15:49624 return browser_policy_connector()->GetPolicyService();
[email protected]a4179c2a2012-02-09 18:14:21625#else
[email protected]3b19e8e2012-10-17 19:15:49626 if (!policy_service_.get())
[email protected]243ec0782012-02-14 21:03:02627 policy_service_.reset(new policy::PolicyServiceStub());
[email protected]8d6b2432012-03-31 12:11:18628 return policy_service_.get();
[email protected]3b19e8e2012-10-17 19:15:49629#endif
[email protected]a4179c2a2012-02-09 18:14:21630}
631
[email protected]e13ad79b2010-07-22 21:36:50632IconManager* BrowserProcessImpl::icon_manager() {
633 DCHECK(CalledOnValidThread());
634 if (!created_icon_manager_)
635 CreateIconManager();
636 return icon_manager_.get();
637}
638
[email protected]a9830b52012-12-17 23:30:36639GLStringManager* BrowserProcessImpl::gl_string_manager() {
640 DCHECK(CalledOnValidThread());
641 if (!gl_string_manager_.get())
642 gl_string_manager_.reset(new GLStringManager());
643 return gl_string_manager_.get();
644}
645
[email protected]e6933d2a2013-03-12 22:32:21646GpuModeManager* BrowserProcessImpl::gpu_mode_manager() {
[email protected]8bbe3a92013-05-12 00:58:35647 DCHECK(CalledOnValidThread());
648 if (!gpu_mode_manager_.get())
649 gpu_mode_manager_.reset(new GpuModeManager());
[email protected]e6933d2a2013-03-12 22:32:21650 return gpu_mode_manager_.get();
651}
652
[email protected]76698ea2012-08-24 20:50:37653void BrowserProcessImpl::CreateDevToolsHttpProtocolHandler(
[email protected]55d342402013-02-22 11:28:06654 chrome::HostDesktopType host_desktop_type,
[email protected]01000cf12011-02-04 11:39:22655 const std::string& ip,
[email protected]d9ce37d22014-03-12 05:06:29656 int port) {
[email protected]e13ad79b2010-07-22 21:36:50657 DCHECK(CalledOnValidThread());
[email protected]81054f812012-08-30 00:47:09658#if !defined(OS_ANDROID)
[email protected]76698ea2012-08-24 20:50:37659 // StartupBrowserCreator::LaunchBrowser can be run multiple times when browser
660 // is started with several profiles or existing browser process is reused.
661 if (!remote_debugging_server_.get()) {
662 remote_debugging_server_.reset(
[email protected]d9ce37d22014-03-12 05:06:29663 new RemoteDebuggingServer(host_desktop_type, ip, port));
[email protected]76698ea2012-08-24 20:50:37664 }
[email protected]81054f812012-08-30 00:47:09665#endif
[email protected]5613126e2011-01-31 15:27:55666}
667
[email protected]e13ad79b2010-07-22 21:36:50668bool BrowserProcessImpl::IsShuttingDown() {
669 DCHECK(CalledOnValidThread());
670 return did_start_ && 0 == module_ref_count_;
671}
672
initial.commit09911bf2008-07-26 23:55:29673printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
[email protected]3222c3262013-09-18 10:26:14674 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29675 return print_job_manager_.get();
676}
677
[email protected]d8ce31e2012-12-19 05:09:21678printing::PrintPreviewDialogController*
679 BrowserProcessImpl::print_preview_dialog_controller() {
[email protected]1e54c1c2013-08-12 17:16:05680#if defined(ENABLE_FULL_PRINTING)
[email protected]dbeebd52010-11-16 20:34:16681 DCHECK(CalledOnValidThread());
[email protected]d8ce31e2012-12-19 05:09:21682 if (!print_preview_dialog_controller_.get())
683 CreatePrintPreviewDialogController();
684 return print_preview_dialog_controller_.get();
[email protected]658677f2012-06-09 06:04:02685#else
686 NOTIMPLEMENTED();
687 return NULL;
[email protected]058e5732012-03-01 22:48:03688#endif
[email protected]dbeebd52010-11-16 20:34:16689}
690
[email protected]68f88b992011-05-07 02:01:39691printing::BackgroundPrintingManager*
692 BrowserProcessImpl::background_printing_manager() {
[email protected]1e54c1c2013-08-12 17:16:05693#if defined(ENABLE_FULL_PRINTING)
[email protected]68f88b992011-05-07 02:01:39694 DCHECK(CalledOnValidThread());
695 if (!background_printing_manager_.get())
696 CreateBackgroundPrintingManager();
697 return background_printing_manager_.get();
[email protected]658677f2012-06-09 06:04:02698#else
699 NOTIMPLEMENTED();
700 return NULL;
[email protected]058e5732012-03-01 22:48:03701#endif
[email protected]68f88b992011-05-07 02:01:39702}
703
[email protected]e13ad79b2010-07-22 21:36:50704IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
705 DCHECK(CalledOnValidThread());
706 if (!intranet_redirect_detector_.get())
707 CreateIntranetRedirectDetector();
708 return intranet_redirect_detector_.get();
709}
710
711const std::string& BrowserProcessImpl::GetApplicationLocale() {
712 DCHECK(!locale_.empty());
713 return locale_;
714}
715
716void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
717 locale_ = locale;
718 extension_l10n_util::SetProcessLocale(locale);
[email protected]eabbfb12013-04-05 23:28:35719 chrome::ChromeContentBrowserClient::SetApplicationLocale(locale);
[email protected]e5e72cc2014-07-22 19:06:49720 translate::TranslateDownloadManager::GetInstance()->set_application_locale(
721 locale);
[email protected]e13ad79b2010-07-22 21:36:50722}
723
[email protected]073ed7b2010-09-27 09:20:02724DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
[email protected]7a0e72f2011-09-20 19:59:11725 return download_status_updater_.get();
[email protected]073ed7b2010-09-27 09:20:02726}
727
[email protected]95951992013-09-13 00:37:54728MediaFileSystemRegistry* BrowserProcessImpl::media_file_system_registry() {
[email protected]84b7a552014-07-19 04:52:06729#if defined(ENABLE_EXTENSIONS)
[email protected]42aec8df2012-12-12 06:03:57730 if (!media_file_system_registry_)
[email protected]95951992013-09-13 00:37:54731 media_file_system_registry_.reset(new MediaFileSystemRegistry());
[email protected]42aec8df2012-12-12 06:03:57732 return media_file_system_registry_.get();
[email protected]84b7a552014-07-19 04:52:06733#else
734 return NULL;
[email protected]e8262a52013-04-24 02:02:51735#endif
[email protected]42aec8df2012-12-12 06:03:57736}
737
[email protected]16951ead2013-03-27 06:44:44738bool BrowserProcessImpl::created_local_state() const {
[email protected]84b7a552014-07-19 04:52:06739 return created_local_state_;
[email protected]16951ead2013-03-27 06:44:44740}
741
[email protected]bbf32c52013-06-03 14:05:52742#if defined(ENABLE_WEBRTC)
743WebRtcLogUploader* BrowserProcessImpl::webrtc_log_uploader() {
744 if (!webrtc_log_uploader_.get())
745 webrtc_log_uploader_.reset(new WebRtcLogUploader());
746 return webrtc_log_uploader_.get();
747}
748#endif
749
[email protected]d0ab865b2014-06-04 19:41:34750network_time::NetworkTimeTracker* BrowserProcessImpl::network_time_tracker() {
[email protected]ad9cfab2014-05-30 10:17:03751 if (!network_time_tracker_) {
[email protected]d0ab865b2014-06-04 19:41:34752 network_time_tracker_.reset(new network_time::NetworkTimeTracker(
[email protected]ad9cfab2014-05-30 10:17:03753 scoped_ptr<base::TickClock>(new base::DefaultTickClock()),
754 local_state()));
755 }
[email protected]72f93712014-05-19 15:10:39756 return network_time_tracker_.get();
757}
758
[email protected]c027d6d2014-06-05 15:25:40759gcm::GCMDriver* BrowserProcessImpl::gcm_driver() {
760 DCHECK(CalledOnValidThread());
761 if (!gcm_driver_)
762 CreateGCMDriver();
763 return gcm_driver_.get();
764}
765
[email protected]b1de2c72013-02-06 02:45:47766// static
767void BrowserProcessImpl::RegisterPrefs(PrefRegistrySimple* registry) {
768 registry->RegisterBooleanPref(prefs::kDefaultBrowserSettingEnabled,
769 false);
770 // This policy needs to be defined before the net subsystem is initialized,
771 // so we do it here.
772 registry->RegisterIntegerPref(prefs::kMaxConnectionsPerProxy,
773 net::kDefaultMaxSocketsPerProxyServer);
774
[email protected]b1de2c72013-02-06 02:45:47775 registry->RegisterBooleanPref(prefs::kAllowCrossOriginAuthPrompt, false);
776
[email protected]b8e70ea2014-08-14 12:36:43777 registry->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled, true);
778
[email protected]b4b709a2013-04-19 21:08:14779#if defined(OS_CHROMEOS) || defined(OS_ANDROID) || defined(OS_IOS)
780 registry->RegisterBooleanPref(prefs::kEulaAccepted, false);
781#endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) || defined(OS_IOS)
[email protected]b1de2c72013-02-06 02:45:47782#if defined(OS_WIN)
[email protected]d51373d2013-10-31 15:22:00783 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
784 registry->RegisterStringPref(prefs::kRelaunchMode,
785 upgrade_util::kRelaunchModeDefault);
786 }
[email protected]b1de2c72013-02-06 02:45:47787#endif
788
789 // TODO(brettw,*): this comment about ResourceBundle was here since
790 // initial commit. This comment seems unrelated, bit-rotten and
791 // a candidate for removal.
792 // Initialize ResourceBundle which handles files loaded from external
793 // sources. This has to be done before uninstall code path and before prefs
794 // are registered.
795 registry->RegisterStringPref(prefs::kApplicationLocale, std::string());
796#if defined(OS_CHROMEOS)
797 registry->RegisterStringPref(prefs::kOwnerLocale, std::string());
798 registry->RegisterStringPref(prefs::kHardwareKeyboardLayout,
799 std::string());
800#endif // defined(OS_CHROMEOS)
801#if !defined(OS_CHROMEOS)
802 registry->RegisterBooleanPref(
803 prefs::kMetricsReportingEnabled,
804 GoogleUpdateSettings::GetCollectStatsConsent());
805#endif // !defined(OS_CHROMEOS)
[email protected]1505e012013-05-09 23:23:47806
807#if defined(OS_ANDROID)
808 registry->RegisterBooleanPref(
809 prefs::kCrashReportingEnabled, false);
[email protected]ffaf7322013-05-15 00:26:11810#endif // defined(OS_ANDROID)
[email protected]b1de2c72013-02-06 02:45:47811}
812
[email protected]254ed742011-08-16 18:45:27813DownloadRequestLimiter* BrowserProcessImpl::download_request_limiter() {
814 DCHECK(CalledOnValidThread());
[email protected]dc24976f2013-06-02 21:15:09815 if (!download_request_limiter_.get())
[email protected]254ed742011-08-16 18:45:27816 download_request_limiter_ = new DownloadRequestLimiter();
[email protected]dc24976f2013-06-02 21:15:09817 return download_request_limiter_.get();
[email protected]254ed742011-08-16 18:45:27818}
819
[email protected]9e7f015f2011-05-28 00:24:25820BackgroundModeManager* BrowserProcessImpl::background_mode_manager() {
821 DCHECK(CalledOnValidThread());
[email protected]7cee1902012-08-23 00:02:45822#if defined(ENABLE_BACKGROUND)
[email protected]9e7f015f2011-05-28 00:24:25823 if (!background_mode_manager_.get())
824 CreateBackgroundModeManager();
825 return background_mode_manager_.get();
[email protected]7cee1902012-08-23 00:02:45826#else
827 NOTIMPLEMENTED();
828 return NULL;
829#endif
[email protected]9e7f015f2011-05-28 00:24:25830}
831
[email protected]e178ad92013-06-28 02:29:25832void BrowserProcessImpl::set_background_mode_manager_for_test(
833 scoped_ptr<BackgroundModeManager> manager) {
834 background_mode_manager_ = manager.Pass();
835}
836
[email protected]9e7f015f2011-05-28 00:24:25837StatusTray* BrowserProcessImpl::status_tray() {
838 DCHECK(CalledOnValidThread());
839 if (!status_tray_.get())
840 CreateStatusTray();
841 return status_tray_.get();
842}
843
[email protected]462a0ff2011-06-02 17:15:34844
845SafeBrowsingService* BrowserProcessImpl::safe_browsing_service() {
846 DCHECK(CalledOnValidThread());
847 if (!created_safe_browsing_service_)
848 CreateSafeBrowsingService();
849 return safe_browsing_service_.get();
850}
851
[email protected]a7a5e992010-12-09 23:39:51852safe_browsing::ClientSideDetectionService*
853 BrowserProcessImpl::safe_browsing_detection_service() {
854 DCHECK(CalledOnValidThread());
[email protected]9efb46692011-08-23 12:56:05855 if (safe_browsing_service())
856 return safe_browsing_service()->safe_browsing_detection_service();
857 return NULL;
[email protected]a7a5e992010-12-09 23:39:51858}
859
[email protected]e13ad79b2010-07-22 21:36:50860#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
861void BrowserProcessImpl::StartAutoupdateTimer() {
[email protected]d323a172011-09-02 18:23:02862 autoupdate_timer_.Start(FROM_HERE,
[email protected]e13ad79b2010-07-22 21:36:50863 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
864 this,
865 &BrowserProcessImpl::OnAutoupdateTimer);
866}
867#endif
868
[email protected]d6f37fc2011-02-13 23:58:41869ChromeNetLog* BrowserProcessImpl::net_log() {
870 return net_log_.get();
871}
872
[email protected]1459fb62011-05-25 19:03:27873prerender::PrerenderTracker* BrowserProcessImpl::prerender_tracker() {
874 if (!prerender_tracker_.get())
875 prerender_tracker_.reset(new prerender::PrerenderTracker);
876
877 return prerender_tracker_.get();
878}
879
[email protected]055981f2014-01-17 20:22:32880component_updater::ComponentUpdateService*
881BrowserProcessImpl::component_updater() {
[email protected]c1adf5a2011-08-03 22:11:37882 if (!component_updater_.get()) {
[email protected]ad94f722013-11-02 01:57:36883 if (!BrowserThread::CurrentlyOn(BrowserThread::UI))
884 return NULL;
[email protected]655043812014-06-24 01:50:36885 component_updater::Configurator* configurator =
[email protected]055981f2014-01-17 20:22:32886 component_updater::MakeChromeComponentUpdaterConfigurator(
[email protected]c1adf5a2011-08-03 22:11:37887 CommandLine::ForCurrentProcess(),
888 io_thread()->system_url_request_context_getter());
889 // Creating the component updater does not do anything, components
890 // need to be registered and Start() needs to be called.
891 component_updater_.reset(ComponentUpdateServiceFactory(configurator));
892 }
893 return component_updater_.get();
[email protected]aafbcb572011-08-03 22:22:32894}
[email protected]c1adf5a2011-08-03 22:11:37895
[email protected]ca167a32011-09-30 15:19:11896CRLSetFetcher* BrowserProcessImpl::crl_set_fetcher() {
[email protected]ed304482013-01-04 04:45:32897 if (!crl_set_fetcher_.get())
[email protected]ca167a32011-09-30 15:19:11898 crl_set_fetcher_ = new CRLSetFetcher();
[email protected]ca167a32011-09-30 15:19:11899 return crl_set_fetcher_.get();
[email protected]9f108f22013-04-09 00:39:56900}
901
[email protected]055981f2014-01-17 20:22:32902component_updater::PnaclComponentInstaller*
903BrowserProcessImpl::pnacl_component_installer() {
904 if (!pnacl_component_installer_.get()) {
905 pnacl_component_installer_.reset(
906 new component_updater::PnaclComponentInstaller());
907 }
[email protected]9f108f22013-04-09 00:39:56908 return pnacl_component_installer_.get();
[email protected]ca167a32011-09-30 15:19:11909}
910
[email protected]99907362012-01-11 05:41:40911void BrowserProcessImpl::ResourceDispatcherHostCreated() {
[email protected]8bcdf072011-06-03 16:52:15912 resource_dispatcher_host_delegate_.reset(
[email protected]ea114722012-03-12 01:11:25913 new ChromeResourceDispatcherHostDelegate(prerender_tracker()));
914 ResourceDispatcherHost::Get()->SetDelegate(
915 resource_dispatcher_host_delegate_.get());
[email protected]40850a52011-05-26 22:07:35916
[email protected]cf5a95822012-11-13 14:44:01917 pref_change_registrar_.Add(
918 prefs::kAllowCrossOriginAuthPrompt,
919 base::Bind(&BrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy,
920 base::Unretained(this)));
[email protected]40850a52011-05-26 22:07:35921 ApplyAllowCrossOriginAuthPromptPolicy();
initial.commit09911bf2008-07-26 23:55:29922}
923
[email protected]0b565182011-03-02 18:11:15924void BrowserProcessImpl::CreateWatchdogThread() {
925 DCHECK(!created_watchdog_thread_ && watchdog_thread_.get() == NULL);
926 created_watchdog_thread_ = true;
927
928 scoped_ptr<WatchDogThread> thread(new WatchDogThread());
[email protected]1c8fcb52014-06-26 22:36:32929 base::Thread::Options options;
930 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
931 if (!thread->StartWithOptions(options))
[email protected]0b565182011-03-02 18:11:15932 return;
933 watchdog_thread_.swap(thread);
934}
935
initial.commit09911bf2008-07-26 23:55:29936void BrowserProcessImpl::CreateProfileManager() {
937 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
938 created_profile_manager_ = true;
939
[email protected]650b2d52013-02-10 03:41:45940 base::FilePath user_data_dir;
[email protected]49a25632011-08-31 17:03:48941 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
942 profile_manager_.reset(new ProfileManager(user_data_dir));
initial.commit09911bf2008-07-26 23:55:29943}
944
945void BrowserProcessImpl::CreateLocalState() {
946 DCHECK(!created_local_state_ && local_state_.get() == NULL);
947 created_local_state_ = true;
948
[email protected]650b2d52013-02-10 03:41:45949 base::FilePath local_state_path;
[email protected]0de615a2012-11-08 04:40:59950 CHECK(PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path));
[email protected]b1de2c72013-02-06 02:45:47951 scoped_refptr<PrefRegistrySimple> pref_registry = new PrefRegistrySimple;
[email protected]4082306d92013-02-21 14:51:01952
953 // Register local state preferences.
[email protected]dc24976f2013-06-02 21:15:09954 chrome::RegisterLocalState(pref_registry.get());
[email protected]4082306d92013-02-21 14:51:01955
[email protected]e90a01f2013-11-19 04:22:12956 local_state_ =
[email protected]5b199522012-12-22 17:24:44957 chrome_prefs::CreateLocalState(local_state_path,
[email protected]dc24976f2013-06-02 21:15:09958 local_state_task_runner_.get(),
[email protected]5b199522012-12-22 17:24:44959 policy_service(),
[email protected]b1de2c72013-02-06 02:45:47960 pref_registry,
[email protected]e90a01f2013-11-19 04:22:12961 false).Pass();
[email protected]492d2142010-09-10 13:55:18962
[email protected]2a7e7c152010-10-01 20:12:09963 pref_change_registrar_.Init(local_state_.get());
964
[email protected]e450fa62011-02-01 12:52:56965 // Initialize the notification for the default browser setting policy.
[email protected]cf5a95822012-11-13 14:44:01966 pref_change_registrar_.Add(
967 prefs::kDefaultBrowserSettingEnabled,
968 base::Bind(&BrowserProcessImpl::ApplyDefaultBrowserPolicy,
969 base::Unretained(this)));
[email protected]8b08a47f2011-02-25 12:36:37970
[email protected]459158c2013-11-14 00:25:15971 // This preference must be kept in sync with external values; update them
972 // whenever the preference or its controlling policy changes.
973#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
974 pref_change_registrar_.Add(
975 prefs::kMetricsReportingEnabled,
976 base::Bind(&BrowserProcessImpl::ApplyMetricsReportingPolicy,
977 base::Unretained(this)));
[email protected]459158c2013-11-14 00:25:15978#endif
979
[email protected]12c84e22011-07-11 09:35:45980 int max_per_proxy = local_state_->GetInteger(prefs::kMaxConnectionsPerProxy);
981 net::ClientSocketPoolManager::set_max_sockets_per_proxy_server(
[email protected]a8af2152012-03-21 20:29:52982 net::HttpNetworkSession::NORMAL_SOCKET_POOL,
[email protected]12c84e22011-07-11 09:35:45983 std::max(std::min(max_per_proxy, 99),
[email protected]a8af2152012-03-21 20:29:52984 net::ClientSocketPoolManager::max_sockets_per_group(
985 net::HttpNetworkSession::NORMAL_SOCKET_POOL)));
[email protected]fd6159a2010-09-03 09:38:39986}
initial.commit09911bf2008-07-26 23:55:29987
[email protected]99907362012-01-11 05:41:40988void BrowserProcessImpl::PreCreateThreads() {
[email protected]84b7a552014-07-19 04:52:06989 io_thread_.reset(
990 new IOThread(local_state(), policy_service(), net_log_.get(),
991 extension_event_router_forwarder()));
[email protected]99907362012-01-11 05:41:40992}
993
[email protected]d2caaa22011-12-12 03:04:15994void BrowserProcessImpl::PreMainMessageLoopRun() {
[email protected]362d694f2013-02-28 09:07:24995#if defined(ENABLE_CONFIGURATION_POLICY)
996 // browser_policy_connector() is created very early because local_state()
997 // needs policy to be initialized with the managed preference values.
998 // However, policy fetches from the network and loading of disk caches
999 // requires that threads are running; this Init() call lets the connector
1000 // resume its initialization now that the loops are spinning and the
1001 // system request context is available for the fetchers.
1002 browser_policy_connector()->Init(local_state(), system_request_context());
1003#endif
1004
[email protected]0ca957872013-01-03 20:24:041005 if (local_state_->IsManagedPreference(prefs::kDefaultBrowserSettingEnabled))
1006 ApplyDefaultBrowserPolicy();
1007
[email protected]fc0cd312014-01-13 14:25:541008#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
1009 ApplyMetricsReportingPolicy();
1010#endif
1011
[email protected]ebd71962012-12-20 02:56:551012#if defined(ENABLE_PLUGINS)
[email protected]99907362012-01-11 05:41:401013 PluginService* plugin_service = PluginService::GetInstance();
1014 plugin_service->SetFilter(ChromePluginServiceFilter::GetInstance());
1015 plugin_service->StartWatchingPlugins();
1016
[email protected]99907362012-01-11 05:41:401017#if defined(OS_POSIX)
1018 // Also find plugins in a user-specific plugins dir,
1019 // e.g. ~/.config/chromium/Plugins.
[email protected]e87ce862013-06-12 17:30:481020 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
1021 if (!cmd_line.HasSwitch(switches::kDisablePluginsDiscovery)) {
1022 base::FilePath user_data_dir;
1023 if (PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
1024 plugin_service->AddExtraPluginDir(user_data_dir.Append("Plugins"));
1025 }
[email protected]99907362012-01-11 05:41:401026#endif
1027
[email protected]de75c702012-09-25 23:06:021028 // Triggers initialization of the singleton instance on UI thread.
1029 PluginFinder::GetInstance()->Init();
[email protected]2366a3a2012-10-02 20:41:141030
1031#if defined(ENABLE_PLUGIN_INSTALLATION)
[email protected]ed304482013-01-04 04:45:321032 DCHECK(!plugins_resource_service_.get());
1033 plugins_resource_service_ = new PluginsResourceService(local_state());
[email protected]97eddfe2013-02-12 19:16:301034 plugins_resource_service_->Init();
[email protected]2366a3a2012-10-02 20:41:141035#endif
[email protected]cd34acd2013-01-04 05:04:491036#endif // defined(ENABLE_PLUGINS)
[email protected]ed304482013-01-04 04:45:321037
1038 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1039 if (!command_line.HasSwitch(switches::kDisableWebResources)) {
1040 DCHECK(!promo_resource_service_.get());
1041 promo_resource_service_ = new PromoResourceService;
1042 promo_resource_service_->StartAfterDelay();
1043 }
[email protected]195c99c2012-10-31 06:24:511044
[email protected]8b60ca722013-07-19 01:29:021045#if !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]7bfe94602014-02-25 10:22:391046 storage_monitor::StorageMonitor::Create();
[email protected]8b60ca722013-07-19 01:29:021047#endif
1048
[email protected]90a4b9d2013-07-31 12:14:401049 platform_part_->PreMainMessageLoopRun();
[email protected]d2caaa22011-12-12 03:04:151050}
1051
initial.commit09911bf2008-07-26 23:55:291052void BrowserProcessImpl::CreateIconManager() {
1053 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
1054 created_icon_manager_ = true;
1055 icon_manager_.reset(new IconManager);
1056}
1057
[email protected]c4ff4952010-01-08 19:12:471058void BrowserProcessImpl::CreateIntranetRedirectDetector() {
1059 DCHECK(intranet_redirect_detector_.get() == NULL);
1060 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
1061 new IntranetRedirectDetector);
1062 intranet_redirect_detector_.swap(intranet_redirect_detector);
1063}
1064
[email protected]29672ab2009-10-30 03:44:031065void BrowserProcessImpl::CreateNotificationUIManager() {
[email protected]29cebbd2012-06-06 21:43:421066#if defined(ENABLE_NOTIFICATIONS)
[email protected]29672ab2009-10-30 03:44:031067 DCHECK(notification_ui_manager_.get() == NULL);
[email protected]b0b2a3dd2011-01-06 00:30:051068 notification_ui_manager_.reset(NotificationUIManager::Create(local_state()));
[email protected]29672ab2009-10-30 03:44:031069 created_notification_ui_manager_ = true;
[email protected]29cebbd2012-06-06 21:43:421070#endif
[email protected]29672ab2009-10-30 03:44:031071}
1072
[email protected]9e7f015f2011-05-28 00:24:251073void BrowserProcessImpl::CreateBackgroundModeManager() {
1074 DCHECK(background_mode_manager_.get() == NULL);
1075 background_mode_manager_.reset(
[email protected]279170832011-10-12 23:38:031076 new BackgroundModeManager(CommandLine::ForCurrentProcess(),
1077 &profile_manager()->GetProfileInfoCache()));
[email protected]9e7f015f2011-05-28 00:24:251078}
1079
1080void BrowserProcessImpl::CreateStatusTray() {
1081 DCHECK(status_tray_.get() == NULL);
1082 status_tray_.reset(StatusTray::Create());
1083}
1084
[email protected]d8ce31e2012-12-19 05:09:211085void BrowserProcessImpl::CreatePrintPreviewDialogController() {
[email protected]1e54c1c2013-08-12 17:16:051086#if defined(ENABLE_FULL_PRINTING)
[email protected]d8ce31e2012-12-19 05:09:211087 DCHECK(print_preview_dialog_controller_.get() == NULL);
1088 print_preview_dialog_controller_ =
1089 new printing::PrintPreviewDialogController();
[email protected]658677f2012-06-09 06:04:021090#else
1091 NOTIMPLEMENTED();
[email protected]058e5732012-03-01 22:48:031092#endif
[email protected]dbeebd52010-11-16 20:34:161093}
1094
[email protected]68f88b992011-05-07 02:01:391095void BrowserProcessImpl::CreateBackgroundPrintingManager() {
[email protected]1e54c1c2013-08-12 17:16:051096#if defined(ENABLE_FULL_PRINTING)
[email protected]68f88b992011-05-07 02:01:391097 DCHECK(background_printing_manager_.get() == NULL);
1098 background_printing_manager_.reset(new printing::BackgroundPrintingManager());
[email protected]658677f2012-06-09 06:04:021099#else
1100 NOTIMPLEMENTED();
1101#endif
[email protected]68f88b992011-05-07 02:01:391102}
1103
[email protected]462a0ff2011-06-02 17:15:341104void BrowserProcessImpl::CreateSafeBrowsingService() {
1105 DCHECK(safe_browsing_service_.get() == NULL);
[email protected]8c40da62011-07-13 22:58:461106 // Set this flag to true so that we don't retry indefinitely to
1107 // create the service class if there was an error.
[email protected]462a0ff2011-06-02 17:15:341108 created_safe_browsing_service_ = true;
[email protected]6c521fed2012-11-29 17:00:031109#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
[email protected]462a0ff2011-06-02 17:15:341110 safe_browsing_service_ = SafeBrowsingService::CreateSafeBrowsingService();
1111 safe_browsing_service_->Initialize();
[email protected]4b58e7d2011-07-11 10:22:561112#endif
[email protected]462a0ff2011-06-02 17:15:341113}
1114
[email protected]c027d6d2014-06-05 15:25:401115void BrowserProcessImpl::CreateGCMDriver() {
1116 DCHECK(!gcm_driver_);
1117
1118#if defined(OS_ANDROID)
1119 gcm_driver_.reset(new gcm::GCMDriverAndroid);
1120#else
1121 base::FilePath store_path;
1122 CHECK(PathService::Get(chrome::DIR_GLOBAL_GCM_STORE, &store_path));
1123 gcm_driver_ = gcm::CreateGCMDriverDesktop(
1124 make_scoped_ptr(new gcm::GCMClientFactory),
[email protected]c027d6d2014-06-05 15:25:401125 store_path,
1126 system_request_context());
[email protected]da54623f2014-06-17 17:03:551127 // Sign-in is not required for device-level GCM usage. So we just call
1128 // OnSignedIn to assume always signed-in. Note that GCM will not be started
1129 // at this point since no one has asked for it yet.
1130 // TODO(jianli): To be removed when sign-in enforcement is dropped.
1131 gcm_driver_->OnSignedIn();
[email protected]c027d6d2014-06-05 15:25:401132#endif // defined(OS_ANDROID)
1133}
1134
[email protected]b61f62a2011-11-01 02:02:101135void BrowserProcessImpl::ApplyDefaultBrowserPolicy() {
[email protected]ed304482013-01-04 04:45:321136 if (local_state()->GetBoolean(prefs::kDefaultBrowserSettingEnabled)) {
[email protected]b61f62a2011-11-01 02:02:101137 scoped_refptr<ShellIntegration::DefaultWebClientWorker>
1138 set_browser_worker = new ShellIntegration::DefaultBrowserWorker(NULL);
1139 set_browser_worker->StartSetAsDefault();
1140 }
1141}
1142
[email protected]40850a52011-05-26 22:07:351143void BrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy() {
1144 bool value = local_state()->GetBoolean(prefs::kAllowCrossOriginAuthPrompt);
[email protected]ea114722012-03-12 01:11:251145 ResourceDispatcherHost::Get()->SetAllowCrossOriginAuthPrompt(value);
[email protected]40850a52011-05-26 22:07:351146}
1147
[email protected]459158c2013-11-14 00:25:151148void BrowserProcessImpl::ApplyMetricsReportingPolicy() {
1149#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]fc0cd312014-01-13 14:25:541150 CHECK(BrowserThread::PostTask(
[email protected]a178ed22013-11-17 15:55:391151 BrowserThread::FILE, FROM_HERE,
1152 base::Bind(
1153 base::IgnoreResult(&GoogleUpdateSettings::SetCollectStatsConsent),
[email protected]fc0cd312014-01-13 14:25:541154 local_state()->GetBoolean(prefs::kMetricsReportingEnabled))));
[email protected]459158c2013-11-14 00:25:151155#endif
1156}
1157
[email protected]3cdacd42010-04-30 18:55:531158// Mac is currently not supported.
1159#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:421160
1161bool BrowserProcessImpl::CanAutorestartForUpdate() const {
1162 // Check if browser is in the background and if it needs to be restarted to
1163 // apply a pending update.
[email protected]0665ebe2013-02-13 09:53:191164 return chrome::GetTotalBrowserCount() == 0 && chrome::WillKeepAlive() &&
[email protected]815856722011-04-13 17:19:191165 upgrade_util::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:421166}
1167
[email protected]5c08f222010-09-22 09:37:211168// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:471169const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:401170 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:421171};
1172
[email protected]f6e6d2e2011-10-28 18:39:221173void BrowserProcessImpl::RestartBackgroundInstance() {
[email protected]bd48c2b02010-04-09 20:32:421174 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:531175 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:421176
1177 std::map<std::string, CommandLine::StringType> switches =
1178 old_cl->GetSwitches();
1179
[email protected]5c08f222010-09-22 09:37:211180 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:421181
1182 // Append the rest of the switches (along with their values, if any)
1183 // to the new command line
1184 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
1185 switches.begin(); i != switches.end(); ++i) {
1186 CommandLine::StringType switch_value = i->second;
1187 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:571188 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:421189 } else {
[email protected]3cdacd42010-04-30 18:55:531190 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:421191 }
1192 }
1193
[email protected]6c10c972010-08-11 04:15:471194 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:211195 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:471196 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
1197 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
1198 }
[email protected]bd48c2b02010-04-09 20:32:421199
[email protected]3cdacd42010-04-30 18:55:531200 DLOG(WARNING) << "Shutting down current instance of the browser.";
[email protected]0c98ab652013-02-18 00:39:371201 chrome::AttemptExit();
[email protected]3cdacd42010-04-30 18:55:531202
1203 // Transfer ownership to Upgrade.
[email protected]815856722011-04-13 17:19:191204 upgrade_util::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:421205}
1206
1207void BrowserProcessImpl::OnAutoupdateTimer() {
1208 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:531209 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]f6e6d2e2011-10-28 18:39:221210 RestartBackgroundInstance();
[email protected]bd48c2b02010-04-09 20:32:421211 }
1212}
1213
[email protected]3cdacd42010-04-30 18:55:531214#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)