diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-07 21:39:55 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-07 21:39:55 +0000 |
commit | 68d2a05fb6380f35eb10e7e1b9f789a742b89871 (patch) | |
tree | 74939817681c52027083ef86ebba375f047f5228 /chrome/browser/net | |
parent | 527498450ae8b478b6719d4603be03dfcdc8d812 (diff) | |
download | chromium_src-68d2a05fb6380f35eb10e7e1b9f789a742b89871.zip chromium_src-68d2a05fb6380f35eb10e7e1b9f789a742b89871.tar.gz chromium_src-68d2a05fb6380f35eb10e7e1b9f789a742b89871.tar.bz2 |
Moved URLFetcher and related classes to chrome/common/net.
BUG=None
TEST=Build and Unittests
Review URL: http://codereview.chromium.org/1995008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46739 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r-- | chrome/browser/net/chrome_url_request_context.h | 2 | ||||
-rw-r--r-- | chrome/browser/net/cookie_policy_browsertest.cc | 2 | ||||
-rw-r--r-- | chrome/browser/net/resolve_proxy_msg_helper.cc | 2 | ||||
-rw-r--r-- | chrome/browser/net/sdch_dictionary_fetcher.h | 2 | ||||
-rw-r--r-- | chrome/browser/net/test_url_fetcher_factory.cc | 27 | ||||
-rw-r--r-- | chrome/browser/net/test_url_fetcher_factory.h | 82 | ||||
-rw-r--r-- | chrome/browser/net/url_fetcher.cc | 361 | ||||
-rw-r--r-- | chrome/browser/net/url_fetcher.h | 195 | ||||
-rw-r--r-- | chrome/browser/net/url_fetcher_protect.cc | 176 | ||||
-rw-r--r-- | chrome/browser/net/url_fetcher_protect.h | 146 | ||||
-rw-r--r-- | chrome/browser/net/url_fetcher_unittest.cc | 613 | ||||
-rw-r--r-- | chrome/browser/net/url_request_context_getter.cc | 27 | ||||
-rw-r--r-- | chrome/browser/net/url_request_context_getter.h | 55 | ||||
-rw-r--r-- | chrome/browser/net/websocket_experiment/websocket_experiment_task.cc | 2 | ||||
-rw-r--r-- | chrome/browser/net/websocket_experiment/websocket_experiment_task.h | 2 |
15 files changed, 6 insertions, 1688 deletions
diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h index a42464f..c9e0f99 100644 --- a/chrome/browser/net/chrome_url_request_context.h +++ b/chrome/browser/net/chrome_url_request_context.h @@ -19,8 +19,8 @@ #include "chrome/browser/pref_service.h" #include "chrome/browser/privacy_blacklist/blacklist.h" #include "chrome/browser/net/chrome_cookie_policy.h" -#include "chrome/browser/net/url_request_context_getter.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/net/url_request_context_getter.h" #include "chrome/common/notification_registrar.h" #include "net/url_request/url_request_context.h" diff --git a/chrome/browser/net/cookie_policy_browsertest.cc b/chrome/browser/net/cookie_policy_browsertest.cc index 923f4c5..7a33ce9 100644 --- a/chrome/browser/net/cookie_policy_browsertest.cc +++ b/chrome/browser/net/cookie_policy_browsertest.cc @@ -4,8 +4,8 @@ #include "chrome/browser/browser.h" #include "chrome/browser/host_content_settings_map.h" -#include "chrome/browser/net/url_request_context_getter.h" #include "chrome/browser/profile.h" +#include "chrome/common/net/url_request_context_getter.h" #include "chrome/test/in_process_browser_test.h" #include "chrome/test/ui_test_utils.h" #include "net/base/mock_host_resolver.h" diff --git a/chrome/browser/net/resolve_proxy_msg_helper.cc b/chrome/browser/net/resolve_proxy_msg_helper.cc index d73f0fb..3ceab9a 100644 --- a/chrome/browser/net/resolve_proxy_msg_helper.cc +++ b/chrome/browser/net/resolve_proxy_msg_helper.cc @@ -6,7 +6,7 @@ #include "base/compiler_specific.h" #include "chrome/browser/profile.h" -#include "chrome/browser/net/url_request_context_getter.h" +#include "chrome/common/net/url_request_context_getter.h" #include "net/base/net_errors.h" #include "net/url_request/url_request_context.h" diff --git a/chrome/browser/net/sdch_dictionary_fetcher.h b/chrome/browser/net/sdch_dictionary_fetcher.h index 12c022a..3a975ce 100644 --- a/chrome/browser/net/sdch_dictionary_fetcher.h +++ b/chrome/browser/net/sdch_dictionary_fetcher.h @@ -16,7 +16,7 @@ #include "base/compiler_specific.h" #include "base/scoped_ptr.h" #include "base/task.h" -#include "chrome/browser/net/url_fetcher.h" +#include "chrome/common/net/url_fetcher.h" #include "net/base/sdch_manager.h" class SdchDictionaryFetcher : public URLFetcher::Delegate, diff --git a/chrome/browser/net/test_url_fetcher_factory.cc b/chrome/browser/net/test_url_fetcher_factory.cc deleted file mode 100644 index 7573646..0000000 --- a/chrome/browser/net/test_url_fetcher_factory.cc +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2009 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 "chrome/browser/net/test_url_fetcher_factory.h" - -TestURLFetcher::TestURLFetcher(const GURL& url, - URLFetcher::RequestType request_type, - URLFetcher::Delegate* d) - : URLFetcher(url, request_type, d), - original_url_(url) { -} - -URLFetcher* TestURLFetcherFactory::CreateURLFetcher( - int id, - const GURL& url, - URLFetcher::RequestType request_type, - URLFetcher::Delegate* d) { - TestURLFetcher* fetcher = new TestURLFetcher(url, request_type, d); - fetchers_[id] = fetcher; - return fetcher; -} - -TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { - Fetchers::const_iterator i = fetchers_.find(id); - return i == fetchers_.end() ? NULL : i->second; -} diff --git a/chrome/browser/net/test_url_fetcher_factory.h b/chrome/browser/net/test_url_fetcher_factory.h deleted file mode 100644 index a6ea7c4..0000000 --- a/chrome/browser/net/test_url_fetcher_factory.h +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2009 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. - -#ifndef CHROME_BROWSER_NET_TEST_URL_FETCHER_FACTORY_H_ -#define CHROME_BROWSER_NET_TEST_URL_FETCHER_FACTORY_H_ - -#include <map> -#include <string> - -#include "chrome/browser/net/url_fetcher.h" -#include "googleurl/src/gurl.h" - -// TestURLFetcher and TestURLFetcherFactory are used for testing consumers of -// URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates -// TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is -// expected that you'll grab the delegate from the TestURLFetcher and invoke -// the callback method when appropriate. In this way it's easy to mock a -// URLFetcher. -// Typical usage: -// // TestURLFetcher requires a MessageLoop: -// MessageLoopForUI message_loop; -// // Create and register factory. -// TestURLFetcherFactory factory; -// URLFetcher::set_factory(&factory); -// // Do something that triggers creation of a URLFetcher. -// TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); -// ASSERT(fetcher); -// // Notify delegate with whatever data you want. -// fetcher->delegate()->OnURLFetchComplete(...); -// // Make sure consumer of URLFetcher does the right thing. -// ... -// // Reset factory. -// URLFetcher::set_factory(NULL); - - -class TestURLFetcher : public URLFetcher { - public: - TestURLFetcher(const GURL& url, RequestType request_type, Delegate* d); - - // Returns the delegate installed on the URLFetcher. - Delegate* delegate() const { return URLFetcher::delegate(); } - - // Overriden to do nothing. It is assumed the caller will notify the delegate. - virtual void Start() {} - - // URL we were created with. Because of how we're using URLFetcher url() - // always returns an empty URL. Chances are you'll want to use original_url() - // in your tests. - const GURL& original_url() const { return original_url_; } - - // Returns the data uploaded on this URLFetcher. - const std::string& upload_data() const { return URLFetcher::upload_data(); } - - private: - const GURL original_url_; - - DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); -}; - -// Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers -// are registered in a map by the id passed to the create method. -class TestURLFetcherFactory : public URLFetcher::Factory { - public: - TestURLFetcherFactory() {} - - virtual URLFetcher* CreateURLFetcher(int id, - const GURL& url, - URLFetcher::RequestType request_type, - URLFetcher::Delegate* d); - - TestURLFetcher* GetFetcherByID(int id) const; - - private: - // Maps from id passed to create to the returned URLFetcher. - typedef std::map<int, TestURLFetcher*> Fetchers; - Fetchers fetchers_; - - DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); -}; - -#endif // CHROME_BROWSER_NET_TEST_URL_FETCHER_FACTORY_H_ diff --git a/chrome/browser/net/url_fetcher.cc b/chrome/browser/net/url_fetcher.cc deleted file mode 100644 index 9b4923d..0000000 --- a/chrome/browser/net/url_fetcher.cc +++ /dev/null @@ -1,361 +0,0 @@ -// Copyright (c) 2009 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 "chrome/browser/net/url_fetcher.h" - -#include "base/compiler_specific.h" -#include "base/message_loop_proxy.h" -#include "base/string_util.h" -#include "base/thread.h" -#include "chrome/browser/net/url_fetcher_protect.h" -#include "chrome/browser/net/url_request_context_getter.h" -#include "googleurl/src/gurl.h" -#include "net/base/load_flags.h" -#include "net/base/io_buffer.h" -#include "net/http/http_response_headers.h" -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_context.h" - -static const int kBufferSize = 4096; - -bool URLFetcher::g_interception_enabled = false; - -class URLFetcher::Core - : public base::RefCountedThreadSafe<URLFetcher::Core>, - public URLRequest::Delegate { - public: - // For POST requests, set |content_type| to the MIME type of the content - // and set |content| to the data to upload. |flags| are flags to apply to - // the load operation--these should be one or more of the LOAD_* flags - // defined in url_request.h. - Core(URLFetcher* fetcher, - const GURL& original_url, - RequestType request_type, - URLFetcher::Delegate* d); - - // Starts the load. It's important that this not happen in the constructor - // because it causes the IO thread to begin AddRef()ing and Release()ing - // us. If our caller hasn't had time to fully construct us and take a - // reference, the IO thread could interrupt things, run a task, Release() - // us, and destroy us, leaving the caller with an already-destroyed object - // when construction finishes. - void Start(); - - // Stops any in-progress load and ensures no callback will happen. It is - // safe to call this multiple times. - void Stop(); - - // URLRequest::Delegate implementations - virtual void OnResponseStarted(URLRequest* request); - virtual void OnReadCompleted(URLRequest* request, int bytes_read); - - URLFetcher::Delegate* delegate() const { return delegate_; } - - private: - friend class base::RefCountedThreadSafe<URLFetcher::Core>; - - ~Core() {} - - // Wrapper functions that allow us to ensure actions happen on the right - // thread. - void StartURLRequest(); - void CancelURLRequest(); - void OnCompletedURLRequest(const URLRequestStatus& status); - - URLFetcher* fetcher_; // Corresponding fetcher object - GURL original_url_; // The URL we were asked to fetch - GURL url_; // The URL we eventually wound up at - RequestType request_type_; // What type of request is this? - URLFetcher::Delegate* delegate_; // Object to notify on completion - MessageLoop* delegate_loop_; // Message loop of the creating thread - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; - // The message loop proxy for the thread - // on which the request IO happens. - URLRequest* request_; // The actual request this wraps - int load_flags_; // Flags for the load operation - int response_code_; // HTTP status code for the request - std::string data_; // Results of the request - scoped_refptr<net::IOBuffer> buffer_; - // Read buffer - scoped_refptr<URLRequestContextGetter> request_context_getter_; - // Cookie/cache info for the request - ResponseCookies cookies_; // Response cookies - std::string extra_request_headers_;// Extra headers for the request, if any - scoped_refptr<net::HttpResponseHeaders> response_headers_; - - std::string upload_content_; // HTTP POST payload - std::string upload_content_type_; // MIME type of POST payload - - // The overload protection entry for this URL. This is used to - // incrementally back off how rapidly we'll send requests to a particular - // URL, to avoid placing too much demand on the remote resource. We update - // this with the status of all requests as they return, and in turn use it - // to determine how long to wait before making another request. - URLFetcherProtectEntry* protect_entry_; - // |num_retries_| indicates how many times we've failed to successfully - // fetch this URL. Once this value exceeds the maximum number of retries - // specified by the protection manager, we'll give up. - int num_retries_; - - // True if the URLFetcher has been cancelled. - bool was_cancelled_; - - friend class URLFetcher; - DISALLOW_COPY_AND_ASSIGN(Core); -}; - -// static -URLFetcher::Factory* URLFetcher::factory_ = NULL; - -URLFetcher::URLFetcher(const GURL& url, - RequestType request_type, - Delegate* d) - : ALLOW_THIS_IN_INITIALIZER_LIST( - core_(new Core(this, url, request_type, d))), - automatically_retry_on_5xx_(true) { -} - -URLFetcher::~URLFetcher() { - core_->Stop(); -} - -// static -URLFetcher* URLFetcher::Create(int id, const GURL& url, - RequestType request_type, Delegate* d) { - return factory_ ? factory_->CreateURLFetcher(id, url, request_type, d) : - new URLFetcher(url, request_type, d); -} - -URLFetcher::Core::Core(URLFetcher* fetcher, - const GURL& original_url, - RequestType request_type, - URLFetcher::Delegate* d) - : fetcher_(fetcher), - original_url_(original_url), - request_type_(request_type), - delegate_(d), - delegate_loop_(MessageLoop::current()), - request_(NULL), - load_flags_(net::LOAD_NORMAL), - response_code_(-1), - buffer_(new net::IOBuffer(kBufferSize)), - protect_entry_(URLFetcherProtectManager::GetInstance()->Register( - original_url_.host())), - num_retries_(0), - was_cancelled_(false) { -} - -void URLFetcher::Core::Start() { - DCHECK(delegate_loop_); - CHECK(request_context_getter_) << "We need an URLRequestContext!"; - io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); - CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; - io_message_loop_proxy_->PostDelayedTask( - FROM_HERE, - NewRunnableMethod(this, &Core::StartURLRequest), - protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SEND)); -} - -void URLFetcher::Core::Stop() { - DCHECK_EQ(MessageLoop::current(), delegate_loop_); - delegate_ = NULL; - fetcher_ = NULL; - if (io_message_loop_proxy_.get()) { - io_message_loop_proxy_->PostTask( - FROM_HERE, NewRunnableMethod(this, &Core::CancelURLRequest)); - } -} - -void URLFetcher::Core::OnResponseStarted(URLRequest* request) { - DCHECK(request == request_); - DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); - if (request_->status().is_success()) { - response_code_ = request_->GetResponseCode(); - response_headers_ = request_->response_headers(); - } - - int bytes_read = 0; - // Some servers may treat HEAD requests as GET requests. To free up the - // network connection as soon as possible, signal that the request has - // completed immediately, without trying to read any data back (all we care - // about is the response code and headers, which we already have). - if (request_->status().is_success() && (request_type_ != HEAD)) - request_->Read(buffer_, kBufferSize, &bytes_read); - OnReadCompleted(request_, bytes_read); -} - -void URLFetcher::Core::OnReadCompleted(URLRequest* request, int bytes_read) { - DCHECK(request == request_); - DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); - - url_ = request->url(); - - do { - if (!request_->status().is_success() || bytes_read <= 0) - break; - data_.append(buffer_->data(), bytes_read); - } while (request_->Read(buffer_, kBufferSize, &bytes_read)); - - if (request_->status().is_success()) - request_->GetResponseCookies(&cookies_); - - // See comments re: HEAD requests in OnResponseStarted(). - if (!request_->status().is_io_pending() || (request_type_ == HEAD)) { - delegate_loop_->PostTask(FROM_HERE, NewRunnableMethod( - this, &Core::OnCompletedURLRequest, request_->status())); - delete request_; - request_ = NULL; - } -} - -void URLFetcher::Core::StartURLRequest() { - DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); - - if (was_cancelled_) { - // Since StartURLRequest() is posted as a *delayed* task, it may - // run after the URLFetcher was already stopped. - return; - } - - CHECK(request_context_getter_); - DCHECK(!request_); - - request_ = new URLRequest(original_url_, this); - int flags = request_->load_flags() | load_flags_; - if (!g_interception_enabled) { - flags = flags | net::LOAD_DISABLE_INTERCEPT; - } - request_->set_load_flags(flags); - request_->set_context(request_context_getter_->GetURLRequestContext()); - - switch (request_type_) { - case GET: - break; - - case POST: - DCHECK(!upload_content_.empty()); - DCHECK(!upload_content_type_.empty()); - - request_->set_method("POST"); - if (!extra_request_headers_.empty()) - extra_request_headers_ += "\r\n"; - StringAppendF(&extra_request_headers_, - "Content-Type: %s", upload_content_type_.c_str()); - request_->AppendBytesToUpload(upload_content_.data(), - static_cast<int>(upload_content_.size())); - break; - - case HEAD: - request_->set_method("HEAD"); - break; - - default: - NOTREACHED(); - } - - if (!extra_request_headers_.empty()) - request_->SetExtraRequestHeaders(extra_request_headers_); - - request_->Start(); -} - -void URLFetcher::Core::CancelURLRequest() { - DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); - - if (request_) { - request_->Cancel(); - delete request_; - request_ = NULL; - } - // Release the reference to the request context. There could be multiple - // references to URLFetcher::Core at this point so it may take a while to - // delete the object, but we cannot delay the destruction of the request - // context. - request_context_getter_ = NULL; - was_cancelled_ = true; -} - -void URLFetcher::Core::OnCompletedURLRequest(const URLRequestStatus& status) { - DCHECK(MessageLoop::current() == delegate_loop_); - - // Checks the response from server. - if (response_code_ >= 500) { - // When encountering a server error, we will send the request again - // after backoff time. - int64 back_off_time = - protect_entry_->UpdateBackoff(URLFetcherProtectEntry::FAILURE); - if (delegate_) { - fetcher_->backoff_delay_ = - base::TimeDelta::FromMilliseconds(back_off_time); - } - ++num_retries_; - // Restarts the request if we still need to notify the delegate. - if (delegate_) { - if (fetcher_->automatically_retry_on_5xx_ && - num_retries_ <= protect_entry_->max_retries()) { - io_message_loop_proxy_->PostDelayedTask( - FROM_HERE, - NewRunnableMethod(this, &Core::StartURLRequest), back_off_time); - } else { - delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, - cookies_, data_); - } - } - } else { - protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); - if (delegate_) { - fetcher_->backoff_delay_ = base::TimeDelta(); - delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, - cookies_, data_); - } - } -} - -void URLFetcher::set_upload_data(const std::string& upload_content_type, - const std::string& upload_content) { - core_->upload_content_type_ = upload_content_type; - core_->upload_content_ = upload_content; -} - -const std::string& URLFetcher::upload_data() const { - return core_->upload_content_; -} - -void URLFetcher::set_load_flags(int load_flags) { - core_->load_flags_ = load_flags; -} - -int URLFetcher::load_flags() const { - return core_->load_flags_; -} - -void URLFetcher::set_extra_request_headers( - const std::string& extra_request_headers) { - core_->extra_request_headers_ = extra_request_headers; -} - -void URLFetcher::set_request_context( - URLRequestContextGetter* request_context_getter) { - core_->request_context_getter_ = request_context_getter; -} - -void URLFetcher::set_automatcally_retry_on_5xx(bool retry) { - automatically_retry_on_5xx_ = retry; -} - -net::HttpResponseHeaders* URLFetcher::response_headers() const { - return core_->response_headers_; -} - -void URLFetcher::Start() { - core_->Start(); -} - -const GURL& URLFetcher::url() const { - return core_->url_; -} - -URLFetcher::Delegate* URLFetcher::delegate() const { - return core_->delegate(); -} diff --git a/chrome/browser/net/url_fetcher.h b/chrome/browser/net/url_fetcher.h deleted file mode 100644 index b585124..0000000 --- a/chrome/browser/net/url_fetcher.h +++ /dev/null @@ -1,195 +0,0 @@ -// Copyright (c) 2009 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. -// -// This file contains URLFetcher, a wrapper around URLRequest that handles -// low-level details like thread safety, ref counting, and incremental buffer -// reading. This is useful for callers who simply want to get the data from a -// URL and don't care about all the nitty-gritty details. - -#ifndef CHROME_BROWSER_NET_URL_FETCHER_H_ -#define CHROME_BROWSER_NET_URL_FETCHER_H_ - -#include <string> - -#include "base/leak_tracker.h" -#include "base/message_loop.h" -#include "base/ref_counted.h" -#include "base/time.h" - -class GURL; -typedef std::vector<std::string> ResponseCookies; -class URLFetcher; -class URLRequestContextGetter; -class URLRequestStatus; - -namespace net { -class HttpResponseHeaders; -} - -// To use this class, create an instance with the desired URL and a pointer to -// the object to be notified when the URL has been loaded: -// URLFetcher* fetcher = new URLFetcher("http://www.google.com", this); -// -// Then, optionally set properties on this object, like the request context or -// extra headers: -// fetcher->SetExtraRequestHeaders("X-Foo: bar"); -// -// Finally, start the request: -// fetcher->Start(); -// -// The object you supply as a delegate must inherit from URLFetcher::Delegate; -// when the fetch is completed, OnURLFetchComplete() will be called with the -// resulting status and (if applicable) HTTP response code. From that point -// until the original URLFetcher instance is destroyed, you may examine the -// provided status and data for the URL. (You should copy these objects if you -// need them to live longer than the URLFetcher instance.) If the URLFetcher -// instance is destroyed before the callback happens, the fetch will be -// canceled and no callback will occur. -// -// You may create the URLFetcher instance on any thread; OnURLFetchComplete() -// will be called back on the same thread you use to create the instance. -// -// -// NOTE: By default URLFetcher requests are NOT intercepted, except when -// interception is explicitly enabled in tests. - -class URLFetcher { - public: - enum RequestType { - GET, - POST, - HEAD, - }; - - class Delegate { - public: - // This will be called when the URL has been fetched, successfully or not. - // |response_code| is the HTTP response code (200, 404, etc.) if - // applicable. |url|, |status| and |data| are all valid until the - // URLFetcher instance is destroyed. - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) = 0; - }; - - // URLFetcher::Create uses the currently registered Factory to create the - // URLFetcher. Factory is intended for testing. - class Factory { - public: - virtual URLFetcher* CreateURLFetcher(int id, - const GURL& url, - RequestType request_type, - Delegate* d) = 0; - }; - - // |url| is the URL to send the request to. - // |request_type| is the type of request to make. - // |d| the object that will receive the callback on fetch completion. - URLFetcher(const GURL& url, RequestType request_type, Delegate* d); - - virtual ~URLFetcher(); - - // Sets the factory used by the static method Create to create a URLFetcher. - // URLFetcher does not take ownership of |factory|. A value of NULL results - // in a URLFetcher being created directly. -#if defined(UNIT_TEST) - static void set_factory(Factory* factory) { factory_ = factory; } -#endif - - // Normally interception is disabled for URLFetcher, but you can use this - // to enable it for tests. Also see the set_factory method for another way - // of testing code that uses an URLFetcher. - static void enable_interception_for_tests(bool enabled) { - g_interception_enabled = enabled; - } - - // Creates a URLFetcher, ownership returns to the caller. If there is no - // Factory (the default) this creates and returns a new URLFetcher. See the - // constructor for a description of the args. |id| may be used during testing - // to identify who is creating the URLFetcher. - static URLFetcher* Create(int id, const GURL& url, RequestType request_type, - Delegate* d); - - // Sets data only needed by POSTs. All callers making POST requests should - // call this before the request is started. |upload_content_type| is the MIME - // type of the content, while |upload_content| is the data to be sent (the - // Content-Length header value will be set to the length of this data). - void set_upload_data(const std::string& upload_content_type, - const std::string& upload_content); - - // Set one or more load flags as defined in net/base/load_flags.h. Must be - // called before the request is started. - void set_load_flags(int load_flags); - - // Returns the current load flags. - int load_flags() const; - - // Set extra headers on the request. Must be called before the request - // is started. - void set_extra_request_headers(const std::string& extra_request_headers); - - // Set the URLRequestContext on the request. Must be called before the - // request is started. - void set_request_context(URLRequestContextGetter* request_context_getter); - - // If |retry| is false, 5xx responses will be propagated to the observer, - // if it is true URLFetcher will automatically re-execute the request, - // after backoff_delay() elapses. URLFetcher has it set to true by default. - void set_automatcally_retry_on_5xx(bool retry); - - // Returns the back-off delay before the request will be retried, - // when a 5xx response was received. - base::TimeDelta backoff_delay() const { return backoff_delay_; } - - // Sets the back-off delay, allowing to mock 5xx requests in unit-tests. -#if defined(UNIT_TEST) - void set_backoff_delay(base::TimeDelta backoff_delay) { - backoff_delay_ = backoff_delay; - } -#endif // defined(UNIT_TEST) - - // Retrieve the response headers from the request. Must only be called after - // the OnURLFetchComplete callback has run. - virtual net::HttpResponseHeaders* response_headers() const; - - // Start the request. After this is called, you may not change any other - // settings. - virtual void Start(); - - // Return the URL that this fetcher is processing. - const GURL& url() const; - - protected: - // Returns the delegate. - Delegate* delegate() const; - - // Used by tests. - const std::string& upload_data() const; - - private: - class Core; - - scoped_refptr<Core> core_; - - static Factory* factory_; - - base::LeakTracker<URLFetcher> leak_tracker_; - - // If |automatically_retry_on_5xx_| is false, 5xx responses will be - // propagated to the observer, if it is true URLFetcher will automatically - // re-execute the request, after the back-off delay has expired. - // true by default. - bool automatically_retry_on_5xx_; - // Back-off time delay. 0 by default. - base::TimeDelta backoff_delay_; - - static bool g_interception_enabled; - - DISALLOW_EVIL_CONSTRUCTORS(URLFetcher); -}; - -#endif // CHROME_BROWSER_NET_URL_FETCHER_H_ diff --git a/chrome/browser/net/url_fetcher_protect.cc b/chrome/browser/net/url_fetcher_protect.cc deleted file mode 100644 index 748a675..0000000 --- a/chrome/browser/net/url_fetcher_protect.cc +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright (c) 2006-2008 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 "chrome/browser/net/url_fetcher_protect.h" - -#include "base/logging.h" - -// URLFetcherProtectEntry ---------------------------------------------------- - -using base::TimeDelta; -using base::TimeTicks; - -// Default parameters. Time is in milliseconds. -// static -const int URLFetcherProtectEntry::kDefaultSlidingWindowPeriod = 2000; - -const int URLFetcherProtectEntry::kDefaultMaxSendThreshold = 20; -const int URLFetcherProtectEntry::kDefaultMaxRetries = 0; - -const int URLFetcherProtectEntry::kDefaultInitialTimeout = 100; -const double URLFetcherProtectEntry::kDefaultMultiplier = 2.0; -const int URLFetcherProtectEntry::kDefaultConstantFactor = 100; -const int URLFetcherProtectEntry::kDefaultMaximumTimeout = 60000; - - -URLFetcherProtectEntry::URLFetcherProtectEntry() - : sliding_window_period_(kDefaultSlidingWindowPeriod), - max_send_threshold_(kDefaultMaxSendThreshold), - max_retries_(kDefaultMaxRetries), - initial_timeout_(kDefaultInitialTimeout), - multiplier_(kDefaultMultiplier), - constant_factor_(kDefaultConstantFactor), - maximum_timeout_(kDefaultMaximumTimeout) { - ResetBackoff(); -} - -URLFetcherProtectEntry::URLFetcherProtectEntry(int sliding_window_period, - int max_send_threshold, - int max_retries, - int initial_timeout, - double multiplier, - int constant_factor, - int maximum_timeout) - : sliding_window_period_(sliding_window_period), - max_send_threshold_(max_send_threshold), - max_retries_(max_retries), - initial_timeout_(initial_timeout), - multiplier_(multiplier), - constant_factor_(constant_factor), - maximum_timeout_(maximum_timeout) { - ResetBackoff(); -} - -int64 URLFetcherProtectEntry::UpdateBackoff(EventType event_type) { - // request may be sent in different threads - AutoLock lock(lock_); - - TimeDelta t; - switch (event_type) { - case SEND: - t = AntiOverload(); - break; - case SUCCESS: - t = ResetBackoff(); - break; - case FAILURE: - t = IncreaseBackoff(); - break; - default: - NOTREACHED(); - } - - int64 wait = t.InMilliseconds(); - DCHECK(wait >= 0); - return wait; -} - -TimeDelta URLFetcherProtectEntry::AntiOverload() { - TimeDelta sw = TimeDelta::FromMilliseconds(sliding_window_period_); - TimeTicks now = TimeTicks::Now(); - // Estimate when the next request will be sent. - release_time_ = now; - if (send_log_.size() > 0) { - release_time_ = std::max(release_time_, send_log_.back()); - } - // Checks if there are too many send events in recent time. - if (send_log_.size() >= static_cast<unsigned>(max_send_threshold_)) { - release_time_ = std::max(release_time_, send_log_.front() + sw); - } - // Logs the new send event. - send_log_.push(release_time_); - // Drops the out-of-date events in the event list. - while (!send_log_.empty() && - (send_log_.front() + sw <= send_log_.back())) { - send_log_.pop(); - } - return release_time_ - now; -} - -TimeDelta URLFetcherProtectEntry::ResetBackoff() { - timeout_period_ = initial_timeout_; - release_time_ = TimeTicks::Now(); - return TimeDelta::FromMilliseconds(0); -} - -TimeDelta URLFetcherProtectEntry::IncreaseBackoff() { - TimeTicks now = TimeTicks::Now(); - - release_time_ = std::max(release_time_, now) + - TimeDelta::FromMilliseconds(timeout_period_); - - // Calculates the new backoff time. - timeout_period_ = static_cast<int> - (multiplier_ * timeout_period_ + constant_factor_); - if (maximum_timeout_ && timeout_period_ > maximum_timeout_) - timeout_period_ = maximum_timeout_; - - return release_time_ - now; -} - -// URLFetcherProtectManager -------------------------------------------------- - -// static -scoped_ptr<URLFetcherProtectManager> URLFetcherProtectManager::protect_manager_; -Lock URLFetcherProtectManager::lock_; - -URLFetcherProtectManager::~URLFetcherProtectManager() { - // Deletes all entries - ProtectService::iterator i; - for (i = services_.begin(); i != services_.end(); ++i) { - if (i->second) - delete i->second; - } -} - -// static -URLFetcherProtectManager* URLFetcherProtectManager::GetInstance() { - AutoLock lock(lock_); - - if (protect_manager_.get() == NULL) { - protect_manager_.reset(new URLFetcherProtectManager()); - } - return protect_manager_.get(); -} - -URLFetcherProtectEntry* URLFetcherProtectManager::Register( - const std::string& id) { - AutoLock lock(lock_); - - ProtectService::iterator i = services_.find(id); - - if (i != services_.end()) { - // The entry exists. - return i->second; - } - - // Creates a new entry. - URLFetcherProtectEntry* entry = new URLFetcherProtectEntry(); - services_[id] = entry; - return entry; -} - -URLFetcherProtectEntry* URLFetcherProtectManager::Register( - const std::string& id, URLFetcherProtectEntry* entry) { - AutoLock lock(lock_); - - ProtectService::iterator i = services_.find(id); - if (i != services_.end()) { - // The entry exists. - delete i->second; - } - - services_[id] = entry; - return entry; -} diff --git a/chrome/browser/net/url_fetcher_protect.h b/chrome/browser/net/url_fetcher_protect.h deleted file mode 100644 index f47e0d6..0000000 --- a/chrome/browser/net/url_fetcher_protect.h +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright (c) 2006-2008 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. -// -// This file implements backoff in the suggest system so that we don't -// DOS the Suggest servers when using URLFetcher. - -#ifndef CHROME_BROWSER_NET_URL_FETCHER_PROTECT_H_ -#define CHROME_BROWSER_NET_URL_FETCHER_PROTECT_H_ - -#include <map> -#include <queue> -#include <string> - -#include "base/lock.h" -#include "base/scoped_ptr.h" -#include "base/time.h" - - -// This class is used to manage one service's rate protection. It maintains -// a queue of connection successes and failures and analyzes the requests -// over some period of time, in order to deduce the backoff time of every -// request. -// The backoff algorithm consists of two parts. Firstly, avoid too many -// send events in a sliding window. That will prevent traffic overload. -// Secondly, exponential backoff is used when receiving an error message -// from server. Exponential backoff period is calculated using the following -// formula: -// -// initial backoff time (the first time to receive error) -// backoff = k * current_backoff + c (the second, third, ... error) -// maximum backoff time (when backoff > maximum backoff time) -// -// where |k| is the multiplier, and |c| is the constant factor. -class URLFetcherProtectEntry { - public: - enum EventType { - SEND, // request will be sent out - SUCCESS, // successful response - FAILURE // no response or error - }; - - URLFetcherProtectEntry(); - URLFetcherProtectEntry(int sliding_window_period, int max_send_threshold, - int max_retries, int initial_timeout, - double multiplier, int constant_factor, - int maximum_timeout); - - - virtual ~URLFetcherProtectEntry() { } - - // When a connection event happens, log it to the queue, and recalculate - // the timeout period. It returns the backoff time, in milliseconds, that - // indicates to the sender how long should it wait before sending the request. - // If the request is allowed to be sent immediately, the backoff time is 0. - int64 UpdateBackoff(EventType event_type); - - // Returns the max retries allowed. - int max_retries() const { - return max_retries_; - } - - private: - // When a request comes, calculate the release time for it. - // Returns the backoff time before sending. - base::TimeDelta AntiOverload(); - // Resets backoff when service is ok. - // Returns the backoff time before sending. - base::TimeDelta ResetBackoff(); - // Calculates new backoff when encountering a failure. - // Returns the backoff time before sending. - base::TimeDelta IncreaseBackoff(); - - // Default parameters. Time is in milliseconds. - static const int kDefaultSlidingWindowPeriod; - static const int kDefaultMaxSendThreshold; - static const int kDefaultMaxRetries; - static const int kDefaultInitialTimeout; - static const double kDefaultMultiplier; - static const int kDefaultConstantFactor; - static const int kDefaultMaximumTimeout; - - // time to consider events when checking backoff - int sliding_window_period_; - - // maximum number of requests allowed in sliding window period - int max_send_threshold_; - // maximum retris allowed - int max_retries_; - - // initial timeout on first failure - int initial_timeout_; - // factor by which to multiply on exponential backoff (e.g., 2.0) - double multiplier_; - // constant time term to add to each attempt - int constant_factor_; - // maximum amount of time between requests - int maximum_timeout_; - - // current exponential backoff period - int timeout_period_; - // time that protection is scheduled to end - base::TimeTicks release_time_; - - // Sets up a lock to ensure thread safe. - Lock lock_; - - // A list of the recent send events. We ues them to decide whether - // there are too many requests sent in sliding window. - std::queue<base::TimeTicks> send_log_; - - DISALLOW_COPY_AND_ASSIGN(URLFetcherProtectEntry); -}; - - -// This singleton class is used to manage all protect entries. -// Now we use the host name as service id. -class URLFetcherProtectManager { - public: - ~URLFetcherProtectManager(); - - // Returns the global instance of this class. - static URLFetcherProtectManager* GetInstance(); - - // Registers a new entry in this service. If the entry already exists, - // just returns it. Ownership of the return object remains with the manager. - URLFetcherProtectEntry* Register(const std::string& id); - // Always registers the entry even when it exists; any existing entry for this - // id will be deleted and existing references to it will become invalid. - // Ownership of the return object remains with the manager. - URLFetcherProtectEntry* Register(const std::string& id, - URLFetcherProtectEntry* entry); - - private: - URLFetcherProtectManager() { } - - typedef std::map<const std::string, URLFetcherProtectEntry*> ProtectService; - - static Lock lock_; - static scoped_ptr<URLFetcherProtectManager> protect_manager_; - ProtectService services_; - - DISALLOW_COPY_AND_ASSIGN(URLFetcherProtectManager); -}; - -#endif // CHROME_BROWSER_NET_URL_FETCHER_PROTECT_H_ diff --git a/chrome/browser/net/url_fetcher_unittest.cc b/chrome/browser/net/url_fetcher_unittest.cc deleted file mode 100644 index 2a35b04..0000000 --- a/chrome/browser/net/url_fetcher_unittest.cc +++ /dev/null @@ -1,613 +0,0 @@ -// Copyright (c) 2006-2008 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 "base/message_loop_proxy.h" -#include "base/thread.h" -#include "base/waitable_event.h" -#include "chrome/browser/net/url_fetcher.h" -#include "chrome/browser/net/url_fetcher_protect.h" -#include "chrome/browser/net/url_request_context_getter.h" -#include "chrome/common/chrome_plugin_lib.h" -#include "net/http/http_response_headers.h" -#include "net/socket/ssl_test_util.h" -#include "net/url_request/url_request_unittest.h" -#include "testing/gtest/include/gtest/gtest.h" - -using base::Time; -using base::TimeDelta; - -// TODO(eroman): Add a regression test for http://crbug.com/40505. - -namespace { - -const wchar_t kDocRoot[] = L"chrome/test/data"; - -class TestURLRequestContextGetter : public URLRequestContextGetter { - public: - explicit TestURLRequestContextGetter( - base::MessageLoopProxy* io_message_loop_proxy) - : io_message_loop_proxy_(io_message_loop_proxy) { - } - virtual URLRequestContext* GetURLRequestContext() { - if (!context_) - context_ = new TestURLRequestContext(); - return context_; - } - virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() { - return io_message_loop_proxy_; - } - - protected: - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; - - private: - ~TestURLRequestContextGetter() {} - - scoped_refptr<URLRequestContext> context_; -}; - -class URLFetcherTest : public testing::Test, public URLFetcher::Delegate { - public: - URLFetcherTest() : fetcher_(NULL) { } - - // Creates a URLFetcher, using the program's main thread to do IO. - virtual void CreateFetcher(const GURL& url); - - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { - return io_message_loop_proxy_; - } - - protected: - virtual void SetUp() { - testing::Test::SetUp(); - - io_message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); - - // Ensure that any plugin operations done by other tests are cleaned up. - ChromePluginLib::UnloadAllPlugins(); - } - - // URLFetcher is designed to run on the main UI thread, but in our tests - // we assume that the current thread is the IO thread where the URLFetcher - // dispatches its requests to. When we wish to simulate being used from - // a UI thread, we dispatch a worker thread to do so. - MessageLoopForIO io_loop_; - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; - - URLFetcher* fetcher_; -}; - -// Version of URLFetcherTest that does a POST instead -class URLFetcherPostTest : public URLFetcherTest { - public: - virtual void CreateFetcher(const GURL& url); - - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); -}; - -// Version of URLFetcherTest that tests headers. -class URLFetcherHeadersTest : public URLFetcherTest { - public: - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); -}; - -// Version of URLFetcherTest that tests overload protection. -class URLFetcherProtectTest : public URLFetcherTest { - public: - virtual void CreateFetcher(const GURL& url); - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - private: - Time start_time_; -}; - -// Version of URLFetcherTest that tests overload protection, when responses -// passed through. -class URLFetcherProtectTestPassedThrough : public URLFetcherTest { - public: - virtual void CreateFetcher(const GURL& url); - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - private: - Time start_time_; -}; - -// Version of URLFetcherTest that tests bad HTTPS requests. -class URLFetcherBadHTTPSTest : public URLFetcherTest { - public: - URLFetcherBadHTTPSTest(); - - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - - private: - FilePath cert_dir_; -}; - -// Version of URLFetcherTest that tests request cancellation on shutdown. -class URLFetcherCancelTest : public URLFetcherTest { - public: - virtual void CreateFetcher(const GURL& url); - // URLFetcher::Delegate - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - - void CancelRequest(); -}; - -// Version of TestURLRequestContext that posts a Quit task to the IO -// thread once it is deleted. -class CancelTestURLRequestContext : public TestURLRequestContext { - virtual ~CancelTestURLRequestContext() { - // The d'tor should execute in the IO thread. Post the quit task to the - // current thread. - MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - } -}; - -class CancelTestURLRequestContextGetter : public URLRequestContextGetter { - public: - explicit CancelTestURLRequestContextGetter( - base::MessageLoopProxy* io_message_loop_proxy) - : io_message_loop_proxy_(io_message_loop_proxy), - context_created_(false, false) { - } - virtual URLRequestContext* GetURLRequestContext() { - if (!context_) { - context_ = new CancelTestURLRequestContext(); - context_created_.Signal(); - } - return context_; - } - virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() { - return io_message_loop_proxy_; - } - void WaitForContextCreation() { - context_created_.Wait(); - } - - private: - ~CancelTestURLRequestContextGetter() {} - - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; - base::WaitableEvent context_created_; - scoped_refptr<URLRequestContext> context_; -}; - -// Wrapper that lets us call CreateFetcher() on a thread of our choice. We -// could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call -// CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit -// confusing in that case because GTest doesn't know about the refcounting. -// It's less confusing to just do it this way. -class FetcherWrapperTask : public Task { - public: - FetcherWrapperTask(URLFetcherTest* test, const GURL& url) - : test_(test), url_(url) { } - virtual void Run() { - test_->CreateFetcher(url_); - } - - private: - URLFetcherTest* test_; - GURL url_; -}; - -void URLFetcherTest::CreateFetcher(const GURL& url) { - fetcher_ = new URLFetcher(url, URLFetcher::GET, this); - fetcher_->set_request_context(new TestURLRequestContextGetter( - io_message_loop_proxy())); - fetcher_->Start(); -} - -void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - EXPECT_TRUE(status.is_success()); - EXPECT_EQ(200, response_code); // HTTP OK - EXPECT_FALSE(data.empty()); - - delete fetcher_; // Have to delete this here and not in the destructor, - // because the destructor won't necessarily run on the - // same thread that CreateFetcher() did. - - io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - // If the current message loop is not the IO loop, it will be shut down when - // the main loop returns and this thread subsequently goes out of scope. -} - -void URLFetcherPostTest::CreateFetcher(const GURL& url) { - fetcher_ = new URLFetcher(url, URLFetcher::POST, this); - fetcher_->set_request_context(new TestURLRequestContextGetter( - io_message_loop_proxy())); - fetcher_->set_upload_data("application/x-www-form-urlencoded", - "bobsyeruncle"); - fetcher_->Start(); -} - -void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - EXPECT_EQ(std::string("bobsyeruncle"), data); - URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, - cookies, data); -} - -void URLFetcherHeadersTest::OnURLFetchComplete( - const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - std::string header; - EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", - &header)); - EXPECT_EQ("private", header); - URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, - cookies, data); -} - -void URLFetcherProtectTest::CreateFetcher(const GURL& url) { - fetcher_ = new URLFetcher(url, URLFetcher::GET, this); - fetcher_->set_request_context(new TestURLRequestContextGetter( - io_message_loop_proxy())); - start_time_ = Time::Now(); - fetcher_->Start(); -} - -void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); - if (response_code >= 500) { - // Now running ServerUnavailable test. - // It takes more than 1 second to finish all 11 requests. - EXPECT_TRUE(Time::Now() - start_time_ >= one_second); - EXPECT_TRUE(status.is_success()); - EXPECT_FALSE(data.empty()); - delete fetcher_; - io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - } else { - // Now running Overload test. - static int count = 0; - count++; - if (count < 20) { - fetcher_->Start(); - } else { - // We have already sent 20 requests continuously. And we expect that - // it takes more than 1 second due to the overload pretection settings. - EXPECT_TRUE(Time::Now() - start_time_ >= one_second); - URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, - cookies, data); - } - } -} - -void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { - fetcher_ = new URLFetcher(url, URLFetcher::GET, this); - fetcher_->set_request_context(new TestURLRequestContextGetter( - io_message_loop_proxy())); - fetcher_->set_automatcally_retry_on_5xx(false); - start_time_ = Time::Now(); - fetcher_->Start(); -} - -void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( - const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); - if (response_code >= 500) { - // Now running ServerUnavailable test. - // It should get here on the first attempt, so almost immediately and - // *not* to attempt to execute all 11 requests (2.5 minutes). - EXPECT_TRUE(Time::Now() - start_time_ < one_minute); - EXPECT_TRUE(status.is_success()); - // Check that suggested back off time is bigger than 0. - EXPECT_GT(fetcher_->backoff_delay().InMicroseconds(), 0); - EXPECT_FALSE(data.empty()); - delete fetcher_; - io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - } else { - // We should not get here! - FAIL(); - } -} - - -URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { - PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); - cert_dir_ = cert_dir_.AppendASCII("chrome"); - cert_dir_ = cert_dir_.AppendASCII("test"); - cert_dir_ = cert_dir_.AppendASCII("data"); - cert_dir_ = cert_dir_.AppendASCII("ssl"); - cert_dir_ = cert_dir_.AppendASCII("certificates"); -} - -// The "server certificate expired" error should result in automatic -// cancellation of the request by -// URLRequest::Delegate::OnSSLCertificateError. -void URLFetcherBadHTTPSTest::OnURLFetchComplete( - const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - // This part is different from URLFetcherTest::OnURLFetchComplete - // because this test expects the request to be cancelled. - EXPECT_EQ(URLRequestStatus::CANCELED, status.status()); - EXPECT_EQ(net::ERR_ABORTED, status.os_error()); - EXPECT_EQ(-1, response_code); - EXPECT_TRUE(cookies.empty()); - EXPECT_TRUE(data.empty()); - - // The rest is the same as URLFetcherTest::OnURLFetchComplete. - delete fetcher_; - io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); -} - -void URLFetcherCancelTest::CreateFetcher(const GURL& url) { - fetcher_ = new URLFetcher(url, URLFetcher::GET, this); - CancelTestURLRequestContextGetter* context_getter = - new CancelTestURLRequestContextGetter(io_message_loop_proxy()); - fetcher_->set_request_context(context_getter); - fetcher_->Start(); - // We need to wait for the creation of the URLRequestContext, since we - // rely on it being destroyed as a signal to end the test. - context_getter->WaitForContextCreation(); - CancelRequest(); -} - -void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - // We should have cancelled the request before completion. - ADD_FAILURE(); - delete fetcher_; - io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); -} - -void URLFetcherCancelTest::CancelRequest() { - delete fetcher_; - // The URLFetcher's test context will post a Quit task once it is - // deleted. So if this test simply hangs, it means cancellation - // did not work. -} - -TEST_F(URLFetcherTest, SameThreadsTest) { - // Create the fetcher on the main thread. Since IO will happen on the main - // thread, this will test URLFetcher's ability to do everything on one - // thread. - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(kDocRoot, NULL); - ASSERT_TRUE(NULL != server.get()); - - CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherTest, DifferentThreadsTest) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(kDocRoot, NULL); - ASSERT_TRUE(NULL != server.get()); - // Create a separate thread that will create the URLFetcher. The current - // (main) thread will do the IO, and when the fetch is complete it will - // terminate the main thread's message loop; then the other thread's - // message loop will be shut down automatically as the thread goes out of - // scope. - base::Thread t("URLFetcher test thread"); - ASSERT_TRUE(t.Start()); - t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, - GURL(server->TestServerPage("defaultresponse")))); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherPostTest, Basic) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(kDocRoot, NULL); - ASSERT_TRUE(NULL != server.get()); - CreateFetcher(GURL(server->TestServerPage("echo"))); - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherHeadersTest, DISABLED_Headers) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); - ASSERT_TRUE(NULL != server.get()); - CreateFetcher(GURL(server->TestServerPage("files/with-headers.html"))); - MessageLoop::current()->Run(); - // The actual tests are in the URLFetcherHeadersTest fixture. -} - -TEST_F(URLFetcherProtectTest, Overload) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(kDocRoot, NULL); - ASSERT_TRUE(NULL != server.get()); - GURL url = GURL(server->TestServerPage("defaultresponse")); - - // Registers an entry for test url. It only allows 3 requests to be sent - // in 200 milliseconds. - URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); - URLFetcherProtectEntry* entry = - new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); - manager->Register(url.host(), entry); - - CreateFetcher(url); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherProtectTest, ServerUnavailable) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"chrome/test/data", NULL); - ASSERT_TRUE(NULL != server.get()); - GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); - - // Registers an entry for test url. The backoff time is calculated by: - // new_backoff = 2.0 * old_backoff + 0 - // and maximum backoff time is 256 milliseconds. - // Maximum retries allowed is set to 11. - URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); - URLFetcherProtectEntry* entry = - new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); - manager->Register(url.host(), entry); - - CreateFetcher(url); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"chrome/test/data", NULL); - ASSERT_TRUE(NULL != server.get()); - GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); - - // Registers an entry for test url. The backoff time is calculated by: - // new_backoff = 2.0 * old_backoff + 0 - // and maximum backoff time is 256 milliseconds. - // Maximum retries allowed is set to 11. - URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); - // Total time if *not* for not doing automatic backoff would be 150s. - // In reality it should be "as soon as server responds". - URLFetcherProtectEntry* entry = - new URLFetcherProtectEntry(200, 3, 11, 100, 2.0, 0, 150000); - manager->Register(url.host(), entry); - - CreateFetcher(url); - - MessageLoop::current()->Run(); -} - - -TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { - scoped_refptr<HTTPSTestServer> server = - HTTPSTestServer::CreateExpiredServer(kDocRoot); - ASSERT_TRUE(NULL != server.get()); - - CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherCancelTest, ReleasesContext) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"chrome/test/data", NULL); - ASSERT_TRUE(NULL != server.get()); - GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); - - // Registers an entry for test url. The backoff time is calculated by: - // new_backoff = 2.0 * old_backoff + 0 - // The initial backoff is 2 seconds and maximum backoff is 4 seconds. - // Maximum retries allowed is set to 2. - URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); - URLFetcherProtectEntry* entry = - new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000); - manager->Register(url.host(), entry); - - // Create a separate thread that will create the URLFetcher. The current - // (main) thread will do the IO, and when the fetch is complete it will - // terminate the main thread's message loop; then the other thread's - // message loop will be shut down automatically as the thread goes out of - // scope. - base::Thread t("URLFetcher test thread"); - ASSERT_TRUE(t.Start()); - t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); - - MessageLoop::current()->Run(); -} - -TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { - scoped_refptr<HTTPTestServer> server = - HTTPTestServer::CreateServer(L"chrome/test/data", NULL); - ASSERT_TRUE(NULL != server.get()); - GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); - - // Register an entry for test url. - // - // Ideally we would mock URLFetcherProtectEntry to return XXX seconds - // in response to entry->UpdateBackoff(SEND). - // - // Unfortunately this function is time sensitive, so we fudge some numbers - // to make it at least somewhat likely to have a non-zero deferred - // delay when running. - // - // Using a sliding window of 2 seconds, and max of 1 request, under a fast - // run we expect to have a 4 second delay when posting the Start task. - URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); - URLFetcherProtectEntry* entry = - new URLFetcherProtectEntry(2000, 1, 2, 2000, 2.0, 0, 4000); - EXPECT_EQ(0, entry->UpdateBackoff(URLFetcherProtectEntry::SEND)); - entry->UpdateBackoff(URLFetcherProtectEntry::SEND); // Returns about 2000. - manager->Register(url.host(), entry); - - // The next request we try to send will be delayed by ~4 seconds. - // The slower the test runs, the less the delay will be (since it takes the - // time difference from now). - - base::Thread t("URLFetcher test thread"); - ASSERT_TRUE(t.Start()); - t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); - - MessageLoop::current()->Run(); -} - -} // namespace. diff --git a/chrome/browser/net/url_request_context_getter.cc b/chrome/browser/net/url_request_context_getter.cc deleted file mode 100644 index 64198b5..0000000 --- a/chrome/browser/net/url_request_context_getter.cc +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2009 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 "base/message_loop_proxy.h" -#include "chrome/browser/net/url_request_context_getter.h" -#include "net/url_request/url_request_context.h" - -net::CookieStore* URLRequestContextGetter::GetCookieStore() { - return GetURLRequestContext()->cookie_store(); -} - -void URLRequestContextGetter::OnDestruct() { - scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy = - GetIOMessageLoopProxy(); - DCHECK(io_message_loop_proxy); - if (io_message_loop_proxy) { - if (io_message_loop_proxy->BelongsToCurrentThread()) { - delete this; - } else { - io_message_loop_proxy->DeleteSoon(FROM_HERE, this); - } - } - // If no IO message loop proxy was available, we will just leak memory. - // This is also true if the IO thread is gone. -} - diff --git a/chrome/browser/net/url_request_context_getter.h b/chrome/browser/net/url_request_context_getter.h deleted file mode 100644 index 2cedeb5..0000000 --- a/chrome/browser/net/url_request_context_getter.h +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2009 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. - -#ifndef CHROME_BROWSER_NET_URL_REQUEST_CONTEXT_GETTER_H_ -#define CHROME_BROWSER_NET_URL_REQUEST_CONTEXT_GETTER_H_ - -#include "base/ref_counted.h" -#include "base/task.h" - -namespace base { -class MessageLoopProxy; -} - -namespace net { -class CookieStore; -} - -class URLRequestContext; -struct URLRequestContextGetterTraits; - -// Interface for retrieving an URLRequestContext. -class URLRequestContextGetter - : public base::RefCountedThreadSafe<URLRequestContextGetter, - URLRequestContextGetterTraits> { - public: - virtual URLRequestContext* GetURLRequestContext() = 0; - - // Defaults to GetURLRequestContext()->cookie_store(), but - // implementations can override it. - virtual net::CookieStore* GetCookieStore(); - // Returns a MessageLoopProxy corresponding to the thread on which the - // request IO happens (the thread on which the returned URLRequestContext - // may be used). - virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() = 0; - - protected: - friend class DeleteTask<URLRequestContextGetter>; - friend struct URLRequestContextGetterTraits; - - virtual ~URLRequestContextGetter() {} - private: - // OnDestruct is meant to ensure deletion on the thread on which the request - // IO happens. - void OnDestruct(); -}; - -struct URLRequestContextGetterTraits { - static void Destruct(URLRequestContextGetter* context_getter) { - context_getter->OnDestruct(); - } -}; - -#endif // CHROME_BROWSER_NET_URL_REQUEST_CONTEXT_GETTER_H_ - diff --git a/chrome/browser/net/websocket_experiment/websocket_experiment_task.cc b/chrome/browser/net/websocket_experiment/websocket_experiment_task.cc index 8f0b7f1..0474e12 100644 --- a/chrome/browser/net/websocket_experiment/websocket_experiment_task.cc +++ b/chrome/browser/net/websocket_experiment/websocket_experiment_task.cc @@ -7,8 +7,8 @@ #include "base/hash_tables.h" #include "base/histogram.h" #include "chrome/browser/chrome_thread.h" -#include "chrome/browser/net/url_request_context_getter.h" #include "chrome/browser/profile.h" +#include "chrome/common/net/url_request_context_getter.h" #include "net/base/load_flags.h" #include "net/base/net_errors.h" #include "net/websockets/websocket.h" diff --git a/chrome/browser/net/websocket_experiment/websocket_experiment_task.h b/chrome/browser/net/websocket_experiment/websocket_experiment_task.h index e3ec500..fc62fd7 100644 --- a/chrome/browser/net/websocket_experiment/websocket_experiment_task.h +++ b/chrome/browser/net/websocket_experiment/websocket_experiment_task.h @@ -40,7 +40,7 @@ #include "base/basictypes.h" #include "base/task.h" #include "base/time.h" -#include "chrome/browser/net/url_fetcher.h" +#include "chrome/common/net/url_fetcher.h" #include "googleurl/src/gurl.h" #include "net/base/completion_callback.h" #include "net/base/net_errors.h" |