| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" |
| 11 | 12 |
| 12 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { | 13 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { |
| 13 } | 14 } |
| 14 | 15 |
| 15 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { | 16 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { |
| 16 } | 17 } |
| 17 | 18 |
| 18 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() | 19 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() |
| 19 : auto_post_fetch_response_on_message_loop_(false), | 20 : ProfileOAuth2TokenService(new FakeOAuth2TokenServiceDelegate(nullptr)), |
| 21 auto_post_fetch_response_on_message_loop_(false), |
| 20 weak_ptr_factory_(this) { | 22 weak_ptr_factory_(this) { |
| 21 } | 23 } |
| 22 | 24 |
| 23 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { | 25 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { |
| 24 } | 26 } |
| 25 | 27 |
| 26 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( | |
| 27 const std::string& account_id) const { | |
| 28 return !GetRefreshToken(account_id).empty(); | |
| 29 } | |
| 30 | |
| 31 void FakeProfileOAuth2TokenService::LoadCredentials( | |
| 32 const std::string& primary_account_id) { | |
| 33 // Empty implementation as FakeProfileOAuth2TokenService does not have any | |
| 34 // credentials to load. | |
| 35 } | |
| 36 | |
| 37 std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() { | |
| 38 std::vector<std::string> account_ids; | |
| 39 for (std::map<std::string, std::string>::const_iterator iter = | |
| 40 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { | |
| 41 account_ids.push_back(iter->first); | |
| 42 } | |
| 43 return account_ids; | |
| 44 } | |
| 45 | |
| 46 void FakeProfileOAuth2TokenService::UpdateCredentials( | |
| 47 const std::string& account_id, | |
| 48 const std::string& refresh_token) { | |
| 49 IssueRefreshTokenForUser(account_id, refresh_token); | |
| 50 } | |
| 51 | |
| 52 void FakeProfileOAuth2TokenService::IssueRefreshToken( | |
| 53 const std::string& token) { | |
| 54 IssueRefreshTokenForUser("account_id", token); | |
| 55 } | |
| 56 | |
| 57 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( | |
| 58 const std::string& account_id, | |
| 59 const std::string& token) { | |
| 60 ScopedBatchChange batch(this); | |
| 61 if (token.empty()) { | |
| 62 refresh_tokens_.erase(account_id); | |
| 63 FireRefreshTokenRevoked(account_id); | |
| 64 } else { | |
| 65 refresh_tokens_[account_id] = token; | |
| 66 FireRefreshTokenAvailable(account_id); | |
| 67 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() { | |
| 72 FireRefreshTokensLoaded(); | |
| 73 } | |
| 74 | |
| 75 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( | 28 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( |
| 76 const std::string& account_id, | 29 const std::string& account_id, |
| 77 const std::string& access_token, | 30 const std::string& access_token, |
| 78 const base::Time& expiration) { | 31 const base::Time& expiration) { |
| 79 CompleteRequests(account_id, | 32 CompleteRequests(account_id, |
| 80 true, | 33 true, |
| 81 ScopeSet(), | 34 ScopeSet(), |
| 82 GoogleServiceAuthError::AuthErrorNone(), | 35 GoogleServiceAuthError::AuthErrorNone(), |
| 83 access_token, | 36 access_token, |
| 84 expiration); | 37 expiration); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 it != requests.end(); ++it) { | 97 it != requests.end(); ++it) { |
| 145 DCHECK(it->request); | 98 DCHECK(it->request); |
| 146 | 99 |
| 147 bool scope_matches = all_scopes || it->scopes == scope; | 100 bool scope_matches = all_scopes || it->scopes == scope; |
| 148 bool account_matches = account_id.empty() || account_id == it->account_id; | 101 bool account_matches = account_id.empty() || account_id == it->account_id; |
| 149 if (account_matches && scope_matches) | 102 if (account_matches && scope_matches) |
| 150 it->request->InformConsumer(error, access_token, expiration); | 103 it->request->InformConsumer(error, access_token, expiration); |
| 151 } | 104 } |
| 152 } | 105 } |
| 153 | 106 |
| 154 std::string FakeProfileOAuth2TokenService::GetRefreshToken( | |
| 155 const std::string& account_id) const { | |
| 156 std::map<std::string, std::string>::const_iterator it = | |
| 157 refresh_tokens_.find(account_id); | |
| 158 if (it != refresh_tokens_.end()) | |
| 159 return it->second; | |
| 160 return std::string(); | |
| 161 } | |
| 162 | |
| 163 net::URLRequestContextGetter* | |
| 164 FakeProfileOAuth2TokenService::GetRequestContext() { | |
| 165 return NULL; | |
| 166 } | |
| 167 | |
| 168 std::vector<FakeProfileOAuth2TokenService::PendingRequest> | 107 std::vector<FakeProfileOAuth2TokenService::PendingRequest> |
| 169 FakeProfileOAuth2TokenService::GetPendingRequests() { | 108 FakeProfileOAuth2TokenService::GetPendingRequests() { |
| 170 std::vector<PendingRequest> valid_requests; | 109 std::vector<PendingRequest> valid_requests; |
| 171 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); | 110 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); |
| 172 it != pending_requests_.end(); ++it) { | 111 it != pending_requests_.end(); ++it) { |
| 173 if (it->request) | 112 if (it->request) |
| 174 valid_requests.push_back(*it); | 113 valid_requests.push_back(*it); |
| 175 } | 114 } |
| 176 return valid_requests; | 115 return valid_requests; |
| 177 } | 116 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 193 | 132 |
| 194 if (auto_post_fetch_response_on_message_loop_) { | 133 if (auto_post_fetch_response_on_message_loop_) { |
| 195 base::ThreadTaskRunnerHandle::Get()->PostTask( | 134 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 196 FROM_HERE, | 135 FROM_HERE, |
| 197 base::Bind(&FakeProfileOAuth2TokenService::IssueAllTokensForAccount, | 136 base::Bind(&FakeProfileOAuth2TokenService::IssueAllTokensForAccount, |
| 198 weak_ptr_factory_.GetWeakPtr(), account_id, "access_token", | 137 weak_ptr_factory_.GetWeakPtr(), account_id, "access_token", |
| 199 base::Time::Max())); | 138 base::Time::Max())); |
| 200 } | 139 } |
| 201 } | 140 } |
| 202 | 141 |
| 203 OAuth2AccessTokenFetcher* | 142 void FakeProfileOAuth2TokenService::InvalidateAccessTokenImpl( |
| 204 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher( | |
| 205 const std::string& account_id, | |
| 206 net::URLRequestContextGetter* getter, | |
| 207 OAuth2AccessTokenConsumer* consumer) { | |
| 208 NOTREACHED(); | |
| 209 return NULL; | |
| 210 } | |
| 211 | |
| 212 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( | |
| 213 const std::string& account_id, | 143 const std::string& account_id, |
| 214 const std::string& client_id, | 144 const std::string& client_id, |
| 215 const ScopeSet& scopes, | 145 const ScopeSet& scopes, |
| 216 const std::string& access_token) { | 146 const std::string& access_token) { |
| 217 // Do nothing, as we don't have a cache from which to remove the token. | 147 // Do nothing, as we don't have a cache from which to remove the token. |
| 218 } | 148 } |
| OLD | NEW |