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

#include "chrome/browser/ui/page_info/chrome_page_info_delegate.h"

#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/page_specific_content_settings_delegate.h"
#include "chrome/browser/hid/hid_chooser_context.h"
#include "chrome/browser/hid/hid_chooser_context_factory.h"
#include "chrome/browser/infobars/browser_infobar_manager.h"
#include "chrome/browser/infobars/infobar_features.h"
#include "chrome/browser/permissions/permission_actions_history_factory.h"
#include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/browser/picture_in_picture/auto_picture_in_picture_tab_helper.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/chrome_password_protection_service.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/serial/serial_chooser_context.h"
#include "chrome/browser/serial/serial_chooser_context_factory.h"
#include "chrome/browser/ssl/chrome_security_state_util.h"
#include "chrome/browser/ssl/https_upgrades_util.h"
#include "chrome/browser/ssl/stateful_ssl_host_state_delegate_factory.h"
#include "chrome/browser/subresource_filter/subresource_filter_profile_context_factory.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/safe_browsing/android/suspicious_site_controller_android.h"
#endif
#include "chrome/browser/ui/url_identity.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/vr/vr_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/page_info/core/features.h"
#include "components/permissions/contexts/bluetooth_chooser_context.h"
#include "components/permissions/object_permission_context_base.h"
#include "components/permissions/permission_manager.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/ui_manager.h"
#include "components/safe_browsing/core/browser/suspicious_site_warning_allowlist.h"
#include "components/security_interstitials/content/stateful_ssl_host_state_delegate.h"
#include "components/subresource_filter/content/browser/subresource_filter_content_settings_manager.h"
#include "components/subresource_filter/content/browser/subresource_filter_profile_context.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "media/base/media_switches.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/events/event.h"
#include "url/origin.h"
#include "url/url_constants.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/grit/branded_strings.h"
#include "ui/base/l10n/l10n_util.h"
#else
#include "chrome/browser/certificate_viewer.h"
#include "chrome/browser/infobars/infobar_spec.h"
#include "chrome/browser/lookalikes/safety_tip_ui_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_web_contents_delegate/browser_web_contents_delegate.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/global_browser_collection.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service_factory.h"
#include "chrome/browser/ui/page_info/page_info_infobar_delegate.h"
#include "chrome/browser/ui/tab_dialogs.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/web_app_ui_utils.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/reload_type.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#include "chrome/browser/ash/floating_sso/floating_sso_service.h"
#include "chrome/browser/ash/floating_sso/floating_sso_service_factory.h"
#include "chrome/browser/smart_card/smart_card_permission_context.h"
#include "chrome/browser/smart_card/smart_card_permission_context_factory.h"
#endif

namespace {

// Expected URL types for `UrlIdentity::CreateFromUrl()`.
constexpr UrlIdentity::TypeSet kUrlIdentityAllowedTypes = {
    UrlIdentity::Type::kDefault, UrlIdentity::Type::kFile,
    UrlIdentity::Type::kIsolatedWebApp, UrlIdentity::Type::kChromeExtension};

constexpr UrlIdentity::FormatOptions kUrlIdentityOptions{
    .default_options = {UrlIdentity::DefaultFormatOptions::
                            kOmitSchemePathAndTrivialSubdomains}};

}  // namespace

// static
ChromePageInfoDelegate::GetBrowserCallback
ChromePageInfoDelegate::DefaultGetBrowserCallback() {
  return base::BindRepeating([](content::WebContents* contents) {
    return GlobalBrowserCollection::GetInstance()->FindBrowserWithTab(contents);
  });
}

ChromePageInfoDelegate::ChromePageInfoDelegate(
    content::WebContents* web_contents,
    GetBrowserCallback get_browser_callback)
    : get_browser_callback_(std::move(get_browser_callback)),
      web_contents_(web_contents) {
  CHECK(get_browser_callback_);
#if !BUILDFLAG(IS_ANDROID)
  sentiment_service_ =
      TrustSafetySentimentServiceFactory::GetForProfile(GetProfile());
#endif
  base::UmaHistogramBoolean("Security.PageInfo.AboutThisSiteLanguageSupported",
                            page_info::IsAboutThisSiteFeatureEnabled(
                                g_browser_process->GetApplicationLocale()));
}

