1 // Copyright 2014 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 COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_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/webdata/common/web_data_service_base.h"
12 #include "components/webdata/common/web_data_service_consumer.h"
14 // A specialization of ProfileOAuth2TokenService that can can mutate its OAuth2
17 // Note: This class is just a placeholder for now. Methods used to mutate
18 // the tokens are currently being migrated from ProfileOAuth2TokenService.
19 class MutableProfileOAuth2TokenService : public ProfileOAuth2TokenService,
20 public WebDataServiceConsumer {
22 // ProfileOAuth2TokenService overrides.
23 virtual void Shutdown() OVERRIDE;
24 virtual std::vector<std::string> GetAccounts() OVERRIDE;
26 // The below three methods should be called only on the thread on which this
27 // object was created.
28 virtual void LoadCredentials(const std::string& primary_account_id) OVERRIDE;
29 virtual void UpdateCredentials(const std::string& account_id,
30 const std::string& refresh_token) OVERRIDE;
31 virtual void RevokeAllCredentials() OVERRIDE;
32 virtual bool RefreshTokenIsAvailable(const std::string& account_id) const
35 // Revokes credentials related to |account_id|.
36 void RevokeCredentials(const std::string& account_id);
39 class AccountInfo : public SigninErrorController::AuthStatusProvider {
41 AccountInfo(ProfileOAuth2TokenService* token_service,
42 const std::string& account_id,
43 const std::string& refresh_token);
44 virtual ~AccountInfo();
46 const std::string& refresh_token() const { return refresh_token_; }
47 void set_refresh_token(const std::string& token) {
48 refresh_token_ = token;
51 void SetLastAuthError(const GoogleServiceAuthError& error);
53 // SigninErrorController::AuthStatusProvider implementation.
54 virtual std::string GetAccountId() const OVERRIDE;
55 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
58 ProfileOAuth2TokenService* token_service_;
59 std::string account_id_;
60 std::string refresh_token_;
61 GoogleServiceAuthError last_auth_error_;
63 DISALLOW_COPY_AND_ASSIGN(AccountInfo);
66 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
67 // to information about the account.
68 typedef std::map<std::string, linked_ptr<AccountInfo> > AccountInfoMap;
70 friend class ProfileOAuth2TokenServiceFactory;
71 friend class MutableProfileOAuth2TokenServiceTest;
73 MutableProfileOAuth2TokenService();
74 virtual ~MutableProfileOAuth2TokenService();
76 // OAuth2TokenService implementation.
77 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
78 const std::string& account_id,
79 net::URLRequestContextGetter* getter,
80 OAuth2AccessTokenConsumer* consumer) OVERRIDE;
81 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
83 // Updates the internal cache of the result from the most-recently-completed
84 // auth request (used for reporting errors to the user).
85 virtual void UpdateAuthError(const std::string& account_id,
86 const GoogleServiceAuthError& error) OVERRIDE;
88 virtual std::string GetRefreshToken(const std::string& account_id) const;
90 AccountInfoMap& refresh_tokens() { return refresh_tokens_; }
93 class RevokeServerRefreshToken;
95 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
96 TokenServiceUpdateClearsCache);
97 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
98 PersistenceDBUpgrade);
99 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
100 PersistenceLoadCredentials);
102 // WebDataServiceConsumer implementation:
103 virtual void OnWebDataServiceRequestDone(
104 WebDataServiceBase::Handle handle,
105 const WDTypedResult* result) OVERRIDE;
107 // Loads credentials into in memory stucture.
108 void LoadAllCredentialsIntoMemory(
109 const std::map<std::string, std::string>& db_tokens);
111 // Persists credentials for |account_id|. Enables overriding for
112 // testing purposes, or other cases, when accessing the DB is not desired.
113 void PersistCredentials(const std::string& account_id,
114 const std::string& refresh_token);
116 // Clears credentials persisted for |account_id|. Enables overriding for
117 // testing purposes, or other cases, when accessing the DB is not desired.
118 void ClearPersistedCredentials(const std::string& account_id);
120 // Revokes the refresh token on the server.
121 void RevokeCredentialsOnServer(const std::string& refresh_token);
123 // Cancels any outstanding fetch for tokens from the web database.
124 void CancelWebTokenFetch();
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 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService);
145 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_