OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/loader/url_loader_factory_impl.h" | 5 #include "content/browser/loader/url_loader_factory_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 15 matching lines...) Expand all Loading... | |
26 #include "content/browser/loader/resource_message_filter.h" | 26 #include "content/browser/loader/resource_message_filter.h" |
27 #include "content/browser/loader/resource_request_info_impl.h" | 27 #include "content/browser/loader/resource_request_info_impl.h" |
28 #include "content/browser/loader/test_url_loader_client.h" | 28 #include "content/browser/loader/test_url_loader_client.h" |
29 #include "content/browser/loader_delegate_impl.h" | 29 #include "content/browser/loader_delegate_impl.h" |
30 #include "content/common/resource_request.h" | 30 #include "content/common/resource_request.h" |
31 #include "content/common/resource_request_completion_status.h" | 31 #include "content/common/resource_request_completion_status.h" |
32 #include "content/common/url_loader.mojom.h" | 32 #include "content/common/url_loader.mojom.h" |
33 #include "content/public/browser/resource_context.h" | 33 #include "content/public/browser/resource_context.h" |
34 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 34 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
35 #include "content/public/common/content_paths.h" | 35 #include "content/public/common/content_paths.h" |
36 #include "content/public/common/process_type.h" | |
37 #include "content/public/test/test_browser_context.h" | 36 #include "content/public/test/test_browser_context.h" |
38 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
39 #include "mojo/public/c/system/data_pipe.h" | 38 #include "mojo/public/c/system/data_pipe.h" |
40 #include "mojo/public/c/system/types.h" | 39 #include "mojo/public/c/system/types.h" |
41 #include "mojo/public/cpp/bindings/binding.h" | 40 #include "mojo/public/cpp/bindings/binding.h" |
42 #include "mojo/public/cpp/system/data_pipe.h" | 41 #include "mojo/public/cpp/system/data_pipe.h" |
43 #include "net/base/io_buffer.h" | 42 #include "net/base/io_buffer.h" |
44 #include "net/base/net_errors.h" | 43 #include "net/base/net_errors.h" |
45 #include "net/http/http_response_headers.h" | 44 #include "net/http/http_response_headers.h" |
46 #include "net/http/http_response_info.h" | 45 #include "net/http/http_response_info.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
66 bool ShouldBeginRequest(const std::string& method, | 65 bool ShouldBeginRequest(const std::string& method, |
67 const GURL& url, | 66 const GURL& url, |
68 ResourceType resource_type, | 67 ResourceType resource_type, |
69 ResourceContext* resource_context) override { | 68 ResourceContext* resource_context) override { |
70 return false; | 69 return false; |
71 } | 70 } |
72 | 71 |
73 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); | 72 DISALLOW_COPY_AND_ASSIGN(RejectingResourceDispatcherHostDelegate); |
74 }; | 73 }; |
75 | 74 |
75 } // namespace | |
76 | |
76 // The test parameter is the number of bytes allocated for the buffer in the | 77 // The test parameter is the number of bytes allocated for the buffer in the |
77 // data pipe, for testing the case where the allocated size is smaller than the | 78 // data pipe, for testing the case where the allocated size is smaller than the |
78 // size the mime sniffer *implicitly* requires. | 79 // size the mime sniffer *implicitly* requires. |
79 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { | 80 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { |
80 public: | 81 public: |
81 URLLoaderFactoryImplTest() | 82 URLLoaderFactoryImplTest() |
82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 83 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
83 browser_context_(new TestBrowserContext()), | 84 browser_context_(new TestBrowserContext()), |
84 resource_message_filter_(new ResourceMessageFilter( | 85 resource_message_filter_(new ResourceMessageFilter( |
85 kChildId, | 86 kChildId, |
86 // If browser side navigation is enabled then | |
87 // ResourceDispatcherHostImpl prevents main frame URL requests from | |
88 // the renderer. Ensure that these checks don't trip us up by | |
89 // setting the process type in ResourceMessageFilter as | |
90 // PROCESS_TYPE_UNKNOWN. | |
91 PROCESS_TYPE_UNKNOWN, | |
92 nullptr, | 87 nullptr, |
93 nullptr, | 88 nullptr, |
94 nullptr, | 89 nullptr, |
95 nullptr, | 90 nullptr, |
96 base::Bind(&URLLoaderFactoryImplTest::GetContexts, | 91 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
97 base::Unretained(this)))) { | 92 base::Unretained(this)))) { |
93 resource_message_filter_->InitializeOnIOThread(); | |
98 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); | 94 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); |
99 rdh_.SetLoaderDelegate(&loader_deleate_); | 95 rdh_.SetLoaderDelegate(&loader_deleate_); |
100 | 96 |
101 URLLoaderFactoryImpl::Create(resource_message_filter_, | 97 URLLoaderFactoryImpl::Create(resource_message_filter_->requester_info(), |
102 mojo::GetProxy(&factory_)); | 98 mojo::GetProxy(&factory_)); |
103 | 99 |
104 // Calling this function creates a request context. | 100 // Calling this function creates a request context. |
105 browser_context_->GetResourceContext()->GetRequestContext(); | 101 browser_context_->GetResourceContext()->GetRequestContext(); |
106 base::RunLoop().RunUntilIdle(); | 102 base::RunLoop().RunUntilIdle(); |
107 } | 103 } |
108 | 104 |
109 ~URLLoaderFactoryImplTest() override { | 105 ~URLLoaderFactoryImplTest() override { |
110 rdh_.SetDelegate(nullptr); | 106 rdh_.SetDelegate(nullptr); |
111 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 107 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
(...skipping 30 matching lines...) Expand all Loading... | |
142 mojom::URLLoaderAssociatedPtr loader; | 138 mojom::URLLoaderAssociatedPtr loader; |
143 base::FilePath root; | 139 base::FilePath root; |
144 PathService::Get(DIR_TEST_DATA, &root); | 140 PathService::Get(DIR_TEST_DATA, &root); |
145 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | 141 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
146 BrowserThread::GetBlockingPool()); | 142 BrowserThread::GetBlockingPool()); |
147 ResourceRequest request; | 143 ResourceRequest request; |
148 TestURLLoaderClient client; | 144 TestURLLoaderClient client; |
149 // Assume the file contents is small enough to be stored in the data pipe. | 145 // Assume the file contents is small enough to be stored in the data pipe. |
150 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | 146 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
151 request.method = "GET"; | 147 request.method = "GET"; |
152 request.is_main_frame = true; | 148 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
149 // enabled, the url scheme of frame type requests from the renderer process | |
150 // must be blob scheme. | |
151 request.resource_type = RESOURCE_TYPE_XHR; | |
152 // Need to set |request_initiator| for non main frame type request. | |
153 request.request_initiator = url::Origin(); | |
mmenke
2016/11/21 19:51:56
include url/origin.h
horo
2016/11/22 01:12:08
Done.
| |
153 factory_->CreateLoaderAndStart( | 154 factory_->CreateLoaderAndStart( |
154 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, | 155 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, |
155 kRequestId, request, | 156 kRequestId, request, |
156 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 157 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
157 | 158 |
158 ASSERT_FALSE(client.has_received_response()); | 159 ASSERT_FALSE(client.has_received_response()); |
159 ASSERT_FALSE(client.response_body().is_valid()); | 160 ASSERT_FALSE(client.response_body().is_valid()); |
160 ASSERT_FALSE(client.has_received_completion()); | 161 ASSERT_FALSE(client.has_received_completion()); |
161 | 162 |
162 client.RunUntilResponseReceived(); | 163 client.RunUntilResponseReceived(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 | 208 |
208 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 209 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
209 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 210 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
210 mojom::URLLoaderAssociatedPtr loader; | 211 mojom::URLLoaderAssociatedPtr loader; |
211 ResourceRequest request; | 212 ResourceRequest request; |
212 TestURLLoaderClient client; | 213 TestURLLoaderClient client; |
213 net::URLRequestFailedJob::AddUrlHandler(); | 214 net::URLRequestFailedJob::AddUrlHandler(); |
214 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 215 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
215 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 216 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
216 request.method = "GET"; | 217 request.method = "GET"; |
218 // |resource_type| can't be a frame type. It is because when PlzNavigate is | |
219 // enabled, the url scheme of frame type requests from the renderer process | |
220 // must be blob scheme. | |
221 request.resource_type = RESOURCE_TYPE_XHR; | |
222 // Need to set |request_initiator| for non main frame type request. | |
223 request.request_initiator = url::Origin(); | |
217 factory_->CreateLoaderAndStart( | 224 factory_->CreateLoaderAndStart( |
218 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 225 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
219 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 226 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
220 | 227 |
221 client.RunUntilComplete(); | 228 client.RunUntilComplete(); |
222 ASSERT_FALSE(client.has_received_response()); | 229 ASSERT_FALSE(client.has_received_response()); |
223 ASSERT_FALSE(client.response_body().is_valid()); | 230 ASSERT_FALSE(client.response_body().is_valid()); |
224 | 231 |
225 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 232 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
226 } | 233 } |
227 | 234 |
228 // This test tests a case where resource loading is cancelled before started. | 235 // This test tests a case where resource loading is cancelled before started. |
229 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 236 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
230 mojom::URLLoaderAssociatedPtr loader; | 237 mojom::URLLoaderAssociatedPtr loader; |
231 ResourceRequest request; | 238 ResourceRequest request; |
232 TestURLLoaderClient client; | 239 TestURLLoaderClient client; |
233 request.url = GURL(); | 240 request.url = GURL(); |
234 request.method = "GET"; | 241 request.method = "GET"; |
242 // |resource_type| can't be a frame type. It is because when PlzNavigate is | |
243 // enabled, the url scheme of frame type requests from the renderer process | |
244 // must be blob scheme. | |
245 request.resource_type = RESOURCE_TYPE_XHR; | |
246 // Need to set |request_initiator| for non main frame type request. | |
247 request.request_initiator = url::Origin(); | |
235 ASSERT_FALSE(request.url.is_valid()); | 248 ASSERT_FALSE(request.url.is_valid()); |
236 factory_->CreateLoaderAndStart( | 249 factory_->CreateLoaderAndStart( |
237 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 250 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
238 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 251 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
239 | 252 |
240 client.RunUntilComplete(); | 253 client.RunUntilComplete(); |
241 ASSERT_FALSE(client.has_received_response()); | 254 ASSERT_FALSE(client.has_received_response()); |
242 ASSERT_FALSE(client.response_body().is_valid()); | 255 ASSERT_FALSE(client.response_body().is_valid()); |
243 | 256 |
244 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 257 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
245 } | 258 } |
246 | 259 |
247 // This test tests a case where resource loading is cancelled before started. | 260 // This test tests a case where resource loading is cancelled before started. |
248 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { | 261 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
249 mojom::URLLoaderAssociatedPtr loader; | 262 mojom::URLLoaderAssociatedPtr loader; |
250 RejectingResourceDispatcherHostDelegate rdh_delegate; | 263 RejectingResourceDispatcherHostDelegate rdh_delegate; |
251 rdh_.SetDelegate(&rdh_delegate); | 264 rdh_.SetDelegate(&rdh_delegate); |
252 ResourceRequest request; | 265 ResourceRequest request; |
253 TestURLLoaderClient client; | 266 TestURLLoaderClient client; |
254 request.url = GURL("http://localhost/"); | 267 request.url = GURL("http://localhost/"); |
255 request.method = "GET"; | 268 request.method = "GET"; |
269 // |resource_type| can't be a frame type. It is because when PlzNavigate is | |
270 // enabled, the url scheme of frame type requests from the renderer process | |
271 // must be blob scheme. | |
272 request.resource_type = RESOURCE_TYPE_XHR; | |
273 // Need to set |request_initiator| for non main frame type request. | |
274 request.request_initiator = url::Origin(); | |
256 factory_->CreateLoaderAndStart( | 275 factory_->CreateLoaderAndStart( |
257 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 276 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
258 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 277 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
259 | 278 |
260 client.RunUntilComplete(); | 279 client.RunUntilComplete(); |
261 rdh_.SetDelegate(nullptr); | 280 rdh_.SetDelegate(nullptr); |
262 | 281 |
263 ASSERT_FALSE(client.has_received_response()); | 282 ASSERT_FALSE(client.has_received_response()); |
264 ASSERT_FALSE(client.response_body().is_valid()); | 283 ASSERT_FALSE(client.response_body().is_valid()); |
265 | 284 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
397 mojom::URLLoaderAssociatedPtr loader; | 416 mojom::URLLoaderAssociatedPtr loader; |
398 base::FilePath root; | 417 base::FilePath root; |
399 PathService::Get(DIR_TEST_DATA, &root); | 418 PathService::Get(DIR_TEST_DATA, &root); |
400 net::URLRequestFailedJob::AddUrlHandler(); | 419 net::URLRequestFailedJob::AddUrlHandler(); |
401 ResourceRequest request; | 420 ResourceRequest request; |
402 TestURLLoaderClient client; | 421 TestURLLoaderClient client; |
403 // Assume the file contents is small enough to be stored in the data pipe. | 422 // Assume the file contents is small enough to be stored in the data pipe. |
404 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); | 423 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); |
405 request.method = "GET"; | 424 request.method = "GET"; |
406 request.is_main_frame = true; | 425 request.is_main_frame = true; |
426 // |resource_type| can't be a frame type. It is because when PlzNavigate is | |
427 // enabled, the url scheme of frame type requests from the renderer process | |
428 // must be blob scheme. | |
429 request.resource_type = RESOURCE_TYPE_XHR; | |
430 // Need to set |request_initiator| for non main frame type request. | |
431 request.request_initiator = url::Origin(); | |
407 factory_->CreateLoaderAndStart( | 432 factory_->CreateLoaderAndStart( |
408 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, | 433 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, |
409 kRequestId, request, | 434 kRequestId, request, |
410 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 435 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
411 | 436 |
412 base::RunLoop().RunUntilIdle(); | 437 base::RunLoop().RunUntilIdle(); |
413 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 438 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
414 ASSERT_FALSE(client.has_received_response()); | 439 ASSERT_FALSE(client.has_received_response()); |
415 ASSERT_FALSE(client.response_body().is_valid()); | 440 ASSERT_FALSE(client.response_body().is_valid()); |
416 ASSERT_FALSE(client.has_received_completion()); | 441 ASSERT_FALSE(client.has_received_completion()); |
417 | 442 |
418 loader = nullptr; | 443 loader = nullptr; |
419 base::RunLoop().RunUntilIdle(); | 444 base::RunLoop().RunUntilIdle(); |
420 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 445 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
421 } | 446 } |
422 | 447 |
423 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 448 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
424 URLLoaderFactoryImplTest, | 449 URLLoaderFactoryImplTest, |
425 ::testing::Values(128, 32 * 1024)); | 450 ::testing::Values(128, 32 * 1024)); |
426 | 451 |
427 } // namespace | |
428 | |
429 } // namespace content | 452 } // namespace content |
OLD | NEW |