Handle account removal correctly on all platforms.
[chromium-blink-merge.git] / components / signin / core / browser / about_signin_internals.h
blobf3458219fa0f40210268a67eaa9d95ffa3403a89
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/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/values.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "components/signin/core/browser/signin_client.h"
16 #include "components/signin/core/browser/signin_internals_util.h"
17 #include "components/signin/core/browser/signin_manager.h"
18 #include "google_apis/gaia/gaia_auth_consumer.h"
19 #include "google_apis/gaia/oauth2_token_service.h"
21 class GaiaAuthFetcher;
22 class ProfileOAuth2TokenService;
23 class SigninClient;
24 class SigninManagerBase;
26 // Many values in SigninStatus are also associated with a timestamp.
27 // This makes it easier to keep values and their associated times together.
28 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
30 // This class collects authentication, signin and token information
31 // to propagate to about:signin-internals via SigninInternalsUI.
32 class AboutSigninInternals
33 : public KeyedService,
34 public signin_internals_util::SigninDiagnosticsObserver,
35 public OAuth2TokenService::DiagnosticsObserver,
36 public GaiaAuthConsumer {
37 public:
38 class Observer {
39 public:
40 // |info| will contain the dictionary of signin_status_ values as indicated
41 // in the comments for GetSigninStatus() below.
42 virtual void OnSigninStateChanged(
43 scoped_ptr<base::DictionaryValue> info) = 0;
45 // Notification that the cookie accounts are ready to be displayed.
46 virtual void OnCookieAccountsFetched(
47 scoped_ptr<base::DictionaryValue> info) = 0;
50 AboutSigninInternals(ProfileOAuth2TokenService* token_service,
51 SigninManagerBase* signin_manager);
52 virtual ~AboutSigninInternals();
54 // Each instance of SigninInternalsUI adds itself as an observer to be
55 // notified of all updates that AboutSigninInternals receives.
56 void AddSigninObserver(Observer* observer);
57 void RemoveSigninObserver(Observer* observer);
59 // Pulls all signin values that have been persisted in the user prefs.
60 void RefreshSigninPrefs();
62 // SigninManager::SigninDiagnosticsObserver implementation.
63 virtual void NotifySigninValueChanged(
64 const signin_internals_util::UntimedSigninStatusField& field,
65 const std::string& value) OVERRIDE;
67 virtual void NotifySigninValueChanged(
68 const signin_internals_util::TimedSigninStatusField& field,
69 const std::string& value) OVERRIDE;
71 void Initialize(SigninClient* client);
73 // KeyedService implementation.
74 virtual void Shutdown() OVERRIDE;
76 // Returns a dictionary of values in signin_status_ for use in
77 // about:signin-internals. The values are formatted as shown -
79 // { "signin_info" :
80 // [ {"title": "Basic Information",
81 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
82 // },
83 // { "title": "Detailed Information",
84 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
85 // }],
86 // "token_info" :
87 // [ List of {"name": "foo-name", "token" : "foo-token",
88 // "status": "foo_stat", "time" : "foo_time"} elems]
89 // }
90 scoped_ptr<base::DictionaryValue> GetSigninStatus();
92 // Triggers a ListAccounts call to acquire a list of the email addresses
93 // corresponding to the cookies residing on the current cookie jar.
94 void GetCookieAccountsAsync();
96 // OAuth2TokenService::DiagnosticsObserver implementations.
97 virtual void OnAccessTokenRequested(
98 const std::string& account_id,
99 const std::string& consumer_id,
100 const OAuth2TokenService::ScopeSet& scopes) OVERRIDE;
101 virtual void OnFetchAccessTokenComplete(
102 const std::string& account_id,
103 const std::string& consumer_id,
104 const OAuth2TokenService::ScopeSet& scopes,
105 GoogleServiceAuthError error,
106 base::Time expiration_time) OVERRIDE;
107 virtual void OnTokenRemoved(const std::string& account_id,
108 const OAuth2TokenService::ScopeSet& scopes)
109 OVERRIDE;
111 void OnRefreshTokenReceived(std::string status);
112 void OnAuthenticationResultReceived(std::string status);
114 private:
115 // Encapsulates diagnostic information about tokens for different services.
116 struct TokenInfo {
117 TokenInfo(const std::string& consumer_id,
118 const OAuth2TokenService::ScopeSet& scopes);
119 ~TokenInfo();
120 base::DictionaryValue* ToValue() const;
122 static bool LessThan(const TokenInfo* a, const TokenInfo* b);
124 // Called when the token is invalidated.
125 void Invalidate();
127 std::string consumer_id; // service that requested the token.
128 OAuth2TokenService::ScopeSet scopes; // Scoped that are requested.
129 base::Time request_time;
130 base::Time receive_time;
131 base::Time expiration_time;
132 GoogleServiceAuthError error;
133 bool removed_;
136 // Map account id to tokens associated to the account.
137 typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
139 // Encapsulates both authentication and token related information. Used
140 // by SigninInternals to maintain information that needs to be shown in
141 // the about:signin-internals page.
142 struct SigninStatus {
143 std::vector<std::string> untimed_signin_fields;
144 std::vector<TimedSigninStatusValue> timed_signin_fields;
145 TokenInfoMap token_info_map;
147 SigninStatus();
148 ~SigninStatus();
150 TokenInfo* FindToken(const std::string& account_id,
151 const std::string& consumer_id,
152 const OAuth2TokenService::ScopeSet& scopes);
154 // Returns a dictionary with the following form:
155 // { "signin_info" :
156 // [ {"title": "Basic Information",
157 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
158 // },
159 // { "title": "Detailed Information",
160 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
161 // }],
162 // "token_info" :
163 // [ List of
164 // { "title": account id,
165 // "data": [List of {"service" : service name,
166 // "scopes" : requested scoped,
167 // "request_time" : request time,
168 // "status" : request status} elems]
169 // }],
170 // }
171 scoped_ptr<base::DictionaryValue> ToValue(std::string product_version);
174 void NotifyObservers();
177 // Overriden from GaiaAuthConsumer.
178 virtual void OnListAccountsSuccess(const std::string& data) OVERRIDE;
179 virtual void OnListAccountsFailure(const GoogleServiceAuthError& error)
180 OVERRIDE;
182 // Callback for ListAccounts. Once the email addresses are fetched from GAIA,
183 // they are pushed to the signin_internals_ui.
184 void OnListAccountsComplete(
185 std::vector<std::pair<std::string, bool> >& gaia_accounts);
187 // Called when a cookie changes. If the cookie relates to a GAIA LSID cookie,
188 // then we call ListAccounts and update the UI element.
189 void OnCookieChanged(const net::CanonicalCookie* cookie);
191 // Weak pointer to the token service.
192 ProfileOAuth2TokenService* token_service_;
194 // Weak pointer to the signin manager.
195 SigninManagerBase* signin_manager_;
197 // Weak pointer to the client.
198 SigninClient* client_;
200 // Fetcher for information about accounts in the cookie jar from GAIA.
201 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_;
203 // Encapsulates the actual signin and token related values.
204 // Most of the values are mirrored in the prefs for persistence.
205 SigninStatus signin_status_;
207 ObserverList<Observer> signin_observers_;
209 scoped_ptr<SigninClient::CookieChangedCallbackList::Subscription>
210 cookie_changed_subscription_;
212 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
215 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_