Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 #include "remoting/host/gaia_user_email_fetcher.h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/values.h" | |
| 10 #include "googleurl/src/gurl.h" | |
| 11 #include "net/http/http_status_code.h" | |
| 12 #include "net/url_request/url_fetcher.h" | |
| 13 #include "net/url_request/url_fetcher_delegate.h" | |
| 14 #include "net/url_request/url_request_context_getter.h" | |
| 15 | |
| 16 namespace { | |
| 17 const char kOAuth2UserInfoUrl[] = | |
| 18 "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.
| |
| 19 } // namespace | |
| 20 | |
| 21 namespace remoting { | |
| 22 | |
| 23 class GaiaUserEmailFetcher::Core | |
| 24 : public base::RefCountedThreadSafe<GaiaUserEmailFetcher::Core>, | |
| 25 public net::URLFetcherDelegate { | |
| 26 public: | |
| 27 Core(net::URLRequestContextGetter* request_context_getter) | |
| 28 : request_context_getter_(request_context_getter), | |
| 29 delegate_(NULL) { | |
| 30 } | |
| 31 | |
| 32 void GetUserEmail(const std::string& oauth_access_token, Delegate* delegate); | |
| 33 | |
| 34 // net::URLFetcherDelegate interface | |
| 35 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 36 | |
| 37 private: | |
| 38 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
| |
| 39 | |
| 40 virtual ~Core() {} | |
| 41 | |
| 42 void FetchUserInfoAndInvokeCallback(); | |
| 43 void OnUserInfoFetchComplete(const net::URLRequestStatus& status, | |
| 44 int response_code, | |
| 45 const std::string& response); | |
| 46 | |
| 47 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 48 GaiaUserEmailFetcher::Delegate* delegate_; | |
| 49 scoped_ptr<net::URLFetcher> request_; | |
| 50 }; | |
| 51 | |
| 52 void GaiaUserEmailFetcher::Core::GetUserEmail( | |
| 53 const std::string& oauth_access_token, Delegate* delegate) { | |
| 54 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
| |
| 55 | |
| 56 delegate_ = delegate; | |
| 57 | |
| 58 request_.reset(net::URLFetcher::Create( | |
| 59 GURL(kOAuth2UserInfoUrl), net::URLFetcher::GET, this)); | |
| 60 request_->SetRequestContext(request_context_getter_); | |
| 61 request_->AddExtraRequestHeader( | |
| 62 "Authorization: OAuth " + oauth_access_token); | |
| 63 request_->Start(); | |
| 64 } | |
| 65 | |
| 66 void GaiaUserEmailFetcher::Core::OnURLFetchComplete( | |
| 67 const net::URLFetcher* source) { | |
| 68 std::string response_string; | |
| 69 source->GetResponseAsString(&response_string); | |
| 70 OnUserInfoFetchComplete(source->GetStatus(), | |
| 71 source->GetResponseCode(), | |
| 72 response_string); | |
| 73 } | |
| 74 | |
| 75 void GaiaUserEmailFetcher::Core::OnUserInfoFetchComplete( | |
| 76 const net::URLRequestStatus& status, | |
| 77 int response_code, | |
| 78 const std::string& response) { | |
| 79 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
| |
| 80 std::string email; | |
| 81 if (response_code == net::HTTP_OK) { | |
| 82 scoped_ptr<Value> response_value(base::JSONReader::Read(response)); | |
| 83 if (response_value.get()) { | |
| 84 base::DictionaryValue* response_dict = NULL; | |
| 85 if (response_value->GetAsDictionary(&response_dict)) { | |
| 86 response_dict->GetString("email", &email); | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 if (email.empty()) { | |
| 91 delegate_->OnNetworkError(response_code); | |
| 92 } else { | |
| 93 delegate_->OnGetUserEmailResponse(email); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 GaiaUserEmailFetcher::GaiaUserEmailFetcher( | |
| 98 net::URLRequestContextGetter* context_getter) { | |
| 99 core_ = new Core(context_getter); | |
| 100 } | |
| 101 | |
| 102 GaiaUserEmailFetcher::~GaiaUserEmailFetcher() { | |
| 103 } | |
| 104 | |
| 105 void GaiaUserEmailFetcher::GetUserEmail( | |
| 106 const std::string& oauth_access_token, | |
| 107 Delegate* delegate) { | |
| 108 return core_->GetUserEmail(oauth_access_token, delegate); | |
| 109 } | |
| 110 | |
| 111 } // namespace remoting | |
| OLD | NEW |