OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/url_loader_factory_holder.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 9 #include "content/browser/loader/resource_message_filter.h" |
| 10 #include "content/common/resource_request.h" |
| 11 #include "content/common/url_loader.mojom.h" |
| 12 #include "content/common/url_loader_factory.mojom.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 // This class is an implementation of mojom::URLLoaderFactory that creates |
| 19 // a mojom::URLLoader. |
| 20 class URLLoaderFactoryHolder::URLLoaderFactoryImpl final |
| 21 : public mojom::URLLoaderFactory { |
| 22 public: |
| 23 explicit URLLoaderFactoryImpl( |
| 24 scoped_refptr<ResourceMessageFilter> resource_message_filter) |
| 25 : resource_message_filter_(std::move(resource_message_filter)) { |
| 26 DCHECK(resource_message_filter_); |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 28 } |
| 29 ~URLLoaderFactoryImpl() override { DCHECK_CURRENTLY_ON(BrowserThread::IO); } |
| 30 |
| 31 void CreateLoaderAndStart(mojom::URLLoaderRequest request, |
| 32 int32_t request_id, |
| 33 const ResourceRequest& url_request, |
| 34 mojom::URLLoaderClientPtr client) override { |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 36 |
| 37 // TODO(yhirano): Provide the right routing ID. |
| 38 const int routing_id = 0; |
| 39 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 40 rdh->OnRequestResourceWithMojo(routing_id, request_id, url_request, |
| 41 std::move(request), std::move(client), |
| 42 resource_message_filter_.get()); |
| 43 } |
| 44 |
| 45 void Bind(mojo::InterfaceRequest<mojom::URLLoaderFactory> request) { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 47 binding_.reset( |
| 48 new mojo::Binding<URLLoaderFactory>(this, std::move(request))); |
| 49 } |
| 50 |
| 51 private: |
| 52 scoped_refptr<ResourceMessageFilter> resource_message_filter_; |
| 53 std::unique_ptr<mojo::Binding<mojom::URLLoaderFactory>> binding_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImpl); |
| 56 }; |
| 57 |
| 58 URLLoaderFactoryHolder::URLLoaderFactoryHolder( |
| 59 scoped_refptr<ResourceMessageFilter> resource_message_filter, |
| 60 mojo::InterfaceRequest<mojom::URLLoaderFactory> request) |
| 61 : factory_(new URLLoaderFactoryImpl(resource_message_filter)) { |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 63 // This |Unretained| is safe because ~URLLoaderFactoryHolder is the only |
| 64 // function that posts a |factory_| deletion task to the IO thread and |
| 65 // ~URLLoaderFactoryHolder is called on this thread. |
| 66 BrowserThread::PostTask( |
| 67 BrowserThread::IO, FROM_HERE, |
| 68 base::Bind(&URLLoaderFactoryImpl::Bind, base::Unretained(factory_.get()), |
| 69 base::Passed(std::move(request)))); |
| 70 } |
| 71 |
| 72 URLLoaderFactoryHolder::~URLLoaderFactoryHolder() { |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 74 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, factory_.release()); |
| 75 } |
| 76 |
| 77 } // namespace content |
OLD | NEW |