about_signin_internals.h revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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
19class ProfileOAuth2TokenService;
20class SigninClient;
21class 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.
25typedef 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.
29class 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 private:
100  // Encapsulates diagnostic information about tokens for different services.
101  struct TokenInfo {
102    TokenInfo(const std::string& consumer_id,
103              const OAuth2TokenService::ScopeSet& scopes);
104    ~TokenInfo();
105    base::DictionaryValue* ToValue() const;
106
107    static bool LessThan(const TokenInfo* a, const TokenInfo* b);
108
109    // Called when the token is invalidated.
110    void Invalidate();
111
112    std::string consumer_id;              // service that requested the token.
113    OAuth2TokenService::ScopeSet scopes;  // Scoped that are requested.
114    base::Time request_time;
115    base::Time receive_time;
116    base::Time expiration_time;
117    GoogleServiceAuthError error;
118    bool removed_;
119  };
120
121  // Map account id to tokens associated to the account.
122  typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
123
124  // Encapsulates both authentication and token related information. Used
125  // by SigninInternals to maintain information that needs to be shown in
126  // the about:signin-internals page.
127  struct SigninStatus {
128    std::vector<std::string> untimed_signin_fields;
129    std::vector<TimedSigninStatusValue> timed_signin_fields;
130    TokenInfoMap token_info_map;
131
132    SigninStatus();
133    ~SigninStatus();
134
135    TokenInfo* FindToken(const std::string& account_id,
136                         const std::string& consumer_id,
137                         const OAuth2TokenService::ScopeSet& scopes);
138
139    // Returns a dictionary with the following form:
140    // { "signin_info" :
141    //     [ {"title": "Basic Information",
142    //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
143    //       },
144    //       { "title": "Detailed Information",
145    //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
146    //       }],
147    //   "token_info" :
148    //     [ List of
149    //       { "title": account id,
150    //         "data": [List of {"service" : service name,
151    //                           "scopes" : requested scoped,
152    //                           "request_time" : request time,
153    //                           "status" : request status} elems]
154    //       }],
155    //  }
156    scoped_ptr<base::DictionaryValue> ToValue(std::string product_version);
157  };
158
159  void NotifyObservers();
160
161  // Weak pointer to the token service.
162  ProfileOAuth2TokenService* token_service_;
163
164  // Weak pointer to the signin manager.
165  SigninManagerBase* signin_manager_;
166
167  // Weak pointer to the client.
168  SigninClient* client_;
169
170  // Encapsulates the actual signin and token related values.
171  // Most of the values are mirrored in the prefs for persistence.
172  SigninStatus signin_status_;
173
174  ObserverList<Observer> signin_observers_;
175
176  DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
177};
178
179#endif  // COMPONENTS_SIGNIN_CORE_BROWSER_ABOUT_SIGNIN_INTERNALS_H_
180