Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: chrome/browser/signin/mutable_profile_oauth2_token_service_delegate.h

Issue 1143323005: Refactor AO2TS to make it easier to componentize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update for google_apis_unittests Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
29 // object was created.
30 void LoadCredentials(const std::string& primary_account_id) override; 41 void LoadCredentials(const std::string& primary_account_id) override;
31 void UpdateCredentials(const std::string& account_id, 42 void UpdateCredentials(const std::string& account_id,
32 const std::string& refresh_token) override; 43 const std::string& refresh_token) override;
33 void RevokeAllCredentials() override; 44 void RevokeAllCredentials() override;
34 bool RefreshTokenIsAvailable(const std::string& account_id) const override;
35 45
36 // Revokes credentials related to |account_id|. 46 // Revokes credentials related to |account_id|.
37 void RevokeCredentials(const std::string& account_id); 47 void RevokeCredentials(const std::string& account_id) override;
38 48
39 protected: 49 void Shutdown() override;
50
51 private:
52 friend class MutableProfileOAuth2TokenServiceDelegateTest;
53
54 class RevokeServerRefreshToken;
55
40 class AccountInfo : public SigninErrorController::AuthStatusProvider { 56 class AccountInfo : public SigninErrorController::AuthStatusProvider {
41 public: 57 public:
42 AccountInfo(SigninErrorController* signin_error_controller, 58 AccountInfo(SigninErrorController* signin_error_controller,
43 const std::string& account_id, 59 const std::string& account_id,
44 const std::string& refresh_token); 60 const std::string& refresh_token);
45 ~AccountInfo() override; 61 ~AccountInfo() override;
46 62
47 const std::string& refresh_token() const { return refresh_token_; } 63 const std::string& refresh_token() const { return refresh_token_; }
48 void set_refresh_token(const std::string& token) { 64 void set_refresh_token(const std::string& token) { refresh_token_ = token; }
49 refresh_token_ = token;
50 }
51 65
52 void SetLastAuthError(const GoogleServiceAuthError& error); 66 void SetLastAuthError(const GoogleServiceAuthError& error);
53 67
54 // SigninErrorController::AuthStatusProvider implementation. 68 // SigninErrorController::AuthStatusProvider implementation.
55 std::string GetAccountId() const override; 69 std::string GetAccountId() const override;
56 GoogleServiceAuthError GetAuthStatus() const override; 70 GoogleServiceAuthError GetAuthStatus() const override;
57 71
58 private: 72 private:
59 SigninErrorController* signin_error_controller_; 73 SigninErrorController* signin_error_controller_;
60 std::string account_id_; 74 std::string account_id_;
61 std::string refresh_token_; 75 std::string refresh_token_;
62 GoogleServiceAuthError last_auth_error_; 76 GoogleServiceAuthError last_auth_error_;
63 77
64 DISALLOW_COPY_AND_ASSIGN(AccountInfo); 78 DISALLOW_COPY_AND_ASSIGN(AccountInfo);
65 }; 79 };
66 80
67 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService 81 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); 82 PersistenceDBUpgrade);
102 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 83 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
84 FetchPersistentError);
85 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
103 PersistenceLoadCredentials); 86 PersistenceLoadCredentials);
104 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 87 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
88 GetAccounts);
89 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
90 RetryBackoff);
91 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
105 CanonicalizeAccountId); 92 CanonicalizeAccountId);
106 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest, 93 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest,
107 FetchPersistentError); 94 ShutdownService);
108 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
109 RetryBackoff);
110 95
111 // WebDataServiceConsumer implementation: 96 // WebDataServiceConsumer implementation:
112 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle, 97 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle,
113 const WDTypedResult* result) override; 98 const WDTypedResult* result) override;
114 99
115 // Loads credentials into in memory stucture. 100 // Loads credentials into in memory stucture.
116 void LoadAllCredentialsIntoMemory( 101 void LoadAllCredentialsIntoMemory(
117 const std::map<std::string, std::string>& db_tokens); 102 const std::map<std::string, std::string>& db_tokens);
118 103
119 // Persists credentials for |account_id|. Enables overriding for 104 // Persists credentials for |account_id|. Enables overriding for
120 // testing purposes, or other cases, when accessing the DB is not desired. 105 // testing purposes, or other cases, when accessing the DB is not desired.
121 void PersistCredentials(const std::string& account_id, 106 void PersistCredentials(const std::string& account_id,
122 const std::string& refresh_token); 107 const std::string& refresh_token);
123 108
124 // Clears credentials persisted for |account_id|. Enables overriding for 109 // Clears credentials persisted for |account_id|. Enables overriding for
125 // 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.
126 void ClearPersistedCredentials(const std::string& account_id); 111 void ClearPersistedCredentials(const std::string& account_id);
127 112
128 // Revokes the refresh token on the server. 113 // Revokes the refresh token on the server.
129 void RevokeCredentialsOnServer(const std::string& refresh_token); 114 void RevokeCredentialsOnServer(const std::string& refresh_token);
130 115
131 // Cancels any outstanding fetch for tokens from the web database. 116 // Cancels any outstanding fetch for tokens from the web database.
132 void CancelWebTokenFetch(); 117 void CancelWebTokenFetch();
133 118
119 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
120 // to information about the account.
121 typedef std::map<std::string, linked_ptr<AccountInfo>> AccountInfoMap;
134 // In memory refresh token store mapping account_id to refresh_token. 122 // In memory refresh token store mapping account_id to refresh_token.
135 AccountInfoMap refresh_tokens_; 123 AccountInfoMap refresh_tokens_;
136 124
137 // Handle to the request reading tokens from database. 125 // Handle to the request reading tokens from database.
138 WebDataServiceBase::Handle web_data_service_request_; 126 WebDataServiceBase::Handle web_data_service_request_;
139 127
140 // The primary account id of this service's profile during the loading of 128 // The primary account id of this service's profile during the loading of
141 // credentials. This member is empty otherwise. 129 // credentials. This member is empty otherwise.
142 std::string loading_primary_account_id_; 130 std::string loading_primary_account_id_;
143 131
144 ScopedVector<RevokeServerRefreshToken> server_revokes_; 132 ScopedVector<RevokeServerRefreshToken> server_revokes_;
145 133
146 // Used to verify that certain methods are called only on the thread on which 134 // Used to verify that certain methods are called only on the thread on which
147 // this instance was created. 135 // this instance was created.
148 base::ThreadChecker thread_checker_; 136 base::ThreadChecker thread_checker_;
149 137
150 // Used to rate-limit network token requests so as to not overload the server. 138 // Used to rate-limit network token requests so as to not overload the server.
151 net::BackoffEntry::Policy backoff_policy_; 139 net::BackoffEntry::Policy backoff_policy_;
152 net::BackoffEntry backoff_entry_; 140 net::BackoffEntry backoff_entry_;
153 GoogleServiceAuthError backoff_error_; 141 GoogleServiceAuthError backoff_error_;
154 142
155 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService); 143 SigninClient* client_;
144 SigninErrorController* signin_error_controller_;
145
146 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenServiceDelegate);
156 }; 147 };
157 148 #endif
158 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H _
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698