// Copyright 2019 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_SSL_KNOWN_INTERCEPTION_DISCLOSURE_INFOBAR_DELEGATE_H_
#define CHROME_BROWSER_SSL_KNOWN_INTERCEPTION_DISCLOSURE_INFOBAR_DELEGATE_H_

#include <algorithm>
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar_delegate.h"
#include "net/cert/cert_status_flags.h"
#include "url/gurl.h"

namespace base {
class Clock;
}  // namespace base

namespace content {
class WebContents;
}

namespace user_prefs {
class PrefRegistrySyncable;
}  // namespace user_prefs

class Profile;

// Singleton that tracks the known interception disclosure cooldown time. On
// Android, this is measured across browser sessions (which tend to be short) by
// storing the last dismissal time in a pref. On Desktop, the last dismissal
// time is stored in memory, so this is is only measured within the same
// browsing session (and thus will trigger on every browser startup).
class KnownInterceptionDisclosureCooldown {
 public:
  static KnownInterceptionDisclosureCooldown* GetInstance();

  bool IsActive(Profile* profile);
  void Activate(Profile* profile);

  bool get_has_seen_known_interception() {
    return has_seen_known_interception_;
  }
  void set_has_seen_known_interception(bool has_seen) {
    has_seen_known_interception_ = has_seen;
  }

  void SetClockForTesting(std::unique_ptr<base::Clock> clock);

 private:
  friend struct base::DefaultSingletonTraits<
      KnownInterceptionDisclosureCooldown>;

  KnownInterceptionDisclosureCooldown();
  ~KnownInterceptionDisclosureCooldown();

  std::unique_ptr<base::Clock> clock_ = std::make_unique<base::DefaultClock>();
  bool has_seen_known_interception_ = false;

#if !BUILDFLAG(IS_ANDROID)
  base::Time last_dismissal_time_;
#endif
};

// Shows the known interception disclosure UI if it has not been recently
// dismissed.
void MaybeShowKnownInterceptionDisclosureDialog(
    content::WebContents* web_contents,
    net::CertStatus cert_status);

class KnownInterceptionDisclosureInfoBarDelegate
    : public ConfirmInfoBarDelegate {
 public:
  explicit KnownInterceptionDisclosureInfoBarDelegate(Profile* profile);
  ~KnownInterceptionDisclosureInfoBarDelegate() override = default;

  // ConfirmInfoBarDelegate:
  infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
  infobars::InfoBarDelegate::InfobarPriority GetPriority() const override;
  std::u16string GetLinkText() const override;
  GURL GetLinkURL() const override;
  bool ShouldExpire(const NavigationDetails& details) const override;
  void InfoBarDismissed() override;
  std::u16string GetMessageText() const override;
  int GetButtons() const override;
  bool Accept() override;

#if BUILDFLAG(IS_ANDROID)
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
#endif

 private:
  raw_ptr<Profile> profile_;
};

#endif  // CHROME_BROWSER_SSL_KNOWN_INTERCEPTION_DISCLOSURE_INFOBAR_DELEGATE_H_
