| Index: content/browser/service_worker/service_worker_url_loader_factory_creation.cc
|
| diff --git a/content/browser/service_worker/service_worker_url_loader_factory_creation.cc b/content/browser/service_worker/service_worker_url_loader_factory_creation.cc
|
| index a61921b31829fbad2cc6871051034ea38e40da14..1fa2884dbfd8a47ec301dbbaa6ebc2e36f0e2257 100644
|
| --- a/content/browser/service_worker/service_worker_url_loader_factory_creation.cc
|
| +++ b/content/browser/service_worker/service_worker_url_loader_factory_creation.cc
|
| @@ -26,10 +26,97 @@ std::unique_ptr<service_manager::Connector> GetConnector() {
|
| return ServiceManagerConnection::GetForProcess()->GetConnector()->Clone();
|
| }
|
|
|
| -// A URLLoader implementation created by ControlleeHandlerFactory, used to serve
|
| -// requests to the document that's using a service worker. Currently simply
|
| -// proxies to the network service, rather than issuing the fetch request to the
|
| -// context provider host.
|
| +// A URLLoader implementation created by HandlerFactory, used to serve requests
|
| +// to the renderer that's being the service worker. Currently simply proxies to
|
| +// the network service.
|
| +class ContextURLLoaderImpl : public mojom::URLLoader,
|
| + public mojom::URLLoaderClient {
|
| + public:
|
| + ContextURLLoaderImpl(mojom::URLLoaderAssociatedRequest loader,
|
| + uint32_t options,
|
| + const ResourceRequest& request,
|
| + mojom::URLLoaderClientPtr client,
|
| + ServiceWorkerProviderHost* provider_host)
|
| + : binding_(this),
|
| + client_(std::move(client)),
|
| + version_(provider_host->running_hosted_version()),
|
| + weak_ptr_factory_(this) {
|
| + // TODO(scottmg): Stuff from
|
| + // ServiceWorkerContextRequestHandler::MaybeCreateJobImpl.
|
| +
|
| + // TODO(scottmg): As an intermediate step, all requests are forwarded to the
|
| + // network service. Acquire a connector that can be used on the IO thread by
|
| + // posting to the UI thread here.
|
| + BrowserThread::PostTaskAndReplyWithResult(
|
| + BrowserThread::UI, FROM_HERE, base::Bind(&GetConnector),
|
| + base::Bind(&ContextURLLoaderImpl::ContinueAfterGettingConnector,
|
| + weak_ptr_factory_.GetWeakPtr(), request));
|
| + }
|
| +
|
| + // mojom::URLLoader implementation:
|
| + void FollowRedirect() override {}
|
| + void SetPriority(net::RequestPriority priority,
|
| + int32_t intra_priority_value) override {}
|
| +
|
| + // mojom::URLLoaderClient implementation. This implementation is only required
|
| + // temporarily while this is a simple forwarder to the real network service,
|
| + // but can be removed.
|
| + void OnReceiveResponse(
|
| + const ResourceResponseHead& head,
|
| + const base::Optional<net::SSLInfo>& ssl_info,
|
| + mojom::DownloadedTempFilePtr downloaded_file) override {
|
| + client_->OnReceiveResponse(head, ssl_info, std::move(downloaded_file));
|
| + }
|
| + void OnReceiveRedirect(const net::RedirectInfo& redirect_info,
|
| + const ResourceResponseHead& head) override {}
|
| + void OnDataDownloaded(int64_t data_length, int64_t encoded_length) override {}
|
| + void OnUploadProgress(int64_t current_position,
|
| + int64_t total_size,
|
| + OnUploadProgressCallback callback) override {}
|
| + void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override {}
|
| + void OnTransferSizeUpdated(int32_t transfer_size_diff) override {}
|
| + void OnStartLoadingResponseBody(
|
| + mojo::ScopedDataPipeConsumerHandle body) override {
|
| + client_->OnStartLoadingResponseBody(std::move(body));
|
| + }
|
| + void OnComplete(
|
| + const ResourceRequestCompletionStatus& completion_status) override {
|
| + client_->OnComplete(completion_status);
|
| + }
|
| +
|
| + private:
|
| + void ContinueAfterGettingConnector(
|
| + const ResourceRequest& request,
|
| + std::unique_ptr<service_manager::Connector> connector) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| + connector->BindInterface(mojom::kNetworkServiceName,
|
| + &network_url_loader_factory_);
|
| +
|
| + mojom::URLLoaderClientPtr url_loader_client_ptr_to_pass;
|
| + binding_.Bind(&url_loader_client_ptr_to_pass);
|
| +
|
| + network_url_loader_factory_->CreateLoaderAndStart(
|
| + mojo::MakeRequest(&url_loader_associated_ptr_), 0 /* routing_id? */,
|
| + 0 /* request_id? */, mojom::kURLLoadOptionSendSSLInfo, request,
|
| + std::move(url_loader_client_ptr_to_pass));
|
| + }
|
| +
|
| + mojom::URLLoaderFactoryPtr network_url_loader_factory_;
|
| + mojom::URLLoaderAssociatedPtr url_loader_associated_ptr_;
|
| + mojo::Binding<mojom::URLLoaderClient> binding_;
|
| + mojom::URLLoaderClientPtr client_;
|
| +
|
| + scoped_refptr<ServiceWorkerVersion> version_;
|
| +
|
| + base::WeakPtrFactory<ContextURLLoaderImpl> weak_ptr_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ContextURLLoaderImpl);
|
| +};
|
| +
|
| +// A URLLoader implementation created by HandlerFactory, used to serve requests
|
| +// to the document that's using a service worker. Currently simply proxies to
|
| +// the network service, rather than issuing the fetch request to the context
|
| +// provider host.
|
| class ControlleeURLLoaderImpl : public mojom::URLLoader,
|
| public mojom::URLLoaderClient {
|
| public:
|
| @@ -113,19 +200,23 @@ class ControlleeURLLoaderImpl : public mojom::URLLoader,
|
| mojo::Binding<mojom::URLLoaderClient> binding_;
|
| mojom::URLLoaderClientPtr client_;
|
|
|
| - std::unique_ptr<base::WeakPtrFactory<ControlleeURLLoaderImpl>>
|
| - weak_ptr_factory_ui_;
|
| base::WeakPtrFactory<ControlleeURLLoaderImpl> weak_ptr_factory_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(ControlleeURLLoaderImpl);
|
| };
|
|
|
| +enum class HandlerType {
|
| + kContext,
|
| + kControllee,
|
| +};
|
| +
|
| // URLLoaderFactory implementation for the document side.
|
| -class ControlleeHandlerFactory : public mojom::URLLoaderFactory {
|
| +class HandlerFactory : public mojom::URLLoaderFactory {
|
| public:
|
| - ControlleeHandlerFactory(ServiceWorkerProviderHost* provider_host)
|
| - : provider_host_(provider_host) {}
|
| - ~ControlleeHandlerFactory() override {}
|
| + HandlerFactory(ServiceWorkerProviderHost* provider_host,
|
| + HandlerType handler_type)
|
| + : provider_host_(provider_host), handler_type_(handler_type) {}
|
| + ~HandlerFactory() override {}
|
|
|
| void CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest loader,
|
| int32_t routing_id,
|
| @@ -133,8 +224,18 @@ class ControlleeHandlerFactory : public mojom::URLLoaderFactory {
|
| uint32_t options,
|
| const ResourceRequest& request,
|
| mojom::URLLoaderClientPtr client) override {
|
| - new ControlleeURLLoaderImpl(std::move(loader), options, request,
|
| - std::move(client), provider_host_);
|
| + switch (handler_type_) {
|
| + case HandlerType::kContext:
|
| + new ContextURLLoaderImpl(std::move(loader), options, request,
|
| + std::move(client), provider_host_);
|
| + break;
|
| + case HandlerType::kControllee:
|
| + new ControlleeURLLoaderImpl(std::move(loader), options, request,
|
| + std::move(client), provider_host_);
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| }
|
|
|
| void SyncLoad(int32_t routing_id,
|
| @@ -146,8 +247,9 @@ class ControlleeHandlerFactory : public mojom::URLLoaderFactory {
|
|
|
| private:
|
| ServiceWorkerProviderHost* provider_host_;
|
| + HandlerType handler_type_;
|
|
|
| - DISALLOW_COPY_AND_ASSIGN(ControlleeHandlerFactory);
|
| + DISALLOW_COPY_AND_ASSIGN(HandlerFactory);
|
| };
|
|
|
| } // namespace
|
| @@ -177,9 +279,9 @@ ServiceWorkerURLLoaderFactoryCreation::InitializeForNavigation(
|
| if (ServiceWorkerUtils::IsMainResourceType(creation_data.resource_type) ||
|
| provider_host->controlling_version()) {
|
| mojom::URLLoaderFactoryPtr controllee;
|
| - mojo::MakeStrongBinding(
|
| - base::MakeUnique<ControlleeHandlerFactory>(provider_host.get()),
|
| - mojo::MakeRequest(&controllee));
|
| + mojo::MakeStrongBinding(base::MakeUnique<HandlerFactory>(
|
| + provider_host.get(), HandlerType::kControllee),
|
| + mojo::MakeRequest(&controllee));
|
|
|
| // Transfer ownership to the ServiceWorkerNavigationHandleCore.
|
| // In the case of a successful navigation, the SWProviderHost will be
|
| @@ -204,9 +306,12 @@ ServiceWorkerURLLoaderFactoryCreation::InitializeForRenderer(
|
| if (!provider_host->IsContextAlive())
|
| return mojom::URLLoaderFactoryPtrInfo();
|
|
|
| + const bool is_context = provider_host->IsHostToRunningServiceWorker();
|
| mojom::URLLoaderFactoryPtr controllee;
|
| mojo::MakeStrongBinding(
|
| - base::MakeUnique<ControlleeHandlerFactory>(provider_host),
|
| + base::MakeUnique<HandlerFactory>(
|
| + provider_host,
|
| + is_context ? HandlerType::kContext : HandlerType::kControllee),
|
| mojo::MakeRequest(&controllee));
|
| return controllee.PassInterface();
|
| }
|
|
|