ChromePageInfoDelegate::ChromePageInfoDelegate(
    content::WebContents* web_contents)
    : ChromePageInfoDelegate(web_contents, DefaultGetBrowserCallback()) {}

ChromePageInfoDelegate::~ChromePageInfoDelegate() = default;

Profile* ChromePageInfoDelegate::GetProfile() const {
  return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
}

permissions::ObjectPermissionContextBase*
ChromePageInfoDelegate::GetChooserContext(ContentSettingsType type) {
  switch (type) {
    case ContentSettingsType::USB_CHOOSER_DATA:
      return UsbChooserContextFactory::GetForProfile(GetProfile());
    case ContentSettingsType::BLUETOOTH_CHOOSER_DATA:
      if (base::FeatureList::IsEnabled(
              features::kWebBluetoothNewPermissionsBackend)) {
        return BluetoothChooserContextFactory::GetForProfile(GetProfile());
      }
      return nullptr;
    case ContentSettingsType::SERIAL_CHOOSER_DATA:
      return SerialChooserContextFactory::GetForProfile(GetProfile());
    case ContentSettingsType::HID_CHOOSER_DATA:
      return HidChooserContextFactory::GetForProfile(GetProfile());
    case ContentSettingsType::SMART_CARD_DATA:
#if BUILDFLAG(IS_CHROMEOS)
      if (base::FeatureList::IsEnabled(blink::features::kSmartCard)) {
        return &SmartCardPermissionContextFactory::GetForProfile(*GetProfile());
      }
#endif
      return nullptr;
    default:
      NOTREACHED();
  }
}

#if BUILDFLAG(FULL_SAFE_BROWSING)
safe_browsing::ChromePasswordProtectionService*
ChromePageInfoDelegate::GetChromePasswordProtectionService() const {
  return safe_browsing::ChromePasswordProtectionService::
      GetPasswordProtectionService(GetProfile());
}

safe_browsing::PasswordProtectionService*
ChromePageInfoDelegate::GetPasswordProtectionService() const {
  return GetChromePasswordProtectionService();
}

void ChromePageInfoDelegate::OnUserActionOnPasswordUi(
    safe_browsing::WarningAction action) {
  auto* chrome_password_protection_service =
      GetChromePasswordProtectionService();
  DCHECK(chrome_password_protection_service);

  chrome_password_protection_service->OnUserAction(
      web_contents_->GetWeakPtr(),
      chrome_password_protection_service
          ->reused_password_account_type_for_last_shown_warning(),
      safe_browsing::RequestOutcome::UNKNOWN,
      safe_browsing::LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
      /*verdict_token=*/"", safe_browsing::WarningUIType::PAGE_INFO, action);
}

std::u16string ChromePageInfoDelegate::GetWarningDetailText() {
  auto* chrome_password_protection_service =
      GetChromePasswordProtectionService();

  // |password_protection_service| may be null in test.
  return chrome_password_protection_service
             ? chrome_password_protection_service->GetWarningDetailText(
                   chrome_password_protection_service
                       ->reused_password_account_type_for_last_shown_warning())
             : std::u16string();
}
#endif

content::PermissionResult ChromePageInfoDelegate::GetPermissionResult(
    blink::PermissionType permission,
    const url::Origin& origin,
    const std::optional<url::Origin>& requesting_origin) {
  auto* controller = GetProfile()->GetPermissionController();
  auto descriptor = content::PermissionDescriptorUtil::
      CreatePermissionDescriptorForPermissionType(permission);
  if (requesting_origin.has_value()) {
    return controller->GetPermissionResultForOriginWithoutContext(
        descriptor, *requesting_origin, origin);
  } else {
    return controller->GetPermissionResultForOriginWithoutContext(descriptor,
                                                                  origin);
  }
}

