OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // The TokenService will supply authentication tokens for any service that | 5 // The TokenService will supply authentication tokens for any service that |
6 // needs it, such as sync. Whenever the user logs in, a controller watching | 6 // needs it, such as sync. Whenever the user logs in, a controller watching |
7 // the token service is expected to call ClientLogin to derive a new SID and | 7 // the token service is expected to call ClientLogin to derive a new SID and |
8 // LSID. Whenever such credentials are available, the TokenService should be | 8 // LSID. Whenever such credentials are available, the TokenService should be |
9 // updated with new credentials. The controller should then start fetching | 9 // updated with new credentials. The controller should then start fetching |
10 // tokens, which will be written to the database after retrieval, as well as | 10 // tokens, which will be written to the database after retrieval, as well as |
(...skipping 19 matching lines...) Expand all Loading... |
30 // SetMyToken(notification.token()) | 30 // SetMyToken(notification.token()) |
31 // } | 31 // } |
32 // } | 32 // } |
33 | 33 |
34 #ifndef CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 34 #ifndef CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
35 #define CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 35 #define CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
36 #pragma once | 36 #pragma once |
37 | 37 |
38 #include <map> | 38 #include <map> |
39 #include <string> | 39 #include <string> |
| 40 |
| 41 #include "base/gtest_prod_util.h" |
40 #include "base/scoped_ptr.h" | 42 #include "base/scoped_ptr.h" |
41 #include "chrome/browser/webdata/web_data_service.h" | 43 #include "chrome/browser/webdata/web_data_service.h" |
42 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 44 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
43 #include "chrome/common/net/gaia/gaia_authenticator2.h" | 45 #include "chrome/common/net/gaia/gaia_authenticator2.h" |
44 #include "chrome/common/net/gaia/google_service_auth_error.h" | 46 #include "chrome/common/net/gaia/google_service_auth_error.h" |
45 #include "base/gtest_prod_util.h" | 47 #include "chrome/common/notification_observer.h" |
| 48 #include "chrome/common/notification_registrar.h" |
46 | 49 |
47 class URLRequestContextGetter; | 50 class URLRequestContextGetter; |
48 class Profile; | 51 class Profile; |
49 | 52 |
50 // The TokenService is a Profile member, so all calls are expected | 53 // The TokenService is a Profile member, so all calls are expected |
51 // from the UI thread. | 54 // from the UI thread. |
52 class TokenService : public GaiaAuthConsumer, | 55 class TokenService : public GaiaAuthConsumer, |
53 public WebDataServiceConsumer { | 56 public WebDataServiceConsumer, |
| 57 public NotificationObserver { |
54 public: | 58 public: |
55 TokenService(); | 59 TokenService(); |
56 virtual ~TokenService(); | 60 virtual ~TokenService(); |
57 | 61 |
58 // Notification classes | 62 // Notification classes |
59 class TokenAvailableDetails { | 63 class TokenAvailableDetails { |
60 public: | 64 public: |
61 TokenAvailableDetails() {} | 65 TokenAvailableDetails() {} |
62 TokenAvailableDetails(const std::string& service, | 66 TokenAvailableDetails(const std::string& service, |
63 const std::string& token) | 67 const std::string& token) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // Did we get a proper LSID? | 120 // Did we get a proper LSID? |
117 const bool AreCredentialsValid() const; | 121 const bool AreCredentialsValid() const; |
118 | 122 |
119 // Tokens will be fetched for all services(sync, talk) in the background. | 123 // Tokens will be fetched for all services(sync, talk) in the background. |
120 // Results come back via event channel. Services can also poll before events | 124 // Results come back via event channel. Services can also poll before events |
121 // are issued. | 125 // are issued. |
122 void StartFetchingTokens(); | 126 void StartFetchingTokens(); |
123 const bool HasTokenForService(const char* const service) const; | 127 const bool HasTokenForService(const char* const service) const; |
124 const std::string& GetTokenForService(const char* const service) const; | 128 const std::string& GetTokenForService(const char* const service) const; |
125 | 129 |
| 130 // For tests only. Doesn't save to the WebDB. |
| 131 void IssueAuthTokenForTest(const std::string& service, |
| 132 const std::string& auth_token); |
| 133 |
126 // GaiaAuthConsumer implementation. | 134 // GaiaAuthConsumer implementation. |
127 virtual void OnIssueAuthTokenSuccess(const std::string& service, | 135 virtual void OnIssueAuthTokenSuccess(const std::string& service, |
128 const std::string& auth_token); | 136 const std::string& auth_token); |
129 virtual void OnIssueAuthTokenFailure(const std::string& service, | 137 virtual void OnIssueAuthTokenFailure(const std::string& service, |
130 const GoogleServiceAuthError& error); | 138 const GoogleServiceAuthError& error); |
131 | 139 |
132 // WebDataServiceConsumer implementation. | 140 // WebDataServiceConsumer implementation. |
133 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, | 141 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, |
134 const WDTypedResult* result); | 142 const WDTypedResult* result); |
135 | 143 |
| 144 // NotificationObserver implementation. |
| 145 virtual void Observe(NotificationType type, |
| 146 const NotificationSource& source, |
| 147 const NotificationDetails& details); |
| 148 |
136 private: | 149 private: |
137 | 150 |
138 void FireTokenAvailableNotification(const std::string& service, | 151 void FireTokenAvailableNotification(const std::string& service, |
139 const std::string& auth_token); | 152 const std::string& auth_token); |
140 | 153 |
141 void FireTokenRequestFailedNotification(const std::string& service, | 154 void FireTokenRequestFailedNotification(const std::string& service, |
142 const GoogleServiceAuthError& error); | 155 const GoogleServiceAuthError& error); |
143 | 156 |
144 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, | 157 void LoadTokensIntoMemory(const std::map<std::string, std::string>& in_toks, |
145 std::map<std::string, std::string>* out_toks); | 158 std::map<std::string, std::string>* out_toks); |
(...skipping 16 matching lines...) Expand all Loading... |
162 // Size of array of services (must be defined here). | 175 // Size of array of services (must be defined here). |
163 static const int kNumServices = 2; | 176 static const int kNumServices = 2; |
164 // List of services that we're performing operations for. | 177 // List of services that we're performing operations for. |
165 static const char* kServices[kNumServices]; | 178 static const char* kServices[kNumServices]; |
166 // A bunch of fetchers suitable for token issuing. We don't care about | 179 // A bunch of fetchers suitable for token issuing. We don't care about |
167 // the ordering, nor do we care which is for which service. | 180 // the ordering, nor do we care which is for which service. |
168 scoped_ptr<GaiaAuthenticator2> fetchers_[kNumServices]; | 181 scoped_ptr<GaiaAuthenticator2> fetchers_[kNumServices]; |
169 // Map from service to token. | 182 // Map from service to token. |
170 std::map<std::string, std::string> token_map_; | 183 std::map<std::string, std::string> token_map_; |
171 | 184 |
| 185 NotificationRegistrar registrar_; |
| 186 |
172 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); | 187 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryBasic); |
173 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); | 188 FRIEND_TEST_ALL_PREFIXES(TokenServiceTest, LoadTokensIntoMemoryAdvanced); |
174 | 189 |
175 DISALLOW_COPY_AND_ASSIGN(TokenService); | 190 DISALLOW_COPY_AND_ASSIGN(TokenService); |
176 }; | 191 }; |
177 | 192 |
178 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ | 193 #endif // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_H_ |
OLD | NEW |