blob: 73cd0a4d6764467ce9f5e2f509efe2c7fbca004d [file] [log] [blame]
// Copyright 2014 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 <stddef.h>
#include <memory>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "build/build_config.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "components/signin/core/browser/signin_status_metrics_provider_base.h"
#include "components/signin/core/browser/signin_status_metrics_provider_delegate.h"
namespace metrics {
class ChromeUserMetricsExtension;
class SigninStatusMetricsProviderDelegate;
// Collect login status of all opened profiles during one UMA session and
// record the value into a histogram before UMA log is uploaded on platform
// Windows, Linux, Mac and Android.
class SigninStatusMetricsProvider : public SigninStatusMetricsProviderBase,
public SigninManagerBase::Observer {
~SigninStatusMetricsProvider() override;
// SigninStatusMetricsProviderBase:
void ProvideCurrentSessionData(
metrics::ChromeUserMetricsExtension* uma_proto) override;
// Factory method, creates a new instance of this class.
static std::unique_ptr<SigninStatusMetricsProvider> CreateInstance(
std::unique_ptr<SigninStatusMetricsProviderDelegate> delegate);
// Update the sign-in status when a SigninManager is created.
void OnSigninManagerCreated(SigninManagerBase* manager);
// Update the sign-in status when a SigninManager is shut down.
void OnSigninManagerShutdown(SigninManagerBase* manager);
// Updates the initial sign-in status. For testing purpose only.
void UpdateInitialSigninStatusForTesting(size_t total_count,
size_t signed_in_count);
// Get the current recorded sign-in status. For testing purpose only.
SigninStatus GetSigninStatusForTesting();
FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProviderTest, GoogleSignedOut);
// The boolean |is_test| indicates whether or not this is an instance for
// testing purpose. If so, skip the initialization. Except for testing
// purpose, this class's instance should be created through the static
// CreateInstance() method.
std::unique_ptr<SigninStatusMetricsProviderDelegate> delegate,
bool is_test);
// SigninManagerBase::Observer:
void GoogleSigninSucceeded(const std::string& account_id,
const std::string& username) override;
void GoogleSignedOut(const std::string& account_id,
const std::string& username) override;
// Obtain sign-in status and add observers.
void Initialize();
// Update the sign-in status based on all currently opened profiles. Called by
// ComputeCurrentSigninStatus at class construction and right after each UMA
// log upload. |total_count| is the number of opened profiles and
// |signed_in_count| represents the number of signed-in profiles among those
// |total_count| profiles.
void UpdateInitialSigninStatus(size_t total_count, size_t signed_in_count);
// Compute current sign-in status of all opened profiles.
void ComputeCurrentSigninStatus();
std::unique_ptr<SigninStatusMetricsProviderDelegate> delegate_;
// Used to track the SigninManagers that this instance is observing so that
// this instance can be removed as an observer on its destruction.
ScopedObserver<SigninManagerBase, SigninManagerBase::Observer>
// Whether the instance is for testing or not.
bool is_test_;
base::WeakPtrFactory<SigninStatusMetricsProvider> weak_ptr_factory_;