#if !BUILDFLAG(IS_ANDROID)
void ChromePageInfoDelegate::FocusWebContents() {
  BrowserWindowInterface* browser = get_browser_callback_.Run(web_contents_);
  BrowserWebContentsDelegate::From(browser)->ActivateContents(web_contents_);
}

std::optional<std::u16string> ChromePageInfoDelegate::GetRwsOwner(
    const GURL& site_url) {
  return PrivacySandboxServiceFactory::GetForProfile(GetProfile())
      ->GetRelatedWebsiteSetOwnerForDisplay(site_url);
}

bool ChromePageInfoDelegate::IsRwsManaged(const GURL& site_url) {
  return PrivacySandboxServiceFactory::GetForProfile(GetProfile())
      ->IsPartOfManagedRelatedWebsiteSet(net::SchemefulSite(site_url));
}

// static
void ChromePageInfoDelegate::RegisterPageInfoInfoBar(
    infobars::BrowserInfoBarManager* infobar_manager) {
  auto spec =
      infobars::InfoBarSpec::Builder(
          infobars::InfoBarDelegate::PAGE_INFO_INFOBAR_DELEGATE)
          .SetMessageText(l10n_util::GetStringUTF16(IDS_PAGE_INFO_INFOBAR_TEXT))
          .SetIcon(features::IsRoundedIconsEnabled()
                       ? vector_icons::kSettingsIcon
                       : vector_icons::kSettingsChromeRefreshOldIcon)
          .SetScope(infobars::InfoBarScope::kTab)
          .AddOkButton(
              l10n_util::GetStringUTF16(IDS_PAGE_INFO_INFOBAR_BUTTON),
              base::BindRepeating([](content::WebContents* web_contents) {
                if (web_contents) {
                  web_contents->GetController().Reload(
                      content::ReloadType::NORMAL, true);
                }
              }))
          .Build();
  infobar_manager->Register(std::move(spec));
}

bool ChromePageInfoDelegate::CreateInfoBarDelegate() {
  if (infobars::IsInfoBarMigrated(
          infobars::InfoBarDelegate::PAGE_INFO_INFOBAR_DELEGATE)) {
    auto* browser_infobar_manager =
        infobars::BrowserInfoBarManager::From(g_browser_process);
    if (browser_infobar_manager) {
      auto* tab = tabs::TabInterface::MaybeGetFromContents(web_contents_);
      if (tab) {
        browser_infobar_manager->Show(
            tab, infobars::InfoBarDelegate::PAGE_INFO_INFOBAR_DELEGATE);
        return true;
      }
    }
    return false;
  }

  infobars::ContentInfoBarManager* infobar_manager =
      infobars::ContentInfoBarManager::FromWebContents(web_contents_);
  if (infobar_manager) {
    PageInfoInfoBarDelegate::Create(infobar_manager);
    return true;
  }
  return false;
}

std::unique_ptr<content_settings::CookieControlsController>
ChromePageInfoDelegate::CreateCookieControlsController() {
  Profile* profile = GetProfile();
  return std::make_unique<content_settings::CookieControlsController>(
      CookieSettingsFactory::GetForProfile(profile),
      profile->IsOffTheRecord()
          ? CookieSettingsFactory::GetForProfile(profile->GetOriginalProfile())
          : nullptr,
      HostContentSettingsMapFactory::GetForProfile(profile),
      profile->IsIncognitoProfile());
}

bool ChromePageInfoDelegate::IsIsolatedWebApp() {
  CHECK(web_contents_);
  web_app::WebAppProvider* provider =
      web_app::WebAppProvider::GetForWebContents(web_contents_);
  if (!provider) {
    return false;
  }

  const webapps::AppId* app_id =
      web_app::WebAppTabHelper::GetAppId(web_contents_);
  return app_id && provider->registrar_unsafe().AppMatches(
                       *app_id, web_app::WebAppFilter::IsIsolatedApp() |
                                    web_app::WebAppFilter::IsIsolatedSubApp());
}

