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#ifndef GOOGLE_APIS_GAIA_FAKE_OAUTH2_TOKEN_SERVICE_H_
6#define GOOGLE_APIS_GAIA_FAKE_OAUTH2_TOKEN_SERVICE_H_
7
8#include <set>
9#include <string>
10
11#include "base/compiler_specific.h"
12#include "base/memory/weak_ptr.h"
13#include "google_apis/gaia/oauth2_token_service.h"
14
15namespace net {
16class URLRequestContextGetter;
17}
18
19// Do-nothing implementation of OAuth2TokenService.
20class FakeOAuth2TokenService : public OAuth2TokenService {
21 public:
22  FakeOAuth2TokenService();
23  virtual ~FakeOAuth2TokenService();
24
25  virtual std::vector<std::string> GetAccounts() OVERRIDE;
26
27  void AddAccount(const std::string& account_id);
28  void RemoveAccount(const std::string& account_id);
29
30  // Helper routines to issue tokens for pending requests or complete them with
31  // error.
32  void IssueAllTokensForAccount(const std::string& account_id,
33                                const std::string& access_token,
34                                const base::Time& expiration);
35  void IssueErrorForAllPendingRequestsForAccount(
36      const std::string& account_id,
37      const GoogleServiceAuthError& auth_error);
38
39  void set_request_context(net::URLRequestContextGetter* request_context) {
40    request_context_ = request_context;
41  }
42
43 protected:
44  // OAuth2TokenService overrides.
45  virtual void FetchOAuth2Token(RequestImpl* request,
46                                const std::string& account_id,
47                                net::URLRequestContextGetter* getter,
48                                const std::string& client_id,
49                                const std::string& client_secret,
50                                const ScopeSet& scopes) OVERRIDE;
51
52  virtual void InvalidateOAuth2Token(const std::string& account_id,
53                                     const std::string& client_id,
54                                     const ScopeSet& scopes,
55                                     const std::string& access_token) OVERRIDE;
56
57  virtual bool RefreshTokenIsAvailable(const std::string& account_id) const
58      OVERRIDE;
59
60 private:
61  struct PendingRequest {
62    PendingRequest();
63    ~PendingRequest();
64
65    std::string account_id;
66    std::string client_id;
67    std::string client_secret;
68    ScopeSet scopes;
69    base::WeakPtr<RequestImpl> request;
70  };
71
72  // OAuth2TokenService overrides.
73  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
74
75  virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
76      const std::string& account_id,
77      net::URLRequestContextGetter* getter,
78      OAuth2AccessTokenConsumer* consumer) OVERRIDE;
79
80  std::set<std::string> account_ids_;
81  std::vector<PendingRequest> pending_requests_;
82
83  net::URLRequestContextGetter* request_context_;  // weak
84
85  DISALLOW_COPY_AND_ASSIGN(FakeOAuth2TokenService);
86};
87
88#endif  // GOOGLE_APIS_GAIA_FAKE_OAUTH2_TOKEN_SERVICE_H_
89