| 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/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/memory/shared_memory.h" | 19 #include "base/memory/shared_memory.h" |
| 19 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
| 21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 22 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 23 #include "content/child/request_extra_data.h" | 24 #include "content/child/request_extra_data.h" |
| 24 #include "content/child/request_info.h" | 25 #include "content/child/request_info.h" |
| 25 #include "content/common/appcache_interfaces.h" | 26 #include "content/common/appcache_interfaces.h" |
| 26 #include "content/common/resource_messages.h" | 27 #include "content/common/resource_messages.h" |
| 27 #include "content/common/resource_request.h" | 28 #include "content/common/resource_request.h" |
| 28 #include "content/common/resource_request_completion_status.h" | 29 #include "content/common/resource_request_completion_status.h" |
| 29 #include "content/common/service_worker/service_worker_types.h" | 30 #include "content/common/service_worker/service_worker_types.h" |
| 30 #include "content/public/child/fixed_received_data.h" | 31 #include "content/public/child/fixed_received_data.h" |
| 31 #include "content/public/child/request_peer.h" | 32 #include "content/public/child/request_peer.h" |
| 32 #include "content/public/child/resource_dispatcher_delegate.h" | 33 #include "content/public/child/resource_dispatcher_delegate.h" |
| 34 #include "content/public/common/content_features.cc" |
| 33 #include "content/public/common/resource_response.h" | 35 #include "content/public/common/resource_response.h" |
| 34 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 35 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 39 |
| 38 namespace content { | 40 namespace content { |
| 39 | 41 |
| 40 static const char kTestPageUrl[] = "http://www.google.com/"; | 42 static const char kTestPageUrl[] = "http://www.google.com/"; |
| 41 static const char kTestPageHeaders[] = | 43 static const char kTestPageHeaders[] = |
| 42 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; | 44 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 &duplicate_handle)); | 270 &duplicate_handle)); |
| 269 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_SetDataBuffer( | 271 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_SetDataBuffer( |
| 270 request_id, duplicate_handle, shared_memory->requested_size(), 0))); | 272 request_id, duplicate_handle, shared_memory->requested_size(), 0))); |
| 271 } | 273 } |
| 272 | 274 |
| 273 void NotifyDataReceived(int request_id, const std::string& data) { | 275 void NotifyDataReceived(int request_id, const std::string& data) { |
| 274 ASSERT_LE(data.length(), shared_memory_map_[request_id]->requested_size()); | 276 ASSERT_LE(data.length(), shared_memory_map_[request_id]->requested_size()); |
| 275 memcpy(shared_memory_map_[request_id]->memory(), data.c_str(), | 277 memcpy(shared_memory_map_[request_id]->memory(), data.c_str(), |
| 276 data.length()); | 278 data.length()); |
| 277 | 279 |
| 278 EXPECT_TRUE(dispatcher_->OnMessageReceived( | 280 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_DataReceived( |
| 279 ResourceMsg_DataReceived(request_id, 0, data.length(), data.length()))); | 281 request_id, 0, data.length(), data.length(), data.length()))); |
| 280 } | 282 } |
| 281 | 283 |
| 282 void NotifyDataDownloaded(int request_id, int decoded_length, | 284 void NotifyInlinedDataChunkReceived(int request_id, |
| 285 const std::vector<char>& data) { |
| 286 auto size = data.size(); |
| 287 EXPECT_TRUE(dispatcher_->OnMessageReceived( |
| 288 ResourceMsg_InlinedDataChunkReceived(request_id, data, size, size))); |
| 289 } |
| 290 |
| 291 void NotifyDataDownloaded(int request_id, |
| 292 int decoded_length, |
| 283 int encoded_length) { | 293 int encoded_length) { |
| 284 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_DataDownloaded( | 294 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_DataDownloaded( |
| 285 request_id, decoded_length, encoded_length))); | 295 request_id, decoded_length, encoded_length))); |
| 286 } | 296 } |
| 287 | 297 |
| 288 void NotifyRequestComplete(int request_id, size_t total_size) { | 298 void NotifyRequestComplete(int request_id, size_t total_size) { |
| 289 ResourceRequestCompletionStatus request_complete_data; | 299 ResourceRequestCompletionStatus request_complete_data; |
| 290 request_complete_data.error_code = net::OK; | 300 request_complete_data.error_code = net::OK; |
| 291 request_complete_data.was_ignored_by_handler = false; | 301 request_complete_data.was_ignored_by_handler = false; |
| 292 request_complete_data.exists_in_cache = false; | 302 request_complete_data.exists_in_cache = false; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 NotifyDataReceived(id, kTestPageContents + kFirstReceiveSize); | 372 NotifyDataReceived(id, kTestPageContents + kFirstReceiveSize); |
| 363 ConsumeDataReceived_ACK(id); | 373 ConsumeDataReceived_ACK(id); |
| 364 EXPECT_EQ(0u, queued_messages()); | 374 EXPECT_EQ(0u, queued_messages()); |
| 365 | 375 |
| 366 NotifyRequestComplete(id, strlen(kTestPageContents)); | 376 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 367 EXPECT_EQ(kTestPageContents, peer_context.data); | 377 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 368 EXPECT_TRUE(peer_context.complete); | 378 EXPECT_TRUE(peer_context.complete); |
| 369 EXPECT_EQ(0u, queued_messages()); | 379 EXPECT_EQ(0u, queued_messages()); |
| 370 } | 380 } |
| 371 | 381 |
| 382 // A simple request with an inline data response. |
| 383 TEST_F(ResourceDispatcherTest, ResponseWithInlinedData) { |
| 384 auto feature_list = base::MakeUnique<base::FeatureList>(); |
| 385 feature_list->InitializeFromCommandLine( |
| 386 features::kOptimizeLoadingIPCForSmallResources.name, std::string()); |
| 387 base::FeatureList::SetInstance(std::move(feature_list)); |
| 388 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false)); |
| 389 TestRequestPeer::Context peer_context; |
| 390 StartAsync(*request_info.get(), NULL, &peer_context); |
| 391 |
| 392 int id = ConsumeRequestResource(); |
| 393 EXPECT_EQ(0u, queued_messages()); |
| 394 |
| 395 NotifyReceivedResponse(id); |
| 396 EXPECT_EQ(0u, queued_messages()); |
| 397 EXPECT_TRUE(peer_context.received_response); |
| 398 |
| 399 std::vector<char> data(kTestPageContents, |
| 400 kTestPageContents + strlen(kTestPageContents)); |
| 401 NotifyInlinedDataChunkReceived(id, data); |
| 402 EXPECT_EQ(0u, queued_messages()); |
| 403 |
| 404 NotifyRequestComplete(id, strlen(kTestPageContents)); |
| 405 EXPECT_EQ(kTestPageContents, peer_context.data); |
| 406 EXPECT_TRUE(peer_context.complete); |
| 407 EXPECT_EQ(0u, queued_messages()); |
| 408 } |
| 409 |
| 372 // Tests that the request IDs are straight when there are two interleaving | 410 // Tests that the request IDs are straight when there are two interleaving |
| 373 // requests. | 411 // requests. |
| 374 TEST_F(ResourceDispatcherTest, MultipleRequests) { | 412 TEST_F(ResourceDispatcherTest, MultipleRequests) { |
| 375 const char kTestPageContents2[] = "Not kTestPageContents"; | 413 const char kTestPageContents2[] = "Not kTestPageContents"; |
| 376 | 414 |
| 377 std::unique_ptr<RequestInfo> request_info1(CreateRequestInfo(false)); | 415 std::unique_ptr<RequestInfo> request_info1(CreateRequestInfo(false)); |
| 378 TestRequestPeer::Context peer_context1; | 416 TestRequestPeer::Context peer_context1; |
| 379 StartAsync(*request_info1.get(), NULL, &peer_context1); | 417 StartAsync(*request_info1.get(), NULL, &peer_context1); |
| 380 | 418 |
| 381 std::unique_ptr<RequestInfo> request_info2(CreateRequestInfo(false)); | 419 std::unique_ptr<RequestInfo> request_info2(CreateRequestInfo(false)); |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 ResourceResponseHead response_head; | 934 ResourceResponseHead response_head; |
| 897 | 935 |
| 898 PerformTest(response_head); | 936 PerformTest(response_head); |
| 899 | 937 |
| 900 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); | 938 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); |
| 901 EXPECT_EQ(base::TimeTicks(), | 939 EXPECT_EQ(base::TimeTicks(), |
| 902 response_info().load_timing.connect_timing.dns_start); | 940 response_info().load_timing.connect_timing.dns_start); |
| 903 } | 941 } |
| 904 | 942 |
| 905 } // namespace content | 943 } // namespace content |
| OLD | NEW |