Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / about_signin_internals.h
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 CHROME_BROWSER_SIGNIN_ABOUT_SIGNIN_INTERNALS_H_
6 #define CHROME_BROWSER_SIGNIN_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 "chrome/browser/signin/signin_manager.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/signin/core/browser/signin_internals_util.h"
18 #include "google_apis/gaia/oauth2_token_service.h"
19
20 class Profile;
21
22 // Many values in SigninStatus are also associated with a timestamp.
23 // This makes it easier to keep values and their associated times together.
24 typedef std::pair<std::string, std::string> TimedSigninStatusValue;
25
26 // This class collects authentication, signin and token information
27 // to propagate to about:signin-internals via SigninInternalsUI.
28 class AboutSigninInternals
29     : public KeyedService,
30       public signin_internals_util::SigninDiagnosticsObserver,
31       public OAuth2TokenService::DiagnosticsObserver {
32  public:
33   class Observer {
34    public:
35     // |info| will contain the dictionary of signin_status_ values as indicated
36     // in the comments for GetSigninStatus() below.
37     virtual void OnSigninStateChanged(
38         scoped_ptr<base::DictionaryValue> info) = 0;
39   };
40
41   AboutSigninInternals();
42   virtual ~AboutSigninInternals();
43
44   // Each instance of SigninInternalsUI adds itself as an observer to be
45   // notified of all updates that AboutSigninInternals receives.
46   void AddSigninObserver(Observer* observer);
47   void RemoveSigninObserver(Observer* observer);
48
49   // Pulls all signin values that have been persisted in the user prefs.
50   void RefreshSigninPrefs();
51
52   // SigninManager::SigninDiagnosticsObserver implementation.
53   virtual void NotifySigninValueChanged(
54       const signin_internals_util::UntimedSigninStatusField& field,
55       const std::string& value) OVERRIDE;
56
57   virtual void NotifySigninValueChanged(
58       const signin_internals_util::TimedSigninStatusField& field,
59       const std::string& value) OVERRIDE;
60
61   void Initialize(Profile* profile);
62
63   // KeyedService implementation.
64   virtual void Shutdown() OVERRIDE;
65
66   // Returns a dictionary of values in signin_status_ for use in
67   // about:signin-internals. The values are formatted as shown -
68   //
69   // { "signin_info" :
70   //     [ {"title": "Basic Information",
71   //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
72   //       },
73   //       { "title": "Detailed Information",
74   //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
75   //       }],
76   //   "token_info" :
77   //     [ List of {"name": "foo-name", "token" : "foo-token",
78   //                 "status": "foo_stat", "time" : "foo_time"} elems]
79   //  }
80   scoped_ptr<base::DictionaryValue> GetSigninStatus();
81
82   // OAuth2TokenService::DiagnosticsObserver implementations.
83   virtual void OnAccessTokenRequested(
84       const std::string& account_id,
85       const std::string& consumer_id,
86       const OAuth2TokenService::ScopeSet& scopes) OVERRIDE;
87   virtual void OnFetchAccessTokenComplete(
88       const std::string& account_id,
89       const std::string& consumer_id,
90       const OAuth2TokenService::ScopeSet& scopes,
91       GoogleServiceAuthError error,
92       base::Time expiration_time) OVERRIDE;
93   virtual void OnTokenRemoved(
94       const std::string& account_id,
95       const OAuth2TokenService::ScopeSet& scopes) OVERRIDE;
96
97  private:
98   // Encapsulates diagnostic information about tokens for different services.
99   struct TokenInfo {
100     TokenInfo(const std::string& consumer_id,
101               const OAuth2TokenService::ScopeSet& scopes);
102     ~TokenInfo();
103     base::DictionaryValue* ToValue() const;
104
105     static bool LessThan(const TokenInfo* a, const TokenInfo* b);
106
107     // Called when the token is invalidated.
108     void Invalidate();
109
110     std::string consumer_id;              // service that requested the token.
111     OAuth2TokenService::ScopeSet scopes;  // Scoped that are requested.
112     base::Time request_time;
113     base::Time receive_time;
114     base::Time expiration_time;
115     GoogleServiceAuthError error;
116     bool removed_;
117   };
118
119   // Map account id to tokens associated to the account.
120   typedef std::map<std::string, std::vector<TokenInfo*> > TokenInfoMap;
121
122   // Encapsulates both authentication and token related information. Used
123   // by SigninInternals to maintain information that needs to be shown in
124   // the about:signin-internals page.
125   struct SigninStatus {
126     std::vector<std::string> untimed_signin_fields;
127     std::vector<TimedSigninStatusValue> timed_signin_fields;
128     TokenInfoMap token_info_map;
129
130     SigninStatus();
131     ~SigninStatus();
132
133     TokenInfo* FindToken(const std::string& account_id,
134                          const std::string& consumer_id,
135                          const OAuth2TokenService::ScopeSet& scopes);
136
137     // Returns a dictionary with the following form:
138     // { "signin_info" :
139     //     [ {"title": "Basic Information",
140     //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
141     //       },
142     //       { "title": "Detailed Information",
143     //        "data": [List of {"label" : "foo-field", "value" : "foo"} elems]
144     //       }],
145     //   "token_info" :
146     //     [ List of
147     //       { "title": account id,
148     //         "data": [List of {"service" : service name,
149     //                           "scopes" : requested scoped,
150     //                           "request_time" : request time,
151     //                           "status" : request status} elems]
152     //       }],
153     //  }
154     scoped_ptr<base::DictionaryValue> ToValue();
155   };
156
157   void NotifyObservers();
158
159   // Weak pointer to parent profile.
160   Profile* profile_;
161
162   // Encapsulates the actual signin and token related values.
163   // Most of the values are mirrored in the prefs for persistence.
164   SigninStatus signin_status_;
165
166   ObserverList<Observer> signin_observers_;
167
168   DISALLOW_COPY_AND_ASSIGN(AboutSigninInternals);
169 };
170
171 #endif  // CHROME_BROWSER_SIGNIN_ABOUT_SIGNIN_INTERNALS_H_