Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / components / signin / core / browser / about_signin_internals.h
blob55e4de6d5ed73fa26a28b93b4a358037f8e24dbc
1 // Copyright (c) 2012 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 COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_
8 #include <map>
9 #include <string>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/values.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
17 #include "components/signin/core/browser/signin_client.h"
18 #include "components/signin/core/browser/signin_error_controller.h"
19 #include "components/signin/core/browser/signin_internals_util.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "google_apis/gaia/oauth2_token_service.h"
23 namespace user_prefs {
24 class PrefRegistrySyncable;
27 class AccountTrackerService;
28 class GaiaAuthFetcher;
29 class ProfileOAuth2TokenService;
30 class SigninClient;
32 // Many values in SigninStatus are also associated with a timestamp.
33 // This makes it easier to keep values and their associated times together.
34 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
36 // This class collects authentication, signin and token information
37 // to propagate to about:signin-internals via SigninInternalsUI.
38 class AboutSigninInternals
39 : public KeyedService,
40 public signin_internals_util::SigninDiagnosticsObserver,
41 public OAuth2TokenService::DiagnosticsObserver,
42 public GaiaCookieManagerService::Observer,
43 SigninManagerBase::Observer,
44 SigninErrorController::Observer {
45 public:
46 class Observer {
47 public:
48 // |info| will contain the dictionary of signin_status_ values as indicated
49 // in the comments for GetSigninStatus() below.
50 virtual void OnSigninStateChanged(const base::DictionaryValue* info) = 0;
52 // Notification that the cookie accounts are ready to be displayed.
53 virtual void OnCookieAccountsFetched(const base::DictionaryValue* info) = 0;
56 AboutSigninInternals(ProfileOAuth2TokenService* token_service,
57 AccountTrackerService* account_tracker,
58 SigninManagerBase* signin_manager,
59 SigninErrorController* signin_error_controller,
60 GaiaCookieManagerService* cookie_manager_service);
61 ~AboutSigninInternals() override;
63 // Registers the preferences used by AboutSigninInternals.
64 static void RegisterPrefs(user_prefs::PrefRegistrySyncable* user_prefs);
66 // Each instance of SigninInternalsUI adds itself as an observer to be
67 // notified of all updates that AboutSigninInternals receives.
68 void AddSigninObserver(Observer* observer);
69 void RemoveSigninObserver(Observer* observer);
71 // Pulls all signin values that have been persisted in the user prefs.
72 void RefreshSigninPrefs();
74 void Initialize(SigninClient* client);
76 void OnRefreshTokenReceived(std::string status);
77 void OnAuthenticationResultReceived(std::string status);
79 // KeyedService implementation.
80 void Shutdown() override;
82 // Returns a dictionary of values in signin_status_ for use in
83 // about:signin-internals. The values are formatted as shown -
85 // { "signin_info" :
86 // [ {"title": "Basic Information",
87 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
88 // },
89 // { "title": "Detailed Information",
90 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
91 // }],
92 // "token_info" :
93 // [ List of {"name": "foo-name", "token" : "foo-token",
94 // "status": "foo_stat", "time" : "foo_time"} elems]
95 // }
96 scoped_ptr<base::DictionaryValue> GetSigninStatus();
98 // GaiaCookieManagerService::Observer implementations.
99 void OnGaiaAccountsInCookieUpdated(
100 const std::vector<gaia::ListedAccount>& gaia_accounts,
101 const GoogleServiceAuthError& error) override;
103 private:
104 // Encapsulates diagnostic information about tokens for different services.
105 struct TokenInfo {
106 TokenInfo(const std::string& consumer_id,
107 const OAuth2TokenService::ScopeSet& scopes);
108 ~TokenInfo();
109 base::DictionaryValue* ToValue() const;
111 static bool LessThan(const TokenInfo* a, const TokenInfo* b);
113 // Called when the token is invalidated.
114 void Invalidate();
116 std::string consumer_id; // service that requested the token.
117 OAuth2TokenService::ScopeSet scopes; // Scoped that are requested.
118 base::Time request_time;
119 base::Time receive_time;
120 base::Time expiration_time;
121 GoogleServiceAuthError error;
122 bool removed_;
125 // Map account id to tokens associated to the account.
126 typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
128 // Encapsulates both authentication and token related information. Used
129 // by SigninInternals to maintain information that needs to be shown in
130 // the about:signin-internals page.
131 struct SigninStatus {
132 std::vector<TimedSigninStatusValue> timed_signin_fields;
133 TokenInfoMap token_info_map;
135 SigninStatus();
136 ~SigninStatus();
138 TokenInfo* FindToken(const std::string& account_id,
139 const std::string& consumer_id,
140 const OAuth2TokenService::ScopeSet& scopes);
142 // Returns a dictionary with the following form:
143 // { "signin_info" :
144 // [ {"title": "Basic Information",
145 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
146 // },
147 // { "title": "Detailed Information",
148 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
149 // }],
150 // "token_info" :
151 // [ List of
152 // { "title": account id,
153 // "data": [List of {"service" : service name,
154 // "scopes" : requested scoped,
155 // "request_time" : request time,
156 // "status" : request status} elems]
157 // }],
158 // }
159 scoped_ptr<base::DictionaryValue> ToValue(
160 AccountTrackerService* account_tracker,
161 SigninManagerBase* signin_manager,
162 SigninErrorController* signin_error_controller,
163 ProfileOAuth2TokenService* token_service,
164 const std::string& product_version);
167 // SigninManager::SigninDiagnosticsObserver implementation.
168 void NotifySigninValueChanged(
169 const signin_internals_util::TimedSigninStatusField& field,
170 const std::string& value) override;
172 // OAuth2TokenService::DiagnosticsObserver implementations.
173 void OnAccessTokenRequested(
174 const std::string& account_id,
175 const std::string& consumer_id,
176 const OAuth2TokenService::ScopeSet& scopes) override;
177 void OnFetchAccessTokenComplete(const std::string& account_id,
178 const std::string& consumer_id,
179 const OAuth2TokenService::ScopeSet& scopes,
180 GoogleServiceAuthError error,
181 base::Time expiration_time) override;
182 void OnTokenRemoved(const std::string& account_id,
183 const OAuth2TokenService::ScopeSet& scopes) override;
185 // SigninManagerBase::Observer implementations.
186 void GoogleSigninFailed(const GoogleServiceAuthError& error) override;
187 void GoogleSigninSucceeded(const std::string& account_id,
188 const std::string& username,
189 const std::string& password) override;
190 void GoogleSignedOut(const std::string& account_id,
191 const std::string& username) override;
193 void NotifyObservers();
195 // SigninErrorController::Observer implementation
196 void OnErrorChanged() override;
198 // Weak pointer to the token service.
199 ProfileOAuth2TokenService* token_service_;
201 // Weak pointer to the account tracker.
202 AccountTrackerService* account_tracker_;
204 // Weak pointer to the signin manager.
205 SigninManagerBase* signin_manager_;
207 // Weak pointer to the client.
208 SigninClient* client_;
210 // Weak pointer to the SigninErrorController
211 SigninErrorController* signin_error_controller_;
213 // Weak pointer to the GaiaCookieManagerService
214 GaiaCookieManagerService* cookie_manager_service_;
216 // Encapsulates the actual signin and token related values.
217 // Most of the values are mirrored in the prefs for persistence.
218 SigninStatus signin_status_;
220 base::ObserverList<Observer> signin_observers_;
222 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
225 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_