Skip to content

Commit

Permalink
[ads] Add search result ad clicked InfoBar
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Dec 2, 2024
1 parent 59a62da commit c7ecffe
Show file tree
Hide file tree
Showing 21 changed files with 436 additions and 36 deletions.
6 changes: 6 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,12 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_BRAVE_ADS_NOTIFICATION_CLICKED_SAME_AD_MULTIPLE_TIMES_BODY" desc="The notification body to show when a user clicks an ad multiple times">
You don't need to click to earn, but do click if you're interested!
</message>
<message name="IDS_BRAVE_ADS_SEARCH_RESULT_AD_CLICKED_INFOBAR_MESSAGE" desc="The text label of creative search result ad infobar message">
Thanks for supporting Brave Search by clicking a private ad. Unlike Big Tech, we measure ad performance anonymously to preserve your privacy.
</message>
<message name="IDS_BRAVE_ADS_SEARCH_RESULT_AD_LEARN_MORE_OPT_OUT_CHOICES_LABEL" desc="The text label of creative search result ad learn more / opt out choices link">
Learn more / opt out choices
</message>

<!-- Brave Search -->
<message name="IDS_SETTINGS_WEB_DISCOVERY_LABEL" desc="Description that shows settings for toggle Web Discovery">
Expand Down
12 changes: 12 additions & 0 deletions browser/brave_ads/creatives/search_result_ad/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,37 @@

source_set("search_result_ad") {
sources = [
"creative_search_result_ad_clicked_infobar_delegate.cc",
"creative_search_result_ad_clicked_infobar_delegate.h",
"creative_search_result_ad_tab_helper.cc",
"creative_search_result_ad_tab_helper.h",
]

public_deps = [
"//base",
"//brave/components/brave_ads/core/mojom",
"//components/infobars/core",
"//content/public/browser",
]

deps = [
"//brave/app:brave_generated_resources_grit_grit",
"//brave/browser/brave_ads",
"//brave/components/brave_ads/browser",
"//brave/components/brave_ads/content/browser",
"//brave/components/brave_ads/core",
"//brave/components/brave_rewards/common",
"//brave/components/brave_search/common",
"//brave/components/vector_icons",
"//chrome/browser:browser_public_dependencies",
"//chrome/browser/profiles:profile",
"//chrome/common",
"//components/infobars/content",
"//components/infobars/core",
"//components/prefs",
"//components/strings:components_strings_grit",
"//components/vector_icons",
"//ui/base",
"//url",
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_clicked_infobar_delegate.h"

#include <memory>

#include "brave/components/brave_ads/core/public/prefs/pref_names.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/infobars/confirm_infobar_creator.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "url/gurl.h"

namespace brave_ads {

namespace {

constexpr int kIconSize = 20;

constexpr char kLearnMoreUrl[] =
"https://search.brave.com/help/conversion-reporting";

} // namespace

// static
void CreativeSearchResultAdClickedInfoBarDelegate::Create(
content::WebContents* web_contents,
PrefService* prefs) {
CHECK(web_contents);
CHECK(prefs);

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())));
}

CreativeSearchResultAdClickedInfoBarDelegate::
CreativeSearchResultAdClickedInfoBarDelegate() = default;

CreativeSearchResultAdClickedInfoBarDelegate::
~CreativeSearchResultAdClickedInfoBarDelegate() = default;

infobars::InfoBarDelegate::InfoBarIdentifier
CreativeSearchResultAdClickedInfoBarDelegate::GetIdentifier() const {
return SEARCH_RESULT_AD_CLICKED_INFOBAR_DELEGATE;
}

ui::ImageModel CreativeSearchResultAdClickedInfoBarDelegate::GetIcon() const {
return ui::ImageModel::FromVectorIcon(vector_icons::kProductIcon,
ui::kColorIcon, kIconSize);
}

std::u16string CreativeSearchResultAdClickedInfoBarDelegate::GetMessageText()
const {
return l10n_util::GetStringUTF16(
IDS_BRAVE_ADS_SEARCH_RESULT_AD_CLICKED_INFOBAR_MESSAGE);
}

int CreativeSearchResultAdClickedInfoBarDelegate::GetButtons() const {
return BUTTON_NONE;
}

std::u16string CreativeSearchResultAdClickedInfoBarDelegate::GetLinkText()
const {
return l10n_util::GetStringUTF16(
IDS_BRAVE_ADS_SEARCH_RESULT_AD_LEARN_MORE_OPT_OUT_CHOICES_LABEL);
}

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
@@ -0,0 +1,45 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_ADS_CREATIVES_SEARCH_RESULT_AD_CREATIVE_SEARCH_RESULT_AD_CLICKED_INFOBAR_DELEGATE_H_
#define BRAVE_BROWSER_BRAVE_ADS_CREATIVES_SEARCH_RESULT_AD_CREATIVE_SEARCH_RESULT_AD_CLICKED_INFOBAR_DELEGATE_H_

#include "components/infobars/core/confirm_infobar_delegate.h"

class PrefService;

namespace content {
class WebContents;
} // namespace content

