Commit 7bf6d365 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Remove use of NotificationService from IncognitoObserver

This CL also splits the IncognitoObserver class into separate
implementations for Android and desktop.

Bug: 268984
Change-Id: Ic59988fe8d08a5217400dffd78eff3d7838f7630
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1715533Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680503}
parent 1cbc6ff4
...@@ -2742,6 +2742,7 @@ jumbo_split_static_library("browser") { ...@@ -2742,6 +2742,7 @@ jumbo_split_static_library("browser") {
"media/webrtc/screen_capture_infobar_delegate_android.h", "media/webrtc/screen_capture_infobar_delegate_android.h",
"metrics/android_metrics_provider.cc", "metrics/android_metrics_provider.cc",
"metrics/android_metrics_provider.h", "metrics/android_metrics_provider.h",
"metrics/incognito_observer_android.cc",
"metrics/page_load_metrics_provider.cc", "metrics/page_load_metrics_provider.cc",
"metrics/page_load_metrics_provider.h", "metrics/page_load_metrics_provider.h",
"notifications/notification_platform_bridge_android.cc", "notifications/notification_platform_bridge_android.cc",
...@@ -3145,6 +3146,7 @@ jumbo_split_static_library("browser") { ...@@ -3145,6 +3146,7 @@ jumbo_split_static_library("browser") {
"metrics/desktop_session_duration/desktop_session_duration_tracker.h", "metrics/desktop_session_duration/desktop_session_duration_tracker.h",
"metrics/first_web_contents_profiler.cc", "metrics/first_web_contents_profiler.cc",
"metrics/first_web_contents_profiler.h", "metrics/first_web_contents_profiler.h",
"metrics/incognito_observer_desktop.cc",
"metrics/tab_reactivation_tracker.cc", "metrics/tab_reactivation_tracker.cc",
"metrics/tab_reactivation_tracker.h", "metrics/tab_reactivation_tracker.h",
"metrics/tab_stats_data_store.cc", "metrics/tab_stats_data_store.cc",
......
...@@ -400,7 +400,7 @@ ChromeMetricsServiceClient::ChromeMetricsServiceClient( ...@@ -400,7 +400,7 @@ ChromeMetricsServiceClient::ChromeMetricsServiceClient(
: metrics_state_manager_(state_manager) { : metrics_state_manager_(state_manager) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RecordCommandLineMetrics(); RecordCommandLineMetrics();
incognito_observer_ = std::make_unique<IncognitoObserver>( incognito_observer_ = IncognitoObserver::Create(
base::BindRepeating(&ChromeMetricsServiceClient::UpdateRunningServices, base::BindRepeating(&ChromeMetricsServiceClient::UpdateRunningServices,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
......
...@@ -4,48 +4,5 @@ ...@@ -4,48 +4,5 @@
#include "chrome/browser/metrics/incognito_observer.h" #include "chrome/browser/metrics/incognito_observer.h"
#include "chrome/browser/chrome_notification_types.h" IncognitoObserver::~IncognitoObserver() = default;
#include "content/public/browser/notification_service.h" IncognitoObserver::IncognitoObserver() = default;
IncognitoObserver::IncognitoObserver(
const base::RepeatingClosure& update_closure)
: update_closure_(update_closure) {
#if defined(OS_ANDROID)
TabModelList::AddObserver(this);
#endif
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllBrowserContextsAndSources());
}
IncognitoObserver::~IncognitoObserver() {
#if defined(OS_ANDROID)
TabModelList::RemoveObserver(this);
#endif
}
#if defined(OS_ANDROID)
void IncognitoObserver::OnTabModelAdded() {
update_closure_.Run();
}
void IncognitoObserver::OnTabModelRemoved() {
update_closure_.Run();
}
#endif
void IncognitoObserver::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_BROWSER_OPENED:
case chrome::NOTIFICATION_PROFILE_DESTROYED:
update_closure_.Run();
break;
default:
NOTREACHED();
}
}
...@@ -6,14 +6,6 @@ ...@@ -6,14 +6,6 @@
#define CHROME_BROWSER_METRICS_INCOGNITO_OBSERVER_H_ #define CHROME_BROWSER_METRICS_INCOGNITO_OBSERVER_H_
#include "base/callback.h" #include "base/callback.h"
#include "build/build_config.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#if defined(OS_ANDROID)
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list_observer.h"
#endif
// Encapsulates platform-specific functionality for observing events that may // Encapsulates platform-specific functionality for observing events that may
// cause "is incognito active?" state to change. The class takes a closure that // cause "is incognito active?" state to change. The class takes a closure that
...@@ -21,29 +13,15 @@ ...@@ -21,29 +13,15 @@
// The incognito state should then be checked by the callback. // The incognito state should then be checked by the callback.
// TODO(asvitkine): Considering moving the check for incognito to this class // TODO(asvitkine): Considering moving the check for incognito to this class
// too; see ChromeMetricsServicesManagerClient::IsIncognitoSessionActive(). // too; see ChromeMetricsServicesManagerClient::IsIncognitoSessionActive().
class IncognitoObserver : class IncognitoObserver {
#if defined(OS_ANDROID)
public TabModelListObserver,
#endif
content::NotificationObserver {
public: public:
explicit IncognitoObserver(const base::RepeatingClosure& update_closure); static std::unique_ptr<IncognitoObserver> Create(
~IncognitoObserver() override; const base::RepeatingClosure& update_closure);
private:
#if defined(OS_ANDROID)
// TabModelListObserver:
void OnTabModelAdded() override;
void OnTabModelRemoved() override;
#endif
// content::NotificationObserver: virtual ~IncognitoObserver();
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
const base::RepeatingClosure update_closure_; protected:
content::NotificationRegistrar registrar_; IncognitoObserver();
DISALLOW_COPY_AND_ASSIGN(IncognitoObserver); DISALLOW_COPY_AND_ASSIGN(IncognitoObserver);
}; };
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/metrics/incognito_observer.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list_observer.h"
namespace {
class IncognitoObserverAndroid : public IncognitoObserver,
public TabModelListObserver {
public:
explicit IncognitoObserverAndroid(
const base::RepeatingClosure& update_closure)
: update_closure_(update_closure) {
TabModelList::AddObserver(this);
}
~IncognitoObserverAndroid() override { TabModelList::RemoveObserver(this); }
// TabModelListObserver:
void OnTabModelAdded() override { update_closure_.Run(); }
void OnTabModelRemoved() override { update_closure_.Run(); }
private:
const base::RepeatingClosure update_closure_;
DISALLOW_COPY_AND_ASSIGN(IncognitoObserverAndroid);
};
} // namespace
// static
std::unique_ptr<IncognitoObserver> IncognitoObserver::Create(
const base::RepeatingClosure& update_closure) {
return std::make_unique<IncognitoObserverAndroid>(update_closure);
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/metrics/incognito_observer.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
namespace {
class IncognitoObserverDesktop : public IncognitoObserver,
public BrowserListObserver {
public:
explicit IncognitoObserverDesktop(
const base::RepeatingClosure& update_closure)
: update_closure_(update_closure) {
BrowserList::AddObserver(this);
}
~IncognitoObserverDesktop() override { BrowserList::RemoveObserver(this); }
private:
// BrowserListObserver:
void OnBrowserAdded(Browser* browser) override { update_closure_.Run(); }
void OnBrowserRemoved(Browser* browser) override { update_closure_.Run(); }
const base::RepeatingClosure update_closure_;
DISALLOW_COPY_AND_ASSIGN(IncognitoObserverDesktop);
};
} // namespace
// static
std::unique_ptr<IncognitoObserver> IncognitoObserver::Create(
const base::RepeatingClosure& update_closure) {
return std::make_unique<IncognitoObserverDesktop>(update_closure);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment