28c82077d60e80ac21da2cbfffa4cb3c6453012d
[platform/framework/web/crosswalk.git] / src / chrome / browser / metrics / signin_status_metrics_provider.h
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.
4
5 #ifndef CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_
6 #define CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_
7
8 #include <string>
9
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"
17
18 class Browser;
19
20 namespace base {
21 class FilePath;
22 }
23
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 {
31  public:
32   virtual ~SigninStatusMetricsProvider();
33
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();
38
39   // Factory method, creates a new instance of this class.
40   static SigninStatusMetricsProvider* CreateInstance();
41
42  private:
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);
48
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);
55
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,
62     MIXED_SIGNIN_STATUS,
63     UNKNOWN_SIGNIN_STATUS,
64     ERROR_GETTING_SIGNIN_STATUS,
65     SIGNIN_STATUS_MAX,
66   };
67
68   // chrome::BrowserListObserver:
69   // This will never be called on Android.
70   virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
71
72   // SigninManagerFactory::Observer:
73   virtual void SigninManagerCreated(SigninManagerBase* manager) OVERRIDE;
74   virtual void SigninManagerShutdown(SigninManagerBase* manager) OVERRIDE;
75
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;
82
83   // Obtain sign-in status and add observers.
84   void Initialize();
85
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);
92
93   // Update the sign-in status right after a new browser is opened.
94   void UpdateStatusWhenBrowserAdded(bool signed_in);
95
96   // Compute current sign-in status of all opened profiles.
97   void ComputeCurrentSigninStatus();
98
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);
102
103   // Get the current recorded sign-in status. For testing purpose only.
104   ProfilesSigninStatus GetSigninStatusForTesting();
105
106   // Sign-in status of all profiles seen so far.
107   ProfilesSigninStatus signin_status_;
108
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>
112       scoped_observer_;
113
114   // Whether the instance is for testing or not.
115   bool is_test_;
116
117   base::WeakPtrFactory<SigninStatusMetricsProvider> weak_ptr_factory_;
118
119   DISALLOW_COPY_AND_ASSIGN(SigninStatusMetricsProvider);
120 };
121
122 #endif  // CHROME_BROWSER_METRICS_SIGNIN_STATUS_METRICS_PROVIDER_H_