bool ChromePageInfoDelegate::IsSubApp() {
  CHECK(web_contents_);
  web_app::WebAppProvider* provider =
      web_app::WebAppProvider::GetForWebContents(web_contents_);
  if (!provider) {
    return false;
  }

  const webapps::AppId* app_id =
      web_app::WebAppTabHelper::GetAppId(web_contents_);
  return app_id && provider->registrar_unsafe().AppMatches(
                       *app_id, web_app::WebAppFilter::IsIsolatedSubApp());
}

bool ChromePageInfoDelegate::HasSubApps() {
  CHECK(web_contents_);
  web_app::WebAppProvider* provider =
      web_app::WebAppProvider::GetForWebContents(web_contents_);
  if (!provider) {
    return false;
  }

  const webapps::AppId* app_id =
      web_app::WebAppTabHelper::GetAppId(web_contents_);
  return app_id &&
         !provider->registrar_unsafe().GetAllSubAppIds(*app_id).empty();
}

void ChromePageInfoDelegate::ShowSiteSettings(const GURL& site_url) {
  if (web_app::HandleAppManagementLinkClickedInPageInfo(web_contents_)) {
    return;
  }

  BrowserWindowInterface* browser = get_browser_callback_.Run(web_contents_);
  chrome::ShowSiteSettings(browser, site_url);
}

void ChromePageInfoDelegate::ShowCookiesSettings() {
  BrowserWindowInterface* browser = get_browser_callback_.Run(web_contents_);
  chrome::ShowSettingsSubPage(browser, chrome::kCookieSettingsSubPage);
}

void ChromePageInfoDelegate::ShowAllSitesSettingsFilteredByRwsOwner(
    const std::u16string& rws_owner) {
  BrowserWindowInterface* browser = get_browser_callback_.Run(web_contents_);
  chrome::ShowAllSitesSettingsFilteredByRwsOwner(browser,
                                                 base::UTF16ToUTF8(rws_owner));
}

void ChromePageInfoDelegate::ShowSyncSettings() {
  BrowserWindowInterface* browser = get_browser_callback_.Run(web_contents_);
  chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupSubPage);
}

void ChromePageInfoDelegate::OpenCookiesDialog() {
  FocusWebContents();
  TabDialogs::FromWebContents(web_contents_)->ShowCollectedCookies();
}

void ChromePageInfoDelegate::OpenCertificateDialog(
    net::X509Certificate* certificate) {
  gfx::NativeWindow top_window = web_contents_->GetTopLevelNativeWindow();
  DCHECK(certificate);
  DCHECK(top_window);

  FocusWebContents();
  ShowCertificateViewer(web_contents_, top_window, certificate);
}

void ChromePageInfoDelegate::OpenConnectionHelpCenterPage(
    const ui::Event& event) {
  web_contents_->OpenURL(
      content::OpenURLParams(
          GURL(chrome::kPageInfoHelpCenterURL), content::Referrer(),
          ui::DispositionFromEventFlags(
              event.flags(), WindowOpenDisposition::NEW_FOREGROUND_TAB),
          ui::PAGE_TRANSITION_LINK, false),
      /*navigation_handle_callback=*/{});
}

void ChromePageInfoDelegate::OpenSafetyTipHelpCenterPage() {
  OpenHelpCenterFromSafetyTip(web_contents_);
}

void ChromePageInfoDelegate::OpenContentSettingsExceptions(
    ContentSettingsType content_settings_type) {
  if (content_settings_type == ContentSettingsType::FILE_SYSTEM_WRITE_GUARD) {
    const GURL& url = web_contents_->GetLastCommittedURL();
    chrome::ShowSiteSettingsFileSystem(GetProfile(), url);
    return;
  }
  chrome::ShowContentSettingsExceptionsForProfile(GetProfile(),
                                                  content_settings_type);
}

void ChromePageInfoDelegate::OnPageInfoActionOccurred(
    page_info::PageInfoAction action) {
  if (sentiment_service_) {
    if (action == page_info::PAGE_INFO_OPENED) {
      sentiment_service_->PageInfoOpened();
    } else {
      sentiment_service_->InteractedWithPageInfo();
    }
  }
}

