// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_MAIN_PARTS_CHROME_BROWSER_MAIN_PARTS_ASH_H_
#define CHROME_BROWSER_ASH_MAIN_PARTS_CHROME_BROWSER_MAIN_PARTS_ASH_H_

#include <memory>

#include "base/callback_list.h"
#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ash/external_metrics/external_metrics.h"
#include "chrome/browser/ash/performance/doze_mode_power_status_scheduler.h"
#include "chrome/browser/chrome_browser_main_linux.h"
#include "chrome/browser/memory/memory_kills_monitor.h"
#include "chromeos/ash/components/pcie_peripheral/ash_usb_detector.h"
#include "chromeos/ash/experiences/arc/arc_platform_support_impl.h"
#include "printing/buildflags/buildflags.h"  // USE_CUPS

class AmbientClientImpl;
class AssistantBrowserDelegateImpl;
class ChromeKeyboardControllerClient;
class ImageDownloaderImpl;
class LobsterClientFactoryImpl;

namespace apps {
class AppServiceRegistry;
}  // namespace apps

namespace arc {
class ArcServiceLauncher;
class ContainerAppKiller;
}  // namespace arc

namespace chromeos {
class MahiWebContentsManager;

namespace default_app_order {
class ExternalLoader;
}  // namespace default_app_order

}  // namespace chromeos

namespace crostini {
class CrostiniUnsupportedActionNotifier;
}  // namespace crostini

namespace policy {
class LockToSingleUserManager;
class QuirksPolicyController;
}  // namespace policy

namespace video_conference {
class VideoConferenceManagerClientImpl;
}  // namespace video_conference

namespace ash {

class AccessibilityEventRewriterDelegateImpl;
class ApnMigrator;
class AudioSurveyHandler;
class AuthEventsRecorder;
class BluetoothLogController;
class BluetoothPrefStateObserver;
class BrowserControllerImpl;
class BulkPrintersCalculatorFactory;
class CameraGeneralSurveyHandler;
class ChromeAuthParts;
class CrosUsbDetector;
class DebugdNotificationHandler;
class DozeModePowerStatusScheduler;
class EventRewriterDelegateImpl;
class FastTransitionObserver;
class FwupdDownloadClientImpl;
class GnubbyNotification;
class HatsBluetoothRevampTriggerImpl;
class DeskSyncServiceProvider;
class IdentityManagerProvider;
class SyncServiceProvider;
class TemplateURLServiceProvider;
class IdleActionWarningObserver;
class KioskController;
class LoginScreenExtensionsStorageCleaner;
class LowDiskNotification;
class MagicBoostControllerImpl;
class MisconfiguredUserCleaner;
class NetworkChangeManagerClient;
class NetworkPrefStateObserver;
class NetworkThrottlingObserver;
class PowerMetricsReporter;
class RendererFreezer;
class ReportControllerInitializer;
class ScreenLockerController;
class SessionTerminationManager;
class ShortcutMappingPrefService;
class ShutdownPolicyForwarder;
class SigninProfileHandler;
class SuspendPerfReporter;
class SystemTokenCertDBInitializer;
class TokenHandleStoreFactory;
class UserLoginPermissionTracker;
class VideoConferenceAppServiceClient;
class VideoConferenceAshFeatureClient;

#if BUILDFLAG(USE_CUPS)
class LocalPrinter;
#endif

namespace carrier_lock {
class CarrierLockManager;
}

namespace cros_healthd::internal {
class DataCollector;
}

namespace internal {
class DBusServices;
}

namespace parent_access {
class ParentAccessService;
}

namespace platform_keys {
class KeyPermissionsManager;
}

namespace power {
class SmartChargingManager;
namespace auto_screen_brightness {
class Controller;
}
namespace ml {
class AdaptiveScreenBrightnessManager;
}
}  // namespace power

namespace quick_pair {
class QuickPairBrowserDelegateImpl;
}

namespace system {
class DarkResumeController;
}  // namespace system

// ChromeBrowserMainParts implementation for chromeos specific code.
// NOTE: Chromeos UI (Ash) support should be added to
// ChromeBrowserMainExtraPartsAsh instead. This class should not depend on
// src/ash or chrome/browser/ui/ash.
class ChromeBrowserMainPartsAsh : public ChromeBrowserMainPartsLinux {
 public:
  ChromeBrowserMainPartsAsh(bool is_integration_test,
                            StartupData* startup_data);

