OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "google_apis/gaia/fake_oauth2_token_service_delegate.h" | 5 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" |
6 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" | 6 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" |
7 | 7 |
| 8 FakeOAuth2TokenServiceDelegate::AccountInfo::AccountInfo( |
| 9 const std::string& refresh_token) |
| 10 : refresh_token(refresh_token), |
| 11 error(GoogleServiceAuthError::NONE) {} |
| 12 |
8 FakeOAuth2TokenServiceDelegate::FakeOAuth2TokenServiceDelegate( | 13 FakeOAuth2TokenServiceDelegate::FakeOAuth2TokenServiceDelegate( |
9 net::URLRequestContextGetter* request_context) | 14 net::URLRequestContextGetter* request_context) |
10 : request_context_(request_context) { | 15 : request_context_(request_context) { |
11 } | 16 } |
12 | 17 |
13 FakeOAuth2TokenServiceDelegate::~FakeOAuth2TokenServiceDelegate() { | 18 FakeOAuth2TokenServiceDelegate::~FakeOAuth2TokenServiceDelegate() { |
14 } | 19 } |
15 | 20 |
16 OAuth2AccessTokenFetcher* | 21 OAuth2AccessTokenFetcher* |
17 FakeOAuth2TokenServiceDelegate::CreateAccessTokenFetcher( | 22 FakeOAuth2TokenServiceDelegate::CreateAccessTokenFetcher( |
18 const std::string& account_id, | 23 const std::string& account_id, |
19 net::URLRequestContextGetter* getter, | 24 net::URLRequestContextGetter* getter, |
20 OAuth2AccessTokenConsumer* consumer) { | 25 OAuth2AccessTokenConsumer* consumer) { |
21 std::map<std::string, std::string>::const_iterator it = | 26 AccountInfoMap::const_iterator it = refresh_tokens_.find(account_id); |
22 refresh_tokens_.find(account_id); | |
23 DCHECK(it != refresh_tokens_.end()); | 27 DCHECK(it != refresh_tokens_.end()); |
24 std::string refresh_token(it->second); | 28 return new OAuth2AccessTokenFetcherImpl(consumer, getter, |
25 return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token); | 29 it->second->refresh_token); |
26 } | 30 } |
27 | 31 |
28 bool FakeOAuth2TokenServiceDelegate::RefreshTokenIsAvailable( | 32 bool FakeOAuth2TokenServiceDelegate::RefreshTokenIsAvailable( |
29 const std::string& account_id) const { | 33 const std::string& account_id) const { |
30 return !GetRefreshToken(account_id).empty(); | 34 return !GetRefreshToken(account_id).empty(); |
31 } | 35 } |
32 | 36 |
| 37 bool FakeOAuth2TokenServiceDelegate::RefreshTokenHasError( |
| 38 const std::string& account_id) const { |
| 39 auto it = refresh_tokens_.find(account_id); |
| 40 // TODO(rogerta): should we distinguish between transient and persistent? |
| 41 return it == refresh_tokens_.end() ? false : IsError(it->second->error); |
| 42 } |
| 43 |
33 std::string FakeOAuth2TokenServiceDelegate::GetRefreshToken( | 44 std::string FakeOAuth2TokenServiceDelegate::GetRefreshToken( |
34 const std::string& account_id) const { | 45 const std::string& account_id) const { |
35 std::map<std::string, std::string>::const_iterator it = | 46 AccountInfoMap::const_iterator it = refresh_tokens_.find(account_id); |
36 refresh_tokens_.find(account_id); | |
37 if (it != refresh_tokens_.end()) | 47 if (it != refresh_tokens_.end()) |
38 return it->second; | 48 return it->second->refresh_token; |
39 return std::string(); | 49 return std::string(); |
40 } | 50 } |
41 | 51 |
42 std::vector<std::string> FakeOAuth2TokenServiceDelegate::GetAccounts() { | 52 std::vector<std::string> FakeOAuth2TokenServiceDelegate::GetAccounts() { |
43 std::vector<std::string> account_ids; | 53 std::vector<std::string> account_ids; |
44 for (std::map<std::string, std::string>::const_iterator iter = | 54 for (AccountInfoMap::const_iterator iter = refresh_tokens_.begin(); |
45 refresh_tokens_.begin(); | |
46 iter != refresh_tokens_.end(); ++iter) { | 55 iter != refresh_tokens_.end(); ++iter) { |
47 account_ids.push_back(iter->first); | 56 account_ids.push_back(iter->first); |
48 } | 57 } |
49 return account_ids; | 58 return account_ids; |
50 } | 59 } |
51 | 60 |
52 void FakeOAuth2TokenServiceDelegate::RevokeAllCredentials() { | 61 void FakeOAuth2TokenServiceDelegate::RevokeAllCredentials() { |
53 std::vector<std::string> account_ids = GetAccounts(); | 62 std::vector<std::string> account_ids = GetAccounts(); |
54 for (std::vector<std::string>::const_iterator it = account_ids.begin(); | 63 for (std::vector<std::string>::const_iterator it = account_ids.begin(); |
55 it != account_ids.end(); it++) { | 64 it != account_ids.end(); it++) { |
(...skipping 13 matching lines...) Expand all Loading... |
69 } | 78 } |
70 | 79 |
71 void FakeOAuth2TokenServiceDelegate::IssueRefreshTokenForUser( | 80 void FakeOAuth2TokenServiceDelegate::IssueRefreshTokenForUser( |
72 const std::string& account_id, | 81 const std::string& account_id, |
73 const std::string& token) { | 82 const std::string& token) { |
74 ScopedBatchChange batch(this); | 83 ScopedBatchChange batch(this); |
75 if (token.empty()) { | 84 if (token.empty()) { |
76 refresh_tokens_.erase(account_id); | 85 refresh_tokens_.erase(account_id); |
77 FireRefreshTokenRevoked(account_id); | 86 FireRefreshTokenRevoked(account_id); |
78 } else { | 87 } else { |
79 refresh_tokens_[account_id] = token; | 88 refresh_tokens_[account_id].reset(new AccountInfo(token)); |
80 FireRefreshTokenAvailable(account_id); | 89 FireRefreshTokenAvailable(account_id); |
81 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? | 90 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? |
82 } | 91 } |
83 } | 92 } |
84 | 93 |
85 void FakeOAuth2TokenServiceDelegate::RevokeCredentials( | 94 void FakeOAuth2TokenServiceDelegate::RevokeCredentials( |
86 const std::string& account_id) { | 95 const std::string& account_id) { |
87 IssueRefreshTokenForUser(account_id, std::string()); | 96 IssueRefreshTokenForUser(account_id, std::string()); |
88 } | 97 } |
89 | 98 |
90 net::URLRequestContextGetter* | 99 net::URLRequestContextGetter* |
91 FakeOAuth2TokenServiceDelegate::GetRequestContext() const { | 100 FakeOAuth2TokenServiceDelegate::GetRequestContext() const { |
92 return request_context_.get(); | 101 return request_context_.get(); |
93 } | 102 } |
| 103 |
| 104 void FakeOAuth2TokenServiceDelegate::SetLastErrorForAccount( |
| 105 const std::string& account_id, |
| 106 const GoogleServiceAuthError& error) { |
| 107 auto it = refresh_tokens_.find(account_id); |
| 108 DCHECK(it != refresh_tokens_.end()); |
| 109 it->second->error = error; |
| 110 } |
OLD | NEW |