Commit 28610054 authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Chromium LUCI CQ

service worker: Simplify main resource "handler" & "interceptor" (2/3)

The "handler" can call the loader callback directly.

Bug: 1138155
Change-Id: I618991ca62f83d1336405de7f824fcf43c893ac0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2596592
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarAsami Doi <asamidoi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840521}
parent e1dbf97d
...@@ -116,14 +116,14 @@ void ServiceWorkerControlleeRequestHandler::MaybeScheduleUpdate() { ...@@ -116,14 +116,14 @@ void ServiceWorkerControlleeRequestHandler::MaybeScheduleUpdate() {
void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader( void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader(
const network::ResourceRequest& tentative_resource_request, const network::ResourceRequest& tentative_resource_request,
BrowserContext* browser_context, BrowserContext* browser_context,
ServiceWorkerLoaderCallback callback, NavigationLoaderInterceptor::LoaderCallback loader_callback,
NavigationLoaderInterceptor::FallbackCallback fallback_callback) { NavigationLoaderInterceptor::FallbackCallback fallback_callback) {
// InitializeContainerHost() will update the host. This is important to do // InitializeContainerHost() will update the host. This is important to do
// before falling back to network below, so service worker APIs still work // before falling back to network below, so service worker APIs still work
// even if the service worker is bypassed for request interception. // even if the service worker is bypassed for request interception.
if (!InitializeContainerHost(tentative_resource_request)) { if (!InitializeContainerHost(tentative_resource_request)) {
// We can't do anything other than to fall back to network. // We can't do anything other than to fall back to network.
std::move(callback).Run({}); std::move(loader_callback).Run({});
return; return;
} }
...@@ -131,7 +131,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader( ...@@ -131,7 +131,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader(
// request interception, or if the context is gone so we have to bypass // request interception, or if the context is gone so we have to bypass
// anyway. // anyway.
if (skip_service_worker_ || !context_) { if (skip_service_worker_ || !context_) {
std::move(callback).Run({}); std::move(loader_callback).Run({});
return; return;
} }
...@@ -145,7 +145,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader( ...@@ -145,7 +145,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader(
// headers between now and when the request handler passed to // headers between now and when the request handler passed to
// |loader_callback_| is invoked. // |loader_callback_| is invoked.
if (ShouldFallbackToLoadOfflinePage(tentative_resource_request.headers)) { if (ShouldFallbackToLoadOfflinePage(tentative_resource_request.headers)) {
std::move(callback).Run({}); std::move(loader_callback).Run({});
return; return;
} }
#endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES)
...@@ -160,7 +160,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader( ...@@ -160,7 +160,7 @@ void ServiceWorkerControlleeRequestHandler::MaybeCreateLoader(
TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "URL", TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "URL",
tentative_resource_request.url.spec()); tentative_resource_request.url.spec());
loader_callback_ = std::move(callback); loader_callback_ = std::move(loader_callback);
fallback_callback_ = std::move(fallback_callback); fallback_callback_ = std::move(fallback_callback);
registration_lookup_start_time_ = base::TimeTicks::Now(); registration_lookup_start_time_ = base::TimeTicks::Now();
browser_context_ = browser_context; browser_context_ = browser_context;
...@@ -426,8 +426,9 @@ void ServiceWorkerControlleeRequestHandler::ContinueWithActivatedVersion( ...@@ -426,8 +426,9 @@ void ServiceWorkerControlleeRequestHandler::ContinueWithActivatedVersion(
TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "Info", TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "Info",
"Forwarded to the ServiceWorker"); "Forwarded to the ServiceWorker");
std::move(loader_callback_) std::move(loader_callback_)
.Run(base::BindOnce(&ServiceWorkerMainResourceLoader::StartRequest, .Run(base::MakeRefCounted<SingleRequestURLLoaderFactory>(
loader_wrapper_->get()->AsWeakPtr())); base::BindOnce(&ServiceWorkerMainResourceLoader::StartRequest,
loader_wrapper_->get()->AsWeakPtr())));
} }
void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration(
......
...@@ -52,12 +52,10 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler final { ...@@ -52,12 +52,10 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler final {
// This could get called multiple times during the lifetime in redirect // This could get called multiple times during the lifetime in redirect
// cases. (In fallback-to-network cases we basically forward the request // cases. (In fallback-to-network cases we basically forward the request
// to the request to the next request handler) // to the request to the next request handler)
using ServiceWorkerLoaderCallback =
base::OnceCallback<void(SingleRequestURLLoaderFactory::RequestHandler)>;
void MaybeCreateLoader( void MaybeCreateLoader(
const network::ResourceRequest& tentative_request, const network::ResourceRequest& tentative_request,
BrowserContext* browser_context, BrowserContext* browser_context,
ServiceWorkerLoaderCallback callback, NavigationLoaderInterceptor::LoaderCallback loader_callback,
NavigationLoaderInterceptor::FallbackCallback fallback_callback); NavigationLoaderInterceptor::FallbackCallback fallback_callback);
// Does all initialization of |container_host_| for a request. // Does all initialization of |container_host_| for a request.
...@@ -113,7 +111,7 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler final { ...@@ -113,7 +111,7 @@ class CONTENT_EXPORT ServiceWorkerControlleeRequestHandler final {
bool force_update_started_; bool force_update_started_;
base::TimeTicks registration_lookup_start_time_; base::TimeTicks registration_lookup_start_time_;
ServiceWorkerLoaderCallback loader_callback_; NavigationLoaderInterceptor::LoaderCallback loader_callback_;
NavigationLoaderInterceptor::FallbackCallback fallback_callback_; NavigationLoaderInterceptor::FallbackCallback fallback_callback_;
ServiceWorkerAccessedCallback service_worker_accessed_callback_; ServiceWorkerAccessedCallback service_worker_accessed_callback_;
......
...@@ -124,8 +124,7 @@ void ServiceWorkerMainResourceLoaderInterceptor::MaybeCreateLoader( ...@@ -124,8 +124,7 @@ void ServiceWorkerMainResourceLoaderInterceptor::MaybeCreateLoader(
ServiceWorkerContextCore* context_core = ServiceWorkerContextCore* context_core =
handle_->context_wrapper()->context(); handle_->context_wrapper()->context();
if (!context_core || !browser_context) { if (!context_core || !browser_context) {
LoaderCallbackWrapper(std::move(loader_callback), std::move(loader_callback).Run(/*handler=*/{});
/*handler=*/{});
return; return;
} }
...@@ -198,19 +197,15 @@ void ServiceWorkerMainResourceLoaderInterceptor::MaybeCreateLoader( ...@@ -198,19 +197,15 @@ void ServiceWorkerMainResourceLoaderInterceptor::MaybeCreateLoader(
// ControllerServiceWorkerInfoPtr and ServiceWorkerObjectHost from the // ControllerServiceWorkerInfoPtr and ServiceWorkerObjectHost from the
// subresource loader params which is created by the interceptor. // subresource loader params which is created by the interceptor.
if (inherit_container_host_only) { if (inherit_container_host_only) {
LoaderCallbackWrapper(std::move(loader_callback), std::move(loader_callback).Run(/*handler=*/{});
/*handler=*/{});
return; return;
} }
} }
// Start the inner interceptor. We continue in // Start the inner interceptor. It will invoke the loader callback
// LoaderCallbackWrapper() or the fallback callback is called. // or fallback callback.
handle_->interceptor()->MaybeCreateLoader( handle_->interceptor()->MaybeCreateLoader(
tentative_resource_request, browser_context, tentative_resource_request, browser_context, std::move(loader_callback),
base::BindOnce(
&ServiceWorkerMainResourceLoaderInterceptor::LoaderCallbackWrapper,
GetWeakPtr(), std::move(loader_callback)),
std::move(fallback_callback)); std::move(fallback_callback));
} }
...@@ -261,28 +256,6 @@ ServiceWorkerMainResourceLoaderInterceptor:: ...@@ -261,28 +256,6 @@ ServiceWorkerMainResourceLoaderInterceptor::
return base::Optional<SubresourceLoaderParams>(std::move(params)); return base::Optional<SubresourceLoaderParams>(std::move(params));
} }
void ServiceWorkerMainResourceLoaderInterceptor::LoaderCallbackWrapper(
LoaderCallback loader_callback,
SingleRequestURLLoaderFactory::RequestHandler handler) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!handler) {
std::move(loader_callback).Run({});
return;
}
// The inner interceptor wants to handle the request.
std::move(loader_callback)
.Run(base::MakeRefCounted<SingleRequestURLLoaderFactory>(
std::move(handler)));
}
base::WeakPtr<ServiceWorkerMainResourceLoaderInterceptor>
ServiceWorkerMainResourceLoaderInterceptor::GetWeakPtr() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return weak_factory_.GetWeakPtr();
}
ServiceWorkerMainResourceLoaderInterceptor:: ServiceWorkerMainResourceLoaderInterceptor::
ServiceWorkerMainResourceLoaderInterceptor( ServiceWorkerMainResourceLoaderInterceptor(
base::WeakPtr<ServiceWorkerMainResourceHandle> handle, base::WeakPtr<ServiceWorkerMainResourceHandle> handle,
......
...@@ -72,12 +72,6 @@ class CONTENT_EXPORT ServiceWorkerMainResourceLoaderInterceptor final ...@@ -72,12 +72,6 @@ class CONTENT_EXPORT ServiceWorkerMainResourceLoaderInterceptor final
base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams() base::Optional<SubresourceLoaderParams> MaybeCreateSubresourceLoaderParams()
override; override;
void LoaderCallbackWrapper(
LoaderCallback loader_callback,
SingleRequestURLLoaderFactory::RequestHandler handler);
base::WeakPtr<ServiceWorkerMainResourceLoaderInterceptor> GetWeakPtr();
private: private:
friend class ServiceWorkerMainResourceLoaderInterceptorTest; friend class ServiceWorkerMainResourceLoaderInterceptorTest;
...@@ -132,9 +126,6 @@ class CONTENT_EXPORT ServiceWorkerMainResourceLoaderInterceptor final ...@@ -132,9 +126,6 @@ class CONTENT_EXPORT ServiceWorkerMainResourceLoaderInterceptor final
const int process_id_; const int process_id_;
const base::Optional<DedicatedOrSharedWorkerToken> worker_token_; const base::Optional<DedicatedOrSharedWorkerToken> worker_token_;
base::WeakPtrFactory<ServiceWorkerMainResourceLoaderInterceptor>
weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerMainResourceLoaderInterceptor); DISALLOW_COPY_AND_ASSIGN(ServiceWorkerMainResourceLoaderInterceptor);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment