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 be7fcf142da85e587b9c028dafeb1e7041470926..d405803a8ef5ac3c4ee4197f659f2a934d4e316b 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/url_data_manager_backend.h" |
#include "content/browser/webui/web_ui_url_loader_factory.h" |
#include "content/public/browser/browser_thread.h" |
@@ -38,21 +44,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 CompleteNavigationStartCallback = |
+ base::Callback<void(mojom::URLLoaderFactoryPtrInfo, |
+ std::unique_ptr<ResourceRequest>)>; |
+ |
+void PrepareNavigationStartOnIOThread( |
+ 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, |
+ CompleteNavigationStartCallback 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())))); |
+ } |
+ |
+ 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 |
@@ -62,7 +110,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), |
@@ -105,19 +153,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( |
+ &PrepareNavigationStartOnIOThread, |
+ 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() {} |
@@ -200,9 +270,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); |
@@ -224,8 +298,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? */, |