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 #include "chrome/browser/net/gaia/token_service.h" | 5 #include "chrome/browser/net/gaia/token_service.h" |
6 | 6 |
7 void TokenService::SetClientLoginResult( | 7 #include "base/string_util.h" |
| 8 #include "chrome/common/net/gaia/gaia_authenticator2.h" |
| 9 #include "chrome/common/net/gaia/gaia_constants.h" |
| 10 #include "chrome/common/notification_service.h" |
| 11 |
| 12 void TokenService::Initialize( |
| 13 const char* const source, |
| 14 URLRequestContextGetter* getter, |
8 const GaiaAuthConsumer::ClientLoginResult& credentials) { | 15 const GaiaAuthConsumer::ClientLoginResult& credentials) { |
| 16 |
9 credentials_ = credentials; | 17 credentials_ = credentials; |
| 18 source_ = std::string(source); |
| 19 sync_token_fetcher_.reset(new GaiaAuthenticator2(this, source_, getter)); |
| 20 talk_token_fetcher_.reset(new GaiaAuthenticator2(this, source_, getter)); |
10 } | 21 } |
11 | 22 |
12 bool TokenService::HasLsid() { | 23 const bool TokenService::AreCredentialsValid() const { |
| 24 return !credentials_.lsid.empty() && !credentials_.sid.empty(); |
| 25 } |
| 26 |
| 27 const bool TokenService::HasLsid() const { |
13 return !credentials_.lsid.empty(); | 28 return !credentials_.lsid.empty(); |
14 } | 29 } |
15 | 30 |
16 const std::string& TokenService::GetLsid() { | 31 const std::string& TokenService::GetLsid() const { |
17 return credentials_.lsid; | 32 return credentials_.lsid; |
18 } | 33 } |
| 34 |
| 35 void TokenService::StartFetchingTokens() { |
| 36 DCHECK(AreCredentialsValid()); |
| 37 sync_token_fetcher_->StartIssueAuthToken(credentials_.sid, |
| 38 credentials_.lsid, |
| 39 GaiaConstants::kSyncService); |
| 40 talk_token_fetcher_->StartIssueAuthToken(credentials_.sid, |
| 41 credentials_.lsid, |
| 42 GaiaConstants::kTalkService); |
| 43 } |
| 44 |
| 45 // Services dependent on a token will check if a token is available. |
| 46 // If it isn't, they'll go to sleep until they get a token event. |
| 47 const bool TokenService::HasTokenForService(const char* const service) const { |
| 48 return token_map_.count(service); |
| 49 } |
| 50 |
| 51 const std::string& TokenService::GetTokenForService( |
| 52 const char* const service) const { |
| 53 |
| 54 if (token_map_.count(service) > 0) { |
| 55 // map[key] is not const |
| 56 return (*token_map_.find(service)).second; |
| 57 } |
| 58 return EmptyString(); |
| 59 } |
| 60 |
| 61 void TokenService::OnIssueAuthTokenSuccess(const std::string& service, |
| 62 const std::string& auth_token) { |
| 63 LOG(INFO) << "Got an authorization token for " << service; |
| 64 token_map_[service] = auth_token; |
| 65 TokenAvailableDetails details(service, auth_token); |
| 66 NotificationService::current()->Notify( |
| 67 NotificationType::TOKEN_AVAILABLE, |
| 68 Source<TokenService>(this), |
| 69 Details<const TokenAvailableDetails>(&details)); |
| 70 } |
| 71 void TokenService::OnIssueAuthTokenFailure(const std::string& service, |
| 72 const GaiaAuthError& error) { |
| 73 LOG(WARNING) << "Auth token issuing failed for service:" << service; |
| 74 TokenRequestFailedDetails details(service, error); |
| 75 NotificationService::current()->Notify( |
| 76 NotificationType::TOKEN_REQUEST_FAILED, |
| 77 Source<TokenService>(this), |
| 78 Details<const TokenRequestFailedDetails>(&details)); |
| 79 } |
OLD | NEW |