| 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" | |
| 24 #include "content/child/request_extra_data.h" | 23 #include "content/child/request_extra_data.h" |
| 25 #include "content/common/appcache_interfaces.h" | 24 #include "content/common/appcache_interfaces.h" |
| 26 #include "content/common/resource_messages.h" | 25 #include "content/common/resource_messages.h" |
| 27 #include "content/common/resource_request.h" | 26 #include "content/common/resource_request.h" |
| 28 #include "content/common/resource_request_completion_status.h" | 27 #include "content/common/resource_request_completion_status.h" |
| 29 #include "content/common/service_worker/service_worker_types.h" | 28 #include "content/common/service_worker/service_worker_types.h" |
| 30 #include "content/public/child/fixed_received_data.h" | 29 #include "content/public/child/fixed_received_data.h" |
| 31 #include "content/public/child/request_peer.h" | 30 #include "content/public/child/request_peer.h" |
| 32 #include "content/public/child/resource_dispatcher_delegate.h" | 31 #include "content/public/child/resource_dispatcher_delegate.h" |
| 33 #include "content/public/common/content_features.h" | 32 #include "content/public/common/content_features.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 }; | 141 }; |
| 143 | 142 |
| 144 // Sets up the message sender override for the unit test. | 143 // Sets up the message sender override for the unit test. |
| 145 class ResourceDispatcherTest : public testing::Test, public IPC::Sender { | 144 class ResourceDispatcherTest : public testing::Test, public IPC::Sender { |
| 146 public: | 145 public: |
| 147 ResourceDispatcherTest() | 146 ResourceDispatcherTest() |
| 148 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) { | 147 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) { |
| 149 } | 148 } |
| 150 | 149 |
| 151 ~ResourceDispatcherTest() override { | 150 ~ResourceDispatcherTest() override { |
| 152 base::STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), | 151 shared_memory_map_.clear(); |
| 153 shared_memory_map_.end()); | |
| 154 dispatcher_.reset(); | 152 dispatcher_.reset(); |
| 155 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 156 } | 154 } |
| 157 | 155 |
| 158 // Emulates IPC send operations (IPC::Sender) by adding | 156 // Emulates IPC send operations (IPC::Sender) by adding |
| 159 // pending messages to the queue. | 157 // pending messages to the queue. |
| 160 bool Send(IPC::Message* msg) override { | 158 bool Send(IPC::Message* msg) override { |
| 161 message_queue_.push_back(IPC::Message(*msg)); | 159 message_queue_.push_back(IPC::Message(*msg)); |
| 162 delete msg; | 160 delete msg; |
| 163 return true; | 161 return true; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 head.headers = new net::HttpResponseHeaders(raw_headers); | 255 head.headers = new net::HttpResponseHeaders(raw_headers); |
| 258 head.mime_type = kTestPageMimeType; | 256 head.mime_type = kTestPageMimeType; |
| 259 head.charset = kTestPageCharset; | 257 head.charset = kTestPageCharset; |
| 260 EXPECT_EQ(true, dispatcher_->OnMessageReceived( | 258 EXPECT_EQ(true, dispatcher_->OnMessageReceived( |
| 261 ResourceMsg_ReceivedResponse(request_id, head))); | 259 ResourceMsg_ReceivedResponse(request_id, head))); |
| 262 } | 260 } |
| 263 | 261 |
| 264 void NotifySetDataBuffer(int request_id, size_t buffer_size) { | 262 void NotifySetDataBuffer(int request_id, size_t buffer_size) { |
| 265 base::SharedMemory* shared_memory = new base::SharedMemory(); | 263 base::SharedMemory* shared_memory = new base::SharedMemory(); |
| 266 ASSERT_FALSE(shared_memory_map_[request_id]); | 264 ASSERT_FALSE(shared_memory_map_[request_id]); |
| 267 shared_memory_map_[request_id] = shared_memory; | 265 shared_memory_map_[request_id] = base::WrapUnique(shared_memory); |
| 268 EXPECT_TRUE(shared_memory->CreateAndMapAnonymous(buffer_size)); | 266 EXPECT_TRUE(shared_memory->CreateAndMapAnonymous(buffer_size)); |
| 269 | 267 |
| 270 base::SharedMemoryHandle duplicate_handle; | 268 base::SharedMemoryHandle duplicate_handle; |
| 271 EXPECT_TRUE(shared_memory->ShareToProcess(base::GetCurrentProcessHandle(), | 269 EXPECT_TRUE(shared_memory->ShareToProcess(base::GetCurrentProcessHandle(), |
| 272 &duplicate_handle)); | 270 &duplicate_handle)); |
| 273 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_SetDataBuffer( | 271 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_SetDataBuffer( |
| 274 request_id, duplicate_handle, shared_memory->requested_size(), 0))); | 272 request_id, duplicate_handle, shared_memory->requested_size(), 0))); |
| 275 } | 273 } |
| 276 | 274 |
| 277 void NotifyDataReceived(int request_id, const std::string& data) { | 275 void NotifyDataReceived(int request_id, const std::string& data) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 new TestRequestPeer(dispatcher(), peer_context)); | 334 new TestRequestPeer(dispatcher(), peer_context)); |
| 337 int request_id = dispatcher()->StartAsync( | 335 int request_id = dispatcher()->StartAsync( |
| 338 std::move(request), 0, nullptr, GURL(), std::move(peer), | 336 std::move(request), 0, nullptr, GURL(), std::move(peer), |
| 339 blink::WebURLRequest::LoadingIPCType::ChromeIPC, nullptr); | 337 blink::WebURLRequest::LoadingIPCType::ChromeIPC, nullptr); |
| 340 peer_context->request_id = request_id; | 338 peer_context->request_id = request_id; |
| 341 return request_id; | 339 return request_id; |
| 342 } | 340 } |
| 343 | 341 |
| 344 private: | 342 private: |
| 345 // Map of request IDs to shared memory. | 343 // Map of request IDs to shared memory. |
| 346 std::map<int, base::SharedMemory*> shared_memory_map_; | 344 std::map<int, std::unique_ptr<base::SharedMemory>> shared_memory_map_; |
| 347 | 345 |
| 348 std::vector<IPC::Message> message_queue_; | 346 std::vector<IPC::Message> message_queue_; |
| 349 base::MessageLoop message_loop_; | 347 base::MessageLoop message_loop_; |
| 350 std::unique_ptr<ResourceDispatcher> dispatcher_; | 348 std::unique_ptr<ResourceDispatcher> dispatcher_; |
| 351 }; | 349 }; |
| 352 | 350 |
| 353 // Does a simple request and tests that the correct data is received. Simulates | 351 // Does a simple request and tests that the correct data is received. Simulates |
| 354 // two reads. | 352 // two reads. |
| 355 TEST_F(ResourceDispatcherTest, RoundTrip) { | 353 TEST_F(ResourceDispatcherTest, RoundTrip) { |
| 356 // Number of bytes received in the first read. | 354 // Number of bytes received in the first read. |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 938 ResourceResponseHead response_head; | 936 ResourceResponseHead response_head; |
| 939 | 937 |
| 940 PerformTest(response_head); | 938 PerformTest(response_head); |
| 941 | 939 |
| 942 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); | 940 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start); |
| 943 EXPECT_EQ(base::TimeTicks(), | 941 EXPECT_EQ(base::TimeTicks(), |
| 944 response_info().load_timing.connect_timing.dns_start); | 942 response_info().load_timing.connect_timing.dns_start); |
| 945 } | 943 } |
| 946 | 944 |
| 947 } // namespace content | 945 } // namespace content |
| OLD | NEW |