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

#include "content/public/browser/btm_service.h"

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/btm_service_test_utils.h"
#include "net/http/http_status_code.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
bool Has3pcException(BrowserContext* browser_context,
                     WebContents* web_contents,
                     const GURL& url,
                     const GURL& initial_url,
                     const GURL& final_url) {
  BtmRedirectPtr redirect = BtmRedirect::CreateForServer(
      url, ukm::kInvalidSourceId, BtmDataAccessType::kWrite, base::Time::Now(),
      false, net::HTTP_FOUND, base::TimeDelta());
  Populate3PcExceptions(browser_context, web_contents, initial_url, final_url,
                        base::span_from_ref(redirect));
  return redirect->has_3pc_exception.value();
}

class BtmService3pcExceptionsTest : public testing::Test {
 public:
  BtmService3pcExceptionsTest()
      : cookie_settings_(
            CookieSettingsFactory::GetForProfile(&profile_).get()) {}

  TestingProfile* GetProfile() { return &profile_; }

 protected:
  BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
  raw_ptr<content_settings::CookieSettings> cookie_settings_;

  void SetUp() override {
    profile_.GetPrefs()->SetInteger(
        prefs::kCookieControlsMode,
        static_cast<int>(
            content_settings::CookieControlsMode::kBlockThirdParty));
    ASSERT_FALSE(Are3PcsGenerallyEnabled());
  }

  // Add an exception to the third-party cookie blocking rule for
  // `third_party_url` embedded by `first_party_url`.
  void Add3PCException(const GURL& first_party_url,
                       const GURL& third_party_url) {
    HostContentSettingsMapFactory::GetForProfile(&profile_)
        ->SetContentSettingDefaultScope(third_party_url, first_party_url,
                                        ContentSettingsType::COOKIES,
                                        CONTENT_SETTING_ALLOW);

    auto* client = GetContentClientForTesting()->browser();
    ASSERT_TRUE(client->IsFullCookieAccessAllowed(
        &profile_, nullptr, third_party_url,
        blink::StorageKey::CreateFirstParty(
            url::Origin::Create(first_party_url)),
        /*overrides=*/{}));
    ASSERT_FALSE(client->IsFullCookieAccessAllowed(
        &profile_, nullptr, first_party_url,
        blink::StorageKey::CreateFirstParty(
            url::Origin::Create(third_party_url)),
        /*overrides=*/{}));
  }

  bool Are3PcsGenerallyEnabled() {
    return ::content::Are3PcsGenerallyEnabled(&profile_, nullptr);
  }
};

// Verifies that redirect chains that start or end on embedder URLs do not
// have 3PC exceptions.
TEST_F(BtmService3pcExceptionsTest, EmbedderUrl_DoesNotHaveException) {
  GURL non_web_url = chrome::ChromeUINewTabURLAsGURL();
  GURL redirect_url("https://redirect.com");

  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url, non_web_url,
                               GURL("https://final.com")));
  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url,
                               GURL("https://initial.com"), non_web_url));

  // Verify that exception logic is working as expected for HTTP(S) URLs.
  EXPECT_FALSE(Has3pcException(GetProfile(), nullptr, redirect_url,
                               GURL("https://initial.com"),
                               GURL("https://final.com")));
  Add3PCException(GURL("https://initial.com"), redirect_url);
  EXPECT_TRUE(Has3pcException(GetProfile(), nullptr, redirect_url,
                              GURL("https://initial.com"),
                              GURL("https://final.com")));
}

}  // namespace content
