Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / browser / chromeos / login / signin / token_handle_fetcher.h
blob5c471a4384c3319c91feafc318da4a1134c88bd9
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_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_FETCHER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_FETCHER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/containers/scoped_ptr_hash_map.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
16 #include "components/user_manager/user_id.h"
17 #include "google_apis/gaia/gaia_oauth_client.h"
18 #include "google_apis/gaia/oauth2_token_service.h"
20 class TokenHandleUtil;
21 class Profile;
23 // This class is resposible for obtaining new token handle for user.
24 // It can be used in two ways. When user have just used GAIA signin there is
25 // an OAuth2 token available. If there is profile already loaded, then
26 // minting additional access token might be required.
28 class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
29 public OAuth2TokenService::Consumer,
30 public OAuth2TokenService::Observer {
31 public:
32 TokenHandleFetcher(TokenHandleUtil* util,
33 const user_manager::UserID& user_id);
34 ~TokenHandleFetcher() override;
36 typedef base::Callback<void(const user_manager::UserID&, bool success)>
37 TokenFetchingCallback;
39 // Get token handle for user who have just signed in via GAIA. This
40 // request will be performed using signin profile.
41 void FillForNewUser(const std::string& access_token,
42 const TokenFetchingCallback& callback);
44 // Get token handle for existing user.
45 void BackfillToken(Profile* profile, const TokenFetchingCallback& callback);
47 private:
48 // OAuth2TokenService::Observer override:
49 void OnRefreshTokenAvailable(const std::string& account_id) override;
51 // OAuth2TokenService::Consumer overrides:
52 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
53 const std::string& access_token,
54 const base::Time& expiration_time) override;
55 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
56 const GoogleServiceAuthError& error) override;
58 // GaiaOAuthClient::Delegate overrides:
59 void OnOAuthError() override;
60 void OnNetworkError(int response_code) override;
61 void OnGetTokenInfoResponse(
62 scoped_ptr<base::DictionaryValue> token_info) override;
64 void RequestAccessToken(const std::string& account_id);
65 void FillForAccessToken(const std::string& access_token);
67 // This is called before profile is detroyed.
68 void OnProfileDestroyed();
70 TokenHandleUtil* token_handle_util_;
71 user_manager::UserID user_id_;
72 OAuth2TokenService* token_service_;
74 bool waiting_for_refresh_token_;
75 std::string account_without_token_;
76 Profile* profile_;
77 base::TimeTicks tokeninfo_response_start_time_;
78 TokenFetchingCallback callback_;
79 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_;
80 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
81 scoped_ptr<KeyedServiceShutdownNotifier::Subscription>
82 profile_shutdown_notification_;
84 DISALLOW_COPY_AND_ASSIGN(TokenHandleFetcher);
87 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_FETCHER_H_