| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/signin/fake_profile_oauth2_token_service.h" | 5 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/signin/fake_profile_oauth2_token_service_delegate.h" |
| 9 | 10 |
| 10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { | 11 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { |
| 11 } | 12 } |
| 12 | 13 |
| 13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { | 14 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { |
| 14 } | 15 } |
| 15 | 16 |
| 16 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() | 17 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() |
| 17 : auto_post_fetch_response_on_message_loop_(false), | 18 : ProfileOAuth2TokenService(new FakeProfileOAuth2TokenServiceDelegate()), |
| 19 auto_post_fetch_response_on_message_loop_(false), |
| 18 weak_ptr_factory_(this) { | 20 weak_ptr_factory_(this) { |
| 19 } | 21 } |
| 20 | 22 |
| 21 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { | 23 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { |
| 22 } | 24 } |
| 23 | 25 |
| 24 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( | |
| 25 const std::string& account_id) const { | |
| 26 return !GetRefreshToken(account_id).empty(); | |
| 27 } | |
| 28 | |
| 29 void FakeProfileOAuth2TokenService::LoadCredentials( | |
| 30 const std::string& primary_account_id) { | |
| 31 // Empty implementation as FakeProfileOAuth2TokenService does not have any | |
| 32 // credentials to load. | |
| 33 } | |
| 34 | |
| 35 std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() { | |
| 36 std::vector<std::string> account_ids; | |
| 37 for (std::map<std::string, std::string>::const_iterator iter = | |
| 38 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { | |
| 39 account_ids.push_back(iter->first); | |
| 40 } | |
| 41 return account_ids; | |
| 42 } | |
| 43 | |
| 44 void FakeProfileOAuth2TokenService::UpdateCredentials( | |
| 45 const std::string& account_id, | |
| 46 const std::string& refresh_token) { | |
| 47 IssueRefreshTokenForUser(account_id, refresh_token); | |
| 48 } | |
| 49 | |
| 50 void FakeProfileOAuth2TokenService::IssueRefreshToken( | 26 void FakeProfileOAuth2TokenService::IssueRefreshToken( |
| 51 const std::string& token) { | 27 const std::string& token) { |
| 52 IssueRefreshTokenForUser("account_id", token); | 28 static_cast<FakeProfileOAuth2TokenServiceDelegate*>(GetDelegate()) |
| 29 ->IssueRefreshToken(token); |
| 53 } | 30 } |
| 54 | 31 |
| 55 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( | 32 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( |
| 56 const std::string& account_id, | 33 const std::string& account_id, |
| 57 const std::string& token) { | 34 const std::string& token) { |
| 58 ScopedBatchChange batch(this); | 35 static_cast<FakeProfileOAuth2TokenServiceDelegate*>(GetDelegate()) |
| 59 if (token.empty()) { | 36 ->IssueRefreshTokenForUser(account_id, token); |
| 60 refresh_tokens_.erase(account_id); | |
| 61 FireRefreshTokenRevoked(account_id); | |
| 62 } else { | |
| 63 refresh_tokens_[account_id] = token; | |
| 64 FireRefreshTokenAvailable(account_id); | |
| 65 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? | |
| 66 } | |
| 67 } | 37 } |
| 68 | 38 |
| 69 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() { | 39 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() { |
| 70 FireRefreshTokensLoaded(); | 40 static_cast<FakeProfileOAuth2TokenServiceDelegate*>(GetDelegate()) |
| 41 ->IssueAllRefreshTokensLoaded(); |
| 71 } | 42 } |
| 72 | 43 |
| 73 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( | 44 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( |
| 74 const std::string& account_id, | 45 const std::string& account_id, |
| 75 const std::string& access_token, | 46 const std::string& access_token, |
| 76 const base::Time& expiration) { | 47 const base::Time& expiration) { |
| 77 CompleteRequests(account_id, | 48 CompleteRequests(account_id, |
| 78 true, | 49 true, |
| 79 ScopeSet(), | 50 ScopeSet(), |
| 80 GoogleServiceAuthError::AuthErrorNone(), | 51 GoogleServiceAuthError::AuthErrorNone(), |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 it != requests.end(); ++it) { | 113 it != requests.end(); ++it) { |
| 143 DCHECK(it->request); | 114 DCHECK(it->request); |
| 144 | 115 |
| 145 bool scope_matches = all_scopes || it->scopes == scope; | 116 bool scope_matches = all_scopes || it->scopes == scope; |
| 146 bool account_matches = account_id.empty() || account_id == it->account_id; | 117 bool account_matches = account_id.empty() || account_id == it->account_id; |
| 147 if (account_matches && scope_matches) | 118 if (account_matches && scope_matches) |
| 148 it->request->InformConsumer(error, access_token, expiration); | 119 it->request->InformConsumer(error, access_token, expiration); |
| 149 } | 120 } |
| 150 } | 121 } |
| 151 | 122 |
| 152 std::string FakeProfileOAuth2TokenService::GetRefreshToken( | |
| 153 const std::string& account_id) const { | |
| 154 std::map<std::string, std::string>::const_iterator it = | |
| 155 refresh_tokens_.find(account_id); | |
| 156 if (it != refresh_tokens_.end()) | |
| 157 return it->second; | |
| 158 return std::string(); | |
| 159 } | |
| 160 | |
| 161 net::URLRequestContextGetter* | |
| 162 FakeProfileOAuth2TokenService::GetRequestContext() { | |
| 163 return NULL; | |
| 164 } | |
| 165 | |
| 166 std::vector<FakeProfileOAuth2TokenService::PendingRequest> | 123 std::vector<FakeProfileOAuth2TokenService::PendingRequest> |
| 167 FakeProfileOAuth2TokenService::GetPendingRequests() { | 124 FakeProfileOAuth2TokenService::GetPendingRequests() { |
| 168 std::vector<PendingRequest> valid_requests; | 125 std::vector<PendingRequest> valid_requests; |
| 169 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); | 126 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); |
| 170 it != pending_requests_.end(); ++it) { | 127 it != pending_requests_.end(); ++it) { |
| 171 if (it->request) | 128 if (it->request) |
| 172 valid_requests.push_back(*it); | 129 valid_requests.push_back(*it); |
| 173 } | 130 } |
| 174 return valid_requests; | 131 return valid_requests; |
| 175 } | 132 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 192 if (auto_post_fetch_response_on_message_loop_) { | 149 if (auto_post_fetch_response_on_message_loop_) { |
| 193 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 150 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 194 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, | 151 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, |
| 195 weak_ptr_factory_.GetWeakPtr(), | 152 weak_ptr_factory_.GetWeakPtr(), |
| 196 account_id, | 153 account_id, |
| 197 "access_token", | 154 "access_token", |
| 198 base::Time::Max())); | 155 base::Time::Max())); |
| 199 } | 156 } |
| 200 } | 157 } |
| 201 | 158 |
| 202 OAuth2AccessTokenFetcher* | |
| 203 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher( | |
| 204 const std::string& account_id, | |
| 205 net::URLRequestContextGetter* getter, | |
| 206 OAuth2AccessTokenConsumer* consumer) { | |
| 207 NOTREACHED(); | |
| 208 return NULL; | |
| 209 } | |
| 210 | |
| 211 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( | 159 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( |
| 212 const std::string& account_id, | 160 const std::string& account_id, |
| 213 const std::string& client_id, | 161 const std::string& client_id, |
| 214 const ScopeSet& scopes, | 162 const ScopeSet& scopes, |
| 215 const std::string& access_token) { | 163 const std::string& access_token) { |
| 216 // Do nothing, as we don't have a cache from which to remove the token. | 164 // Do nothing, as we don't have a cache from which to remove the token. |
| 217 } | 165 } |
| OLD | NEW |