Skip to content

Commit

Permalink
[ads] Close search result ad infobar after the user clicks "Learn more"
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Sep 6, 2024
1 parent 9e4d009 commit 9c839b3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ void CreativeSearchResultAdClickedInfoBarDelegate::Create(
CHECK(web_contents);
CHECK(prefs);

infobars::ContentInfoBarManager* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(web_contents);
if (!infobar_manager) {
return;
}

if (!prefs->GetBoolean(prefs::kShouldShowSearchResultAdClickedInfoBar)) {
return;
}
prefs->SetBoolean(prefs::kShouldShowSearchResultAdClickedInfoBar, false);

infobars::ContentInfoBarManager* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(web_contents);
if (!infobar_manager) {
return;
}
infobar_manager->AddInfoBar(
CreateConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate>(
new CreativeSearchResultAdClickedInfoBarDelegate())));
Expand Down Expand Up @@ -87,4 +86,11 @@ GURL CreativeSearchResultAdClickedInfoBarDelegate::GetLinkURL() const {
return GURL(kLearnMoreUrl);
}

bool CreativeSearchResultAdClickedInfoBarDelegate::LinkClicked(
WindowOpenDisposition disposition) {
ConfirmInfoBarDelegate::LinkClicked(disposition);
// Return true to immediately close the infobar.
return true;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CreativeSearchResultAdClickedInfoBarDelegate
int GetButtons() const override;
std::u16string GetLinkText() const override;
GURL GetLinkURL() const override;
bool LinkClicked(WindowOpenDisposition disposition) override;
};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool CreativeSearchResultAdTabHelper::ShouldHandleCreativeAdEvents() const {

void CreativeSearchResultAdTabHelper::MaybeTriggerCreativeAdClickedEvent(
const GURL& url,
base::OnceCallback<void(bool success)> callback) {
TriggerAdEventCallback callback) {
if (creative_search_result_ad_handler_) {
creative_search_result_ad_handler_->MaybeTriggerCreativeAdClickedEvent(
url, std::move(callback));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include "base/memory/weak_ptr.h"
#include "brave/components/brave_ads/core/public/ads_callback.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

Expand Down Expand Up @@ -40,9 +41,8 @@ class CreativeSearchResultAdTabHelper

bool ShouldHandleCreativeAdEvents() const;

void MaybeTriggerCreativeAdClickedEvent(
const GURL& url,
base::OnceCallback<void(bool success)> callback);
void MaybeTriggerCreativeAdClickedEvent(const GURL& url,
TriggerAdEventCallback callback);

private:
friend class content::WebContentsUserData<CreativeSearchResultAdTabHelper>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void CreativeSearchResultAdHandler::

void CreativeSearchResultAdHandler::MaybeTriggerCreativeAdClickedEvent(
const GURL& url,
base::OnceCallback<void(bool success)> callback) {
TriggerAdEventCallback callback) {
if (!creative_search_result_ads_) {
// No creative search result ads are present on the web page.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_mojom_web_page_entities_extractor.h"
#include "brave/components/brave_ads/core/public/ads_callback.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/mojom/document_metadata/document_metadata.mojom-forward.h"

Expand Down Expand Up @@ -53,9 +54,8 @@ class CreativeSearchResultAdHandler final {
ExtractCreativeAdPlacementIdsFromWebPageCallback callback);

void MaybeTriggerCreativeAdViewedEvent(const std::string& placement_id);
void MaybeTriggerCreativeAdClickedEvent(
const GURL& url,
base::OnceCallback<void(bool success)> callback);
void MaybeTriggerCreativeAdClickedEvent(const GURL& url,
TriggerAdEventCallback callback);

private:
friend class BraveAdsCreativeSearchResultAdHandlerTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_mojom_web_page_entities_test_util.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_test_constants.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"
#include "brave/components/brave_ads/core/public/ads_callback.h"
#include "brave/components/brave_ads/core/public/ads_feature.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -162,7 +163,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
SimulateMaybeExtractCreativeAdPlacementIdsFromWebPageCallback(
creative_search_result_ad_handler.get(), std::move(mojom_web_page));

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/false));

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -195,7 +196,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
SimulateMaybeExtractCreativeAdPlacementIdsFromWebPageCallback(
creative_search_result_ad_handler.get(), blink::mojom::WebPagePtr());

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -228,7 +229,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
SimulateMaybeExtractCreativeAdPlacementIdsFromWebPageCallback(
creative_search_result_ad_handler.get(), blink::mojom::WebPage::New());

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -263,7 +264,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
test::CreativeSearchResultAdMojomWebPage(
/*excluded_property_names=*/{kCreativeAdRewardsValuePropertyName}));

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -307,7 +308,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
/*excluded_property_names=*/{
kCreativeSetConversionUrlPatternPropertyName}));

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/false));

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -380,15 +381,13 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
SimulateMaybeExtractCreativeAdPlacementIdsFromWebPageCallback(
creative_search_result_ad_handler.get(), mojom_web_page->Clone());

base::MockCallback<base::OnceCallback<void(bool success)>>
fist_click_callback;
base::MockCallback<TriggerAdEventCallback> fist_click_callback;
EXPECT_CALL(fist_click_callback, Run(/*success=*/true));

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
ClickRedirectUrl(), fist_click_callback.Get());

base::MockCallback<base::OnceCallback<void(bool success)>>
second_click_callback;
base::MockCallback<TriggerAdEventCallback> second_click_callback;
EXPECT_CALL(second_click_callback, Run(/*success=*/false));

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -422,15 +421,15 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
test::CreativeSearchResultAdMojomWebPage(/*excluded_property_names=*/{}));

{
base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
InvalidClickRedirectUrlWithNoQueryValue(), callback.Get());
}

{
base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand All @@ -439,7 +438,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
}

{
base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/::testing::_)).Times(0);

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down Expand Up @@ -509,7 +508,7 @@ TEST_F(BraveAdsCreativeSearchResultAdHandlerTest,
SimulateMaybeExtractCreativeAdPlacementIdsFromWebPageCallback(
creative_search_result_ad_handler.get(), mojom_web_page->Clone());

base::MockCallback<base::OnceCallback<void(bool success)>> callback;
base::MockCallback<TriggerAdEventCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/true));

creative_search_result_ad_handler->MaybeTriggerCreativeAdClickedEvent(
Expand Down

0 comments on commit 9c839b3

Please sign in to comment.