Chromium Code Reviews| Index: content/browser/loader/navigation_url_loader_network_service.cc |
| diff --git a/content/browser/loader/navigation_url_loader_network_service.cc b/content/browser/loader/navigation_url_loader_network_service.cc |
| index e82a39da4e07f37a329fdbcd5124fbf3330ce2d4..b7bce15a5ce6d1e4109303b1eb0c89d8e96f6c57 100644 |
| --- a/content/browser/loader/navigation_url_loader_network_service.cc |
| +++ b/content/browser/loader/navigation_url_loader_network_service.cc |
| @@ -15,6 +15,12 @@ |
| #include "content/browser/loader/navigation_resource_handler.h" |
| #include "content/browser/loader/navigation_resource_throttle.h" |
| #include "content/browser/loader/navigation_url_loader_delegate.h" |
| +#include "content/browser/resource_context_impl.h" |
| +#include "content/browser/service_worker/service_worker_context_wrapper.h" |
| +#include "content/browser/service_worker/service_worker_navigation_handle.h" |
| +#include "content/browser/service_worker/service_worker_navigation_handle_core.h" |
| +#include "content/browser/service_worker/service_worker_request_handler.h" |
| +#include "content/browser/web_contents/web_contents_impl.h" |
| #include "content/browser/webui/web_ui_url_loader_factory.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/global_request_id.h" |
| @@ -37,21 +43,63 @@ namespace { |
| static base::LazyInstance<mojom::URLLoaderFactoryPtr>::Leaky |
| g_url_loader_factory = LAZY_INSTANCE_INITIALIZER; |
| -// This function is called on the IO thread for POST/PUT requests for |
| -// attaching blob information to the request body. |
| -void HandleRequestsWithBody( |
| - std::unique_ptr<ResourceRequest> request, |
| +using CompleteNavigationStart = |
|
kinuko
2017/05/09 14:45:25
nit: CompleteNavigationStartCallback
(preferring
scottmg
2017/05/09 22:14:10
Done.
|
| + base::Callback<void(mojom::URLLoaderFactoryPtrInfo, |
| + std::unique_ptr<ResourceRequest>)>; |
| + |
| +void NavigationStartupWorkOnIO( |
|
kinuko
2017/05/09 14:45:25
nit: maybe... PrepareNavigationStartOnIOThread
scottmg
2017/05/09 22:14:10
Done.
|
| + std::unique_ptr<ResourceRequest> resource_request, |
| ResourceContext* resource_context, |
| - base::WeakPtr<NavigationURLLoaderNetworkService> url_loader) { |
| + ServiceWorkerNavigationHandleCore* service_worker_navigation_handle_core, |
| + bool skip_service_worker, |
| + ResourceType resource_type, |
| + RequestContextType request_context_type, |
| + RequestContextFrameType frame_type, |
| + bool is_parent_frame_secure, |
| + scoped_refptr<ResourceRequestBodyImpl> body, |
| + const base::Callback<WebContents*(void)>& web_contents_getter, |
| + CompleteNavigationStart complete_request) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - DCHECK(request->request_body.get()); |
| + DCHECK(service_worker_navigation_handle_core || |
| + resource_request->request_body.get()); |
| + |
| + mojom::URLLoaderFactoryPtrInfo url_loader_factory_ptr_info; |
| + if (service_worker_navigation_handle_core) { |
| + storage::BlobStorageContext* blob_storage_context = GetBlobStorageContext( |
| + GetChromeBlobStorageContextForResourceContext(resource_context)); |
| + url_loader_factory_ptr_info = |
| + ServiceWorkerRequestHandler::InitializeForNavigationNetworkService( |
| + *resource_request, resource_context, |
| + service_worker_navigation_handle_core, blob_storage_context, |
| + skip_service_worker, resource_type, request_context_type, |
| + frame_type, is_parent_frame_secure, body, web_contents_getter, |
| + // This is a fallback callback if the service worker handler decides |
| + // later that the request should fallback to the network. |
| + base::Bind( |
| + complete_request, |
| + base::Passed(std::move(mojom::URLLoaderFactoryPtrInfo())))); |
|
kinuko
2017/05/09 14:45:25
I think it'd be probably nicer to have some code s
scottmg
2017/05/09 22:14:10
Yes, we'll need something like that in the full ap
|
| + } |
| + |
| + if (resource_request->request_body) { |
| + AttachRequestBodyBlobDataHandles(resource_request->request_body.get(), |
| + resource_context); |
| + } |
| - AttachRequestBodyBlobDataHandles(request->request_body.get(), |
| - resource_context); |
| BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, |
| - base::Bind(&NavigationURLLoaderNetworkService::StartURLRequest, |
| - url_loader, base::Passed(&request))); |
| + base::Bind(complete_request, |
| + base::Passed(std::move(url_loader_factory_ptr_info)), |
| + base::Passed(std::move(resource_request)))); |
| +} |
| + |
| +WebContents* GetWebContentsFromFrameTreeNodeID(int frame_tree_node_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + FrameTreeNode* frame_tree_node = |
| + FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| + if (!frame_tree_node) |
| + return nullptr; |
| + |
| + return WebContentsImpl::FromFrameTreeNode(frame_tree_node); |
| } |
| } // namespace |
| @@ -61,7 +109,7 @@ NavigationURLLoaderNetworkService::NavigationURLLoaderNetworkService( |
| StoragePartition* storage_partition, |
| std::unique_ptr<NavigationRequestInfo> request_info, |
| std::unique_ptr<NavigationUIData> navigation_ui_data, |
| - ServiceWorkerNavigationHandle* service_worker_handle, |
| + ServiceWorkerNavigationHandle* service_worker_navigation_handle, |
| AppCacheNavigationHandle* appcache_handle, |
| NavigationURLLoaderDelegate* delegate) |
| : delegate_(delegate), |
| @@ -104,19 +152,41 @@ NavigationURLLoaderNetworkService::NavigationURLLoaderNetworkService( |
| new_request->load_flags = load_flags; |
| new_request->request_body = request_info_->common_params.post_data.get(); |
| - if (new_request->request_body.get()) { |
| - // The request body may need blob handles to be added to it. This |
| - // functionality has to be invoked on the IO thread. |
| + |
| + ResourceType resource_type = request_info_->is_main_frame |
| + ? RESOURCE_TYPE_MAIN_FRAME |
| + : RESOURCE_TYPE_SUB_FRAME; |
| + RequestContextFrameType frame_type = |
| + request_info_->is_main_frame ? REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL |
| + : REQUEST_CONTEXT_FRAME_TYPE_NESTED; |
| + |
| + // Determine where the request will be serviced. If there's work that needs to |
| + // be done on the IO thread due to Service Worker or POST body, then post to |
| + // the IO thread to have it do that work, and then continue on to |
| + // StartURLRequest. Otherwise, handle the request directly. |
| + const bool need_to_work_on_io = |
| + service_worker_navigation_handle || new_request->request_body; |
| + if (need_to_work_on_io) { |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, |
| - base::Bind(&HandleRequestsWithBody, |
| - base::Passed(&new_request), |
| - resource_context, |
| - weak_factory_.GetWeakPtr())); |
| + base::Bind( |
| + &NavigationStartupWorkOnIO, base::Passed(std::move(new_request)), |
| + resource_context, |
| + service_worker_navigation_handle |
| + ? service_worker_navigation_handle->core() |
| + : nullptr, |
| + request_info_->begin_params.skip_service_worker, resource_type, |
| + request_info_->begin_params.request_context_type, frame_type, |
| + request_info_->are_ancestors_secure, |
| + request_info_->common_params.post_data, |
| + base::Bind(&GetWebContentsFromFrameTreeNodeID, |
| + request_info_->frame_tree_node_id), |
| + base::Bind(&NavigationURLLoaderNetworkService::StartURLRequest, |
| + weak_factory_.GetWeakPtr()))); |
| return; |
| } |
| - StartURLRequest(std::move(new_request)); |
| + StartURLRequest(mojom::URLLoaderFactoryPtrInfo(), std::move(new_request)); |
| } |
| NavigationURLLoaderNetworkService::~NavigationURLLoaderNetworkService() {} |
| @@ -199,9 +269,13 @@ void NavigationURLLoaderNetworkService::OnComplete( |
| } |
| void NavigationURLLoaderNetworkService::StartURLRequest( |
| + mojom::URLLoaderFactoryPtrInfo url_loader_factory_info, |
| std::unique_ptr<ResourceRequest> request) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + // Bind the URLClient implementation to this object to pass to the URLLoader. |
| + if (binding_.is_bound()) |
| + binding_.Unbind(); |
| mojom::URLLoaderClientPtr url_loader_client_ptr_to_pass; |
| binding_.Bind(&url_loader_client_ptr_to_pass); |
| @@ -221,8 +295,16 @@ void NavigationURLLoaderNetworkService::StartURLRequest( |
| factory = factory_ptr.get(); |
| } |
| - if (!factory) |
| - factory = GetURLLoaderFactory(); |
| + if (!factory) { |
| + // If a URLLoaderFactory was provided, then we use that one, otherwise |
| + // fall back to connecting directly to the network service. |
| + if (url_loader_factory_info.is_valid()) { |
| + url_loader_factory_.Bind(std::move(url_loader_factory_info)); |
| + factory = url_loader_factory_.get(); |
| + } else { |
| + factory = GetURLLoaderFactory(); |
| + } |
| + } |
| factory->CreateLoaderAndStart(mojo::MakeRequest(&url_loader_associated_ptr_), |
| 0 /* routing_id? */, 0 /* request_id? */, |