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