  ChromeBrowserMainPartsAsh(const ChromeBrowserMainPartsAsh&) = delete;
  ChromeBrowserMainPartsAsh& operator=(const ChromeBrowserMainPartsAsh&) =
      delete;

  ~ChromeBrowserMainPartsAsh() override;

  // ChromeBrowserMainParts overrides.
  int PreEarlyInitialization() override;
  void PreCreateMainMessageLoop() override;
  void PostCreateMainMessageLoop() override;
  int PreCreateThreads() override;
  int PreMainMessageLoopRun() override;

  // Stages called from PreMainMessageLoopRun.
  void PreProfileInit() override;
  void PostProfileInit(Profile* profile, bool is_initial_profile) override;
  void PreBrowserStart() override;
  void PostBrowserStart() override;

  void PostMainMessageLoopRun() override;
  void PostDestroyThreads() override;

 private:
  std::unique_ptr<chromeos::default_app_order::ExternalLoader>
      app_order_loader_;
  std::unique_ptr<NetworkPrefStateObserver> network_pref_state_observer_;
  std::unique_ptr<BluetoothPrefStateObserver> bluetooth_pref_state_observer_;
  std::unique_ptr<BluetoothLogController> bluetooth_log_controller_;
  std::unique_ptr<IdleActionWarningObserver> idle_action_warning_observer_;
  std::unique_ptr<RendererFreezer> renderer_freezer_;
  std::unique_ptr<PowerMetricsReporter> power_metrics_reporter_;
  std::unique_ptr<SuspendPerfReporter> suspend_perf_reporter_;
  std::unique_ptr<FastTransitionObserver> fast_transition_observer_;
  std::unique_ptr<NetworkThrottlingObserver> network_throttling_observer_;
  std::unique_ptr<NetworkChangeManagerClient> network_change_manager_client_;
  std::unique_ptr<LobsterClientFactoryImpl> lobster_client_factory_;
  std::unique_ptr<DebugdNotificationHandler> debugd_notification_handler_;
  std::unique_ptr<HatsBluetoothRevampTriggerImpl>
      hats_bluetooth_revamp_trigger_;

  std::unique_ptr<internal::DBusServices> dbus_services_;

  base::ScopedClosureRunner mojo_service_manager_closer_;

  std::unique_ptr<SystemTokenCertDBInitializer>
      system_token_certdb_initializer_;

  std::unique_ptr<ShutdownPolicyForwarder> shutdown_policy_forwarder_;

  std::unique_ptr<ash::UserLoginPermissionTracker>
      user_login_permission_tracker_;

  std::unique_ptr<EventRewriterDelegateImpl> event_rewriter_delegate_;

  std::unique_ptr<carrier_lock::CarrierLockManager> carrier_lock_manager_;

  // Handles event dispatch to the accessibility component extensions.
  std::unique_ptr<AccessibilityEventRewriterDelegateImpl>
      accessibility_event_rewriter_delegate_;

  scoped_refptr<ExternalMetrics> external_metrics_;

  std::unique_ptr<DozeModePowerStatusScheduler>
      doze_mode_power_status_scheduler_;

  std::unique_ptr<apps::AppServiceRegistry> app_service_registry_;
  std::unique_ptr<DeskSyncServiceProvider> desk_sync_service_provider_;
  std::unique_ptr<IdentityManagerProvider> identity_manager_provider_;
  std::unique_ptr<SyncServiceProvider> sync_service_provider_;
  std::unique_ptr<TemplateURLServiceProvider> template_url_service_provider_;

  std::unique_ptr<arc::ArcServiceLauncher> arc_service_launcher_;
  std::unique_ptr<arc::ArcPlatformSupportImpl> arc_platform_support_;

  std::unique_ptr<ImageDownloaderImpl> image_downloader_;

  std::unique_ptr<AssistantBrowserDelegateImpl> assistant_delegate_;

  std::unique_ptr<LowDiskNotification> low_disk_notification_;
  std::unique_ptr<KioskController> kiosk_controller_;
  std::unique_ptr<AmbientClientImpl> ambient_client_;

  std::unique_ptr<ShortcutMappingPrefService> shortcut_mapping_pref_service_;
  std::unique_ptr<ChromeKeyboardControllerClient>
      chrome_keyboard_controller_client_;

