diff options
author | joaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 16:12:36 +0000 |
---|---|---|
committer | joaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 16:12:36 +0000 |
commit | 3cd384c6025a6267948ea00d6aa20792a72c8a75 (patch) | |
tree | 5bb1d2049e530d022bcb21113c0dc1cb8646bb45 /net | |
parent | a8171203aadbc0d452ec3b46eb16d6b5b5e8427b (diff) | |
download | chromium_src-3cd384c6025a6267948ea00d6aa20792a72c8a75.zip chromium_src-3cd384c6025a6267948ea00d6aa20792a72c8a75.tar.gz chromium_src-3cd384c6025a6267948ea00d6aa20792a72c8a75.tar.bz2 |
Allow the NetworkDelegate to synchronously cancel a URLRequest.
BUG=49612
TEST=net_unittests:URLRequestTestHTTP.NetworkDelegateCancelRequestSynchronously
Review URL: http://codereview.chromium.org/7747016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98978 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/url_request/url_request.cc | 19 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 37 |
2 files changed, 49 insertions, 7 deletions
diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc index 7c94964..8f25db9 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -403,11 +403,17 @@ void URLRequest::Start() { // Only notify the delegate for the initial request. if (context_ && context_->network_delegate()) { - if (context_->network_delegate()->NotifyBeforeURLRequest( - this, &before_request_callback_, &delegate_redirect_url_) == - net::ERR_IO_PENDING) { - SetBlockedOnDelegate(); - return; // paused + int error = context_->network_delegate()->NotifyBeforeURLRequest( + this, &before_request_callback_, &delegate_redirect_url_); + if (error != net::OK) { + if (error == net::ERR_IO_PENDING) { + // Paused on the delegate, will invoke |before_request_callback_| later. + SetBlockedOnDelegate(); + } else { + // The delegate immediately returned some error code. + BeforeRequestComplete(error); + } + return; } } @@ -420,7 +426,8 @@ void URLRequest::BeforeRequestComplete(int error) { DCHECK(!job_); DCHECK_NE(ERR_IO_PENDING, error); - SetUnblockedOnDelegate(); + if (blocked_on_delegate_) + SetUnblockedOnDelegate(); if (error != OK) { net_log_.AddEvent(NetLog::TYPE_CANCELLED, make_scoped_refptr(new NetLogStringParameter("source", "delegate"))); diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index f6721ba..6f7c196 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -123,9 +123,11 @@ void CheckSSLInfo(const SSLInfo& ssl_info) { class BlockingNetworkDelegate : public TestNetworkDelegate { public: BlockingNetworkDelegate() - : callback_retval_(net::OK), + : retval_(net::ERR_IO_PENDING), + callback_retval_(net::OK), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} + void set_retval(int retval) { retval_ = retval; } void set_callback_retval(int retval) { callback_retval_ = retval; } void set_redirect_url(const GURL& url) { redirect_url_ = url; } @@ -143,6 +145,10 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { if (!redirect_url_.is_empty()) *new_url = redirect_url_; + + if (retval_ != net::ERR_IO_PENDING) + return retval_; + MessageLoop::current()->PostTask( FROM_HERE, method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback, @@ -154,6 +160,7 @@ class BlockingNetworkDelegate : public TestNetworkDelegate { callback->Run(callback_retval_); } + int retval_; int callback_retval_; GURL redirect_url_; ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; @@ -343,6 +350,34 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { EXPECT_EQ(1, network_delegate.destroyed_requests()); } +// Tests that the network delegate can cancel a request synchronously. +TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { + ASSERT_TRUE(test_server_.Start()); + + TestDelegate d; + BlockingNetworkDelegate network_delegate; + network_delegate.set_retval(ERR_EMPTY_RESPONSE); + + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); + context->SetProxyFromString(test_server_.host_port_pair().ToString()); + context->set_network_delegate(&network_delegate); + context->Init(); + + { + TestURLRequest r(test_server_.GetURL(""), &d); + r.set_context(context); + + r.Start(); + MessageLoop::current()->Run(); + + EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); + EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().os_error()); + EXPECT_EQ(1, network_delegate.created_requests()); + EXPECT_EQ(0, network_delegate.destroyed_requests()); + } + EXPECT_EQ(1, network_delegate.destroyed_requests()); +} + // Tests that the network delegate can block and redirect a request to a new // URL. TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |