Home | History | Annotate | Download | only in browser
      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 COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
      6 #define COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
      7 
      8 #include "base/memory/scoped_vector.h"
      9 #include "base/threading/thread_checker.h"
     10 #include "components/signin/core/browser/profile_oauth2_token_service.h"
     11 #include "components/webdata/common/web_data_service_base.h"
     12 #include "components/webdata/common/web_data_service_consumer.h"
     13 
     14 // A specialization of ProfileOAuth2TokenService that can can mutate its OAuth2
     15 // tokens.
     16 //
     17 // Note: This class is just a placeholder for now. Methods used to mutate
     18 // the tokens are currently being migrated from ProfileOAuth2TokenService.
     19 class MutableProfileOAuth2TokenService : public ProfileOAuth2TokenService,
     20                                          public WebDataServiceConsumer  {
     21  public:
     22   // ProfileOAuth2TokenService overrides.
     23   virtual void Shutdown() OVERRIDE;
     24   virtual std::vector<std::string> GetAccounts() OVERRIDE;
     25 
     26   // The below three methods should be called only on the thread on which this
     27   // object was created.
     28   virtual void LoadCredentials(const std::string& primary_account_id) OVERRIDE;
     29   virtual void UpdateCredentials(const std::string& account_id,
     30                                  const std::string& refresh_token) OVERRIDE;
     31   virtual void RevokeAllCredentials() OVERRIDE;
     32   virtual bool RefreshTokenIsAvailable(const std::string& account_id) const
     33       OVERRIDE;
     34 
     35   // Revokes credentials related to |account_id|.
     36   void RevokeCredentials(const std::string& account_id);
     37 
     38  protected:
     39   class AccountInfo : public SigninErrorController::AuthStatusProvider {
     40    public:
     41     AccountInfo(ProfileOAuth2TokenService* token_service,
     42                 const std::string& account_id,
     43                 const std::string& refresh_token);
     44     virtual ~AccountInfo();
     45 
     46     const std::string& refresh_token() const { return refresh_token_; }
     47     void set_refresh_token(const std::string& token) {
     48       refresh_token_ = token;
     49     }
     50 
     51     void SetLastAuthError(const GoogleServiceAuthError& error);
     52 
     53     // SigninErrorController::AuthStatusProvider implementation.
     54     virtual std::string GetAccountId() const OVERRIDE;
     55     virtual std::string GetUsername() const OVERRIDE;
     56     virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
     57 
     58    private:
     59     ProfileOAuth2TokenService* token_service_;
     60     std::string account_id_;
     61     std::string refresh_token_;
     62     GoogleServiceAuthError last_auth_error_;
     63 
     64     DISALLOW_COPY_AND_ASSIGN(AccountInfo);
     65   };
     66 
     67   // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
     68   // to information about the account.
     69   typedef std::map<std::string, linked_ptr<AccountInfo> > AccountInfoMap;
     70 
     71   friend class ProfileOAuth2TokenServiceFactory;
     72   friend class MutableProfileOAuth2TokenServiceTest;
     73 
     74   MutableProfileOAuth2TokenService();
     75   virtual ~MutableProfileOAuth2TokenService();
     76 
     77   // OAuth2TokenService implementation.
     78   virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
     79       const std::string& account_id,
     80       net::URLRequestContextGetter* getter,
     81       OAuth2AccessTokenConsumer* consumer) OVERRIDE;
     82   virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
     83 
     84   // Updates the internal cache of the result from the most-recently-completed
     85   // auth request (used for reporting errors to the user).
     86   virtual void UpdateAuthError(const std::string& account_id,
     87                                const GoogleServiceAuthError& error) OVERRIDE;
     88 
     89   virtual std::string GetRefreshToken(const std::string& account_id) const;
     90 
     91   AccountInfoMap& refresh_tokens() { return refresh_tokens_; }
     92 
     93  private:
     94   class RevokeServerRefreshToken;
     95 
     96   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
     97                            TokenServiceUpdateClearsCache);
     98   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
     99                            PersistenceDBUpgrade);
    100   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
    101                            PersistenceLoadCredentials);
    102 
    103   // WebDataServiceConsumer implementation:
    104   virtual void OnWebDataServiceRequestDone(
    105       WebDataServiceBase::Handle handle,
    106       const WDTypedResult* result) OVERRIDE;
    107 
    108   // Loads credentials into in memory stucture.
    109   void LoadAllCredentialsIntoMemory(
    110       const std::map<std::string, std::string>& db_tokens);
    111 
    112   // Persists credentials for |account_id|. Enables overriding for
    113   // testing purposes, or other cases, when accessing the DB is not desired.
    114   void PersistCredentials(const std::string& account_id,
    115                           const std::string& refresh_token);
    116 
    117   // Clears credentials persisted for |account_id|. Enables overriding for
    118   // testing purposes, or other cases, when accessing the DB is not desired.
    119   void ClearPersistedCredentials(const std::string& account_id);
    120 
    121   // Revokes the refresh token on the server.
    122   void RevokeCredentialsOnServer(const std::string& refresh_token);
    123 
    124   // Cancels any outstanding fetch for tokens from the web database.
    125   void CancelWebTokenFetch();
    126 
    127   // In memory refresh token store mapping account_id to refresh_token.
    128   AccountInfoMap refresh_tokens_;
    129 
    130   // Handle to the request reading tokens from database.
    131   WebDataServiceBase::Handle web_data_service_request_;
    132 
    133   // The primary account id of this service's profile during the loading of
    134   // credentials.  This member is empty otherwise.
    135   std::string loading_primary_account_id_;
    136 
    137   ScopedVector<RevokeServerRefreshToken> server_revokes_;
    138 
    139   // Used to verify that certain methods are called only on the thread on which
    140   // this instance was created.
    141   base::ThreadChecker thread_checker_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService);
    144 };
    145 
    146 #endif  // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
    147