15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Use of this source code is governed by a BSD-style license that can be
35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// found in the LICENSE file.
45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
55c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_controllee_request_handler.h"
65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/debug/trace_event.h"
85c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_context_core.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/service_worker/service_worker_metrics.h"
105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_provider_host.h"
115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_registration.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_url_request_job.h"
135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_utils.h"
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "content/common/resource_request_body.h"
155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/common/service_worker/service_worker_types.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/base/load_flags.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/base/net_util.h"
185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "net/url_request/url_request.h"
195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace content {
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler(
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    base::WeakPtr<ServiceWorkerContextCore> context,
245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    base::WeakPtr<ServiceWorkerProviderHost> provider_host,
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ResourceType resource_type,
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    scoped_refptr<ResourceRequestBody> body)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : ServiceWorkerRequestHandler(context,
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  provider_host,
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  blob_storage_context,
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  resource_type),
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      is_main_resource_load_(
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          ServiceWorkerUtils::IsMainResourceType(resource_type)),
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      body_(body),
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      weak_factory_(this) {
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuServiceWorkerControlleeRequestHandler::
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ~ServiceWorkerControlleeRequestHandler() {
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Navigation triggers an update to occur shortly after the page and
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // its initial subresources load.
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (provider_host_ && provider_host_->active_version()) {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (is_main_resource_load_)
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      provider_host_->active_version()->ScheduleUpdate();
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    else
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      provider_host_->active_version()->DeferScheduledUpdate();
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (is_main_resource_load_ && provider_host_)
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_->SetAllowAssociation(true);
515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunet::URLRequestJob* ServiceWorkerControlleeRequestHandler::MaybeCreateJob(
545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    net::URLRequest* request,
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    net::NetworkDelegate* network_delegate) {
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (!context_ || !provider_host_) {
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // We can't do anything other than to fall back to network.
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    job_ = NULL;
595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return NULL;
605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // This may get called multiple times for original and redirect requests:
635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // A. original request case: job_ is null, no previous location info.
645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // B. redirect or restarted request case:
655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  //  a) job_ is non-null if the previous location was forwarded to SW.
665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  //  b) job_ is null if the previous location was fallback.
675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  //  c) job_ is non-null if additional restart was required to fall back.
685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // We've come here by restart, we already have original request and it
705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // tells we should fallback to network. (Case B-c)
715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (job_.get() && job_->ShouldFallbackToNetwork()) {
725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    job_ = NULL;
735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return NULL;
745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // It's for original request (A) or redirect case (B-a or B-b).
775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker());
785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  job_ = new ServiceWorkerURLRequestJob(
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      request, network_delegate, provider_host_, blob_storage_context_, body_);
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (is_main_resource_load_)
825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PrepareForMainResource(request->url());
835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  else
845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PrepareForSubResource();
855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (job_->ShouldFallbackToNetwork()) {
875c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // If we know we can fallback to network at this point (in case
885c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // the storage lookup returned immediately), just return NULL here to
895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // fallback to network.
905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    job_ = NULL;
915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return NULL;
925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  return job_.get();
955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ServiceWorkerControlleeRequestHandler::GetExtraResponseInfo(
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    bool* was_fetched_via_service_worker,
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    GURL* original_url_via_service_worker,
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::TimeTicks* fetch_start_time,
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::TimeTicks* fetch_ready_time,
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::TimeTicks* fetch_end_time) const {
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!job_.get()) {
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    *was_fetched_via_service_worker = false;
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    *original_url_via_service_worker = GURL();
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  job_->GetExtraResponseInfo(was_fetched_via_service_worker,
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             original_url_via_service_worker,
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             fetch_start_time,
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             fetch_ready_time,
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             fetch_end_time);
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid ServiceWorkerControlleeRequestHandler::PrepareForMainResource(
1165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const GURL& url) {
1175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(job_.get());
1185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(context_);
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(provider_host_);
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TRACE_EVENT_ASYNC_BEGIN1(
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "ServiceWorker",
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      job_.get(),
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "URL", url.spec());
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // The corresponding provider_host may already have associated a registration
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // in redirect case, unassociate it now.
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->DisassociateRegistration();
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Also prevent a registrater job for establishing an association to a new
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // registration while we're finding an existing registration.
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->SetAllowAssociation(false);
13246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GURL stripped_url = net::SimplifyUrlForRequest(url);
13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  provider_host_->SetDocumentUrl(stripped_url);
1355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  context_->storage()->FindRegistrationForDocument(
13646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      stripped_url,
1375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      base::Bind(&self::DidLookupRegistrationForMainResource,
1385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                 weak_factory_.GetWeakPtr()));
1395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
1405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
1415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid
1425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource(
1435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ServiceWorkerStatusCode status,
1445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const scoped_refptr<ServiceWorkerRegistration>& registration) {
1455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(job_.get());
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (provider_host_)
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_->SetAllowAssociation(true);
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (status != SERVICE_WORKER_OK || !provider_host_) {
1495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    job_->FallbackToNetwork();
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    TRACE_EVENT_ASYNC_END1(
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorker",
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        job_.get(),
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "Status", status);
1555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return;
1565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(registration.get());
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Initiate activation of a waiting version.
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Usually a register job initiates activation but that
1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // doesn't happen if the browser exits prior to activation
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // having occurred. This check handles that case.
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (registration->waiting_version())
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    registration->ActivateWaitingVersionWhenReady();
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<ServiceWorkerVersion> active_version =
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      registration->active_version();
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Wait until it's activated before firing fetch events.
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (active_version.get() &&
1716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      active_version->status() == ServiceWorkerVersion::ACTIVATING) {
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_->SetAllowAssociation(false);
1735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    registration->active_version()->RegisterStatusChangeCallback(
1745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::Bind(&self::OnVersionStatusChanged,
1755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   weak_factory_.GetWeakPtr(),
1765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   registration,
1775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   active_version));
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    TRACE_EVENT_ASYNC_END2(
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorker",
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        job_.get(),
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "Status", status,
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "Info", "Wait until finished SW activation");
1845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
1855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!active_version.get() ||
1885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      active_version->status() != ServiceWorkerVersion::ACTIVATED) {
1895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    job_->FallbackToNetwork();
1901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    TRACE_EVENT_ASYNC_END2(
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorker",
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
1931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        job_.get(),
1941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "Status", status,
1951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "Info",
1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        "ServiceWorkerVersion is not available, so falling back to network");
1975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerMetrics::CountControlledPageLoad();
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->AssociateRegistration(registration.get());
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  job_->ForwardToServiceWorker();
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TRACE_EVENT_ASYNC_END2(
2051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "ServiceWorker",
2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
2071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      job_.get(),
2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Status", status,
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Info",
2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Forwarded to the ServiceWorker");
2115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged(
2145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ServiceWorkerRegistration* registration,
2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ServiceWorkerVersion* version) {
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (provider_host_)
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_->SetAllowAssociation(true);
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (version != registration->active_version() ||
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      version->status() != ServiceWorkerVersion::ACTIVATED ||
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      !provider_host_) {
2215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    job_->FallbackToNetwork();
2225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
2235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerMetrics::CountControlledPageLoad();
2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  provider_host_->AssociateRegistration(registration);
2285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  job_->ForwardToServiceWorker();
2295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid ServiceWorkerControlleeRequestHandler::PrepareForSubResource() {
2325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(job_.get());
2335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(context_);
2345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(provider_host_->active_version());
2355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  job_->ForwardToServiceWorker();
2365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}  // namespace content
239