Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
Sergey Ulanov
2014/02/08 03:16:36
lose (c), 2014
rmsousa
2014/02/10 23:07:39
Done.
| |
| 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 REMOTING_HOST_OAUTH_TOKEN_GETTER_H_ | |
| 6 #define REMOTING_HOST_OAUTH_TOKEN_GETTER_H_ | |
| 7 | |
| 8 #include <queue> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/threading/non_thread_safe.h" | |
| 13 #include "google_apis/gaia/gaia_oauth_client.h" | |
| 14 | |
| 15 namespace net { | |
| 16 class URLRequestContextGetter; | |
| 17 } // namespace net | |
| 18 | |
| 19 namespace remoting { | |
| 20 | |
| 21 // OAuthTokenGetter caches OAuth access tokens and refreshes them as needed. | |
| 22 class OAuthTokenGetter : | |
| 23 public base::NonThreadSafe, | |
| 24 public gaia::GaiaOAuthClient::Delegate { | |
| 25 public: | |
| 26 typedef base::Callback<void(const std::string& user_email, | |
| 27 const std::string& access_token)> TokenCallback; | |
| 28 | |
| 29 // This structure contains information required to perform | |
| 30 // authentication to OAuth2. | |
| 31 struct OAuthCredentials { | |
| 32 OAuthCredentials(const std::string& login_value, | |
| 33 const std::string& refresh_token_value, | |
| 34 bool is_service_account); | |
| 35 | |
| 36 // The user's account name (i.e. their email address). | |
| 37 std::string login; | |
| 38 | |
| 39 // Token delegating authority to us to act as the user. | |
| 40 std::string refresh_token; | |
| 41 | |
| 42 // Whether these credentials belong to a service account. | |
| 43 bool is_service_account; | |
| 44 }; | |
| 45 | |
| 46 OAuthTokenGetter( | |
| 47 scoped_ptr<OAuthCredentials> oauth_credentials, | |
| 48 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); | |
| 49 virtual ~OAuthTokenGetter(); | |
| 50 | |
| 51 // Call |on_access_token| with an access token, or one of the callbacks. | |
| 52 // These are individual callbacks, rather than a delegate object, to provide | |
| 53 // flexibility for the caller to keep state or use WeakPtrs where needed. | |
| 54 void CallWithToken(const TokenCallback& on_access_token, | |
| 55 const base::Closure& on_network_error, | |
| 56 const base::Closure& on_auth_error); | |
|
Sergey Ulanov
2014/02/08 03:16:36
Do we need three separate callbacks? Maybe just ad
rmsousa
2014/02/10 23:07:39
Done.
| |
| 57 | |
| 58 // gaia::GaiaOAuthClient::Delegate interface. | |
| 59 virtual void OnGetTokensResponse(const std::string& user_email, | |
| 60 const std::string& access_token, | |
| 61 int expires_seconds) OVERRIDE; | |
| 62 virtual void OnRefreshTokenResponse(const std::string& access_token, | |
| 63 int expires_in_seconds) OVERRIDE; | |
| 64 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; | |
| 65 virtual void OnOAuthError() OVERRIDE; | |
| 66 virtual void OnNetworkError(int response_code) OVERRIDE; | |
| 67 | |
| 68 private: | |
| 69 struct Callbacks { | |
| 70 Callbacks(const TokenCallback& on_access_token, | |
| 71 const base::Closure& on_network_error, | |
| 72 const base::Closure& on_auth_error); | |
| 73 ~Callbacks(); | |
| 74 TokenCallback on_access_token; | |
| 75 base::Closure on_network_error; | |
| 76 base::Closure on_auth_error; | |
| 77 }; | |
| 78 | |
| 79 void RefreshOAuthToken(); | |
| 80 | |
| 81 scoped_ptr<OAuthCredentials> oauth_credentials_; | |
| 82 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; | |
| 83 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
| 84 | |
| 85 bool refreshing_oauth_token_; | |
| 86 std::string oauth_access_token_; | |
| 87 base::Time auth_token_expiry_time_; | |
| 88 std::queue<Callbacks> pending_callbacks_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter); | |
| 91 }; | |
| 92 | |
| 93 } // namespace remoting | |
| 94 | |
| 95 #endif // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_ | |
| OLD | NEW |