| 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/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 10 | 9 |
| 11 namespace content { | 10 namespace content { |
| 12 | 11 |
| 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 | |
| 74 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} | 12 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} |
| 75 TestURLLoaderClient::~TestURLLoaderClient() {} | 13 TestURLLoaderClient::~TestURLLoaderClient() {} |
| 76 | 14 |
| 77 void TestURLLoaderClient::OnReceiveResponse( | 15 void TestURLLoaderClient::OnReceiveResponse( |
| 78 const ResourceResponseHead& response_head) { | 16 const ResourceResponseHead& response_head) { |
| 79 has_received_response_ = true; | 17 has_received_response_ = true; |
| 80 response_head_ = response_head; | 18 response_head_ = response_head; |
| 81 if (quit_closure_for_on_received_response_) | 19 if (quit_closure_for_on_received_response_) |
| 82 quit_closure_for_on_received_response_.Run(); | 20 quit_closure_for_on_received_response_.Run(); |
| 83 } | 21 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 100 | 38 |
| 101 void TestURLLoaderClient::OnComplete( | 39 void TestURLLoaderClient::OnComplete( |
| 102 const ResourceRequestCompletionStatus& status) { | 40 const ResourceRequestCompletionStatus& status) { |
| 103 has_received_completion_ = true; | 41 has_received_completion_ = true; |
| 104 completion_status_ = status; | 42 completion_status_ = status; |
| 105 if (quit_closure_for_on_complete_) | 43 if (quit_closure_for_on_complete_) |
| 106 quit_closure_for_on_complete_.Run(); | 44 quit_closure_for_on_complete_.Run(); |
| 107 } | 45 } |
| 108 | 46 |
| 109 mojom::URLLoaderClientAssociatedPtrInfo | 47 mojom::URLLoaderClientAssociatedPtrInfo |
| 110 TestURLLoaderClient::CreateLocalAssociatedPtrInfo() { | |
| 111 scoped_refptr<TestURLLoaderFactory::Waiter> waiter( | |
| 112 new TestURLLoaderFactory::Waiter()); | |
| 113 CreateURLLoaderFactory(waiter, mojo::GetProxy(&url_loader_factory_)); | |
| 114 ResourceRequest request; | |
| 115 mojom::URLLoaderAssociatedPtr loader_proxy; | |
| 116 | |
| 117 mojom::URLLoaderClientAssociatedPtrInfo info; | |
| 118 binding_.Bind(&info, url_loader_factory_.associated_group()); | |
| 119 url_loader_factory_->CreateLoaderAndStart( | |
| 120 mojo::GetProxy(&loader_proxy, url_loader_factory_.associated_group()), 0, | |
| 121 0, request, std::move(info)); | |
| 122 | |
| 123 return waiter->Wait(); | |
| 124 } | |
| 125 | |
| 126 mojom::URLLoaderClientAssociatedPtrInfo | |
| 127 TestURLLoaderClient::CreateRemoteAssociatedPtrInfo( | 48 TestURLLoaderClient::CreateRemoteAssociatedPtrInfo( |
| 128 mojo::AssociatedGroup* associated_group) { | 49 mojo::AssociatedGroup* associated_group) { |
| 129 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info; | 50 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info; |
| 130 binding_.Bind(&client_ptr_info, associated_group); | 51 binding_.Bind(&client_ptr_info, associated_group); |
| 131 return client_ptr_info; | 52 return client_ptr_info; |
| 132 } | 53 } |
| 133 | 54 |
| 134 void TestURLLoaderClient::Unbind() { | 55 void TestURLLoaderClient::Unbind() { |
| 135 binding_.Unbind(); | 56 binding_.Unbind(); |
| 136 response_body_.reset(); | 57 response_body_.reset(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 162 void TestURLLoaderClient::RunUntilComplete() { | 83 void TestURLLoaderClient::RunUntilComplete() { |
| 163 if (has_received_completion_) | 84 if (has_received_completion_) |
| 164 return; | 85 return; |
| 165 base::RunLoop run_loop; | 86 base::RunLoop run_loop; |
| 166 quit_closure_for_on_complete_ = run_loop.QuitClosure(); | 87 quit_closure_for_on_complete_ = run_loop.QuitClosure(); |
| 167 run_loop.Run(); | 88 run_loop.Run(); |
| 168 quit_closure_for_on_complete_.Reset(); | 89 quit_closure_for_on_complete_.Reset(); |
| 169 } | 90 } |
| 170 | 91 |
| 171 } // namespace content | 92 } // namespace content |
| OLD | NEW |