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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 23 #include "content/browser/child_process_security_policy_impl.h" |
23 #include "content/browser/loader/mojo_async_resource_handler.h" | 24 #include "content/browser/loader/mojo_async_resource_handler.h" |
24 #include "content/browser/loader/navigation_resource_throttle.h" | 25 #include "content/browser/loader/navigation_resource_throttle.h" |
25 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 26 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
26 #include "content/browser/loader/resource_message_filter.h" | 27 #include "content/browser/loader/resource_message_filter.h" |
27 #include "content/browser/loader/resource_request_info_impl.h" | 28 #include "content/browser/loader/resource_request_info_impl.h" |
28 #include "content/browser/loader/test_url_loader_client.h" | 29 #include "content/browser/loader/test_url_loader_client.h" |
29 #include "content/browser/loader_delegate_impl.h" | 30 #include "content/browser/loader_delegate_impl.h" |
30 #include "content/common/resource_request.h" | 31 #include "content/common/resource_request.h" |
31 #include "content/common/resource_request_completion_status.h" | 32 #include "content/common/resource_request_completion_status.h" |
32 #include "content/common/url_loader.mojom.h" | 33 #include "content/common/url_loader.mojom.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 nullptr, | 87 nullptr, |
87 nullptr, | 88 nullptr, |
88 nullptr, | 89 nullptr, |
89 nullptr, | 90 nullptr, |
90 base::Bind(&URLLoaderFactoryImplTest::GetContexts, | 91 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
91 base::Unretained(this)))) { | 92 base::Unretained(this)))) { |
| 93 // Some tests specify request.report_raw_headers, but the RDH checks the |
| 94 // CanReadRawCookies permission before enabling it. |
| 95 ChildProcessSecurityPolicyImpl::GetInstance()->Add(kChildId); |
| 96 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadRawCookies( |
| 97 kChildId); |
| 98 |
92 resource_message_filter_->InitializeForTest(); | 99 resource_message_filter_->InitializeForTest(); |
93 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); | 100 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); |
94 rdh_.SetLoaderDelegate(&loader_deleate_); | 101 rdh_.SetLoaderDelegate(&loader_deleate_); |
95 | 102 |
96 URLLoaderFactoryImpl::Create( | 103 URLLoaderFactoryImpl::Create( |
97 resource_message_filter_->requester_info_for_test(), | 104 resource_message_filter_->requester_info_for_test(), |
98 mojo::GetProxy(&factory_)); | 105 mojo::GetProxy(&factory_)); |
99 | 106 |
100 // Calling this function creates a request context. | 107 // Calling this function creates a request context. |
101 browser_context_->GetResourceContext()->GetRequestContext(); | 108 browser_context_->GetResourceContext()->GetRequestContext(); |
102 base::RunLoop().RunUntilIdle(); | 109 base::RunLoop().RunUntilIdle(); |
103 } | 110 } |
104 | 111 |
105 ~URLLoaderFactoryImplTest() override { | 112 ~URLLoaderFactoryImplTest() override { |
| 113 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(kChildId); |
106 rdh_.SetDelegate(nullptr); | 114 rdh_.SetDelegate(nullptr); |
107 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 115 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
108 | 116 |
109 resource_message_filter_->OnChannelClosing(); | 117 resource_message_filter_->OnChannelClosing(); |
110 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); | 118 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); |
111 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
112 MojoAsyncResourceHandler::SetAllocationSizeForTesting( | 120 MojoAsyncResourceHandler::SetAllocationSizeForTesting( |
113 MojoAsyncResourceHandler::kDefaultAllocationSize); | 121 MojoAsyncResourceHandler::kDefaultAllocationSize); |
114 } | 122 } |
115 | 123 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 break; | 205 break; |
198 if (r == MOJO_RESULT_SHOULD_WAIT) | 206 if (r == MOJO_RESULT_SHOULD_WAIT) |
199 continue; | 207 continue; |
200 ASSERT_EQ(MOJO_RESULT_OK, r); | 208 ASSERT_EQ(MOJO_RESULT_OK, r); |
201 contents += std::string(buffer, read_size); | 209 contents += std::string(buffer, read_size); |
202 } | 210 } |
203 std::string expected; | 211 std::string expected; |
204 base::ReadFileToString( | 212 base::ReadFileToString( |
205 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 213 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
206 EXPECT_EQ(expected, contents); | 214 EXPECT_EQ(expected, contents); |
207 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 215 EXPECT_EQ(static_cast<int64_t>(expected.size()) + |
| 216 client.response_head().encoded_data_length, |
208 client.completion_status().encoded_data_length); | 217 client.completion_status().encoded_data_length); |
209 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 218 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
210 client.completion_status().encoded_body_length); | 219 client.completion_status().encoded_body_length); |
| 220 // OnTransferSizeUpdated is not dispatched as report_raw_headers is not set. |
| 221 EXPECT_EQ(0, client.body_transfer_size()); |
| 222 EXPECT_GT(client.response_head().encoded_data_length, 0); |
| 223 EXPECT_GT(client.completion_status().encoded_data_length, 0); |
211 } | 224 } |
212 | 225 |
213 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 226 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
214 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 227 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
215 mojom::URLLoaderAssociatedPtr loader; | 228 mojom::URLLoaderAssociatedPtr loader; |
216 ResourceRequest request; | 229 ResourceRequest request; |
217 TestURLLoaderClient client; | 230 TestURLLoaderClient client; |
218 net::URLRequestFailedJob::AddUrlHandler(); | 231 net::URLRequestFailedJob::AddUrlHandler(); |
219 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 232 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
220 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 233 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 388 |
376 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 389 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
377 client.download_data_length()); | 390 client.download_data_length()); |
378 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 391 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
379 client.encoded_download_data_length()); | 392 client.encoded_download_data_length()); |
380 | 393 |
381 std::string expected; | 394 std::string expected; |
382 base::ReadFileToString( | 395 base::ReadFileToString( |
383 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 396 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
384 EXPECT_EQ(expected, contents); | 397 EXPECT_EQ(expected, contents); |
385 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 398 EXPECT_EQ(static_cast<int64_t>(expected.size()) + |
| 399 client.response_head().encoded_data_length, |
386 client.completion_status().encoded_data_length); | 400 client.completion_status().encoded_data_length); |
387 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 401 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
388 client.completion_status().encoded_body_length); | 402 client.completion_status().encoded_body_length); |
389 } | 403 } |
390 | 404 |
391 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { | 405 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { |
392 constexpr int32_t kRoutingId = 1; | 406 constexpr int32_t kRoutingId = 1; |
393 constexpr int32_t kRequestId = 2; | 407 constexpr int32_t kRequestId = 2; |
394 | 408 |
395 mojom::URLLoaderAssociatedPtr loader; | 409 mojom::URLLoaderAssociatedPtr loader; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 | 454 |
441 url_request->Cancel(); | 455 url_request->Cancel(); |
442 client.RunUntilComplete(); | 456 client.RunUntilComplete(); |
443 | 457 |
444 ASSERT_TRUE(client.has_received_completion()); | 458 ASSERT_TRUE(client.has_received_completion()); |
445 | 459 |
446 EXPECT_EQ(200, client.response_head().headers->response_code()); | 460 EXPECT_EQ(200, client.response_head().headers->response_code()); |
447 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 461 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
448 } | 462 } |
449 | 463 |
| 464 TEST_P(URLLoaderFactoryImplTest, OnTransferSizeUpdated) { |
| 465 constexpr int32_t kRoutingId = 81; |
| 466 constexpr int32_t kRequestId = 28; |
| 467 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
| 468 mojom::URLLoaderAssociatedPtr loader; |
| 469 base::FilePath root; |
| 470 PathService::Get(DIR_TEST_DATA, &root); |
| 471 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
| 472 BrowserThread::GetBlockingPool()); |
| 473 ResourceRequest request; |
| 474 TestURLLoaderClient client; |
| 475 // Assume the file contents is small enough to be stored in the data pipe. |
| 476 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz"); |
| 477 request.method = "GET"; |
| 478 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 479 // enabled, the url scheme of frame type requests from the renderer process |
| 480 // must be blob scheme. |
| 481 request.resource_type = RESOURCE_TYPE_XHR; |
| 482 // Need to set |request_initiator| for non main frame type request. |
| 483 request.request_initiator = url::Origin(); |
| 484 request.report_raw_headers = true; |
| 485 factory_->CreateLoaderAndStart( |
| 486 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, |
| 487 kRequestId, request, |
| 488 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
| 489 |
| 490 client.RunUntilComplete(); |
| 491 |
| 492 std::string contents; |
| 493 while (true) { |
| 494 char buffer[16]; |
| 495 uint32_t read_size = sizeof(buffer); |
| 496 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size, |
| 497 MOJO_READ_DATA_FLAG_NONE); |
| 498 if (r == MOJO_RESULT_FAILED_PRECONDITION) |
| 499 break; |
| 500 if (r == MOJO_RESULT_SHOULD_WAIT) |
| 501 continue; |
| 502 ASSERT_EQ(MOJO_RESULT_OK, r); |
| 503 contents.append(buffer, read_size); |
| 504 } |
| 505 |
| 506 std::string expected_encoded_body; |
| 507 base::ReadFileToString( |
| 508 root.Append(base::FilePath(FILE_PATH_LITERAL("gzip-content.svgz"))), |
| 509 &expected_encoded_body); |
| 510 |
| 511 EXPECT_GT(client.response_head().encoded_data_length, 0); |
| 512 EXPECT_GT(client.completion_status().encoded_data_length, 0); |
| 513 EXPECT_EQ(static_cast<int64_t>(expected_encoded_body.size()), |
| 514 client.body_transfer_size()); |
| 515 EXPECT_EQ(200, client.response_head().headers->response_code()); |
| 516 EXPECT_EQ( |
| 517 client.response_head().encoded_data_length + client.body_transfer_size(), |
| 518 client.completion_status().encoded_data_length); |
| 519 EXPECT_NE(client.body_transfer_size(), static_cast<int64_t>(contents.size())); |
| 520 EXPECT_EQ(client.body_transfer_size(), |
| 521 client.completion_status().encoded_body_length); |
| 522 EXPECT_EQ(contents, "Hello World!\n"); |
| 523 } |
| 524 |
450 // Removing the loader in the remote side will cancel the request. | 525 // Removing the loader in the remote side will cancel the request. |
451 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) { | 526 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) { |
452 constexpr int32_t kRoutingId = 81; | 527 constexpr int32_t kRoutingId = 81; |
453 constexpr int32_t kRequestId = 28; | 528 constexpr int32_t kRequestId = 28; |
454 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 529 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
455 mojom::URLLoaderAssociatedPtr loader; | 530 mojom::URLLoaderAssociatedPtr loader; |
456 base::FilePath root; | 531 base::FilePath root; |
457 PathService::Get(DIR_TEST_DATA, &root); | 532 PathService::Get(DIR_TEST_DATA, &root); |
458 net::URLRequestFailedJob::AddUrlHandler(); | 533 net::URLRequestFailedJob::AddUrlHandler(); |
459 ResourceRequest request; | 534 ResourceRequest request; |
(...skipping 24 matching lines...) Expand all Loading... |
484 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 559 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
485 } | 560 } |
486 | 561 |
487 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 562 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
488 URLLoaderFactoryImplTest, | 563 URLLoaderFactoryImplTest, |
489 ::testing::Values(128, 32 * 1024)); | 564 ::testing::Values(128, 32 * 1024)); |
490 | 565 |
491 } // namespace | 566 } // namespace |
492 | 567 |
493 } // namespace content | 568 } // namespace content |
OLD | NEW |