Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / fake_profile_oauth2_token_service.h
blob0acc172d46bcb68f858242383c03f4ba6453f20b
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.
5 #ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
6 #define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
14 #if defined(OS_ANDROID)
15 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
16 #else
17 #include "chrome/browser/signin/profile_oauth2_token_service.h"
18 #endif
20 namespace content {
21 class BrowserContext;
24 // Helper class to simplify writing unittests that depend on an instance of
25 // ProfileOAuth2TokenService.
27 // Tests would typically do something like the following:
29 // FakeProfileOAuth2TokenService service;
30 // ...
31 // service.IssueRefreshToken("token"); // Issue refresh token/notify observers
32 // ...
33 // // Confirm that there is at least one active request.
34 // EXPECT_GT(0U, service.GetPendingRequests().size());
35 // ...
36 // // Make any pending token fetches for a given scope succeed.
37 // ScopeSet scopes;
38 // scopes.insert(GaiaConstants::kYourServiceScope);
39 // IssueTokenForScope(scopes, "access_token", base::Time()::Max());
40 // ...
41 // // ...or make them fail...
42 // IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS));
44 class FakeProfileOAuth2TokenService
45 #if defined(OS_ANDROID)
46 : public AndroidProfileOAuth2TokenService {
47 #else
48 : public ProfileOAuth2TokenService {
49 #endif
50 public:
51 struct PendingRequest {
52 PendingRequest();
53 ~PendingRequest();
55 std::string account_id;
56 std::string client_id;
57 std::string client_secret;
58 ScopeSet scopes;
59 base::WeakPtr<RequestImpl> request;
62 FakeProfileOAuth2TokenService();
63 virtual ~FakeProfileOAuth2TokenService();
65 // Overriden to make sure it works on Android.
66 virtual bool RefreshTokenIsAvailable(
67 const std::string& account_id) OVERRIDE;
69 virtual std::vector<std::string> GetAccounts() OVERRIDE;
71 // Overriden to make sure it works on Android. Simply calls
72 // IssueRefreshToken().
73 virtual void UpdateCredentials(const std::string& account_id,
74 const std::string& refresh_token) OVERRIDE;
76 // Sets the current refresh token. If |token| is non-empty, this will invoke
77 // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke
78 // OnRefreshTokenRevoked().
79 void IssueRefreshToken(const std::string& token);
81 // TODO(fgorski,rogerta): Merge with UpdateCredentials when this class fully
82 // supports multiple accounts.
83 void IssueRefreshTokenForUser(const std::string& account_id,
84 const std::string& token);
86 // Gets a list of active requests (can be used by tests to validate that the
87 // correct request has been issued).
88 std::vector<PendingRequest> GetPendingRequests();
90 // Helper routines to issue tokens for pending requests.
91 void IssueAllTokensForAccount(const std::string& account_id,
92 const std::string& access_token,
93 const base::Time& expiration);
95 void IssueTokenForScope(const ScopeSet& scopes,
96 const std::string& access_token,
97 const base::Time& expiration);
99 void IssueErrorForScope(const ScopeSet& scopes,
100 const GoogleServiceAuthError& error);
102 void IssueTokenForAllPendingRequests(const std::string& access_token,
103 const base::Time& expiration);
105 void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error);
107 // Helper function to be used with
108 // BrowserContextKeyedService::SetTestingFactory().
109 static BrowserContextKeyedService* Build(content::BrowserContext* profile);
111 protected:
112 // OAuth2TokenService overrides.
113 virtual void FetchOAuth2Token(RequestImpl* request,
114 const std::string& account_id,
115 net::URLRequestContextGetter* getter,
116 const std::string& client_id,
117 const std::string& client_secret,
118 const ScopeSet& scopes) OVERRIDE;
120 virtual void InvalidateOAuth2Token(const std::string& account_id,
121 const std::string& client_id,
122 const ScopeSet& scopes,
123 const std::string& access_token) OVERRIDE;
125 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE;
127 private:
128 // Helper function to complete pending requests - if |all_scopes| is true,
129 // then all pending requests are completed, otherwise, only those requests
130 // matching |scopes| are completed. If |account_id| is empty, then pending
131 // requests for all accounts are completed, otherwise only requests for the
132 // given account.
133 void CompleteRequests(const std::string& account_id,
134 bool all_scopes,
135 const ScopeSet& scopes,
136 const GoogleServiceAuthError& error,
137 const std::string& access_token,
138 const base::Time& expiration);
140 std::vector<PendingRequest> pending_requests_;
142 // Maps account ids to their refresh token strings.
143 std::map<std::string, std::string> refresh_tokens_;
145 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService);
148 #endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_