1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/browser_process_platform_part_ash.h"
10 #include "base/check_op.h"
11 #include "base/functional/bind.h"
12 #include "base/memory/singleton.h"
13 #include "base/time/default_clock.h"
14 #include "base/time/default_tick_clock.h"
15 #include "base/time/tick_clock.h"
16 #include "chrome/browser/ash/crosapi/browser_manager.h"
17 #include "chrome/browser/ash/login/saml/in_session_password_change_manager.h"
18 #include "chrome/browser/ash/login/session/chrome_session_manager.h"
19 #include "chrome/browser/ash/login/users/chrome_user_manager_impl.h"
20 #include "chrome/browser/ash/net/ash_proxy_monitor.h"
21 #include "chrome/browser/ash/net/delay_network_call.h"
22 #include "chrome/browser/ash/net/system_proxy_manager.h"
23 #include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
24 #include "chrome/browser/ash/profiles/profile_helper.h"
25 #include "chrome/browser/ash/scheduler_configuration_manager.h"
26 #include "chrome/browser/ash/settings/cros_settings.h"
27 #include "chrome/browser/ash/system/automatic_reboot_manager.h"
28 #include "chrome/browser/ash/system/device_disabling_manager.h"
29 #include "chrome/browser/ash/system/device_disabling_manager_default_delegate.h"
30 #include "chrome/browser/ash/system/system_clock.h"
31 #include "chrome/browser/ash/system/timezone_resolver_manager.h"
32 #include "chrome/browser/ash/system/timezone_util.h"
33 #include "chrome/browser/browser_process.h"
34 #include "chrome/browser/component_updater/metadata_table_chromeos.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chromeos/ash/components/account_manager/account_manager_factory.h"
37 #include "chromeos/ash/components/browser_context_helper/browser_context_flusher.h"
38 #include "chromeos/ash/components/dbus/debug_daemon/debug_daemon_client.h"
39 #include "chromeos/ash/components/geolocation/simple_geolocation_provider.h"
40 #include "chromeos/ash/components/timezone/timezone_resolver.h"
41 #include "components/keep_alive_registry/keep_alive_types.h"
42 #include "components/keep_alive_registry/scoped_keep_alive.h"
43 #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
44 #include "components/session_manager/core/session_manager.h"
45 #include "components/user_manager/user_manager.h"
46 #include "services/network/public/cpp/shared_url_loader_factory.h"
47 #include "services/preferences/public/mojom/preferences.mojom.h"
48 #include "services/service_manager/public/cpp/binder_registry.h"
49 #include "services/service_manager/public/cpp/interface_provider.h"
50 #include "services/service_manager/public/cpp/service.h"
54 class PrimaryProfileServicesShutdownNotifierFactory
55 : public BrowserContextKeyedServiceShutdownNotifierFactory {
57 static PrimaryProfileServicesShutdownNotifierFactory* GetInstance() {
58 return base::Singleton<
59 PrimaryProfileServicesShutdownNotifierFactory>::get();
62 PrimaryProfileServicesShutdownNotifierFactory(
63 const PrimaryProfileServicesShutdownNotifierFactory&) = delete;
64 PrimaryProfileServicesShutdownNotifierFactory& operator=(
65 const PrimaryProfileServicesShutdownNotifierFactory&) = delete;
68 friend struct base::DefaultSingletonTraits<
69 PrimaryProfileServicesShutdownNotifierFactory>;
71 PrimaryProfileServicesShutdownNotifierFactory()
72 : BrowserContextKeyedServiceShutdownNotifierFactory(
73 "PrimaryProfileServices") {}
74 ~PrimaryProfileServicesShutdownNotifierFactory() override {}
79 BrowserProcessPlatformPart::BrowserProcessPlatformPart()
80 : created_profile_helper_(false),
81 browser_context_flusher_(std::make_unique<ash::BrowserContextFlusher>()),
82 account_manager_factory_(std::make_unique<ash::AccountManagerFactory>()) {
85 BrowserProcessPlatformPart::~BrowserProcessPlatformPart() {
86 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
89 void BrowserProcessPlatformPart::InitializeAutomaticRebootManager() {
90 DCHECK(!automatic_reboot_manager_);
92 automatic_reboot_manager_ =
93 std::make_unique<ash::system::AutomaticRebootManager>(
94 base::DefaultClock::GetInstance(),
95 base::DefaultTickClock::GetInstance());
98 void BrowserProcessPlatformPart::ShutdownAutomaticRebootManager() {
99 automatic_reboot_manager_.reset();
102 void BrowserProcessPlatformPart::InitializeChromeUserManager() {
103 DCHECK(!chrome_user_manager_);
104 chrome_user_manager_ = ash::ChromeUserManagerImpl::CreateChromeUserManager();
105 // LoginState and DeviceCloudPolicyManager outlives UserManager, so on
106 // their initialization, there's no way to start observing UserManager.
107 // This is the earliest timing to do so.
108 if (auto* login_state = ash::LoginState::Get()) {
109 login_state->OnUserManagerCreated(chrome_user_manager_.get());
111 if (auto* policy_manager =
112 browser_policy_connector_ash()->GetDeviceCloudPolicyManager()) {
113 policy_manager->OnUserManagerCreated(chrome_user_manager_.get());
116 chrome_user_manager_->Initialize();
119 void BrowserProcessPlatformPart::DestroyChromeUserManager() {
120 chrome_user_manager_->Destroy();
121 if (auto* policy_manager =
122 browser_policy_connector_ash()->GetDeviceCloudPolicyManager()) {
123 policy_manager->OnUserManagerWillBeDestroyed(chrome_user_manager_.get());
125 if (auto* login_state = ash::LoginState::Get()) {
126 login_state->OnUserManagerWillBeDestroyed(chrome_user_manager_.get());
129 chrome_user_manager_.reset();
132 void BrowserProcessPlatformPart::InitializeDeviceDisablingManager() {
133 DCHECK(!device_disabling_manager_);
135 device_disabling_manager_delegate_ =
136 std::make_unique<ash::system::DeviceDisablingManagerDefaultDelegate>();
137 device_disabling_manager_ =
138 std::make_unique<ash::system::DeviceDisablingManager>(
139 device_disabling_manager_delegate_.get(), ash::CrosSettings::Get(),
140 user_manager::UserManager::Get());
141 device_disabling_manager_->Init();
144 void BrowserProcessPlatformPart::ShutdownDeviceDisablingManager() {
145 device_disabling_manager_.reset();
146 device_disabling_manager_delegate_.reset();
149 void BrowserProcessPlatformPart::InitializeSessionManager() {
150 DCHECK(!session_manager_);
151 session_manager_ = std::make_unique<ash::ChromeSessionManager>();
154 void BrowserProcessPlatformPart::ShutdownSessionManager() {
155 session_manager_.reset();
158 void BrowserProcessPlatformPart::InitializeCrosComponentManager() {
159 if (using_testing_cros_component_manager_)
162 DCHECK(!cros_component_manager_);
163 cros_component_manager_ =
164 base::MakeRefCounted<component_updater::CrOSComponentInstaller>(
165 std::make_unique<component_updater::MetadataTable>(
166 g_browser_process->local_state()),
167 g_browser_process->component_updater());
169 // Register all installed components for regular update.
170 cros_component_manager_->RegisterInstalled();
173 void BrowserProcessPlatformPart::ShutdownCrosComponentManager() {
174 if (using_testing_cros_component_manager_)
177 cros_component_manager_.reset();
180 void BrowserProcessPlatformPart::InitializeSchedulerConfigurationManager() {
181 DCHECK(!scheduler_configuration_manager_);
182 scheduler_configuration_manager_ =
183 std::make_unique<ash::SchedulerConfigurationManager>(
184 ash::DebugDaemonClient::Get(), g_browser_process->local_state());
187 void BrowserProcessPlatformPart::ShutdownSchedulerConfigurationManager() {
188 scheduler_configuration_manager_.reset();
191 void BrowserProcessPlatformPart::InitializeAshProxyMonitor() {
192 DCHECK(!ash_proxy_monitor_);
193 ash_proxy_monitor_ = std::make_unique<ash::AshProxyMonitor>(
194 g_browser_process->local_state(), g_browser_process->profile_manager());
197 void BrowserProcessPlatformPart::ShutdownAshProxyMonitor() {
198 ash_proxy_monitor_.reset();
201 void BrowserProcessPlatformPart::InitializePrimaryProfileServices(
202 Profile* primary_profile) {
203 DCHECK(primary_profile);
205 DCHECK(!in_session_password_change_manager_);
206 in_session_password_change_manager_ =
207 ash::InSessionPasswordChangeManager::CreateIfEnabled(primary_profile);
209 primary_profile_shutdown_subscription_ =
210 PrimaryProfileServicesShutdownNotifierFactory::GetInstance()
211 ->Get(primary_profile)
212 ->Subscribe(base::BindRepeating(
213 &BrowserProcessPlatformPart::ShutdownPrimaryProfileServices,
214 base::Unretained(this)));
216 if (ash::SystemProxyManager::Get()) {
217 ash::SystemProxyManager::Get()->StartObservingPrimaryProfilePrefs(
222 void BrowserProcessPlatformPart::ShutdownPrimaryProfileServices() {
223 if (ash::SystemProxyManager::Get())
224 ash::SystemProxyManager::Get()->StopObservingPrimaryProfilePrefs();
225 in_session_password_change_manager_.reset();
228 void BrowserProcessPlatformPart::RegisterKeepAlive() {
229 DCHECK(!keep_alive_);
230 keep_alive_ = std::make_unique<ScopedKeepAlive>(
231 KeepAliveOrigin::BROWSER_PROCESS_CHROMEOS,
232 KeepAliveRestartOption::DISABLED);
235 void BrowserProcessPlatformPart::UnregisterKeepAlive() {
239 ash::ProfileHelper* BrowserProcessPlatformPart::profile_helper() {
240 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
241 if (!created_profile_helper_)
242 CreateProfileHelper();
243 return profile_helper_.get();
246 policy::BrowserPolicyConnectorAsh*
247 BrowserProcessPlatformPart::browser_policy_connector_ash() {
248 return static_cast<policy::BrowserPolicyConnectorAsh*>(
249 g_browser_process->browser_policy_connector());
252 ash::system::TimeZoneResolverManager*
253 BrowserProcessPlatformPart::GetTimezoneResolverManager() {
254 if (!timezone_resolver_manager_.get()) {
255 timezone_resolver_manager_ =
256 std::make_unique<ash::system::TimeZoneResolverManager>();
258 return timezone_resolver_manager_.get();
261 ash::TimeZoneResolver* BrowserProcessPlatformPart::GetTimezoneResolver() {
262 if (!timezone_resolver_.get()) {
263 timezone_resolver_ = std::make_unique<ash::TimeZoneResolver>(
264 GetTimezoneResolverManager(),
265 g_browser_process->shared_url_loader_factory(),
266 ash::SimpleGeolocationProvider::DefaultGeolocationProviderURL(),
267 base::BindRepeating(&ash::system::ApplyTimeZone),
268 base::BindRepeating(&ash::DelayNetworkCall),
269 g_browser_process->local_state());
271 return timezone_resolver_.get();
274 void BrowserProcessPlatformPart::StartTearDown() {
275 // Some tests check for memory leaks before this object is
276 // destroyed. So we need to destroy |timezone_resolver_| and
277 // |timezone_resolver_manager_| here.
278 timezone_resolver_.reset();
279 timezone_resolver_manager_.reset();
280 profile_helper_.reset();
281 browser_context_flusher_.reset();
284 void BrowserProcessPlatformPart::AttemptExit(bool try_to_quit_application) {
285 // Request Lacros terminate early during shutdown to give it the opportunity
286 // to shutdown gracefully. Check to make sure `browser_manager` is available
287 // as it may be null in tests.
288 if (auto* browser_manager = crosapi::BrowserManager::Get())
289 browser_manager->Shutdown();
291 BrowserProcessPlatformPartChromeOS::AttemptExit(try_to_quit_application);
294 ash::system::SystemClock* BrowserProcessPlatformPart::GetSystemClock() {
295 if (!system_clock_.get())
296 system_clock_ = std::make_unique<ash::system::SystemClock>();
297 return system_clock_.get();
300 void BrowserProcessPlatformPart::DestroySystemClock() {
301 system_clock_.reset();
304 void BrowserProcessPlatformPart::CreateProfileHelper() {
305 DCHECK(!created_profile_helper_ && !profile_helper_);
306 created_profile_helper_ = true;
307 profile_helper_ = ash::ProfileHelper::CreateInstance();
310 ash::AccountManagerFactory*
311 BrowserProcessPlatformPart::GetAccountManagerFactory() {
312 return account_manager_factory_.get();
315 bool BrowserProcessPlatformPart::CanRestoreUrlsForProfile(
316 const Profile* profile) const {
317 return profile->IsRegularProfile() && !profile->IsSystemProfile() &&
318 ash::ProfileHelper::IsUserProfile(profile) &&
319 !ash::ProfileHelper::IsEphemeralUserProfile(profile);
323 void BrowserProcessPlatformPart::EnsureFactoryBuilt() {
324 PrimaryProfileServicesShutdownNotifierFactory::GetInstance();