1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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_chromeos.h"
10 #include "base/check_op.h"
11 #include "base/memory/singleton.h"
12 #include "base/time/default_tick_clock.h"
13 #include "base/time/tick_clock.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/login/saml/in_session_password_change_manager.h"
16 #include "chrome/browser/chromeos/login/session/chrome_session_manager.h"
17 #include "chrome/browser/chromeos/login/users/chrome_user_manager_impl.h"
18 #include "chrome/browser/chromeos/net/delay_network_call.h"
19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
20 #include "chrome/browser/chromeos/policy/system_proxy_manager.h"
21 #include "chrome/browser/chromeos/profiles/profile_helper.h"
22 #include "chrome/browser/chromeos/scheduler_configuration_manager.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
25 #include "chrome/browser/chromeos/system/device_disabling_manager.h"
26 #include "chrome/browser/chromeos/system/device_disabling_manager_default_delegate.h"
27 #include "chrome/browser/chromeos/system/system_clock.h"
28 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h"
29 #include "chrome/browser/chromeos/system/timezone_util.h"
30 #include "chrome/browser/component_updater/metadata_table_chromeos.h"
31 #include "chrome/common/chrome_features.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chromeos/components/account_manager/account_manager_factory.h"
34 #include "chromeos/dbus/dbus_thread_manager.h"
35 #include "chromeos/geolocation/simple_geolocation_provider.h"
36 #include "chromeos/timezone/timezone_resolver.h"
37 #include "components/keep_alive_registry/keep_alive_types.h"
38 #include "components/keep_alive_registry/scoped_keep_alive.h"
39 #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
40 #include "components/session_manager/core/session_manager.h"
41 #include "components/user_manager/user_manager.h"
42 #include "services/network/public/cpp/shared_url_loader_factory.h"
43 #include "services/preferences/public/mojom/preferences.mojom.h"
44 #include "services/service_manager/public/cpp/binder_registry.h"
45 #include "services/service_manager/public/cpp/interface_provider.h"
46 #include "services/service_manager/public/cpp/service.h"
50 class PrimaryProfileServicesShutdownNotifierFactory
51 : public BrowserContextKeyedServiceShutdownNotifierFactory {
53 static PrimaryProfileServicesShutdownNotifierFactory* GetInstance() {
54 return base::Singleton<
55 PrimaryProfileServicesShutdownNotifierFactory>::get();
59 friend struct base::DefaultSingletonTraits<
60 PrimaryProfileServicesShutdownNotifierFactory>;
62 PrimaryProfileServicesShutdownNotifierFactory()
63 : BrowserContextKeyedServiceShutdownNotifierFactory(
64 "PrimaryProfileServices") {}
65 ~PrimaryProfileServicesShutdownNotifierFactory() override {}
67 DISALLOW_COPY_AND_ASSIGN(PrimaryProfileServicesShutdownNotifierFactory);
72 BrowserProcessPlatformPart::BrowserProcessPlatformPart()
73 : created_profile_helper_(false),
74 account_manager_factory_(
75 std::make_unique<chromeos::AccountManagerFactory>()) {}
77 BrowserProcessPlatformPart::~BrowserProcessPlatformPart() {
78 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
81 void BrowserProcessPlatformPart::InitializeAutomaticRebootManager() {
82 DCHECK(!automatic_reboot_manager_);
84 automatic_reboot_manager_.reset(new chromeos::system::AutomaticRebootManager(
85 base::DefaultTickClock::GetInstance()));
88 void BrowserProcessPlatformPart::ShutdownAutomaticRebootManager() {
89 automatic_reboot_manager_.reset();
92 void BrowserProcessPlatformPart::InitializeChromeUserManager() {
93 DCHECK(!chrome_user_manager_);
94 chrome_user_manager_ =
95 chromeos::ChromeUserManagerImpl::CreateChromeUserManager();
96 chrome_user_manager_->Initialize();
99 void BrowserProcessPlatformPart::DestroyChromeUserManager() {
100 chrome_user_manager_->Destroy();
101 chrome_user_manager_.reset();
104 void BrowserProcessPlatformPart::InitializeDeviceDisablingManager() {
105 DCHECK(!device_disabling_manager_);
107 device_disabling_manager_delegate_.reset(
108 new chromeos::system::DeviceDisablingManagerDefaultDelegate);
109 device_disabling_manager_.reset(new chromeos::system::DeviceDisablingManager(
110 device_disabling_manager_delegate_.get(), chromeos::CrosSettings::Get(),
111 user_manager::UserManager::Get()));
112 device_disabling_manager_->Init();
115 void BrowserProcessPlatformPart::ShutdownDeviceDisablingManager() {
116 device_disabling_manager_.reset();
117 device_disabling_manager_delegate_.reset();
120 void BrowserProcessPlatformPart::InitializeSessionManager() {
121 DCHECK(!session_manager_);
122 session_manager_ = std::make_unique<chromeos::ChromeSessionManager>();
125 void BrowserProcessPlatformPart::ShutdownSessionManager() {
126 session_manager_.reset();
129 void BrowserProcessPlatformPart::InitializeCrosComponentManager() {
130 if (using_testing_cros_component_manager_)
133 DCHECK(!cros_component_manager_);
134 cros_component_manager_ =
135 base::MakeRefCounted<component_updater::CrOSComponentInstaller>(
136 std::make_unique<component_updater::MetadataTable>(
137 g_browser_process->local_state()),
138 g_browser_process->component_updater());
140 // Register all installed components for regular update.
141 cros_component_manager_->RegisterInstalled();
144 void BrowserProcessPlatformPart::ShutdownCrosComponentManager() {
145 if (using_testing_cros_component_manager_)
148 cros_component_manager_.reset();
151 void BrowserProcessPlatformPart::InitializeSchedulerConfigurationManager() {
152 DCHECK(!scheduler_configuration_manager_);
153 scheduler_configuration_manager_ =
154 std::make_unique<chromeos::SchedulerConfigurationManager>(
155 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(),
156 g_browser_process->local_state());
159 void BrowserProcessPlatformPart::ShutdownSchedulerConfigurationManager() {
160 scheduler_configuration_manager_.reset();
163 void BrowserProcessPlatformPart::InitializePrimaryProfileServices(
164 Profile* primary_profile) {
165 DCHECK(primary_profile);
167 DCHECK(!in_session_password_change_manager_);
168 in_session_password_change_manager_ =
169 chromeos::InSessionPasswordChangeManager::CreateIfEnabled(
172 primary_profile_shutdown_subscription_ =
173 PrimaryProfileServicesShutdownNotifierFactory::GetInstance()
174 ->Get(primary_profile)
175 ->Subscribe(base::Bind(
176 &BrowserProcessPlatformPart::ShutdownPrimaryProfileServices,
177 base::Unretained(this)));
178 browser_policy_connector_chromeos()
179 ->GetSystemProxyManager()
180 ->StartObservingPrimaryProfilePrefs(primary_profile);
183 void BrowserProcessPlatformPart::ShutdownPrimaryProfileServices() {
184 browser_policy_connector_chromeos()
185 ->GetSystemProxyManager()
186 ->StopObservingPrimaryProfilePrefs();
187 in_session_password_change_manager_.reset();
190 void BrowserProcessPlatformPart::RegisterKeepAlive() {
191 DCHECK(!keep_alive_);
193 new ScopedKeepAlive(KeepAliveOrigin::BROWSER_PROCESS_CHROMEOS,
194 KeepAliveRestartOption::DISABLED));
197 void BrowserProcessPlatformPart::UnregisterKeepAlive() {
201 chromeos::ProfileHelper* BrowserProcessPlatformPart::profile_helper() {
202 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
203 if (!created_profile_helper_)
204 CreateProfileHelper();
205 return profile_helper_.get();
208 policy::BrowserPolicyConnectorChromeOS*
209 BrowserProcessPlatformPart::browser_policy_connector_chromeos() {
210 return static_cast<policy::BrowserPolicyConnectorChromeOS*>(
211 g_browser_process->browser_policy_connector());
214 chromeos::system::TimeZoneResolverManager*
215 BrowserProcessPlatformPart::GetTimezoneResolverManager() {
216 if (!timezone_resolver_manager_.get()) {
217 timezone_resolver_manager_.reset(
218 new chromeos::system::TimeZoneResolverManager());
220 return timezone_resolver_manager_.get();
223 chromeos::TimeZoneResolver* BrowserProcessPlatformPart::GetTimezoneResolver() {
224 if (!timezone_resolver_.get()) {
225 timezone_resolver_.reset(new chromeos::TimeZoneResolver(
226 GetTimezoneResolverManager(),
227 g_browser_process->shared_url_loader_factory(),
228 chromeos::SimpleGeolocationProvider::DefaultGeolocationProviderURL(),
229 base::Bind(&chromeos::system::ApplyTimeZone),
230 base::Bind(&chromeos::DelayNetworkCall,
231 base::TimeDelta::FromMilliseconds(
232 chromeos::kDefaultNetworkRetryDelayMS)),
233 g_browser_process->local_state()));
235 return timezone_resolver_.get();
238 void BrowserProcessPlatformPart::StartTearDown() {
239 // interactive_ui_tests check for memory leaks before this object is
240 // destroyed. So we need to destroy |timezone_resolver_| here.
241 timezone_resolver_.reset();
242 profile_helper_.reset();
245 std::unique_ptr<policy::ChromeBrowserPolicyConnector>
246 BrowserProcessPlatformPart::CreateBrowserPolicyConnector() {
247 return std::unique_ptr<policy::ChromeBrowserPolicyConnector>(
248 new policy::BrowserPolicyConnectorChromeOS());
251 chromeos::system::SystemClock* BrowserProcessPlatformPart::GetSystemClock() {
252 if (!system_clock_.get())
253 system_clock_.reset(new chromeos::system::SystemClock());
254 return system_clock_.get();
257 void BrowserProcessPlatformPart::DestroySystemClock() {
258 system_clock_.reset();
261 void BrowserProcessPlatformPart::CreateProfileHelper() {
262 DCHECK(!created_profile_helper_ && !profile_helper_);
263 created_profile_helper_ = true;
264 profile_helper_ = chromeos::ProfileHelper::CreateInstance();
267 chromeos::AccountManagerFactory*
268 BrowserProcessPlatformPart::GetAccountManagerFactory() {
269 return account_manager_factory_.get();