Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: content/browser/loader/resource_dispatcher_host_unittest.cc

Issue 25772002: Allows prefetch requests to live beyond the renderer by delaying (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change delay from 3s to 60s for initial timing. Obey the browser process when it wants to cancel. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/command_line.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
9 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/pickle.h" 12 #include "base/pickle.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
14 #include "content/browser/browser_thread_impl.h" 15 #include "content/browser/browser_thread_impl.h"
15 #include "content/browser/child_process_security_policy_impl.h" 16 #include "content/browser/child_process_security_policy_impl.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h" 17 #include "content/browser/loader/resource_dispatcher_host_impl.h"
18 #include "content/browser/loader/resource_loader.h"
17 #include "content/browser/loader/resource_message_filter.h" 19 #include "content/browser/loader/resource_message_filter.h"
18 #include "content/browser/loader/resource_request_info_impl.h" 20 #include "content/browser/loader/resource_request_info_impl.h"
19 #include "content/browser/worker_host/worker_service_impl.h" 21 #include "content/browser/worker_host/worker_service_impl.h"
20 #include "content/common/child_process_host_impl.h" 22 #include "content/common/child_process_host_impl.h"
21 #include "content/common/resource_messages.h" 23 #include "content/common/resource_messages.h"
22 #include "content/common/view_messages.h" 24 #include "content/common/view_messages.h"
23 #include "content/public/browser/global_request_id.h" 25 #include "content/public/browser/global_request_id.h"
24 #include "content/public/browser/resource_context.h" 26 #include "content/public/browser/resource_context.h"
25 #include "content/public/browser/resource_dispatcher_host_delegate.h" 27 #include "content/public/browser/resource_dispatcher_host_delegate.h"
26 #include "content/public/browser/resource_request_info.h" 28 #include "content/public/browser/resource_request_info.h"
27 #include "content/public/browser/resource_throttle.h" 29 #include "content/public/browser/resource_throttle.h"
30 #include "content/public/common/content_switches.h"
28 #include "content/public/common/process_type.h" 31 #include "content/public/common/process_type.h"
29 #include "content/public/common/resource_response.h" 32 #include "content/public/common/resource_response.h"
30 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
31 #include "content/test/test_content_browser_client.h" 34 #include "content/test/test_content_browser_client.h"
32 #include "net/base/net_errors.h" 35 #include "net/base/net_errors.h"
33 #include "net/base/upload_bytes_element_reader.h" 36 #include "net/base/upload_bytes_element_reader.h"
34 #include "net/base/upload_data_stream.h" 37 #include "net/base/upload_data_stream.h"
35 #include "net/http/http_util.h" 38 #include "net/http/http_util.h"
36 #include "net/url_request/url_request.h" 39 #include "net/url_request/url_request.h"
37 #include "net/url_request/url_request_context.h" 40 #include "net/url_request/url_request_context.h"
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 browser_context_.reset(); 595 browser_context_.reset();
593 message_loop_.RunUntilIdle(); 596 message_loop_.RunUntilIdle();
594 } 597 }
595 598
596 // Creates a request using the current test object as the filter. 599 // Creates a request using the current test object as the filter.
597 void MakeTestRequest(int render_view_id, 600 void MakeTestRequest(int render_view_id,
598 int request_id, 601 int request_id,
599 const GURL& url); 602 const GURL& url);
600 603
601 // Generates a request using the given filter. This will probably be a 604 // Generates a request using the given filter. This will probably be a
605 // ForwardingFilter. Uses the resource_type_ as the resource type.
606 void MakeTestRequest(ResourceMessageFilter* filter,
607 int render_view_id,
608 int request_id,
609 const GURL& url);
610
611 // Generates a request using the given filter. This will probably be a
602 // ForwardingFilter. 612 // ForwardingFilter.
603 void MakeTestRequest(ResourceMessageFilter* filter, 613 void MakeTestRequest(ResourceMessageFilter* filter,
604 int render_view_id, 614 int render_view_id,
605 int request_id, 615 int request_id,
616 ResourceType::Type type,
606 const GURL& url); 617 const GURL& url);
mmenke 2013/10/07 19:16:24 I think it's a bad idea to have two ways to set th
mmenke 2013/10/07 19:16:24 Google style guide discourages operator overloadin
mmenke 2013/10/07 19:28:28 -operator + function
jkarlin2 2013/10/08 11:53:01 Done.
jkarlin2 2013/10/08 11:53:01 Done, though the precedent is already set in the f
607 618
619
mmenke 2013/10/07 19:16:24 nit: Remove extra blank line.
jkarlin2 2013/10/08 11:53:01 Done.
608 void CancelRequest(int request_id); 620 void CancelRequest(int request_id);
609 621
610 void CompleteStartRequest(int request_id); 622 void CompleteStartRequest(int request_id);
611 void CompleteStartRequest(ResourceMessageFilter* filter, int request_id); 623 void CompleteStartRequest(ResourceMessageFilter* filter, int request_id);
612 624
613 void EnsureSchemeIsAllowed(const std::string& scheme) { 625 void EnsureSchemeIsAllowed(const std::string& scheme) {
614 ChildProcessSecurityPolicyImpl* policy = 626 ChildProcessSecurityPolicyImpl* policy =
615 ChildProcessSecurityPolicyImpl::GetInstance(); 627 ChildProcessSecurityPolicyImpl::GetInstance();
616 if (!policy->IsWebSafeScheme(scheme)) 628 if (!policy->IsWebSafeScheme(scheme))
617 policy->RegisterWebSafeScheme(scheme); 629 policy->RegisterWebSafeScheme(scheme);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 int request_id, 753 int request_id,
742 const GURL& url) { 754 const GURL& url) {
743 MakeTestRequest(filter_.get(), render_view_id, request_id, url); 755 MakeTestRequest(filter_.get(), render_view_id, request_id, url);
744 } 756 }
745 757
746 void ResourceDispatcherHostTest::MakeTestRequest( 758 void ResourceDispatcherHostTest::MakeTestRequest(
747 ResourceMessageFilter* filter, 759 ResourceMessageFilter* filter,
748 int render_view_id, 760 int render_view_id,
749 int request_id, 761 int request_id,
750 const GURL& url) { 762 const GURL& url) {
763 MakeTestRequest(filter, render_view_id, request_id, resource_type_, url);
764 }
765
766 void ResourceDispatcherHostTest::MakeTestRequest(
767 ResourceMessageFilter* filter,
768 int render_view_id,
769 int request_id,
770 ResourceType::Type type,
771 const GURL& url) {
751 // If it's already there, this'll be dropped on the floor, which is fine. 772 // If it's already there, this'll be dropped on the floor, which is fine.
752 child_ids_.insert(filter->child_id()); 773 child_ids_.insert(filter->child_id());
753 774
754 ResourceHostMsg_Request request = 775 ResourceHostMsg_Request request =
755 CreateResourceRequest("GET", resource_type_, url); 776 CreateResourceRequest("GET", type, url);
756 ResourceHostMsg_RequestResource msg(render_view_id, request_id, request); 777 ResourceHostMsg_RequestResource msg(render_view_id, request_id, request);
757 bool msg_was_ok; 778 bool msg_was_ok;
758 host_.OnMessageReceived(msg, filter, &msg_was_ok); 779 host_.OnMessageReceived(msg, filter, &msg_was_ok);
759 KickOffRequest(); 780 KickOffRequest();
760 } 781 }
761 782
762 void ResourceDispatcherHostTest::CancelRequest(int request_id) { 783 void ResourceDispatcherHostTest::CancelRequest(int request_id) {
763 host_.CancelRequest(filter_->child_id(), request_id, false); 784 host_.CancelRequest(filter_->child_id(), request_id, false);
764 } 785 }
765 786
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 921
901 CheckSuccessfulRequest(msgs[0], net::URLRequestTestJob::test_data_1()); 922 CheckSuccessfulRequest(msgs[0], net::URLRequestTestJob::test_data_1());
902 CheckSuccessfulRequest(msgs[2], net::URLRequestTestJob::test_data_3()); 923 CheckSuccessfulRequest(msgs[2], net::URLRequestTestJob::test_data_3());
903 924
904 // Check that request 2 got canceled. 925 // Check that request 2 got canceled.
905 ASSERT_EQ(2U, msgs[1].size()); 926 ASSERT_EQ(2U, msgs[1].size());
906 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[1][0].type()); 927 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[1][0].type());
907 CheckCancelledRequestCompleteMessage(msgs[1][1]); 928 CheckCancelledRequestCompleteMessage(msgs[1][1]);
908 } 929 }
909 930
931 // Shows that unlike normal requests, prefetched async requests are not
932 // immediately canceled, and will complete within a timeout period.
933 TEST_F(ResourceDispatcherHostTest, PrefetchIgnoreCancel) {
934 CommandLine* command_line = CommandLine::ForCurrentProcess();
935 command_line->AppendSwitch(switches::kDelayPrefetchCancellation);
936 MakeTestRequest(filter_.get(), 0, 1, ResourceType::PREFETCH,
937 net::URLRequestTestJob::test_url_1());
938 MakeTestRequest(filter_.get(), 0, 2, ResourceType::PREFETCH,
939 net::URLRequestTestJob::test_url_2());
940 MakeTestRequest(0, 3, net::URLRequestTestJob::test_url_3());
941
942 // test_url_1 is synchronous and already complete.
943 EXPECT_EQ(2, host_.pending_requests());
944 CancelRequest(2);
945 CancelRequest(3);
946
947 // Process any completion messages from cancelling.
948 base::MessageLoop::current()->RunUntilIdle();
949
950 EXPECT_EQ(1, host_.pending_requests());
951
952 // Run the requests to completion.
953 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {}
954 base::MessageLoop::current()->RunUntilIdle();
955
956 EXPECT_EQ(0, host_.pending_requests());
957
958 ResourceIPCAccumulator::ClassifiedMessages msgs;
959 accum_.GetClassifiedMessages(&msgs);
960
961 ASSERT_EQ(3U, msgs.size());
962
963 // The first fetch was synchronous and should have completed successfully.
964 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type());
965 ASSERT_EQ(ResourceMsg_RequestComplete::ID, msgs[0][1].type());
966
967 // The prefetch should have ignored the cancel and completed. Note that
968 // prefetches run detached and do not receive data notifications.
969 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[1][0].type());
970 ASSERT_EQ(ResourceMsg_RequestComplete::ID, msgs[1][1].type());
971 // Verify that there was no error.
972 int request_id;
973 int error_code;
974 PickleIterator iter(msgs[1][1]);
975 ASSERT_TRUE(IPC::ReadParam(&msgs[1][1], &iter, &request_id));
976 ASSERT_TRUE(IPC::ReadParam(&msgs[1][1], &iter, &error_code));
977 EXPECT_EQ(0, error_code);
978
979 // Request 3 should have been cancelled immediately.
980 ASSERT_EQ(2U, msgs[2].size());
981 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[2][0].type());
982 CheckCancelledRequestCompleteMessage(msgs[2][1]);
983 }
984
985 // Shows that prefetched requests will timeout if the request takes too long
986 // to complete.
987 TEST_F(ResourceDispatcherHostTest, PrefetchIgnoreCancelTimesOut) {
988 CommandLine* command_line = CommandLine::ForCurrentProcess();
989 command_line->AppendSwitch(switches::kDelayPrefetchCancellation);
990
991 // Reduce the cancel timeout so that our test doesn't take forever.
992 const int kDelay = 200;
993 ResourceLoader::set_delay_prefetch_cancel_ms(kDelay);
994
995 MakeTestRequest(filter_.get(), 0, 1, ResourceType::PREFETCH,
996 net::URLRequestTestJob::test_url_2());
997 CancelRequest(1);
998
999 EXPECT_EQ(1, host_.pending_requests());
1000
1001
1002 // Wait until after the delay timer times before letting any Reads complete.
1003 base::OneShotTimer<base::MessageLoop> timer;
1004 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kDelay + 10),
1005 base::MessageLoop::current(), &base::MessageLoop::QuitWhenIdle);
1006
1007 // We should have cancelled the prefetch by now.
1008 base::MessageLoop::current()->Run();
1009 EXPECT_EQ(0, host_.pending_requests());
1010
1011 // In case any messages are still to be processed.
1012 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {}
1013 base::MessageLoop::current()->RunUntilIdle();
1014
1015 ResourceIPCAccumulator::ClassifiedMessages msgs;
1016 accum_.GetClassifiedMessages(&msgs);
1017
1018 ASSERT_EQ(1U, msgs.size());
1019
1020 // The request should have cancelled.
1021 ASSERT_EQ(2U, msgs[0].size());
1022 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type());
1023 CheckCancelledRequestCompleteMessage(msgs[0][1]);
1024 }
1025
910 TEST_F(ResourceDispatcherHostTest, CancelWhileStartIsDeferred) { 1026 TEST_F(ResourceDispatcherHostTest, CancelWhileStartIsDeferred) {
911 bool was_deleted = false; 1027 bool was_deleted = false;
912 1028
913 // Arrange to have requests deferred before starting. 1029 // Arrange to have requests deferred before starting.
914 TestResourceDispatcherHostDelegate delegate; 1030 TestResourceDispatcherHostDelegate delegate;
915 delegate.set_flags(DEFER_STARTING_REQUEST); 1031 delegate.set_flags(DEFER_STARTING_REQUEST);
916 delegate.set_url_request_user_data(new TestUserData(&was_deleted)); 1032 delegate.set_url_request_user_data(new TestUserData(&was_deleted));
917 host_.SetDelegate(&delegate); 1033 host_.SetDelegate(&delegate);
918 1034
919 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_1()); 1035 MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_1());
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after
1985 2101
1986 size_t size = msgs[0].size(); 2102 size_t size = msgs[0].size();
1987 2103
1988 EXPECT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type()); 2104 EXPECT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type());
1989 EXPECT_EQ(ResourceMsg_SetDataBuffer::ID, msgs[0][1].type()); 2105 EXPECT_EQ(ResourceMsg_SetDataBuffer::ID, msgs[0][1].type());
1990 for (size_t i = 2; i < size - 1; ++i) 2106 for (size_t i = 2; i < size - 1; ++i)
1991 EXPECT_EQ(ResourceMsg_DataReceived::ID, msgs[0][i].type()); 2107 EXPECT_EQ(ResourceMsg_DataReceived::ID, msgs[0][i].type());
1992 EXPECT_EQ(ResourceMsg_RequestComplete::ID, msgs[0][size - 1].type()); 2108 EXPECT_EQ(ResourceMsg_RequestComplete::ID, msgs[0][size - 1].type());
1993 } 2109 }
1994 2110
2111 // Request a very large prefetch. This tests to make sure that the data
2112 // is not sent to the render process and verifies that the async handler
2113 // doesn't fill up its pending buffers and block.
2114 TEST_F(ResourceDispatcherHostTest, DetachedNoDataSentOrReceived) {
2115 EXPECT_EQ(0, host_.pending_requests());
2116
2117 SendDataReceivedACKs(true);
2118
2119 HandleScheme("big-job");
2120
2121 // This request would normally result in many messages (>300).
2122 MakeTestRequest(filter_.get(), 0, 1, ResourceType::PREFETCH,
2123 GURL("big-job:0123456789,1000000"));
2124
2125 // Sort all the messages we saw by request.
2126 ResourceIPCAccumulator::ClassifiedMessages msgs;
2127 accum_.GetClassifiedMessages(&msgs);
2128
2129 EXPECT_EQ(size_t(2), msgs[0].size());
2130 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type());
2131 ASSERT_EQ(ResourceMsg_RequestComplete::ID, msgs[0][1].type());
2132 }
2133
1995 TEST_F(ResourceDispatcherHostTest, DelayedDataReceivedACKs) { 2134 TEST_F(ResourceDispatcherHostTest, DelayedDataReceivedACKs) {
1996 EXPECT_EQ(0, host_.pending_requests()); 2135 EXPECT_EQ(0, host_.pending_requests());
1997 2136
1998 HandleScheme("big-job"); 2137 HandleScheme("big-job");
1999 MakeTestRequest(0, 1, GURL("big-job:0123456789,1000000")); 2138 MakeTestRequest(0, 1, GURL("big-job:0123456789,1000000"));
2000 2139
2001 // Sort all the messages we saw by request. 2140 // Sort all the messages we saw by request.
2002 ResourceIPCAccumulator::ClassifiedMessages msgs; 2141 ResourceIPCAccumulator::ClassifiedMessages msgs;
2003 accum_.GetClassifiedMessages(&msgs); 2142 accum_.GetClassifiedMessages(&msgs);
2004 2143
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2086 } 2225 }
2087 2226
2088 base::MessageLoop::current()->RunUntilIdle(); 2227 base::MessageLoop::current()->RunUntilIdle();
2089 2228
2090 msgs.clear(); 2229 msgs.clear();
2091 accum_.GetClassifiedMessages(&msgs); 2230 accum_.GetClassifiedMessages(&msgs);
2092 } 2231 }
2093 } 2232 }
2094 2233
2095 } // namespace content 2234 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698