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 weak_ptr_factory_(this) {
19 SigninAccountIdHelper::SetDisableForTest(true);
22 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
23 SigninAccountIdHelper::SetDisableForTest(false);
26 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
27 const std::string
& account_id
) const {
28 return !GetRefreshToken(account_id
).empty();
31 void FakeProfileOAuth2TokenService::LoadCredentials(
32 const std::string
& primary_account_id
) {
33 // Empty implementation as FakeProfileOAuth2TokenService does not have any
34 // credentials to load.
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
);
46 void FakeProfileOAuth2TokenService::UpdateCredentials(
47 const std::string
& account_id
,
48 const std::string
& refresh_token
) {
49 IssueRefreshTokenForUser(account_id
, refresh_token
);
52 void FakeProfileOAuth2TokenService::IssueRefreshToken(
53 const std::string
& token
) {
54 IssueRefreshTokenForUser("account_id", token
);
57 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
58 const std::string
& account_id
,
59 const std::string
& token
) {
60 ScopedBatchChange
batch(this);
62 refresh_tokens_
.erase(account_id
);
63 FireRefreshTokenRevoked(account_id
);
65 refresh_tokens_
[account_id
] = token
;
66 FireRefreshTokenAvailable(account_id
);
67 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
71 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() {
72 FireRefreshTokensLoaded();
75 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
76 const std::string
& account_id
,
77 const std::string
& access_token
,
78 const base::Time
& expiration
) {
79 CompleteRequests(account_id
,
82 GoogleServiceAuthError::AuthErrorNone(),
87 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
88 const std::string
& account_id
,
89 const GoogleServiceAuthError
& error
) {
90 CompleteRequests(account_id
,
98 void FakeProfileOAuth2TokenService::IssueTokenForScope(
99 const ScopeSet
& scope
,
100 const std::string
& access_token
,
101 const base::Time
& expiration
) {
105 GoogleServiceAuthError::AuthErrorNone(),
110 void FakeProfileOAuth2TokenService::IssueErrorForScope(
111 const ScopeSet
& scope
,
112 const GoogleServiceAuthError
& error
) {
113 CompleteRequests("", false, scope
, error
, std::string(), base::Time());
116 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
117 const GoogleServiceAuthError
& error
) {
118 CompleteRequests("", true, ScopeSet(), error
, std::string(), base::Time());
121 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
122 const std::string
& access_token
,
123 const base::Time
& expiration
) {
127 GoogleServiceAuthError::AuthErrorNone(),
132 void FakeProfileOAuth2TokenService::CompleteRequests(
133 const std::string
& account_id
,
135 const ScopeSet
& scope
,
136 const GoogleServiceAuthError
& error
,
137 const std::string
& access_token
,
138 const base::Time
& expiration
) {
139 std::vector
<FakeProfileOAuth2TokenService::PendingRequest
> requests
=
140 GetPendingRequests();
142 // Walk the requests and notify the callbacks.
143 for (std::vector
<PendingRequest
>::iterator it
= requests
.begin();
144 it
!= requests
.end(); ++it
) {
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
);
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())
160 return std::string();
163 net::URLRequestContextGetter
*
164 FakeProfileOAuth2TokenService::GetRequestContext() {
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
) {
174 valid_requests
.push_back(*it
);
176 return valid_requests
;
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
);
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(),
204 OAuth2AccessTokenFetcher
*
205 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
206 const std::string
& account_id
,
207 net::URLRequestContextGetter
* getter
,
208 OAuth2AccessTokenConsumer
* consumer
) {
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.