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