fake_profile_oauth2_token_service.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#ifndef CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#define CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include <string>
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include <vector>
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/compiler_specific.h"
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/memory/weak_ptr.h"
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_ANDROID)
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/signin/android_profile_oauth2_token_service.h"
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service.h"
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Helper class to simplify writing unittests that depend on an instance of
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ProfileOAuth2TokenService.
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Tests would typically do something like the following:
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// FakeProfileOAuth2TokenService service;
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ...
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// service.IssueRefreshToken("token");  // Issue refresh token/notify observers
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ...
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// // Confirm that there is at least one active request.
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// EXPECT_GT(0U, service.GetPendingRequests().size());
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ...
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// // Make any pending token fetches for a given scope succeed.
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ScopeSet scopes;
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// scopes.insert(GaiaConstants::kYourServiceScope);
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// IssueTokenForScope(scopes, "access_token", base::Time()::Max());
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ...
373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// // ...or make them fail...
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// IssueErrorForScope(scopes, GoogleServiceAuthError(INVALID_GAIA_CREDENTIALS));
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class FakeProfileOAuth2TokenService
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_ANDROID)
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  : public AndroidProfileOAuth2TokenService {
4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  : public ProfileOAuth2TokenService {
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public:
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  struct PendingRequest {
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    PendingRequest();
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ~PendingRequest();
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    std::string account_id;
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    std::string client_id;
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    std::string client_secret;
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ScopeSet scopes;
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::WeakPtr<RequestImpl> request;
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  };
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  FakeProfileOAuth2TokenService();
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual ~FakeProfileOAuth2TokenService();
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Overriden to make sure it works on Android.
6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  virtual bool RefreshTokenIsAvailable(
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string& account_id) OVERRIDE;
6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Overriden to make sure it works on iOS.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void LoadCredentials(const std::string& primary_account_id) OVERRIDE;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual std::vector<std::string> GetAccounts() OVERRIDE;
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Overriden to make sure it works on Android.  Simply calls
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // IssueRefreshToken().
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UpdateCredentials(const std::string& account_id,
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                 const std::string& refresh_token) OVERRIDE;
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Sets the current refresh token. If |token| is non-empty, this will invoke
763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // OnRefreshTokenAvailable() on all Observers, otherwise this will invoke
773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // OnRefreshTokenRevoked().
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void IssueRefreshToken(const std::string& token);
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // TODO(fgorski,rogerta): Merge with UpdateCredentials when this class fully
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // supports multiple accounts.
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void IssueRefreshTokenForUser(const std::string& account_id,
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                const std::string& token);
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Gets a list of active requests (can be used by tests to validate that the
863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // correct request has been issued).
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::vector<PendingRequest> GetPendingRequests();
883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Helper routines to issue tokens for pending requests.
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void IssueAllTokensForAccount(const std::string& account_id,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                const std::string& access_token,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                const base::Time& expiration);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void IssueTokenForScope(const ScopeSet& scopes,
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          const std::string& access_token,
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          const base::Time& expiration);
973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void IssueErrorForScope(const ScopeSet& scopes,
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          const GoogleServiceAuthError& error);
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void IssueTokenForAllPendingRequests(const std::string& access_token,
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                       const base::Time& expiration);
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error);
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void set_auto_post_fetch_response_on_message_loop(bool auto_post_response) {
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    auto_post_fetch_response_on_message_loop_ = auto_post_response;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) protected:
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // OAuth2TokenService overrides.
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void FetchOAuth2Token(RequestImpl* request,
11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                const std::string& account_id,
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                net::URLRequestContextGetter* getter,
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                const std::string& client_id,
1163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                const std::string& client_secret,
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                const ScopeSet& scopes) OVERRIDE;
1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void InvalidateOAuth2Token(const std::string& account_id,
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                     const std::string& client_id,
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                     const ScopeSet& scopes,
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                     const std::string& access_token) OVERRIDE;
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
12468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE;
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private:
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Helper function to complete pending requests - if |all_scopes| is true,
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // then all pending requests are completed, otherwise, only those requests
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // matching |scopes| are completed.  If |account_id| is empty, then pending
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // requests for all accounts are completed, otherwise only requests for the
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // given account.
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CompleteRequests(const std::string& account_id,
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        bool all_scopes,
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        const ScopeSet& scopes,
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        const GoogleServiceAuthError& error,
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        const std::string& access_token,
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                        const base::Time& expiration);
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::vector<PendingRequest> pending_requests_;
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Maps account ids to their refresh token strings.
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<std::string, std::string> refresh_tokens_;
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If true, then this fake service will post responses to
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |FetchOAuth2Token| on the current run loop. There is no need to call
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |IssueTokenForScope| in this case.
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool auto_post_fetch_response_on_message_loop_;
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenService);
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)};
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif  // CHROME_BROWSER_SIGNIN_FAKE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
155