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_
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/signin_client.h"
17 #include "components/signin/core/browser/signin_internals_util.h"
18 #include "components/signin/core/browser/signin_manager.h"
19 #include "google_apis/gaia/gaia_auth_consumer.h"
20 #include "google_apis/gaia/oauth2_token_service.h"
22 class GaiaAuthFetcher;
23 class ProfileOAuth2TokenService;
25 class SigninManagerBase;
27 // Many values in SigninStatus are also associated with a timestamp.
28 // This makes it easier to keep values and their associated times together.
29 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
31 // This class collects authentication, signin and token information
32 // to propagate to about:signin-internals via SigninInternalsUI.
33 class AboutSigninInternals
34 : public KeyedService,
35 public signin_internals_util::SigninDiagnosticsObserver,
36 public OAuth2TokenService::DiagnosticsObserver,
37 public GaiaAuthConsumer {
41 // |info| will contain the dictionary of signin_status_ values as indicated
42 // in the comments for GetSigninStatus() below.
43 virtual void OnSigninStateChanged(const base::DictionaryValue* info) = 0;
45 // Notification that the cookie accounts are ready to be displayed.
46 virtual void OnCookieAccountsFetched(const base::DictionaryValue* info) = 0;
49 AboutSigninInternals(ProfileOAuth2TokenService* token_service,
50 SigninManagerBase* signin_manager);
51 virtual ~AboutSigninInternals();
53 // Each instance of SigninInternalsUI adds itself as an observer to be
54 // notified of all updates that AboutSigninInternals receives.
55 void AddSigninObserver(Observer* observer);
56 void RemoveSigninObserver(Observer* observer);
58 // Pulls all signin values that have been persisted in the user prefs.
59 void RefreshSigninPrefs();
61 // SigninManager::SigninDiagnosticsObserver implementation.
62 virtual void NotifySigninValueChanged(
63 const signin_internals_util::UntimedSigninStatusField& field,
64 const std::string& value) OVERRIDE;
66 virtual void NotifySigninValueChanged(
67 const signin_internals_util::TimedSigninStatusField& field,
68 const std::string& value) OVERRIDE;
70 void Initialize(SigninClient* client);
72 // KeyedService implementation.
73 virtual void Shutdown() OVERRIDE;
75 // Returns a dictionary of values in signin_status_ for use in
76 // about:signin-internals. The values are formatted as shown -
79 // [ {"title": "Basic Information",
80 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
82 // { "title": "Detailed Information",
83 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
86 // [ List of {"name": "foo-name", "token" : "foo-token",
87 // "status": "foo_stat", "time" : "foo_time"} elems]
89 scoped_ptr<base::DictionaryValue> GetSigninStatus();
91 // Triggers a ListAccounts call to acquire a list of the email addresses
92 // corresponding to the cookies residing on the current cookie jar.
93 void GetCookieAccountsAsync();
95 // OAuth2TokenService::DiagnosticsObserver implementations.
96 virtual void OnAccessTokenRequested(
97 const std::string& account_id,
98 const std::string& consumer_id,
99 const OAuth2TokenService::ScopeSet& scopes) OVERRIDE;
100 virtual void OnFetchAccessTokenComplete(
101 const std::string& account_id,
102 const std::string& consumer_id,
103 const OAuth2TokenService::ScopeSet& scopes,
104 GoogleServiceAuthError error,
105 base::Time expiration_time) OVERRIDE;
106 virtual void OnTokenRemoved(const std::string& account_id,
107 const OAuth2TokenService::ScopeSet& scopes)
110 void OnRefreshTokenReceived(std::string status);
111 void OnAuthenticationResultReceived(std::string status);
114 // Encapsulates diagnostic information about tokens for different services.
116 TokenInfo(const std::string& consumer_id,
117 const OAuth2TokenService::ScopeSet& scopes);
119 base::DictionaryValue* ToValue() const;
121 static bool LessThan(const TokenInfo* a, const TokenInfo* b);
123 // Called when the token is invalidated.
126 std::string consumer_id; // service that requested the token.
127 OAuth2TokenService::ScopeSet scopes; // Scoped that are requested.
128 base::Time request_time;
129 base::Time receive_time;
130 base::Time expiration_time;
131 GoogleServiceAuthError error;
135 // Map account id to tokens associated to the account.
136 typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
138 // Encapsulates both authentication and token related information. Used
139 // by SigninInternals to maintain information that needs to be shown in
140 // the about:signin-internals page.
141 struct SigninStatus {
142 std::vector<std::string> untimed_signin_fields;
143 std::vector<TimedSigninStatusValue> timed_signin_fields;
144 TokenInfoMap token_info_map;
149 TokenInfo* FindToken(const std::string& account_id,
150 const std::string& consumer_id,
151 const OAuth2TokenService::ScopeSet& scopes);
153 // Returns a dictionary with the following form:
155 // [ {"title": "Basic Information",
156 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
158 // { "title": "Detailed Information",
159 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
163 // { "title": account id,
164 // "data": [List of {"service" : service name,
165 // "scopes" : requested scoped,
166 // "request_time" : request time,
167 // "status" : request status} elems]
170 scoped_ptr<base::DictionaryValue> ToValue(std::string product_version);
173 void NotifyObservers();
176 // Overriden from GaiaAuthConsumer.
177 virtual void OnListAccountsSuccess(const std::string& data) OVERRIDE;
178 virtual void OnListAccountsFailure(const GoogleServiceAuthError& error)
181 // Callback for ListAccounts. Once the email addresses are fetched from GAIA,
182 // they are pushed to the signin_internals_ui.
183 void OnListAccountsComplete(
184 std::vector<std::pair<std::string, bool> >& gaia_accounts);
186 // Called when a cookie changes. If the cookie relates to a GAIA LSID cookie,
187 // then we call ListAccounts and update the UI element.
188 void OnCookieChanged(const net::CanonicalCookie* cookie);
190 // Weak pointer to the token service.
191 ProfileOAuth2TokenService* token_service_;
193 // Weak pointer to the signin manager.
194 SigninManagerBase* signin_manager_;
196 // Weak pointer to the client.
197 SigninClient* client_;
199 // Fetcher for information about accounts in the cookie jar from GAIA.
200 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_;
202 // Encapsulates the actual signin and token related values.
203 // Most of the values are mirrored in the prefs for persistence.
204 SigninStatus signin_status_;
206 ObserverList<Observer> signin_observers_;
208 scoped_ptr<SigninClient::CookieChangedCallbackList::Subscription>
209 cookie_changed_subscription_;
211 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
214 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_