fake_profile_oauth2_token_service.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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.
4
5#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
6
7FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
8}
9
10FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
11}
12
13// static
14BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
15    content::BrowserContext* profile) {
16  FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
17  service->Initialize(reinterpret_cast<Profile*>(profile));
18  return service;
19}
20
21FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() {
22}
23
24FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
25}
26
27bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
28    const std::string& account_id) {
29  return !GetRefreshToken(account_id).empty();
30}
31
32void FakeProfileOAuth2TokenService::IssueRefreshToken(
33    const std::string& token) {
34  refresh_token_ = token;
35  if (refresh_token_.empty())
36    FireRefreshTokenRevoked("account_id");
37  else
38    FireRefreshTokenAvailable("account_id");
39  // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
40}
41
42void FakeProfileOAuth2TokenService::IssueTokenForScope(
43    const ScopeSet& scope,
44    const std::string& access_token,
45    const base::Time& expiration) {
46  CompleteRequests(false,
47                   scope,
48                   GoogleServiceAuthError::AuthErrorNone(),
49                   access_token,
50                   expiration);
51}
52
53void FakeProfileOAuth2TokenService::IssueErrorForScope(
54    const ScopeSet& scope,
55    const GoogleServiceAuthError& error) {
56  CompleteRequests(false, scope, error, std::string(), base::Time());
57}
58
59void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
60    const GoogleServiceAuthError& error) {
61  CompleteRequests(true, ScopeSet(), error, std::string(), base::Time());
62}
63
64void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
65    const std::string& access_token,
66    const base::Time& expiration) {
67  CompleteRequests(true,
68                   ScopeSet(),
69                   GoogleServiceAuthError::AuthErrorNone(),
70                   access_token,
71                   expiration);
72}
73
74void FakeProfileOAuth2TokenService::CompleteRequests(
75    bool all_scopes,
76    const ScopeSet& scope,
77    const GoogleServiceAuthError& error,
78    const std::string& access_token,
79    const base::Time& expiration) {
80  std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
81      GetPendingRequests();
82  // Walk the requests and notify the callbacks.
83  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
84       it != pending_requests_.end(); ++it) {
85    if (it->request && (all_scopes || it->scopes == scope))
86      it->request->InformConsumer(error, access_token, expiration);
87  }
88}
89
90std::string FakeProfileOAuth2TokenService::GetRefreshToken(
91    const std::string& account_id) {
92  return refresh_token_;
93}
94
95net::URLRequestContextGetter*
96FakeProfileOAuth2TokenService::GetRequestContext() {
97  return NULL;
98}
99
100std::vector<FakeProfileOAuth2TokenService::PendingRequest>
101FakeProfileOAuth2TokenService::GetPendingRequests() {
102  std::vector<PendingRequest> valid_requests;
103  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
104       it != pending_requests_.end(); ++it) {
105    if (it->request)
106      valid_requests.push_back(*it);
107  }
108  return valid_requests;
109}
110
111void FakeProfileOAuth2TokenService::FetchOAuth2Token(
112    RequestImpl* request,
113    const std::string& account_id,
114    net::URLRequestContextGetter* getter,
115    const std::string& client_id,
116    const std::string& client_secret,
117    const ScopeSet& scopes) {
118  PendingRequest pending_request;
119  pending_request.account_id = account_id;
120  pending_request.client_id = client_id;
121  pending_request.client_secret = client_secret;
122  pending_request.scopes = scopes;
123  pending_request.request = request->AsWeakPtr();
124  pending_requests_.push_back(pending_request);
125}
126