Commit 2c23b14c authored by Ting Shao's avatar Ting Shao Committed by Commit Bot

ServiceWorker: Add URLLoaderFactoryType::kServiceWorkerScript

Service workers load scripts and sub resources. Currently There is only
one URLLoaderFactory type (kServiceWorkerSubResources) for all cases.
However, When ServiceWorkerImportedScriptUpdateCheck is enabled, only
scripts are loaded in browser process during the check. It's necessary
to separate the loader factory into two types.

So add ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerScript.

Change-Id: I41564fba6b4702257324d77c3d9125be747b2ee4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1774051Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Ting Shao <ting.shao@intel.com>
Cr-Commit-Position: refs/heads/master@{#691528}
parent 63030b69
......@@ -206,9 +206,11 @@ void ServiceWorkerDevToolsAgentHost::UpdateLoaderFactories(
}
const url::Origin origin = url::Origin::Create(url_);
auto script_bundle = EmbeddedWorkerInstance::CreateFactoryBundleOnUI(
rph, worker_route_id_, origin);
rph, worker_route_id_, origin,
ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerScript);
auto subresource_bundle = EmbeddedWorkerInstance::CreateFactoryBundleOnUI(
rph, worker_route_id_, origin);
rph, worker_route_id_, origin,
ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerSubResource);
base::PostTaskAndReply(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&UpdateLoaderFactoriesOnIO, context_weak_, version_id_,
......
......@@ -33,7 +33,6 @@
#include "content/common/url_schemes.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/child_process_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
......@@ -237,8 +236,9 @@ void SetupOnUIThread(
// the 'installed' state.
if (!params->is_installed) {
factory_bundle_for_new_scripts =
EmbeddedWorkerInstance::CreateFactoryBundleOnUI(rph, routing_id,
origin);
EmbeddedWorkerInstance::CreateFactoryBundleOnUI(
rph, routing_id, origin,
ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerScript);
}
// The bundle for the renderer is passed to the service worker, and
......@@ -246,8 +246,9 @@ void SetupOnUIThread(
// It's OK to not support reconnection to the network service because the
// service worker terminates itself when the connection breaks, so a new
// instance can be started.
factory_bundle_for_renderer =
EmbeddedWorkerInstance::CreateFactoryBundleOnUI(rph, routing_id, origin);
factory_bundle_for_renderer = EmbeddedWorkerInstance::CreateFactoryBundleOnUI(
rph, routing_id, origin,
ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerSubResource);
// TODO(crbug.com/862854): Support changes to
// blink::mojom::RendererPreferences while the worker is running.
......@@ -1036,9 +1037,11 @@ base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() {
//
// The network factory does not support reconnection to the network service.
std::unique_ptr<blink::URLLoaderFactoryBundleInfo>
EmbeddedWorkerInstance::CreateFactoryBundleOnUI(RenderProcessHost* rph,
int routing_id,
const url::Origin& origin) {
EmbeddedWorkerInstance::CreateFactoryBundleOnUI(
RenderProcessHost* rph,
int routing_id,
const url::Origin& origin,
ContentBrowserClient::URLLoaderFactoryType factory_type) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto factory_bundle = std::make_unique<blink::URLLoaderFactoryBundleInfo>();
mojo::PendingReceiver<network::mojom::URLLoaderFactory>
......@@ -1051,8 +1054,7 @@ EmbeddedWorkerInstance::CreateFactoryBundleOnUI(RenderProcessHost* rph,
// See if the default factory needs to be tweaked by the embedder.
GetContentClient()->browser()->WillCreateURLLoaderFactory(
rph->GetBrowserContext(), nullptr /* frame_host */, rph->GetID(),
ContentBrowserClient::URLLoaderFactoryType::kServiceWorkerSubResource,
origin, &default_factory_receiver, &default_header_client,
factory_type, origin, &default_factory_receiver, &default_header_client,
&bypass_redirect_checks);
devtools_instrumentation::WillCreateURLLoaderFactoryForServiceWorker(
rph, routing_id, &default_factory_receiver);
......
......@@ -24,6 +24,7 @@
#include "content/browser/service_worker/embedded_worker_status.h"
#include "content/browser/service_worker/service_worker_metrics.h"
#include "content/common/content_export.h"
#include "content/public/browser/content_browser_client.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
......@@ -237,9 +238,11 @@ class CONTENT_EXPORT EmbeddedWorkerInstance
// The below can only be called on the UI thread. The returned factory may be
// later supplied to UpdateLoaderFactories().
static std::unique_ptr<blink::URLLoaderFactoryBundleInfo>
CreateFactoryBundleOnUI(RenderProcessHost* rph,
int routing_id,
const url::Origin& origin);
CreateFactoryBundleOnUI(
RenderProcessHost* rph,
int routing_id,
const url::Origin& origin,
ContentBrowserClient::URLLoaderFactoryType factory_type);
private:
typedef base::ObserverList<Listener>::Unchecked ListenerList;
......
......@@ -1189,7 +1189,8 @@ class CONTENT_EXPORT ContentBrowserClient {
// Allows the embedder to intercept URLLoaderFactory interfaces used for
// navigation or being brokered on behalf of a renderer fetching subresources.
//
// |frame| is nullptr for kWorkerSubResource and kServiceWorkerSubResource.
// |frame| is nullptr for kWorkerSubResource, kServiceWorkerSubResource and
// kServiceWorkerScript.
// For kNavigation type, it's the RenderFrameHost the navigation might commit
// in. Else it's the initiating frame.
//
......@@ -1200,8 +1201,9 @@ class CONTENT_EXPORT ContentBrowserClient {
// requests that will use the URLLoaderFactory. It's set when this factory is
// created a) for a renderer to use to fetch subresources
// (kDocumentSubResource, kWorkerSubResource, kServiceWorkerSubResource), or
// b) for the browser to use to fetch a worker (kWorkerMainResource). An
// opaque origin is passed currently for navigation (kNavigation) and
// b) for the browser to use to fetch a worker (kWorkerMainResource) or
// service worker scripts (kServiceWorkerScript).
// An opaque origin is passed currently for navigation (kNavigation) and
// download (kDownload) factories even though requests from these factories
// can have a valid |network::ResourceRequest::request_initiator|.
// Note: For the kDocumentSubResource case, the |request_initiator| may be
......@@ -1235,7 +1237,12 @@ class CONTENT_EXPORT ContentBrowserClient {
kWorkerMainResource,
kWorkerSubResource,
// For regular fetches from a service worker (e.g., fetch(), XHR).
kServiceWorkerSubResource,
// For fetching a service worker main script or subresource scripts
// (e.g., importScripts()).
kServiceWorkerScript,
};
virtual bool WillCreateURLLoaderFactory(
BrowserContext* browser_context,
......
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