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.
5 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/signin/signin_account_id_helper.h"
10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
16 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService()
17 : auto_post_fetch_response_on_message_loop_(false) {
18 SigninAccountIdHelper::SetDisableForTest(true);
21 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
22 SigninAccountIdHelper::SetDisableForTest(false);
25 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
26 const std::string& account_id) {
27 return !GetRefreshToken(account_id).empty();
30 void FakeProfileOAuth2TokenService::LoadCredentials(
31 const std::string& primary_account_id) {
32 // Empty implementation as FakeProfileOAuth2TokenService does not have any
33 // credentials to load.
36 std::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);
45 void FakeProfileOAuth2TokenService::UpdateCredentials(
46 const std::string& account_id,
47 const std::string& refresh_token) {
48 IssueRefreshTokenForUser(account_id, refresh_token);
51 void FakeProfileOAuth2TokenService::IssueRefreshToken(
52 const std::string& token) {
53 IssueRefreshTokenForUser("account_id", token);
56 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
57 const std::string& account_id,
58 const std::string& token) {
60 refresh_tokens_.erase(account_id);
61 FireRefreshTokenRevoked(account_id);
63 refresh_tokens_[account_id] = token;
64 FireRefreshTokenAvailable(account_id);
65 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
69 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
70 const std::string& account_id,
71 const std::string& access_token,
72 const base::Time& expiration) {
73 CompleteRequests(account_id,
76 GoogleServiceAuthError::AuthErrorNone(),
81 void FakeProfileOAuth2TokenService::IssueTokenForScope(
82 const ScopeSet& scope,
83 const std::string& access_token,
84 const base::Time& expiration) {
88 GoogleServiceAuthError::AuthErrorNone(),
93 void FakeProfileOAuth2TokenService::IssueErrorForScope(
94 const ScopeSet& scope,
95 const GoogleServiceAuthError& error) {
96 CompleteRequests("", false, scope, error, std::string(), base::Time());
99 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
100 const GoogleServiceAuthError& error) {
101 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
104 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
105 const std::string& access_token,
106 const base::Time& expiration) {
110 GoogleServiceAuthError::AuthErrorNone(),
115 void FakeProfileOAuth2TokenService::CompleteRequests(
116 const std::string& account_id,
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();
125 // Walk the requests and notify the callbacks.
126 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
127 it != pending_requests_.end(); ++it) {
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);
138 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
139 const std::string& account_id) {
140 return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] :
144 net::URLRequestContextGetter*
145 FakeProfileOAuth2TokenService::GetRequestContext() {
149 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
150 FakeProfileOAuth2TokenService::GetPendingRequests() {
151 std::vector<PendingRequest> valid_requests;
152 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
153 it != pending_requests_.end(); ++it) {
155 valid_requests.push_back(*it);
157 return valid_requests;
160 void 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);
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),
185 void 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.