Commit 0cfa87d4 authored by Travis Skare's avatar Travis Skare Committed by Commit Bot

Disable UKM when we couldn't register observers

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I4bdb81dd016abb71951a08c86f965a4128be0eed
Reviewed-on: https://chromium-review.googlesource.com/887300Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarSteven Holte <holte@chromium.org>
Commit-Queue: Steven Holte <holte@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550424}
parent 8d537d0f
...@@ -418,9 +418,10 @@ ChromeMetricsServiceClient::ChromeMetricsServiceClient( ...@@ -418,9 +418,10 @@ ChromeMetricsServiceClient::ChromeMetricsServiceClient(
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RecordCommandLineMetrics(); RecordCommandLineMetrics();
RegisterForNotifications(); notification_listeners_active_ = RegisterForNotifications();
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
incognito_observer_ = std::make_unique<AndroidIncognitoObserver>(this); incognito_observer_ = std::make_unique<AndroidIncognitoObserver>(this);
notification_listeners_active_ &= (incognito_observer_ != nullptr);
#endif #endif
} }
...@@ -850,7 +851,7 @@ void ChromeMetricsServiceClient::RecordCommandLineMetrics() { ...@@ -850,7 +851,7 @@ void ChromeMetricsServiceClient::RecordCommandLineMetrics() {
switch_count - common_commands); switch_count - common_commands);
} }
void ChromeMetricsServiceClient::RegisterForNotifications() { bool ChromeMetricsServiceClient::RegisterForNotifications() {
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
content::NotificationService::AllBrowserContextsAndSources()); content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
...@@ -878,13 +879,18 @@ void ChromeMetricsServiceClient::RegisterForNotifications() { ...@@ -878,13 +879,18 @@ void ChromeMetricsServiceClient::RegisterForNotifications() {
content::NotificationService::AllBrowserContextsAndSources()); content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllBrowserContextsAndSources()); content::NotificationService::AllBrowserContextsAndSources());
bool all_profiles_succeeded = true;
for (Profile* profile : for (Profile* profile :
g_browser_process->profile_manager()->GetLoadedProfiles()) { g_browser_process->profile_manager()->GetLoadedProfiles()) {
RegisterForProfileEvents(profile); if (!RegisterForProfileEvents(profile)) {
all_profiles_succeeded = false;
}
} }
return all_profiles_succeeded;
} }
void ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) { bool ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) {
// Register chrome://ukm handler // Register chrome://ukm handler
content::URLDataSource::Add( content::URLDataSource::Add(
profile, new ukm::debug::DebugPage(base::Bind( profile, new ukm::debug::DebugPage(base::Bind(
...@@ -894,7 +900,8 @@ void ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) { ...@@ -894,7 +900,8 @@ void ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) {
// deletion. // deletion.
if (chromeos::ProfileHelper::IsSigninProfile(profile) || if (chromeos::ProfileHelper::IsSigninProfile(profile) ||
chromeos::ProfileHelper::IsLockScreenAppProfile(profile)) { chromeos::ProfileHelper::IsLockScreenAppProfile(profile)) {
return; // No listeners, but still a success case.
return true;
} }
#endif #endif
#if defined(OS_WIN) || defined(OS_MACOSX) || \ #if defined(OS_WIN) || defined(OS_MACOSX) || \
...@@ -908,11 +915,19 @@ void ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) { ...@@ -908,11 +915,19 @@ void ChromeMetricsServiceClient::RegisterForProfileEvents(Profile* profile) {
history::HistoryService* history_service = history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(profile, HistoryServiceFactory::GetForProfile(profile,
ServiceAccessType::IMPLICIT_ACCESS); ServiceAccessType::IMPLICIT_ACCESS);
if (!history_service) {
return false;
}
ObserveServiceForDeletions(history_service); ObserveServiceForDeletions(history_service);
browser_sync::ProfileSyncService* sync = browser_sync::ProfileSyncService* sync =
ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
if (sync) if (!sync) {
ObserveServiceForSyncDisables(static_cast<syncer::SyncService*>(sync)); return false;
}
ObserveServiceForSyncDisables(static_cast<syncer::SyncService*>(sync));
return true;
} }
void ChromeMetricsServiceClient::Observe( void ChromeMetricsServiceClient::Observe(
...@@ -937,9 +952,15 @@ void ChromeMetricsServiceClient::Observe( ...@@ -937,9 +952,15 @@ void ChromeMetricsServiceClient::Observe(
metrics_service_->OnApplicationNotIdle(); metrics_service_->OnApplicationNotIdle();
break; break;
case chrome::NOTIFICATION_PROFILE_ADDED: case chrome::NOTIFICATION_PROFILE_ADDED: {
RegisterForProfileEvents(content::Source<Profile>(source).ptr()); bool success =
RegisterForProfileEvents(content::Source<Profile>(source).ptr());
if (!success && notification_listeners_active_) {
notification_listeners_active_ = false;
UpdateRunningServices();
}
break; break;
}
case chrome::NOTIFICATION_PROFILE_DESTROYED: case chrome::NOTIFICATION_PROFILE_DESTROYED:
// May have closed last incognito window. // May have closed last incognito window.
UpdateRunningServices(); UpdateRunningServices();
...@@ -1035,3 +1056,17 @@ bool ChromeMetricsServiceClient::IsHistorySyncEnabledOnAllProfiles() { ...@@ -1035,3 +1056,17 @@ bool ChromeMetricsServiceClient::IsHistorySyncEnabledOnAllProfiles() {
bool ChromeMetricsServiceClient::IsExtensionSyncEnabledOnAllProfiles() { bool ChromeMetricsServiceClient::IsExtensionSyncEnabledOnAllProfiles() {
return SyncDisableObserver::IsExtensionSyncEnabledOnAllProfiles(); return SyncDisableObserver::IsExtensionSyncEnabledOnAllProfiles();
} }
bool g_notification_listeners_failed = false;
void ChromeMetricsServiceClient::SetNotificationListenerSetupFailedForTesting(
bool simulate_failure) {
g_notification_listeners_failed = simulate_failure;
}
bool ChromeMetricsServiceClient::
AreNotificationListenersEnabledOnAllProfiles() {
// For testing
if (g_notification_listeners_failed)
return false;
return notification_listeners_active_;
}
...@@ -89,6 +89,9 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient, ...@@ -89,6 +89,9 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
bool IsUMACellularUploadLogicEnabled() override; bool IsUMACellularUploadLogicEnabled() override;
bool IsHistorySyncEnabledOnAllProfiles() override; bool IsHistorySyncEnabledOnAllProfiles() override;
bool IsExtensionSyncEnabledOnAllProfiles() override; bool IsExtensionSyncEnabledOnAllProfiles() override;
bool AreNotificationListenersEnabledOnAllProfiles() override;
static void SetNotificationListenerSetupFailedForTesting(
bool simulate_failure);
// ukm::HistoryDeleteObserver: // ukm::HistoryDeleteObserver:
void OnHistoryDeleted() override; void OnHistoryDeleted() override;
...@@ -143,10 +146,12 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient, ...@@ -143,10 +146,12 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
// user is performing work. This is useful to allow some features to sleep, // user is performing work. This is useful to allow some features to sleep,
// until the machine becomes active, such as precluding UMA uploads unless // until the machine becomes active, such as precluding UMA uploads unless
// there was recent activity. // there was recent activity.
void RegisterForNotifications(); // Returns true if registration was successful for all profiles.
bool RegisterForNotifications();
// Call to listen for events on the selected profile's services. // Call to listen for events on the selected profile's services.
void RegisterForProfileEvents(Profile* profile); // Returns true if we registered for all notifications we wanted successfully.
bool RegisterForProfileEvents(Profile* profile);
// content::NotificationObserver: // content::NotificationObserver:
void Observe(int type, void Observe(int type,
...@@ -185,6 +190,9 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient, ...@@ -185,6 +190,9 @@ class ChromeMetricsServiceClient : public metrics::MetricsServiceClient,
std::unique_ptr<TabModelListObserver> incognito_observer_; std::unique_ptr<TabModelListObserver> incognito_observer_;
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
// Whether we registered all notification listeners successfully.
bool notification_listeners_active_;
// A queue of tasks for initial metrics gathering. These may be asynchronous // A queue of tasks for initial metrics gathering. These may be asynchronous
// or synchronous. // or synchronous.
base::circular_deque<base::Closure> initialize_task_queue_; base::circular_deque<base::Closure> initialize_task_queue_;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h" #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_service_client.h"
#include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h"
#include "chrome/browser/metrics/testing/metrics_reporting_pref_helper.h" #include "chrome/browser/metrics/testing/metrics_reporting_pref_helper.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
...@@ -715,6 +716,23 @@ IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MultiSyncSignoutCheck) { ...@@ -715,6 +716,23 @@ IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MultiSyncSignoutCheck) {
CloseBrowserSynchronously(browser1); CloseBrowserSynchronously(browser1);
} }
// Make sure that if history/sync services weren't available when we tried to
// attach listeners, UKM is not enabled.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, ServiceListenerInitFailedCheck) {
MetricsConsentOverride metrics_consent(true);
ChromeMetricsServiceClient::SetNotificationListenerSetupFailedForTesting(
true);
Profile* profile = ProfileManager::GetActiveUserProfile();
std::unique_ptr<ProfileSyncServiceHarness> harness =
EnableSyncForProfile(profile);
Browser* sync_browser = CreateBrowser(profile);
EXPECT_FALSE(ukm_enabled());
harness->service()->RequestStop(browser_sync::ProfileSyncService::CLEAR_DATA);
CloseBrowserSynchronously(sync_browser);
}
// Make sure that UKM is not affected by MetricsReporting Feature (sampling). // Make sure that UKM is not affected by MetricsReporting Feature (sampling).
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MetricsReportingCheck) { IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MetricsReportingCheck) {
// Need to set the Metrics Default to OPT_OUT to trigger MetricsReporting. // Need to set the Metrics Default to OPT_OUT to trigger MetricsReporting.
......
...@@ -44,6 +44,10 @@ bool MetricsServiceClient::IsExtensionSyncEnabledOnAllProfiles() { ...@@ -44,6 +44,10 @@ bool MetricsServiceClient::IsExtensionSyncEnabledOnAllProfiles() {
return false; return false;
} }
bool MetricsServiceClient::AreNotificationListenersEnabledOnAllProfiles() {
return false;
}
void MetricsServiceClient::SetUpdateRunningServicesCallback( void MetricsServiceClient::SetUpdateRunningServicesCallback(
const base::Closure& callback) { const base::Closure& callback) {
update_running_services_ = callback; update_running_services_ = callback;
......
...@@ -119,12 +119,15 @@ class MetricsServiceClient { ...@@ -119,12 +119,15 @@ class MetricsServiceClient {
// Returns whether cellular logic is enabled for metrics reporting. // Returns whether cellular logic is enabled for metrics reporting.
virtual bool IsUMACellularUploadLogicEnabled(); virtual bool IsUMACellularUploadLogicEnabled();
// Returns if history sync is enabled on all active profiles. // Returns whether history sync is enabled on all active profiles.
virtual bool IsHistorySyncEnabledOnAllProfiles(); virtual bool IsHistorySyncEnabledOnAllProfiles();
// Returns if extensions sync is enabled on all active profiles. // Returns if extensions sync is enabled on all active profiles.
virtual bool IsExtensionSyncEnabledOnAllProfiles(); virtual bool IsExtensionSyncEnabledOnAllProfiles();
// Returns whether UKM notification listeners were attached to all profiles.
virtual bool AreNotificationListenersEnabledOnAllProfiles();
// Sets the callback to run MetricsServiceManager::UpdateRunningServices. // Sets the callback to run MetricsServiceManager::UpdateRunningServices.
void SetUpdateRunningServicesCallback(const base::Closure& callback); void SetUpdateRunningServicesCallback(const base::Closure& callback);
......
...@@ -138,12 +138,16 @@ void MetricsServicesManager::UpdateUkmService() { ...@@ -138,12 +138,16 @@ void MetricsServicesManager::UpdateUkmService() {
ukm::UkmService* ukm = GetUkmService(); ukm::UkmService* ukm = GetUkmService();
if (!ukm) if (!ukm)
return; return;
bool listeners_active =
GetMetricsServiceClient()->AreNotificationListenersEnabledOnAllProfiles();
bool sync_enabled = bool sync_enabled =
client_->IsMetricsReportingForceEnabled() || client_->IsMetricsReportingForceEnabled() ||
metrics_service_client_->IsHistorySyncEnabledOnAllProfiles(); metrics_service_client_->IsHistorySyncEnabledOnAllProfiles();
bool is_incognito = client_->IsIncognitoSessionActive(); bool is_incognito = client_->IsIncognitoSessionActive();
if (consent_given_ && sync_enabled && !is_incognito) { if (consent_given_ && listeners_active && sync_enabled && !is_incognito) {
// TODO(skare): revise this - merged in a big change
ukm->EnableRecording( ukm->EnableRecording(
metrics_service_client_->IsExtensionSyncEnabledOnAllProfiles()); metrics_service_client_->IsExtensionSyncEnabledOnAllProfiles());
if (may_upload_) if (may_upload_)
......
...@@ -76,6 +76,7 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver, ...@@ -76,6 +76,7 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver,
base::TimeDelta GetStandardUploadInterval() override; base::TimeDelta GetStandardUploadInterval() override;
void OnRendererProcessCrash() override; void OnRendererProcessCrash() override;
bool IsHistorySyncEnabledOnAllProfiles() override; bool IsHistorySyncEnabledOnAllProfiles() override;
bool AreNotificationListenersEnabledOnAllProfiles() override;
// ukm::HistoryDeleteObserver: // ukm::HistoryDeleteObserver:
void OnHistoryDeleted() override; void OnHistoryDeleted() override;
...@@ -108,10 +109,12 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver, ...@@ -108,10 +109,12 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver,
// user is performing work. This is useful to allow some features to sleep, // user is performing work. This is useful to allow some features to sleep,
// until the machine becomes active, such as precluding UMA uploads unless // until the machine becomes active, such as precluding UMA uploads unless
// there was recent activity. // there was recent activity.
void RegisterForNotifications(); // Returns true if registration was successful.
bool RegisterForNotifications();
// Register to observe events on a browser state's services. // Register to observe events on a browser state's services.
void RegisterForBrowserStateEvents(ios::ChromeBrowserState* browser_state); // Returns true if registration was successful.
bool RegisterForBrowserStateEvents(ios::ChromeBrowserState* browser_state);
// Called when a tab is parented. // Called when a tab is parented.
void OnTabParented(web::WebState* web_state); void OnTabParented(web::WebState* web_state);
...@@ -130,6 +133,9 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver, ...@@ -130,6 +133,9 @@ class IOSChromeMetricsServiceClient : public IncognitoWebStateObserver,
// The UkmService that |this| is a client of. // The UkmService that |this| is a client of.
std::unique_ptr<ukm::UkmService> ukm_service_; std::unique_ptr<ukm::UkmService> ukm_service_;
// Whether we registered all notification listeners successfully.
bool notification_listeners_active_;
// The IOSChromeStabilityMetricsProvider instance that was registered with // The IOSChromeStabilityMetricsProvider instance that was registered with
// MetricsService. Has the same lifetime as |metrics_service_|. // MetricsService. Has the same lifetime as |metrics_service_|.
IOSChromeStabilityMetricsProvider* stability_metrics_provider_; IOSChromeStabilityMetricsProvider* stability_metrics_provider_;
......
...@@ -70,7 +70,7 @@ IOSChromeMetricsServiceClient::IOSChromeMetricsServiceClient( ...@@ -70,7 +70,7 @@ IOSChromeMetricsServiceClient::IOSChromeMetricsServiceClient(
stability_metrics_provider_(nullptr), stability_metrics_provider_(nullptr),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
RegisterForNotifications(); notification_listeners_active_ = RegisterForNotifications();
} }
IOSChromeMetricsServiceClient::~IOSChromeMetricsServiceClient() { IOSChromeMetricsServiceClient::~IOSChromeMetricsServiceClient() {
...@@ -250,7 +250,7 @@ void IOSChromeMetricsServiceClient::CollectFinalHistograms() { ...@@ -250,7 +250,7 @@ void IOSChromeMetricsServiceClient::CollectFinalHistograms() {
collect_final_metrics_done_callback_.Run(); collect_final_metrics_done_callback_.Run();
} }
void IOSChromeMetricsServiceClient::RegisterForNotifications() { bool IOSChromeMetricsServiceClient::RegisterForNotifications() {
tab_parented_subscription_ = tab_parented_subscription_ =
TabParentingGlobalObserver::GetInstance()->RegisterCallback( TabParentingGlobalObserver::GetInstance()->RegisterCallback(
base::Bind(&IOSChromeMetricsServiceClient::OnTabParented, base::Bind(&IOSChromeMetricsServiceClient::OnTabParented,
...@@ -264,12 +264,16 @@ void IOSChromeMetricsServiceClient::RegisterForNotifications() { ...@@ -264,12 +264,16 @@ void IOSChromeMetricsServiceClient::RegisterForNotifications() {
GetApplicationContext() GetApplicationContext()
->GetChromeBrowserStateManager() ->GetChromeBrowserStateManager()
->GetLoadedBrowserStates(); ->GetLoadedBrowserStates();
bool all_profiles_succeeded = true;
for (ios::ChromeBrowserState* browser_state : loaded_browser_states) { for (ios::ChromeBrowserState* browser_state : loaded_browser_states) {
RegisterForBrowserStateEvents(browser_state); if (!RegisterForBrowserStateEvents(browser_state)) {
all_profiles_succeeded = false;
}
} }
return all_profiles_succeeded;
} }
void IOSChromeMetricsServiceClient::RegisterForBrowserStateEvents( bool IOSChromeMetricsServiceClient::RegisterForBrowserStateEvents(
ios::ChromeBrowserState* browser_state) { ios::ChromeBrowserState* browser_state) {
history::HistoryService* history_service = history::HistoryService* history_service =
ios::HistoryServiceFactory::GetForBrowserState( ios::HistoryServiceFactory::GetForBrowserState(
...@@ -279,6 +283,7 @@ void IOSChromeMetricsServiceClient::RegisterForBrowserStateEvents( ...@@ -279,6 +283,7 @@ void IOSChromeMetricsServiceClient::RegisterForBrowserStateEvents(
IOSChromeProfileSyncServiceFactory::GetInstance()->GetForBrowserState( IOSChromeProfileSyncServiceFactory::GetInstance()->GetForBrowserState(
browser_state); browser_state);
ObserveServiceForSyncDisables(static_cast<syncer::SyncService*>(sync)); ObserveServiceForSyncDisables(static_cast<syncer::SyncService*>(sync));
return (history_service != nullptr && sync != nullptr);
} }
void IOSChromeMetricsServiceClient::OnTabParented(web::WebState* web_state) { void IOSChromeMetricsServiceClient::OnTabParented(web::WebState* web_state) {
...@@ -324,3 +329,8 @@ void IOSChromeMetricsServiceClient::OnIncognitoWebStateRemoved() { ...@@ -324,3 +329,8 @@ void IOSChromeMetricsServiceClient::OnIncognitoWebStateRemoved() {
bool IOSChromeMetricsServiceClient::IsHistorySyncEnabledOnAllProfiles() { bool IOSChromeMetricsServiceClient::IsHistorySyncEnabledOnAllProfiles() {
return SyncDisableObserver::IsHistorySyncEnabledOnAllProfiles(); return SyncDisableObserver::IsHistorySyncEnabledOnAllProfiles();
} }
bool IOSChromeMetricsServiceClient::
AreNotificationListenersEnabledOnAllProfiles() {
return notification_listeners_active_;
}
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