OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_controllee_request_handl er.h" | 5 #include "content/browser/service_worker/service_worker_controllee_request_handl er.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
13 #include "content/browser/service_worker/service_worker_job_wrapper.h" | |
13 #include "content/browser/service_worker/service_worker_metrics.h" | 14 #include "content/browser/service_worker/service_worker_metrics.h" |
14 #include "content/browser/service_worker/service_worker_provider_host.h" | 15 #include "content/browser/service_worker/service_worker_provider_host.h" |
15 #include "content/browser/service_worker/service_worker_registration.h" | 16 #include "content/browser/service_worker/service_worker_registration.h" |
16 #include "content/browser/service_worker/service_worker_response_info.h" | 17 #include "content/browser/service_worker/service_worker_response_info.h" |
17 #include "content/browser/service_worker/service_worker_url_request_job.h" | 18 #include "content/browser/service_worker/service_worker_url_request_job.h" |
18 #include "content/common/resource_request_body_impl.h" | 19 #include "content/common/resource_request_body_impl.h" |
19 #include "content/common/service_worker/service_worker_types.h" | 20 #include "content/common/service_worker/service_worker_types.h" |
20 #include "content/common/service_worker/service_worker_utils.h" | 21 #include "content/common/service_worker/service_worker_utils.h" |
21 #include "content/public/browser/content_browser_client.h" | 22 #include "content/public/browser/content_browser_client.h" |
22 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/resource_request_info.h" | 24 #include "content/public/browser/resource_request_info.h" |
24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/browser_side_navigation_policy.h" | 26 #include "content/public/common/browser_side_navigation_policy.h" |
26 #include "content/public/common/content_client.h" | 27 #include "content/public/common/content_client.h" |
27 #include "content/public/common/resource_response_info.h" | 28 #include "content/public/common/resource_response_info.h" |
28 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
29 #include "net/base/url_util.h" | 30 #include "net/base/url_util.h" |
30 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
31 #include "ui/base/page_transition_types.h" | 32 #include "ui/base/page_transition_types.h" |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 bool MaybeForwardToServiceWorker(ServiceWorkerURLRequestJob* job, | 38 bool MaybeForwardToServiceWorker(ServiceWorkerJobWrapper* job, |
38 const ServiceWorkerVersion* version) { | 39 const ServiceWorkerVersion* version) { |
39 DCHECK(job); | 40 DCHECK(job); |
40 DCHECK(version); | 41 DCHECK(version); |
41 DCHECK_NE(version->fetch_handler_existence(), | 42 DCHECK_NE(version->fetch_handler_existence(), |
42 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 43 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
43 if (version->fetch_handler_existence() == | 44 if (version->fetch_handler_existence() == |
44 ServiceWorkerVersion::FetchHandlerExistence::EXISTS) { | 45 ServiceWorkerVersion::FetchHandlerExistence::EXISTS) { |
45 job->ForwardToServiceWorker(); | 46 job->ForwardToServiceWorker(); |
46 return true; | 47 return true; |
47 } | 48 } |
48 | 49 |
49 job->FallbackToNetworkOrRenderer(); | 50 job->FallbackToNetworkOrRenderer(); |
50 return false; | 51 return false; |
51 } | 52 } |
52 | 53 |
53 ui::PageTransition GetPageTransition(net::URLRequest* request) { | |
54 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
55 // ResourceRequestInfo may not be set in some tests. | |
56 if (!info) | |
57 return ui::PAGE_TRANSITION_LINK; | |
58 return info->GetPageTransition(); | |
59 } | |
60 | |
61 } // namespace | 54 } // namespace |
62 | 55 |
63 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( | 56 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( |
64 base::WeakPtr<ServiceWorkerContextCore> context, | 57 base::WeakPtr<ServiceWorkerContextCore> context, |
65 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 58 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
66 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 59 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
67 FetchRequestMode request_mode, | 60 FetchRequestMode request_mode, |
68 FetchCredentialsMode credentials_mode, | 61 FetchCredentialsMode credentials_mode, |
69 FetchRedirectMode redirect_mode, | 62 FetchRedirectMode redirect_mode, |
70 ResourceType resource_type, | 63 ResourceType resource_type, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 } | 126 } |
134 | 127 |
135 // It's for original request (A) or redirect case (B-a or B-b). | 128 // It's for original request (A) or redirect case (B-a or B-b). |
136 std::unique_ptr<ServiceWorkerURLRequestJob> job( | 129 std::unique_ptr<ServiceWorkerURLRequestJob> job( |
137 new ServiceWorkerURLRequestJob( | 130 new ServiceWorkerURLRequestJob( |
138 request, network_delegate, provider_host_->client_uuid(), | 131 request, network_delegate, provider_host_->client_uuid(), |
139 blob_storage_context_, resource_context, request_mode_, | 132 blob_storage_context_, resource_context, request_mode_, |
140 credentials_mode_, redirect_mode_, resource_type_, | 133 credentials_mode_, redirect_mode_, resource_type_, |
141 request_context_type_, frame_type_, body_, | 134 request_context_type_, frame_type_, body_, |
142 ServiceWorkerFetchType::FETCH, base::nullopt, this)); | 135 ServiceWorkerFetchType::FETCH, base::nullopt, this)); |
143 job_ = job->GetWeakPtr(); | 136 job_ = base::MakeUnique<ServiceWorkerJobWrapper>(job->GetWeakPtr()); |
144 | 137 |
145 resource_context_ = resource_context; | 138 resource_context_ = resource_context; |
146 | 139 |
147 if (is_main_resource_load_) | 140 if (is_main_resource_load_) |
148 PrepareForMainResource(request); | 141 PrepareForMainResource(request->url(), request->first_party_for_cookies()); |
149 else | 142 else |
150 PrepareForSubResource(); | 143 PrepareForSubResource(); |
151 | 144 |
152 if (job_->ShouldFallbackToNetwork()) { | 145 if (job_->ShouldFallbackToNetwork()) { |
153 // If we know we can fallback to network at this point (in case | 146 // If we know we can fallback to network at this point (in case |
154 // the storage lookup returned immediately), just destroy the job and return | 147 // the storage lookup returned immediately), just destroy the job and return |
155 // NULL here to fallback to network. | 148 // NULL here to fallback to network. |
156 | 149 |
157 // If this is a subresource request, all subsequent requests should also use | 150 // If this is a subresource request, all subsequent requests should also use |
158 // the network. | 151 // the network. |
159 if (!is_main_resource_load_) | 152 if (!is_main_resource_load_) |
160 use_network_ = true; | 153 use_network_ = true; |
161 | 154 |
162 job.reset(); | 155 job.reset(); |
163 ClearJob(); | 156 ClearJob(); |
164 } | 157 } |
165 | 158 |
166 return job.release(); | 159 return job.release(); |
167 } | 160 } |
168 | 161 |
169 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource( | 162 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource( |
170 const net::URLRequest* request) { | 163 const GURL& url, |
164 const GURL& first_party_for_cookies) { | |
171 DCHECK(job_.get()); | 165 DCHECK(job_.get()); |
172 DCHECK(context_); | 166 DCHECK(context_); |
173 DCHECK(provider_host_); | 167 DCHECK(provider_host_); |
174 TRACE_EVENT_ASYNC_BEGIN1( | 168 TRACE_EVENT_ASYNC_BEGIN1( |
175 "ServiceWorker", | 169 "ServiceWorker", |
176 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 170 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
177 job_.get(), | 171 job_.get(), "URL", url.spec()); |
178 "URL", request->url().spec()); | |
179 // The corresponding provider_host may already have associated a registration | 172 // The corresponding provider_host may already have associated a registration |
180 // in redirect case, unassociate it now. | 173 // in redirect case, unassociate it now. |
181 provider_host_->DisassociateRegistration(); | 174 provider_host_->DisassociateRegistration(); |
182 | 175 |
183 // Also prevent a registrater job for establishing an association to a new | 176 // Also prevent a registrater job for establishing an association to a new |
184 // registration while we're finding an existing registration. | 177 // registration while we're finding an existing registration. |
185 provider_host_->SetAllowAssociation(false); | 178 provider_host_->SetAllowAssociation(false); |
186 | 179 |
187 stripped_url_ = net::SimplifyUrlForRequest(request->url()); | 180 stripped_url_ = net::SimplifyUrlForRequest(url); |
188 provider_host_->SetDocumentUrl(stripped_url_); | 181 provider_host_->SetDocumentUrl(stripped_url_); |
189 provider_host_->SetTopmostFrameUrl(request->first_party_for_cookies()); | 182 provider_host_->SetTopmostFrameUrl(first_party_for_cookies); |
190 context_->storage()->FindRegistrationForDocument( | 183 context_->storage()->FindRegistrationForDocument( |
191 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, | 184 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, |
192 weak_factory_.GetWeakPtr())); | 185 weak_factory_.GetWeakPtr())); |
193 } | 186 } |
194 | 187 |
195 void ServiceWorkerControlleeRequestHandler:: | 188 void ServiceWorkerControlleeRequestHandler:: |
196 DidLookupRegistrationForMainResource( | 189 DidLookupRegistrationForMainResource( |
197 ServiceWorkerStatusCode status, | 190 ServiceWorkerStatusCode status, |
198 scoped_refptr<ServiceWorkerRegistration> registration) { | 191 scoped_refptr<ServiceWorkerRegistration> registration) { |
199 // The job may have been canceled and then destroyed before this was invoked. | 192 // The job may have been canceled and then destroyed before this was invoked. |
200 if (!job_) | 193 if (job_->WasCanceled()) |
201 return; | 194 return; |
202 | 195 |
203 const bool need_to_update = !force_update_started_ && registration && | 196 const bool need_to_update = !force_update_started_ && registration && |
204 context_->force_update_on_page_load(); | 197 context_->force_update_on_page_load(); |
205 | 198 |
206 if (provider_host_ && !need_to_update) | 199 if (provider_host_ && !need_to_update) |
207 provider_host_->SetAllowAssociation(true); | 200 provider_host_->SetAllowAssociation(true); |
208 if (status != SERVICE_WORKER_OK || !provider_host_ || !context_) { | 201 if (status != SERVICE_WORKER_OK || !provider_host_ || !context_) { |
209 job_->FallbackToNetwork(); | 202 job_->FallbackToNetwork(); |
210 TRACE_EVENT_ASYNC_END1( | 203 TRACE_EVENT_ASYNC_END1( |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 "Status", status, | 298 "Status", status, |
306 "Info", | 299 "Info", |
307 "ServiceWorkerVersion is not available, so falling back to network"); | 300 "ServiceWorkerVersion is not available, so falling back to network"); |
308 return; | 301 return; |
309 } | 302 } |
310 | 303 |
311 DCHECK_NE(active_version->fetch_handler_existence(), | 304 DCHECK_NE(active_version->fetch_handler_existence(), |
312 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 305 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
313 ServiceWorkerMetrics::CountControlledPageLoad( | 306 ServiceWorkerMetrics::CountControlledPageLoad( |
314 active_version->site_for_uma(), stripped_url_, is_main_frame_load_, | 307 active_version->site_for_uma(), stripped_url_, is_main_frame_load_, |
315 GetPageTransition(job_->request()), job_->request()->url_chain().size()); | 308 job_->GetPageTransition(), job_->GetURLChainSize()); |
316 | 309 |
317 bool is_forwarded = | 310 bool is_forwarded = |
318 MaybeForwardToServiceWorker(job_.get(), active_version.get()); | 311 MaybeForwardToServiceWorker(job_.get(), active_version.get()); |
319 | 312 |
320 TRACE_EVENT_ASYNC_END2( | 313 TRACE_EVENT_ASYNC_END2( |
321 "ServiceWorker", | 314 "ServiceWorker", |
322 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 315 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
323 job_.get(), "Status", status, "Info", | 316 job_.get(), "Status", status, "Info", |
324 (is_forwarded) ? "Forwarded to the ServiceWorker" | 317 (is_forwarded) ? "Forwarded to the ServiceWorker" |
325 : "Skipped the ServiceWorker which has no fetch handler"); | 318 : "Skipped the ServiceWorker which has no fetch handler"); |
326 } | 319 } |
327 | 320 |
328 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( | 321 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( |
329 ServiceWorkerRegistration* registration, | 322 ServiceWorkerRegistration* registration, |
330 ServiceWorkerVersion* version) { | 323 ServiceWorkerVersion* version) { |
331 // The job may have been canceled and then destroyed before this was invoked. | 324 // The job may have been canceled and then destroyed before this was invoked. |
332 if (!job_) | 325 if (job_->WasCanceled()) |
333 return; | 326 return; |
334 | 327 |
335 if (provider_host_) | 328 if (provider_host_) |
336 provider_host_->SetAllowAssociation(true); | 329 provider_host_->SetAllowAssociation(true); |
337 if (version != registration->active_version() || | 330 if (version != registration->active_version() || |
338 version->status() != ServiceWorkerVersion::ACTIVATED || | 331 version->status() != ServiceWorkerVersion::ACTIVATED || |
339 !provider_host_) { | 332 !provider_host_) { |
340 job_->FallbackToNetwork(); | 333 job_->FallbackToNetwork(); |
341 return; | 334 return; |
342 } | 335 } |
343 | 336 |
344 DCHECK_NE(version->fetch_handler_existence(), | 337 DCHECK_NE(version->fetch_handler_existence(), |
345 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 338 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
346 ServiceWorkerMetrics::CountControlledPageLoad( | 339 ServiceWorkerMetrics::CountControlledPageLoad( |
347 version->site_for_uma(), stripped_url_, is_main_frame_load_, | 340 version->site_for_uma(), stripped_url_, is_main_frame_load_, |
348 GetPageTransition(job_->request()), job_->request()->url_chain().size()); | 341 job_->GetPageTransition(), job_->GetURLChainSize()); |
349 | 342 |
350 provider_host_->AssociateRegistration(registration, | 343 provider_host_->AssociateRegistration(registration, |
351 false /* notify_controllerchange */); | 344 false /* notify_controllerchange */); |
352 | 345 |
353 MaybeForwardToServiceWorker(job_.get(), version); | 346 MaybeForwardToServiceWorker(job_.get(), version); |
354 } | 347 } |
355 | 348 |
356 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( | 349 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( |
357 const scoped_refptr<ServiceWorkerRegistration>& original_registration, | 350 const scoped_refptr<ServiceWorkerRegistration>& original_registration, |
358 ServiceWorkerStatusCode status, | 351 ServiceWorkerStatusCode status, |
359 const std::string& status_message, | 352 const std::string& status_message, |
360 int64_t registration_id) { | 353 int64_t registration_id) { |
361 DCHECK(force_update_started_); | 354 DCHECK(force_update_started_); |
362 | 355 |
363 // The job may have been canceled and then destroyed before this was invoked. | 356 // The job may have been canceled and then destroyed before this was invoked. |
364 if (!job_) | 357 if (job_->WasCanceled()) |
365 return; | 358 return; |
366 | 359 |
367 if (!context_) { | 360 if (!context_) { |
368 job_->FallbackToNetwork(); | 361 job_->FallbackToNetwork(); |
369 return; | 362 return; |
370 } | 363 } |
371 if (status != SERVICE_WORKER_OK || | 364 if (status != SERVICE_WORKER_OK || |
372 !original_registration->installing_version()) { | 365 !original_registration->installing_version()) { |
373 // Update failed. Look up the registration again since the original | 366 // Update failed. Look up the registration again since the original |
374 // registration was possibly unregistered in the meantime. | 367 // registration was possibly unregistered in the meantime. |
375 context_->storage()->FindRegistrationForDocument( | 368 context_->storage()->FindRegistrationForDocument( |
376 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, | 369 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, |
377 weak_factory_.GetWeakPtr())); | 370 weak_factory_.GetWeakPtr())); |
378 return; | 371 return; |
379 } | 372 } |
380 DCHECK_EQ(original_registration->id(), registration_id); | 373 DCHECK_EQ(original_registration->id(), registration_id); |
381 scoped_refptr<ServiceWorkerVersion> new_version = | 374 scoped_refptr<ServiceWorkerVersion> new_version = |
382 original_registration->installing_version(); | 375 original_registration->installing_version(); |
383 new_version->ReportForceUpdateToDevTools(); | 376 new_version->ReportForceUpdateToDevTools(); |
384 new_version->set_skip_waiting(true); | 377 new_version->set_skip_waiting(true); |
385 new_version->RegisterStatusChangeCallback(base::Bind( | 378 new_version->RegisterStatusChangeCallback(base::Bind( |
386 &self::OnUpdatedVersionStatusChanged, weak_factory_.GetWeakPtr(), | 379 &self::OnUpdatedVersionStatusChanged, weak_factory_.GetWeakPtr(), |
387 original_registration, new_version)); | 380 original_registration, new_version)); |
388 } | 381 } |
389 | 382 |
390 void ServiceWorkerControlleeRequestHandler::OnUpdatedVersionStatusChanged( | 383 void ServiceWorkerControlleeRequestHandler::OnUpdatedVersionStatusChanged( |
391 const scoped_refptr<ServiceWorkerRegistration>& registration, | 384 const scoped_refptr<ServiceWorkerRegistration>& registration, |
392 const scoped_refptr<ServiceWorkerVersion>& version) { | 385 const scoped_refptr<ServiceWorkerVersion>& version) { |
393 // The job may have been canceled and then destroyed before this was invoked. | 386 // The job may have been canceled and then destroyed before this was invoked. |
394 if (!job_) | 387 if (job_->WasCanceled()) |
395 return; | 388 return; |
396 | 389 |
397 if (!context_) { | 390 if (!context_) { |
398 job_->FallbackToNetwork(); | 391 job_->FallbackToNetwork(); |
399 return; | 392 return; |
400 } | 393 } |
401 if (version->status() == ServiceWorkerVersion::ACTIVATED || | 394 if (version->status() == ServiceWorkerVersion::ACTIVATED || |
402 version->status() == ServiceWorkerVersion::REDUNDANT) { | 395 version->status() == ServiceWorkerVersion::REDUNDANT) { |
403 // When the status is REDUNDANT, the update failed (eg: script error), we | 396 // When the status is REDUNDANT, the update failed (eg: script error), we |
404 // continue with the incumbent version. | 397 // continue with the incumbent version. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
464 return true; | 457 return true; |
465 } | 458 } |
466 | 459 |
467 void ServiceWorkerControlleeRequestHandler::MainResourceLoadFailed() { | 460 void ServiceWorkerControlleeRequestHandler::MainResourceLoadFailed() { |
468 DCHECK(provider_host_); | 461 DCHECK(provider_host_); |
469 // Detach the controller so subresource requests also skip the worker. | 462 // Detach the controller so subresource requests also skip the worker. |
470 provider_host_->NotifyControllerLost(); | 463 provider_host_->NotifyControllerLost(); |
471 } | 464 } |
472 | 465 |
473 void ServiceWorkerControlleeRequestHandler::ClearJob() { | 466 void ServiceWorkerControlleeRequestHandler::ClearJob() { |
474 job_.reset(); | 467 job_.reset(); |
kinuko
2017/05/11 12:48:53
This and job_->WasCanceled() don't look to work to
scottmg
2017/05/16 18:28:51
Done.
| |
475 } | 468 } |
476 | 469 |
477 } // namespace content | 470 } // namespace content |
OLD | NEW |