Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search result ad aggressive mode checks. #17196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/mock_ads_service.h"
#include "brave/components/brave_ads/common/features.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/constants/brave_paths.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "content/public/test/test_navigation_observer.h"
Expand Down Expand Up @@ -176,6 +179,26 @@ IN_PROC_BROWSER_TEST_F(SearchResultAdTest, NotAllowedDomain) {
EXPECT_EQ(url, web_contents->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(SearchResultAdTest, AggressiveAdBlockingMode) {
ScopedTestingAdsServiceSetter scoped_setter(ads_service());
GURL url = GetURL(kAllowedDomain, kSearchResultUrlPath);

auto* settings_map =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
brave_shields::SetCosmeticFilteringControlType(
settings_map, brave_shields::ControlType::BLOCK, url);
brave_shields::SetAdControlType(settings_map,
brave_shields::ControlType::BLOCK, url);

EXPECT_CALL(*ads_service(), IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(*ads_service(), TriggerSearchResultAdEvent(_, _)).Times(0);

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(url, web_contents->GetVisibleURL());
}

IN_PROC_BROWSER_TEST_F(SearchResultAdTest, BrokenSearchAdMetadata) {
ScopedTestingAdsServiceSetter scoped_setter(ads_service());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "brave/components/brave_ads/common/features.h"
#include "brave/components/brave_ads/content/browser/search_result_ad/search_result_ad_handler.h"
#include "brave/components/brave_search/common/brave_search_utils.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
Expand Down Expand Up @@ -76,14 +77,16 @@ void SearchResultAdTabHelper::DidFinishNavigation(

MaybeProcessSearchResultAdClickedEvent(navigation_handle);

auto* settings_map = HostContentSettingsMapFactory::GetForProfile(
web_contents()->GetBrowserContext());
const bool should_trigger_viewed_event =
navigation_handle->GetRestoreType() ==
content::RestoreType::kNotRestored &&
!(navigation_handle->GetPageTransition() &
ui::PAGE_TRANSITION_FORWARD_BACK);
search_result_ad_handler_ =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
GetAdsService(), navigation_handle->GetURL(),
GetAdsService(), navigation_handle->GetURL(), settings_map,
should_trigger_viewed_event);
}

Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/content/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static_library("browser") {
"//brave/components/brave_ads/common",
"//brave/components/brave_ads/core/browser",
"//brave/components/brave_search/common",
"//brave/components/brave_shields/browser",
"//components/content_settings/core/browser",
"//components/schema_org/common:mojom",
"//content/public/browser",
"//mojo/public/cpp/bindings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@
#include "brave/components/brave_ads/core/browser/search_result_ad/search_result_ad_converting_util.h"
#include "brave/components/brave_ads/core/browser/search_result_ad/search_result_ad_util.h"
#include "brave/components/brave_search/common/brave_search_utils.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/render_frame_host.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"

namespace brave_ads {

namespace {

bool IsSearchResultAdBlocked(HostContentSettingsMap* settings_map,
const GURL& site_url) {
DCHECK(settings_map);

if (!brave_shields::GetBraveShieldsEnabled(settings_map, site_url)) {
return false;
}

const brave_shields::ControlType control_type_ad =
brave_shields::GetAdControlType(settings_map, site_url);
if (control_type_ad == brave_shields::ControlType::ALLOW) {
return false;
}

const brave_shields::ControlType control_type_cosmetic =
brave_shields::GetCosmeticFilteringControlType(settings_map, site_url);
return control_type_cosmetic == brave_shields::ControlType::BLOCK;
}

} // namespace

SearchResultAdHandler::SearchResultAdHandler(
AdsService* ads_service,
const bool should_trigger_viewed_event)
Expand All @@ -35,11 +60,13 @@ std::unique_ptr<SearchResultAdHandler>
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
AdsService* ads_service,
const GURL& url,
HostContentSettingsMap* settings_map,
const bool should_trigger_viewed_event) {
if (!ads_service || !ads_service->IsEnabled() ||
!base::FeatureList::IsEnabled(
features::kSupportBraveSearchResultAdConfirmationEvents) ||
!brave_search::IsAllowedHost(url)) {
!brave_search::IsAllowedHost(url) ||
IsSearchResultAdBlocked(settings_map, url)) {
return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "third_party/blink/public/mojom/document_metadata/document_metadata.mojom.h"

class GURL;
class HostContentSettingsMap;

namespace content {
class RenderFrameHost;
Expand All @@ -39,6 +40,7 @@ class SearchResultAdHandler final {
static std::unique_ptr<SearchResultAdHandler>
MaybeCreateSearchResultAdHandler(AdsService* ads_service,
const GURL& url,
HostContentSettingsMap* settings_map,
bool should_trigger_viewed_event);

void MaybeRetrieveSearchResultAd(content::RenderFrameHost* render_frame_host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
#include <utility>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.h"
#include "base/test/scoped_feature_list.h"
#include "brave/components/brave_ads/browser/mock_ads_service.h"
#include "brave/components/brave_ads/common/features.h"
#include "brave/components/brave_ads/content/browser/search_result_ad/search_result_ad_handler.h"
#include "brave/components/brave_ads/core/browser/search_result_ad/test_web_page_util.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -60,9 +65,23 @@ class SearchResultAdHandlerTest : public ::testing::Test {
mojo::Remote<blink::mojom::DocumentMetadata>(), std::move(web_page));
}

void SetUp() override {
HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
&prefs_, false /* is_off_the_record */, false /* store_last_modified */,
false /* restore_session */, false /* should_record_metrics */);
}

void TearDown() override { settings_map_->ShutdownOnUIThread(); }
thypon marked this conversation as resolved.
Show resolved Hide resolved

HostContentSettingsMap* settings_map() { return settings_map_.get(); }

protected:
MockAdsService ads_service_;
content::BrowserTaskEnvironment task_environment_;
base::test::ScopedFeatureList feature_list_;
MockAdsService ads_service_;
sync_preferences::TestingPrefServiceSyncable prefs_;
scoped_refptr<HostContentSettingsMap> settings_map_;
};

TEST_F(SearchResultAdHandlerTest,
Expand All @@ -71,7 +90,7 @@ TEST_F(SearchResultAdHandlerTest,

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);

EXPECT_FALSE(search_result_ad_handler.get());
Expand All @@ -81,7 +100,8 @@ TEST_F(SearchResultAdHandlerTest,
IncognitoModeMaybeCreateSearchResultAdHandler) {
auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
nullptr, GURL(kAllowedDomain), /*should_trigger_viewed_event*/ true);
nullptr, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);

EXPECT_FALSE(search_result_ad_handler.get());
}
Expand All @@ -92,12 +112,61 @@ TEST_F(SearchResultAdHandlerTest,

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kNotAllowedDomain),
&ads_service_, GURL(kNotAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);

EXPECT_FALSE(search_result_ad_handler.get());
}

TEST_F(SearchResultAdHandlerTest,
CosmeticFilteringMaybeCreateSearchResultAdHandler) {
const GURL url = GURL(kAllowedDomain);
EXPECT_CALL(ads_service_, IsEnabled()).WillRepeatedly(Return(true));

// Aggressively block ads.
brave_shields::SetCosmeticFilteringControlType(
settings_map(), brave_shields::ControlType::BLOCK, url);
brave_shields::SetAdControlType(settings_map(),
brave_shields::ControlType::BLOCK, url);
auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, url, settings_map(),
/*should_trigger_viewed_event*/ true);
EXPECT_FALSE(search_result_ad_handler.get());

// Disable aggressive mode.
brave_shields::SetCosmeticFilteringControlType(
settings_map(), brave_shields::ControlType::BLOCK_THIRD_PARTY, url);
search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, url, settings_map(),
/*should_trigger_viewed_event*/ true);
EXPECT_TRUE(search_result_ad_handler.get());
brave_shields::SetCosmeticFilteringControlType(
settings_map(), brave_shields::ControlType::BLOCK, url);

// Allow ads.
brave_shields::SetAdControlType(settings_map(),
brave_shields::ControlType::ALLOW, url);
search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, url, settings_map(),
/*should_trigger_viewed_event*/ true);
EXPECT_TRUE(search_result_ad_handler.get());
brave_shields::SetAdControlType(settings_map(),
brave_shields::ControlType::BLOCK, url);

// Disable Brave Shields.
brave_shields::SetAdControlType(settings_map(),
brave_shields::ControlType::BLOCK, url);
brave_shields::SetBraveShieldsEnabled(settings_map(), false, url);
search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, url, settings_map(),
/*should_trigger_viewed_event*/ true);
EXPECT_TRUE(search_result_ad_handler.get());
}

