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

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

Issue 2481093003: Introduce ResourceRequesterInfo to abstract the requester of resource request (Closed)
Patch Set: incorporated yhirano & kinuko's comment Created 4 years, 1 month 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 } 232 }
233 } 233 }
234 234
235 // This is used to create a filter matching a specified child id. 235 // This is used to create a filter matching a specified child id.
236 class TestFilterSpecifyingChild : public ResourceMessageFilter { 236 class TestFilterSpecifyingChild : public ResourceMessageFilter {
237 public: 237 public:
238 explicit TestFilterSpecifyingChild(ResourceContext* resource_context, 238 explicit TestFilterSpecifyingChild(ResourceContext* resource_context,
239 int process_id) 239 int process_id)
240 : ResourceMessageFilter( 240 : ResourceMessageFilter(
241 process_id, 241 process_id,
242 PROCESS_TYPE_RENDERER,
243 NULL, 242 NULL,
244 NULL, 243 NULL,
245 NULL, 244 NULL,
246 NULL, 245 NULL,
247 base::Bind(&TestFilterSpecifyingChild::GetContexts, 246 base::Bind(&TestFilterSpecifyingChild::GetContexts,
248 base::Unretained(this))), 247 base::Unretained(this))),
249 resource_context_(resource_context), 248 resource_context_(resource_context),
250 canceled_(false), 249 canceled_(false),
251 received_after_canceled_(0) { 250 received_after_canceled_(0) {
252 set_peer_process_for_testing(base::Process::Current()); 251 set_peer_process_for_testing(base::Process::Current());
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 void MakeTestRequestWithPriorityAndRenderFrame(int render_view_id, 976 void MakeTestRequestWithPriorityAndRenderFrame(int render_view_id,
978 int render_frame_id, 977 int render_frame_id,
979 int request_id, 978 int request_id,
980 net::RequestPriority priority); 979 net::RequestPriority priority);
981 980
982 void MakeWebContentsAssociatedDownloadRequest(int request_id, 981 void MakeWebContentsAssociatedDownloadRequest(int request_id,
983 const GURL& url); 982 const GURL& url);
984 983
985 void CancelRequest(int request_id); 984 void CancelRequest(int request_id);
986 void RendererCancelRequest(int request_id) { 985 void RendererCancelRequest(int request_id) {
987 ResourceMessageFilter* old_filter = SetFilter(filter_.get()); 986 host_.OnCancelRequest(filter_.get(), request_id);
988 host_.OnCancelRequest(request_id);
989 SetFilter(old_filter);
990 } 987 }
991 988
992 void CompleteStartRequest(int request_id); 989 void CompleteStartRequest(int request_id);
993 void CompleteStartRequest(ResourceMessageFilter* filter, int request_id); 990 void CompleteStartRequest(ResourceMessageFilter* filter, int request_id);
994 991
995 net::TestNetworkDelegate* network_delegate() { return &network_delegate_; } 992 net::TestNetworkDelegate* network_delegate() { return &network_delegate_; }
996 993
997 void EnsureSchemeIsAllowed(const std::string& scheme) { 994 void EnsureSchemeIsAllowed(const std::string& scheme) {
998 ChildProcessSecurityPolicyImpl* policy = 995 ChildProcessSecurityPolicyImpl* policy =
999 ChildProcessSecurityPolicyImpl::GetInstance(); 996 ChildProcessSecurityPolicyImpl::GetInstance();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 bool result = base::PickleIterator(msg).ReadInt(&request_id); 1031 bool result = base::PickleIterator(msg).ReadInt(&request_id);
1035 DCHECK(result); 1032 DCHECK(result);
1036 std::unique_ptr<IPC::Message> ack( 1033 std::unique_ptr<IPC::Message> ack(
1037 new ResourceHostMsg_DataReceived_ACK(request_id)); 1034 new ResourceHostMsg_DataReceived_ACK(request_id));
1038 1035
1039 base::ThreadTaskRunnerHandle::Get()->PostTask( 1036 base::ThreadTaskRunnerHandle::Get()->PostTask(
1040 FROM_HERE, 1037 FROM_HERE,
1041 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack))); 1038 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack)));
1042 } 1039 }
1043 1040
1044 // Setting filters for testing renderer messages.
1045 // Returns the previous filter.
1046 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) {
1047 ResourceMessageFilter* old_filter = host_.filter_;
1048 host_.filter_ = new_filter;
1049 return old_filter;
1050 }
1051
1052 void WaitForRequestComplete() { 1041 void WaitForRequestComplete() {
1053 DCHECK(!wait_for_request_complete_loop_); 1042 DCHECK(!wait_for_request_complete_loop_);
1054 wait_for_request_complete_loop_.reset(new base::RunLoop); 1043 wait_for_request_complete_loop_.reset(new base::RunLoop);
1055 wait_for_request_complete_loop_->Run(); 1044 wait_for_request_complete_loop_->Run();
1056 wait_for_request_complete_loop_.reset(); 1045 wait_for_request_complete_loop_.reset();
1057 } 1046 }
1058 1047
1059 void DeleteRenderFrame(const GlobalFrameRoutingId& global_routing_id) { 1048 void DeleteRenderFrame(const GlobalFrameRoutingId& global_routing_id) {
1060 host_.OnRenderFrameDeleted(global_routing_id); 1049 host_.OnRenderFrameDeleted(global_routing_id);
1061 } 1050 }
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
1564 host_.OnMessageReceived(msg_prefetch, filter_.get()); 1553 host_.OnMessageReceived(msg_prefetch, filter_.get());
1565 ResourceHostMsg_RequestResource msg_ping(0, 2, request_ping); 1554 ResourceHostMsg_RequestResource msg_ping(0, 2, request_ping);
1566 host_.OnMessageReceived(msg_ping, filter_.get()); 1555 host_.OnMessageReceived(msg_ping, filter_.get());
1567 1556
1568 // Remove the filter before processing the requests by simulating channel 1557 // Remove the filter before processing the requests by simulating channel
1569 // closure. 1558 // closure.
1570 ResourceRequestInfoImpl* info_prefetch = ResourceRequestInfoImpl::ForRequest( 1559 ResourceRequestInfoImpl* info_prefetch = ResourceRequestInfoImpl::ForRequest(
1571 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 1))); 1560 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 1)));
1572 ResourceRequestInfoImpl* info_ping = ResourceRequestInfoImpl::ForRequest( 1561 ResourceRequestInfoImpl* info_ping = ResourceRequestInfoImpl::ForRequest(
1573 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 2))); 1562 host_.GetURLRequest(GlobalRequestID(filter_->child_id(), 2)));
1574 DCHECK_EQ(filter_.get(), info_prefetch->filter()); 1563 DCHECK_EQ(filter_.get(), info_prefetch->requester_info().filter());
1575 DCHECK_EQ(filter_.get(), info_ping->filter()); 1564 DCHECK_EQ(filter_.get(), info_ping->requester_info().filter());
1576 filter_->OnChannelClosing(); 1565 filter_->OnChannelClosing();
1577 info_prefetch->filter_.reset();
1578 info_ping->filter_.reset();
1579 1566
1580 // From the renderer's perspective, the requests were cancelled. 1567 // From the renderer's perspective, the requests were cancelled.
1581 ResourceIPCAccumulator::ClassifiedMessages msgs; 1568 ResourceIPCAccumulator::ClassifiedMessages msgs;
1582 accum_.GetClassifiedMessages(&msgs); 1569 accum_.GetClassifiedMessages(&msgs);
1583 ASSERT_EQ(2U, msgs.size()); 1570 ASSERT_EQ(2U, msgs.size());
1584 CheckRequestCompleteErrorCode(msgs[0][0], net::ERR_ABORTED); 1571 CheckRequestCompleteErrorCode(msgs[0][0], net::ERR_ABORTED);
1585 CheckRequestCompleteErrorCode(msgs[1][0], net::ERR_ABORTED); 1572 CheckRequestCompleteErrorCode(msgs[1][0], net::ERR_ABORTED);
1586 1573
1587 // But it continues detached. 1574 // But it continues detached.
1588 EXPECT_EQ(2, host_.pending_requests()); 1575 EXPECT_EQ(2, host_.pending_requests());
(...skipping 22 matching lines...) Expand all
1611 net::URLRequestTestJob::test_url_redirect_to_url_2()); 1598 net::URLRequestTestJob::test_url_redirect_to_url_2());
1612 1599
1613 ResourceHostMsg_RequestResource msg(0, 1, request); 1600 ResourceHostMsg_RequestResource msg(0, 1, request);
1614 host_.OnMessageReceived(msg, filter_.get()); 1601 host_.OnMessageReceived(msg, filter_.get());
1615 1602
1616 // Remove the filter before processing the request by simulating channel 1603 // Remove the filter before processing the request by simulating channel
1617 // closure. 1604 // closure.
1618 GlobalRequestID global_request_id(filter_->child_id(), 1); 1605 GlobalRequestID global_request_id(filter_->child_id(), 1);
1619 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest( 1606 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
1620 host_.GetURLRequest(global_request_id)); 1607 host_.GetURLRequest(global_request_id));
1621 info->filter_->OnChannelClosing(); 1608 info->requester_info().filter()->OnChannelClosing();
1622 info->filter_.reset();
1623 1609
1624 // From the renderer's perspective, the request was cancelled. 1610 // From the renderer's perspective, the request was cancelled.
1625 ResourceIPCAccumulator::ClassifiedMessages msgs; 1611 ResourceIPCAccumulator::ClassifiedMessages msgs;
1626 accum_.GetClassifiedMessages(&msgs); 1612 accum_.GetClassifiedMessages(&msgs);
1627 ASSERT_EQ(1U, msgs.size()); 1613 ASSERT_EQ(1U, msgs.size());
1628 CheckRequestCompleteErrorCode(msgs[0][0], net::ERR_ABORTED); 1614 CheckRequestCompleteErrorCode(msgs[0][0], net::ERR_ABORTED);
1629 1615
1630 // But it continues detached. 1616 // But it continues detached.
1631 EXPECT_EQ(1, host_.pending_requests()); 1617 EXPECT_EQ(1, host_.pending_requests());
1632 EXPECT_TRUE(info->detachable_handler()->is_detached()); 1618 EXPECT_TRUE(info->detachable_handler()->is_detached());
(...skipping 1418 matching lines...) Expand 10 before | Expand all | Expand 10 after
3051 new_render_view_id, new_request_id, request); 3037 new_render_view_id, new_request_id, request);
3052 host_.OnMessageReceived(transfer_request_msg, second_filter.get()); 3038 host_.OnMessageReceived(transfer_request_msg, second_filter.get());
3053 3039
3054 // Verify that we update the ResourceRequestInfo. 3040 // Verify that we update the ResourceRequestInfo.
3055 GlobalRequestID global_request_id(second_filter->child_id(), new_request_id); 3041 GlobalRequestID global_request_id(second_filter->child_id(), new_request_id);
3056 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest( 3042 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
3057 host_.GetURLRequest(global_request_id)); 3043 host_.GetURLRequest(global_request_id));
3058 EXPECT_EQ(second_filter->child_id(), info->GetChildID()); 3044 EXPECT_EQ(second_filter->child_id(), info->GetChildID());
3059 EXPECT_EQ(new_render_view_id, info->GetRouteID()); 3045 EXPECT_EQ(new_render_view_id, info->GetRouteID());
3060 EXPECT_EQ(new_request_id, info->GetRequestID()); 3046 EXPECT_EQ(new_request_id, info->GetRequestID());
3061 EXPECT_EQ(second_filter.get(), info->filter()); 3047 EXPECT_EQ(second_filter.get(), info->requester_info().filter());
3062 3048
3063 // Let request complete. 3049 // Let request complete.
3064 base::RunLoop().RunUntilIdle(); 3050 base::RunLoop().RunUntilIdle();
3065 3051
3066 // Check generated messages. 3052 // Check generated messages.
3067 ResourceIPCAccumulator::ClassifiedMessages msgs; 3053 ResourceIPCAccumulator::ClassifiedMessages msgs;
3068 accum_.GetClassifiedMessages(&msgs); 3054 accum_.GetClassifiedMessages(&msgs);
3069 3055
3070 ASSERT_EQ(2U, msgs.size()); 3056 ASSERT_EQ(2U, msgs.size());
3071 EXPECT_EQ(ResourceMsg_ReceivedRedirect::ID, msgs[0][0].type()); 3057 EXPECT_EQ(ResourceMsg_ReceivedRedirect::ID, msgs[0][0].type());
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after
3710 } // namespace 3696 } // namespace
3711 3697
3712 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoSuccess) { 3698 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoSuccess) {
3713 ResourceRequest request = CreateResourceRequest( 3699 ResourceRequest request = CreateResourceRequest(
3714 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_1()); 3700 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_1());
3715 request.priority = net::MAXIMUM_PRIORITY; 3701 request.priority = net::MAXIMUM_PRIORITY;
3716 3702
3717 bool called = false; 3703 bool called = false;
3718 bool was_null = false; 3704 bool was_null = false;
3719 SyncLoadResult result; 3705 SyncLoadResult result;
3720 host_.OnSyncLoadWithMojo(0, 1, request, filter_.get(), 3706 host_.OnSyncLoadWithMojo(
3721 base::Bind(&StoreSyncLoadResult, 3707 0, 1, request,
3722 &called, &was_null, &result)); 3708 ResourceRequesterInfo::CreateForRenderer(filter_->GetWeakPtr()),
3709 base::Bind(&StoreSyncLoadResult, &called, &was_null, &result));
3723 base::RunLoop().RunUntilIdle(); 3710 base::RunLoop().RunUntilIdle();
3724 EXPECT_TRUE(called); 3711 EXPECT_TRUE(called);
3725 EXPECT_FALSE(was_null); 3712 EXPECT_FALSE(was_null);
3726 EXPECT_EQ(net::OK, result.error_code); 3713 EXPECT_EQ(net::OK, result.error_code);
3727 } 3714 }
3728 3715
3729 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoError) { 3716 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoError) {
3730 ResourceRequest request = CreateResourceRequest( 3717 ResourceRequest request = CreateResourceRequest(
3731 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_error()); 3718 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_error());
3732 request.priority = net::MAXIMUM_PRIORITY; 3719 request.priority = net::MAXIMUM_PRIORITY;
3733 3720
3734 bool called = false; 3721 bool called = false;
3735 bool was_null = false; 3722 bool was_null = false;
3736 SyncLoadResult result; 3723 SyncLoadResult result;
3737 host_.OnSyncLoadWithMojo(0, 1, request, filter_.get(), 3724 host_.OnSyncLoadWithMojo(
3738 base::Bind(&StoreSyncLoadResult, 3725 0, 1, request,
3739 &called, &was_null, &result)); 3726 ResourceRequesterInfo::CreateForRenderer(filter_->GetWeakPtr()),
3727 base::Bind(&StoreSyncLoadResult, &called, &was_null, &result));
3740 base::RunLoop().RunUntilIdle(); 3728 base::RunLoop().RunUntilIdle();
3741 EXPECT_TRUE(called); 3729 EXPECT_TRUE(called);
3742 EXPECT_FALSE(was_null); 3730 EXPECT_FALSE(was_null);
3743 EXPECT_EQ(net::ERR_INVALID_URL, result.error_code); 3731 EXPECT_EQ(net::ERR_INVALID_URL, result.error_code);
3744 } 3732 }
3745 3733
3746 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoCancel) { 3734 TEST_P(ResourceDispatcherHostTest, SyncLoadWithMojoCancel) {
3747 ResourceRequest request = CreateResourceRequest( 3735 ResourceRequest request = CreateResourceRequest(
3748 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_error()); 3736 "GET", RESOURCE_TYPE_XHR, net::URLRequestTestJob::test_url_error());
3749 request.priority = net::MAXIMUM_PRIORITY; 3737 request.priority = net::MAXIMUM_PRIORITY;
3750 3738
3751 bool called = false; 3739 bool called = false;
3752 bool was_null = false; 3740 bool was_null = false;
3753 SyncLoadResult result; 3741 SyncLoadResult result;
3754 host_.OnSyncLoadWithMojo(0, 1, request, filter_.get(), 3742 host_.OnSyncLoadWithMojo(
3755 base::Bind(&StoreSyncLoadResult, 3743 0, 1, request,
3756 &called, &was_null, &result)); 3744 ResourceRequesterInfo::CreateForRenderer(filter_->GetWeakPtr()),
3745 base::Bind(&StoreSyncLoadResult, &called, &was_null, &result));
3757 host_.CancelRequestsForProcess(filter_->child_id()); 3746 host_.CancelRequestsForProcess(filter_->child_id());
3758 base::RunLoop().RunUntilIdle(); 3747 base::RunLoop().RunUntilIdle();
3759 EXPECT_TRUE(called); 3748 EXPECT_TRUE(called);
3760 EXPECT_TRUE(was_null); 3749 EXPECT_TRUE(was_null);
3761 } 3750 }
3762 3751
3763 // A URLRequestTestJob that sets a test certificate on the |ssl_info| 3752 // A URLRequestTestJob that sets a test certificate on the |ssl_info|
3764 // field of the response. 3753 // field of the response.
3765 class TestHTTPSURLRequestJob : public net::URLRequestTestJob { 3754 class TestHTTPSURLRequestJob : public net::URLRequestTestJob {
3766 public: 3755 public:
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
3843 return nullptr; 3832 return nullptr;
3844 } 3833 }
3845 3834
3846 INSTANTIATE_TEST_CASE_P( 3835 INSTANTIATE_TEST_CASE_P(
3847 ResourceDispatcherHostTests, 3836 ResourceDispatcherHostTests,
3848 ResourceDispatcherHostTest, 3837 ResourceDispatcherHostTest,
3849 testing::Values(TestConfig::kDefault, 3838 testing::Values(TestConfig::kDefault,
3850 TestConfig::kOptimizeIPCForSmallResourceEnabled)); 3839 TestConfig::kOptimizeIPCForSmallResourceEnabled));
3851 3840
3852 } // namespace content 3841 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698