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/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 class AccountTrackerService
;
24 class GaiaAuthFetcher
;
25 class ProfileOAuth2TokenService
;
28 // Many values in SigninStatus are also associated with a timestamp.
29 // This makes it easier to keep values and their associated times together.
30 typedef std::pair
<std::string
, std::string
> TimedSigninStatusValue
;
32 // This class collects authentication, signin and token information
33 // to propagate to about:signin-internals via SigninInternalsUI.
34 class AboutSigninInternals
35 : public KeyedService
,
36 public signin_internals_util::SigninDiagnosticsObserver
,
37 public OAuth2TokenService::DiagnosticsObserver
,
38 public GaiaCookieManagerService::Observer
,
39 SigninManagerBase::Observer
,
40 SigninErrorController::Observer
{
44 // |info| will contain the dictionary of signin_status_ values as indicated
45 // in the comments for GetSigninStatus() below.
46 virtual void OnSigninStateChanged(const base::DictionaryValue
* info
) = 0;
48 // Notification that the cookie accounts are ready to be displayed.
49 virtual void OnCookieAccountsFetched(const base::DictionaryValue
* info
) = 0;
52 AboutSigninInternals(ProfileOAuth2TokenService
* token_service
,
53 AccountTrackerService
* account_tracker
,
54 SigninManagerBase
* signin_manager
,
55 SigninErrorController
* signin_error_controller
,
56 GaiaCookieManagerService
* cookie_manager_service
);
57 ~AboutSigninInternals() override
;
59 // Each instance of SigninInternalsUI adds itself as an observer to be
60 // notified of all updates that AboutSigninInternals receives.
61 void AddSigninObserver(Observer
* observer
);
62 void RemoveSigninObserver(Observer
* observer
);
64 // Pulls all signin values that have been persisted in the user prefs.
65 void RefreshSigninPrefs();
67 void Initialize(SigninClient
* client
);
69 void OnRefreshTokenReceived(std::string status
);
70 void OnAuthenticationResultReceived(std::string status
);
72 // KeyedService implementation.
73 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 // GaiaCookieManagerService::Observer implementations.
92 void OnGaiaAccountsInCookieUpdated(
93 const std::vector
<gaia::ListedAccount
>& gaia_accounts
,
94 const GoogleServiceAuthError
& error
) override
;
97 // Encapsulates diagnostic information about tokens for different services.
99 TokenInfo(const std::string
& consumer_id
,
100 const OAuth2TokenService::ScopeSet
& scopes
);
102 base::DictionaryValue
* ToValue() const;
104 static bool LessThan(const TokenInfo
* a
, const TokenInfo
* b
);
106 // Called when the token is invalidated.
109 std::string consumer_id
; // service that requested the token.
110 OAuth2TokenService::ScopeSet scopes
; // Scoped that are requested.
111 base::Time request_time
;
112 base::Time receive_time
;
113 base::Time expiration_time
;
114 GoogleServiceAuthError error
;
118 // Map account id to tokens associated to the account.
119 typedef std::map
<std::string
, std::vector
<TokenInfo
*> > TokenInfoMap
;
121 // Encapsulates both authentication and token related information. Used
122 // by SigninInternals to maintain information that needs to be shown in
123 // the about:signin-internals page.
124 struct SigninStatus
{
125 std::vector
<TimedSigninStatusValue
> timed_signin_fields
;
126 TokenInfoMap token_info_map
;
131 TokenInfo
* FindToken(const std::string
& account_id
,
132 const std::string
& consumer_id
,
133 const OAuth2TokenService::ScopeSet
& scopes
);
135 // Returns a dictionary with the following form:
137 // [ {"title": "Basic Information",
138 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
140 // { "title": "Detailed Information",
141 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
145 // { "title": account id,
146 // "data": [List of {"service" : service name,
147 // "scopes" : requested scoped,
148 // "request_time" : request time,
149 // "status" : request status} elems]
152 scoped_ptr
<base::DictionaryValue
> ToValue(
153 AccountTrackerService
* account_tracker
,
154 SigninManagerBase
* signin_manager
,
155 SigninErrorController
* signin_error_controller
,
156 ProfileOAuth2TokenService
* token_service
,
157 const std::string
& product_version
);
160 // SigninManager::SigninDiagnosticsObserver implementation.
161 void NotifySigninValueChanged(
162 const signin_internals_util::TimedSigninStatusField
& field
,
163 const std::string
& value
) override
;
165 // OAuth2TokenService::DiagnosticsObserver implementations.
166 void OnAccessTokenRequested(
167 const std::string
& account_id
,
168 const std::string
& consumer_id
,
169 const OAuth2TokenService::ScopeSet
& scopes
) override
;
170 void OnFetchAccessTokenComplete(const std::string
& account_id
,
171 const std::string
& consumer_id
,
172 const OAuth2TokenService::ScopeSet
& scopes
,
173 GoogleServiceAuthError error
,
174 base::Time expiration_time
) override
;
175 void OnTokenRemoved(const std::string
& account_id
,
176 const OAuth2TokenService::ScopeSet
& scopes
) override
;
178 // SigninManagerBase::Observer implementations.
179 void GoogleSigninFailed(const GoogleServiceAuthError
& error
) override
;
180 void GoogleSigninSucceeded(const std::string
& account_id
,
181 const std::string
& username
,
182 const std::string
& password
) override
;
183 void GoogleSignedOut(const std::string
& account_id
,
184 const std::string
& username
) override
;
186 void NotifyObservers();
188 // SigninErrorController::Observer implementation
189 void OnErrorChanged() override
;
191 // Weak pointer to the token service.
192 ProfileOAuth2TokenService
* token_service_
;
194 // Weak pointer to the account tracker.
195 AccountTrackerService
* account_tracker_
;
197 // Weak pointer to the signin manager.
198 SigninManagerBase
* signin_manager_
;
200 // Weak pointer to the client.
201 SigninClient
* client_
;
203 // Weak pointer to the SigninErrorController
204 SigninErrorController
* signin_error_controller_
;
206 // Weak pointer to the GaiaCookieManagerService
207 GaiaCookieManagerService
* cookie_manager_service_
;
209 // Encapsulates the actual signin and token related values.
210 // Most of the values are mirrored in the prefs for persistence.
211 SigninStatus signin_status_
;
213 base::ObserverList
<Observer
> signin_observers_
;
215 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals
);
218 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_