Index: content/browser/loader/resource_dispatcher_host_unittest.cc |
diff --git a/content/browser/loader/resource_dispatcher_host_unittest.cc b/content/browser/loader/resource_dispatcher_host_unittest.cc |
index 9d58b8f290e4a8c87a6ff760bdc68cc21f0deeae..009b364b06f843698f03a76ba433cf4b902f00d4 100644 |
--- a/content/browser/loader/resource_dispatcher_host_unittest.cc |
+++ b/content/browser/loader/resource_dispatcher_host_unittest.cc |
@@ -210,6 +210,33 @@ class ForwardingFilter : public ResourceMessageFilter { |
DISALLOW_COPY_AND_ASSIGN(ForwardingFilter); |
}; |
+// This class is a variation on URLRequestTestJob that will call |
+// URLRequest::OnBeforeNetworkStart before starting. |
+class URLRequestTestDelayedNetworkJob : public net::URLRequestTestJob { |
+ public: |
+ URLRequestTestDelayedNetworkJob(net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) |
+ : net::URLRequestTestJob(request, network_delegate) {} |
+ |
+ // Only start if not deferred for network start. |
+ virtual void Start() OVERRIDE { |
+ bool defer = false; |
+ NotifyBeforeNetworkStart(&defer); |
+ if (defer) |
+ return; |
+ net::URLRequestTestJob::Start(); |
+ } |
+ |
+ virtual void ResumeNetworkStart() OVERRIDE { |
+ net::URLRequestTestJob::StartAsync(); |
+ } |
+ |
+ private: |
+ virtual ~URLRequestTestDelayedNetworkJob() {} |
+ |
+ DISALLOW_COPY_AND_ASSIGN(URLRequestTestDelayedNetworkJob); |
+}; |
+ |
// This class is a variation on URLRequestTestJob in that it does |
// not complete start upon entry, only when specifically told to. |
class URLRequestTestDelayedStartJob : public net::URLRequestTestJob { |
@@ -394,7 +421,8 @@ enum GenericResourceThrottleFlags { |
NONE = 0, |
DEFER_STARTING_REQUEST = 1 << 0, |
DEFER_PROCESSING_RESPONSE = 1 << 1, |
- CANCEL_BEFORE_START = 1 << 2 |
+ CANCEL_BEFORE_START = 1 << 2, |
+ DEFER_NETWORK_START = 1 << 3 |
}; |
// Throttle that tracks the current throttle blocking a request. Only one |
@@ -441,6 +469,15 @@ class GenericResourceThrottle : public ResourceThrottle { |
} |
} |
+ virtual void OnBeforeNetworkStart(bool* defer) OVERRIDE { |
+ ASSERT_EQ(NULL, active_throttle_); |
+ |
+ if (flags_ & DEFER_NETWORK_START) { |
+ active_throttle_ = this; |
+ *defer = true; |
+ } |
+ } |
+ |
virtual const char* GetNameForLogging() const OVERRIDE { |
return "GenericResourceThrottle"; |
} |
@@ -573,6 +610,7 @@ class ResourceDispatcherHostTest : public testing::Test, |
EnsureTestSchemeIsAllowed(); |
delay_start_ = false; |
delay_complete_ = false; |
+ network_start_notification_ = false; |
url_request_jobs_created_count_ = 0; |
} |
@@ -670,6 +708,8 @@ class ResourceDispatcherHostTest : public testing::Test, |
} else if (delay_complete_) { |
return new URLRequestTestDelayedCompletionJob(request, |
network_delegate); |
+ } else if (network_start_notification_) { |
+ return new URLRequestTestDelayedNetworkJob(request, network_delegate); |
} else if (scheme == "big-job") { |
return new URLRequestBigJob(request, network_delegate); |
} else { |
@@ -703,6 +743,10 @@ class ResourceDispatcherHostTest : public testing::Test, |
delay_complete_ = delay_job_complete; |
} |
+ void SetNetworkStartNotificationJobGeneration(bool notification) { |
+ network_start_notification_ = notification; |
+ } |
+ |
void GenerateDataReceivedACK(const IPC::Message& msg) { |
EXPECT_EQ(ResourceMsg_DataReceived::ID, msg.type()); |
@@ -736,12 +780,14 @@ class ResourceDispatcherHostTest : public testing::Test, |
static ResourceDispatcherHostTest* test_fixture_; |
static bool delay_start_; |
static bool delay_complete_; |
+ static bool network_start_notification_; |
static int url_request_jobs_created_count_; |
}; |
// Static. |
ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; |
bool ResourceDispatcherHostTest::delay_start_ = false; |
bool ResourceDispatcherHostTest::delay_complete_ = false; |
+bool ResourceDispatcherHostTest::network_start_notification_ = false; |
int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; |
void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id, |
@@ -1228,6 +1274,33 @@ TEST_F(ResourceDispatcherHostTest, PausedStartError) { |
EXPECT_EQ(0, host_.pending_requests()); |
} |
+// Test the OnBeforeNetworkStart throttle. |
+TEST_F(ResourceDispatcherHostTest, ThrottleNetworkStart) { |
+ // Arrange to have requests deferred before processing response headers. |
+ TestResourceDispatcherHostDelegate delegate; |
+ delegate.set_flags(DEFER_NETWORK_START); |
+ host_.SetDelegate(&delegate); |
+ |
+ SetNetworkStartNotificationJobGeneration(true); |
+ MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_2()); |
+ |
+ // Should have deferred for network start. |
+ GenericResourceThrottle* first_throttle = |
+ GenericResourceThrottle::active_throttle(); |
+ ASSERT_TRUE(first_throttle); |
+ EXPECT_EQ(0, network_delegate()->completed_requests()); |
+ EXPECT_EQ(1, host_.pending_requests()); |
+ |
+ first_throttle->Resume(); |
+ |
+ // Flush all the pending requests. |
+ while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_EQ(1, network_delegate()->completed_requests()); |
+ EXPECT_EQ(0, host_.pending_requests()); |
+} |
+ |
TEST_F(ResourceDispatcherHostTest, ThrottleAndResumeTwice) { |
// Arrange to have requests deferred before starting. |
TestResourceDispatcherHostDelegate delegate; |