Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: chrome/common/net/gaia/gaia_oauth_client.cc

Issue 8373021: Convert URLFetcher::Delegates to use an interface in content/public/common. Also remove the old U... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync and remove unncessary forward declares Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/net/gaia/gaia_oauth_client.h" 5 #include "chrome/common/net/gaia/gaia_oauth_client.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/common/net/http_return.h" 10 #include "chrome/common/net/http_return.h"
11 #include "content/common/net/url_fetcher.h" 11 #include "content/common/net/url_fetcher.h"
12 #include "content/public/common/url_fetcher_delegate.h"
12 #include "googleurl/src/gurl.h" 13 #include "googleurl/src/gurl.h"
13 #include "net/base/escape.h" 14 #include "net/base/escape.h"
14 #include "net/url_request/url_request_context_getter.h" 15 #include "net/url_request/url_request_context_getter.h"
15 16
16 namespace { 17 namespace {
17 const char kAccessTokenValue[] = "access_token"; 18 const char kAccessTokenValue[] = "access_token";
18 const char kRefreshTokenValue[] = "refresh_token"; 19 const char kRefreshTokenValue[] = "refresh_token";
19 const char kExpiresInValue[] = "expires_in"; 20 const char kExpiresInValue[] = "expires_in";
20 } 21 }
21 22
22 namespace gaia { 23 namespace gaia {
23 24
24 class GaiaOAuthClient::Core 25 class GaiaOAuthClient::Core
25 : public base::RefCountedThreadSafe<GaiaOAuthClient::Core>, 26 : public base::RefCountedThreadSafe<GaiaOAuthClient::Core>,
26 public URLFetcher::Delegate { 27 public content::URLFetcherDelegate {
27 public: 28 public:
28 Core(const std::string& gaia_url, 29 Core(const std::string& gaia_url,
29 net::URLRequestContextGetter* request_context_getter) 30 net::URLRequestContextGetter* request_context_getter)
30 : gaia_url_(gaia_url), 31 : gaia_url_(gaia_url),
31 num_retries_(0), 32 num_retries_(0),
32 request_context_getter_(request_context_getter), 33 request_context_getter_(request_context_getter),
33 delegate_(NULL) {} 34 delegate_(NULL) {}
34 35
35 virtual ~Core() { } 36 virtual ~Core() { }
36 37
37 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, 38 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info,
38 const std::string& auth_code, 39 const std::string& auth_code,
39 int max_retries, 40 int max_retries,
40 GaiaOAuthClient::Delegate* delegate); 41 GaiaOAuthClient::Delegate* delegate);
41 void RefreshToken(const OAuthClientInfo& oauth_client_info, 42 void RefreshToken(const OAuthClientInfo& oauth_client_info,
42 const std::string& refresh_token, 43 const std::string& refresh_token,
43 int max_retries, 44 int max_retries,
44 GaiaOAuthClient::Delegate* delegate); 45 GaiaOAuthClient::Delegate* delegate);
45 46
46 // URLFetcher::Delegate implementation. 47 // content::URLFetcherDelegate implementation.
47 virtual void OnURLFetchComplete( 48 virtual void OnURLFetchComplete(const URLFetcher* source);
48 const URLFetcher* source,
49 const GURL& url,
50 const net::URLRequestStatus& status,
51 int response_code,
52 const net::ResponseCookies& cookies,
53 const std::string& data);
54 49
55 private: 50 private:
56 void MakeGaiaRequest(std::string post_body, 51 void MakeGaiaRequest(std::string post_body,
57 int max_retries, 52 int max_retries,
58 GaiaOAuthClient::Delegate* delegate); 53 GaiaOAuthClient::Delegate* delegate);
59 void HandleResponse(const URLFetcher* source, 54 void HandleResponse(const URLFetcher* source, bool* should_retry_request);
60 const GURL& url,
61 const net::URLRequestStatus& status,
62 int response_code,
63 const std::string& data,
64 bool* should_retry_request);
65 55
66 GURL gaia_url_; 56 GURL gaia_url_;
67 int num_retries_; 57 int num_retries_;
68 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 58 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
69 GaiaOAuthClient::Delegate* delegate_; 59 GaiaOAuthClient::Delegate* delegate_;
70 scoped_ptr<URLFetcher> request_; 60 scoped_ptr<URLFetcher> request_;
71 }; 61 };
72 62
73 void GaiaOAuthClient::Core::GetTokensFromAuthCode( 63 void GaiaOAuthClient::Core::GetTokensFromAuthCode(
74 const OAuthClientInfo& oauth_client_info, 64 const OAuthClientInfo& oauth_client_info,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 delegate_ = delegate; 96 delegate_ = delegate;
107 num_retries_ = 0; 97 num_retries_ = 0;
108 request_.reset(URLFetcher::Create(0, gaia_url_, URLFetcher::POST, this)); 98 request_.reset(URLFetcher::Create(0, gaia_url_, URLFetcher::POST, this));
109 request_->set_request_context(request_context_getter_); 99 request_->set_request_context(request_context_getter_);
110 request_->set_upload_data("application/x-www-form-urlencoded", post_body); 100 request_->set_upload_data("application/x-www-form-urlencoded", post_body);
111 request_->set_max_retries(max_retries); 101 request_->set_max_retries(max_retries);
112 request_->Start(); 102 request_->Start();
113 } 103 }
114 104
115 // URLFetcher::Delegate implementation. 105 // URLFetcher::Delegate implementation.
116 void GaiaOAuthClient::Core::OnURLFetchComplete( 106 void GaiaOAuthClient::Core::OnURLFetchComplete(const URLFetcher* source) {
117 const URLFetcher* source,
118 const GURL& url,
119 const net::URLRequestStatus& status,
120 int response_code,
121 const net::ResponseCookies& cookies,
122 const std::string& data) {
123 bool should_retry = false; 107 bool should_retry = false;
124 HandleResponse(source, url, status, response_code, data, &should_retry); 108 HandleResponse(source, &should_retry);
125 if (should_retry) { 109 if (should_retry) {
126 // Explicitly call ReceivedContentWasMalformed() to ensure the current 110 // Explicitly call ReceivedContentWasMalformed() to ensure the current
127 // request gets counted as a failure for calculation of the back-off 111 // request gets counted as a failure for calculation of the back-off
128 // period. If it was already a failure by status code, this call will 112 // period. If it was already a failure by status code, this call will
129 // be ignored. 113 // be ignored.
130 request_->ReceivedContentWasMalformed(); 114 request_->ReceivedContentWasMalformed();
131 num_retries_++; 115 num_retries_++;
132 // We must set our request_context_getter_ again because 116 // We must set our request_context_getter_ again because
133 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... 117 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL...
134 request_->set_request_context(request_context_getter_); 118 request_->set_request_context(request_context_getter_);
135 request_->Start(); 119 request_->Start();
136 } else { 120 } else {
137 request_.reset(); 121 request_.reset();
138 } 122 }
139 } 123 }
140 124
141 void GaiaOAuthClient::Core::HandleResponse( 125 void GaiaOAuthClient::Core::HandleResponse(
142 const URLFetcher* source, 126 const URLFetcher* source,
143 const GURL& url,
144 const net::URLRequestStatus& status,
145 int response_code,
146 const std::string& data,
147 bool* should_retry_request) { 127 bool* should_retry_request) {
148 *should_retry_request = false; 128 *should_retry_request = false;
149 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are 129 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are
150 // done here. 130 // done here.
151 if (response_code == RC_BAD_REQUEST) { 131 if (source->response_code() == RC_BAD_REQUEST) {
152 delegate_->OnOAuthError(); 132 delegate_->OnOAuthError();
153 return; 133 return;
154 } 134 }
155 std::string access_token; 135 std::string access_token;
156 std::string refresh_token; 136 std::string refresh_token;
157 int expires_in_seconds = 0; 137 int expires_in_seconds = 0;
158 if (response_code == RC_REQUEST_OK) { 138 if (source->response_code() == RC_REQUEST_OK) {
159 scoped_ptr<Value> message_value( 139 std::string data;
160 base::JSONReader::Read(data, false)); 140 source->GetResponseAsString(&data);
141 scoped_ptr<Value> message_value(base::JSONReader::Read(data, false));
161 if (message_value.get() && 142 if (message_value.get() &&
162 message_value->IsType(Value::TYPE_DICTIONARY)) { 143 message_value->IsType(Value::TYPE_DICTIONARY)) {
163 scoped_ptr<DictionaryValue> response_dict( 144 scoped_ptr<DictionaryValue> response_dict(
164 static_cast<DictionaryValue*>(message_value.release())); 145 static_cast<DictionaryValue*>(message_value.release()));
165 response_dict->GetString(kAccessTokenValue, &access_token); 146 response_dict->GetString(kAccessTokenValue, &access_token);
166 response_dict->GetString(kRefreshTokenValue, &refresh_token); 147 response_dict->GetString(kRefreshTokenValue, &refresh_token);
167 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds); 148 response_dict->GetInteger(kExpiresInValue, &expires_in_seconds);
168 } 149 }
169 } 150 }
170 if (access_token.empty()) { 151 if (access_token.empty()) {
171 // If we don't have an access token yet and the the error was not 152 // If we don't have an access token yet and the the error was not
172 // RC_BAD_REQUEST, we may need to retry. 153 // RC_BAD_REQUEST, we may need to retry.
173 if ((-1 != source->max_retries()) && 154 if ((-1 != source->max_retries()) &&
174 (num_retries_ > source->max_retries())) { 155 (num_retries_ > source->max_retries())) {
175 // Retry limit reached. Give up. 156 // Retry limit reached. Give up.
176 delegate_->OnNetworkError(response_code); 157 delegate_->OnNetworkError(source->response_code());
177 } else { 158 } else {
178 *should_retry_request = true; 159 *should_retry_request = true;
179 } 160 }
180 } else if (refresh_token.empty()) { 161 } else if (refresh_token.empty()) {
181 // If we only have an access token, then this was a refresh request. 162 // If we only have an access token, then this was a refresh request.
182 delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds); 163 delegate_->OnRefreshTokenResponse(access_token, expires_in_seconds);
183 } else { 164 } else {
184 delegate_->OnGetTokensResponse(refresh_token, 165 delegate_->OnGetTokensResponse(refresh_token,
185 access_token, 166 access_token,
186 expires_in_seconds); 167 expires_in_seconds);
(...skipping 24 matching lines...) Expand all
211 const std::string& refresh_token, 192 const std::string& refresh_token,
212 int max_retries, 193 int max_retries,
213 Delegate* delegate) { 194 Delegate* delegate) {
214 return core_->RefreshToken(oauth_client_info, 195 return core_->RefreshToken(oauth_client_info,
215 refresh_token, 196 refresh_token,
216 max_retries, 197 max_retries,
217 delegate); 198 delegate);
218 } 199 }
219 200
220 } // namespace gaia 201 } // namespace gaia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698