TEST_F(SearchResultAdHandlerTest, NullWebPage) {
EXPECT_CALL(ads_service_, IsEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(ads_service_,
Expand All @@ -111,7 +180,7 @@ TEST_F(SearchResultAdHandlerTest, NullWebPage) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -135,7 +204,7 @@ TEST_F(SearchResultAdHandlerTest, EmptyWebPage) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -159,7 +228,7 @@ TEST_F(SearchResultAdHandlerTest, NotValidSearchResultAd) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -186,7 +255,7 @@ TEST_F(SearchResultAdHandlerTest, EmptyConversions) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -204,7 +273,7 @@ TEST_F(SearchResultAdHandlerTest, BraveAdsBecomeDisabled) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());
Mock::VerifyAndClearExpectations(&ads_service_);
Expand Down Expand Up @@ -241,7 +310,7 @@ TEST_F(SearchResultAdHandlerTest, BraveAdsViewedClicked) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -267,7 +336,7 @@ TEST_F(SearchResultAdHandlerTest, BraveAdsTabRestored) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ false);
ASSERT_TRUE(search_result_ad_handler.get());

Expand All @@ -293,7 +362,7 @@ TEST_F(SearchResultAdHandlerTest, WrongClickedUrl) {

auto search_result_ad_handler =
SearchResultAdHandler::MaybeCreateSearchResultAdHandler(
&ads_service_, GURL(kAllowedDomain),
&ads_service_, GURL(kAllowedDomain), settings_map(),
/*should_trigger_viewed_event*/ true);
ASSERT_TRUE(search_result_ad_handler.get());

Expand Down