1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_request_handler.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_context_wrapper.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_url_request_job.h"
14 #include "content/browser/service_worker/service_worker_utils.h"
15 #include "content/common/resource_request_body.h"
16 #include "content/common/service_worker/service_worker_types.h"
17 #include "content/public/browser/resource_context.h"
18 #include "content/public/common/origin_util.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_interceptor.h"
22 #include "storage/browser/blob/blob_storage_context.h"
28 int kUserDataKey
; // Key value is not important.
30 class ServiceWorkerRequestInterceptor
31 : public net::URLRequestInterceptor
{
33 explicit ServiceWorkerRequestInterceptor(ResourceContext
* resource_context
)
34 : resource_context_(resource_context
) {}
35 ~ServiceWorkerRequestInterceptor() override
{}
36 net::URLRequestJob
* MaybeInterceptRequest(
37 net::URLRequest
* request
,
38 net::NetworkDelegate
* network_delegate
) const override
{
39 ServiceWorkerRequestHandler
* handler
=
40 ServiceWorkerRequestHandler::GetHandler(request
);
43 return handler
->MaybeCreateJob(
44 request
, network_delegate
, resource_context_
);
48 ResourceContext
* resource_context_
;
49 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor
);
54 void ServiceWorkerRequestHandler::InitializeHandler(
55 net::URLRequest
* request
,
56 ServiceWorkerContextWrapper
* context_wrapper
,
57 storage::BlobStorageContext
* blob_storage_context
,
60 bool skip_service_worker
,
61 FetchRequestMode request_mode
,
62 FetchCredentialsMode credentials_mode
,
63 ResourceType resource_type
,
64 RequestContextType request_context_type
,
65 RequestContextFrameType frame_type
,
66 scoped_refptr
<ResourceRequestBody
> body
) {
67 if (!OriginCanAccessServiceWorkers(request
->url()))
70 if (!context_wrapper
|| !context_wrapper
->context() ||
71 provider_id
== kInvalidServiceWorkerProviderId
) {
75 ServiceWorkerProviderHost
* provider_host
=
76 context_wrapper
->context()->GetProviderHost(process_id
, provider_id
);
77 if (!provider_host
|| !provider_host
->IsContextAlive())
80 if (skip_service_worker
) {
81 if (ServiceWorkerUtils::IsMainResourceType(resource_type
)) {
82 provider_host
->SetDocumentUrl(net::SimplifyUrlForRequest(request
->url()));
83 provider_host
->SetTopmostFrameUrl(request
->first_party_for_cookies());
84 // A page load with skip_service_worker should be triggered by
85 // shift-reload, so retain all live matching registrations.
86 provider_host
->AddAllMatchingRegistrations();
91 scoped_ptr
<ServiceWorkerRequestHandler
> handler(
92 provider_host
->CreateRequestHandler(request_mode
,
97 blob_storage_context
->AsWeakPtr(),
102 request
->SetUserData(&kUserDataKey
, handler
.release());
105 ServiceWorkerRequestHandler
* ServiceWorkerRequestHandler::GetHandler(
106 net::URLRequest
* request
) {
107 return static_cast<ServiceWorkerRequestHandler
*>(
108 request
->GetUserData(&kUserDataKey
));
111 scoped_ptr
<net::URLRequestInterceptor
>
112 ServiceWorkerRequestHandler::CreateInterceptor(
113 ResourceContext
* resource_context
) {
114 return scoped_ptr
<net::URLRequestInterceptor
>(
115 new ServiceWorkerRequestInterceptor(resource_context
));
118 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker(
119 net::URLRequest
* request
) {
120 ServiceWorkerRequestHandler
* handler
= GetHandler(request
);
121 if (!handler
|| !handler
->provider_host_
)
123 return handler
->provider_host_
->associated_registration() ||
124 handler
->provider_host_
->running_hosted_version();
127 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
128 int old_process_id
) {
129 if (!provider_host_
|| !context_
)
131 old_process_id_
= old_process_id
;
132 old_provider_id_
= provider_host_
->provider_id();
133 host_for_cross_site_transfer_
=
134 context_
->TransferProviderHostOut(old_process_id
,
135 provider_host_
->provider_id());
136 DCHECK_EQ(provider_host_
.get(), host_for_cross_site_transfer_
.get());
139 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer(
140 int new_process_id
, int new_provider_id
) {
141 if (!host_for_cross_site_transfer_
.get() || !context_
)
143 DCHECK_EQ(provider_host_
.get(), host_for_cross_site_transfer_
.get());
144 context_
->TransferProviderHostIn(
147 host_for_cross_site_transfer_
.Pass());
148 DCHECK_EQ(provider_host_
->provider_id(), new_provider_id
);
151 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess(
152 int old_process_id
) {
153 if (!host_for_cross_site_transfer_
.get() || !context_
||
154 old_process_id_
!= old_process_id
) {
157 CompleteCrossSiteTransfer(old_process_id_
, old_provider_id_
);
160 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
163 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
164 base::WeakPtr
<ServiceWorkerContextCore
> context
,
165 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
166 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
167 ResourceType resource_type
)
169 provider_host_(provider_host
),
170 blob_storage_context_(blob_storage_context
),
171 resource_type_(resource_type
),
173 old_provider_id_(kInvalidServiceWorkerProviderId
) {
176 } // namespace content