1 // Copyright 2013 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_H_
6 #define CHROME_BROWSER_SIGNIN_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
8 #include "chrome/browser/signin/profile_oauth2_token_service.h"
9 #include "components/webdata/common/web_data_service_base.h"
10 #include "components/webdata/common/web_data_service_consumer.h"
12 // A specialization of ProfileOAuth2TokenService that can can mutate its OAuth2
15 // Note: This class is just a placeholder for now. Methods used to mutate
16 // the tokens are currently being migrated from ProfileOAuth2TokenService.
17 class MutableProfileOAuth2TokenService
: public ProfileOAuth2TokenService
,
18 public WebDataServiceConsumer
{
20 // ProfileOAuth2TokenService overrides.
21 virtual void Shutdown() OVERRIDE
;
22 virtual std::vector
<std::string
> GetAccounts() OVERRIDE
;
23 virtual void LoadCredentials() OVERRIDE
;
24 virtual void UpdateCredentials(const std::string
& account_id
,
25 const std::string
& refresh_token
) OVERRIDE
;
26 virtual void RevokeAllCredentials() OVERRIDE
;
28 // Revokes credentials related to |account_id|.
29 void RevokeCredentials(const std::string
& account_id
);
32 class AccountInfo
: public SigninGlobalError::AuthStatusProvider
{
34 AccountInfo(ProfileOAuth2TokenService
* token_service
,
35 const std::string
& account_id
,
36 const std::string
& refresh_token
);
37 virtual ~AccountInfo();
39 const std::string
& refresh_token() const { return refresh_token_
; }
40 void set_refresh_token(const std::string
& token
) {
41 refresh_token_
= token
;
44 void SetLastAuthError(const GoogleServiceAuthError
& error
);
46 // SigninGlobalError::AuthStatusProvider implementation.
47 virtual std::string
GetAccountId() const OVERRIDE
;
48 virtual GoogleServiceAuthError
GetAuthStatus() const OVERRIDE
;
51 ProfileOAuth2TokenService
* token_service_
;
52 std::string account_id_
;
53 std::string refresh_token_
;
54 GoogleServiceAuthError last_auth_error_
;
56 DISALLOW_COPY_AND_ASSIGN(AccountInfo
);
59 // Maps the |account_id| of accounts known to ProfileOAuth2TokenService
60 // to information about the account.
61 typedef std::map
<std::string
, linked_ptr
<AccountInfo
> > AccountInfoMap
;
63 friend class ProfileOAuth2TokenServiceFactory
;
65 MutableProfileOAuth2TokenService();
66 virtual ~MutableProfileOAuth2TokenService();
68 // OAuth2TokenService implementation.
69 virtual std::string
GetRefreshToken(const std::string
& account_id
) OVERRIDE
;
70 virtual net::URLRequestContextGetter
* GetRequestContext() OVERRIDE
;
72 AccountInfoMap
& refresh_tokens() { return refresh_tokens_
; }
75 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest
,
76 TokenServiceUpdateClearsCache
);
77 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest
,
78 PersistenceDBUpgrade
);
79 FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest
,
80 PersistenceLoadCredentials
);
82 // Updates the internal cache of the result from the most-recently-completed
83 // auth request (used for reporting errors to the user).
84 virtual void UpdateAuthError(
85 const std::string
& account_id
,
86 const GoogleServiceAuthError
& error
) OVERRIDE
;
88 // WebDataServiceConsumer implementation:
89 virtual void OnWebDataServiceRequestDone(
90 WebDataServiceBase::Handle handle
,
91 const WDTypedResult
* result
) OVERRIDE
;
93 // When migrating an old login-scoped refresh token, this returns the account
94 // ID with which the token was associated.
95 std::string
GetAccountIdForMigratingRefreshToken();
97 // Loads credentials into in memory stucture.
98 void LoadAllCredentialsIntoMemory(
99 const std::map
<std::string
, std::string
>& db_tokens
);
101 // Persists credentials for |account_id|. Enables overriding for
102 // testing purposes, or other cases, when accessing the DB is not desired.
103 void PersistCredentials(const std::string
& account_id
,
104 const std::string
& refresh_token
);
106 // Clears credentials persisted for |account_id|. Enables overriding for
107 // testing purposes, or other cases, when accessing the DB is not desired.
108 void ClearPersistedCredentials(const std::string
& account_id
);
110 // Revokes the refresh token on the server.
111 void RevokeCredentialsOnServer(const std::string
& refresh_token
);
113 // In memory refresh token store mapping account_id to refresh_token.
114 AccountInfoMap refresh_tokens_
;
116 // Handle to the request reading tokens from database.
117 WebDataServiceBase::Handle web_data_service_request_
;
119 DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService
);
122 #endif // CHROME_BROWSER_SIGNIN_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_