[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / browser_process_platform_part_ash.cc
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.
4
5 #include "chrome/browser/browser_process_platform_part_ash.h"
6
7 #include <memory>
8 #include <utility>
9
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"
51
52 namespace {
53
54 class PrimaryProfileServicesShutdownNotifierFactory
55     : public BrowserContextKeyedServiceShutdownNotifierFactory {
56  public:
57   static PrimaryProfileServicesShutdownNotifierFactory* GetInstance() {
58     return base::Singleton<
59         PrimaryProfileServicesShutdownNotifierFactory>::get();
60   }
61
62   PrimaryProfileServicesShutdownNotifierFactory(
63       const PrimaryProfileServicesShutdownNotifierFactory&) = delete;
64   PrimaryProfileServicesShutdownNotifierFactory& operator=(
65       const PrimaryProfileServicesShutdownNotifierFactory&) = delete;
66
67  private:
68   friend struct base::DefaultSingletonTraits<
69       PrimaryProfileServicesShutdownNotifierFactory>;
70
71   PrimaryProfileServicesShutdownNotifierFactory()
72       : BrowserContextKeyedServiceShutdownNotifierFactory(
73             "PrimaryProfileServices") {}
74   ~PrimaryProfileServicesShutdownNotifierFactory() override {}
75 };
76
77 }  // namespace
78
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>()) {
83 }
84
85 BrowserProcessPlatformPart::~BrowserProcessPlatformPart() {
86   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
87 }
88
89 void BrowserProcessPlatformPart::InitializeAutomaticRebootManager() {
90   DCHECK(!automatic_reboot_manager_);
91
92   automatic_reboot_manager_ =
93       std::make_unique<ash::system::AutomaticRebootManager>(
94           base::DefaultClock::GetInstance(),
95           base::DefaultTickClock::GetInstance());
96 }
97
98 void BrowserProcessPlatformPart::ShutdownAutomaticRebootManager() {
99   automatic_reboot_manager_.reset();
100 }
101
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());
110   }
111   if (auto* policy_manager =
112           browser_policy_connector_ash()->GetDeviceCloudPolicyManager()) {
113     policy_manager->OnUserManagerCreated(chrome_user_manager_.get());
114   }
115
116   chrome_user_manager_->Initialize();
117 }
118
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());
124   }
125   if (auto* login_state = ash::LoginState::Get()) {
126     login_state->OnUserManagerWillBeDestroyed(chrome_user_manager_.get());
127   }
128
129   chrome_user_manager_.reset();
130 }
131
132 void BrowserProcessPlatformPart::InitializeDeviceDisablingManager() {
133   DCHECK(!device_disabling_manager_);
134
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();
142 }
143
144 void BrowserProcessPlatformPart::ShutdownDeviceDisablingManager() {
145   device_disabling_manager_.reset();
146   device_disabling_manager_delegate_.reset();
147 }
148
149 void BrowserProcessPlatformPart::InitializeSessionManager() {
150   DCHECK(!session_manager_);
151   session_manager_ = std::make_unique<ash::ChromeSessionManager>();
152 }
153
154 void BrowserProcessPlatformPart::ShutdownSessionManager() {
155   session_manager_.reset();
156 }
157
158 void BrowserProcessPlatformPart::InitializeCrosComponentManager() {
159   if (using_testing_cros_component_manager_)
160     return;
161
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());
168
169   // Register all installed components for regular update.
170   cros_component_manager_->RegisterInstalled();
171 }
172
173 void BrowserProcessPlatformPart::ShutdownCrosComponentManager() {
174   if (using_testing_cros_component_manager_)
175     return;
176
177   cros_component_manager_.reset();
178 }
179
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());
185 }
186
187 void BrowserProcessPlatformPart::ShutdownSchedulerConfigurationManager() {
188   scheduler_configuration_manager_.reset();
189 }
190
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());
195 }
196
197 void BrowserProcessPlatformPart::ShutdownAshProxyMonitor() {
198   ash_proxy_monitor_.reset();
199 }
200
201 void BrowserProcessPlatformPart::InitializePrimaryProfileServices(
202     Profile* primary_profile) {
203   DCHECK(primary_profile);
204
205   DCHECK(!in_session_password_change_manager_);
206   in_session_password_change_manager_ =
207       ash::InSessionPasswordChangeManager::CreateIfEnabled(primary_profile);
208
209   primary_profile_shutdown_subscription_ =
210       PrimaryProfileServicesShutdownNotifierFactory::GetInstance()
211           ->Get(primary_profile)
212           ->Subscribe(base::BindRepeating(
213               &BrowserProcessPlatformPart::ShutdownPrimaryProfileServices,
214               base::Unretained(this)));
215
216   if (ash::SystemProxyManager::Get()) {
217     ash::SystemProxyManager::Get()->StartObservingPrimaryProfilePrefs(
218         primary_profile);
219   }
220 }
221
222 void BrowserProcessPlatformPart::ShutdownPrimaryProfileServices() {
223   if (ash::SystemProxyManager::Get())
224     ash::SystemProxyManager::Get()->StopObservingPrimaryProfilePrefs();
225   in_session_password_change_manager_.reset();
226 }
227
228 void BrowserProcessPlatformPart::RegisterKeepAlive() {
229   DCHECK(!keep_alive_);
230   keep_alive_ = std::make_unique<ScopedKeepAlive>(
231       KeepAliveOrigin::BROWSER_PROCESS_CHROMEOS,
232       KeepAliveRestartOption::DISABLED);
233 }
234
235 void BrowserProcessPlatformPart::UnregisterKeepAlive() {
236   keep_alive_.reset();
237 }
238
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();
244 }
245
246 policy::BrowserPolicyConnectorAsh*
247 BrowserProcessPlatformPart::browser_policy_connector_ash() {
248   return static_cast<policy::BrowserPolicyConnectorAsh*>(
249       g_browser_process->browser_policy_connector());
250 }
251
252 ash::system::TimeZoneResolverManager*
253 BrowserProcessPlatformPart::GetTimezoneResolverManager() {
254   if (!timezone_resolver_manager_.get()) {
255     timezone_resolver_manager_ =
256         std::make_unique<ash::system::TimeZoneResolverManager>();
257   }
258   return timezone_resolver_manager_.get();
259 }
260
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());
270   }
271   return timezone_resolver_.get();
272 }
273
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();
282 }
283
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();
290
291   BrowserProcessPlatformPartChromeOS::AttemptExit(try_to_quit_application);
292 }
293
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();
298 }
299
300 void BrowserProcessPlatformPart::DestroySystemClock() {
301   system_clock_.reset();
302 }
303
304 void BrowserProcessPlatformPart::CreateProfileHelper() {
305   DCHECK(!created_profile_helper_ && !profile_helper_);
306   created_profile_helper_ = true;
307   profile_helper_ = ash::ProfileHelper::CreateInstance();
308 }
309
310 ash::AccountManagerFactory*
311 BrowserProcessPlatformPart::GetAccountManagerFactory() {
312   return account_manager_factory_.get();
313 }
314
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);
320 }
321
322 // static
323 void BrowserProcessPlatformPart::EnsureFactoryBuilt() {
324   PrimaryProfileServicesShutdownNotifierFactory::GetInstance();
325 }