diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-22 19:00:58 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-22 19:00:58 +0000 |
commit | 8523ba5fab2b52f359b644fea1b9c2b3ea078260 (patch) | |
tree | f77d449eb28f0fa1b2f0bc0bf8006751423be59f /net | |
parent | 653f4ec848607162cd842a3dbcb6d5f18f72bcba (diff) | |
download | chromium_src-8523ba5fab2b52f359b644fea1b9c2b3ea078260.zip chromium_src-8523ba5fab2b52f359b644fea1b9c2b3ea078260.tar.gz chromium_src-8523ba5fab2b52f359b644fea1b9c2b3ea078260.tar.bz2 |
Kill URLRequestJobTracker.
BUG=81160
TEST=none
Review URL: http://codereview.chromium.org/7043007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86241 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/network_delegate.cc | 6 | ||||
-rw-r--r-- | net/base/network_delegate.h | 4 | ||||
-rw-r--r-- | net/http/http_cache.cc | 4 | ||||
-rw-r--r-- | net/http/http_cache.h | 1 | ||||
-rw-r--r-- | net/http/http_network_layer.cc | 10 | ||||
-rw-r--r-- | net/http/http_network_layer.h | 7 | ||||
-rw-r--r-- | net/http/http_network_layer_unittest.cc | 4 | ||||
-rw-r--r-- | net/http/http_transaction_factory.h | 4 | ||||
-rw-r--r-- | net/http/http_transaction_unittest.cc | 2 | ||||
-rw-r--r-- | net/http/http_transaction_unittest.h | 3 | ||||
-rw-r--r-- | net/net.gyp | 3 | ||||
-rw-r--r-- | net/proxy/network_delegate_error_observer_unittest.cc | 6 | ||||
-rw-r--r-- | net/url_request/url_request_job.cc | 25 | ||||
-rw-r--r-- | net/url_request/url_request_job.h | 8 | ||||
-rw-r--r-- | net/url_request/url_request_job_tracker.cc | 62 | ||||
-rw-r--r-- | net/url_request/url_request_job_tracker.h | 105 | ||||
-rw-r--r-- | net/url_request/url_request_job_tracker_unittest.cc | 229 | ||||
-rw-r--r-- | net/url_request/url_request_test_util.cc | 9 | ||||
-rw-r--r-- | net/url_request/url_request_test_util.h | 1 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 20 |
20 files changed, 69 insertions, 444 deletions
diff --git a/net/base/network_delegate.cc b/net/base/network_delegate.cc index 248ef45..a236c5d 100644 --- a/net/base/network_delegate.cc +++ b/net/base/network_delegate.cc @@ -40,6 +40,12 @@ void NetworkDelegate::NotifyResponseStarted(URLRequest* request) { OnResponseStarted(request); } +void NetworkDelegate::NotifyRawBytesRead(const URLRequest& request, + int bytes_read) { + DCHECK(CalledOnValidThread()); + OnRawBytesRead(request, bytes_read); +} + void NetworkDelegate::NotifyBeforeRedirect(URLRequest* request, const GURL& new_location) { DCHECK(CalledOnValidThread()); diff --git a/net/base/network_delegate.h b/net/base/network_delegate.h index 977df2f..741b7f2 100644 --- a/net/base/network_delegate.h +++ b/net/base/network_delegate.h @@ -49,6 +49,7 @@ class NetworkDelegate : public base::NonThreadSafe { void NotifyBeforeRedirect(URLRequest* request, const GURL& new_location); void NotifyResponseStarted(URLRequest* request); + void NotifyRawBytesRead(const URLRequest& request, int bytes_read); void NotifyCompleted(URLRequest* request); void NotifyURLRequestDestroyed(URLRequest* request); void NotifyHttpTransactionDestroyed(uint64 request_id); @@ -89,6 +90,9 @@ class NetworkDelegate : public base::NonThreadSafe { // This corresponds to URLRequestDelegate::OnResponseStarted. virtual void OnResponseStarted(URLRequest* request) = 0; + // Called every time we read raw bytes. + virtual void OnRawBytesRead(const URLRequest& request, int bytes_read) = 0; + // Indicates that the URL request has been completed or failed. virtual void OnCompleted(URLRequest* request) = 0; diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc index 3943619..fbc6461 100644 --- a/net/http/http_cache.cc +++ b/net/http/http_cache.cc @@ -478,10 +478,6 @@ HttpNetworkSession* HttpCache::GetSession() { return network->GetSession(); } -void HttpCache::Suspend(bool suspend) { - network_layer_->Suspend(suspend); -} - //----------------------------------------------------------------------------- int HttpCache::CreateBackend(disk_cache::Backend** backend, diff --git a/net/http/http_cache.h b/net/http/http_cache.h index 5728dbd..3fbb5e1 100644 --- a/net/http/http_cache.h +++ b/net/http/http_cache.h @@ -183,7 +183,6 @@ class NET_API HttpCache : public HttpTransactionFactory, virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans); virtual HttpCache* GetCache(); virtual HttpNetworkSession* GetSession(); - virtual void Suspend(bool suspend); protected: // Disk cache entry data indices. diff --git a/net/http/http_network_layer.cc b/net/http/http_network_layer.cc index ce3fab8..68bc5f8 100644 --- a/net/http/http_network_layer.cc +++ b/net/http/http_network_layer.cc @@ -152,11 +152,15 @@ HttpNetworkSession* HttpNetworkLayer::GetSession() { return session_; } -void HttpNetworkLayer::Suspend(bool suspend) { - suspended_ = suspend; +void HttpNetworkLayer::OnSuspend() { + suspended_ = true; - if (suspend && session_) + if (session_) session_->CloseIdleConnections(); } +void HttpNetworkLayer::OnResume() { + suspended_ = false; +} + } // namespace net diff --git a/net/http/http_network_layer.h b/net/http/http_network_layer.h index 35652c3..448ae8d 100644 --- a/net/http/http_network_layer.h +++ b/net/http/http_network_layer.h @@ -10,6 +10,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "base/system_monitor/system_monitor.h" #include "base/threading/non_thread_safe.h" #include "net/base/net_api.h" #include "net/http/http_transaction_factory.h" @@ -31,6 +32,7 @@ class SSLConfigService; class SSLHostInfoFactory; class NET_API HttpNetworkLayer : public HttpTransactionFactory, + public base::SystemMonitor::PowerObserver, NON_EXPORTED_BASE(public base::NonThreadSafe) { public: // Construct a HttpNetworkLayer with an existing HttpNetworkSession which @@ -60,7 +62,10 @@ class NET_API HttpNetworkLayer : public HttpTransactionFactory, virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans); virtual HttpCache* GetCache(); virtual HttpNetworkSession* GetSession(); - virtual void Suspend(bool suspend); + + // base::SystemMonitor::PowerObserver methods: + virtual void OnSuspend(); + virtual void OnResume(); private: const scoped_refptr<HttpNetworkSession> session_; diff --git a/net/http/http_network_layer_unittest.cc b/net/http/http_network_layer_unittest.cc index 3bd6db8..3c356e8 100644 --- a/net/http/http_network_layer_unittest.cc +++ b/net/http/http_network_layer_unittest.cc @@ -57,14 +57,14 @@ TEST_F(HttpNetworkLayerTest, Suspend) { trans.reset(); - factory_->Suspend(true); + factory_->OnSuspend(); rv = factory_->CreateTransaction(&trans); EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, rv); ASSERT_TRUE(trans == NULL); - factory_->Suspend(false); + factory_->OnResume(); rv = factory_->CreateTransaction(&trans); EXPECT_EQ(OK, rv); diff --git a/net/http/http_transaction_factory.h b/net/http/http_transaction_factory.h index aef7e03..2dcb9f8 100644 --- a/net/http/http_transaction_factory.h +++ b/net/http/http_transaction_factory.h @@ -30,10 +30,6 @@ class NET_API HttpTransactionFactory { // Returns the associated HttpNetworkSession used by new transactions. virtual HttpNetworkSession* GetSession() = 0; - - // Suspends the creation of new transactions. If |suspend| is false, creation - // of new transactions is resumed. - virtual void Suspend(bool suspend) = 0; }; } // namespace net diff --git a/net/http/http_transaction_unittest.cc b/net/http/http_transaction_unittest.cc index c900247..cec930d 100644 --- a/net/http/http_transaction_unittest.cc +++ b/net/http/http_transaction_unittest.cc @@ -339,8 +339,6 @@ net::HttpNetworkSession* MockNetworkLayer::GetSession() { return NULL; } -void MockNetworkLayer::Suspend(bool suspend) {} - //----------------------------------------------------------------------------- // helpers diff --git a/net/http/http_transaction_unittest.h b/net/http/http_transaction_unittest.h index 0a11bb5..589133c 100644 --- a/net/http/http_transaction_unittest.h +++ b/net/http/http_transaction_unittest.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -204,7 +204,6 @@ class MockNetworkLayer : public net::HttpTransactionFactory { virtual int CreateTransaction(scoped_ptr<net::HttpTransaction>* trans); virtual net::HttpCache* GetCache(); virtual net::HttpNetworkSession* GetSession(); - virtual void Suspend(bool suspend); private: int transaction_count_; diff --git a/net/net.gyp b/net/net.gyp index 2c90ed6..9169f97 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -608,8 +608,6 @@ 'url_request/url_request_job_factory.h', 'url_request/url_request_job_manager.cc', 'url_request/url_request_job_manager.h', - 'url_request/url_request_job_tracker.cc', - 'url_request/url_request_job_tracker.h', 'url_request/url_request_netlog_params.cc', 'url_request/url_request_netlog_params.h', 'url_request/url_request_redirect_job.cc', @@ -979,7 +977,6 @@ 'tools/dump_cache/url_utilities_unittest.cc', 'udp/udp_socket_unittest.cc', 'url_request/url_request_job_factory_unittest.cc', - 'url_request/url_request_job_tracker_unittest.cc', 'url_request/url_request_throttler_unittest.cc', 'url_request/url_request_unittest.cc', 'url_request/view_cache_helper_unittest.cc', diff --git a/net/proxy/network_delegate_error_observer_unittest.cc b/net/proxy/network_delegate_error_observer_unittest.cc index f76f2d0..0239b53 100644 --- a/net/proxy/network_delegate_error_observer_unittest.cc +++ b/net/proxy/network_delegate_error_observer_unittest.cc @@ -15,6 +15,7 @@ DISABLE_RUNNABLE_METHOD_REFCOUNT(net::NetworkDelegateErrorObserver); namespace net { namespace { + class TestNetworkDelegate : public net::NetworkDelegate { public: TestNetworkDelegate() : got_pac_error_(false) {} @@ -40,13 +41,16 @@ class TestNetworkDelegate : public net::NetworkDelegate { virtual void OnBeforeRedirect(URLRequest* request, const GURL& new_location) {} virtual void OnResponseStarted(URLRequest* request) {} + virtual void OnRawBytesRead(const URLRequest& request, + int bytes_read) {} virtual void OnCompleted(URLRequest* request) {} virtual void OnURLRequestDestroyed(URLRequest* request) {} virtual void OnHttpTransactionDestroyed(uint64 request_id) {} virtual URLRequestJob* OnMaybeCreateURLRequestJob(URLRequest* request) { return NULL; } - virtual void OnPACScriptError(int line_number, const string16& error) { + virtual void OnPACScriptError(int line_number, + const string16& error) { got_pac_error_ = true; } diff --git a/net/url_request/url_request_job.cc b/net/url_request/url_request_job.cc index 3073414..331b968 100644 --- a/net/url_request/url_request_job.cc +++ b/net/url_request/url_request_job.cc @@ -17,7 +17,6 @@ #include "net/http/http_response_headers.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_context.h" -#include "net/url_request/url_request_job_tracker.h" namespace net { @@ -33,14 +32,15 @@ URLRequestJob::URLRequestJob(URLRequest* request) expected_content_size_(-1), deferred_redirect_status_code_(-1), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { - g_url_request_job_tracker.AddNewJob(this); + base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); + if (system_monitor) + base::SystemMonitor::Get()->AddObserver(this); } void URLRequestJob::SetUpload(UploadData* upload) { } -void URLRequestJob::SetExtraRequestHeaders( - const HttpRequestHeaders& headers) { +void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) { } void URLRequestJob::Kill() { @@ -203,8 +203,14 @@ HostPortPair URLRequestJob::GetSocketAddress() const { return HostPortPair(); } +void URLRequestJob::OnSuspend() { + Kill(); +} + URLRequestJob::~URLRequestJob() { - g_url_request_job_tracker.RemoveJob(this); + base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); + if (system_monitor) + base::SystemMonitor::Get()->RemoveObserver(this); } void URLRequestJob::NotifyHeadersComplete() { @@ -362,8 +368,6 @@ void URLRequestJob::NotifyDone(const URLRequestStatus &status) { request_->set_status(status); } - g_url_request_job_tracker.OnJobDone(this, status); - if (request_ && request_->context() && request_->context()->network_delegate()) { request_->context()->network_delegate()->NotifyCompleted(request_); @@ -578,8 +582,6 @@ bool URLRequestJob::ReadRawDataHelper(IOBuffer* buf, int buf_size, } void URLRequestJob::FollowRedirect(const GURL& location, int http_status_code) { - g_url_request_job_tracker.OnJobRedirect(this, location, http_status_code); - int rv = request_->Redirect(location, http_status_code); if (rv != OK) NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); @@ -596,8 +598,9 @@ void URLRequestJob::OnRawReadComplete(int bytes_read) { void URLRequestJob::RecordBytesRead(int bytes_read) { filter_input_byte_count_ += bytes_read; UpdatePacketReadTimes(); // Facilitate stats recording if it is active. - g_url_request_job_tracker.OnBytesRead(this, raw_read_buffer_->data(), - bytes_read); + const URLRequestContext* context = request_->context(); + if (context && context->network_delegate()) + context->network_delegate()->NotifyRawBytesRead(*request_, bytes_read); } bool URLRequestJob::FilterHasData() { diff --git a/net/url_request/url_request_job.h b/net/url_request/url_request_job.h index d4330c2..d2261ca 100644 --- a/net/url_request/url_request_job.h +++ b/net/url_request/url_request_job.h @@ -12,6 +12,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/string16.h" +#include "base/system_monitor/system_monitor.h" #include "base/task.h" #include "base/time.h" #include "googleurl/src/gurl.h" @@ -31,7 +32,8 @@ class UploadData; class URLRequestStatus; class X509Certificate; -class NET_API URLRequestJob : public base::RefCounted<URLRequestJob> { +class NET_API URLRequestJob : public base::RefCounted<URLRequestJob>, + public base::SystemMonitor::PowerObserver { public: explicit URLRequestJob(URLRequest* request); @@ -180,6 +182,10 @@ class NET_API URLRequestJob : public base::RefCounted<URLRequestJob> { // See url_request.h for details. virtual HostPortPair GetSocketAddress() const; + // base::SystemMonitor::PowerObserver methods: + // We invoke URLRequestJob::Kill on suspend (crbug.com/4606). + virtual void OnSuspend(); + protected: friend class base::RefCounted<URLRequestJob>; virtual ~URLRequestJob(); diff --git a/net/url_request/url_request_job_tracker.cc b/net/url_request/url_request_job_tracker.cc deleted file mode 100644 index 632d0a3..0000000 --- a/net/url_request/url_request_job_tracker.cc +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2010 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 "net/url_request/url_request_job_tracker.h" - -#include <algorithm> - -#include "base/logging.h" -#include "net/url_request/url_request_job.h" - -namespace net { - -URLRequestJobTracker g_url_request_job_tracker; - -URLRequestJobTracker::URLRequestJobTracker() { -} - -URLRequestJobTracker::~URLRequestJobTracker() { - DLOG_IF(WARNING, !active_jobs_.empty()) << - "Leaking " << active_jobs_.size() << " URLRequestJob object(s), this " - "could be because the URLRequest forgot to free it (bad), or if the " - "program was terminated while a request was active (normal)."; -} - -void URLRequestJobTracker::AddNewJob(URLRequestJob* job) { - active_jobs_.push_back(job); - FOR_EACH_OBSERVER(JobObserver, observers_, OnJobAdded(job)); -} - -void URLRequestJobTracker::RemoveJob(URLRequestJob* job) { - JobList::iterator iter = std::find(active_jobs_.begin(), active_jobs_.end(), - job); - if (iter == active_jobs_.end()) { - NOTREACHED() << "Removing a non-active job"; - return; - } - active_jobs_.erase(iter); - - FOR_EACH_OBSERVER(JobObserver, observers_, OnJobRemoved(job)); -} - -void URLRequestJobTracker::OnJobDone(URLRequestJob* job, - const URLRequestStatus& status) { - FOR_EACH_OBSERVER(JobObserver, observers_, OnJobDone(job, status)); -} - -void URLRequestJobTracker::OnJobRedirect(URLRequestJob* job, - const GURL& location, - int status_code) { - FOR_EACH_OBSERVER(JobObserver, observers_, - OnJobRedirect(job, location, status_code)); -} - -void URLRequestJobTracker::OnBytesRead(URLRequestJob* job, - const char* buf, - int byte_count) { - FOR_EACH_OBSERVER(JobObserver, observers_, - OnBytesRead(job, buf, byte_count)); -} - -} // namespace net diff --git a/net/url_request/url_request_job_tracker.h b/net/url_request/url_request_job_tracker.h deleted file mode 100644 index cb691ce..0000000 --- a/net/url_request/url_request_job_tracker.h +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright (c) 2011 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 NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_ -#define NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_ -#pragma once - -#include <vector> - -#include "base/observer_list.h" -#include "net/base/net_api.h" -#include "net/url_request/url_request_status.h" - -class GURL; - -namespace net { -class URLRequestJob; - -// This class maintains a list of active URLRequestJobs for debugging purposes. -// This allows us to warn on leaked jobs and also allows an observer to track -// what is happening, for example, for the network status monitor. -// -// NOTE: URLRequest is single-threaded, so this class should only be used -// onthe same thread where all of the application's URLRequest calls are -// made. -// -class NET_API URLRequestJobTracker { - public: - typedef std::vector<URLRequestJob*> JobList; - typedef JobList::const_iterator JobIterator; - - // The observer's methods are called on the thread that called AddObserver. - class JobObserver { - public: - virtual ~JobObserver() {} - - // Called after the given job has been added to the list - virtual void OnJobAdded(URLRequestJob* job) = 0; - - // Called after the given job has been removed from the list - virtual void OnJobRemoved(URLRequestJob* job) = 0; - - // Called when the given job has completed, before notifying the request - virtual void OnJobDone(URLRequestJob* job, - const URLRequestStatus& status) = 0; - - // Called when the given job is about to follow a redirect to the given - // new URL. The redirect type is given in status_code - virtual void OnJobRedirect(URLRequestJob* job, const GURL& location, - int status_code) = 0; - - // Called when a new chunk of unfiltered bytes has been read for - // the given job. |byte_count| is the number of bytes for that - // read event only. |buf| is a pointer to the data buffer that - // contains those bytes. The data in |buf| is only valid for the - // duration of the OnBytesRead callback. - virtual void OnBytesRead(URLRequestJob* job, const char* buf, - int byte_count) = 0; - }; - - URLRequestJobTracker(); - ~URLRequestJobTracker(); - - // adds or removes an observer from the list. note, these methods should - // only be called on the same thread where URLRequest objects are used. - void AddObserver(JobObserver* observer) { - observers_.AddObserver(observer); - } - void RemoveObserver(JobObserver* observer) { - observers_.RemoveObserver(observer); - } - - // adds or removes the job from the active list, should be called by the - // job constructor and destructor. Note: don't use "AddJob" since that - // is #defined by windows.h :( - void AddNewJob(URLRequestJob* job); - void RemoveJob(URLRequestJob* job); - - // Job status change notifications - void OnJobDone(URLRequestJob* job, const URLRequestStatus& status); - void OnJobRedirect(URLRequestJob* job, const GURL& location, - int status_code); - - // Bytes read notifications. - void OnBytesRead(URLRequestJob* job, const char* buf, int byte_count); - - // allows iteration over all active jobs - JobIterator begin() const { - return active_jobs_.begin(); - } - JobIterator end() const { - return active_jobs_.end(); - } - - private: - ObserverList<JobObserver> observers_; - JobList active_jobs_; -}; - -NET_API extern URLRequestJobTracker g_url_request_job_tracker; - -} // namespace net - -#endif // NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_ diff --git a/net/url_request/url_request_job_tracker_unittest.cc b/net/url_request/url_request_job_tracker_unittest.cc deleted file mode 100644 index d29f2d3..0000000 --- a/net/url_request/url_request_job_tracker_unittest.cc +++ /dev/null @@ -1,229 +0,0 @@ -// Copyright (c) 2011 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 <string.h> -#include <algorithm> -#include <string> -#include <vector> -#include "base/message_loop.h" -#include "googleurl/src/gurl.h" -#include "net/base/filter.h" -#include "net/base/io_buffer.h" -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_job.h" -#include "net/url_request/url_request_job_tracker.h" -#include "net/url_request/url_request_status.h" -#include "net/url_request/url_request_test_util.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" - -using testing::Eq; -using testing::InSequence; -using testing::NotNull; -using testing::StrictMock; - -namespace net { -namespace { - -const char kBasic[] = "Hello\n"; - -// The above string "Hello\n", gzip compressed. -const unsigned char kCompressed[] = { - 0x1f, 0x8b, 0x08, 0x08, 0x38, 0x18, 0x2e, 0x4c, 0x00, 0x03, 0x63, - 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2e, 0x68, - 0x74, 0x6d, 0x6c, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, - 0x00, 0x16, 0x35, 0x96, 0x31, 0x06, 0x00, 0x00, 0x00 -}; - -bool GetResponseBody(const GURL& url, std::string* out_body) { - if (url.spec() == "test:basic") { - *out_body = kBasic; - } else if (url.spec() == "test:compressed") { - out_body->assign(reinterpret_cast<const char*>(kCompressed), - sizeof(kCompressed)); - } else { - return false; - } - - return true; -} - -class MockJobObserver : public URLRequestJobTracker::JobObserver { - public: - MOCK_METHOD1(OnJobAdded, void(URLRequestJob* job)); - MOCK_METHOD1(OnJobRemoved, void(URLRequestJob* job)); - MOCK_METHOD2(OnJobDone, void(URLRequestJob* job, - const URLRequestStatus& status)); - MOCK_METHOD3(OnJobRedirect, void(URLRequestJob* job, - const GURL& location, - int status_code)); - MOCK_METHOD3(OnBytesRead, void(URLRequestJob* job, - const char* buf, - int byte_count)); -}; - -// A URLRequestJob that returns static content for given URLs. We do -// not use URLRequestTestJob here because URLRequestTestJob fakes -// async operations by calling ReadRawData synchronously in an async -// callback. This test requires a URLRequestJob that returns false for -// async reads, in order to exercise the real async read codepath. -class URLRequestJobTrackerTestJob : public URLRequestJob { - public: - URLRequestJobTrackerTestJob(URLRequest* request, bool async_reads) - : URLRequestJob(request), async_reads_(async_reads) {} - - void Start() { - ASSERT_TRUE(GetResponseBody(request_->url(), &response_data_)); - - // Start reading asynchronously so that all error reporting and data - // callbacks happen as they would for network requests. - MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( - this, &URLRequestJobTrackerTestJob::NotifyHeadersComplete)); - } - - bool ReadRawData(IOBuffer* buf, int buf_size, - int *bytes_read) { - const size_t bytes_to_read = std::min( - response_data_.size(), static_cast<size_t>(buf_size)); - - // Regardless of whether we're performing a sync or async read, - // copy the data into the caller's buffer now. That way we don't - // have to hold on to the buffers in the async case. - memcpy(buf->data(), response_data_.data(), bytes_to_read); - response_data_.erase(0, bytes_to_read); - - if (async_reads_) { - SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); - MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( - this, &URLRequestJobTrackerTestJob::OnReadCompleted, - bytes_to_read)); - } else { - SetStatus(URLRequestStatus()); - *bytes_read = bytes_to_read; - } - return !async_reads_; - } - - void OnReadCompleted(int status) { - if (status == 0) { - NotifyDone(URLRequestStatus()); - } else if (status > 0) { - SetStatus(URLRequestStatus()); - } else { - ASSERT_FALSE(true) << "Unexpected OnReadCompleted callback."; - } - - NotifyReadComplete(status); - } - - Filter* SetupFilter() const { - return request_->url().spec() == "test:compressed" - ? Filter::GZipFactory() : NULL; - } - - // The data to send, will be set in Start(). - std::string response_data_; - - // Should reads be synchronous or asynchronous? - const bool async_reads_; -}; - -// Google Mock Matcher to check two URLRequestStatus instances for -// equality. -MATCHER_P(StatusEq, other, "") { - return (arg.status() == other.status() && - arg.os_error() == other.os_error()); -} - -// Google Mock Matcher to check that two blocks of memory are equal. -MATCHER_P2(MemEq, other, len, "") { - return memcmp(arg, other, len) == 0; -} - -class URLRequestJobTrackerTest : public PlatformTest { - protected: - static void SetUpTestCase() { - URLRequest::RegisterProtocolFactory("test", &Factory); - } - - virtual void SetUp() { - g_async_reads = true; - } - - void AssertJobTrackerCallbacks(const char* url) { - InSequence seq; - testing::StrictMock<MockJobObserver> observer; - - const GURL gurl(url); - std::string body; - ASSERT_TRUE(GetResponseBody(gurl, &body)); - - // We expect to receive one call for each method on the JobObserver, - // in the following order: - EXPECT_CALL(observer, OnJobAdded(NotNull())); - EXPECT_CALL(observer, OnBytesRead(NotNull(), - MemEq(body.data(), body.size()), - Eq(static_cast<int>(body.size())))); - EXPECT_CALL(observer, OnJobDone(NotNull(), - StatusEq(URLRequestStatus()))); - EXPECT_CALL(observer, OnJobRemoved(NotNull())); - - // Attach our observer and perform the resource fetch. - g_url_request_job_tracker.AddObserver(&observer); - Fetch(gurl); - g_url_request_job_tracker.RemoveObserver(&observer); - } - - void Fetch(const GURL& url) { - TestDelegate d; - { - URLRequest request(url, &d); - request.Start(); - MessageLoop::current()->RunAllPending(); - } - - // A few sanity checks to make sure that the delegate also - // receives the expected callbacks. - EXPECT_EQ(1, d.response_started_count()); - EXPECT_FALSE(d.received_data_before_response()); - EXPECT_STREQ(kBasic, d.data_received().c_str()); - } - - static URLRequest::ProtocolFactory Factory; - static bool g_async_reads; -}; - -// static -URLRequestJob* URLRequestJobTrackerTest::Factory( - URLRequest* request, - const std::string& scheme) { - return new URLRequestJobTrackerTestJob(request, g_async_reads); -} - -// static -bool URLRequestJobTrackerTest::g_async_reads = true; - -TEST_F(URLRequestJobTrackerTest, BasicAsync) { - g_async_reads = true; - AssertJobTrackerCallbacks("test:basic"); -} - -TEST_F(URLRequestJobTrackerTest, BasicSync) { - g_async_reads = false; - AssertJobTrackerCallbacks("test:basic"); -} - -TEST_F(URLRequestJobTrackerTest, CompressedAsync) { - g_async_reads = true; - AssertJobTrackerCallbacks("test:compressed"); -} - -TEST_F(URLRequestJobTrackerTest, CompressedSync) { - g_async_reads = false; - AssertJobTrackerCallbacks("test:compressed"); -} - -} // namespace -} // namespace net diff --git a/net/url_request/url_request_test_util.cc b/net/url_request/url_request_test_util.cc index c9e2fe4..67d685e 100644 --- a/net/url_request/url_request_test_util.cc +++ b/net/url_request/url_request_test_util.cc @@ -252,7 +252,7 @@ TestNetworkDelegate::~TestNetworkDelegate() {} int TestNetworkDelegate::OnBeforeURLRequest( net::URLRequest* request, net::CompletionCallback* callback, - GURL* new_url) { + GURL* new_url ) { created_requests_++; return net::OK; } @@ -281,6 +281,10 @@ void TestNetworkDelegate::OnResponseStarted(net::URLRequest* request) { } } +void TestNetworkDelegate::OnRawBytesRead(const net::URLRequest& request, + int bytes_read) { +} + void TestNetworkDelegate::OnCompleted(net::URLRequest* request) { if (request->status().status() == net::URLRequestStatus::FAILED) { error_count_++; @@ -288,7 +292,8 @@ void TestNetworkDelegate::OnCompleted(net::URLRequest* request) { } } -void TestNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) { +void TestNetworkDelegate::OnURLRequestDestroyed( + net::URLRequest* request) { destroyed_requests_++; } diff --git a/net/url_request/url_request_test_util.h b/net/url_request/url_request_test_util.h index c4371c7..772abc6 100644 --- a/net/url_request/url_request_test_util.h +++ b/net/url_request/url_request_test_util.h @@ -194,6 +194,7 @@ class TestNetworkDelegate : public net::NetworkDelegate { virtual void OnBeforeRedirect(net::URLRequest* request, const GURL& new_location); virtual void OnResponseStarted(net::URLRequest* request); + virtual void OnRawBytesRead(const net::URLRequest& request, int bytes_read); virtual void OnCompleted(net::URLRequest* request); virtual void OnURLRequestDestroyed(net::URLRequest* request); virtual void OnHttpTransactionDestroyed(uint64 request_id); diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 31bea3b..ebabe8d 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -12,6 +12,7 @@ #include <algorithm> #include <string> +#include "base/compiler_specific.h" #include "base/file_util.h" #include "base/format_macros.h" #include "base/message_loop.h" @@ -50,13 +51,6 @@ using base::Time; -// We don't need a refcount because we are guaranteed the test will not proceed -// until its task is run. -namespace net { -class BlockingNetworkDelegate; -} // namespace net -DISABLE_RUNNABLE_METHOD_REFCOUNT(net::BlockingNetworkDelegate); - namespace net { namespace { @@ -126,7 +120,9 @@ void CheckSSLInfo(const SSLInfo& ssl_info) { // them. class BlockingNetworkDelegate : public TestNetworkDelegate { public: - BlockingNetworkDelegate() : callback_retval_(net::OK) {} + BlockingNetworkDelegate() + : callback_retval_(net::OK), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} void set_callback_retval(int retval) { callback_retval_ = retval; } void set_redirect_url(const GURL& url) { redirect_url_ = url; } @@ -145,9 +141,10 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { if (!redirect_url_.is_empty()) *new_url = redirect_url_; - MessageLoop::current()->PostTask(FROM_HERE, - NewRunnableMethod(this, &BlockingNetworkDelegate::DoCallback, - callback)); + MessageLoop::current()->PostTask( + FROM_HERE, + method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback, + callback)); return net::ERR_IO_PENDING; } @@ -157,6 +154,7 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { int callback_retval_; GURL redirect_url_; + ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; }; // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |