1 // Copyright (c) 2012 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 // When each service is created, we set a flag indicating this. At this point,
6 // the service initialization could fail or succeed. This allows us to remember
7 // if we tried to create a service, and not try creating it over and over if
8 // the creation failed.
10 #ifndef CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
11 #define CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
18 #include "base/macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/sequence_checker.h"
21 #include "base/timer/timer.h"
22 #include "build/build_config.h"
23 #include "chrome/browser/browser_process.h"
24 #include "chrome/common/buildflags.h"
25 #include "components/keep_alive_registry/keep_alive_state_observer.h"
26 #include "components/metrics_services_manager/metrics_services_manager.h"
27 #include "components/nacl/common/buildflags.h"
28 #include "components/prefs/persistent_pref_store.h"
29 #include "components/prefs/pref_change_registrar.h"
30 #include "extensions/buildflags/buildflags.h"
31 #include "media/media_buildflags.h"
32 #include "ppapi/buildflags/buildflags.h"
33 #include "printing/buildflags/buildflags.h"
34 #include "services/network/public/cpp/network_quality_tracker.h"
35 #include "services/network/public/mojom/network_service.mojom-forward.h"
37 #if !defined(OS_ANDROID)
38 #include "chrome/browser/upgrade_detector/build_state.h"
42 class ChromeFeatureListCreator;
43 class ChromeMetricsServicesManagerClient;
44 class DevToolsAutoOpener;
45 class RemoteDebuggingServer;
46 class PrefRegistrySimple;
47 class SecureOriginPrefsObserver;
48 class SiteIsolationPrefsObserver;
49 class SystemNotificationHelper;
52 #if BUILDFLAG(ENABLE_PLUGINS)
53 class PluginsResourceService;
60 namespace extensions {
61 class ExtensionsBrowserClient;
69 class ChromeBrowserPolicyConnector;
73 namespace webrtc_event_logging {
74 class WebRtcEventLogManager;
75 } // namespace webrtc_event_logging
77 // Real implementation of BrowserProcess that creates and returns the services.
78 class BrowserProcessImpl : public BrowserProcess,
79 public KeepAliveStateObserver {
81 // |startup_data| should not be null. The BrowserProcessImpl
82 // will take the PrefService owned by the creator as the Local State instead
83 // of loading the JSON file from disk.
84 explicit BrowserProcessImpl(StartupData* startup_data);
85 ~BrowserProcessImpl() override;
87 // Called to complete initialization.
90 #if !defined(OS_ANDROID)
91 // Sets a closure to be run to break out of a run loop on browser shutdown
92 // (when the KeepAlive count reaches zero).
93 // TODO(https://crbug.com/845966): This is also used on macOS for the Cocoa
94 // first run dialog so that shutdown can be initiated via a signal while the
95 // first run dialog is showing.
96 void SetQuitClosure(base::OnceClosure quit_closure);
99 #if defined(OS_MACOSX)
100 // Clears the quit closure. Shutdown will not be initiated should the
101 // KeepAlive count reach zero. This function may be called more than once.
102 // TODO(https://crbug.com/845966): Remove this once the Cocoa first run
103 // dialog no longer needs it.
104 void ClearQuitClosure();
107 // Called before the browser threads are created.
108 void PreCreateThreads(const base::CommandLine& command_line);
110 // Called after the threads have been created but before the message loops
111 // starts running. Allows the browser process to do any initialization that
112 // requires all threads running.
113 void PreMainMessageLoopRun();
115 // Most cleanup is done by these functions, driven from
116 // ChromeBrowserMain based on notifications from the content
117 // framework, rather than in the destructor, so that we can
118 // interleave cleanup with threads being stopped.
119 #if !defined(OS_ANDROID)
120 void StartTearDown();
121 void PostDestroyThreads();
124 // Sets |metrics_services_manager_| and |metrics_services_manager_client_|
125 // which is owned by it.
126 void SetMetricsServices(
127 std::unique_ptr<metrics_services_manager::MetricsServicesManager> manager,
128 metrics_services_manager::MetricsServicesManagerClient* client);
130 // BrowserProcess implementation.
131 void EndSession() override;
132 void FlushLocalStateAndReply(base::OnceClosure reply) override;
133 metrics_services_manager::MetricsServicesManager* GetMetricsServicesManager()
135 metrics::MetricsService* metrics_service() override;
136 rappor::RapporServiceImpl* rappor_service() override;
137 // TODO(qinmin): Remove this method as callers can retrieve the global
138 // instance from SystemNetworkContextManager directly.
139 SystemNetworkContextManager* system_network_context_manager() override;
140 scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory()
142 network::NetworkQualityTracker* network_quality_tracker() override;
143 WatchDogThread* watchdog_thread() override;
144 ProfileManager* profile_manager() override;
145 PrefService* local_state() override;
146 variations::VariationsService* variations_service() override;
147 BrowserProcessPlatformPart* platform_part() override;
148 extensions::EventRouterForwarder* extension_event_router_forwarder() override;
149 NotificationUIManager* notification_ui_manager() override;
150 NotificationPlatformBridge* notification_platform_bridge() override;
151 policy::ChromeBrowserPolicyConnector* browser_policy_connector() override;
152 policy::PolicyService* policy_service() override;
153 IconManager* icon_manager() override;
154 GpuModeManager* gpu_mode_manager() override;
155 void CreateDevToolsProtocolHandler() override;
156 void CreateDevToolsAutoOpener() override;
157 bool IsShuttingDown() override;
158 printing::PrintJobManager* print_job_manager() override;
159 printing::PrintPreviewDialogController* print_preview_dialog_controller()
161 printing::BackgroundPrintingManager* background_printing_manager() override;
162 IntranetRedirectDetector* intranet_redirect_detector() override;
163 const std::string& GetApplicationLocale() override;
164 void SetApplicationLocale(const std::string& actual_locale) override;
165 DownloadStatusUpdater* download_status_updater() override;
166 DownloadRequestLimiter* download_request_limiter() override;
167 BackgroundModeManager* background_mode_manager() override;
168 void set_background_mode_manager_for_test(
169 std::unique_ptr<BackgroundModeManager> manager) override;
170 StatusTray* status_tray() override;
171 safe_browsing::SafeBrowsingService* safe_browsing_service() override;
172 subresource_filter::RulesetService* subresource_filter_ruleset_service()
174 federated_learning::FlocBlocklistService* floc_blocklist_service() override;
175 optimization_guide::OptimizationGuideService* optimization_guide_service()
178 StartupData* startup_data() override;
180 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
181 void StartAutoupdateTimer() override;
184 component_updater::ComponentUpdateService* component_updater() override;
185 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
186 component_updater::SupervisedUserWhitelistInstaller*
187 supervised_user_whitelist_installer() override;
189 MediaFileSystemRegistry* media_file_system_registry() override;
190 WebRtcLogUploader* webrtc_log_uploader() override;
191 network_time::NetworkTimeTracker* network_time_tracker() override;
192 gcm::GCMDriver* gcm_driver() override;
193 resource_coordinator::TabManager* GetTabManager() override;
194 resource_coordinator::ResourceCoordinatorParts* resource_coordinator_parts()
197 BuildState* GetBuildState() override;
199 static void RegisterPrefs(PrefRegistrySimple* registry);
202 using WebRtcEventLogManager = webrtc_event_logging::WebRtcEventLogManager;
204 // KeepAliveStateObserver implementation
205 void OnKeepAliveStateChanged(bool is_keeping_alive) override;
206 void OnKeepAliveRestartStateChanged(bool can_restart) override;
208 // Create network quality observer so that it can propagate network quality
209 // changes to the render process.
210 void CreateNetworkQualityObserver();
212 void CreateWatchdogThread();
213 void CreateProfileManager();
214 void CreateLocalState();
215 void CreateIconManager();
216 void CreateIntranetRedirectDetector();
217 void CreateNotificationPlatformBridge();
218 void CreateNotificationUIManager();
219 void CreatePrintPreviewDialogController();
220 void CreateBackgroundPrintingManager();
221 void CreateSafeBrowsingService();
222 void CreateSubresourceFilterRulesetService();
223 void CreateFlocBlocklistService();
224 void CreateOptimizationGuideService();
225 void CreateStatusTray();
226 void CreateBackgroundModeManager();
227 void CreateGCMDriver();
228 void CreatePhysicalWebDataSource();
230 void ApplyDefaultBrowserPolicy();
232 // Methods called to control our lifetime. The browser process can be "pinned"
233 // to make sure it keeps running.
237 bool created_watchdog_thread_ = false;
238 std::unique_ptr<WatchDogThread> watchdog_thread_;
240 bool created_browser_policy_connector_ = false;
241 // Must be destroyed after |local_state_|.
242 std::unique_ptr<policy::ChromeBrowserPolicyConnector>
243 browser_policy_connector_;
245 bool created_profile_manager_ = false;
246 std::unique_ptr<ProfileManager> profile_manager_;
248 std::unique_ptr<PrefService> local_state_;
250 // |metrics_services_manager_| owns this.
251 ChromeMetricsServicesManagerClient* metrics_services_manager_client_ =
254 // Must be destroyed before |local_state_|.
255 std::unique_ptr<metrics_services_manager::MetricsServicesManager>
256 metrics_services_manager_;
258 std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_;
260 // Listens to NetworkQualityTracker and sends network quality updates to the
263 network::NetworkQualityTracker::RTTAndThroughputEstimatesObserver>
264 network_quality_observer_;
266 bool created_icon_manager_ = false;
267 std::unique_ptr<IconManager> icon_manager_;
269 std::unique_ptr<GpuModeManager> gpu_mode_manager_;
271 #if BUILDFLAG(ENABLE_EXTENSIONS)
272 std::unique_ptr<extensions::ExtensionsBrowserClient>
273 extensions_browser_client_;
275 scoped_refptr<extensions::EventRouterForwarder>
276 extension_event_router_forwarder_;
278 std::unique_ptr<MediaFileSystemRegistry> media_file_system_registry_;
281 #if !defined(OS_ANDROID)
282 std::unique_ptr<RemoteDebuggingServer> remote_debugging_server_;
283 std::unique_ptr<DevToolsAutoOpener> devtools_auto_opener_;
286 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
287 scoped_refptr<printing::PrintPreviewDialogController>
288 print_preview_dialog_controller_;
290 std::unique_ptr<printing::BackgroundPrintingManager>
291 background_printing_manager_;
294 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
295 // Manager for desktop notification UI.
296 bool created_notification_ui_manager_ = false;
297 std::unique_ptr<NotificationUIManager> notification_ui_manager_;
300 std::unique_ptr<IntranetRedirectDetector> intranet_redirect_detector_;
302 std::unique_ptr<StatusTray> status_tray_;
304 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS)
305 bool created_notification_bridge_ = false;
308 std::unique_ptr<NotificationPlatformBridge> notification_bridge_;
310 // Use SystemNotificationHelper::GetInstance to get this instance.
311 std::unique_ptr<SystemNotificationHelper> system_notification_helper_;
313 #if BUILDFLAG(ENABLE_BACKGROUND_MODE)
314 std::unique_ptr<BackgroundModeManager> background_mode_manager_;
317 bool created_safe_browsing_service_ = false;
318 scoped_refptr<safe_browsing::SafeBrowsingService> safe_browsing_service_;
320 bool created_subresource_filter_ruleset_service_ = false;
321 std::unique_ptr<subresource_filter::RulesetService>
322 subresource_filter_ruleset_service_;
324 std::unique_ptr<federated_learning::FlocBlocklistService>
325 floc_blocklist_service_;
327 bool created_optimization_guide_service_ = false;
328 std::unique_ptr<optimization_guide::OptimizationGuideService>
329 optimization_guide_service_;
331 bool shutting_down_ = false;
333 bool tearing_down_ = false;
335 // Ensures that all the print jobs are finished before closing the browser.
336 std::unique_ptr<printing::PrintJobManager> print_job_manager_;
340 // Download status updates (like a changing application icon on dock/taskbar)
341 // are global per-application. DownloadStatusUpdater does no work in the ctor
342 // so we don't have to worry about lazy initialization.
343 std::unique_ptr<DownloadStatusUpdater> download_status_updater_;
345 scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
347 // If non-null, this object holds a pref store that will be taken by
348 // BrowserProcessImpl to create the |local_state_|.
349 ChromeFeatureListCreator* chrome_feature_list_creator_;
351 StartupData* startup_data_;
353 // Ensures that the observers of plugin/print disable/enable state
354 // notifications are properly added and removed.
355 PrefChangeRegistrar pref_change_registrar_;
357 std::unique_ptr<BatteryMetrics> battery_metrics_;
359 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
360 base::RepeatingTimer autoupdate_timer_;
362 // Gets called by autoupdate timer to see if browser needs restart and can be
363 // restarted, and if that's the case, restarts the browser.
364 void OnAutoupdateTimer();
365 bool IsRunningInBackground() const;
366 void OnPendingRestartResult(bool is_update_pending_restart);
367 void RestartBackgroundInstance();
368 #endif // defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
370 // component updater is normally not used under ChromeOS due
371 // to concerns over integrity of data shared between profiles,
372 // but some users of component updater only install per-user.
373 std::unique_ptr<component_updater::ComponentUpdateService> component_updater_;
375 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
376 std::unique_ptr<component_updater::SupervisedUserWhitelistInstaller>
377 supervised_user_whitelist_installer_;
380 #if BUILDFLAG(ENABLE_PLUGINS)
381 std::unique_ptr<PluginsResourceService> plugins_resource_service_;
384 std::unique_ptr<BrowserProcessPlatformPart> platform_part_;
386 // Lazily initialized.
387 std::unique_ptr<WebRtcLogUploader> webrtc_log_uploader_;
389 // WebRtcEventLogManager is a singleton which is instaniated before anything
390 // that needs it, and lives until ~BrowserProcessImpl(). This allows it to
391 // safely post base::Unretained(this) references to an internally owned task
392 // queue, since after ~BrowserProcessImpl(), those tasks would no longer run.
393 std::unique_ptr<WebRtcEventLogManager> webrtc_event_log_manager_;
395 std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
397 std::unique_ptr<gcm::GCMDriver> gcm_driver_;
399 std::unique_ptr<resource_coordinator::ResourceCoordinatorParts>
400 resource_coordinator_parts_;
402 std::unique_ptr<SecureOriginPrefsObserver> secure_origin_prefs_observer_;
403 std::unique_ptr<SiteIsolationPrefsObserver> site_isolation_prefs_observer_;
405 #if !defined(OS_ANDROID)
406 // Called to signal the process' main message loop to exit.
407 base::OnceClosure quit_closure_;
409 BuildState build_state_;
412 SEQUENCE_CHECKER(sequence_checker_);
414 DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
417 #endif // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_