| 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/mojo_async_resource_handler.h" | 5 #include "content/browser/loader/mojo_async_resource_handler.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "content/browser/loader/mock_resource_loader.h" | 18 #include "content/browser/loader/mock_resource_loader.h" |
| 18 #include "content/browser/loader/resource_controller.h" | 19 #include "content/browser/loader/resource_controller.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 num_on_response_started_calls_expectation_ = expectation; | 168 num_on_response_started_calls_expectation_ = expectation; |
| 168 } | 169 } |
| 169 | 170 |
| 170 private: | 171 private: |
| 171 int num_on_response_started_calls_ = 0; | 172 int num_on_response_started_calls_ = 0; |
| 172 int num_on_response_started_calls_expectation_ = 0; | 173 int num_on_response_started_calls_expectation_ = 0; |
| 173 | 174 |
| 174 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); | 175 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); |
| 175 }; | 176 }; |
| 176 | 177 |
| 177 class MojoAsyncResourceHandlerWithCustomDataPipeOperations | 178 class MojoAsyncResourceHandlerWithStubOperations |
| 178 : public MojoAsyncResourceHandler { | 179 : public MojoAsyncResourceHandler { |
| 179 public: | 180 public: |
| 180 MojoAsyncResourceHandlerWithCustomDataPipeOperations( | 181 MojoAsyncResourceHandlerWithStubOperations( |
| 181 net::URLRequest* request, | 182 net::URLRequest* request, |
| 182 ResourceDispatcherHostImpl* rdh, | 183 ResourceDispatcherHostImpl* rdh, |
| 183 mojom::URLLoaderAssociatedRequest mojo_request, | 184 mojom::URLLoaderAssociatedRequest mojo_request, |
| 184 mojom::URLLoaderClientAssociatedPtr url_loader_client) | 185 mojom::URLLoaderClientAssociatedPtr url_loader_client) |
| 185 : MojoAsyncResourceHandler(request, | 186 : MojoAsyncResourceHandler(request, |
| 186 rdh, | 187 rdh, |
| 187 std::move(mojo_request), | 188 std::move(mojo_request), |
| 188 std::move(url_loader_client)) {} | 189 std::move(url_loader_client)) {} |
| 189 ~MojoAsyncResourceHandlerWithCustomDataPipeOperations() override {} | 190 ~MojoAsyncResourceHandlerWithStubOperations() override {} |
| 190 | 191 |
| 191 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } | 192 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } |
| 192 | 193 |
| 193 void set_begin_write_expectation(MojoResult begin_write_expectation) { | 194 void set_begin_write_expectation(MojoResult begin_write_expectation) { |
| 194 is_begin_write_expectation_set_ = true; | 195 is_begin_write_expectation_set_ = true; |
| 195 begin_write_expectation_ = begin_write_expectation; | 196 begin_write_expectation_ = begin_write_expectation; |
| 196 } | 197 } |
| 197 void set_end_write_expectation(MojoResult end_write_expectation) { | 198 void set_end_write_expectation(MojoResult end_write_expectation) { |
| 198 is_end_write_expectation_set_ = true; | 199 is_end_write_expectation_set_ = true; |
| 199 end_write_expectation_ = end_write_expectation; | 200 end_write_expectation_ = end_write_expectation; |
| 200 } | 201 } |
| 201 bool has_received_bad_message() const { return has_received_bad_message_; } | 202 bool has_received_bad_message() const { return has_received_bad_message_; } |
| 203 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) { |
| 204 metadata_ = std::move(metadata); |
| 205 } |
| 202 | 206 |
| 203 private: | 207 private: |
| 204 MojoResult BeginWrite(void** data, uint32_t* available) override { | 208 MojoResult BeginWrite(void** data, uint32_t* available) override { |
| 205 if (is_begin_write_expectation_set_) | 209 if (is_begin_write_expectation_set_) |
| 206 return begin_write_expectation_; | 210 return begin_write_expectation_; |
| 207 return MojoAsyncResourceHandler::BeginWrite(data, available); | 211 return MojoAsyncResourceHandler::BeginWrite(data, available); |
| 208 } | 212 } |
| 209 MojoResult EndWrite(uint32_t written) override { | 213 MojoResult EndWrite(uint32_t written) override { |
| 210 if (is_end_write_expectation_set_) | 214 if (is_end_write_expectation_set_) |
| 211 return end_write_expectation_; | 215 return end_write_expectation_; |
| 212 return MojoAsyncResourceHandler::EndWrite(written); | 216 return MojoAsyncResourceHandler::EndWrite(written); |
| 213 } | 217 } |
| 218 net::IOBufferWithSize* GetResponseMetadata( |
| 219 net::URLRequest* request) override { |
| 220 return metadata_.get(); |
| 221 } |
| 222 |
| 214 void ReportBadMessage(const std::string& error) override { | 223 void ReportBadMessage(const std::string& error) override { |
| 215 has_received_bad_message_ = true; | 224 has_received_bad_message_ = true; |
| 216 } | 225 } |
| 217 | 226 |
| 218 bool is_begin_write_expectation_set_ = false; | 227 bool is_begin_write_expectation_set_ = false; |
| 219 bool is_end_write_expectation_set_ = false; | 228 bool is_end_write_expectation_set_ = false; |
| 220 bool has_received_bad_message_ = false; | 229 bool has_received_bad_message_ = false; |
| 221 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; | 230 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; |
| 222 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; | 231 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; |
| 232 scoped_refptr<net::IOBufferWithSize> metadata_; |
| 223 | 233 |
| 224 DISALLOW_COPY_AND_ASSIGN( | 234 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations); |
| 225 MojoAsyncResourceHandlerWithCustomDataPipeOperations); | |
| 226 }; | 235 }; |
| 227 | 236 |
| 228 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { | 237 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { |
| 229 public: | 238 public: |
| 230 TestURLLoaderFactory() {} | 239 TestURLLoaderFactory() {} |
| 231 ~TestURLLoaderFactory() override {} | 240 ~TestURLLoaderFactory() override {} |
| 232 | 241 |
| 233 void CreateLoaderAndStart( | 242 void CreateLoaderAndStart( |
| 234 mojom::URLLoaderAssociatedRequest request, | 243 mojom::URLLoaderAssociatedRequest request, |
| 235 int32_t routing_id, | 244 int32_t routing_id, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo( | 310 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo( |
| 302 url_loader_factory_.associated_group())); | 311 url_loader_factory_.associated_group())); |
| 303 | 312 |
| 304 url_loader_factory_.FlushForTesting(); | 313 url_loader_factory_.FlushForTesting(); |
| 305 DCHECK(weak_binding); | 314 DCHECK(weak_binding); |
| 306 TestURLLoaderFactory* factory_impl = | 315 TestURLLoaderFactory* factory_impl = |
| 307 static_cast<TestURLLoaderFactory*>(weak_binding->impl()); | 316 static_cast<TestURLLoaderFactory*>(weak_binding->impl()); |
| 308 | 317 |
| 309 mojom::URLLoaderClientAssociatedPtr client_ptr; | 318 mojom::URLLoaderClientAssociatedPtr client_ptr; |
| 310 client_ptr.Bind(factory_impl->PassClientPtrInfo()); | 319 client_ptr.Bind(factory_impl->PassClientPtrInfo()); |
| 311 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( | 320 handler_.reset(new MojoAsyncResourceHandlerWithStubOperations( |
| 312 request_.get(), &rdh_, factory_impl->PassLoaderRequest(), | 321 request_.get(), &rdh_, factory_impl->PassLoaderRequest(), |
| 313 std::move(client_ptr))); | 322 std::move(client_ptr))); |
| 314 mock_loader_.reset(new MockResourceLoader(handler_.get())); | 323 mock_loader_.reset(new MockResourceLoader(handler_.get())); |
| 315 } | 324 } |
| 316 | 325 |
| 317 virtual ~MojoAsyncResourceHandlerTestBase() { | 326 virtual ~MojoAsyncResourceHandlerTestBase() { |
| 318 MojoAsyncResourceHandler::SetAllocationSizeForTesting( | 327 MojoAsyncResourceHandler::SetAllocationSizeForTesting( |
| 319 MojoAsyncResourceHandler::kDefaultAllocationSize); | 328 MojoAsyncResourceHandler::kDefaultAllocationSize); |
| 320 base::RunLoop().RunUntilIdle(); | 329 base::RunLoop().RunUntilIdle(); |
| 321 } | 330 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 345 | 354 |
| 346 TestBrowserThreadBundle thread_bundle_; | 355 TestBrowserThreadBundle thread_bundle_; |
| 347 TestResourceDispatcherHostDelegate rdh_delegate_; | 356 TestResourceDispatcherHostDelegate rdh_delegate_; |
| 348 ResourceDispatcherHostImpl rdh_; | 357 ResourceDispatcherHostImpl rdh_; |
| 349 mojom::URLLoaderFactoryPtr url_loader_factory_; | 358 mojom::URLLoaderFactoryPtr url_loader_factory_; |
| 350 mojom::URLLoaderAssociatedPtr url_loader_proxy_; | 359 mojom::URLLoaderAssociatedPtr url_loader_proxy_; |
| 351 TestURLLoaderClient url_loader_client_; | 360 TestURLLoaderClient url_loader_client_; |
| 352 std::unique_ptr<TestBrowserContext> browser_context_; | 361 std::unique_ptr<TestBrowserContext> browser_context_; |
| 353 net::TestDelegate url_request_delegate_; | 362 net::TestDelegate url_request_delegate_; |
| 354 std::unique_ptr<net::URLRequest> request_; | 363 std::unique_ptr<net::URLRequest> request_; |
| 355 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> | 364 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_; |
| 356 handler_; | |
| 357 std::unique_ptr<MockResourceLoader> mock_loader_; | 365 std::unique_ptr<MockResourceLoader> mock_loader_; |
| 358 | 366 |
| 359 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); | 367 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); |
| 360 }; | 368 }; |
| 361 | 369 |
| 362 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, | 370 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, |
| 363 public ::testing::Test {}; | 371 public ::testing::Test {}; |
| 364 | 372 |
| 365 // This test class is parameterized with MojoAsyncResourceHandler's allocation | 373 // This test class is parameterized with MojoAsyncResourceHandler's allocation |
| 366 // size. | 374 // size. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 379 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); | 387 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); |
| 380 } | 388 } |
| 381 | 389 |
| 382 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { | 390 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { |
| 383 EXPECT_EQ(MockResourceLoader::Status::IDLE, | 391 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 384 mock_loader_->OnWillStart(request_->url())); | 392 mock_loader_->OnWillStart(request_->url())); |
| 385 } | 393 } |
| 386 | 394 |
| 387 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { | 395 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { |
| 388 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | 396 rdh_delegate_.set_num_on_response_started_calls_expectation(1); |
| 397 scoped_refptr<net::IOBufferWithSize> metadata = new net::IOBufferWithSize(5); |
| 398 memcpy(metadata->data(), "hello", 5); |
| 399 handler_->SetMetadata(metadata); |
| 400 |
| 389 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 401 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 390 mock_loader_->OnWillStart(request_->url())); | 402 mock_loader_->OnWillStart(request_->url())); |
| 391 | 403 |
| 392 scoped_refptr<ResourceResponse> response = new ResourceResponse(); | 404 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
| 393 response->head.content_length = 99; | 405 response->head.content_length = 99; |
| 394 response->head.request_start = | 406 response->head.request_start = |
| 395 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); | 407 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); |
| 396 response->head.response_start = | 408 response->head.response_start = |
| 397 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); | 409 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); |
| 398 | 410 |
| 399 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls()); | 411 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls()); |
| 400 base::TimeTicks now1 = base::TimeTicks::Now(); | 412 base::TimeTicks now1 = base::TimeTicks::Now(); |
| 401 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 413 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 402 mock_loader_->OnResponseStarted(response)); | 414 mock_loader_->OnResponseStarted(response)); |
| 403 base::TimeTicks now2 = base::TimeTicks::Now(); | 415 base::TimeTicks now2 = base::TimeTicks::Now(); |
| 404 | 416 |
| 405 EXPECT_EQ(request_->creation_time(), response->head.request_start); | 417 EXPECT_EQ(request_->creation_time(), response->head.request_start); |
| 406 EXPECT_LE(now1, response->head.response_start); | 418 EXPECT_LE(now1, response->head.response_start); |
| 407 EXPECT_LE(response->head.response_start, now2); | 419 EXPECT_LE(response->head.response_start, now2); |
| 408 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls()); | 420 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls()); |
| 409 | 421 |
| 410 url_loader_client_.RunUntilResponseReceived(); | 422 url_loader_client_.RunUntilResponseReceived(); |
| 411 EXPECT_EQ(response->head.request_start, | 423 EXPECT_EQ(response->head.request_start, |
| 412 url_loader_client_.response_head().request_start); | 424 url_loader_client_.response_head().request_start); |
| 413 EXPECT_EQ(response->head.response_start, | 425 EXPECT_EQ(response->head.response_start, |
| 414 url_loader_client_.response_head().response_start); | 426 url_loader_client_.response_head().response_start); |
| 415 EXPECT_EQ(99, url_loader_client_.response_head().content_length); | 427 EXPECT_EQ(99, url_loader_client_.response_head().content_length); |
| 428 |
| 429 url_loader_client_.RunUntilCachedMetadataReceived(); |
| 430 EXPECT_EQ("hello", url_loader_client_.cached_metadata()); |
| 416 } | 431 } |
| 417 | 432 |
| 418 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { | 433 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { |
| 419 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); | 434 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); |
| 420 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); | 435 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); |
| 421 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); | 436 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); |
| 422 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); | 437 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); |
| 423 handler_ = nullptr; | 438 handler_ = nullptr; |
| 424 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); | 439 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); |
| 425 } | 440 } |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 } | 1137 } |
| 1123 } | 1138 } |
| 1124 EXPECT_EQ("B", body); | 1139 EXPECT_EQ("B", body); |
| 1125 } | 1140 } |
| 1126 | 1141 |
| 1127 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, | 1142 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1128 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1143 MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1129 ::testing::Values(8, 32 * 2014)); | 1144 ::testing::Values(8, 32 * 2014)); |
| 1130 } // namespace | 1145 } // namespace |
| 1131 } // namespace content | 1146 } // namespace content |
| OLD | NEW |