OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 *resource_context = resource_context_; | 203 *resource_context = resource_context_; |
204 *request_context = resource_context_->GetRequestContext(); | 204 *request_context = resource_context_->GetRequestContext(); |
205 } | 205 } |
206 | 206 |
207 IPC::Sender* dest_; | 207 IPC::Sender* dest_; |
208 ResourceContext* resource_context_; | 208 ResourceContext* resource_context_; |
209 | 209 |
210 DISALLOW_COPY_AND_ASSIGN(ForwardingFilter); | 210 DISALLOW_COPY_AND_ASSIGN(ForwardingFilter); |
211 }; | 211 }; |
212 | 212 |
| 213 // This class is a variation on URLRequestTestJob that will call |
| 214 // URLRequest::OnBeforeNetworkStart before starting. |
| 215 class URLRequestTestDelayedNetworkJob : public net::URLRequestTestJob { |
| 216 public: |
| 217 URLRequestTestDelayedNetworkJob(net::URLRequest* request, |
| 218 net::NetworkDelegate* network_delegate) |
| 219 : net::URLRequestTestJob(request, network_delegate) {} |
| 220 |
| 221 // Only start if not deferred for network start. |
| 222 virtual void Start() OVERRIDE { |
| 223 bool defer = false; |
| 224 NotifyBeforeNetworkStart(&defer); |
| 225 if (defer) |
| 226 return; |
| 227 net::URLRequestTestJob::Start(); |
| 228 } |
| 229 |
| 230 virtual void ResumeNetworkStart() OVERRIDE { |
| 231 net::URLRequestTestJob::StartAsync(); |
| 232 } |
| 233 |
| 234 private: |
| 235 virtual ~URLRequestTestDelayedNetworkJob() {} |
| 236 |
| 237 DISALLOW_COPY_AND_ASSIGN(URLRequestTestDelayedNetworkJob); |
| 238 }; |
| 239 |
213 // This class is a variation on URLRequestTestJob in that it does | 240 // This class is a variation on URLRequestTestJob in that it does |
214 // not complete start upon entry, only when specifically told to. | 241 // not complete start upon entry, only when specifically told to. |
215 class URLRequestTestDelayedStartJob : public net::URLRequestTestJob { | 242 class URLRequestTestDelayedStartJob : public net::URLRequestTestJob { |
216 public: | 243 public: |
217 URLRequestTestDelayedStartJob(net::URLRequest* request, | 244 URLRequestTestDelayedStartJob(net::URLRequest* request, |
218 net::NetworkDelegate* network_delegate) | 245 net::NetworkDelegate* network_delegate) |
219 : net::URLRequestTestJob(request, network_delegate) { | 246 : net::URLRequestTestJob(request, network_delegate) { |
220 Init(); | 247 Init(); |
221 } | 248 } |
222 URLRequestTestDelayedStartJob(net::URLRequest* request, | 249 URLRequestTestDelayedStartJob(net::URLRequest* request, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 const GURL& current_url, | 414 const GURL& current_url, |
388 const GURL& new_url) OVERRIDE { | 415 const GURL& new_url) OVERRIDE { |
389 return true; | 416 return true; |
390 } | 417 } |
391 }; | 418 }; |
392 | 419 |
393 enum GenericResourceThrottleFlags { | 420 enum GenericResourceThrottleFlags { |
394 NONE = 0, | 421 NONE = 0, |
395 DEFER_STARTING_REQUEST = 1 << 0, | 422 DEFER_STARTING_REQUEST = 1 << 0, |
396 DEFER_PROCESSING_RESPONSE = 1 << 1, | 423 DEFER_PROCESSING_RESPONSE = 1 << 1, |
397 CANCEL_BEFORE_START = 1 << 2 | 424 CANCEL_BEFORE_START = 1 << 2, |
| 425 DEFER_NETWORK_START = 1 << 3 |
398 }; | 426 }; |
399 | 427 |
400 // Throttle that tracks the current throttle blocking a request. Only one | 428 // Throttle that tracks the current throttle blocking a request. Only one |
401 // can throttle any request at a time. | 429 // can throttle any request at a time. |
402 class GenericResourceThrottle : public ResourceThrottle { | 430 class GenericResourceThrottle : public ResourceThrottle { |
403 public: | 431 public: |
404 // The value is used to indicate that the throttle should not provide | 432 // The value is used to indicate that the throttle should not provide |
405 // a error code when cancelling a request. net::OK is used, because this | 433 // a error code when cancelling a request. net::OK is used, because this |
406 // is not an error code. | 434 // is not an error code. |
407 static const int USE_DEFAULT_CANCEL_ERROR_CODE = net::OK; | 435 static const int USE_DEFAULT_CANCEL_ERROR_CODE = net::OK; |
(...skipping 26 matching lines...) Expand all Loading... |
434 } | 462 } |
435 | 463 |
436 virtual void WillProcessResponse(bool* defer) OVERRIDE { | 464 virtual void WillProcessResponse(bool* defer) OVERRIDE { |
437 ASSERT_EQ(NULL, active_throttle_); | 465 ASSERT_EQ(NULL, active_throttle_); |
438 if (flags_ & DEFER_PROCESSING_RESPONSE) { | 466 if (flags_ & DEFER_PROCESSING_RESPONSE) { |
439 active_throttle_ = this; | 467 active_throttle_ = this; |
440 *defer = true; | 468 *defer = true; |
441 } | 469 } |
442 } | 470 } |
443 | 471 |
| 472 virtual void OnBeforeNetworkStart(bool* defer) OVERRIDE { |
| 473 ASSERT_EQ(NULL, active_throttle_); |
| 474 |
| 475 if (flags_ & DEFER_NETWORK_START) { |
| 476 active_throttle_ = this; |
| 477 *defer = true; |
| 478 } |
| 479 } |
| 480 |
444 virtual const char* GetNameForLogging() const OVERRIDE { | 481 virtual const char* GetNameForLogging() const OVERRIDE { |
445 return "GenericResourceThrottle"; | 482 return "GenericResourceThrottle"; |
446 } | 483 } |
447 | 484 |
448 void Resume() { | 485 void Resume() { |
449 ASSERT_TRUE(this == active_throttle_); | 486 ASSERT_TRUE(this == active_throttle_); |
450 active_throttle_ = NULL; | 487 active_throttle_ = NULL; |
451 controller()->Resume(); | 488 controller()->Resume(); |
452 } | 489 } |
453 | 490 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 virtual void SetUp() { | 603 virtual void SetUp() { |
567 DCHECK(!test_fixture_); | 604 DCHECK(!test_fixture_); |
568 test_fixture_ = this; | 605 test_fixture_ = this; |
569 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); | 606 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); |
570 net::URLRequest::Deprecated::RegisterProtocolFactory( | 607 net::URLRequest::Deprecated::RegisterProtocolFactory( |
571 "test", | 608 "test", |
572 &ResourceDispatcherHostTest::Factory); | 609 &ResourceDispatcherHostTest::Factory); |
573 EnsureTestSchemeIsAllowed(); | 610 EnsureTestSchemeIsAllowed(); |
574 delay_start_ = false; | 611 delay_start_ = false; |
575 delay_complete_ = false; | 612 delay_complete_ = false; |
| 613 network_start_notification_ = false; |
576 url_request_jobs_created_count_ = 0; | 614 url_request_jobs_created_count_ = 0; |
577 } | 615 } |
578 | 616 |
579 virtual void TearDown() { | 617 virtual void TearDown() { |
580 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); | 618 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); |
581 if (!scheme_.empty()) | 619 if (!scheme_.empty()) |
582 net::URLRequest::Deprecated::RegisterProtocolFactory( | 620 net::URLRequest::Deprecated::RegisterProtocolFactory( |
583 scheme_, old_factory_); | 621 scheme_, old_factory_); |
584 | 622 |
585 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); | 623 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 static net::URLRequestJob* Factory(net::URLRequest* request, | 701 static net::URLRequestJob* Factory(net::URLRequest* request, |
664 net::NetworkDelegate* network_delegate, | 702 net::NetworkDelegate* network_delegate, |
665 const std::string& scheme) { | 703 const std::string& scheme) { |
666 url_request_jobs_created_count_++; | 704 url_request_jobs_created_count_++; |
667 if (test_fixture_->response_headers_.empty()) { | 705 if (test_fixture_->response_headers_.empty()) { |
668 if (delay_start_) { | 706 if (delay_start_) { |
669 return new URLRequestTestDelayedStartJob(request, network_delegate); | 707 return new URLRequestTestDelayedStartJob(request, network_delegate); |
670 } else if (delay_complete_) { | 708 } else if (delay_complete_) { |
671 return new URLRequestTestDelayedCompletionJob(request, | 709 return new URLRequestTestDelayedCompletionJob(request, |
672 network_delegate); | 710 network_delegate); |
| 711 } else if (network_start_notification_) { |
| 712 return new URLRequestTestDelayedNetworkJob(request, network_delegate); |
673 } else if (scheme == "big-job") { | 713 } else if (scheme == "big-job") { |
674 return new URLRequestBigJob(request, network_delegate); | 714 return new URLRequestBigJob(request, network_delegate); |
675 } else { | 715 } else { |
676 return new net::URLRequestTestJob(request, network_delegate); | 716 return new net::URLRequestTestJob(request, network_delegate); |
677 } | 717 } |
678 } else { | 718 } else { |
679 if (delay_start_) { | 719 if (delay_start_) { |
680 return new URLRequestTestDelayedStartJob( | 720 return new URLRequestTestDelayedStartJob( |
681 request, network_delegate, | 721 request, network_delegate, |
682 test_fixture_->response_headers_, test_fixture_->response_data_, | 722 test_fixture_->response_headers_, test_fixture_->response_data_, |
(...skipping 13 matching lines...) Expand all Loading... |
696 } | 736 } |
697 | 737 |
698 void SetDelayedStartJobGeneration(bool delay_job_start) { | 738 void SetDelayedStartJobGeneration(bool delay_job_start) { |
699 delay_start_ = delay_job_start; | 739 delay_start_ = delay_job_start; |
700 } | 740 } |
701 | 741 |
702 void SetDelayedCompleteJobGeneration(bool delay_job_complete) { | 742 void SetDelayedCompleteJobGeneration(bool delay_job_complete) { |
703 delay_complete_ = delay_job_complete; | 743 delay_complete_ = delay_job_complete; |
704 } | 744 } |
705 | 745 |
| 746 void SetNetworkStartNotificationJobGeneration(bool notification) { |
| 747 network_start_notification_ = notification; |
| 748 } |
| 749 |
706 void GenerateDataReceivedACK(const IPC::Message& msg) { | 750 void GenerateDataReceivedACK(const IPC::Message& msg) { |
707 EXPECT_EQ(ResourceMsg_DataReceived::ID, msg.type()); | 751 EXPECT_EQ(ResourceMsg_DataReceived::ID, msg.type()); |
708 | 752 |
709 int request_id = -1; | 753 int request_id = -1; |
710 bool result = PickleIterator(msg).ReadInt(&request_id); | 754 bool result = PickleIterator(msg).ReadInt(&request_id); |
711 DCHECK(result); | 755 DCHECK(result); |
712 scoped_ptr<IPC::Message> ack( | 756 scoped_ptr<IPC::Message> ack( |
713 new ResourceHostMsg_DataReceived_ACK(request_id)); | 757 new ResourceHostMsg_DataReceived_ACK(request_id)); |
714 | 758 |
715 base::MessageLoop::current()->PostTask( | 759 base::MessageLoop::current()->PostTask( |
(...skipping 13 matching lines...) Expand all Loading... |
729 ResourceIPCAccumulator accum_; | 773 ResourceIPCAccumulator accum_; |
730 std::string response_headers_; | 774 std::string response_headers_; |
731 std::string response_data_; | 775 std::string response_data_; |
732 std::string scheme_; | 776 std::string scheme_; |
733 net::URLRequest::ProtocolFactory* old_factory_; | 777 net::URLRequest::ProtocolFactory* old_factory_; |
734 bool send_data_received_acks_; | 778 bool send_data_received_acks_; |
735 std::set<int> child_ids_; | 779 std::set<int> child_ids_; |
736 static ResourceDispatcherHostTest* test_fixture_; | 780 static ResourceDispatcherHostTest* test_fixture_; |
737 static bool delay_start_; | 781 static bool delay_start_; |
738 static bool delay_complete_; | 782 static bool delay_complete_; |
| 783 static bool network_start_notification_; |
739 static int url_request_jobs_created_count_; | 784 static int url_request_jobs_created_count_; |
740 }; | 785 }; |
741 // Static. | 786 // Static. |
742 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; | 787 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; |
743 bool ResourceDispatcherHostTest::delay_start_ = false; | 788 bool ResourceDispatcherHostTest::delay_start_ = false; |
744 bool ResourceDispatcherHostTest::delay_complete_ = false; | 789 bool ResourceDispatcherHostTest::delay_complete_ = false; |
| 790 bool ResourceDispatcherHostTest::network_start_notification_ = false; |
745 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; | 791 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; |
746 | 792 |
747 void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id, | 793 void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id, |
748 int request_id, | 794 int request_id, |
749 const GURL& url) { | 795 const GURL& url) { |
750 MakeTestRequestWithResourceType(filter_.get(), render_view_id, request_id, | 796 MakeTestRequestWithResourceType(filter_.get(), render_view_id, request_id, |
751 url, ResourceType::SUB_RESOURCE); | 797 url, ResourceType::SUB_RESOURCE); |
752 } | 798 } |
753 | 799 |
754 void ResourceDispatcherHostTest::MakeTestRequestWithResourceType( | 800 void ResourceDispatcherHostTest::MakeTestRequestWithResourceType( |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_error()); | 1267 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_error()); |
1222 CompleteStartRequest(1); | 1268 CompleteStartRequest(1); |
1223 | 1269 |
1224 // flush all the pending requests | 1270 // flush all the pending requests |
1225 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1271 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
1226 base::MessageLoop::current()->RunUntilIdle(); | 1272 base::MessageLoop::current()->RunUntilIdle(); |
1227 | 1273 |
1228 EXPECT_EQ(0, host_.pending_requests()); | 1274 EXPECT_EQ(0, host_.pending_requests()); |
1229 } | 1275 } |
1230 | 1276 |
| 1277 // Test the OnBeforeNetworkStart throttle. |
| 1278 TEST_F(ResourceDispatcherHostTest, ThrottleNetworkStart) { |
| 1279 // Arrange to have requests deferred before processing response headers. |
| 1280 TestResourceDispatcherHostDelegate delegate; |
| 1281 delegate.set_flags(DEFER_NETWORK_START); |
| 1282 host_.SetDelegate(&delegate); |
| 1283 |
| 1284 SetNetworkStartNotificationJobGeneration(true); |
| 1285 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_2()); |
| 1286 |
| 1287 // Should have deferred for network start. |
| 1288 GenericResourceThrottle* first_throttle = |
| 1289 GenericResourceThrottle::active_throttle(); |
| 1290 ASSERT_TRUE(first_throttle); |
| 1291 EXPECT_EQ(0, network_delegate()->completed_requests()); |
| 1292 EXPECT_EQ(1, host_.pending_requests()); |
| 1293 |
| 1294 first_throttle->Resume(); |
| 1295 |
| 1296 // Flush all the pending requests. |
| 1297 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1298 base::MessageLoop::current()->RunUntilIdle(); |
| 1299 |
| 1300 EXPECT_EQ(1, network_delegate()->completed_requests()); |
| 1301 EXPECT_EQ(0, host_.pending_requests()); |
| 1302 } |
| 1303 |
1231 TEST_F(ResourceDispatcherHostTest, ThrottleAndResumeTwice) { | 1304 TEST_F(ResourceDispatcherHostTest, ThrottleAndResumeTwice) { |
1232 // Arrange to have requests deferred before starting. | 1305 // Arrange to have requests deferred before starting. |
1233 TestResourceDispatcherHostDelegate delegate; | 1306 TestResourceDispatcherHostDelegate delegate; |
1234 delegate.set_flags(DEFER_STARTING_REQUEST); | 1307 delegate.set_flags(DEFER_STARTING_REQUEST); |
1235 delegate.set_create_two_throttles(true); | 1308 delegate.set_create_two_throttles(true); |
1236 host_.SetDelegate(&delegate); | 1309 host_.SetDelegate(&delegate); |
1237 | 1310 |
1238 // Make sure the first throttle blocked the request, and then resume. | 1311 // Make sure the first throttle blocked the request, and then resume. |
1239 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_1()); | 1312 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_1()); |
1240 GenericResourceThrottle* first_throttle = | 1313 GenericResourceThrottle* first_throttle = |
(...skipping 1361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2602 } | 2675 } |
2603 | 2676 |
2604 base::MessageLoop::current()->RunUntilIdle(); | 2677 base::MessageLoop::current()->RunUntilIdle(); |
2605 | 2678 |
2606 msgs.clear(); | 2679 msgs.clear(); |
2607 accum_.GetClassifiedMessages(&msgs); | 2680 accum_.GetClassifiedMessages(&msgs); |
2608 } | 2681 } |
2609 } | 2682 } |
2610 | 2683 |
2611 } // namespace content | 2684 } // namespace content |
OLD | NEW |