fake_oauth2_token_service.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#include "google_apis/gaia/fake_oauth2_token_service.h"
6
7FakeOAuth2TokenService::PendingRequest::PendingRequest() {
8}
9
10FakeOAuth2TokenService::PendingRequest::~PendingRequest() {
11}
12
13FakeOAuth2TokenService::FakeOAuth2TokenService() : request_context_(NULL) {
14}
15
16FakeOAuth2TokenService::~FakeOAuth2TokenService() {
17}
18
19std::vector<std::string> FakeOAuth2TokenService::GetAccounts() {
20  return std::vector<std::string>(account_ids_.begin(), account_ids_.end());
21}
22
23void FakeOAuth2TokenService::FetchOAuth2Token(
24    RequestImpl* request,
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);
37}
38
39void FakeOAuth2TokenService::InvalidateOAuth2Token(
40    const std::string& account_id,
41    const std::string& client_id,
42    const ScopeSet& scopes,
43    const std::string& access_token) {
44}
45
46net::URLRequestContextGetter* FakeOAuth2TokenService::GetRequestContext() {
47  return request_context_;
48}
49
50bool FakeOAuth2TokenService::RefreshTokenIsAvailable(
51    const std::string& account_id) const {
52  return account_ids_.count(account_id) != 0;
53};
54
55void FakeOAuth2TokenService::AddAccount(const std::string& account_id) {
56  account_ids_.insert(account_id);
57  FireRefreshTokenAvailable(account_id);
58}
59
60void FakeOAuth2TokenService::RemoveAccount(const std::string& account_id) {
61  account_ids_.erase(account_id);
62  FireRefreshTokenRevoked(account_id);
63}
64
65void FakeOAuth2TokenService::IssueAllTokensForAccount(
66    const std::string& account_id,
67    const std::string& access_token,
68    const base::Time& expiration) {
69
70  // Walk the requests and notify the callbacks.
71  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
72       it != pending_requests_.end(); ++it) {
73    if (it->request && (account_id == it->account_id)) {
74      it->request->InformConsumer(
75          GoogleServiceAuthError::AuthErrorNone(), access_token, expiration);
76    }
77  }
78}
79
80void FakeOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
81    const std::string& account_id,
82    const GoogleServiceAuthError& auth_error) {
83  // Walk the requests and notify the callbacks.
84  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
85       it != pending_requests_.end();
86       ++it) {
87    if (it->request && (account_id == it->account_id)) {
88      it->request->InformConsumer(auth_error, std::string(), base::Time());
89    }
90  }
91}
92
93OAuth2AccessTokenFetcher* FakeOAuth2TokenService::CreateAccessTokenFetcher(
94    const std::string& account_id,
95    net::URLRequestContextGetter* getter,
96    OAuth2AccessTokenConsumer* consumer) {
97  // |FakeOAuth2TokenService| overrides |FetchOAuth2Token| and thus
98  // |CreateAccessTokenFetcher| should never be called.
99  NOTREACHED();
100  return NULL;
101}
102