Skip to content

Commit

Permalink
Remove NOTIFICATION_TAB_PARENTED/CLOSING from metrics code.
Browse files Browse the repository at this point in the history
BrowserActivityWatcher is now a TabStripModelObserver, and runs the
activity callback whenever a tab action is taken (to include parenting,
i.e. addition or replacement, as well as closing).

Note these notifications are sent by Browser, so they don't exist on
Android. BrowserActivityWatcher and TabStripModelObserver are also
unique to desktop, so this entire CL only affects desktop.

Bug: 268984
Change-Id: I668e22bf637c33266060cd27c1464118a8c6ed1c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1752912
Reviewed-by: Steven Holte <holte@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#687298}
  • Loading branch information
Evan Stade authored and Commit Bot committed Aug 15, 2019
1 parent c2bfb8f commit c8785a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
28 changes: 24 additions & 4 deletions chrome/browser/metrics/browser_activity_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@

#include "chrome/browser/metrics/browser_activity_watcher.h"

#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"

BrowserActivityWatcher::BrowserActivityWatcher(
const base::RepeatingClosure& on_browser_list_change)
: on_browser_list_change_(on_browser_list_change) {
const base::RepeatingClosure& on_browser_activity)
: on_browser_activity_(on_browser_activity) {
BrowserList::AddObserver(this);

for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->tab_strip_model())
browser->tab_strip_model()->AddObserver(this);
}
}

BrowserActivityWatcher::~BrowserActivityWatcher() {
BrowserList::RemoveObserver(this);
}

void BrowserActivityWatcher::OnBrowserAdded(Browser* browser) {
on_browser_list_change_.Run();
if (browser->tab_strip_model())
browser->tab_strip_model()->AddObserver(this);

on_browser_activity_.Run();
}

void BrowserActivityWatcher::OnBrowserRemoved(Browser* browser) {
on_browser_list_change_.Run();
if (browser->tab_strip_model())
browser->tab_strip_model()->RemoveObserver(this);

on_browser_activity_.Run();
}

void BrowserActivityWatcher::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) {
on_browser_activity_.Run();
}
20 changes: 14 additions & 6 deletions chrome/browser/metrics/browser_activity_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"

// Helper that fires a callback every time a Browser object is added or removed
// from the BrowserList. This class primarily exists to encapsulate this
// behavior and reduce the number of platform-specific macros as Android doesn't
// use BrowserList.
class BrowserActivityWatcher : public BrowserListObserver {
// from the BrowserList, or a browser's tab strip model is modified. This class
// primarily exists to encapsulate this behavior and reduce the number of
// platform-specific macros as Android doesn't use BrowserList or TabStripModel.
class BrowserActivityWatcher : public BrowserListObserver,
public TabStripModelObserver {
public:
explicit BrowserActivityWatcher(
const base::RepeatingClosure& on_browser_list_change);
const base::RepeatingClosure& on_browser_activity);
~BrowserActivityWatcher() override;

// BrowserListObserver:
void OnBrowserAdded(Browser* browser) override;
void OnBrowserRemoved(Browser* browser) override;

// TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;

private:
base::RepeatingClosure on_browser_list_change_;
base::RepeatingClosure on_browser_activity_;

DISALLOW_COPY_AND_ASSIGN(BrowserActivityWatcher);
};
Expand Down
6 changes: 0 additions & 6 deletions chrome/browser/metrics/chrome_metrics_service_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,6 @@ void ChromeMetricsServiceClient::RecordCommandLineMetrics() {
}

bool ChromeMetricsServiceClient::RegisterForNotifications() {
registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_TAB_CLOSING,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_LOAD_START,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
Expand Down Expand Up @@ -1009,8 +1005,6 @@ void ChromeMetricsServiceClient::Observe(
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

switch (type) {
case chrome::NOTIFICATION_TAB_PARENTED:
case chrome::NOTIFICATION_TAB_CLOSING:
case content::NOTIFICATION_LOAD_STOP:
case content::NOTIFICATION_LOAD_START:
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED:
Expand Down
4 changes: 0 additions & 4 deletions chrome/browser/metrics/thread_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ ThreadWatcherObserver::ThreadWatcherObserver(
base::Unretained(this)));
#endif

registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_TAB_CLOSING,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_LOAD_START,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
Expand Down

0 comments on commit c8785a4

Please sign in to comment.