| 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/test_url_loader_client.h" | 5 #include "content/browser/loader/test_url_loader_client.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" |
| 7 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 8 | 10 |
| 9 namespace content { | 11 namespace content { |
| 10 | 12 |
| 13 namespace { |
| 14 |
| 15 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { |
| 16 public: |
| 17 class Waiter final : public base::RefCounted<Waiter> { |
| 18 public: |
| 19 Waiter() = default; |
| 20 void Signal(mojom::URLLoaderClientAssociatedPtrInfo client_ptr) { |
| 21 client_ptr_ = std::move(client_ptr); |
| 22 run_loop_.Quit(); |
| 23 } |
| 24 mojom::URLLoaderClientAssociatedPtrInfo Wait() { |
| 25 run_loop_.Run(); |
| 26 return std::move(client_ptr_); |
| 27 } |
| 28 |
| 29 private: |
| 30 friend class base::RefCounted<Waiter>; |
| 31 ~Waiter() {} |
| 32 |
| 33 base::RunLoop run_loop_; |
| 34 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(Waiter); |
| 37 }; |
| 38 |
| 39 explicit TestURLLoaderFactory(scoped_refptr<Waiter> waiter) |
| 40 : waiter_(waiter) {} |
| 41 ~TestURLLoaderFactory() override {} |
| 42 |
| 43 void CreateLoaderAndStart( |
| 44 mojom::URLLoaderAssociatedRequest request, |
| 45 int32_t routing_id, |
| 46 int32_t request_id, |
| 47 const ResourceRequest& url_request, |
| 48 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info) override { |
| 49 waiter_->Signal(std::move(client_ptr_info)); |
| 50 } |
| 51 |
| 52 void SyncLoad(int32_t routing_id, |
| 53 int32_t request_id, |
| 54 const ResourceRequest& url_request, |
| 55 const SyncLoadCallback& callback) override { |
| 56 NOTREACHED(); |
| 57 } |
| 58 |
| 59 private: |
| 60 scoped_refptr<Waiter> waiter_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory); |
| 63 }; |
| 64 |
| 65 void CreateURLLoaderFactory(scoped_refptr<TestURLLoaderFactory::Waiter> waiter, |
| 66 mojom::URLLoaderFactoryRequest request) { |
| 67 mojo::MakeStrongBinding( |
| 68 base::MakeUnique<TestURLLoaderFactory>(std::move(waiter)), |
| 69 std::move(request)); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 11 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} | 74 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} |
| 12 TestURLLoaderClient::~TestURLLoaderClient() {} | 75 TestURLLoaderClient::~TestURLLoaderClient() {} |
| 13 | 76 |
| 14 void TestURLLoaderClient::OnReceiveResponse( | 77 void TestURLLoaderClient::OnReceiveResponse( |
| 15 const ResourceResponseHead& response_head) { | 78 const ResourceResponseHead& response_head) { |
| 16 has_received_response_ = true; | 79 has_received_response_ = true; |
| 17 response_head_ = response_head; | 80 response_head_ = response_head; |
| 18 if (quit_closure_for_on_received_response_) | 81 if (quit_closure_for_on_received_response_) |
| 19 quit_closure_for_on_received_response_.Run(); | 82 quit_closure_for_on_received_response_.Run(); |
| 20 } | 83 } |
| 21 | 84 |
| 22 void TestURLLoaderClient::OnStartLoadingResponseBody( | 85 void TestURLLoaderClient::OnStartLoadingResponseBody( |
| 23 mojo::ScopedDataPipeConsumerHandle body) { | 86 mojo::ScopedDataPipeConsumerHandle body) { |
| 24 response_body_ = std::move(body); | 87 response_body_ = std::move(body); |
| 25 if (quit_closure_for_on_start_loading_response_body_) | 88 if (quit_closure_for_on_start_loading_response_body_) |
| 26 quit_closure_for_on_start_loading_response_body_.Run(); | 89 quit_closure_for_on_start_loading_response_body_.Run(); |
| 27 } | 90 } |
| 28 | 91 |
| 29 void TestURLLoaderClient::OnComplete( | 92 void TestURLLoaderClient::OnComplete( |
| 30 const ResourceRequestCompletionStatus& status) { | 93 const ResourceRequestCompletionStatus& status) { |
| 31 has_received_completion_ = true; | 94 has_received_completion_ = true; |
| 32 completion_status_ = status; | 95 completion_status_ = status; |
| 33 if (quit_closure_for_on_complete_) | 96 if (quit_closure_for_on_complete_) |
| 34 quit_closure_for_on_complete_.Run(); | 97 quit_closure_for_on_complete_.Run(); |
| 35 } | 98 } |
| 36 | 99 |
| 37 mojom::URLLoaderClientPtr TestURLLoaderClient::CreateInterfacePtrAndBind() { | 100 mojom::URLLoaderClientAssociatedPtrInfo |
| 38 return binding_.CreateInterfacePtrAndBind(); | 101 TestURLLoaderClient::CreateLocalAssociatedPtrInfo() { |
| 102 scoped_refptr<TestURLLoaderFactory::Waiter> waiter( |
| 103 new TestURLLoaderFactory::Waiter()); |
| 104 CreateURLLoaderFactory(waiter, mojo::GetProxy(&url_loader_factory_)); |
| 105 ResourceRequest request; |
| 106 mojom::URLLoaderAssociatedPtr loader_proxy; |
| 107 |
| 108 mojom::URLLoaderClientAssociatedPtrInfo info; |
| 109 binding_.Bind(&info, url_loader_factory_.associated_group()); |
| 110 url_loader_factory_->CreateLoaderAndStart( |
| 111 mojo::GetProxy(&loader_proxy, url_loader_factory_.associated_group()), 0, |
| 112 0, request, std::move(info)); |
| 113 |
| 114 return waiter->Wait(); |
| 115 } |
| 116 |
| 117 mojom::URLLoaderClientAssociatedPtrInfo |
| 118 TestURLLoaderClient::CreateRemoteAssociatedPtrInfo( |
| 119 mojo::AssociatedGroup* associated_group) { |
| 120 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info; |
| 121 binding_.Bind(&client_ptr_info, associated_group); |
| 122 return client_ptr_info; |
| 39 } | 123 } |
| 40 | 124 |
| 41 void TestURLLoaderClient::Unbind() { | 125 void TestURLLoaderClient::Unbind() { |
| 42 binding_.Unbind(); | 126 binding_.Unbind(); |
| 43 response_body_.reset(); | 127 response_body_.reset(); |
| 44 } | 128 } |
| 45 | 129 |
| 46 void TestURLLoaderClient::RunUntilResponseReceived() { | 130 void TestURLLoaderClient::RunUntilResponseReceived() { |
| 47 base::RunLoop run_loop; | 131 base::RunLoop run_loop; |
| 48 quit_closure_for_on_received_response_ = run_loop.QuitClosure(); | 132 quit_closure_for_on_received_response_ = run_loop.QuitClosure(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 62 void TestURLLoaderClient::RunUntilComplete() { | 146 void TestURLLoaderClient::RunUntilComplete() { |
| 63 if (has_received_completion_) | 147 if (has_received_completion_) |
| 64 return; | 148 return; |
| 65 base::RunLoop run_loop; | 149 base::RunLoop run_loop; |
| 66 quit_closure_for_on_complete_ = run_loop.QuitClosure(); | 150 quit_closure_for_on_complete_ = run_loop.QuitClosure(); |
| 67 run_loop.Run(); | 151 run_loop.Run(); |
| 68 quit_closure_for_on_complete_.Reset(); | 152 quit_closure_for_on_complete_.Reset(); |
| 69 } | 153 } |
| 70 | 154 |
| 71 } // namespace content | 155 } // namespace content |
| OLD | NEW |