fake_profile_oauth2_token_service.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 3// found in the LICENSE file. 4 5#include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 6 7#include "base/message_loop/message_loop.h" 8#include "chrome/browser/signin/signin_account_id_helper.h" 9 10FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { 11} 12 13FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { 14} 15 16FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() 17 : auto_post_fetch_response_on_message_loop_(false) { 18 SigninAccountIdHelper::SetDisableForTest(true); 19} 20 21FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { 22 SigninAccountIdHelper::SetDisableForTest(false); 23} 24 25bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( 26 const std::string& account_id) { 27 return !GetRefreshToken(account_id).empty(); 28} 29 30void FakeProfileOAuth2TokenService::LoadCredentials( 31 const std::string& primary_account_id) { 32 // Empty implementation as FakeProfileOAuth2TokenService does not have any 33 // credentials to load. 34} 35 36std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() { 37 std::vector<std::string> account_ids; 38 for (std::map<std::string, std::string>::const_iterator iter = 39 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { 40 account_ids.push_back(iter->first); 41 } 42 return account_ids; 43} 44 45void FakeProfileOAuth2TokenService::UpdateCredentials( 46 const std::string& account_id, 47 const std::string& refresh_token) { 48 IssueRefreshTokenForUser(account_id, refresh_token); 49} 50 51void FakeProfileOAuth2TokenService::IssueRefreshToken( 52 const std::string& token) { 53 IssueRefreshTokenForUser("account_id", token); 54} 55 56void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( 57 const std::string& account_id, 58 const std::string& token) { 59 if (token.empty()) { 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} 68 69void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( 70 const std::string& account_id, 71 const std::string& access_token, 72 const base::Time& expiration) { 73 CompleteRequests(account_id, 74 true, 75 ScopeSet(), 76 GoogleServiceAuthError::AuthErrorNone(), 77 access_token, 78 expiration); 79} 80 81void FakeProfileOAuth2TokenService::IssueTokenForScope( 82 const ScopeSet& scope, 83 const std::string& access_token, 84 const base::Time& expiration) { 85 CompleteRequests("", 86 false, 87 scope, 88 GoogleServiceAuthError::AuthErrorNone(), 89 access_token, 90 expiration); 91} 92 93void FakeProfileOAuth2TokenService::IssueErrorForScope( 94 const ScopeSet& scope, 95 const GoogleServiceAuthError& error) { 96 CompleteRequests("", false, scope, error, std::string(), base::Time()); 97} 98 99void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( 100 const GoogleServiceAuthError& error) { 101 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time()); 102} 103 104void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( 105 const std::string& access_token, 106 const base::Time& expiration) { 107 CompleteRequests("", 108 true, 109 ScopeSet(), 110 GoogleServiceAuthError::AuthErrorNone(), 111 access_token, 112 expiration); 113} 114 115void FakeProfileOAuth2TokenService::CompleteRequests( 116 const std::string& account_id, 117 bool all_scopes, 118 const ScopeSet& scope, 119 const GoogleServiceAuthError& error, 120 const std::string& access_token, 121 const base::Time& expiration) { 122 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = 123 GetPendingRequests(); 124 125 // Walk the requests and notify the callbacks. 126 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 127 it != pending_requests_.end(); ++it) { 128 if (!it->request) 129 continue; 130 131 bool scope_matches = all_scopes || it->scopes == scope; 132 bool account_matches = account_id.empty() || account_id == it->account_id; 133 if (account_matches && scope_matches) 134 it->request->InformConsumer(error, access_token, expiration); 135 } 136} 137 138std::string FakeProfileOAuth2TokenService::GetRefreshToken( 139 const std::string& account_id) { 140 return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] : 141 std::string(); 142} 143 144net::URLRequestContextGetter* 145FakeProfileOAuth2TokenService::GetRequestContext() { 146 return NULL; 147} 148 149std::vector<FakeProfileOAuth2TokenService::PendingRequest> 150FakeProfileOAuth2TokenService::GetPendingRequests() { 151 std::vector<PendingRequest> valid_requests; 152 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 153 it != pending_requests_.end(); ++it) { 154 if (it->request) 155 valid_requests.push_back(*it); 156 } 157 return valid_requests; 158} 159 160void FakeProfileOAuth2TokenService::FetchOAuth2Token( 161 RequestImpl* request, 162 const std::string& account_id, 163 net::URLRequestContextGetter* getter, 164 const std::string& client_id, 165 const std::string& client_secret, 166 const ScopeSet& scopes) { 167 PendingRequest pending_request; 168 pending_request.account_id = account_id; 169 pending_request.client_id = client_id; 170 pending_request.client_secret = client_secret; 171 pending_request.scopes = scopes; 172 pending_request.request = request->AsWeakPtr(); 173 pending_requests_.push_back(pending_request); 174 175 if (auto_post_fetch_response_on_message_loop_) { 176 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 177 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, 178 base::Unretained(this), 179 account_id, 180 "access_token", 181 base::Time::Max())); 182 } 183} 184 185void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( 186 const std::string& account_id, 187 const std::string& client_id, 188 const ScopeSet& scopes, 189 const std::string& access_token) { 190 // Do nothing, as we don't have a cache from which to remove the token. 191} 192