Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / signin / core / browser / mutable_profile_oauth2_token_service.h
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.
4
5 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_
7
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"
13
14 // A specialization of ProfileOAuth2TokenService that can can mutate its OAuth2
15 // tokens.
16 //
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  {
21  public:
22   // ProfileOAuth2TokenService overrides.
23   void Shutdown() override;
24   std::vector<std::string> GetAccounts() override;
25
26   // The below three methods should be called only on the thread on which this
27   // object was created.
28   void LoadCredentials(const std::string& primary_account_id) override;
29   void UpdateCredentials(const std::string& account_id,
30                          const std::string& refresh_token) override;
31   void RevokeAllCredentials() override;
32   bool RefreshTokenIsAvailable(const std::string& account_id) const override;
33
34   // Revokes credentials related to |account_id|.
35   void RevokeCredentials(const std::string& account_id);
36
37  protected:
38   class AccountInfo : public SigninErrorController::AuthStatusProvider {
39    public:
40     AccountInfo(ProfileOAuth2TokenService* token_service,
41                 const std::string& account_id,
42                 const std::string& refresh_token);
43     ~AccountInfo() override;
44
45     const std::string& refresh_token() const { return refresh_token_; }
46     void set_refresh_token(const std::string& token) {
47       refresh_token_ = token;
48     }
49
50     void SetLastAuthError(const GoogleServiceAuthError& error);
51
52     // SigninErrorController::AuthStatusProvider implementation.
53     std::string GetAccountId() const override;
54     std::string GetUsername() const override;
55     GoogleServiceAuthError GetAuthStatus() const override;
56
57    private:
58     ProfileOAuth2TokenService* token_service_;
59     std::string account_id_;
60     std::string refresh_token_;
61     GoogleServiceAuthError last_auth_error_;
62
63     DISALLOW_COPY_AND_ASSIGN(AccountInfo);
64   };
65
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;
69
70   friend class ProfileOAuth2TokenServiceFactory;
71   friend class MutableProfileOAuth2TokenServiceTest;
72
73   MutableProfileOAuth2TokenService();
74   ~MutableProfileOAuth2TokenService() override;
75
76   // OAuth2TokenService implementation.
77   OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
78       const std::string& account_id,
79       net::URLRequestContextGetter* getter,
80       OAuth2AccessTokenConsumer* consumer) override;
81   net::URLRequestContextGetter* GetRequestContext() override;
82
83   // Updates the internal cache of the result from the most-recently-completed
84   // auth request (used for reporting errors to the user).
85   void UpdateAuthError(const std::string& account_id,
86                        const GoogleServiceAuthError& error) override;
87
88   virtual std::string GetRefreshToken(const std::string& account_id) const;
89
90   AccountInfoMap& refresh_tokens() { return refresh_tokens_; }
91
92  private:
93   class RevokeServerRefreshToken;
94
95   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
96                            TokenServiceUpdateClearsCache);
97   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
98                            PersistenceDBUpgrade);
99   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
100                            PersistenceLoadCredentials);
101   FRIEND_TEST_ALL_PREFIXES(MutableProfileOAuth2TokenServiceTest,
102                            CanonicalizeAccountId);
103
104   // WebDataServiceConsumer implementation:
105   void OnWebDataServiceRequestDone(WebDataServiceBase::Handle handle,
106                                    const WDTypedResult* result) override;
107
108   // Loads credentials into in memory stucture.
109   void LoadAllCredentialsIntoMemory(
110       const std::map<std::string, std::string>& db_tokens);
111
112   // Persists credentials for |account_id|. Enables overriding for
113   // testing purposes, or other cases, when accessing the DB is not desired.
114   void PersistCredentials(const std::string& account_id,
115                           const std::string& refresh_token);
116
117   // Clears credentials persisted for |account_id|. Enables overriding for
118   // testing purposes, or other cases, when accessing the DB is not desired.
119   void ClearPersistedCredentials(const std::string& account_id);
120
121   // Revokes the refresh token on the server.
122   void RevokeCredentialsOnServer(const std::string& refresh_token);
123
124   // Cancels any outstanding fetch for tokens from the web database.
125   void CancelWebTokenFetch();
126
127   // In memory refresh token store mapping account_id to refresh_token.
128   AccountInfoMap refresh_tokens_;
129
130   // Handle to the request reading tokens from database.
131   WebDataServiceBase::Handle web_data_service_request_;
132
133   // The primary account id of this service's profile during the loading of
134   // credentials.  This member is empty otherwise.
135   std::string loading_primary_account_id_;
136
137   ScopedVector<RevokeServerRefreshToken> server_revokes_;
138
139   // Used to verify that certain methods are called only on the thread on which
140   // this instance was created.
141   base::ThreadChecker thread_checker_;
142
143   DISALLOW_COPY_AND_ASSIGN(MutableProfileOAuth2TokenService);
144 };
145
146 #endif  // COMPONENTS_SIGNIN_CORE_BROWSER_MUTABLE_PROFILE_OAUTH2_TOKEN_SERVICE_H_