Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: content/browser/loader/mojo_async_resource_handler_unittest.cc

Issue 2466843002: Cancel the request when URLLoader is gone (Closed)
Patch Set: fix Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 257
257 bool is_begin_write_expectation_set_ = false; 258 bool is_begin_write_expectation_set_ = false;
258 bool is_end_write_expectation_set_ = false; 259 bool is_end_write_expectation_set_ = false;
259 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 260 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
260 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 261 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
261 262
262 DISALLOW_COPY_AND_ASSIGN( 263 DISALLOW_COPY_AND_ASSIGN(
263 MojoAsyncResourceHandlerWithCustomDataPipeOperations); 264 MojoAsyncResourceHandlerWithCustomDataPipeOperations);
264 }; 265 };
265 266
267 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
mmenke 2016/11/08 15:14:21 Could you do this in a separate CL? I'm not famil
yhirano 2016/11/09 03:38:07 Done: https://codereview.chromium.org/2489793002/
268 public:
269 class Waiter final : public base::RefCounted<Waiter> {
270 public:
271 Waiter() = default;
272 void Signal(mojom::URLLoaderAssociatedRequest loader_request,
273 mojom::URLLoaderClientAssociatedPtrInfo client_ptr) {
274 loader_request_ = std::move(loader_request);
275 client_ptr_ = std::move(client_ptr);
276 run_loop_.Quit();
277 }
278 std::pair<mojom::URLLoaderAssociatedRequest,
279 mojom::URLLoaderClientAssociatedPtrInfo>
280 Wait() {
281 run_loop_.Run();
282 return std::make_pair(std::move(loader_request_), std::move(client_ptr_));
283 }
284
285 private:
286 friend class base::RefCounted<Waiter>;
287 ~Waiter() {}
288
289 base::RunLoop run_loop_;
290 mojom::URLLoaderAssociatedRequest loader_request_;
291 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_;
292
293 DISALLOW_COPY_AND_ASSIGN(Waiter);
294 };
295
296 explicit TestURLLoaderFactory(scoped_refptr<Waiter> waiter)
297 : waiter_(waiter) {}
298 ~TestURLLoaderFactory() override {}
299
300 void CreateLoaderAndStart(
301 mojom::URLLoaderAssociatedRequest request,
302 int32_t routing_id,
303 int32_t request_id,
304 const ResourceRequest& url_request,
305 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info) override {
306 waiter_->Signal(std::move(request), std::move(client_ptr_info));
307 }
308
309 void SyncLoad(int32_t routing_id,
310 int32_t request_id,
311 const ResourceRequest& url_request,
312 const SyncLoadCallback& callback) override {
313 NOTREACHED();
314 }
315
316 private:
317 scoped_refptr<Waiter> waiter_;
318
319 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory);
320 };
321
266 class MojoAsyncResourceHandlerTestBase { 322 class MojoAsyncResourceHandlerTestBase {
267 public: 323 public:
268 MojoAsyncResourceHandlerTestBase() 324 MojoAsyncResourceHandlerTestBase()
269 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 325 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
270 browser_context_(new TestBrowserContext()) { 326 browser_context_(new TestBrowserContext()) {
271 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024); 327 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024);
272 rdh_.SetDelegate(&rdh_delegate_); 328 rdh_.SetDelegate(&rdh_delegate_);
273 329
274 url_request_delegate_.reset(new net::TestDelegate()); 330 url_request_delegate_.reset(new net::TestDelegate());
275 net::URLRequestContext* request_context = 331 net::URLRequestContext* request_context =
276 browser_context_->GetResourceContext()->GetRequestContext(); 332 browser_context_->GetResourceContext()->GetRequestContext();
277 request_ = request_context->CreateRequest( 333 request_ = request_context->CreateRequest(
278 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_TIMED_OUT), 334 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_TIMED_OUT),
279 net::DEFAULT_PRIORITY, url_request_delegate_.get()); 335 net::DEFAULT_PRIORITY, url_request_delegate_.get());
280 ResourceRequestInfo::AllocateForTesting( 336 ResourceRequestInfo::AllocateForTesting(
281 request_.get(), // request 337 request_.get(), // request
282 RESOURCE_TYPE_XHR, // resource_type 338 RESOURCE_TYPE_XHR, // resource_type
283 browser_context_->GetResourceContext(), // context 339 browser_context_->GetResourceContext(), // context
284 2, // render_process_id 340 2, // render_process_id
285 0, // render_view_id 341 0, // render_view_id
286 0, // render_frame_id 342 0, // render_frame_id
287 true, // is_main_frame 343 true, // is_main_frame
288 false, // parent_is_main_frame 344 false, // parent_is_main_frame
289 false, // allow_download 345 false, // allow_download
290 true, // is_async 346 true, // is_async
291 false // is_using_lofi 347 false // is_using_lofi
292 ); 348 );
293 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info = 349
294 url_loader_client_.CreateLocalAssociatedPtrInfo(); 350 ResourceRequest request;
351 scoped_refptr<TestURLLoaderFactory::Waiter> waiter(
352 new TestURLLoaderFactory::Waiter());
353 mojo::MakeStrongBinding(base::MakeUnique<TestURLLoaderFactory>(waiter),
354 mojo::GetProxy(&url_loader_factory_));
355 url_loader_factory_->CreateLoaderAndStart(
356 mojo::GetProxy(&url_loader_proxy_,
357 url_loader_factory_.associated_group()),
358 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo(
359 url_loader_factory_.associated_group()));
360
361 auto pair = waiter->Wait();
362
295 mojom::URLLoaderClientAssociatedPtr client_ptr; 363 mojom::URLLoaderClientAssociatedPtr client_ptr;
296 client_ptr.Bind(std::move(client_ptr_info)); 364 client_ptr.Bind(std::move(pair.second));
297 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( 365 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations(
298 request_.get(), &rdh_, nullptr, std::move(client_ptr))); 366 request_.get(), &rdh_, std::move(pair.first), std::move(client_ptr)));
299 handler_->SetController(&resource_controller_); 367 handler_->SetController(&resource_controller_);
300 } 368 }
301 369
302 virtual ~MojoAsyncResourceHandlerTestBase() { 370 virtual ~MojoAsyncResourceHandlerTestBase() {
303 net::URLRequestFilter::GetInstance()->ClearHandlers(); 371 net::URLRequestFilter::GetInstance()->ClearHandlers();
304 MojoAsyncResourceHandler::SetAllocationSizeForTesting( 372 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
305 MojoAsyncResourceHandler::kDefaultAllocationSize); 373 MojoAsyncResourceHandler::kDefaultAllocationSize);
306 base::RunLoop().RunUntilIdle(); 374 base::RunLoop().RunUntilIdle();
307 } 375 }
308 376
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; 408 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response.";
341 return false; 409 return false;
342 } 410 }
343 url_loader_client_.RunUntilResponseReceived(); 411 url_loader_client_.RunUntilResponseReceived();
344 return true; 412 return true;
345 } 413 }
346 414
347 TestBrowserThreadBundle thread_bundle_; 415 TestBrowserThreadBundle thread_bundle_;
348 TestResourceDispatcherHostDelegate rdh_delegate_; 416 TestResourceDispatcherHostDelegate rdh_delegate_;
349 ResourceDispatcherHostImpl rdh_; 417 ResourceDispatcherHostImpl rdh_;
418 mojom::URLLoaderFactoryPtr url_loader_factory_;
419 mojom::URLLoaderAssociatedPtr url_loader_proxy_;
350 TestURLLoaderClient url_loader_client_; 420 TestURLLoaderClient url_loader_client_;
351 TestResourceController resource_controller_; 421 TestResourceController resource_controller_;
352 std::unique_ptr<TestBrowserContext> browser_context_; 422 std::unique_ptr<TestBrowserContext> browser_context_;
353 std::unique_ptr<net::TestDelegate> url_request_delegate_; 423 std::unique_ptr<net::TestDelegate> url_request_delegate_;
354 std::unique_ptr<net::URLRequest> request_; 424 std::unique_ptr<net::URLRequest> request_;
355 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> 425 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations>
356 handler_; 426 handler_;
357 427
358 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); 428 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase);
359 }; 429 };
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 } 1205 }
1136 } 1206 }
1137 EXPECT_EQ("B", body); 1207 EXPECT_EQ("B", body);
1138 } 1208 }
1139 1209
1140 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1210 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1141 MojoAsyncResourceHandlerWithAllocationSizeTest, 1211 MojoAsyncResourceHandlerWithAllocationSizeTest,
1142 ::testing::Values(8, 32 * 2014)); 1212 ::testing::Values(8, 32 * 2014));
1143 } // namespace 1213 } // namespace
1144 } // namespace content 1214 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.cc ('k') | content/browser/loader/resource_dispatcher_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698