Skip to content

Commit

Permalink
[ads] Fixes follow up to #38191: Restoring window does not resume lan…
Browse files Browse the repository at this point in the history
…ded counter
  • Loading branch information
tmancey committed May 10, 2024
1 parent f69c439 commit 6732442
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
14 changes: 10 additions & 4 deletions browser/brave_ads/tabs/ads_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,24 @@ void AdsTabHelper::OnBrowserSetLastActive(Browser* browser) {
const bool last_is_browser_active = is_browser_active_;
is_browser_active_ = browser->tab_strip_model()->GetIndexOfWebContents(
web_contents()) != TabStripModel::kNoTab;
if (last_is_browser_active != is_browser_active_) {
MaybeNotifyBrowserDidBecomeActive();
if (last_is_browser_active == is_browser_active_) {
return;
}

MaybeNotifyBrowserDidBecomeActive();
MaybeNotifyTabDidChange();
}

void AdsTabHelper::OnBrowserNoLongerActive(Browser* browser) {
const bool last_is_browser_active = is_browser_active_;
is_browser_active_ = browser->tab_strip_model()->GetIndexOfWebContents(
web_contents()) == TabStripModel::kNoTab;
if (last_is_browser_active != is_browser_active_) {
MaybeNotifyBrowserDidResignActive();
if (last_is_browser_active == is_browser_active_) {
return;
}

MaybeNotifyBrowserDidResignActive();
MaybeNotifyTabDidChange();
}
#endif

Expand Down
3 changes: 0 additions & 3 deletions components/brave_ads/core/internal/tabs/tab_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class TabManager final : public AdsClientNotifierObserver {
void AddObserver(TabManagerObserver* observer);
void RemoveObserver(TabManagerObserver* observer);

std::optional<int32_t> MaybeGetVisibleTabId() const {
return visible_tab_id_;
}
bool IsVisible(int32_t tab_id) const;
std::optional<TabInfo> GetVisible() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ void SiteVisit::StopPageLand(const int32_t tab_id) {
page_lands_.erase(tab_id);
}

void SiteVisit::MaybeSuspendOrResumePageLandForVisibleTabId() {
if (const std::optional<int32_t> tab_id =
TabManager::GetInstance().MaybeGetVisibleTabId()) {
MaybeSuspendOrResumePageLand(*tab_id);
void SiteVisit::MaybeSuspendOrResumePageLandForVisibleTab() {
if (const std::optional<TabInfo> tab =
TabManager::GetInstance().GetVisible()) {
MaybeSuspendOrResumePageLand(tab->id);
}
}

Expand Down Expand Up @@ -275,19 +275,27 @@ void SiteVisit::NotifyCanceledPageLand(const int32_t tab_id,
}

void SiteVisit::OnBrowserDidBecomeActive() {
MaybeSuspendOrResumePageLandForVisibleTabId();
// Required to suspend or resume the page land because `OnTabDidChangeFocus`
// is not called when the browser becomes active on mobile.
MaybeSuspendOrResumePageLandForVisibleTab();
}

void SiteVisit::OnBrowserDidResignActive() {
MaybeSuspendOrResumePageLandForVisibleTabId();
// Required to suspend or resume the page land because `OnTabDidChangeFocus`
// is not called when the browser resigns active on mobile.
MaybeSuspendOrResumePageLandForVisibleTab();
}

void SiteVisit::OnBrowserDidEnterForeground() {
MaybeSuspendOrResumePageLandForVisibleTabId();
// Required to suspend or resume the page land because `OnTabDidChangeFocus`
// is not called when the browser enters the foreground on mobile.
MaybeSuspendOrResumePageLandForVisibleTab();
}

void SiteVisit::OnBrowserDidEnterBackground() {
MaybeSuspendOrResumePageLandForVisibleTabId();
// Required to suspend or resume the page land because `OnTabDidChangeFocus`
// is not called when the browser enters the background on mobile.
MaybeSuspendOrResumePageLandForVisibleTab();
}

void SiteVisit::OnTabDidChangeFocus(const int32_t tab_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class SiteVisit final : public BrowserManagerObserver,
void CancelPageLand(int32_t tab_id);
void StopPageLand(int32_t tab_id);

void MaybeSuspendOrResumePageLandForVisibleTabId();
void MaybeSuspendOrResumePageLandForVisibleTab();
void MaybeSuspendOrResumePageLand(int32_t tab_id);
base::TimeDelta CalculateRemainingTimeToLandOnPage(int32_t tab_id);
void SuspendPageLand(const TabInfo& tab);
Expand Down

0 comments on commit 6732442

Please sign in to comment.