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 10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { 11 } 12 13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { 14 } 15 16 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() 17 : auto_post_fetch_response_on_message_loop_(false), 18 weak_ptr_factory_(this) { 19 SigninAccountIdHelper::SetDisableForTest(true); 20 } 21 22 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { 23 SigninAccountIdHelper::SetDisableForTest(false); 24 } 25 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 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 70 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() { 71 FireRefreshTokensLoaded(); 72 } 73 74 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( 75 const std::string& account_id, 76 const std::string& access_token, 77 const base::Time& expiration) { 78 CompleteRequests(account_id, 79 true, 80 ScopeSet(), 81 GoogleServiceAuthError::AuthErrorNone(), 82 access_token, 83 expiration); 84 } 85 86 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount( 87 const std::string& account_id, 88 const GoogleServiceAuthError& error) { 89 CompleteRequests(account_id, 90 true, 91 ScopeSet(), 92 error, 93 std::string(), 94 base::Time()); 95 } 96 97 void FakeProfileOAuth2TokenService::IssueTokenForScope( 98 const ScopeSet& scope, 99 const std::string& access_token, 100 const base::Time& expiration) { 101 CompleteRequests("", 102 false, 103 scope, 104 GoogleServiceAuthError::AuthErrorNone(), 105 access_token, 106 expiration); 107 } 108 109 void FakeProfileOAuth2TokenService::IssueErrorForScope( 110 const ScopeSet& scope, 111 const GoogleServiceAuthError& error) { 112 CompleteRequests("", false, scope, error, std::string(), base::Time()); 113 } 114 115 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( 116 const GoogleServiceAuthError& error) { 117 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time()); 118 } 119 120 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( 121 const std::string& access_token, 122 const base::Time& expiration) { 123 CompleteRequests("", 124 true, 125 ScopeSet(), 126 GoogleServiceAuthError::AuthErrorNone(), 127 access_token, 128 expiration); 129 } 130 131 void FakeProfileOAuth2TokenService::CompleteRequests( 132 const std::string& account_id, 133 bool all_scopes, 134 const ScopeSet& scope, 135 const GoogleServiceAuthError& error, 136 const std::string& access_token, 137 const base::Time& expiration) { 138 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = 139 GetPendingRequests(); 140 141 // Walk the requests and notify the callbacks. 142 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 143 it != pending_requests_.end(); ++it) { 144 if (!it->request) 145 continue; 146 147 bool scope_matches = all_scopes || it->scopes == scope; 148 bool account_matches = account_id.empty() || account_id == it->account_id; 149 if (account_matches && scope_matches) 150 it->request->InformConsumer(error, access_token, expiration); 151 } 152 } 153 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> 169 FakeProfileOAuth2TokenService::GetPendingRequests() { 170 std::vector<PendingRequest> valid_requests; 171 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 172 it != pending_requests_.end(); ++it) { 173 if (it->request) 174 valid_requests.push_back(*it); 175 } 176 return valid_requests; 177 } 178 179 void FakeProfileOAuth2TokenService::FetchOAuth2Token( 180 RequestImpl* request, 181 const std::string& account_id, 182 net::URLRequestContextGetter* getter, 183 const std::string& client_id, 184 const std::string& client_secret, 185 const ScopeSet& scopes) { 186 PendingRequest pending_request; 187 pending_request.account_id = account_id; 188 pending_request.client_id = client_id; 189 pending_request.client_secret = client_secret; 190 pending_request.scopes = scopes; 191 pending_request.request = request->AsWeakPtr(); 192 pending_requests_.push_back(pending_request); 193 194 if (auto_post_fetch_response_on_message_loop_) { 195 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 196 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, 197 weak_ptr_factory_.GetWeakPtr(), 198 account_id, 199 "access_token", 200 base::Time::Max())); 201 } 202 } 203 204 OAuth2AccessTokenFetcher* 205 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher( 206 const std::string& account_id, 207 net::URLRequestContextGetter* getter, 208 OAuth2AccessTokenConsumer* consumer) { 209 NOTREACHED(); 210 return NULL; 211 } 212 213 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( 214 const std::string& account_id, 215 const std::string& client_id, 216 const ScopeSet& scopes, 217 const std::string& access_token) { 218 // Do nothing, as we don't have a cache from which to remove the token. 219 } 220