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. 4 5 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_ 6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_ 7 8 #include <map> 9 #include <string> 10 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" 18 19 class ProfileOAuth2TokenService; 20 class SigninClient; 21 class SigninManagerBase; 22 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; 26 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 { 33 public: 34 class Observer { 35 public: 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; 40 }; 41 42 AboutSigninInternals(ProfileOAuth2TokenService* token_service, 43 SigninManagerBase* signin_manager); 44 virtual ~AboutSigninInternals(); 45 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); 50 51 // Pulls all signin values that have been persisted in the user prefs. 52 void RefreshSigninPrefs(); 53 54 // SigninManager::SigninDiagnosticsObserver implementation. 55 virtual void NotifySigninValueChanged( 56 const signin_internals_util::UntimedSigninStatusField& field, 57 const std::string& value) OVERRIDE; 58 59 virtual void NotifySigninValueChanged( 60 const signin_internals_util::TimedSigninStatusField& field, 61 const std::string& value) OVERRIDE; 62 63 void Initialize(SigninClient* client); 64 65 // KeyedService implementation. 66 virtual void Shutdown() OVERRIDE; 67 68 // Returns a dictionary of values in signin_status_ for use in 69 // about:signin-internals. The values are formatted as shown - 70 // 71 // { "signin_info" : 72 // [ {"title": "Basic Information", 73 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems] 74 // }, 75 // { "title": "Detailed Information", 76 // "data": [List of {"label" : "foo-field", "value" : "foo"} elems] 77 // }], 78 // "token_info" : 79 // [ List of {"name": "foo-name", "token" : "foo-token", 80 // "status": "foo_stat", "time" : "foo_time"} elems] 81 // } 82 scoped_ptr<base::DictionaryValue> GetSigninStatus(); 83 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) 97 OVERRIDE; 98 99 void OnRefreshTokenReceived(std::string status); 100 void OnAuthenticationResultReceived(std::string status); 101 102 private: 103 // Encapsulates diagnostic information about tokens for different services. 104 struct TokenInfo { 105 TokenInfo(const std::string& consumer_id, 106 const OAuth2TokenService::ScopeSet& scopes); 107 ~TokenInfo(); 108 base::DictionaryValue* ToValue() const; 109 110 static bool LessThan(const TokenInfo* a, const TokenInfo* b); 111 112 // Called when the token is invalidated. 113 void Invalidate(); 114 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; 121 bool removed_; 122 }; 123 124 // Map account id to tokens associated to the account. 125 typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap; 126 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; 134 135 SigninStatus(); 136 ~SigninStatus(); 137 138 TokenInfo* FindToken(const std::string& account_id, 139 const std::string& consumer_id, 140 const OAuth2TokenService::ScopeSet& scopes); 141 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(std::string product_version); 160 }; 161 162 void NotifyObservers(); 163 164 // Weak pointer to the token service. 165 ProfileOAuth2TokenService* token_service_; 166 167 // Weak pointer to the signin manager. 168 SigninManagerBase* signin_manager_; 169 170 // Weak pointer to the client. 171 SigninClient* client_; 172 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_; 176 177 ObserverList<Observer> signin_observers_; 178 179 DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals); 180 }; 181 182 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_ 183