| 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 "content/child/resource_dispatcher.h" | 5 #include "content/child/resource_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <tuple> | 12 #include <tuple> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/feature_list.h" | 16 #include "base/feature_list.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/shared_memory.h" | 19 #include "base/memory/shared_memory.h" |
| 20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
| 22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 23 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 24 #include "content/child/request_extra_data.h" | 24 #include "content/child/request_extra_data.h" |
| 25 #include "content/child/request_info.h" | |
| 26 #include "content/common/appcache_interfaces.h" | 25 #include "content/common/appcache_interfaces.h" |
| 27 #include "content/common/resource_messages.h" | 26 #include "content/common/resource_messages.h" |
| 28 #include "content/common/resource_request.h" | 27 #include "content/common/resource_request.h" |
| 29 #include "content/common/resource_request_completion_status.h" | 28 #include "content/common/resource_request_completion_status.h" |
| 30 #include "content/common/service_worker/service_worker_types.h" | 29 #include "content/common/service_worker/service_worker_types.h" |
| 31 #include "content/public/child/fixed_received_data.h" | 30 #include "content/public/child/fixed_received_data.h" |
| 32 #include "content/public/child/request_peer.h" | 31 #include "content/public/child/request_peer.h" |
| 33 #include "content/public/child/resource_dispatcher_delegate.h" | 32 #include "content/public/child/resource_dispatcher_delegate.h" |
| 34 #include "content/public/common/content_features.h" | 33 #include "content/public/common/content_features.h" |
| 34 #include "content/public/common/request_context_frame_type.h" |
| 35 #include "content/public/common/resource_response.h" | 35 #include "content/public/common/resource_response.h" |
| 36 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 37 #include "net/base/request_priority.h" |
| 37 #include "net/http/http_response_headers.h" | 38 #include "net/http/http_response_headers.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 40 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
| 41 #include "url/gurl.h" |
| 39 | 42 |
| 40 namespace content { | 43 namespace content { |
| 41 | 44 |
| 42 static const char kTestPageUrl[] = "http://www.google.com/"; | 45 static const char kTestPageUrl[] = "http://www.google.com/"; |
| 43 static const char kTestPageHeaders[] = | 46 static const char kTestPageHeaders[] = |
| 44 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; | 47 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; |
| 45 static const char kTestPageMimeType[] = "text/html"; | 48 static const char kTestPageMimeType[] = "text/html"; |
| 46 static const char kTestPageCharset[] = ""; | 49 static const char kTestPageCharset[] = ""; |
| 47 static const char kTestPageContents[] = | 50 static const char kTestPageContents[] = |
| 48 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; | 51 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 void NotifyRequestComplete(int request_id, size_t total_size) { | 300 void NotifyRequestComplete(int request_id, size_t total_size) { |
| 298 ResourceRequestCompletionStatus request_complete_data; | 301 ResourceRequestCompletionStatus request_complete_data; |
| 299 request_complete_data.error_code = net::OK; | 302 request_complete_data.error_code = net::OK; |
| 300 request_complete_data.was_ignored_by_handler = false; | 303 request_complete_data.was_ignored_by_handler = false; |
| 301 request_complete_data.exists_in_cache = false; | 304 request_complete_data.exists_in_cache = false; |
| 302 request_complete_data.encoded_data_length = total_size; | 305 request_complete_data.encoded_data_length = total_size; |
| 303 EXPECT_TRUE(dispatcher_->OnMessageReceived( | 306 EXPECT_TRUE(dispatcher_->OnMessageReceived( |
| 304 ResourceMsg_RequestComplete(request_id, request_complete_data))); | 307 ResourceMsg_RequestComplete(request_id, request_complete_data))); |
| 305 } | 308 } |
| 306 | 309 |
| 307 RequestInfo* CreateRequestInfo(bool download_to_file) { | 310 std::unique_ptr<ResourceRequest> CreateResourceRequest( |
| 308 RequestInfo* request_info = new RequestInfo(); | 311 bool download_to_file) { |
| 309 request_info->method = "GET"; | 312 std::unique_ptr<ResourceRequest> request(new ResourceRequest()); |
| 310 request_info->url = GURL(kTestPageUrl); | |
| 311 request_info->first_party_for_cookies = GURL(kTestPageUrl); | |
| 312 request_info->referrer = Referrer(); | |
| 313 request_info->headers = std::string(); | |
| 314 request_info->load_flags = 0; | |
| 315 request_info->requestor_pid = 0; | |
| 316 request_info->request_type = RESOURCE_TYPE_SUB_RESOURCE; | |
| 317 request_info->appcache_host_id = kAppCacheNoHostId; | |
| 318 request_info->should_reset_appcache = false; | |
| 319 request_info->routing_id = 0; | |
| 320 request_info->download_to_file = download_to_file; | |
| 321 RequestExtraData extra_data; | |
| 322 | 313 |
| 323 return request_info; | 314 request->method = "GET"; |
| 315 request->url = GURL(kTestPageUrl); |
| 316 request->first_party_for_cookies = GURL(kTestPageUrl); |
| 317 request->referrer_policy = blink::WebReferrerPolicyDefault; |
| 318 request->resource_type = RESOURCE_TYPE_SUB_RESOURCE; |
| 319 request->priority = net::LOW; |
| 320 request->fetch_request_mode = FETCH_REQUEST_MODE_NO_CORS; |
| 321 request->fetch_frame_type = REQUEST_CONTEXT_FRAME_TYPE_NONE; |
| 322 request->download_to_file = download_to_file; |
| 323 |
| 324 const RequestExtraData extra_data; |
| 325 extra_data.CopyToResourceRequest(request.get()); |
| 326 |
| 327 return request; |
| 324 } | 328 } |
| 325 | 329 |
| 326 ResourceDispatcher* dispatcher() { return dispatcher_.get(); } | 330 ResourceDispatcher* dispatcher() { return dispatcher_.get(); } |
| 327 | 331 |
| 328 int StartAsync(const RequestInfo& request_info, | 332 int StartAsync(std::unique_ptr<ResourceRequest> request, |
| 329 ResourceRequestBodyImpl* request_body, | 333 ResourceRequestBodyImpl* request_body, |
| 330 TestRequestPeer::Context* peer_context) { | 334 TestRequestPeer::Context* peer_context) { |
| 331 std::unique_ptr<TestRequestPeer> peer( | 335 std::unique_ptr<TestRequestPeer> peer( |
| 332 new TestRequestPeer(dispatcher(), peer_context)); | 336 new TestRequestPeer(dispatcher(), peer_context)); |
| 333 int request_id = dispatcher()->StartAsync( | 337 int request_id = dispatcher()->StartAsync( |
| 334 request_info, request_body, std::move(peer), | 338 std::move(request), 0, nullptr, GURL(), std::move(peer), |
| 335 blink::WebURLRequest::LoadingIPCType::ChromeIPC, nullptr); | 339 blink::WebURLRequest::LoadingIPCType::ChromeIPC, nullptr); |
| 336 peer_context->request_id = request_id; | 340 peer_context->request_id = request_id; |
| 337 return request_id; | 341 return request_id; |
| 338 } | 342 } |
| 339 | 343 |
| 340 private: | 344 private: |
| 341 // Map of request IDs to shared memory. | 345 // Map of request IDs to shared memory. |
| 342 std::map<int, base::SharedMemory*> shared_memory_map_; | 346 std::map<int, base::SharedMemory*> shared_memory_map_; |
| 343 | 347 |
| 344 std::vector<IPC::Message> message_queue_; | 348 std::vector<IPC::Message> message_queue_; |
| 345 base::MessageLoop message_loop_; | 349 base::MessageLoop message_loop_; |
| 346 std::unique_ptr<ResourceDispatcher> dispatcher_; | 350 std::unique_ptr<ResourceDispatcher> dispatcher_; |
| 347 }; | 351 }; |
| 348 | 352 |
| 349 // Does a simple request and tests that the correct data is received. Simulates | 353 // Does a simple request and tests that the correct data is received. Simulates |
| 350 // two reads. | 354 // two reads. |
| 351 TEST_F(ResourceDispatcherTest, RoundTrip) { | 355 TEST_F(ResourceDispatcherTest, RoundTrip) { |
| 352 // Number of bytes received in the first read. | 356 // Number of bytes received in the first read. |
| 353 const size_t kFirstReceiveSize = 2; | 357 const size_t kFirstReceiveSize = 2; |
| 354 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents)); | 358 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents)); |
| 355 | 359 |
| 356 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 360 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 357 TestRequestPeer::Context peer_context; | 361 TestRequestPeer::Context peer_context; |
| 358 StartAsync(*request_info.get(), NULL, &peer_context); | 362 StartAsync(std::move(request), NULL, &peer_context); |
| 359 | 363 |
| 360 int id = ConsumeRequestResource(); | 364 int id = ConsumeRequestResource(); |
| 361 EXPECT_EQ(0u, queued_messages()); | 365 EXPECT_EQ(0u, queued_messages()); |
| 362 | 366 |
| 363 NotifyReceivedResponse(id); | 367 NotifyReceivedResponse(id); |
| 364 EXPECT_EQ(0u, queued_messages()); | 368 EXPECT_EQ(0u, queued_messages()); |
| 365 EXPECT_TRUE(peer_context.received_response); | 369 EXPECT_TRUE(peer_context.received_response); |
| 366 | 370 |
| 367 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 371 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| 368 NotifyDataReceived(id, std::string(kTestPageContents, kFirstReceiveSize)); | 372 NotifyDataReceived(id, std::string(kTestPageContents, kFirstReceiveSize)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 379 EXPECT_EQ(0u, queued_messages()); | 383 EXPECT_EQ(0u, queued_messages()); |
| 380 } | 384 } |
| 381 | 385 |
| 382 // A simple request with an inline data response. | 386 // A simple request with an inline data response. |
| 383 TEST_F(ResourceDispatcherTest, ResponseWithInlinedData) { | 387 TEST_F(ResourceDispatcherTest, ResponseWithInlinedData) { |
| 384 auto feature_list = base::MakeUnique<base::FeatureList>(); | 388 auto feature_list = base::MakeUnique<base::FeatureList>(); |
| 385 feature_list->InitializeFromCommandLine( | 389 feature_list->InitializeFromCommandLine( |
| 386 features::kOptimizeLoadingIPCForSmallResources.name, std::string()); | 390 features::kOptimizeLoadingIPCForSmallResources.name, std::string()); |
| 387 base::FeatureList::ClearInstanceForTesting(); | 391 base::FeatureList::ClearInstanceForTesting(); |
| 388 base::FeatureList::SetInstance(std::move(feature_list)); | 392 base::FeatureList::SetInstance(std::move(feature_list)); |
| 389 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 393 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 390 TestRequestPeer::Context peer_context; | 394 TestRequestPeer::Context peer_context; |
| 391 StartAsync(*request_info.get(), NULL, &peer_context); | 395 StartAsync(std::move(request), NULL, &peer_context); |
| 392 | 396 |
| 393 int id = ConsumeRequestResource(); | 397 int id = ConsumeRequestResource(); |
| 394 EXPECT_EQ(0u, queued_messages()); | 398 EXPECT_EQ(0u, queued_messages()); |
| 395 | 399 |
| 396 NotifyReceivedResponse(id); | 400 NotifyReceivedResponse(id); |
| 397 EXPECT_EQ(0u, queued_messages()); | 401 EXPECT_EQ(0u, queued_messages()); |
| 398 EXPECT_TRUE(peer_context.received_response); | 402 EXPECT_TRUE(peer_context.received_response); |
| 399 | 403 |
| 400 std::vector<char> data(kTestPageContents, | 404 std::vector<char> data(kTestPageContents, |
| 401 kTestPageContents + strlen(kTestPageContents)); | 405 kTestPageContents + strlen(kTestPageContents)); |
| 402 NotifyInlinedDataChunkReceived(id, data); | 406 NotifyInlinedDataChunkReceived(id, data); |
| 403 EXPECT_EQ(0u, queued_messages()); | 407 EXPECT_EQ(0u, queued_messages()); |
| 404 | 408 |
| 405 NotifyRequestComplete(id, strlen(kTestPageContents)); | 409 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 406 EXPECT_EQ(kTestPageContents, peer_context.data); | 410 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 407 EXPECT_TRUE(peer_context.complete); | 411 EXPECT_TRUE(peer_context.complete); |
| 408 EXPECT_EQ(0u, queued_messages()); | 412 EXPECT_EQ(0u, queued_messages()); |
| 409 } | 413 } |
| 410 | 414 |
| 411 // Tests that the request IDs are straight when there are two interleaving | 415 // Tests that the request IDs are straight when there are two interleaving |
| 412 // requests. | 416 // requests. |
| 413 TEST_F(ResourceDispatcherTest, MultipleRequests) { | 417 TEST_F(ResourceDispatcherTest, MultipleRequests) { |
| 414 const char kTestPageContents2[] = "Not kTestPageContents"; | 418 const char kTestPageContents2[] = "Not kTestPageContents"; |
| 415 | 419 |
| 416 std::unique_ptr<RequestInfo> request_info1(CreateRequestInfo(false)); | 420 std::unique_ptr<ResourceRequest> request1(CreateResourceRequest(false)); |
| 417 TestRequestPeer::Context peer_context1; | 421 TestRequestPeer::Context peer_context1; |
| 418 StartAsync(*request_info1.get(), NULL, &peer_context1); | 422 StartAsync(std::move(request1), NULL, &peer_context1); |
| 419 | 423 |
| 420 std::unique_ptr<RequestInfo> request_info2(CreateRequestInfo(false)); | 424 std::unique_ptr<ResourceRequest> request2(CreateResourceRequest(false)); |
| 421 TestRequestPeer::Context peer_context2; | 425 TestRequestPeer::Context peer_context2; |
| 422 StartAsync(*request_info2.get(), NULL, &peer_context2); | 426 StartAsync(std::move(request2), NULL, &peer_context2); |
| 423 | 427 |
| 424 int id1 = ConsumeRequestResource(); | 428 int id1 = ConsumeRequestResource(); |
| 425 int id2 = ConsumeRequestResource(); | 429 int id2 = ConsumeRequestResource(); |
| 426 EXPECT_EQ(0u, queued_messages()); | 430 EXPECT_EQ(0u, queued_messages()); |
| 427 | 431 |
| 428 NotifyReceivedResponse(id1); | 432 NotifyReceivedResponse(id1); |
| 429 EXPECT_TRUE(peer_context1.received_response); | 433 EXPECT_TRUE(peer_context1.received_response); |
| 430 EXPECT_FALSE(peer_context2.received_response); | 434 EXPECT_FALSE(peer_context2.received_response); |
| 431 NotifyReceivedResponse(id2); | 435 NotifyReceivedResponse(id2); |
| 432 EXPECT_TRUE(peer_context2.received_response); | 436 EXPECT_TRUE(peer_context2.received_response); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 447 | 451 |
| 448 NotifyRequestComplete(id2, strlen(kTestPageContents2)); | 452 NotifyRequestComplete(id2, strlen(kTestPageContents2)); |
| 449 EXPECT_EQ(kTestPageContents2, peer_context2.data); | 453 EXPECT_EQ(kTestPageContents2, peer_context2.data); |
| 450 EXPECT_TRUE(peer_context2.complete); | 454 EXPECT_TRUE(peer_context2.complete); |
| 451 | 455 |
| 452 EXPECT_EQ(0u, queued_messages()); | 456 EXPECT_EQ(0u, queued_messages()); |
| 453 } | 457 } |
| 454 | 458 |
| 455 // Tests that the cancel method prevents other messages from being received. | 459 // Tests that the cancel method prevents other messages from being received. |
| 456 TEST_F(ResourceDispatcherTest, Cancel) { | 460 TEST_F(ResourceDispatcherTest, Cancel) { |
| 457 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 461 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 458 TestRequestPeer::Context peer_context; | 462 TestRequestPeer::Context peer_context; |
| 459 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 463 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 460 | 464 |
| 461 int id = ConsumeRequestResource(); | 465 int id = ConsumeRequestResource(); |
| 462 EXPECT_EQ(0u, queued_messages()); | 466 EXPECT_EQ(0u, queued_messages()); |
| 463 | 467 |
| 464 // Cancel the request. | 468 // Cancel the request. |
| 465 dispatcher()->Cancel(request_id); | 469 dispatcher()->Cancel(request_id); |
| 466 ConsumeCancelRequest(id); | 470 ConsumeCancelRequest(id); |
| 467 | 471 |
| 468 // Any future messages related to the request should be ignored. | 472 // Any future messages related to the request should be ignored. |
| 469 NotifyReceivedResponse(id); | 473 NotifyReceivedResponse(id); |
| 470 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 474 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| 471 NotifyDataReceived(id, kTestPageContents); | 475 NotifyDataReceived(id, kTestPageContents); |
| 472 NotifyRequestComplete(id, strlen(kTestPageContents)); | 476 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 473 | 477 |
| 474 EXPECT_EQ(0u, queued_messages()); | 478 EXPECT_EQ(0u, queued_messages()); |
| 475 EXPECT_EQ("", peer_context.data); | 479 EXPECT_EQ("", peer_context.data); |
| 476 EXPECT_FALSE(peer_context.received_response); | 480 EXPECT_FALSE(peer_context.received_response); |
| 477 EXPECT_FALSE(peer_context.complete); | 481 EXPECT_FALSE(peer_context.complete); |
| 478 } | 482 } |
| 479 | 483 |
| 480 // Tests that calling cancel during a callback works as expected. | 484 // Tests that calling cancel during a callback works as expected. |
| 481 TEST_F(ResourceDispatcherTest, CancelDuringCallback) { | 485 TEST_F(ResourceDispatcherTest, CancelDuringCallback) { |
| 482 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 486 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 483 TestRequestPeer::Context peer_context; | 487 TestRequestPeer::Context peer_context; |
| 484 StartAsync(*request_info.get(), NULL, &peer_context); | 488 StartAsync(std::move(request), NULL, &peer_context); |
| 485 peer_context.cancel_on_receive_response = true; | 489 peer_context.cancel_on_receive_response = true; |
| 486 | 490 |
| 487 int id = ConsumeRequestResource(); | 491 int id = ConsumeRequestResource(); |
| 488 EXPECT_EQ(0u, queued_messages()); | 492 EXPECT_EQ(0u, queued_messages()); |
| 489 | 493 |
| 490 NotifyReceivedResponse(id); | 494 NotifyReceivedResponse(id); |
| 491 EXPECT_TRUE(peer_context.received_response); | 495 EXPECT_TRUE(peer_context.received_response); |
| 492 // Request should have been cancelled. | 496 // Request should have been cancelled. |
| 493 ConsumeCancelRequest(id); | 497 ConsumeCancelRequest(id); |
| 494 | 498 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 std::string data_; | 568 std::string data_; |
| 565 | 569 |
| 566 DISALLOW_COPY_AND_ASSIGN(WrapperPeer); | 570 DISALLOW_COPY_AND_ASSIGN(WrapperPeer); |
| 567 }; | 571 }; |
| 568 | 572 |
| 569 private: | 573 private: |
| 570 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherDelegate); | 574 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherDelegate); |
| 571 }; | 575 }; |
| 572 | 576 |
| 573 TEST_F(ResourceDispatcherTest, DelegateTest) { | 577 TEST_F(ResourceDispatcherTest, DelegateTest) { |
| 574 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 578 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 575 TestRequestPeer::Context peer_context; | 579 TestRequestPeer::Context peer_context; |
| 576 StartAsync(*request_info.get(), nullptr, &peer_context); | 580 StartAsync(std::move(request), nullptr, &peer_context); |
| 577 | 581 |
| 578 // Set the delegate that inserts a new peer in OnReceivedResponse. | 582 // Set the delegate that inserts a new peer in OnReceivedResponse. |
| 579 TestResourceDispatcherDelegate delegate; | 583 TestResourceDispatcherDelegate delegate; |
| 580 dispatcher()->set_delegate(&delegate); | 584 dispatcher()->set_delegate(&delegate); |
| 581 | 585 |
| 582 // Run a simple round-trip. | 586 // Run a simple round-trip. |
| 583 const size_t kFirstReceiveSize = 2; | 587 const size_t kFirstReceiveSize = 2; |
| 584 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents)); | 588 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents)); |
| 585 | 589 |
| 586 int id = ConsumeRequestResource(); | 590 int id = ConsumeRequestResource(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 601 // peer at once. | 605 // peer at once. |
| 602 NotifyRequestComplete(id, strlen(kTestPageContents)); | 606 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 603 | 607 |
| 604 EXPECT_TRUE(peer_context.received_response); | 608 EXPECT_TRUE(peer_context.received_response); |
| 605 EXPECT_EQ(kTestPageContents, peer_context.data); | 609 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 606 EXPECT_TRUE(peer_context.complete); | 610 EXPECT_TRUE(peer_context.complete); |
| 607 EXPECT_EQ(0u, queued_messages()); | 611 EXPECT_EQ(0u, queued_messages()); |
| 608 } | 612 } |
| 609 | 613 |
| 610 TEST_F(ResourceDispatcherTest, CancelDuringCallbackWithWrapperPeer) { | 614 TEST_F(ResourceDispatcherTest, CancelDuringCallbackWithWrapperPeer) { |
| 611 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 615 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 612 TestRequestPeer::Context peer_context; | 616 TestRequestPeer::Context peer_context; |
| 613 StartAsync(*request_info.get(), nullptr, &peer_context); | 617 StartAsync(std::move(request), nullptr, &peer_context); |
| 614 peer_context.cancel_on_receive_response = true; | 618 peer_context.cancel_on_receive_response = true; |
| 615 | 619 |
| 616 // Set the delegate that inserts a new peer in OnReceivedResponse. | 620 // Set the delegate that inserts a new peer in OnReceivedResponse. |
| 617 TestResourceDispatcherDelegate delegate; | 621 TestResourceDispatcherDelegate delegate; |
| 618 dispatcher()->set_delegate(&delegate); | 622 dispatcher()->set_delegate(&delegate); |
| 619 | 623 |
| 620 int id = ConsumeRequestResource(); | 624 int id = ConsumeRequestResource(); |
| 621 EXPECT_EQ(0u, queued_messages()); | 625 EXPECT_EQ(0u, queued_messages()); |
| 622 | 626 |
| 623 // The wrapper eats all messages until RequestComplete message is sent. | 627 // The wrapper eats all messages until RequestComplete message is sent. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 645 NotifyDataReceived(id, kTestPageContents); | 649 NotifyDataReceived(id, kTestPageContents); |
| 646 NotifyRequestComplete(id, strlen(kTestPageContents)); | 650 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 647 | 651 |
| 648 EXPECT_EQ(0u, queued_messages()); | 652 EXPECT_EQ(0u, queued_messages()); |
| 649 EXPECT_EQ("", peer_context.data); | 653 EXPECT_EQ("", peer_context.data); |
| 650 EXPECT_FALSE(peer_context.complete); | 654 EXPECT_FALSE(peer_context.complete); |
| 651 } | 655 } |
| 652 | 656 |
| 653 // Checks that redirects work as expected. | 657 // Checks that redirects work as expected. |
| 654 TEST_F(ResourceDispatcherTest, Redirect) { | 658 TEST_F(ResourceDispatcherTest, Redirect) { |
| 655 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 659 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 656 TestRequestPeer::Context peer_context; | 660 TestRequestPeer::Context peer_context; |
| 657 StartAsync(*request_info.get(), NULL, &peer_context); | 661 StartAsync(std::move(request), NULL, &peer_context); |
| 658 | 662 |
| 659 int id = ConsumeRequestResource(); | 663 int id = ConsumeRequestResource(); |
| 660 | 664 |
| 661 NotifyReceivedRedirect(id); | 665 NotifyReceivedRedirect(id); |
| 662 ConsumeFollowRedirect(id); | 666 ConsumeFollowRedirect(id); |
| 663 EXPECT_EQ(1, peer_context.seen_redirects); | 667 EXPECT_EQ(1, peer_context.seen_redirects); |
| 664 | 668 |
| 665 NotifyReceivedRedirect(id); | 669 NotifyReceivedRedirect(id); |
| 666 ConsumeFollowRedirect(id); | 670 ConsumeFollowRedirect(id); |
| 667 EXPECT_EQ(2, peer_context.seen_redirects); | 671 EXPECT_EQ(2, peer_context.seen_redirects); |
| 668 | 672 |
| 669 NotifyReceivedResponse(id); | 673 NotifyReceivedResponse(id); |
| 670 EXPECT_TRUE(peer_context.received_response); | 674 EXPECT_TRUE(peer_context.received_response); |
| 671 | 675 |
| 672 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 676 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| 673 NotifyDataReceived(id, kTestPageContents); | 677 NotifyDataReceived(id, kTestPageContents); |
| 674 ConsumeDataReceived_ACK(id); | 678 ConsumeDataReceived_ACK(id); |
| 675 | 679 |
| 676 NotifyRequestComplete(id, strlen(kTestPageContents)); | 680 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 677 EXPECT_EQ(kTestPageContents, peer_context.data); | 681 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 678 EXPECT_TRUE(peer_context.complete); | 682 EXPECT_TRUE(peer_context.complete); |
| 679 EXPECT_EQ(0u, queued_messages()); | 683 EXPECT_EQ(0u, queued_messages()); |
| 680 EXPECT_EQ(2, peer_context.seen_redirects); | 684 EXPECT_EQ(2, peer_context.seen_redirects); |
| 681 } | 685 } |
| 682 | 686 |
| 683 // Tests that that cancelling during a redirect method prevents other messages | 687 // Tests that that cancelling during a redirect method prevents other messages |
| 684 // from being received. | 688 // from being received. |
| 685 TEST_F(ResourceDispatcherTest, CancelDuringRedirect) { | 689 TEST_F(ResourceDispatcherTest, CancelDuringRedirect) { |
| 686 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 690 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 687 TestRequestPeer::Context peer_context; | 691 TestRequestPeer::Context peer_context; |
| 688 StartAsync(*request_info.get(), NULL, &peer_context); | 692 StartAsync(std::move(request), NULL, &peer_context); |
| 689 peer_context.follow_redirects = false; | 693 peer_context.follow_redirects = false; |
| 690 | 694 |
| 691 int id = ConsumeRequestResource(); | 695 int id = ConsumeRequestResource(); |
| 692 EXPECT_EQ(0u, queued_messages()); | 696 EXPECT_EQ(0u, queued_messages()); |
| 693 | 697 |
| 694 // Redirect the request, which triggers a cancellation. | 698 // Redirect the request, which triggers a cancellation. |
| 695 NotifyReceivedRedirect(id); | 699 NotifyReceivedRedirect(id); |
| 696 ConsumeCancelRequest(id); | 700 ConsumeCancelRequest(id); |
| 697 EXPECT_EQ(1, peer_context.seen_redirects); | 701 EXPECT_EQ(1, peer_context.seen_redirects); |
| 698 EXPECT_EQ(0u, queued_messages()); | 702 EXPECT_EQ(0u, queued_messages()); |
| 699 | 703 |
| 700 // Any future messages related to the request should be ignored. In practice, | 704 // Any future messages related to the request should be ignored. In practice, |
| 701 // only the NotifyRequestComplete should be received after this point. | 705 // only the NotifyRequestComplete should be received after this point. |
| 702 NotifyReceivedRedirect(id); | 706 NotifyReceivedRedirect(id); |
| 703 NotifyReceivedResponse(id); | 707 NotifyReceivedResponse(id); |
| 704 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 708 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| 705 NotifyDataReceived(id, kTestPageContents); | 709 NotifyDataReceived(id, kTestPageContents); |
| 706 NotifyRequestComplete(id, strlen(kTestPageContents)); | 710 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 707 | 711 |
| 708 EXPECT_EQ(0u, queued_messages()); | 712 EXPECT_EQ(0u, queued_messages()); |
| 709 EXPECT_EQ("", peer_context.data); | 713 EXPECT_EQ("", peer_context.data); |
| 710 EXPECT_FALSE(peer_context.complete); | 714 EXPECT_FALSE(peer_context.complete); |
| 711 EXPECT_EQ(1, peer_context.seen_redirects); | 715 EXPECT_EQ(1, peer_context.seen_redirects); |
| 712 } | 716 } |
| 713 | 717 |
| 714 // Checks that deferring a request delays messages until it's resumed. | 718 // Checks that deferring a request delays messages until it's resumed. |
| 715 TEST_F(ResourceDispatcherTest, Defer) { | 719 TEST_F(ResourceDispatcherTest, Defer) { |
| 716 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 720 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 717 TestRequestPeer::Context peer_context; | 721 TestRequestPeer::Context peer_context; |
| 718 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 722 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 719 | 723 |
| 720 int id = ConsumeRequestResource(); | 724 int id = ConsumeRequestResource(); |
| 721 EXPECT_EQ(0u, queued_messages()); | 725 EXPECT_EQ(0u, queued_messages()); |
| 722 | 726 |
| 723 dispatcher()->SetDefersLoading(request_id, true); | 727 dispatcher()->SetDefersLoading(request_id, true); |
| 724 NotifyReceivedResponse(id); | 728 NotifyReceivedResponse(id); |
| 725 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 729 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| 726 NotifyDataReceived(id, kTestPageContents); | 730 NotifyDataReceived(id, kTestPageContents); |
| 727 NotifyRequestComplete(id, strlen(kTestPageContents)); | 731 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 728 | 732 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 740 ConsumeDataReceived_ACK(id); | 744 ConsumeDataReceived_ACK(id); |
| 741 EXPECT_EQ(0u, queued_messages()); | 745 EXPECT_EQ(0u, queued_messages()); |
| 742 EXPECT_TRUE(peer_context.received_response); | 746 EXPECT_TRUE(peer_context.received_response); |
| 743 EXPECT_EQ(kTestPageContents, peer_context.data); | 747 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 744 EXPECT_TRUE(peer_context.complete); | 748 EXPECT_TRUE(peer_context.complete); |
| 745 } | 749 } |
| 746 | 750 |
| 747 // Checks that deferring a request during a redirect delays messages until it's | 751 // Checks that deferring a request during a redirect delays messages until it's |
| 748 // resumed. | 752 // resumed. |
| 749 TEST_F(ResourceDispatcherTest, DeferOnRedirect) { | 753 TEST_F(ResourceDispatcherTest, DeferOnRedirect) { |
| 750 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 754 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 751 TestRequestPeer::Context peer_context; | 755 TestRequestPeer::Context peer_context; |
| 752 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 756 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 753 peer_context.defer_on_redirect = true; | 757 peer_context.defer_on_redirect = true; |
| 754 | 758 |
| 755 int id = ConsumeRequestResource(); | 759 int id = ConsumeRequestResource(); |
| 756 EXPECT_EQ(0u, queued_messages()); | 760 EXPECT_EQ(0u, queued_messages()); |
| 757 | 761 |
| 758 // The request should be deferred during the redirect, including the message | 762 // The request should be deferred during the redirect, including the message |
| 759 // to follow the redirect. | 763 // to follow the redirect. |
| 760 NotifyReceivedRedirect(id); | 764 NotifyReceivedRedirect(id); |
| 761 NotifyReceivedResponse(id); | 765 NotifyReceivedResponse(id); |
| 762 NotifySetDataBuffer(id, strlen(kTestPageContents)); | 766 NotifySetDataBuffer(id, strlen(kTestPageContents)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 779 | 783 |
| 780 EXPECT_EQ(0u, queued_messages()); | 784 EXPECT_EQ(0u, queued_messages()); |
| 781 EXPECT_TRUE(peer_context.received_response); | 785 EXPECT_TRUE(peer_context.received_response); |
| 782 EXPECT_EQ(kTestPageContents, peer_context.data); | 786 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 783 EXPECT_TRUE(peer_context.complete); | 787 EXPECT_TRUE(peer_context.complete); |
| 784 EXPECT_EQ(1, peer_context.seen_redirects); | 788 EXPECT_EQ(1, peer_context.seen_redirects); |
| 785 } | 789 } |
| 786 | 790 |
| 787 // Checks that a deferred request that's cancelled doesn't receive any messages. | 791 // Checks that a deferred request that's cancelled doesn't receive any messages. |
| 788 TEST_F(ResourceDispatcherTest, CancelDeferredRequest) { | 792 TEST_F(ResourceDispatcherTest, CancelDeferredRequest) { |
| 789 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 793 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 790 TestRequestPeer::Context peer_context; | 794 TestRequestPeer::Context peer_context; |
| 791 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 795 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 792 | 796 |
| 793 int id = ConsumeRequestResource(); | 797 int id = ConsumeRequestResource(); |
| 794 EXPECT_EQ(0u, queued_messages()); | 798 EXPECT_EQ(0u, queued_messages()); |
| 795 | 799 |
| 796 dispatcher()->SetDefersLoading(request_id, true); | 800 dispatcher()->SetDefersLoading(request_id, true); |
| 797 NotifyReceivedRedirect(id); | 801 NotifyReceivedRedirect(id); |
| 798 dispatcher()->Cancel(request_id); | 802 dispatcher()->Cancel(request_id); |
| 799 ConsumeCancelRequest(id); | 803 ConsumeCancelRequest(id); |
| 800 | 804 |
| 801 NotifyRequestComplete(id, 0); | 805 NotifyRequestComplete(id, 0); |
| 802 base::RunLoop().RunUntilIdle(); | 806 base::RunLoop().RunUntilIdle(); |
| 803 | 807 |
| 804 // None of the messages should have been processed. | 808 // None of the messages should have been processed. |
| 805 EXPECT_EQ(0u, queued_messages()); | 809 EXPECT_EQ(0u, queued_messages()); |
| 806 EXPECT_EQ("", peer_context.data); | 810 EXPECT_EQ("", peer_context.data); |
| 807 EXPECT_FALSE(peer_context.complete); | 811 EXPECT_FALSE(peer_context.complete); |
| 808 EXPECT_EQ(0, peer_context.seen_redirects); | 812 EXPECT_EQ(0, peer_context.seen_redirects); |
| 809 } | 813 } |
| 810 | 814 |
| 811 TEST_F(ResourceDispatcherTest, DownloadToFile) { | 815 TEST_F(ResourceDispatcherTest, DownloadToFile) { |
| 812 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(true)); | 816 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(true)); |
| 813 TestRequestPeer::Context peer_context; | 817 TestRequestPeer::Context peer_context; |
| 814 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 818 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 815 const int kDownloadedIncrement = 100; | 819 const int kDownloadedIncrement = 100; |
| 816 const int kEncodedIncrement = 50; | 820 const int kEncodedIncrement = 50; |
| 817 | 821 |
| 818 int id = ConsumeRequestResource(); | 822 int id = ConsumeRequestResource(); |
| 819 EXPECT_EQ(0u, queued_messages()); | 823 EXPECT_EQ(0u, queued_messages()); |
| 820 | 824 |
| 821 NotifyReceivedResponse(id); | 825 NotifyReceivedResponse(id); |
| 822 EXPECT_EQ(0u, queued_messages()); | 826 EXPECT_EQ(0u, queued_messages()); |
| 823 EXPECT_TRUE(peer_context.received_response); | 827 EXPECT_TRUE(peer_context.received_response); |
| 824 | 828 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 844 ConsumeReleaseDownloadedFile(id); | 848 ConsumeReleaseDownloadedFile(id); |
| 845 EXPECT_EQ(0u, queued_messages()); | 849 EXPECT_EQ(0u, queued_messages()); |
| 846 EXPECT_EQ(expected_total_downloaded_length, | 850 EXPECT_EQ(expected_total_downloaded_length, |
| 847 peer_context.total_downloaded_data_length); | 851 peer_context.total_downloaded_data_length); |
| 848 EXPECT_EQ(expected_total_encoded_data_length, | 852 EXPECT_EQ(expected_total_encoded_data_length, |
| 849 peer_context.total_encoded_data_length); | 853 peer_context.total_encoded_data_length); |
| 850 } | 854 } |
| 851 | 855 |
| 852 // Make sure that when a download to file is cancelled, the file is destroyed. | 856 // Make sure that when a download to file is cancelled, the file is destroyed. |
| 853 TEST_F(ResourceDispatcherTest, CancelDownloadToFile) { | 857 TEST_F(ResourceDispatcherTest, CancelDownloadToFile) { |
| 854 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(true)); | 858 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(true)); |
| 855 TestRequestPeer::Context peer_context; | 859 TestRequestPeer::Context peer_context; |
| 856 int request_id = StartAsync(*request_info.get(), NULL, &peer_context); | 860 int request_id = StartAsync(std::move(request), NULL, &peer_context); |
| 857 | 861 |
| 858 int id = ConsumeRequestResource(); | 862 int id = ConsumeRequestResource(); |
| 859 EXPECT_EQ(0u, queued_messages()); | 863 EXPECT_EQ(0u, queued_messages()); |
| 860 | 864 |
| 861 NotifyReceivedResponse(id); | 865 NotifyReceivedResponse(id); |
| 862 EXPECT_EQ(0u, queued_messages()); | 866 EXPECT_EQ(0u, queued_messages()); |
| 863 EXPECT_TRUE(peer_context.received_response); | 867 EXPECT_TRUE(peer_context.received_response); |
| 864 | 868 |
| 865 // Cancelling the request deletes the file. | 869 // Cancelling the request deletes the file. |
| 866 dispatcher()->Cancel(request_id); | 870 dispatcher()->Cancel(request_id); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 877 } | 881 } |
| 878 | 882 |
| 879 class TimeConversionTest : public ResourceDispatcherTest { | 883 class TimeConversionTest : public ResourceDispatcherTest { |
| 880 public: | 884 public: |
| 881 bool Send(IPC::Message* msg) override { | 885 bool Send(IPC::Message* msg) override { |
| 882 delete msg; | 886 delete msg; |
| 883 return true; | 887 return true; |
| 884 } | 888 } |
| 885 | 889 |
| 886 void PerformTest(const ResourceResponseHead& response_head) { | 890 void PerformTest(const ResourceResponseHead& response_head) { |
| 887 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); | 891 std::unique_ptr<ResourceRequest> request(CreateResourceRequest(false)); |
| 888 TestRequestPeer::Context peer_context; | 892 TestRequestPeer::Context peer_context; |
| 889 StartAsync(*request_info.get(), NULL, &peer_context); | 893 StartAsync(std::move(request), NULL, &peer_context); |
| 890 | 894 |
| 891 dispatcher()->OnMessageReceived( | 895 dispatcher()->OnMessageReceived( |
| 892 ResourceMsg_ReceivedResponse(0, response_head)); | 896 ResourceMsg_ReceivedResponse(0, response_head)); |
| 893 } | 897 } |
| 894 | 898 |
| 895 const ResourceResponseInfo& response_info() const { return response_info_; } | 899 const ResourceResponseInfo& response_info() const { return response_info_; } |
| 896 | 900 |
| 897 private: | 901 private: |
| 898 ResourceResponseInfo response_info_; | 902 ResourceResponseInfo response_info_; |
| 899 }; | 903 }; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 ResourceResponseHead response_head; | 938 ResourceResponseHead response_head; |
| 935 | 939 |
| 936 PerformTest(response_head); | 940 PerformTest(response_head); |
| 937 | 941 |
| 938 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); | 942 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); |
| 939 EXPECT_EQ(base::TimeTicks(), | 943 EXPECT_EQ(base::TimeTicks(), |
| 940 response_info().load_timing.connect_timing.dns_start); | 944 response_info().load_timing.connect_timing.dns_start); |
| 941 } | 945 } |
| 942 | 946 |
| 943 } // namespace content | 947 } // namespace content |
| OLD | NEW |