Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / profile_oauth2_token_service.h
blobc4431d64c24f711f6583b88e72575b708462fc91
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_PROFILE_OAUTH2_TOKEN_SERVICE_H_
6 #define CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_
8 #include <string>
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/linked_ptr.h"
12 #include "chrome/browser/signin/signin_global_error.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14 #include "google_apis/gaia/oauth2_token_service.h"
16 namespace net {
17 class URLRequestContextGetter;
20 class GoogleServiceAuthError;
21 class Profile;
22 class SigninGlobalError;
24 // ProfileOAuth2TokenService is a BrowserContextKeyedService that retrieves
25 // OAuth2 access tokens for a given set of scopes using the OAuth2 login
26 // refresh tokens.
28 // See |OAuth2TokenService| for usage details.
30 // Note: after StartRequest returns, in-flight requests will continue
31 // even if the TokenService refresh token that was used to initiate
32 // the request changes or is cleared. When the request completes,
33 // Consumer::OnGetTokenSuccess will be invoked, but the access token
34 // won't be cached.
36 // Note: requests should be started from the UI thread. To start a
37 // request from other thread, please use ProfileOAuth2TokenServiceRequest.
38 class ProfileOAuth2TokenService : public OAuth2TokenService,
39 public BrowserContextKeyedService {
40 public:
41 // Initializes this token service with the profile.
42 virtual void Initialize(Profile* profile);
44 // BrowserContextKeyedService implementation.
45 virtual void Shutdown() OVERRIDE;
47 // Gets an account id of the primary account related to the profile.
48 std::string GetPrimaryAccountId();
50 // Lists account IDs of all accounts with a refresh token.
51 virtual std::vector<std::string> GetAccounts() OVERRIDE;
53 // Loads credentials from a backing persistent store to make them available
54 // after service is used between profile restarts.
55 // Usually it's not necessary to directly call this method.
56 // TODO(bauerb): Make this method protected once this class initializes itself
57 // automatically.
58 virtual void LoadCredentials();
60 // Updates a |refresh_token| for an |account_id|. Credentials are persisted,
61 // and available through |LoadCredentials| after service is restarted.
62 virtual void UpdateCredentials(const std::string& account_id,
63 const std::string& refresh_token);
65 // Revokes all credentials handled by the object.
66 virtual void RevokeAllCredentials();
68 SigninGlobalError* signin_global_error() {
69 return signin_global_error_.get();
72 const SigninGlobalError* signin_global_error() const {
73 return signin_global_error_.get();
76 Profile* profile() const { return profile_; }
78 protected:
79 ProfileOAuth2TokenService();
80 virtual ~ProfileOAuth2TokenService();
82 // OAuth2TokenService overrides.
83 // Note: These methods are overriden so that ProfileOAuth2TokenService is a
84 // concrete class.
86 // Simply returns NULL and should be overriden by subsclasses.
87 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
89 // Default implementation of this method is NOTREACHED as it should be be
90 // overriden by subclasses.
91 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE;
93 // Updates the internal cache of the result from the most-recently-completed
94 // auth request (used for reporting errors to the user).
95 virtual void UpdateAuthError(
96 const std::string& account_id,
97 const GoogleServiceAuthError& error) OVERRIDE;
99 private:
100 // The profile with which this instance was initialized, or NULL.
101 Profile* profile_;
103 // Used to show auth errors in the wrench menu. The SigninGlobalError is
104 // different than most GlobalErrors in that its lifetime is controlled by
105 // ProfileOAuth2TokenService (so we can expose a reference for use in the
106 // wrench menu).
107 scoped_ptr<SigninGlobalError> signin_global_error_;
109 DISALLOW_COPY_AND_ASSIGN(ProfileOAuth2TokenService);
112 #endif // CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_