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"
8 #include "base/message_loop/message_loop.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) {
21 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
24 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
25 const std::string
& account_id
) const {
26 return !GetRefreshToken(account_id
).empty();
29 void FakeProfileOAuth2TokenService::LoadCredentials(
30 const std::string
& primary_account_id
) {
31 // Empty implementation as FakeProfileOAuth2TokenService does not have any
32 // credentials to load.
35 std::vector
<std::string
> FakeProfileOAuth2TokenService::GetAccounts() {
36 std::vector
<std::string
> account_ids
;
37 for (std::map
<std::string
, std::string
>::const_iterator iter
=
38 refresh_tokens_
.begin(); iter
!= refresh_tokens_
.end(); ++iter
) {
39 account_ids
.push_back(iter
->first
);
44 void FakeProfileOAuth2TokenService::UpdateCredentials(
45 const std::string
& account_id
,
46 const std::string
& refresh_token
) {
47 IssueRefreshTokenForUser(account_id
, refresh_token
);
50 void FakeProfileOAuth2TokenService::IssueRefreshToken(
51 const std::string
& token
) {
52 IssueRefreshTokenForUser("account_id", token
);
55 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
56 const std::string
& account_id
,
57 const std::string
& token
) {
58 ScopedBatchChange
batch(this);
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::IssueAllRefreshTokensLoaded() {
70 FireRefreshTokensLoaded();
73 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
74 const std::string
& account_id
,
75 const std::string
& access_token
,
76 const base::Time
& expiration
) {
77 CompleteRequests(account_id
,
80 GoogleServiceAuthError::AuthErrorNone(),
85 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
86 const std::string
& account_id
,
87 const GoogleServiceAuthError
& error
) {
88 CompleteRequests(account_id
,
96 void FakeProfileOAuth2TokenService::IssueTokenForScope(
97 const ScopeSet
& scope
,
98 const std::string
& access_token
,
99 const base::Time
& expiration
) {
103 GoogleServiceAuthError::AuthErrorNone(),
108 void FakeProfileOAuth2TokenService::IssueErrorForScope(
109 const ScopeSet
& scope
,
110 const GoogleServiceAuthError
& error
) {
111 CompleteRequests("", false, scope
, error
, std::string(), base::Time());
114 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
115 const GoogleServiceAuthError
& error
) {
116 CompleteRequests("", true, ScopeSet(), error
, std::string(), base::Time());
119 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
120 const std::string
& access_token
,
121 const base::Time
& expiration
) {
125 GoogleServiceAuthError::AuthErrorNone(),
130 void FakeProfileOAuth2TokenService::CompleteRequests(
131 const std::string
& account_id
,
133 const ScopeSet
& scope
,
134 const GoogleServiceAuthError
& error
,
135 const std::string
& access_token
,
136 const base::Time
& expiration
) {
137 std::vector
<FakeProfileOAuth2TokenService::PendingRequest
> requests
=
138 GetPendingRequests();
140 // Walk the requests and notify the callbacks.
141 for (std::vector
<PendingRequest
>::iterator it
= requests
.begin();
142 it
!= requests
.end(); ++it
) {
145 bool scope_matches
= all_scopes
|| it
->scopes
== scope
;
146 bool account_matches
= account_id
.empty() || account_id
== it
->account_id
;
147 if (account_matches
&& scope_matches
)
148 it
->request
->InformConsumer(error
, access_token
, expiration
);
152 std::string
FakeProfileOAuth2TokenService::GetRefreshToken(
153 const std::string
& account_id
) const {
154 std::map
<std::string
, std::string
>::const_iterator it
=
155 refresh_tokens_
.find(account_id
);
156 if (it
!= refresh_tokens_
.end())
158 return std::string();
161 net::URLRequestContextGetter
*
162 FakeProfileOAuth2TokenService::GetRequestContext() {
166 std::vector
<FakeProfileOAuth2TokenService::PendingRequest
>
167 FakeProfileOAuth2TokenService::GetPendingRequests() {
168 std::vector
<PendingRequest
> valid_requests
;
169 for (std::vector
<PendingRequest
>::iterator it
= pending_requests_
.begin();
170 it
!= pending_requests_
.end(); ++it
) {
172 valid_requests
.push_back(*it
);
174 return valid_requests
;
177 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
178 RequestImpl
* request
,
179 const std::string
& account_id
,
180 net::URLRequestContextGetter
* getter
,
181 const std::string
& client_id
,
182 const std::string
& client_secret
,
183 const ScopeSet
& scopes
) {
184 PendingRequest pending_request
;
185 pending_request
.account_id
= account_id
;
186 pending_request
.client_id
= client_id
;
187 pending_request
.client_secret
= client_secret
;
188 pending_request
.scopes
= scopes
;
189 pending_request
.request
= request
->AsWeakPtr();
190 pending_requests_
.push_back(pending_request
);
192 if (auto_post_fetch_response_on_message_loop_
) {
193 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(
194 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount
,
195 weak_ptr_factory_
.GetWeakPtr(),
202 OAuth2AccessTokenFetcher
*
203 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
204 const std::string
& account_id
,
205 net::URLRequestContextGetter
* getter
,
206 OAuth2AccessTokenConsumer
* consumer
) {
211 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
212 const std::string
& account_id
,
213 const std::string
& client_id
,
214 const ScopeSet
& scopes
,
215 const std::string
& access_token
) {
216 // Do nothing, as we don't have a cache from which to remove the token.