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 "components/signin/core/browser/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
) const {
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
) const {
140 std::map
<std::string
, std::string
>::const_iterator it
=
141 refresh_tokens_
.find(account_id
);
142 if (it
!= refresh_tokens_
.end())
144 return std::string();
147 net::URLRequestContextGetter
*
148 FakeProfileOAuth2TokenService::GetRequestContext() {
152 std::vector
<FakeProfileOAuth2TokenService::PendingRequest
>
153 FakeProfileOAuth2TokenService::GetPendingRequests() {
154 std::vector
<PendingRequest
> valid_requests
;
155 for (std::vector
<PendingRequest
>::iterator it
= pending_requests_
.begin();
156 it
!= pending_requests_
.end(); ++it
) {
158 valid_requests
.push_back(*it
);
160 return valid_requests
;
163 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
164 RequestImpl
* request
,
165 const std::string
& account_id
,
166 net::URLRequestContextGetter
* getter
,
167 const std::string
& client_id
,
168 const std::string
& client_secret
,
169 const ScopeSet
& scopes
) {
170 PendingRequest pending_request
;
171 pending_request
.account_id
= account_id
;
172 pending_request
.client_id
= client_id
;
173 pending_request
.client_secret
= client_secret
;
174 pending_request
.scopes
= scopes
;
175 pending_request
.request
= request
->AsWeakPtr();
176 pending_requests_
.push_back(pending_request
);
178 if (auto_post_fetch_response_on_message_loop_
) {
179 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(
180 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount
,
181 base::Unretained(this),
188 OAuth2AccessTokenFetcher
*
189 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
190 const std::string
& account_id
,
191 net::URLRequestContextGetter
* getter
,
192 OAuth2AccessTokenConsumer
* consumer
) {
197 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
198 const std::string
& account_id
,
199 const std::string
& client_id
,
200 const ScopeSet
& scopes
,
201 const std::string
& access_token
) {
202 // Do nothing, as we don't have a cache from which to remove the token.