fake_profile_oauth2_token_service.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "components/signin/core/browser/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 weak_ptr_factory_(this) { 19 SigninAccountIdHelper::SetDisableForTest(true); 20} 21 22FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { 23 SigninAccountIdHelper::SetDisableForTest(false); 24} 25 26bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( 27 const std::string& account_id) const { 28 return !GetRefreshToken(account_id).empty(); 29} 30 31void FakeProfileOAuth2TokenService::LoadCredentials( 32 const std::string& primary_account_id) { 33 // Empty implementation as FakeProfileOAuth2TokenService does not have any 34 // credentials to load. 35} 36 37std::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 46void FakeProfileOAuth2TokenService::UpdateCredentials( 47 const std::string& account_id, 48 const std::string& refresh_token) { 49 IssueRefreshTokenForUser(account_id, refresh_token); 50} 51 52void FakeProfileOAuth2TokenService::IssueRefreshToken( 53 const std::string& token) { 54 IssueRefreshTokenForUser("account_id", token); 55} 56 57void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( 58 const std::string& account_id, 59 const std::string& token) { 60 if (token.empty()) { 61 refresh_tokens_.erase(account_id); 62 FireRefreshTokenRevoked(account_id); 63 } else { 64 refresh_tokens_[account_id] = token; 65 FireRefreshTokenAvailable(account_id); 66 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? 67 } 68} 69 70void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( 71 const std::string& account_id, 72 const std::string& access_token, 73 const base::Time& expiration) { 74 CompleteRequests(account_id, 75 true, 76 ScopeSet(), 77 GoogleServiceAuthError::AuthErrorNone(), 78 access_token, 79 expiration); 80} 81 82void FakeProfileOAuth2TokenService::IssueTokenForScope( 83 const ScopeSet& scope, 84 const std::string& access_token, 85 const base::Time& expiration) { 86 CompleteRequests("", 87 false, 88 scope, 89 GoogleServiceAuthError::AuthErrorNone(), 90 access_token, 91 expiration); 92} 93 94void FakeProfileOAuth2TokenService::IssueErrorForScope( 95 const ScopeSet& scope, 96 const GoogleServiceAuthError& error) { 97 CompleteRequests("", false, scope, error, std::string(), base::Time()); 98} 99 100void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( 101 const GoogleServiceAuthError& error) { 102 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time()); 103} 104 105void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( 106 const std::string& access_token, 107 const base::Time& expiration) { 108 CompleteRequests("", 109 true, 110 ScopeSet(), 111 GoogleServiceAuthError::AuthErrorNone(), 112 access_token, 113 expiration); 114} 115 116void FakeProfileOAuth2TokenService::CompleteRequests( 117 const std::string& account_id, 118 bool all_scopes, 119 const ScopeSet& scope, 120 const GoogleServiceAuthError& error, 121 const std::string& access_token, 122 const base::Time& expiration) { 123 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = 124 GetPendingRequests(); 125 126 // Walk the requests and notify the callbacks. 127 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 128 it != pending_requests_.end(); ++it) { 129 if (!it->request) 130 continue; 131 132 bool scope_matches = all_scopes || it->scopes == scope; 133 bool account_matches = account_id.empty() || account_id == it->account_id; 134 if (account_matches && scope_matches) 135 it->request->InformConsumer(error, access_token, expiration); 136 } 137} 138 139std::string FakeProfileOAuth2TokenService::GetRefreshToken( 140 const std::string& account_id) const { 141 std::map<std::string, std::string>::const_iterator it = 142 refresh_tokens_.find(account_id); 143 if (it != refresh_tokens_.end()) 144 return it->second; 145 return std::string(); 146} 147 148net::URLRequestContextGetter* 149FakeProfileOAuth2TokenService::GetRequestContext() { 150 return NULL; 151} 152 153std::vector<FakeProfileOAuth2TokenService::PendingRequest> 154FakeProfileOAuth2TokenService::GetPendingRequests() { 155 std::vector<PendingRequest> valid_requests; 156 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 157 it != pending_requests_.end(); ++it) { 158 if (it->request) 159 valid_requests.push_back(*it); 160 } 161 return valid_requests; 162} 163 164void FakeProfileOAuth2TokenService::FetchOAuth2Token( 165 RequestImpl* request, 166 const std::string& account_id, 167 net::URLRequestContextGetter* getter, 168 const std::string& client_id, 169 const std::string& client_secret, 170 const ScopeSet& scopes) { 171 PendingRequest pending_request; 172 pending_request.account_id = account_id; 173 pending_request.client_id = client_id; 174 pending_request.client_secret = client_secret; 175 pending_request.scopes = scopes; 176 pending_request.request = request->AsWeakPtr(); 177 pending_requests_.push_back(pending_request); 178 179 if (auto_post_fetch_response_on_message_loop_) { 180 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 181 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, 182 weak_ptr_factory_.GetWeakPtr(), 183 account_id, 184 "access_token", 185 base::Time::Max())); 186 } 187} 188 189OAuth2AccessTokenFetcher* 190FakeProfileOAuth2TokenService::CreateAccessTokenFetcher( 191 const std::string& account_id, 192 net::URLRequestContextGetter* getter, 193 OAuth2AccessTokenConsumer* consumer) { 194 NOTREACHED(); 195 return NULL; 196} 197 198void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( 199 const std::string& account_id, 200 const std::string& client_id, 201 const ScopeSet& scopes, 202 const std::string& access_token) { 203 // Do nothing, as we don't have a cache from which to remove the token. 204} 205