1 // Copyright 2014 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.h"
7 FakeOAuth2TokenService::PendingRequest::PendingRequest() {
10 FakeOAuth2TokenService::PendingRequest::~PendingRequest() {
13 FakeOAuth2TokenService::FakeOAuth2TokenService() : request_context_(NULL
) {
16 FakeOAuth2TokenService::~FakeOAuth2TokenService() {
19 std::vector
<std::string
> FakeOAuth2TokenService::GetAccounts() {
20 return std::vector
<std::string
>(account_ids_
.begin(), account_ids_
.end());
23 void FakeOAuth2TokenService::FetchOAuth2Token(
25 const std::string
& account_id
,
26 net::URLRequestContextGetter
* getter
,
27 const std::string
& client_id
,
28 const std::string
& client_secret
,
29 const ScopeSet
& scopes
) {
30 PendingRequest pending_request
;
31 pending_request
.account_id
= account_id
;
32 pending_request
.client_id
= client_id
;
33 pending_request
.client_secret
= client_secret
;
34 pending_request
.scopes
= scopes
;
35 pending_request
.request
= request
->AsWeakPtr();
36 pending_requests_
.push_back(pending_request
);
39 void FakeOAuth2TokenService::InvalidateOAuth2Token(
40 const std::string
& account_id
,
41 const std::string
& client_id
,
42 const ScopeSet
& scopes
,
43 const std::string
& access_token
) {
46 net::URLRequestContextGetter
* FakeOAuth2TokenService::GetRequestContext() {
47 return request_context_
;
50 bool FakeOAuth2TokenService::RefreshTokenIsAvailable(
51 const std::string
& account_id
) const {
52 return account_ids_
.count(account_id
) != 0;
55 void FakeOAuth2TokenService::AddAccount(const std::string
& account_id
) {
56 account_ids_
.insert(account_id
);
57 FireRefreshTokenAvailable(account_id
);
60 void FakeOAuth2TokenService::RemoveAccount(const std::string
& account_id
) {
61 account_ids_
.erase(account_id
);
62 FireRefreshTokenRevoked(account_id
);
65 void FakeOAuth2TokenService::IssueAllTokensForAccount(
66 const std::string
& account_id
,
67 const std::string
& access_token
,
68 const base::Time
& expiration
) {
69 // Walk the requests and notify the callbacks.
70 // Using a copy of pending requests to make sure a new token request triggered
71 // from the handling code does not invalidate the iterator.
72 std::vector
<PendingRequest
> pending_requests_copy
= pending_requests_
;
73 for (std::vector
<PendingRequest
>::iterator it
= pending_requests_copy
.begin();
74 it
!= pending_requests_copy
.end();
76 if (it
->request
&& (account_id
== it
->account_id
)) {
77 it
->request
->InformConsumer(
78 GoogleServiceAuthError::AuthErrorNone(), access_token
, expiration
);
83 void FakeOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
84 const std::string
& account_id
,
85 const GoogleServiceAuthError
& auth_error
) {
86 // Walk the requests and notify the callbacks.
87 // Using a copy of pending requests to make sure retrying a request in
88 // response to the error does not invalidate the iterator.
89 std::vector
<PendingRequest
> pending_requests_copy
= pending_requests_
;
90 for (std::vector
<PendingRequest
>::iterator it
= pending_requests_copy
.begin();
91 it
!= pending_requests_copy
.end();
93 if (it
->request
&& (account_id
== it
->account_id
)) {
94 it
->request
->InformConsumer(auth_error
, std::string(), base::Time());
99 OAuth2AccessTokenFetcher
* FakeOAuth2TokenService::CreateAccessTokenFetcher(
100 const std::string
& account_id
,
101 net::URLRequestContextGetter
* getter
,
102 OAuth2AccessTokenConsumer
* consumer
) {
103 // |FakeOAuth2TokenService| overrides |FetchOAuth2Token| and thus
104 // |CreateAccessTokenFetcher| should never be called.