| 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 "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "content/browser/service_worker/service_worker_context_core.h" | 8 #include "content/browser/service_worker/service_worker_context_core.h" |
| 9 #include "content/browser/service_worker/service_worker_metrics.h" | 9 #include "content/browser/service_worker/service_worker_metrics.h" |
| 10 #include "content/browser/service_worker/service_worker_provider_host.h" | 10 #include "content/browser/service_worker/service_worker_provider_host.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 stripped_url, | 129 stripped_url, |
| 130 base::Bind(&self::DidLookupRegistrationForMainResource, | 130 base::Bind(&self::DidLookupRegistrationForMainResource, |
| 131 weak_factory_.GetWeakPtr())); | 131 weak_factory_.GetWeakPtr())); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void | 134 void |
| 135 ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource( | 135 ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource( |
| 136 ServiceWorkerStatusCode status, | 136 ServiceWorkerStatusCode status, |
| 137 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 137 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 138 DCHECK(job_.get()); | 138 DCHECK(job_.get()); |
| 139 provider_host_->SetAllowAssociation(true); | 139 if (provider_host_) |
| 140 provider_host_->SetAllowAssociation(true); |
| 140 if (status != SERVICE_WORKER_OK) { | 141 if (status != SERVICE_WORKER_OK) { |
| 141 job_->FallbackToNetwork(); | 142 job_->FallbackToNetwork(); |
| 142 TRACE_EVENT_ASYNC_END1( | 143 TRACE_EVENT_ASYNC_END1( |
| 143 "ServiceWorker", | 144 "ServiceWorker", |
| 144 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 145 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
| 145 job_.get(), | 146 job_.get(), |
| 146 "Status", status); | 147 "Status", status); |
| 147 return; | 148 return; |
| 148 } | 149 } |
| 149 DCHECK(registration.get()); | 150 DCHECK(registration.get()); |
| 150 | 151 |
| 151 // Initiate activation of a waiting version. | 152 // Initiate activation of a waiting version. |
| 152 // Usually a register job initiates activation but that | 153 // Usually a register job initiates activation but that |
| 153 // doesn't happen if the browser exits prior to activation | 154 // doesn't happen if the browser exits prior to activation |
| 154 // having occurred. This check handles that case. | 155 // having occurred. This check handles that case. |
| 155 if (registration->waiting_version()) | 156 if (registration->waiting_version()) |
| 156 registration->ActivateWaitingVersionWhenReady(); | 157 registration->ActivateWaitingVersionWhenReady(); |
| 157 | 158 |
| 158 scoped_refptr<ServiceWorkerVersion> active_version = | 159 scoped_refptr<ServiceWorkerVersion> active_version = |
| 159 registration->active_version(); | 160 registration->active_version(); |
| 160 | 161 |
| 161 // Wait until it's activated before firing fetch events. | 162 // Wait until it's activated before firing fetch events. |
| 162 if (active_version.get() && | 163 if (active_version.get() && |
| 163 active_version->status() == ServiceWorkerVersion::ACTIVATING) { | 164 active_version->status() == ServiceWorkerVersion::ACTIVATING) { |
| 164 provider_host_->SetAllowAssociation(false); | 165 if (provider_host_) |
| 166 provider_host_->SetAllowAssociation(false); |
| 165 registration->active_version()->RegisterStatusChangeCallback( | 167 registration->active_version()->RegisterStatusChangeCallback( |
| 166 base::Bind(&self::OnVersionStatusChanged, | 168 base::Bind(&self::OnVersionStatusChanged, |
| 167 weak_factory_.GetWeakPtr(), | 169 weak_factory_.GetWeakPtr(), |
| 168 registration, | 170 registration, |
| 169 active_version)); | 171 active_version)); |
| 170 TRACE_EVENT_ASYNC_END2( | 172 TRACE_EVENT_ASYNC_END2( |
| 171 "ServiceWorker", | 173 "ServiceWorker", |
| 172 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 174 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
| 173 job_.get(), | 175 job_.get(), |
| 174 "Status", status, | 176 "Status", status, |
| 175 "Info", "Wait until finished SW activation"); | 177 "Info", "Wait until finished SW activation"); |
| 176 return; | 178 return; |
| 177 } | 179 } |
| 178 | 180 |
| 179 if (!active_version.get() || | 181 if (!active_version.get() || |
| 180 active_version->status() != ServiceWorkerVersion::ACTIVATED) { | 182 active_version->status() != ServiceWorkerVersion::ACTIVATED) { |
| 181 job_->FallbackToNetwork(); | 183 job_->FallbackToNetwork(); |
| 182 TRACE_EVENT_ASYNC_END2( | 184 TRACE_EVENT_ASYNC_END2( |
| 183 "ServiceWorker", | 185 "ServiceWorker", |
| 184 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 186 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
| 185 job_.get(), | 187 job_.get(), |
| 186 "Status", status, | 188 "Status", status, |
| 187 "Info", | 189 "Info", |
| 188 "ServiceWorkerVersion is not available, so falling back to network"); | 190 "ServiceWorkerVersion is not available, so falling back to network"); |
| 189 return; | 191 return; |
| 190 } | 192 } |
| 191 | 193 |
| 192 ServiceWorkerMetrics::CountControlledPageLoad(); | 194 ServiceWorkerMetrics::CountControlledPageLoad(); |
| 193 | 195 |
| 194 provider_host_->AssociateRegistration(registration.get()); | 196 if (provider_host_) |
| 197 provider_host_->AssociateRegistration(registration.get()); |
| 195 job_->ForwardToServiceWorker(); | 198 job_->ForwardToServiceWorker(); |
| 196 TRACE_EVENT_ASYNC_END2( | 199 TRACE_EVENT_ASYNC_END2( |
| 197 "ServiceWorker", | 200 "ServiceWorker", |
| 198 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 201 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
| 199 job_.get(), | 202 job_.get(), |
| 200 "Status", status, | 203 "Status", status, |
| 201 "Info", | 204 "Info", |
| 202 "Forwarded to the ServiceWorker"); | 205 "Forwarded to the ServiceWorker"); |
| 203 } | 206 } |
| 204 | 207 |
| 205 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( | 208 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( |
| 206 ServiceWorkerRegistration* registration, | 209 ServiceWorkerRegistration* registration, |
| 207 ServiceWorkerVersion* version) { | 210 ServiceWorkerVersion* version) { |
| 208 provider_host_->SetAllowAssociation(true); | 211 if (provider_host_) |
| 212 provider_host_->SetAllowAssociation(true); |
| 209 if (version != registration->active_version() || | 213 if (version != registration->active_version() || |
| 210 version->status() != ServiceWorkerVersion::ACTIVATED) { | 214 version->status() != ServiceWorkerVersion::ACTIVATED) { |
| 211 job_->FallbackToNetwork(); | 215 job_->FallbackToNetwork(); |
| 212 return; | 216 return; |
| 213 } | 217 } |
| 214 | 218 |
| 215 ServiceWorkerMetrics::CountControlledPageLoad(); | 219 ServiceWorkerMetrics::CountControlledPageLoad(); |
| 216 | 220 |
| 217 provider_host_->AssociateRegistration(registration); | 221 if (provider_host_) |
| 222 provider_host_->AssociateRegistration(registration); |
| 218 job_->ForwardToServiceWorker(); | 223 job_->ForwardToServiceWorker(); |
| 219 } | 224 } |
| 220 | 225 |
| 221 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() { | 226 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() { |
| 222 DCHECK(job_.get()); | 227 DCHECK(job_.get()); |
| 223 DCHECK(context_); | 228 DCHECK(context_); |
| 224 DCHECK(provider_host_->active_version()); | 229 DCHECK(provider_host_->active_version()); |
| 225 job_->ForwardToServiceWorker(); | 230 job_->ForwardToServiceWorker(); |
| 226 } | 231 } |
| 227 | 232 |
| 228 } // namespace content | 233 } // namespace content |
| OLD | NEW |