void ChromePageInfoDelegate::OnUIClosing() {
  if (sentiment_service_) {
    sentiment_service_->PageInfoClosed();
  }
}
#endif

void ChromePageInfoDelegate::OpenSafeBrowsingHelpCenterPage(
    const ui::Event* event,
    bool is_suspicious_site) {
  int event_flags = event ? event->flags() : 0;
  const char* const url = is_suspicious_site
                              ? chrome::kUnsafeSiteWarningHelpCenterURL
                              : chrome::kSafeBrowsingHelpCenterURL;
  web_contents_->OpenURL(
      content::OpenURLParams(
          GURL(url), content::Referrer(),
          ui::DispositionFromEventFlags(
              event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB),
          ui::PAGE_TRANSITION_LINK, false),
      /*navigation_handle_callback=*/{});
}

void ChromePageInfoDelegate::OnSuspiciousSiteBackToSafety() {
#if BUILDFLAG(IS_ANDROID)
  if (auto* ssc =
          safe_browsing::SuspiciousSiteControllerAndroid::FromWebContents(
              web_contents_)) {
    ssc->HandleBackNavigation(
        safe_browsing::SuspiciousSiteWarningUserInteraction::
            kBackToSafetyButton);
    return;
  }
#endif
  if (!web_contents_) {
    return;
  }

  auto& controller = web_contents_->GetController();
  const GURL& current_url = web_contents_->GetLastCommittedURL();

  // Find the most recent navigation entry that belongs to a different site.
  for (int i = controller.GetLastCommittedEntryIndex() - 1; i >= 0; --i) {
    content::NavigationEntry* entry = controller.GetEntryAtIndex(i);
    if (entry && !entry->GetURL().is_empty() &&
        !net::registry_controlled_domains::SameDomainOrHost(
            current_url, entry->GetURL(),
            net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
      controller.GoToIndex(i);
      return;
    }
  }

  // If there is no previous entry on a different site, navigate to the New Tab
  // Page.
  controller.LoadURLWithParams(content::NavigationController::LoadURLParams(
      GURL(chrome::kChromeUINewTabURL)));
}

void ChromePageInfoDelegate::OnSuspiciousSiteMarkAsSafe() {
#if BUILDFLAG(IS_ANDROID)
  if (auto* ssc =
          safe_browsing::SuspiciousSiteControllerAndroid::FromWebContents(
              web_contents_)) {
    ssc->OnContinueButtonClicked();
    return;
  }
#endif
  if (!web_contents_) {
    return;
  }
  const GURL& current_url = web_contents_->GetLastCommittedURL();
  if (!current_url.is_valid() || current_url.host().empty()) {
    return;
  }
  Profile* profile = GetProfile();
  if (profile) {
    HostContentSettingsMap* hcsm =
        HostContentSettingsMapFactory::GetForProfile(profile);
    if (hcsm) {
      safe_browsing::SuspiciousSiteWarningAllowlist(hcsm).AllowSiteForHost(
          std::string(current_url.host()));
    }
  }
}

std::u16string ChromePageInfoDelegate::GetSubjectName(const GURL& url) {
  CHECK(web_contents_);
  return UrlIdentity::CreateFromUrl(GetProfile(), url, kUrlIdentityAllowedTypes,
                                    kUrlIdentityOptions)
      .name;
}

permissions::PermissionDecisionAutoBlocker*
ChromePageInfoDelegate::GetPermissionDecisionAutoblocker() {
  return PermissionDecisionAutoBlockerFactory::GetForProfile(GetProfile());
}

permissions::PermissionActionsHistory*
ChromePageInfoDelegate::GetPermissionActionsHistory() {
  return PermissionActionsHistoryFactory::GetForProfile(GetProfile());
}

StatefulSSLHostStateDelegate*
ChromePageInfoDelegate::GetStatefulSSLHostStateDelegate() {
  return StatefulSSLHostStateDelegateFactory::GetForProfile(GetProfile());
}

HostContentSettingsMap* ChromePageInfoDelegate::GetContentSettings() {
  return HostContentSettingsMapFactory::GetForProfile(GetProfile());
}

bool ChromePageInfoDelegate::IsSubresourceFilterActivated(
    const GURL& site_url) {
  subresource_filter::SubresourceFilterContentSettingsManager*
      settings_manager =
          SubresourceFilterProfileContextFactory::GetForProfile(GetProfile())
              ->settings_manager();

  return settings_manager->GetSiteActivationFromMetadata(site_url);
}

bool ChromePageInfoDelegate::HasAutoPictureInPictureBeenRegistered() {
#if BUILDFLAG(IS_ANDROID)
  if (!base::FeatureList::IsEnabled(media::kAutoPictureInPictureAndroid)) {
    return false;
  }
#endif
  auto* auto_pip_tab_helper =
      AutoPictureInPictureTabHelper::FromWebContents(web_contents_);
  return auto_pip_tab_helper &&
         auto_pip_tab_helper->HasAutoPictureInPictureBeenRegistered();
}

bool ChromePageInfoDelegate::IsContentDisplayedInVrHeadset() {
  return vr::VrTabHelper::IsContentDisplayedInHeadset(web_contents_);
}

security_state::SecurityLevel ChromePageInfoDelegate::GetSecurityLevel() {
  if (security_state_for_tests_set_) {
    return security_level_for_tests_;
  }

  return chrome_security_state::GetSecurityLevel(web_contents_);
}

security_state::VisibleSecurityState
ChromePageInfoDelegate::GetVisibleSecurityState() {
  if (security_state_for_tests_set_) {
    return visible_security_state_for_tests_;
  }

  return *chrome_security_state::GetVisibleSecurityState(web_contents_);
}

void ChromePageInfoDelegate::OnCookiesPageOpened() {
  auto* profile = GetProfile();
  auto cookie_settings = CookieSettingsFactory::GetForProfile(profile);
  // Don't record the preference if 3PC are allowed by default. Since then
  // cookie controls are not available in the cookies page.
  if (!cookie_settings || !cookie_settings->ShouldBlockThirdPartyCookies()) {
    return;
  }

  profile->GetPrefs()->SetBoolean(prefs::kInContextCookieControlsOpened, true);
}

std::unique_ptr<content_settings::PageSpecificContentSettings::Delegate>
ChromePageInfoDelegate::GetPageSpecificContentSettingsDelegate() {
  auto delegate =
      std::make_unique<PageSpecificContentSettingsDelegate>(web_contents_);
  return std::move(delegate);
}

#if BUILDFLAG(IS_ANDROID)
const std::u16string ChromePageInfoDelegate::GetClientApplicationName() {
  return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
}
#endif

bool ChromePageInfoDelegate::IsHttpsFirstModeEnabledForUrl(const GURL& url) {
  return IsInterstitialEnabled(ComputeInterstitialState(web_contents_, url));
}

bool ChromePageInfoDelegate::IsIncognitoProfile() {
  return GetProfile()->IsIncognitoProfile();
}

void ChromePageInfoDelegate::SetSecurityStateForTests(
    security_state::SecurityLevel security_level,
    security_state::VisibleSecurityState visible_security_state) {
  security_state_for_tests_set_ = true;
  security_level_for_tests_ = security_level;
  visible_security_state_for_tests_ = visible_security_state;
}

#if BUILDFLAG(IS_CHROMEOS)
bool ChromePageInfoDelegate::ShouldSyncCookiesForUrl(const GURL& url) {
  if (!ash::features::IsFloatingSsoAllowed()) {
    return false;
  }
  // Floating SSO is an internal name for the feature which can sync cookies for
  // ChromeOS enterprise users.
  auto* floating_sso_service =
      ash::floating_sso::FloatingSsoServiceFactory::GetForProfile(GetProfile());
  // Even when cookie sync is enabled, it isn't applied to every site.
  return floating_sso_service && floating_sso_service->IsFloatingSsoEnabled() &&
         floating_sso_service->ShouldSyncCookiesForUrl(url);
}
#endif
