Chromium Code Reviews| Index: remoting/host/gaia_user_email_fetcher.cc |
| diff --git a/remoting/host/gaia_user_email_fetcher.cc b/remoting/host/gaia_user_email_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aaa9d22ad97279a4b34d5186e30c5ade375404ac |
| --- /dev/null |
| +++ b/remoting/host/gaia_user_email_fetcher.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/gaia_user_email_fetcher.h" |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +namespace { |
| +const char kOAuth2UserInfoUrl[] = |
| + "https://www.googleapis.com/oauth2/v1/userinfo"; |
|
Jamie
2012/10/02 20:38:35
This is a different URL to the one we use in the J
simonmorris
2012/10/02 21:50:52
Yes. This URL is used in google_apis/gaia.
|
| +} // namespace |
| + |
| +namespace remoting { |
| + |
| +class GaiaUserEmailFetcher::Core |
| + : public base::RefCountedThreadSafe<GaiaUserEmailFetcher::Core>, |
| + public net::URLFetcherDelegate { |
| + public: |
| + Core(net::URLRequestContextGetter* request_context_getter) |
| + : request_context_getter_(request_context_getter), |
| + delegate_(NULL) { |
| + } |
| + |
| + void GetUserEmail(const std::string& oauth_access_token, Delegate* delegate); |
| + |
| + // net::URLFetcherDelegate interface |
| + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<Core>; |
|
Jamie
2012/10/02 20:38:35
Why is the friend declaration needed?
simonmorris
2012/10/02 21:50:52
To let the ref counter delete the ref-counted obje
|
| + |
| + virtual ~Core() {} |
| + |
| + void FetchUserInfoAndInvokeCallback(); |
| + void OnUserInfoFetchComplete(const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response); |
| + |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| + GaiaUserEmailFetcher::Delegate* delegate_; |
| + scoped_ptr<net::URLFetcher> request_; |
| +}; |
| + |
| +void GaiaUserEmailFetcher::Core::GetUserEmail( |
| + const std::string& oauth_access_token, Delegate* delegate) { |
| + DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
|
Jamie
2012/10/02 20:38:35
This feels like is needs more than just a DCHECK.
simonmorris
2012/10/02 21:50:52
It's done this way in GaiaOAuthClient, and the sim
Jamie
2012/10/02 22:45:27
It just ensures that the GetUserEmail/callback pai
|
| + |
| + delegate_ = delegate; |
| + |
| + request_.reset(net::URLFetcher::Create( |
| + GURL(kOAuth2UserInfoUrl), net::URLFetcher::GET, this)); |
| + request_->SetRequestContext(request_context_getter_); |
| + request_->AddExtraRequestHeader( |
| + "Authorization: OAuth " + oauth_access_token); |
| + request_->Start(); |
| +} |
| + |
| +void GaiaUserEmailFetcher::Core::OnURLFetchComplete( |
| + const net::URLFetcher* source) { |
| + std::string response_string; |
| + source->GetResponseAsString(&response_string); |
| + OnUserInfoFetchComplete(source->GetStatus(), |
| + source->GetResponseCode(), |
| + response_string); |
| +} |
| + |
| +void GaiaUserEmailFetcher::Core::OnUserInfoFetchComplete( |
| + const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response) { |
| + request_.reset(); |
|
Jamie
2012/10/02 20:38:35
It seems more natural for the reset to happen dire
simonmorris
2012/10/02 21:50:52
Unfortunately, moving the reset to before the OnUs
Jamie
2012/10/02 22:45:27
Well, you could save the status and response code
|
| + std::string email; |
| + if (response_code == net::HTTP_OK) { |
| + scoped_ptr<Value> response_value(base::JSONReader::Read(response)); |
| + if (response_value.get()) { |
| + base::DictionaryValue* response_dict = NULL; |
| + if (response_value->GetAsDictionary(&response_dict)) { |
| + response_dict->GetString("email", &email); |
| + } |
| + } |
| + } |
| + if (email.empty()) { |
| + delegate_->OnNetworkError(response_code); |
| + } else { |
| + delegate_->OnGetUserEmailResponse(email); |
| + } |
| +} |
| + |
| +GaiaUserEmailFetcher::GaiaUserEmailFetcher( |
| + net::URLRequestContextGetter* context_getter) { |
| + core_ = new Core(context_getter); |
| +} |
| + |
| +GaiaUserEmailFetcher::~GaiaUserEmailFetcher() { |
| +} |
| + |
| +void GaiaUserEmailFetcher::GetUserEmail( |
| + const std::string& oauth_access_token, |
| + Delegate* delegate) { |
| + return core_->GetUserEmail(oauth_access_token, delegate); |
| +} |
| + |
| +} // namespace remoting |