Chromium Code Reviews| 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 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "content/public/browser/resource_controller.h" | 26 #include "content/public/browser/resource_controller.h" |
| 27 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 27 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 28 #include "content/public/browser/resource_throttle.h" | 28 #include "content/public/browser/resource_throttle.h" |
| 29 #include "content/public/browser/stream_info.h" | 29 #include "content/public/browser/stream_info.h" |
| 30 #include "content/public/common/resource_response.h" | 30 #include "content/public/common/resource_response.h" |
| 31 #include "content/public/common/resource_type.h" | 31 #include "content/public/common/resource_type.h" |
| 32 #include "content/public/test/test_browser_context.h" | 32 #include "content/public/test/test_browser_context.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
| 34 #include "mojo/public/c/system/data_pipe.h" | 34 #include "mojo/public/c/system/data_pipe.h" |
| 35 #include "mojo/public/c/system/types.h" | 35 #include "mojo/public/c/system/types.h" |
| 36 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 36 #include "mojo/public/cpp/system/data_pipe.h" | 37 #include "mojo/public/cpp/system/data_pipe.h" |
| 37 #include "net/base/auth.h" | 38 #include "net/base/auth.h" |
| 38 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
| 39 #include "net/http/http_response_headers.h" | 40 #include "net/http/http_response_headers.h" |
| 40 #include "net/http/http_response_info.h" | 41 #include "net/http/http_response_info.h" |
| 41 #include "net/http/http_status_code.h" | 42 #include "net/http/http_status_code.h" |
| 42 #include "net/http/http_util.h" | 43 #include "net/http/http_util.h" |
| 43 #include "net/ssl/client_cert_store.h" | 44 #include "net/ssl/client_cert_store.h" |
| 44 #include "net/test/url_request/url_request_failed_job.h" | 45 #include "net/test/url_request/url_request_failed_job.h" |
| 45 #include "net/url_request/url_request.h" | 46 #include "net/url_request/url_request.h" |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 | 261 |
| 261 bool is_begin_write_expectation_set_ = false; | 262 bool is_begin_write_expectation_set_ = false; |
| 262 bool is_end_write_expectation_set_ = false; | 263 bool is_end_write_expectation_set_ = false; |
| 263 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; | 264 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; |
| 264 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; | 265 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; |
| 265 | 266 |
| 266 DISALLOW_COPY_AND_ASSIGN( | 267 DISALLOW_COPY_AND_ASSIGN( |
| 267 MojoAsyncResourceHandlerWithCustomDataPipeOperations); | 268 MojoAsyncResourceHandlerWithCustomDataPipeOperations); |
| 268 }; | 269 }; |
| 269 | 270 |
| 271 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { | |
| 272 public: | |
| 273 class Waiter final : public base::RefCounted<Waiter> { | |
| 274 public: | |
| 275 Waiter() = default; | |
| 276 void Signal(mojom::URLLoaderAssociatedRequest loader_request, | |
| 277 mojom::URLLoaderClientAssociatedPtrInfo client_ptr) { | |
| 278 loader_request_ = std::move(loader_request); | |
| 279 client_ptr_ = std::move(client_ptr); | |
| 280 run_loop_.Quit(); | |
| 281 } | |
| 282 std::pair<mojom::URLLoaderAssociatedRequest, | |
| 283 mojom::URLLoaderClientAssociatedPtrInfo> | |
| 284 Wait() { | |
| 285 run_loop_.Run(); | |
| 286 return std::make_pair(std::move(loader_request_), std::move(client_ptr_)); | |
| 287 } | |
| 288 | |
| 289 private: | |
| 290 friend class base::RefCounted<Waiter>; | |
| 291 ~Waiter() {} | |
| 292 | |
| 293 base::RunLoop run_loop_; | |
| 294 mojom::URLLoaderAssociatedRequest loader_request_; | |
| 295 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_; | |
| 296 | |
| 297 DISALLOW_COPY_AND_ASSIGN(Waiter); | |
| 298 }; | |
| 299 | |
| 300 explicit TestURLLoaderFactory(scoped_refptr<Waiter> waiter) | |
| 301 : waiter_(waiter) {} | |
| 302 ~TestURLLoaderFactory() override {} | |
| 303 | |
| 304 void CreateLoaderAndStart( | |
| 305 mojom::URLLoaderAssociatedRequest request, | |
| 306 int32_t routing_id, | |
| 307 int32_t request_id, | |
| 308 const ResourceRequest& url_request, | |
| 309 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info) override { | |
| 310 waiter_->Signal(std::move(request), std::move(client_ptr_info)); | |
| 311 } | |
| 312 | |
| 313 void SyncLoad(int32_t routing_id, | |
| 314 int32_t request_id, | |
| 315 const ResourceRequest& url_request, | |
| 316 const SyncLoadCallback& callback) override { | |
| 317 NOTREACHED(); | |
| 318 } | |
| 319 | |
| 320 private: | |
| 321 scoped_refptr<Waiter> waiter_; | |
| 322 | |
| 323 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory); | |
| 324 }; | |
| 325 | |
| 270 class MojoAsyncResourceHandlerTestBase { | 326 class MojoAsyncResourceHandlerTestBase { |
| 271 public: | 327 public: |
| 272 MojoAsyncResourceHandlerTestBase() | 328 MojoAsyncResourceHandlerTestBase() |
| 273 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 329 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 274 browser_context_(new TestBrowserContext()) { | 330 browser_context_(new TestBrowserContext()) { |
| 275 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024); | 331 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024); |
| 276 rdh_.SetDelegate(&rdh_delegate_); | 332 rdh_.SetDelegate(&rdh_delegate_); |
| 277 | 333 |
| 278 url_request_delegate_.reset(new net::TestDelegate()); | 334 url_request_delegate_.reset(new net::TestDelegate()); |
| 279 net::URLRequestContext* request_context = | 335 net::URLRequestContext* request_context = |
| 280 browser_context_->GetResourceContext()->GetRequestContext(); | 336 browser_context_->GetResourceContext()->GetRequestContext(); |
| 281 request_ = request_context->CreateRequest( | 337 request_ = request_context->CreateRequest( |
| 282 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_TIMED_OUT), | 338 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_TIMED_OUT), |
| 283 net::DEFAULT_PRIORITY, url_request_delegate_.get()); | 339 net::DEFAULT_PRIORITY, url_request_delegate_.get()); |
| 284 ResourceRequestInfo::AllocateForTesting( | 340 ResourceRequestInfo::AllocateForTesting( |
| 285 request_.get(), // request | 341 request_.get(), // request |
| 286 RESOURCE_TYPE_XHR, // resource_type | 342 RESOURCE_TYPE_XHR, // resource_type |
| 287 browser_context_->GetResourceContext(), // context | 343 browser_context_->GetResourceContext(), // context |
| 288 2, // render_process_id | 344 2, // render_process_id |
| 289 0, // render_view_id | 345 0, // render_view_id |
| 290 0, // render_frame_id | 346 0, // render_frame_id |
| 291 true, // is_main_frame | 347 true, // is_main_frame |
| 292 false, // parent_is_main_frame | 348 false, // parent_is_main_frame |
| 293 false, // allow_download | 349 false, // allow_download |
| 294 true, // is_async | 350 true, // is_async |
| 295 false // is_using_lofi | 351 false // is_using_lofi |
| 296 ); | 352 ); |
| 297 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info = | 353 |
| 298 url_loader_client_.CreateLocalAssociatedPtrInfo(); | 354 ResourceRequest request; |
| 355 scoped_refptr<TestURLLoaderFactory::Waiter> waiter( | |
|
yzshen1
2016/11/09 17:40:48
I think |waiter| may not be necessary, we could:
-
yhirano
2016/11/10 07:33:53
Thanks, done.
| |
| 356 new TestURLLoaderFactory::Waiter()); | |
| 357 mojo::MakeStrongBinding(base::MakeUnique<TestURLLoaderFactory>(waiter), | |
| 358 mojo::GetProxy(&url_loader_factory_)); | |
| 359 url_loader_factory_->CreateLoaderAndStart( | |
| 360 mojo::GetProxy(&url_loader_proxy_, | |
| 361 url_loader_factory_.associated_group()), | |
| 362 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo( | |
| 363 url_loader_factory_.associated_group())); | |
| 364 | |
| 365 auto pair = waiter->Wait(); | |
| 366 | |
| 299 mojom::URLLoaderClientAssociatedPtr client_ptr; | 367 mojom::URLLoaderClientAssociatedPtr client_ptr; |
| 300 client_ptr.Bind(std::move(client_ptr_info)); | 368 client_ptr.Bind(std::move(pair.second)); |
| 301 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( | 369 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( |
| 302 request_.get(), &rdh_, nullptr, std::move(client_ptr))); | 370 request_.get(), &rdh_, std::move(pair.first), std::move(client_ptr))); |
| 303 handler_->SetController(&resource_controller_); | 371 handler_->SetController(&resource_controller_); |
| 304 } | 372 } |
| 305 | 373 |
| 306 virtual ~MojoAsyncResourceHandlerTestBase() { | 374 virtual ~MojoAsyncResourceHandlerTestBase() { |
| 307 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 375 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 308 MojoAsyncResourceHandler::SetAllocationSizeForTesting( | 376 MojoAsyncResourceHandler::SetAllocationSizeForTesting( |
| 309 MojoAsyncResourceHandler::kDefaultAllocationSize); | 377 MojoAsyncResourceHandler::kDefaultAllocationSize); |
| 310 base::RunLoop().RunUntilIdle(); | 378 base::RunLoop().RunUntilIdle(); |
| 311 } | 379 } |
| 312 | 380 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; | 412 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; |
| 345 return false; | 413 return false; |
| 346 } | 414 } |
| 347 url_loader_client_.RunUntilResponseReceived(); | 415 url_loader_client_.RunUntilResponseReceived(); |
| 348 return true; | 416 return true; |
| 349 } | 417 } |
| 350 | 418 |
| 351 TestBrowserThreadBundle thread_bundle_; | 419 TestBrowserThreadBundle thread_bundle_; |
| 352 TestResourceDispatcherHostDelegate rdh_delegate_; | 420 TestResourceDispatcherHostDelegate rdh_delegate_; |
| 353 ResourceDispatcherHostImpl rdh_; | 421 ResourceDispatcherHostImpl rdh_; |
| 422 mojom::URLLoaderFactoryPtr url_loader_factory_; | |
| 423 mojom::URLLoaderAssociatedPtr url_loader_proxy_; | |
| 354 TestURLLoaderClient url_loader_client_; | 424 TestURLLoaderClient url_loader_client_; |
| 355 TestResourceController resource_controller_; | 425 TestResourceController resource_controller_; |
| 356 std::unique_ptr<TestBrowserContext> browser_context_; | 426 std::unique_ptr<TestBrowserContext> browser_context_; |
| 357 std::unique_ptr<net::TestDelegate> url_request_delegate_; | 427 std::unique_ptr<net::TestDelegate> url_request_delegate_; |
| 358 std::unique_ptr<net::URLRequest> request_; | 428 std::unique_ptr<net::URLRequest> request_; |
| 359 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> | 429 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> |
| 360 handler_; | 430 handler_; |
| 361 | 431 |
| 362 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); | 432 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); |
| 363 }; | 433 }; |
| (...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1139 } | 1209 } |
| 1140 } | 1210 } |
| 1141 EXPECT_EQ("B", body); | 1211 EXPECT_EQ("B", body); |
| 1142 } | 1212 } |
| 1143 | 1213 |
| 1144 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, | 1214 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1145 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1215 MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1146 ::testing::Values(8, 32 * 2014)); | 1216 ::testing::Values(8, 32 * 2014)); |
| 1147 } // namespace | 1217 } // namespace |
| 1148 } // namespace content | 1218 } // namespace content |
| OLD | NEW |