Skip to content

Commit

Permalink
[ads] Tab manager code health
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Oct 30, 2024
1 parent ac23694 commit 0ebc1a4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
18 changes: 9 additions & 9 deletions components/brave_ads/core/internal/tabs/tab_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,28 @@ void TabManager::RemoveForId(const int32_t tab_id) {
}
}

void TabManager::NotifyTabDidChangeFocus(const int32_t tab_id) const {
void TabManager::NotifyDidOpenNewTab(const TabInfo& tab) const {
for (TabManagerObserver& observer : observers_) {
observer.OnTabDidChangeFocus(tab_id);
observer.OnDidOpenNewTab(tab);
}
}

void TabManager::NotifyTabDidChange(const TabInfo& tab) const {
void TabManager::NotifyTabDidLoad(const TabInfo& tab,
const int http_status_code) const {
for (TabManagerObserver& observer : observers_) {
observer.OnTabDidChange(tab);
observer.OnTabDidLoad(tab, http_status_code);
}
}

void TabManager::NotifyTabDidLoad(const TabInfo& tab,
const int http_status_code) const {
void TabManager::NotifyTabDidChangeFocus(const int32_t tab_id) const {
for (TabManagerObserver& observer : observers_) {
observer.OnTabDidLoad(tab, http_status_code);
observer.OnTabDidChangeFocus(tab_id);
}
}

void TabManager::NotifyDidOpenNewTab(const TabInfo& tab) const {
void TabManager::NotifyTabDidChange(const TabInfo& tab) const {
for (TabManagerObserver& observer : observers_) {
observer.OnDidOpenNewTab(tab);
observer.OnTabDidChange(tab);
}
}

Expand Down
12 changes: 6 additions & 6 deletions components/brave_ads/core/internal/tabs/tab_manager_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ struct TabInfo;

class TabManagerObserver : public base::CheckedObserver {
public:
// Invoked when a new `tab` is opened.
virtual void OnDidOpenNewTab(const TabInfo& tab) {}

// Invoked when the `tab` has loaded.
virtual void OnTabDidLoad(const TabInfo& tab, const int http_status_code) {}

// Invoked when the tab specfied by `tab_id` changes focus.
virtual void OnTabDidChangeFocus(const int32_t tab_id) {}

// Invoked when the `tab` is updated.
virtual void OnTabDidChange(const TabInfo& tab) {}

// Invoked when the `tab` has loaded.
virtual void OnTabDidLoad(const TabInfo& tab, const int http_status_code) {}

// Invoked when a new `tab` is opened.
virtual void OnDidOpenNewTab(const TabInfo& tab) {}

// Invoked when the text content for the tab specified by `tab_id` did change.
virtual void OnTextContentDidChange(const int32_t tab_id,
const std::vector<GURL>& redirect_chain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ class TabManagerObserverMock : public TabManagerObserver {

~TabManagerObserverMock() override;

MOCK_METHOD(void, OnDidOpenNewTab, (const TabInfo& tab));

MOCK_METHOD(void,
OnTabDidLoad,
(const TabInfo& tab, const int http_status_code));

MOCK_METHOD(void, OnTabDidChangeFocus, (const int32_t tab_id));

MOCK_METHOD(void, OnTabDidChange, (const TabInfo& tab));

MOCK_METHOD(void, OnDidOpenNewTab, (const TabInfo& tab));

MOCK_METHOD(void,
OnTextContentDidChange,
(const int32_t tab_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ PurchaseIntentProcessor::MaybeGetFunnelForUrl(const GURL& url) const {
return iter->second;
}

void PurchaseIntentProcessor::OnTabDidChange(const TabInfo& tab) {
void PurchaseIntentProcessor::OnDidOpenNewTab(const TabInfo& tab) {
CHECK(!tab.redirect_chain.empty());

const GURL& url = tab.redirect_chain.back();
MaybeProcess(tab.id, url);
}

void PurchaseIntentProcessor::OnDidOpenNewTab(const TabInfo& tab) {
void PurchaseIntentProcessor::OnTabDidChange(const TabInfo& tab) {
CHECK(!tab.redirect_chain.empty());

const GURL& url = tab.redirect_chain.back();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class PurchaseIntentProcessor final : public TabManagerObserver {
const GURL& url) const;

// TabManagerObserver:
void OnTabDidChange(const TabInfo& tab) override;
void OnDidOpenNewTab(const TabInfo& tab) override;
void OnTabDidChange(const TabInfo& tab) override;
void OnDidCloseTab(int32_t tab_id) override;

std::map</*tab_id*/ int32_t, GURL> tabs_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ void UserActivityManager::OnBrowserDidEnterBackground() {
RecordEvent(UserActivityEventType::kBrowserDidEnterBackground);
}

void UserActivityManager::OnDidOpenNewTab(const TabInfo& /*tab*/) {
RecordEvent(UserActivityEventType::kOpenedNewTab);
}

void UserActivityManager::OnTabDidChangeFocus(const int32_t /*tab_id*/) {
RecordEvent(UserActivityEventType::kTabChangedFocus);
}
Expand All @@ -163,10 +167,6 @@ void UserActivityManager::OnTabDidChange(const TabInfo& /*tab*/) {
RecordEvent(UserActivityEventType::kTabDidChange);
}

void UserActivityManager::OnDidOpenNewTab(const TabInfo& /*tab*/) {
RecordEvent(UserActivityEventType::kOpenedNewTab);
}

void UserActivityManager::OnDidCloseTab(const int32_t /*tab_id*/) {
RecordEvent(UserActivityEventType::kClosedTab);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class UserActivityManager final : public AdsClientNotifierObserver,
void OnBrowserDidEnterBackground() override;

// TabManagerObserver:
void OnDidOpenNewTab(const TabInfo& tab) override;
void OnTabDidChangeFocus(int32_t tab_id) override;
void OnTabDidChange(const TabInfo& tab) override;
void OnDidOpenNewTab(const TabInfo& tab) override;
void OnDidCloseTab(int32_t tab_id) override;
void OnTabDidStartPlayingMedia(int32_t tab_id) override;
void OnTabDidStopPlayingMedia(int32_t tab_id) override;
Expand Down

0 comments on commit 0ebc1a4

Please sign in to comment.