| 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_metrics.h" | 13 #include "content/browser/service_worker/service_worker_metrics.h" |
| 14 #include "content/browser/service_worker/service_worker_provider_host.h" | 14 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 15 #include "content/browser/service_worker/service_worker_registration.h" | 15 #include "content/browser/service_worker/service_worker_registration.h" |
| 16 #include "content/browser/service_worker/service_worker_response_info.h" | 16 #include "content/browser/service_worker/service_worker_response_info.h" |
| 17 #include "content/browser/service_worker/service_worker_url_request_job.h" | 17 #include "content/browser/service_worker/service_worker_url_request_job.h" |
| 18 #include "content/common/resource_request_body_impl.h" | 18 #include "content/common/resource_request_body_impl.h" |
| 19 #include "content/common/service_worker/service_worker_types.h" | 19 #include "content/common/service_worker/service_worker_types.h" |
| 20 #include "content/common/service_worker/service_worker_utils.h" | 20 #include "content/common/service_worker/service_worker_utils.h" |
| 21 #include "content/public/browser/content_browser_client.h" | 21 #include "content/public/browser/content_browser_client.h" |
| 22 #include "content/public/common/content_client.h" | 22 #include "content/public/common/content_client.h" |
| 23 #include "content/public/common/resource_response_info.h" | 23 #include "content/public/common/resource_response_info.h" |
| 24 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 25 #include "net/base/url_util.h" | 25 #include "net/base/url_util.h" |
| 26 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 namespace { | |
| 31 | |
| 32 bool MaybeForwardToServiceWorker(ServiceWorkerURLRequestJob* job, | |
| 33 const ServiceWorkerVersion* version) { | |
| 34 DCHECK(job); | |
| 35 DCHECK(version); | |
| 36 if (version->has_fetch_handler()) { | |
| 37 job->ForwardToServiceWorker(); | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 job->FallbackToNetwork(); | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( | 30 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( |
| 48 base::WeakPtr<ServiceWorkerContextCore> context, | 31 base::WeakPtr<ServiceWorkerContextCore> context, |
| 49 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 32 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| 50 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 33 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
| 51 FetchRequestMode request_mode, | 34 FetchRequestMode request_mode, |
| 52 FetchCredentialsMode credentials_mode, | 35 FetchCredentialsMode credentials_mode, |
| 53 FetchRedirectMode redirect_mode, | 36 FetchRedirectMode redirect_mode, |
| 54 ResourceType resource_type, | 37 ResourceType resource_type, |
| 55 RequestContextType request_context_type, | 38 RequestContextType request_context_type, |
| 56 RequestContextFrameType frame_type, | 39 RequestContextFrameType frame_type, |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 job_.get(), | 259 job_.get(), |
| 277 "Status", status, | 260 "Status", status, |
| 278 "Info", | 261 "Info", |
| 279 "ServiceWorkerVersion is not available, so falling back to network"); | 262 "ServiceWorkerVersion is not available, so falling back to network"); |
| 280 return; | 263 return; |
| 281 } | 264 } |
| 282 | 265 |
| 283 ServiceWorkerMetrics::CountControlledPageLoad( | 266 ServiceWorkerMetrics::CountControlledPageLoad( |
| 284 stripped_url_, active_version->has_fetch_handler(), is_main_frame_load_); | 267 stripped_url_, active_version->has_fetch_handler(), is_main_frame_load_); |
| 285 | 268 |
| 286 bool is_forwarded = | 269 job_->ForwardToServiceWorker(); |
| 287 MaybeForwardToServiceWorker(job_.get(), active_version.get()); | |
| 288 | |
| 289 TRACE_EVENT_ASYNC_END2( | 270 TRACE_EVENT_ASYNC_END2( |
| 290 "ServiceWorker", | 271 "ServiceWorker", |
| 291 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 272 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
| 292 job_.get(), "Status", status, "Info", | 273 job_.get(), "Status", status, "Info", "Forwarded to the ServiceWorker"); |
| 293 (is_forwarded) ? "Forwarded to the ServiceWorker" | |
| 294 : "Skipped the ServiceWorker which has no fetch handler"); | |
| 295 } | 274 } |
| 296 | 275 |
| 297 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( | 276 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( |
| 298 ServiceWorkerRegistration* registration, | 277 ServiceWorkerRegistration* registration, |
| 299 ServiceWorkerVersion* version) { | 278 ServiceWorkerVersion* version) { |
| 300 // The job may have been canceled and then destroyed before this was invoked. | 279 // The job may have been canceled and then destroyed before this was invoked. |
| 301 if (!job_) | 280 if (!job_) |
| 302 return; | 281 return; |
| 303 | 282 |
| 304 if (provider_host_) | 283 if (provider_host_) |
| 305 provider_host_->SetAllowAssociation(true); | 284 provider_host_->SetAllowAssociation(true); |
| 306 if (version != registration->active_version() || | 285 if (version != registration->active_version() || |
| 307 version->status() != ServiceWorkerVersion::ACTIVATED || | 286 version->status() != ServiceWorkerVersion::ACTIVATED || |
| 308 !provider_host_) { | 287 !provider_host_) { |
| 309 job_->FallbackToNetwork(); | 288 job_->FallbackToNetwork(); |
| 310 return; | 289 return; |
| 311 } | 290 } |
| 312 | 291 |
| 313 ServiceWorkerMetrics::CountControlledPageLoad( | 292 ServiceWorkerMetrics::CountControlledPageLoad( |
| 314 stripped_url_, version->has_fetch_handler(), is_main_frame_load_); | 293 stripped_url_, version->has_fetch_handler(), is_main_frame_load_); |
| 315 | 294 |
| 316 provider_host_->AssociateRegistration(registration, | 295 provider_host_->AssociateRegistration(registration, |
| 317 false /* notify_controllerchange */); | 296 false /* notify_controllerchange */); |
| 318 | 297 job_->ForwardToServiceWorker(); |
| 319 MaybeForwardToServiceWorker(job_.get(), version); | |
| 320 } | 298 } |
| 321 | 299 |
| 322 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( | 300 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( |
| 323 const scoped_refptr<ServiceWorkerRegistration>& original_registration, | 301 const scoped_refptr<ServiceWorkerRegistration>& original_registration, |
| 324 ServiceWorkerStatusCode status, | 302 ServiceWorkerStatusCode status, |
| 325 const std::string& status_message, | 303 const std::string& status_message, |
| 326 int64_t registration_id) { | 304 int64_t registration_id) { |
| 327 DCHECK(force_update_started_); | 305 DCHECK(force_update_started_); |
| 328 | 306 |
| 329 // The job may have been canceled and then destroyed before this was invoked. | 307 // The job may have been canceled and then destroyed before this was invoked. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 357 } |
| 380 version->RegisterStatusChangeCallback( | 358 version->RegisterStatusChangeCallback( |
| 381 base::Bind(&self::OnUpdatedVersionStatusChanged, | 359 base::Bind(&self::OnUpdatedVersionStatusChanged, |
| 382 weak_factory_.GetWeakPtr(), registration, version)); | 360 weak_factory_.GetWeakPtr(), registration, version)); |
| 383 } | 361 } |
| 384 | 362 |
| 385 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() { | 363 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() { |
| 386 DCHECK(job_.get()); | 364 DCHECK(job_.get()); |
| 387 DCHECK(context_); | 365 DCHECK(context_); |
| 388 DCHECK(provider_host_->active_version()); | 366 DCHECK(provider_host_->active_version()); |
| 389 MaybeForwardToServiceWorker(job_.get(), provider_host_->active_version()); | 367 job_->ForwardToServiceWorker(); |
| 390 } | 368 } |
| 391 | 369 |
| 392 void ServiceWorkerControlleeRequestHandler::OnPrepareToRestart() { | 370 void ServiceWorkerControlleeRequestHandler::OnPrepareToRestart() { |
| 393 use_network_ = true; | 371 use_network_ = true; |
| 394 ClearJob(); | 372 ClearJob(); |
| 395 } | 373 } |
| 396 | 374 |
| 397 ServiceWorkerVersion* | 375 ServiceWorkerVersion* |
| 398 ServiceWorkerControlleeRequestHandler::GetServiceWorkerVersion( | 376 ServiceWorkerControlleeRequestHandler::GetServiceWorkerVersion( |
| 399 ServiceWorkerMetrics::URLRequestJobResult* result) { | 377 ServiceWorkerMetrics::URLRequestJobResult* result) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 423 DCHECK(provider_host_); | 401 DCHECK(provider_host_); |
| 424 // Detach the controller so subresource requests also skip the worker. | 402 // Detach the controller so subresource requests also skip the worker. |
| 425 provider_host_->NotifyControllerLost(); | 403 provider_host_->NotifyControllerLost(); |
| 426 } | 404 } |
| 427 | 405 |
| 428 void ServiceWorkerControlleeRequestHandler::ClearJob() { | 406 void ServiceWorkerControlleeRequestHandler::ClearJob() { |
| 429 job_.reset(); | 407 job_.reset(); |
| 430 } | 408 } |
| 431 | 409 |
| 432 } // namespace content | 410 } // namespace content |
| OLD | NEW |