1 // Copyright 2015 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 "google_apis/gaia/fake_oauth2_token_service_delegate.h"
6 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
8 FakeOAuth2TokenServiceDelegate::AccountInfo::AccountInfo(
9 const std::string
& refresh_token
)
10 : refresh_token(refresh_token
),
11 error(GoogleServiceAuthError::NONE
) {}
13 FakeOAuth2TokenServiceDelegate::FakeOAuth2TokenServiceDelegate(
14 net::URLRequestContextGetter
* request_context
)
15 : request_context_(request_context
) {
18 FakeOAuth2TokenServiceDelegate::~FakeOAuth2TokenServiceDelegate() {
21 OAuth2AccessTokenFetcher
*
22 FakeOAuth2TokenServiceDelegate::CreateAccessTokenFetcher(
23 const std::string
& account_id
,
24 net::URLRequestContextGetter
* getter
,
25 OAuth2AccessTokenConsumer
* consumer
) {
26 AccountInfoMap::const_iterator it
= refresh_tokens_
.find(account_id
);
27 DCHECK(it
!= refresh_tokens_
.end());
28 return new OAuth2AccessTokenFetcherImpl(consumer
, getter
,
29 it
->second
->refresh_token
);
32 bool FakeOAuth2TokenServiceDelegate::RefreshTokenIsAvailable(
33 const std::string
& account_id
) const {
34 return !GetRefreshToken(account_id
).empty();
37 bool FakeOAuth2TokenServiceDelegate::RefreshTokenHasError(
38 const std::string
& account_id
) const {
39 auto it
= refresh_tokens_
.find(account_id
);
40 // TODO(rogerta): should we distinguish between transient and persistent?
41 return it
== refresh_tokens_
.end() ? false : IsError(it
->second
->error
);
44 std::string
FakeOAuth2TokenServiceDelegate::GetRefreshToken(
45 const std::string
& account_id
) const {
46 AccountInfoMap::const_iterator it
= refresh_tokens_
.find(account_id
);
47 if (it
!= refresh_tokens_
.end())
48 return it
->second
->refresh_token
;
52 std::vector
<std::string
> FakeOAuth2TokenServiceDelegate::GetAccounts() {
53 std::vector
<std::string
> account_ids
;
54 for (AccountInfoMap::const_iterator iter
= refresh_tokens_
.begin();
55 iter
!= refresh_tokens_
.end(); ++iter
) {
56 account_ids
.push_back(iter
->first
);
61 void FakeOAuth2TokenServiceDelegate::RevokeAllCredentials() {
62 std::vector
<std::string
> account_ids
= GetAccounts();
63 for (std::vector
<std::string
>::const_iterator it
= account_ids
.begin();
64 it
!= account_ids
.end(); it
++) {
65 RevokeCredentials(*it
);
69 void FakeOAuth2TokenServiceDelegate::LoadCredentials(
70 const std::string
& primary_account_id
) {
71 FireRefreshTokensLoaded();
74 void FakeOAuth2TokenServiceDelegate::UpdateCredentials(
75 const std::string
& account_id
,
76 const std::string
& refresh_token
) {
77 IssueRefreshTokenForUser(account_id
, refresh_token
);
80 void FakeOAuth2TokenServiceDelegate::IssueRefreshTokenForUser(
81 const std::string
& account_id
,
82 const std::string
& token
) {
83 ScopedBatchChange
batch(this);
85 refresh_tokens_
.erase(account_id
);
86 FireRefreshTokenRevoked(account_id
);
88 refresh_tokens_
[account_id
].reset(new AccountInfo(token
));
89 FireRefreshTokenAvailable(account_id
);
90 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
94 void FakeOAuth2TokenServiceDelegate::RevokeCredentials(
95 const std::string
& account_id
) {
96 IssueRefreshTokenForUser(account_id
, std::string());
99 net::URLRequestContextGetter
*
100 FakeOAuth2TokenServiceDelegate::GetRequestContext() const {
101 return request_context_
.get();
104 void FakeOAuth2TokenServiceDelegate::SetLastErrorForAccount(
105 const std::string
& account_id
,
106 const GoogleServiceAuthError
& error
) {
107 auto it
= refresh_tokens_
.find(account_id
);
108 DCHECK(it
!= refresh_tokens_
.end());
109 it
->second
->error
= error
;