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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SIGNIN_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_DELEGATE_H_
6 #define CHROME_BROWSER_SIGNIN_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_DELEGATE_H_
8 #include "base/memory/scoped_vector.h"
9 #include "base/threading/thread_checker.h"
10 #include "components/signin/core/browser/account_tracker_service.h"
11 #include "components/signin/core/browser/profile_oauth2_token_service.h"
12 #include "components/signin/core/browser/signin_error_controller.h"
13 #include "components/webdata/common/web_data_service_base.h"
14 #include "components/webdata/common/web_data_service_consumer.h"
15 #include "net/base/backoff_entry.h"
17 class MutableProfileOAuth2TokenServiceDelegate
18 : public OAuth2TokenServiceDelegate
,
19 public WebDataServiceConsumer
{
21 MutableProfileOAuth2TokenServiceDelegate(
23 SigninErrorController
* signin_error_controller
,
24 AccountTrackerService
* account_tracker_service
);
25 ~MutableProfileOAuth2TokenServiceDelegate() override
;
27 // OAuth2TokenServiceDelegate overrides.
28 OAuth2AccessTokenFetcher
* CreateAccessTokenFetcher(
29 const std::string
& account_id
,
30 net::URLRequestContextGetter
* getter
,
31 OAuth2AccessTokenConsumer
* consumer
) override
;
33 // Updates the internal cache of the result from the most-recently-completed
34 // auth request (used for reporting errors to the user).
35 void UpdateAuthError(const std::string
& account_id
,
36 const GoogleServiceAuthError
& error
) override
;
38 bool RefreshTokenIsAvailable(const std::string
& account_id
) const override
;
39 std::vector
<std::string
> GetAccounts() override
;
40 net::URLRequestContextGetter
* GetRequestContext() const override
;
42 void LoadCredentials(const std::string
& primary_account_id
) override
;
43 void UpdateCredentials(const std::string
& account_id
,
44 const std::string
& refresh_token
) override
;
45 void RevokeAllCredentials() override
;
47 // Revokes credentials related to |account_id|.
48 void RevokeCredentials(const std::string
& account_id
) override
;
50 // Overridden from OAuth2TokenServiceDelegate.
51 void Shutdown() override
;
54 friend class MutableProfileOAuth2TokenServiceDelegateTest
;
56 class RevokeServerRefreshToken
;
58 class AccountInfo
: public SigninErrorController::AuthStatusProvider
{
60 AccountInfo(SigninErrorController
* signin_error_controller
,
61 const std::string
& account_id
,
62 const std::string
& refresh_token
);
63 ~AccountInfo() override
;
65 const std::string
& refresh_token() const { return refresh_token_
; }
66 void set_refresh_token(const std::string
& token
) { refresh_token_
= token
; }
68 void SetLastAuthError(const GoogleServiceAuthError
& error
);
70 // SigninErrorController::AuthStatusProvider implementation.
71 std::string
GetAccountId() const override
;
72 GoogleServiceAuthError
GetAuthStatus() const override
;
75 SigninErrorController
* signin_error_controller_
;
76 std::string account_id_
;
77 std::string refresh_token_
;
78 GoogleServiceAuthError last_auth_error_
;
80 DISALLOW_COPY_AND_ASSIGN(AccountInfo
);
83 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
84 PersistenceDBUpgrade
);
85 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
86 FetchPersistentError
);
87 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
88 PersistenceLoadCredentials
);
89 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
91 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
93 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
94 CanonicalizeAccountId
);
95 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
96 CanonAndNonCanonAccountId
);
97 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
100 // WebDataServiceConsumer implementation:
101 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle
,
102 const WDTypedResult
* result
) override
;
104 // Loads credentials into in memory stucture.
105 void LoadAllCredentialsIntoMemory(
106 const std::map
<std::string
, std::string
>& db_tokens
);
108 // Persists credentials for |account_id|. Enables overriding for
109 // testing purposes, or other cases, when accessing the DB is not desired.
110 void PersistCredentials(const std::string
& account_id
,
111 const std::string
& refresh_token
);
113 // Clears credentials persisted for |account_id|. Enables overriding for
114 // testing purposes, or other cases, when accessing the DB is not desired.
115 void ClearPersistedCredentials(const std::string
& account_id
);
117 // Revokes the refresh token on the server.
118 void RevokeCredentialsOnServer(const std::string
& refresh_token
);
120 // Cancels any outstanding fetch for tokens from the web database.
121 void CancelWebTokenFetch();
123 std::string
GetRefreshToken(const std::string
& account_id
) const;
125 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
126 // to information about the account.
127 typedef std::map
<std::string
, linked_ptr
<AccountInfo
>> AccountInfoMap
;
128 // In memory refresh token store mapping account_id to refresh_token.
129 AccountInfoMap refresh_tokens_
;
131 // Handle to the request reading tokens from database.
132 WebDataServiceBase::Handle web_data_service_request_
;
134 // The primary account id of this service's profile during the loading of
135 // credentials. This member is empty otherwise.
136 std::string loading_primary_account_id_
;
138 ScopedVector
<RevokeServerRefreshToken
> server_revokes_
;
140 // Used to verify that certain methods are called only on the thread on which
141 // this instance was created.
142 base::ThreadChecker thread_checker_
;
144 // Used to rate-limit network token requests so as to not overload the server.
145 net::BackoffEntry::Policy backoff_policy_
;
146 net::BackoffEntry backoff_entry_
;
147 GoogleServiceAuthError backoff_error_
;
149 SigninClient
* client_
;
150 SigninErrorController
* signin_error_controller_
;
151 AccountTrackerService
* account_tracker_service_
;
153 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenServiceDelegate
);