  std::unique_ptr<VideoConferenceAppServiceClient> vc_app_service_client_;
  std::unique_ptr<VideoConferenceAshFeatureClient> vc_ash_feature_client_;

  std::unique_ptr<power::SmartChargingManager> smart_charging_manager_;

  std::unique_ptr<power::ml::AdaptiveScreenBrightnessManager>
      adaptive_screen_brightness_manager_;

  std::unique_ptr<power::auto_screen_brightness::Controller>
      auto_screen_brightness_controller_;

  std::unique_ptr<AshUsbDetector> ash_usb_detector_;
  std::unique_ptr<CrosUsbDetector> cros_usb_detector_;

  std::unique_ptr<ReportControllerInitializer> report_controller_initializer_;

  std::unique_ptr<crostini::CrostiniUnsupportedActionNotifier>
      crostini_unsupported_action_notifier_;

  std::unique_ptr<system::DarkResumeController> dark_resume_controller_;

  std::unique_ptr<BulkPrintersCalculatorFactory>
      bulk_printers_calculator_factory_;

  std::unique_ptr<FwupdDownloadClientImpl> fwupd_download_client_;

  std::unique_ptr<SessionTerminationManager> session_termination_manager_;
  base::CallbackListSubscription app_terminating_subscription_;

  std::unique_ptr<cros_healthd::internal::DataCollector>
      cros_healthd_data_collector_;

  std::unique_ptr<ash::BrowserControllerImpl> browser_controller_;

  std::unique_ptr<chromeos::MahiWebContentsManager> mahi_web_contents_manager_;

  // Set when PreProfileInit() is called. If PreMainMessageLoopRun() exits
  // early, this will be false during PostMainMessageLoopRun(), etc.
  // Used to prevent shutting down classes that were not initialized.
  bool pre_profile_init_called_ = false;
  std::unique_ptr<SigninProfileHandler> signin_profile_handler_;

  std::unique_ptr<policy::LockToSingleUserManager> lock_to_single_user_manager_;
  std::unique_ptr<LoginScreenExtensionsStorageCleaner>
      login_screen_extensions_storage_cleaner_;

  std::unique_ptr<policy::QuirksPolicyController> quirks_policy_controller_;

  std::unique_ptr<GnubbyNotification> gnubby_notification_;

  std::unique_ptr<platform_keys::KeyPermissionsManager>
      system_token_key_permissions_manager_;

  std::unique_ptr<quick_pair::QuickPairBrowserDelegateImpl>
      quick_pair_delegate_;

  std::unique_ptr<AudioSurveyHandler> audio_survey_handler_;

  std::unique_ptr<CameraGeneralSurveyHandler> camera_general_survey_handler_;

  std::unique_ptr<ApnMigrator> apn_migrator_;

  std::unique_ptr<arc::ContainerAppKiller> arc_container_app_killer_;

  // Only temporarily owned, will be null after PostCreateMainMessageLoop().
  // The Accessor is constructed before initialization of FeatureList and should
  // only be used by ChromeFeaturesServiceProvider.
  std::unique_ptr<base::FeatureList::Accessor> feature_list_accessor_;

  std::unique_ptr<ash::AuthEventsRecorder> auth_events_recorder_;
  std::unique_ptr<ash::ChromeAuthParts> auth_parts_;

  std::unique_ptr<video_conference::VideoConferenceManagerClientImpl>
      video_conference_manager_client_;

  std::unique_ptr<MisconfiguredUserCleaner> misconfigured_user_cleaner_;

  std::unique_ptr<ash::MagicBoostControllerImpl> magic_boost_controller_;

  std::unique_ptr<parent_access::ParentAccessService> parent_access_service_;

  std::unique_ptr<UserSessionManager> user_session_manager_;

  std::unique_ptr<TokenHandleStoreFactory> token_handle_store_factory_;

#if BUILDFLAG(USE_CUPS)
  std::unique_ptr<ash::LocalPrinter> local_printer_;
#endif

  std::unique_ptr<ScreenLockerController> screen_locker_controller_;

  base::WeakPtrFactory<ChromeBrowserMainPartsAsh> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_MAIN_PARTS_CHROME_BROWSER_MAIN_PARTS_ASH_H_
