// Copyright 2024 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_PASSWORD_MANAGER_CHROME_PASSWORD_CHANGE_SERVICE_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_CHROME_PASSWORD_CHANGE_SERVICE_H_

#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/password_change_delegate.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/password_manager/core/browser/password_change_service_interface.h"
#include "components/password_manager/core/browser/password_form.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/password_manager/password_change/password_change_from_checkup_delegate.h"
#endif  // BUILDFLAG(IS_ANDROID)

class GURL;

namespace autofill {
class LogRouter;
}  // namespace autofill

namespace affiliations {
class AffiliationService;
}

class OptimizationGuideKeyedService;

namespace content {
class WebContents;
}

namespace password_manager {
class PasswordFeatureManager;
class PasswordManagerSettingsService;
struct StoredCredential;
}  // namespace password_manager

class PrefService;

// Password change availability state used for UMA. Corresponds to
// `PasswordChangeAvailability` in enums.xml.
//
// These values are persisted to logs.
// Entries should not be renumbered and numeric values should never be reused.
// LINT.IfChange(PasswordChangeAvailability)
enum class PasswordChangeAvailability {
  kAvailable = 0,
  kPasswordGenerationDisabled = 1,
  kModelExecutionNotAllowed = 2,
  kPasswordSavingDisabled = 3,
  kDisabledByPolicy = 4,
  // Obsolete kFeatureDisabled = 5,
  // Obsolete kUnsupportedLanguage = 6,
  // Obsolete kUnsupportedCountryCode = 7,
  kNotSupportedSite = 8,
  kNoSavedPasswords = 9,
  kThrottled = 10,
  kSignupForm = 11,
  kNonPasswordLogin = 12,
  kInvisiblePasswordField = 13,
  kDisabledByUser = 14,
  kMaxValue = kDisabledByUser,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/password/enums.xml:PasswordChangeAvailability)

class ChromePasswordChangeService
    : public KeyedService,
      public password_manager::PasswordChangeServiceInterface,
      public PasswordChangeDelegate::Observer {
 public:
  // Callback which allows to open a new tab for password change. Useful for
  // testing UI interactions in browser tests.
  using OpenNewTabCallback =
      base::RepeatingCallback<content::WebContents*(const GURL&,
                                                    content::WebContents*)>;
  static constexpr char kHasPasswordChangeUrlHistogram[] =
      "PasswordManager.HasPasswordChangeUrl";

  ChromePasswordChangeService(
      PrefService* pref_service,
      affiliations::AffiliationService* affiliation_service,
      OptimizationGuideKeyedService* optimization_keyed_service,
      password_manager::PasswordManagerSettingsService* settings_service,
      std::unique_ptr<password_manager::PasswordFeatureManager> feature_manager,
      autofill::LogRouter* log_router);
  ~ChromePasswordChangeService() override;

  // Indicates that password change will be proposed to the user for a given
  // `credentials`. `originator` belongs to a tab which initiated the process.
  virtual void OfferPasswordChangeUi(password_manager::PasswordForm credentials,
                                     content::WebContents* originator);

  // Responds with PasswordChangeDelegate for a given `web_contents`.
  // The same object is returned for a tab which initiated password change and a
  // tab where password change is performed. Returns nullptr if `web_contents`
  // isn't associated with any delegate.
  virtual PasswordChangeDelegate* GetPasswordChangeDelegate(
      content::WebContents* web_contents);

#if !BUILDFLAG(IS_ANDROID)
  // Starts the password change flow from the Password Checkup page for the
  // given `credential`.
  virtual base::WeakPtr<PasswordChangeFromCheckupDelegate>
  StartPasswordChangeFromCheckup(
      password_manager::StoredCredential credential,
      content::WebContents* web_contents,
      PasswordChangeFromCheckupDelegate::StateChangeCallback callback);
#endif

  // PasswordChangeServiceInterface implementation.
  bool IsPasswordChangeAvailable() const override;
  bool IsPasswordChangeSupported(
      const password_manager::PasswordForm& form,
      bool is_non_password_login_detected) const override;
  void RecordLoginAttemptQuality(
      password_manager::LogInWithChangedPasswordOutcome login_outcome,
      const GURL& page_url) const override;

  // Add overridden change password URL.
  void AddChangePasswordUrlOverride(const GURL& url) override;

  // Checks if user has interacted with the feature and only then general
  // availability.
  bool UserIsActivePasswordChangeUser() const;

 private:
  // PasswordChangeDelegate::Observer impl.
  void OnPasswordChangeStopped(PasswordChangeDelegate* delegate) override;

  // KeyedService impl.
  void Shutdown() override;

#if !BUILDFLAG(IS_ANDROID)
  PasswordChangeAvailability GetGeneralAvailability() const;

  bool HasChangePasswordUrlOverride() const;
  GURL GetChangePasswordURLOverride(const GURL& url) const;
  PasswordChangeAvailability GetPerSiteAvailability(
      const password_manager::PasswordForm& form,
      bool is_non_password_login_detected = false) const;
#endif

  const raw_ptr<PrefService> pref_service_;
  const raw_ptr<affiliations::AffiliationService> affiliation_service_;
  const raw_ptr<OptimizationGuideKeyedService> optimization_keyed_service_;
  const raw_ptr<password_manager::PasswordManagerSettingsService>
      settings_service_;
  std::unique_ptr<password_manager::PasswordFeatureManager> feature_manager_;

  std::vector<std::unique_ptr<PasswordChangeDelegate>>
      password_change_delegates_;

  // The router for logs. Maybe be null in tests.
  const raw_ptr<autofill::LogRouter> log_router_;

#if !BUILDFLAG(IS_ANDROID)
  std::vector<std::unique_ptr<PasswordChangeFromCheckupDelegate>>
      password_change_from_checkup_delegates_;
#endif

  std::vector<GURL> override_urls_;

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

#endif  // CHROME_BROWSER_PASSWORD_MANAGER_CHROME_PASSWORD_CHANGE_SERVICE_H_
