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/profile_oauth2_token_service.h"
11 #include "components/signin/core/browser/signin_error_controller.h"
12 #include "components/webdata/common/web_data_service_base.h"
13 #include "components/webdata/common/web_data_service_consumer.h"
14 #include "net/base/backoff_entry.h"
16 class MutableProfileOAuth2TokenServiceDelegate
17 : public OAuth2TokenServiceDelegate
,
18 public WebDataServiceConsumer
{
20 MutableProfileOAuth2TokenServiceDelegate(
22 SigninErrorController
* signin_error_controller
);
23 ~MutableProfileOAuth2TokenServiceDelegate() override
;
25 // OAuth2TokenServiceDelegate overrides.
26 OAuth2AccessTokenFetcher
* CreateAccessTokenFetcher(
27 const std::string
& account_id
,
28 net::URLRequestContextGetter
* getter
,
29 OAuth2AccessTokenConsumer
* consumer
) override
;
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
;
36 bool RefreshTokenIsAvailable(const std::string
& account_id
) const override
;
37 std::vector
<std::string
> GetAccounts() override
;
38 net::URLRequestContextGetter
* GetRequestContext() const override
;
40 void LoadCredentials(const std::string
& primary_account_id
) override
;
41 void UpdateCredentials(const std::string
& account_id
,
42 const std::string
& refresh_token
) override
;
43 void RevokeAllCredentials() override
;
45 // Revokes credentials related to |account_id|.
46 void RevokeCredentials(const std::string
& account_id
) override
;
48 // Overridden from OAuth2TokenServiceDelegate.
49 void Shutdown() override
;
52 friend class MutableProfileOAuth2TokenServiceDelegateTest
;
54 class RevokeServerRefreshToken
;
56 class AccountInfo
: public SigninErrorController::AuthStatusProvider
{
58 AccountInfo(SigninErrorController
* signin_error_controller
,
59 const std::string
& account_id
,
60 const std::string
& refresh_token
);
61 ~AccountInfo() override
;
63 const std::string
& refresh_token() const { return refresh_token_
; }
64 void set_refresh_token(const std::string
& token
) { refresh_token_
= token
; }
66 void SetLastAuthError(const GoogleServiceAuthError
& error
);
68 // SigninErrorController::AuthStatusProvider implementation.
69 std::string
GetAccountId() const override
;
70 GoogleServiceAuthError
GetAuthStatus() const override
;
73 SigninErrorController
* signin_error_controller_
;
74 std::string account_id_
;
75 std::string refresh_token_
;
76 GoogleServiceAuthError last_auth_error_
;
78 DISALLOW_COPY_AND_ASSIGN(AccountInfo
);
81 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
82 PersistenceDBUpgrade
);
83 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
84 FetchPersistentError
);
85 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
86 PersistenceLoadCredentials
);
87 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
89 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
91 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
92 CanonicalizeAccountId
);
93 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
94 CanonAndNonCanonAccountId
);
95 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceDelegateTest
,
98 // WebDataServiceConsumer implementation:
99 void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle
,
100 const WDTypedResult
* result
) override
;
102 // Loads credentials into in memory stucture.
103 void LoadAllCredentialsIntoMemory(
104 const std::map
<std::string
, std::string
>& db_tokens
);
106 // Persists credentials for |account_id|. Enables overriding for
107 // testing purposes, or other cases, when accessing the DB is not desired.
108 void PersistCredentials(const std::string
& account_id
,
109 const std::string
& refresh_token
);
111 // Clears credentials persisted for |account_id|. Enables overriding for
112 // testing purposes, or other cases, when accessing the DB is not desired.
113 void ClearPersistedCredentials(const std::string
& account_id
);
115 // Revokes the refresh token on the server.
116 void RevokeCredentialsOnServer(const std::string
& refresh_token
);
118 // Cancels any outstanding fetch for tokens from the web database.
119 void CancelWebTokenFetch();
121 std::string
GetRefreshToken(const std::string
& account_id
) const;
123 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
124 // to information about the account.
125 typedef std::map
<std::string
, linked_ptr
<AccountInfo
>> AccountInfoMap
;
126 // In memory refresh token store mapping account_id to refresh_token.
127 AccountInfoMap refresh_tokens_
;
129 // Handle to the request reading tokens from database.
130 WebDataServiceBase::Handle web_data_service_request_
;
132 // The primary account id of this service's profile during the loading of
133 // credentials. This member is empty otherwise.
134 std::string loading_primary_account_id_
;
136 ScopedVector
<RevokeServerRefreshToken
> server_revokes_
;
138 // Used to verify that certain methods are called only on the thread on which
139 // this instance was created.
140 base::ThreadChecker thread_checker_
;
142 // Used to rate-limit network token requests so as to not overload the server.
143 net::BackoffEntry::Policy backoff_policy_
;
144 net::BackoffEntry backoff_entry_
;
145 GoogleServiceAuthError backoff_error_
;
147 SigninClient
* client_
;
148 SigninErrorController
* signin_error_controller_
;
150 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenServiceDelegate
);