Skip to content

Commit

Permalink
Merge pull request #24568 from brave/issues/39506
Browse files Browse the repository at this point in the history
[ads] Do not classify page content for same document navigations
  • Loading branch information
tmancey committed Jul 11, 2024
2 parents 4516df9 + 7329a1a commit 5f64c50
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
51 changes: 30 additions & 21 deletions browser/brave_ads/tabs/ads_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ AdsTabHelper::AdsTabHelper(content::WebContents* web_contents)
return;
}

Profile* profile =
Profile* const profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
ads_service_ = AdsServiceFactory::GetForProfile(profile);
if (!ads_service_) {
Expand Down Expand Up @@ -163,7 +163,16 @@ bool AdsTabHelper::IsErrorPage(content::NavigationHandle* navigation_handle) {
}

void AdsTabHelper::ProcessNavigation() {
MaybeNotifyTabContentDidChange();
MaybeNotifyTabHtmlContentDidChange();
MaybeNotifyTabTextContentDidChange();

// Set `was_restored_` to `false` so that listeners are notified of tab
// changes after the tab is restored.
was_restored_ = false;
}

void AdsTabHelper::ProcessSameDocumentNavigation() {
MaybeNotifyTabHtmlContentDidChange();

// Set `was_restored_` to `false` so that listeners are notified of tab
// changes after the tab is restored.
Expand Down Expand Up @@ -232,22 +241,18 @@ void AdsTabHelper::MaybeNotifyTabDidChange() {
is_error_page_, IsVisible());
}

void AdsTabHelper::MaybeNotifyTabContentDidChange() {
if (was_restored_ || !is_new_navigation_ || redirect_chain_.empty() ||
is_error_page_) {
// Don't notify content changes if the tab was restored, was a previously
// committed navigation, the web contents are still loading, or an error
// page was displayed.
return;
}

MaybeNotifyTabHtmlContentDidChange();
MaybeNotifyTabTextContentDidChange();
bool AdsTabHelper::ShouldNotifyTabContentDidChange() const {
// Don't notify about content changes if the ads service is not available, the
// tab was restored, was a previously committed navigation, the web contents
// are still loading, or an error page was displayed.
return ads_service_ && !was_restored_ && is_new_navigation_ &&
!redirect_chain_.empty() && !is_error_page_;
}

void AdsTabHelper::MaybeNotifyTabHtmlContentDidChange() {
CHECK(ads_service_);
CHECK(!redirect_chain_.empty());
if (!ShouldNotifyTabContentDidChange()) {
return;
}

if (!UserHasJoinedBraveRewards()) {
// HTML is not required because verifiable conversions are only supported
Expand All @@ -258,8 +263,8 @@ void AdsTabHelper::MaybeNotifyTabHtmlContentDidChange() {
/*html=*/"");
}

// Only utilized for verifiable conversions, which requires the user to
// have joined Brave Rewards.
// Only utilized for verifiable conversions, which requires the user to have
// joined Brave Rewards.
web_contents()->GetPrimaryMainFrame()->ExecuteJavaScriptInIsolatedWorld(
kSerializeDocumentToStringJavaScript,
base::BindOnce(&AdsTabHelper::OnMaybeNotifyTabHtmlContentDidChange,
Expand All @@ -277,7 +282,9 @@ void AdsTabHelper::OnMaybeNotifyTabHtmlContentDidChange(
}

void AdsTabHelper::MaybeNotifyTabTextContentDidChange() {
CHECK(!redirect_chain_.empty());
if (!ShouldNotifyTabContentDidChange()) {
return;
}

if (UserHasOptedInToNotificationAds()) {
// Only utilized for text classification, which requires the user to have
Expand Down Expand Up @@ -351,13 +358,15 @@ void AdsTabHelper::DidFinishNavigation(
// called from `DocumentOnLoadCompletedInPrimaryMainFrame`.
if (navigation_handle->IsSameDocument() &&
web_contents()->IsDocumentOnLoadCompletedInPrimaryMainFrame()) {
// Single-page application.
ProcessNavigation();
ProcessSameDocumentNavigation();
}
}

void AdsTabHelper::DocumentOnLoadCompletedInPrimaryMainFrame() {
// Multi-page application.
if (!ads_service_) {
return;
}

ProcessNavigation();
}

Expand Down
3 changes: 2 additions & 1 deletion browser/brave_ads/tabs/ads_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class AdsTabHelper : public content::WebContentsObserver,
bool IsErrorPage(content::NavigationHandle* navigation_handle);

void ProcessNavigation();
void ProcessSameDocumentNavigation();
void ResetNavigationState();

void MaybeNotifyBrowserDidBecomeActive();
Expand All @@ -73,7 +74,7 @@ class AdsTabHelper : public content::WebContentsObserver,

void MaybeNotifyTabDidChange();

void MaybeNotifyTabContentDidChange();
bool ShouldNotifyTabContentDidChange() const;
void MaybeNotifyTabHtmlContentDidChange();
void OnMaybeNotifyTabHtmlContentDidChange(
const std::vector<GURL>& redirect_chain,
Expand Down

0 comments on commit 5f64c50

Please sign in to comment.