| 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/url_loader_factory_impl.h" | 5 #include "content/browser/loader/url_loader_factory_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 8 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 9 #include "content/browser/loader/resource_requester_info.h" | 9 #include "content/browser/loader/resource_requester_info.h" |
| 10 #include "content/common/resource_request.h" | 10 #include "content/common/resource_request.h" |
| 11 #include "content/common/url_loader.mojom.h" | 11 #include "content/common/url_loader.mojom.h" |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 | 13 |
| 15 namespace content { | 14 namespace content { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 void DispatchSyncLoadResult( | 18 void DispatchSyncLoadResult( |
| 20 const URLLoaderFactoryImpl::SyncLoadCallback& callback, | 19 const URLLoaderFactoryImpl::SyncLoadCallback& callback, |
| 21 const SyncLoadResult* result) { | 20 const SyncLoadResult* result) { |
| 22 // |result| can be null when a loading task is aborted unexpectedly. Reply | 21 // |result| can be null when a loading task is aborted unexpectedly. Reply |
| 23 // with a failure result on that case. | 22 // with a failure result on that case. |
| 24 // TODO(tzik): Test null-result case. | 23 // TODO(tzik): Test null-result case. |
| 25 if (!result) { | 24 if (!result) { |
| 26 SyncLoadResult failure; | 25 SyncLoadResult failure; |
| 27 failure.error_code = net::ERR_FAILED; | 26 failure.error_code = net::ERR_FAILED; |
| 28 callback.Run(failure); | 27 callback.Run(failure); |
| 29 return; | 28 return; |
| 30 } | 29 } |
| 31 | 30 |
| 32 callback.Run(*result); | 31 callback.Run(*result); |
| 33 } | 32 } |
| 34 | 33 |
| 35 } // namespace | 34 } // namespace |
| 36 | 35 |
| 37 URLLoaderFactoryImpl::URLLoaderFactoryImpl( | 36 URLLoaderFactoryImpl::URLLoaderFactoryImpl( |
| 38 scoped_refptr<ResourceRequesterInfo> requester_info) | 37 scoped_refptr<ResourceRequesterInfo> requester_info, |
| 39 : requester_info_(std::move(requester_info)) { | 38 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_runner) |
| 39 : requester_info_(std::move(requester_info)), |
| 40 io_thread_task_runner_(io_thread_runner) { |
| 40 DCHECK((requester_info_->IsRenderer() && requester_info_->filter()) || | 41 DCHECK((requester_info_->IsRenderer() && requester_info_->filter()) || |
| 41 requester_info_->IsNavigationPreload()); | 42 requester_info_->IsNavigationPreload()); |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 43 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 43 } | 44 } |
| 44 | 45 |
| 45 URLLoaderFactoryImpl::~URLLoaderFactoryImpl() { | 46 URLLoaderFactoryImpl::~URLLoaderFactoryImpl() { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 47 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void URLLoaderFactoryImpl::CreateLoaderAndStart( | 50 void URLLoaderFactoryImpl::CreateLoaderAndStart( |
| 50 mojom::URLLoaderAssociatedRequest request, | 51 mojom::URLLoaderAssociatedRequest request, |
| 51 int32_t routing_id, | 52 int32_t routing_id, |
| 52 int32_t request_id, | 53 int32_t request_id, |
| 53 const ResourceRequest& url_request, | 54 const ResourceRequest& url_request, |
| 54 mojom::URLLoaderClientPtr client) { | 55 mojom::URLLoaderClientPtr client) { |
| 55 CreateLoaderAndStart(requester_info_.get(), std::move(request), routing_id, | 56 CreateLoaderAndStart(requester_info_.get(), std::move(request), routing_id, |
| 56 request_id, url_request, std::move(client)); | 57 request_id, url_request, std::move(client)); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void URLLoaderFactoryImpl::SyncLoad(int32_t routing_id, | 60 void URLLoaderFactoryImpl::SyncLoad(int32_t routing_id, |
| 60 int32_t request_id, | 61 int32_t request_id, |
| 61 const ResourceRequest& url_request, | 62 const ResourceRequest& url_request, |
| 62 const SyncLoadCallback& callback) { | 63 const SyncLoadCallback& callback) { |
| 63 SyncLoad(requester_info_.get(), routing_id, request_id, url_request, | 64 SyncLoad(requester_info_.get(), routing_id, request_id, url_request, |
| 64 callback); | 65 callback); |
| 65 } | 66 } |
| 66 | 67 |
| 67 // static | 68 // static |
| 68 void URLLoaderFactoryImpl::CreateLoaderAndStart( | 69 void URLLoaderFactoryImpl::CreateLoaderAndStart( |
| 69 ResourceRequesterInfo* requester_info, | 70 ResourceRequesterInfo* requester_info, |
| 70 mojom::URLLoaderAssociatedRequest request, | 71 mojom::URLLoaderAssociatedRequest request, |
| 71 int32_t routing_id, | 72 int32_t routing_id, |
| 72 int32_t request_id, | 73 int32_t request_id, |
| 73 const ResourceRequest& url_request, | 74 const ResourceRequest& url_request, |
| 74 mojom::URLLoaderClientPtr client) { | 75 mojom::URLLoaderClientPtr client) { |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 76 DCHECK(ResourceDispatcherHostImpl::Get() |
| 77 ->io_thread_task_runner() |
| 78 ->BelongsToCurrentThread()); |
| 76 | 79 |
| 77 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); | 80 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 78 rdh->OnRequestResourceWithMojo(requester_info, routing_id, request_id, | 81 rdh->OnRequestResourceWithMojo(requester_info, routing_id, request_id, |
| 79 url_request, std::move(request), | 82 url_request, std::move(request), |
| 80 std::move(client)); | 83 std::move(client)); |
| 81 } | 84 } |
| 82 | 85 |
| 83 // static | 86 // static |
| 84 void URLLoaderFactoryImpl::SyncLoad(ResourceRequesterInfo* requester_info, | 87 void URLLoaderFactoryImpl::SyncLoad(ResourceRequesterInfo* requester_info, |
| 85 int32_t routing_id, | 88 int32_t routing_id, |
| 86 int32_t request_id, | 89 int32_t request_id, |
| 87 const ResourceRequest& url_request, | 90 const ResourceRequest& url_request, |
| 88 const SyncLoadCallback& callback) { | 91 const SyncLoadCallback& callback) { |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK(ResourceDispatcherHostImpl::Get() |
| 93 ->io_thread_task_runner() |
| 94 ->BelongsToCurrentThread()); |
| 90 | 95 |
| 91 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); | 96 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get(); |
| 92 rdh->OnSyncLoadWithMojo(requester_info, routing_id, request_id, url_request, | 97 rdh->OnSyncLoadWithMojo(requester_info, routing_id, request_id, url_request, |
| 93 base::Bind(&DispatchSyncLoadResult, callback)); | 98 base::Bind(&DispatchSyncLoadResult, callback)); |
| 94 } | 99 } |
| 95 | 100 |
| 96 void URLLoaderFactoryImpl::Create( | 101 void URLLoaderFactoryImpl::Create( |
| 97 scoped_refptr<ResourceRequesterInfo> requester_info, | 102 scoped_refptr<ResourceRequesterInfo> requester_info, |
| 98 mojo::InterfaceRequest<mojom::URLLoaderFactory> request) { | 103 mojo::InterfaceRequest<mojom::URLLoaderFactory> request, |
| 99 mojo::MakeStrongBinding( | 104 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_runner) { |
| 100 base::WrapUnique(new URLLoaderFactoryImpl(std::move(requester_info))), | 105 mojo::MakeStrongBinding(base::WrapUnique(new URLLoaderFactoryImpl( |
| 101 std::move(request)); | 106 std::move(requester_info), io_thread_runner)), |
| 107 std::move(request)); |
| 102 } | 108 } |
| 103 | 109 |
| 104 } // namespace content | 110 } // namespace content |
| OLD | NEW |