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/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_internals_util.h"
16 #include "components/signin/core/browser/signin_manager.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
19 class ProfileOAuth2TokenService;
21 class SigninManagerBase;
23 // Many values in SigninStatus are also associated with a timestamp.
24 // This makes it easier to keep values and their associated times together.
25 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
27 // This class collects authentication, signin and token information
28 // to propagate to about:signin-internals via SigninInternalsUI.
29 class AboutSigninInternals
30 : public KeyedService,
31 public signin_internals_util::SigninDiagnosticsObserver,
32 public OAuth2TokenService::DiagnosticsObserver {
36 // |info| will contain the dictionary of signin_status_ values as indicated
37 // in the comments for GetSigninStatus() below.
38 virtual void OnSigninStateChanged(
39 scoped_ptr<base::DictionaryValue> info) = 0;
42 AboutSigninInternals(ProfileOAuth2TokenService* token_service,
43 SigninManagerBase* signin_manager);
44 virtual ~AboutSigninInternals();
46 // Each instance of SigninInternalsUI adds itself as an observer to be
47 // notified of all updates that AboutSigninInternals receives.
48 void AddSigninObserver(Observer* observer);
49 void RemoveSigninObserver(Observer* observer);
51 // Pulls all signin values that have been persisted in the user prefs.
52 void RefreshSigninPrefs();
54 // SigninManager::SigninDiagnosticsObserver implementation.
55 virtual void NotifySigninValueChanged(
56 const signin_internals_util::UntimedSigninStatusField& field,
57 const std::string& value) OVERRIDE;
59 virtual void NotifySigninValueChanged(
60 const signin_internals_util::TimedSigninStatusField& field,
61 const std::string& value) OVERRIDE;
63 void Initialize(SigninClient* client);
65 // KeyedService implementation.
66 virtual void Shutdown() OVERRIDE;
68 // Returns a dictionary of values in signin_status_ for use in
69 // about:signin-internals. The values are formatted as shown -
72 // [ {"title": "Basic Information",
73 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
75 // { "title": "Detailed Information",
76 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
79 // [ List of {"name": "foo-name", "token" : "foo-token",
80 // "status": "foo_stat", "time" : "foo_time"} elems]
82 scoped_ptr<base::DictionaryValue> GetSigninStatus();
84 // OAuth2TokenService::DiagnosticsObserver implementations.
85 virtual void OnAccessTokenRequested(
86 const std::string& account_id,
87 const std::string& consumer_id,
88 const OAuth2TokenService::ScopeSet& scopes) OVERRIDE;
89 virtual void OnFetchAccessTokenComplete(
90 const std::string& account_id,
91 const std::string& consumer_id,
92 const OAuth2TokenService::ScopeSet& scopes,
93 GoogleServiceAuthError error,
94 base::Time expiration_time) OVERRIDE;
95 virtual void OnTokenRemoved(const std::string& account_id,
96 const OAuth2TokenService::ScopeSet& scopes)
99 void OnRefreshTokenReceived(std::string status);
100 void OnAuthenticationResultReceived(std::string status);
103 // Encapsulates diagnostic information about tokens for different services.
105 TokenInfo(const std::string& consumer_id,
106 const OAuth2TokenService::ScopeSet& scopes);
108 base::DictionaryValue* ToValue() const;
110 static bool LessThan(const TokenInfo* a, const TokenInfo* b);
112 // Called when the token is invalidated.
115 std::string consumer_id; // service that requested the token.
116 OAuth2TokenService::ScopeSet scopes; // Scoped that are requested.
117 base::Time request_time;
118 base::Time receive_time;
119 base::Time expiration_time;
120 GoogleServiceAuthError error;
124 // Map account id to tokens associated to the account.
125 typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
127 // Encapsulates both authentication and token related information. Used
128 // by SigninInternals to maintain information that needs to be shown in
129 // the about:signin-internals page.
130 struct SigninStatus {
131 std::vector<std::string> untimed_signin_fields;
132 std::vector<TimedSigninStatusValue> timed_signin_fields;
133 TokenInfoMap token_info_map;
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:
144 // [ {"title": "Basic Information",
145 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
147 // { "title": "Detailed Information",
148 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
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]
159 scoped_ptr<base::DictionaryValue> ToValue(std::string product_version);
162 void NotifyObservers();
164 // Weak pointer to the token service.
165 ProfileOAuth2TokenService* token_service_;
167 // Weak pointer to the signin manager.
168 SigninManagerBase* signin_manager_;
170 // Weak pointer to the client.
171 SigninClient* client_;
173 // Encapsulates the actual signin and token related values.
174 // Most of the values are mirrored in the prefs for persistence.
175 SigninStatus signin_status_;
177 ObserverList<Observer> signin_observers_;
179 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
182 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_