1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_
6 #define CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/ui/browser_list_observer.h"
15 #include "components/metrics/metrics_provider.h"
16 #include "components/signin/core/browser/signin_manager_base.h"
24 // Collect login status of all opened profiles during one UMA session and record
25 // the value into a histogram before UMA log is uploaded. It's currently not
26 // supported on platform chromeos, Android or iOS.
27 class SigninStatusMetricsProvider : public metrics::MetricsProvider,
28 public chrome::BrowserListObserver,
29 public SigninManagerBase::Observer,
30 public SigninManagerFactory::Observer {
32 virtual ~SigninStatusMetricsProvider();
34 // Record the collected sign-in status into a histogram and re-check current
35 // sign-in status to get prepared for the next UMA session. Called by
36 // MetricsServiceClient when it is collecting final metrics.
37 void RecordSigninStatusHistogram();
39 // Factory method, creates a new instance of this class.
40 static SigninStatusMetricsProvider* CreateInstance();
43 // The boolean |is_test| indicates whether or not this is an instance for
44 // testing purpose. If so, skip the initialization. Except for testing
45 // purpose, this class's instance should be created through the static
46 // CreateInstance() method.
47 explicit SigninStatusMetricsProvider(bool is_test);
49 FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider,
50 UpdateInitialSigninStatus);
51 FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider,
52 UpdateStatusWhenBrowserAdded);
53 FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider, GoogleSigninSucceeded);
54 FRIEND_TEST_ALL_PREFIXES(SigninStatusMetricsProvider, GoogleSignedOut);
56 // Possible sign-in status of all opened profiles during one UMA session. For
57 // MIXED_SIGNIN_STATUS, at least one signed-in profile and at least one
58 // unsigned-in profile were opened between two UMA log uploads.
59 enum ProfilesSigninStatus {
60 ALL_PROFILES_SIGNED_IN,
61 ALL_PROFILES_NOT_SIGNED_IN,
63 UNKNOWN_SIGNIN_STATUS,
64 ERROR_GETTING_SIGNIN_STATUS,
68 // chrome::BrowserListObserver:
69 // This will never be called on Android.
70 virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
72 // SigninManagerFactory::Observer:
73 virtual void SigninManagerCreated(SigninManagerBase* manager) OVERRIDE;
74 virtual void SigninManagerShutdown(SigninManagerBase* manager) OVERRIDE;
76 // SigninManagerBase::Observer:
77 virtual void GoogleSigninSucceeded(const std::string& account_id,
78 const std::string& username,
79 const std::string& password) OVERRIDE;
80 virtual void GoogleSignedOut(const std::string& account_id,
81 const std::string& username) OVERRIDE;
83 // Obtain sign-in status and add observers.
86 // Update the sign-in status based on all currently opened profiles. Called by
87 // ComputeCurrentSigninStatus at class construction and right after each UMA
88 // log upload. |total_count| is the number of opened profiles and
89 // |signed_in_count| represents the number of signed-in profiles among those
90 // |total_count| profiles.
91 void UpdateInitialSigninStatus(size_t total_count, size_t signed_in_count);
93 // Update the sign-in status right after a new browser is opened.
94 void UpdateStatusWhenBrowserAdded(bool signed_in);
96 // Compute current sign-in status of all opened profiles.
97 void ComputeCurrentSigninStatus();
99 // Sets the value of |signin_status_|. It ensures that |signin_status_| will
100 // not be changed if its value is already ERROR_GETTING_SIGNIN_STATUS.
101 void SetSigninStatus(ProfilesSigninStatus new_status);
103 // Get the current recorded sign-in status. For testing purpose only.
104 ProfilesSigninStatus GetSigninStatusForTesting();
106 // Sign-in status of all profiles seen so far.
107 ProfilesSigninStatus signin_status_;
109 // Used to track the SigninManagers that this instance is observing so that
110 // this instance can be removed as an observer on its destruction.
111 ScopedObserver<SigninManagerBase, SigninStatusMetricsProvider>
114 // Whether the instance is for testing or not.
117 base::WeakPtrFactory<SigninStatusMetricsProvider> weak_ptr_factory_;
119 DISALLOW_COPY_AND_ASSIGN(SigninStatusMetricsProvider);
122 #endif // CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_