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_controllee_request_handler.h"
7 #include "base/trace_event/trace_event.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_metrics.h"
10 #include "content/browser/service_worker/service_worker_provider_host.h"
11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_url_request_job.h"
13 #include "content/browser/service_worker/service_worker_utils.h"
14 #include "content/common/resource_request_body.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h"
18 #include "net/base/load_flags.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_request.h"
24 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler(
25 base::WeakPtr
<ServiceWorkerContextCore
> context
,
26 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
27 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
28 FetchRequestMode request_mode
,
29 FetchCredentialsMode credentials_mode
,
30 ResourceType resource_type
,
31 RequestContextType request_context_type
,
32 RequestContextFrameType frame_type
,
33 scoped_refptr
<ResourceRequestBody
> body
)
34 : ServiceWorkerRequestHandler(context
,
38 is_main_resource_load_(
39 ServiceWorkerUtils::IsMainResourceType(resource_type
)),
40 request_mode_(request_mode
),
41 credentials_mode_(credentials_mode
),
42 request_context_type_(request_context_type
),
43 frame_type_(frame_type
),
48 ServiceWorkerControlleeRequestHandler::
49 ~ServiceWorkerControlleeRequestHandler() {
50 // Navigation triggers an update to occur shortly after the page and
51 // its initial subresources load.
52 if (provider_host_
&& provider_host_
->active_version()) {
53 if (is_main_resource_load_
)
54 provider_host_
->active_version()->ScheduleUpdate();
56 provider_host_
->active_version()->DeferScheduledUpdate();
59 if (is_main_resource_load_
&& provider_host_
)
60 provider_host_
->SetAllowAssociation(true);
63 net::URLRequestJob
* ServiceWorkerControlleeRequestHandler::MaybeCreateJob(
64 net::URLRequest
* request
,
65 net::NetworkDelegate
* network_delegate
,
66 ResourceContext
* resource_context
) {
67 if (!context_
|| !provider_host_
) {
68 // We can't do anything other than to fall back to network.
73 // This may get called multiple times for original and redirect requests:
74 // A. original request case: job_ is null, no previous location info.
75 // B. redirect or restarted request case:
76 // a) job_ is non-null if the previous location was forwarded to SW.
77 // b) job_ is null if the previous location was fallback.
78 // c) job_ is non-null if additional restart was required to fall back.
80 // We've come here by restart, we already have original request and it
81 // tells we should fallback to network. (Case B-c)
82 if (job_
.get() && job_
->ShouldFallbackToNetwork()) {
87 // It's for original request (A) or redirect case (B-a or B-b).
88 DCHECK(!job_
.get() || job_
->ShouldForwardToServiceWorker());
90 job_
= new ServiceWorkerURLRequestJob(request
,
93 blob_storage_context_
,
97 request_context_type_
,
100 resource_context_
= resource_context
;
102 if (is_main_resource_load_
)
103 PrepareForMainResource(request
);
105 PrepareForSubResource();
107 if (job_
->ShouldFallbackToNetwork()) {
108 // If we know we can fallback to network at this point (in case
109 // the storage lookup returned immediately), just return NULL here to
110 // fallback to network.
118 void ServiceWorkerControlleeRequestHandler::GetExtraResponseInfo(
119 bool* was_fetched_via_service_worker
,
120 bool* was_fallback_required_by_service_worker
,
121 GURL
* original_url_via_service_worker
,
122 blink::WebServiceWorkerResponseType
* response_type_via_service_worker
,
123 base::TimeTicks
* fetch_start_time
,
124 base::TimeTicks
* fetch_ready_time
,
125 base::TimeTicks
* fetch_end_time
) const {
127 *was_fetched_via_service_worker
= false;
128 *was_fallback_required_by_service_worker
= false;
129 *original_url_via_service_worker
= GURL();
132 job_
->GetExtraResponseInfo(was_fetched_via_service_worker
,
133 was_fallback_required_by_service_worker
,
134 original_url_via_service_worker
,
135 response_type_via_service_worker
,
141 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource(
142 const net::URLRequest
* request
) {
145 DCHECK(provider_host_
);
146 TRACE_EVENT_ASYNC_BEGIN1(
148 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
150 "URL", request
->url().spec());
151 // The corresponding provider_host may already have associated a registration
152 // in redirect case, unassociate it now.
153 provider_host_
->DisassociateRegistration();
155 // Also prevent a registrater job for establishing an association to a new
156 // registration while we're finding an existing registration.
157 provider_host_
->SetAllowAssociation(false);
159 stripped_url_
= net::SimplifyUrlForRequest(request
->url());
160 provider_host_
->SetDocumentUrl(stripped_url_
);
161 provider_host_
->SetTopmostFrameUrl(request
->first_party_for_cookies());
162 context_
->storage()->FindRegistrationForDocument(
163 stripped_url_
, base::Bind(&self::DidLookupRegistrationForMainResource
,
164 weak_factory_
.GetWeakPtr()));
168 ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource(
169 ServiceWorkerStatusCode status
,
170 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
173 provider_host_
->SetAllowAssociation(true);
174 if (status
!= SERVICE_WORKER_OK
|| !provider_host_
) {
175 job_
->FallbackToNetwork();
176 TRACE_EVENT_ASYNC_END1(
178 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
183 DCHECK(registration
.get());
185 if (!GetContentClient()->browser()->AllowServiceWorker(
186 registration
->pattern(),
187 provider_host_
->topmost_frame_url(),
188 resource_context_
)) {
189 job_
->FallbackToNetwork();
190 TRACE_EVENT_ASYNC_END2(
192 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
195 "Info", "ServiceWorker is blocked");
199 // Initiate activation of a waiting version.
200 // Usually a register job initiates activation but that
201 // doesn't happen if the browser exits prior to activation
202 // having occurred. This check handles that case.
203 if (registration
->waiting_version())
204 registration
->ActivateWaitingVersionWhenReady();
206 scoped_refptr
<ServiceWorkerVersion
> active_version
=
207 registration
->active_version();
209 // Wait until it's activated before firing fetch events.
210 if (active_version
.get() &&
211 active_version
->status() == ServiceWorkerVersion::ACTIVATING
) {
212 provider_host_
->SetAllowAssociation(false);
213 registration
->active_version()->RegisterStatusChangeCallback(
214 base::Bind(&self::OnVersionStatusChanged
,
215 weak_factory_
.GetWeakPtr(),
218 TRACE_EVENT_ASYNC_END2(
220 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
223 "Info", "Wait until finished SW activation");
227 if (!active_version
.get() ||
228 active_version
->status() != ServiceWorkerVersion::ACTIVATED
) {
229 job_
->FallbackToNetwork();
230 TRACE_EVENT_ASYNC_END2(
232 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
236 "ServiceWorkerVersion is not available, so falling back to network");
240 ServiceWorkerMetrics::CountControlledPageLoad(stripped_url_
);
242 provider_host_
->AssociateRegistration(registration
.get());
243 job_
->ForwardToServiceWorker();
244 TRACE_EVENT_ASYNC_END2(
246 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
250 "Forwarded to the ServiceWorker");
253 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged(
254 ServiceWorkerRegistration
* registration
,
255 ServiceWorkerVersion
* version
) {
257 provider_host_
->SetAllowAssociation(true);
258 if (version
!= registration
->active_version() ||
259 version
->status() != ServiceWorkerVersion::ACTIVATED
||
261 job_
->FallbackToNetwork();
265 ServiceWorkerMetrics::CountControlledPageLoad(stripped_url_
);
267 provider_host_
->AssociateRegistration(registration
);
268 job_
->ForwardToServiceWorker();
271 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() {
274 DCHECK(provider_host_
->active_version());
275 job_
->ForwardToServiceWorker();
278 } // namespace content