namespace brave_ads {

class CreativeSearchResultAdClickedInfoBarDelegate
: public ConfirmInfoBarDelegate {
public:
CreativeSearchResultAdClickedInfoBarDelegate();
~CreativeSearchResultAdClickedInfoBarDelegate() override;

CreativeSearchResultAdClickedInfoBarDelegate(
const CreativeSearchResultAdClickedInfoBarDelegate&) = delete;
CreativeSearchResultAdClickedInfoBarDelegate& operator=(
const CreativeSearchResultAdClickedInfoBarDelegate&) = delete;

static void Create(content::WebContents* web_contents, PrefService* prefs);

private:
// ConfirmInfoBarDelegate:
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
ui::ImageModel GetIcon() const override;
std::u16string GetMessageText() const override;
int GetButtons() const override;
std::u16string GetLinkText() const override;
GURL GetLinkURL() const override;
bool LinkClicked(WindowOpenDisposition disposition) override;
};

} // namespace brave_ads

#endif // BRAVE_BROWSER_BRAVE_ADS_CREATIVES_SEARCH_RESULT_AD_CREATIVE_SEARCH_RESULT_AD_CLICKED_INFOBAR_DELEGATE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_clicked_infobar_delegate.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_url_placement_id_extractor.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
Expand Down Expand Up @@ -98,9 +99,7 @@ bool CreativeSearchResultAdTabHelper::ShouldHandleCreativeAdEvents() const {

// If the feature is enabled, we should only trigger creative ad events when
// the user has joined Brave Rewards.
const Profile* const profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return profile->GetPrefs()->GetBoolean(brave_rewards::prefs::kEnabled);
return GetPrefs()->GetBoolean(brave_rewards::prefs::kEnabled);
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -117,6 +116,22 @@ AdsService* CreativeSearchResultAdTabHelper::GetAdsService() const {
return AdsServiceFactory::GetForProfile(profile);
}

PrefService* CreativeSearchResultAdTabHelper::GetPrefs() const {
Profile* const profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return profile->GetPrefs();
}

void CreativeSearchResultAdTabHelper::
MaybeTriggerCreativeAdClickedEventCallback(const bool success) {
if (!success) {
return;
}

CreativeSearchResultAdClickedInfoBarDelegate::Create(web_contents(),
GetPrefs());
}

void CreativeSearchResultAdTabHelper::MaybeCreateCreativeSearchResultAdHandler(
content::NavigationHandle* const navigation_handle) {
CHECK(navigation_handle);
Expand Down Expand Up @@ -234,7 +249,10 @@ void CreativeSearchResultAdTabHelper::MaybeHandleCreativeAdClickedEventCallback(

ads_service->TriggerSearchResultAdEvent(
std::move(creative_search_result_ad),
mojom::SearchResultAdEventType::kClicked, base::DoNothing());
mojom::SearchResultAdEventType::kClicked,
base::BindOnce(&CreativeSearchResultAdTabHelper::
MaybeTriggerCreativeAdClickedEventCallback,
weak_factory_.GetWeakPtr()));
}

void CreativeSearchResultAdTabHelper::DidStartNavigation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#include "base/memory/weak_ptr.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.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"

class GURL;
class PrefService;

namespace brave_ads {

Expand All @@ -39,11 +41,18 @@ class CreativeSearchResultAdTabHelper

bool ShouldHandleCreativeAdEvents() const;

void MaybeTriggerCreativeAdClickedEvent(const GURL& url,
TriggerAdEventCallback callback);

private:
friend class content::WebContentsUserData<CreativeSearchResultAdTabHelper>;

AdsService* GetAdsService() const;

PrefService* GetPrefs() const;

void MaybeTriggerCreativeAdClickedEventCallback(bool success);

void MaybeCreateCreativeSearchResultAdHandler(
content::NavigationHandle* navigation_handle);

Expand Down
1 change: 1 addition & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ brave_chrome_browser_ui_allow_circular_includes_from +=
brave_chrome_browser_allow_circular_includes_from = [
"//brave/browser/ui",
"//brave/browser/brave_ads:impl",
"//brave/browser/brave_ads/creatives/search_result_ad",
]

# https://github.com/brave/brave-browser/issues/41418
Expand Down
3 changes: 2 additions & 1 deletion chromium_src/components/infobars/core/infobar_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
WEB_DISCOVERY_INFOBAR_DELEGATE = 506, \
BRAVE_SYNC_ACCOUNT_DELETED_INFOBAR = 507, \
BRAVE_REQUEST_OTR_INFOBAR_DELEGATE = 508, \
DEV_CHANNEL_DEPRECATION_INFOBAR_DELEGATE = 509,
DEV_CHANNEL_DEPRECATION_INFOBAR_DELEGATE = 509, \
SEARCH_RESULT_AD_CLICKED_INFOBAR_DELEGATE = 510,

// Deprecated:
// WAYBACK_MACHINE_INFOBAR_DELEGATE = 502
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
Expand Down
Loading

0 comments on commit c7ecffe

Please sign in to comment.