summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/signin_status_metrics_provider.h
blob: 28c82077d60e80ac21da2cbfffa4cb3c6453012d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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.

#ifndef CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_
#define CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "components/metrics/metrics_provider.h"
#include "components/signin/core/browser/signin_manager_base.h"

class Browser;

namespace base {
class FilePath;
}

// Collect login status of all opened profiles during one UMA session and record
// the value into a histogram before UMA log is uploaded. It's currently not
// supported on platform chromeos, Android or iOS.
class SigninStatusMetricsProvider : public metrics::MetricsProvider,
                                    public chrome::BrowserListObserver,
                                    public SigninManagerBase::Observer,
                                    public SigninManagerFactory::Observer {
 public:
  virtual ~SigninStatusMetricsProvider();

  // Record the collected sign-in status into a histogram and re-check current
  // sign-in status to get prepared for the next UMA session. Called by
  // MetricsServiceClient when it is collecting final metrics.
  void RecordSigninStatusHistogram();

  // Factory method, creates a new instance of this class.
  static SigninStatusMetricsProvider* CreateInstance();

 private:
  // 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.
  explicit SigninStatusMetricsProvider(bool is_test);

  FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider,
                           UpdateInitialSigninStatus);
  FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider,
                           UpdateStatusWhenBrowserAdded);
  FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider, GoogleSigninSucceeded);
  FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider, GoogleSignedOut);

  // Possible sign-in status of all opened profiles during one UMA session. For
  // MIXED_SIGNIN_STATUS, at least one signed-in profile and at least one
  // unsigned-in profile were opened between two UMA log uploads.
  enum ProfilesSigninStatus {
    ALL_PROFILES_SIGNED_IN,
    ALL_PROFILES_NOT_SIGNED_IN,
    MIXED_SIGNIN_STATUS,
    UNKNOWN_SIGNIN_STATUS,
    ERROR_GETTING_SIGNIN_STATUS,
    SIGNIN_STATUS_MAX,
  };

  // chrome::BrowserListObserver:
  // This will never be called on Android.
  virtual void OnBrowserAdded(Browser* browser) OVERRIDE;

  // SigninManagerFactory::Observer:
  virtual void SigninManagerCreated(SigninManagerBase* manager) OVERRIDE;
  virtual void SigninManagerShutdown(SigninManagerBase* manager) OVERRIDE;

  // SigninManagerBase::Observer:
  virtual void GoogleSigninSucceeded(const std::string& account_id,
                                     const std::string& username,
                                     const std::string& password) OVERRIDE;
  virtual 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);

  // Update the sign-in status right after a new browser is opened.
  void UpdateStatusWhenBrowserAdded(bool signed_in);

  // Compute current sign-in status of all opened profiles.
  void ComputeCurrentSigninStatus();

  // Sets the value of |signin_status_|. It ensures that |signin_status_| will
  // not be changed if its value is already ERROR_GETTING_SIGNIN_STATUS.
  void SetSigninStatus(ProfilesSigninStatus new_status);

  // Get the current recorded sign-in status. For testing purpose only.
  ProfilesSigninStatus GetSigninStatusForTesting();

  // Sign-in status of all profiles seen so far.
  ProfilesSigninStatus signin_status_;

  // 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, SigninStatusMetricsProvider>
      scoped_observer_;

  // Whether the instance is for testing or not.
  bool is_test_;

  base::WeakPtrFactory<SigninStatusMetricsProvider> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(SigninStatusMetricsProvider);
};

#endif  // CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_