| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/service/gaia/service_gaia_authenticator.h" |
| 6 |
| 7 #include "base/message_loop_proxy.h" |
| 8 #include "chrome/service/net/service_url_request_context.h" |
| 9 #include "googleurl/src/gurl.h" |
| 10 |
| 11 ServiceGaiaAuthenticator::ServiceGaiaAuthenticator( |
| 12 const std::string& user_agent, const std::string& service_id, |
| 13 const std::string& gaia_url, |
| 14 base::MessageLoopProxy* io_message_loop_proxy) |
| 15 : gaia::GaiaAuthenticator(user_agent, service_id, gaia_url), |
| 16 http_post_completed_(false, false), |
| 17 io_message_loop_proxy_(io_message_loop_proxy), |
| 18 http_response_code_(0) { |
| 19 } |
| 20 |
| 21 ServiceGaiaAuthenticator::~ServiceGaiaAuthenticator() { |
| 22 } |
| 23 |
| 24 bool ServiceGaiaAuthenticator::Post(const GURL& url, |
| 25 const std::string& post_body, |
| 26 unsigned long* response_code, |
| 27 std::string* response_body) { |
| 28 DCHECK(url.SchemeIsSecure()); |
| 29 DCHECK(io_message_loop_proxy_); |
| 30 io_message_loop_proxy_->PostTask( |
| 31 FROM_HERE, |
| 32 NewRunnableMethod(this, &ServiceGaiaAuthenticator::DoPost, url, |
| 33 post_body)); |
| 34 if (!http_post_completed_.Wait()) // Block until network request completes. |
| 35 NOTREACHED(); // See OnURLFetchComplete. |
| 36 |
| 37 *response_code = static_cast<int>(http_response_code_); |
| 38 *response_body = response_data_; |
| 39 return true; |
| 40 } |
| 41 |
| 42 // TODO(sanjeevr): This is a placeholder implementation. Need to move this logic |
| 43 // to a common location within the service process so that it can be resued by |
| 44 // other classes needing a backoff delay calculation. |
| 45 int ServiceGaiaAuthenticator::GetBackoffDelaySeconds( |
| 46 int current_backoff_delay) { |
| 47 const int kMaxBackoffDelaySeconds = 60 * 60; // (1 hour) |
| 48 int ret = 0; |
| 49 if (0 == current_backoff_delay) { |
| 50 ret = 1; |
| 51 } else { |
| 52 ret = current_backoff_delay * 2; |
| 53 } |
| 54 if (ret > kMaxBackoffDelaySeconds) { |
| 55 ret = kMaxBackoffDelaySeconds; |
| 56 } |
| 57 return ret; |
| 58 } |
| 59 |
| 60 void ServiceGaiaAuthenticator::DoPost(const GURL& post_url, |
| 61 const std::string& post_body) { |
| 62 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 63 request_.reset(new URLFetcher(post_url, URLFetcher::POST, this)); |
| 64 ServiceURLRequestContextGetter* context_getter = |
| 65 new ServiceURLRequestContextGetter(); |
| 66 request_->set_request_context(context_getter); |
| 67 request_->set_upload_data("application/x-www-form-urlencoded", post_body); |
| 68 request_->Start(); |
| 69 } |
| 70 |
| 71 // URLFetcher::Delegate implementation |
| 72 void ServiceGaiaAuthenticator::OnURLFetchComplete( |
| 73 const URLFetcher *source, const GURL &url, const URLRequestStatus &status, |
| 74 int response_code, const ResponseCookies &cookies, |
| 75 const std::string &data) { |
| 76 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 77 http_response_code_ = response_code; |
| 78 response_data_ = data; |
| 79 // Add an extra reference because we want http_post_completed_ to remain |
| 80 // valid until after Signal() returns. |
| 81 scoped_refptr<ServiceGaiaAuthenticator> keep_alive(this); |
| 82 // Wake the blocked thread in Post. |
| 83 http_post_completed_.Signal(); |
| 84 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 85 } |
| 86 |
| OLD | NEW |