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