Chromium Code Reviews| 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; | |
| 26 std::vector<std::string> GetAccounts() override; | 37 std::vector<std::string> GetAccounts() override; |
| 38 net::URLRequestContextGetter* GetRequestContext() const override; | |
| 27 | 39 |
| 28 // The below three methods should be called only on the thread on which this | |
| 29 // object was created. | |
| 30 void LoadCredentials(const std::string& primary_account_id) override; | 40 void LoadCredentials(const std::string& primary_account_id) override; |
| 31 void UpdateCredentials(const std::string& account_id, | 41 void UpdateCredentials(const std::string& account_id, |
| 32 const std::string& refresh_token) override; | 42 const std::string& refresh_token) override; |
| 33 void RevokeAllCredentials() override; | 43 void RevokeAllCredentials() override; |
| 34 bool RefreshTokenIsAvailable(const std::string& account_id) const override; | |
| 35 | 44 |
| 36 // Revokes credentials related to |account_id|. | 45 // Revokes credentials related to |account_id|. |
| 37 void RevokeCredentials(const std::string& account_id); | 46 void RevokeCredentials(const std::string& account_id) override; |
| 38 | 47 |
| 39 protected: | 48 void Shutdown() override; |
|
Roger Tawa OOO till Jul 10th
2015/06/04 18:19:55
Add comment: // Overridden from OAuth2TokenServic
gogerald1
2015/06/25 14:06:18
Done.
| |
| 49 | |
| 50 private: | |
| 51 friend class MutableProfileOAuth2TokenServiceDelegateTest; | |
| 52 | |
| 53 class RevokeServerRefreshToken; | |
| 54 | |
| 40 class AccountInfo : public SigninErrorController::AuthStatusProvider { | 55 class AccountInfo : public SigninErrorController::AuthStatusProvider { |
| 41 public: | 56 public: |
| 42 AccountInfo(SigninErrorController* signin_error_controller, | 57 AccountInfo(SigninErrorController* signin_error_controller, |
| 43 const std::string& account_id, | 58 const std::string& account_id, |
| 44 const std::string& refresh_token); | 59 const std::string& refresh_token); |
| 45 ~AccountInfo() override; | 60 ~AccountInfo() override; |
| 46 | 61 |
| 47 const std::string& refresh_token() const { return refresh_token_; } | 62 const std::string& refresh_token() const { return refresh_token_; } |
| 48 void set_refresh_token(const std::string& token) { | 63 void set_refresh_token(const std::string& token) { refresh_token_ = token; } |
| 49 refresh_token_ = token; | |
| 50 } | |
| 51 | 64 |
| 52 void SetLastAuthError(const GoogleServiceAuthError& error); | 65 void SetLastAuthError(const GoogleServiceAuthError& error); |
| 53 | 66 |
| 54 // SigninErrorController::AuthStatusProvider implementation. | 67 // SigninErrorController::AuthStatusProvider implementation. |
| 55 std::string GetAccountId() const override; | 68 std::string GetAccountId() const override; |
| 56 GoogleServiceAuthError GetAuthStatus() const override; | 69 GoogleServiceAuthError GetAuthStatus() const override; |
| 57 | 70 |
| 58 private: | 71 private: |
| 59 SigninErrorController* signin_error_controller_; | 72 SigninErrorController* signin_error_controller_; |
| 60 std::string account_id_; | 73 std::string account_id_; |
| 61 std::string refresh_token_; | 74 std::string refresh_token_; |
| 62 GoogleServiceAuthError last_auth_error_; | 75 GoogleServiceAuthError last_auth_error_; |
| 63 | 76 |
| 64 DISALLOW_COPY_AND_ASSIGN(AccountInfo); | 77 DISALLOW_COPY_AND_ASSIGN(AccountInfo); |
| 65 }; | 78 }; |
| 66 | 79 |
| 67 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService | 80 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); | 81 PersistenceDBUpgrade); |
| 102 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, | 82 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, |
| 83 FetchPersistentError); | |
| 84 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, | |
| 103 PersistenceLoadCredentials); | 85 PersistenceLoadCredentials); |
| 104 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, | 86 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, |
| 87 GetAccounts); | |
| 88 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, | |
| 89 RetryBackoff); | |
| 90 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, | |
| 105 CanonicalizeAccountId); | 91 CanonicalizeAccountId); |
| 106 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, | 92 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest, |
| 107 FetchPersistentError); | 93 ShutdownService); |
| 108 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, | |
| 109 RetryBackoff); | |
| 110 | 94 |
| 111 // WebDataServiceConsumer implementation: | 95 // WebDataServiceConsumer implementation: |
| 112 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle, | 96 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle, |
| 113 const WDTypedResult* result) override; | 97 const WDTypedResult* result) override; |
| 114 | 98 |
| 115 // Loads credentials into in memory stucture. | 99 // Loads credentials into in memory stucture. |
| 116 void LoadAllCredentialsIntoMemory( | 100 void LoadAllCredentialsIntoMemory( |
| 117 const std::map<std::string, std::string>& db_tokens); | 101 const std::map<std::string, std::string>& db_tokens); |
| 118 | 102 |
| 119 // Persists credentials for |account_id|. Enables overriding for | 103 // Persists credentials for |account_id|. Enables overriding for |
| 120 // testing purposes, or other cases, when accessing the DB is not desired. | 104 // testing purposes, or other cases, when accessing the DB is not desired. |
| 121 void PersistCredentials(const std::string& account_id, | 105 void PersistCredentials(const std::string& account_id, |
| 122 const std::string& refresh_token); | 106 const std::string& refresh_token); |
| 123 | 107 |
| 124 // Clears credentials persisted for |account_id|. Enables overriding for | 108 // Clears credentials persisted for |account_id|. Enables overriding for |
| 125 // testing purposes, or other cases, when accessing the DB is not desired. | 109 // testing purposes, or other cases, when accessing the DB is not desired. |
| 126 void ClearPersistedCredentials(const std::string& account_id); | 110 void ClearPersistedCredentials(const std::string& account_id); |
| 127 | 111 |
| 128 // Revokes the refresh token on the server. | 112 // Revokes the refresh token on the server. |
| 129 void RevokeCredentialsOnServer(const std::string& refresh_token); | 113 void RevokeCredentialsOnServer(const std::string& refresh_token); |
| 130 | 114 |
| 131 // Cancels any outstanding fetch for tokens from the web database. | 115 // Cancels any outstanding fetch for tokens from the web database. |
| 132 void CancelWebTokenFetch(); | 116 void CancelWebTokenFetch(); |
| 133 | 117 |
| 118 std::string GetRefreshToken(const std::string& account_id) const; | |
| 119 | |
| 120 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService | |
| 121 // to information about the account. | |
| 122 typedef std::map<std::string, linked_ptr<AccountInfo>> AccountInfoMap; | |
| 134 // In memory refresh token store mapping account_id to refresh_token. | 123 // In memory refresh token store mapping account_id to refresh_token. |
| 135 AccountInfoMap refresh_tokens_; | 124 AccountInfoMap refresh_tokens_; |
| 136 | 125 |
| 137 // Handle to the request reading tokens from database. | 126 // Handle to the request reading tokens from database. |
| 138 WebDataServiceBase::Handle web_data_service_request_; | 127 WebDataServiceBase::Handle web_data_service_request_; |
| 139 | 128 |
| 140 // The primary account id of this service's profile during the loading of | 129 // The primary account id of this service's profile during the loading of |
| 141 // credentials. This member is empty otherwise. | 130 // credentials. This member is empty otherwise. |
| 142 std::string loading_primary_account_id_; | 131 std::string loading_primary_account_id_; |
| 143 | 132 |
| 144 ScopedVector<RevokeServerRefreshToken> server_revokes_; | 133 ScopedVector<RevokeServerRefreshToken> server_revokes_; |
| 145 | 134 |
| 146 // Used to verify that certain methods are called only on the thread on which | 135 // Used to verify that certain methods are called only on the thread on which |
| 147 // this instance was created. | 136 // this instance was created. |
| 148 base::ThreadChecker thread_checker_; | 137 base::ThreadChecker thread_checker_; |
| 149 | 138 |
| 150 // Used to rate-limit network token requests so as to not overload the server. | 139 // Used to rate-limit network token requests so as to not overload the server. |
| 151 net::BackoffEntry::Policy backoff_policy_; | 140 net::BackoffEntry::Policy backoff_policy_; |
| 152 net::BackoffEntry backoff_entry_; | 141 net::BackoffEntry backoff_entry_; |
| 153 GoogleServiceAuthError backoff_error_; | 142 GoogleServiceAuthError backoff_error_; |
| 154 | 143 |
| 155 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService); | 144 SigninClient* client_; |
| 145 SigninErrorController* signin_error_controller_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenServiceDelegate); | |
| 156 }; | 148 }; |
| 157 | 149 #endif |
| 158 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H _ | |
| OLD | NEW |