OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
6 #define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | 6 #define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 | |
14 #if defined(OS_ANDROID) | |
15 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | |
16 #else | |
17 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
18 #endif | |
19 | 14 |
20 // Helper class to simplify writing unittests that depend on an instance of | 15 // Helper class to simplify writing unittests that depend on an instance of |
21 // ProfileOAuth2TokenService. | 16 // ProfileOAuth2TokenService. |
22 // | 17 // |
23 // Tests would typically do something like the following: | 18 // Tests would typically do something like the following: |
24 // | 19 // |
25 // FakeProfileOAuth2TokenService service; | 20 // FakeProfileOAuth2TokenService service; |
26 // ... | 21 // ... |
27 // service.IssueRefreshToken("token"); // Issue refresh token/notify observers | 22 // service.IssueRefreshToken("token"); // Issue refresh token/notify observers |
28 // ... | 23 // ... |
29 // // Confirm that there is at least one active request. | 24 // // Confirm that there is at least one active request. |
30 // EXPECT_GT(0U, service.GetPendingRequests().size()); | 25 // EXPECT_GT(0U, service.GetPendingRequests().size()); |
31 // ... | 26 // ... |
32 // // Make any pending token fetches for a given scope succeed. | 27 // // Make any pending token fetches for a given scope succeed. |
33 // ScopeSet scopes; | 28 // ScopeSet scopes; |
34 // scopes.insert(GaiaConstants::kYourServiceScope); | 29 // scopes.insert(GaiaConstants::kYourServiceScope); |
35 // IssueTokenForScope(scopes, "access_token", base::Time()::Max()); | 30 // IssueTokenForScope(scopes, "access_token", base::Time()::Max()); |
36 // ... | 31 // ... |
37 // // ...or make them fail... | 32 // // ...or make them fail... |
38 // IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS)); | 33 // IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS)); |
39 // | 34 // |
40 class FakeProfileOAuth2TokenService | 35 class FakeProfileOAuth2TokenService |
41 #if defined(OS_ANDROID) | |
42 : public AndroidProfileOAuth2TokenService { | |
43 #else | |
44 : public ProfileOAuth2TokenService { | 36 : public ProfileOAuth2TokenService { |
45 #endif | |
46 public: | 37 public: |
47 struct PendingRequest { | 38 struct PendingRequest { |
48 PendingRequest(); | 39 PendingRequest(); |
49 ~PendingRequest(); | 40 ~PendingRequest(); |
50 | 41 |
51 std::string account_id; | 42 std::string account_id; |
52 std::string client_id; | 43 std::string client_id; |
53 std::string client_secret; | 44 std::string client_secret; |
54 ScopeSet scopes; | 45 ScopeSet scopes; |
55 base::WeakPtr<RequestImpl> request; | 46 base::WeakPtr<RequestImpl> request; |
56 }; | 47 }; |
57 | 48 |
58 FakeProfileOAuth2TokenService(); | 49 FakeProfileOAuth2TokenService(); |
59 ~FakeProfileOAuth2TokenService() override; | 50 ~FakeProfileOAuth2TokenService() override; |
60 | 51 |
61 // Overriden to make sure it works on Android. | |
62 bool RefreshTokenIsAvailable(const std::string& account_id) const override; | |
63 | |
64 // Overriden to make sure it works on iOS. | |
65 void LoadCredentials(const std::string& primary_account_id) override; | |
66 | |
67 std::vector<std::string> GetAccounts() override; | |
68 | |
69 // Overriden to make sure it works on Android. Simply calls | |
70 // IssueRefreshToken(). | |
71 void UpdateCredentials(const std::string& account_id, | |
72 const std::string& refresh_token) override; | |
73 | |
74 // Sets the current refresh token. If |token| is non-empty, this will invoke | |
75 // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke | |
76 // OnRefreshTokenRevoked(). | |
77 void IssueRefreshToken(const std::string& token); | |
78 | |
79 // TODO(fgorski,rogerta): Merge with UpdateCredentials when this class fully | |
80 // supports multiple accounts. | |
81 void IssueRefreshTokenForUser(const std::string& account_id, | |
82 const std::string& token); | |
83 | |
84 // Fire OnRefreshTokensLoaded on all observers. | |
85 void IssueAllRefreshTokensLoaded(); | |
86 | |
87 // Gets a list of active requests (can be used by tests to validate that the | 52 // Gets a list of active requests (can be used by tests to validate that the |
88 // correct request has been issued). | 53 // correct request has been issued). |
89 std::vector<PendingRequest> GetPendingRequests(); | 54 std::vector<PendingRequest> GetPendingRequests(); |
90 | 55 |
91 // Helper routines to issue tokens for pending requests. | 56 // Helper routines to issue tokens for pending requests. |
92 void IssueAllTokensForAccount(const std::string& account_id, | 57 void IssueAllTokensForAccount(const std::string& account_id, |
93 const std::string& access_token, | 58 const std::string& access_token, |
94 const base::Time& expiration); | 59 const base::Time& expiration); |
95 | 60 |
96 void IssueErrorForAllPendingRequestsForAccount( | 61 void IssueErrorForAllPendingRequestsForAccount( |
(...skipping 18 matching lines...) Expand all Loading... |
115 | 80 |
116 protected: | 81 protected: |
117 // OAuth2TokenService overrides. | 82 // OAuth2TokenService overrides. |
118 void FetchOAuth2Token(RequestImpl* request, | 83 void FetchOAuth2Token(RequestImpl* request, |
119 const std::string& account_id, | 84 const std::string& account_id, |
120 net::URLRequestContextGetter* getter, | 85 net::URLRequestContextGetter* getter, |
121 const std::string& client_id, | 86 const std::string& client_id, |
122 const std::string& client_secret, | 87 const std::string& client_secret, |
123 const ScopeSet& scopes) override; | 88 const ScopeSet& scopes) override; |
124 | 89 |
125 OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( | 90 void InvalidateAccessTokenImpl(const std::string& account_id, |
126 const std::string& account_id, | 91 const std::string& client_id, |
127 net::URLRequestContextGetter* getter, | 92 const ScopeSet& scopes, |
128 OAuth2AccessTokenConsumer* consumer) override; | 93 const std::string& access_token) override; |
129 | |
130 void InvalidateOAuth2Token(const std::string& account_id, | |
131 const std::string& client_id, | |
132 const ScopeSet& scopes, | |
133 const std::string& access_token) override; | |
134 | |
135 net::URLRequestContextGetter* GetRequestContext() override; | |
136 | 94 |
137 private: | 95 private: |
138 // Helper function to complete pending requests - if |all_scopes| is true, | 96 // Helper function to complete pending requests - if |all_scopes| is true, |
139 // then all pending requests are completed, otherwise, only those requests | 97 // then all pending requests are completed, otherwise, only those requests |
140 // matching |scopes| are completed. If |account_id| is empty, then pending | 98 // matching |scopes| are completed. If |account_id| is empty, then pending |
141 // requests for all accounts are completed, otherwise only requests for the | 99 // requests for all accounts are completed, otherwise only requests for the |
142 // given account. | 100 // given account. |
143 void CompleteRequests(const std::string& account_id, | 101 void CompleteRequests(const std::string& account_id, |
144 bool all_scopes, | 102 bool all_scopes, |
145 const ScopeSet& scopes, | 103 const ScopeSet& scopes, |
146 const GoogleServiceAuthError& error, | 104 const GoogleServiceAuthError& error, |
147 const std::string& access_token, | 105 const std::string& access_token, |
148 const base::Time& expiration); | 106 const base::Time& expiration); |
149 | 107 |
150 std::string GetRefreshToken(const std::string& account_id) const; | |
151 | |
152 std::vector<PendingRequest> pending_requests_; | 108 std::vector<PendingRequest> pending_requests_; |
153 | 109 |
154 // Maps account ids to their refresh token strings. | |
155 std::map<std::string, std::string> refresh_tokens_; | |
156 | |
157 // If true, then this fake service will post responses to | 110 // If true, then this fake service will post responses to |
158 // |FetchOAuth2Token| on the current run loop. There is no need to call | 111 // |FetchOAuth2Token| on the current run loop. There is no need to call |
159 // |IssueTokenForScope| in this case. | 112 // |IssueTokenForScope| in this case. |
160 bool auto_post_fetch_response_on_message_loop_; | 113 bool auto_post_fetch_response_on_message_loop_; |
161 | 114 |
162 base::WeakPtrFactory<FakeProfileOAuth2TokenService> weak_ptr_factory_; | 115 base::WeakPtrFactory<FakeProfileOAuth2TokenService> weak_ptr_factory_; |
163 | 116 |
164 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService); | 117 DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService); |
165 }; | 118 }; |
166 | 119 |
167 #endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | 120 #endif // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |