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_controllee_url_loader_fa
ctory.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" |
| 29 #include "mojo/public/cpp/bindings/strong_binding.h" |
28 #include "net/base/load_flags.h" | 30 #include "net/base/load_flags.h" |
29 #include "net/base/url_util.h" | 31 #include "net/base/url_util.h" |
30 #include "net/url_request/url_request.h" | 32 #include "net/url_request/url_request.h" |
31 #include "ui/base/page_transition_types.h" | 33 #include "ui/base/page_transition_types.h" |
32 | 34 |
33 namespace content { | 35 namespace content { |
34 | 36 |
| 37 // Helper to support both a URLRequestJob and a URLLoaderFactory to support |
| 38 // running with and without --enable-network-service. |
| 39 class JobWrapper { |
| 40 public: |
| 41 explicit JobWrapper(base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job) |
| 42 : url_request_job_(std::move(url_request_job)), |
| 43 factory_(nullptr), |
| 44 response_type_(ResponseType::kNotDetermined) {} |
| 45 |
| 46 // --enable-network-service. |
| 47 explicit JobWrapper(ServiceWorkerControlleeURLLoaderFactory* factory) |
| 48 : factory_(factory), response_type_(ResponseType::kNotDetermined) {} |
| 49 |
| 50 void FallbackToNetwork() { |
| 51 if (factory_) { |
| 52 DCHECK_EQ(ResponseType::kNotDetermined, response_type_); |
| 53 DCHECK(!IsFallbackToRendererNeeded()); |
| 54 response_type_ = ResponseType::kFallbackToNetwork; |
| 55 } else { |
| 56 url_request_job_->FallbackToNetwork(); |
| 57 } |
| 58 } |
| 59 |
| 60 bool ShouldFallbackToNetwork() { |
| 61 if (factory_) { |
| 62 return response_type_ == ResponseType::kFallbackToNetwork; |
| 63 } else { |
| 64 return url_request_job_->ShouldFallbackToNetwork(); |
| 65 } |
| 66 } |
| 67 |
| 68 ui::PageTransition GetPageTransition() { |
| 69 if (factory_) { |
| 70 NOTIMPLEMENTED(); |
| 71 return ui::PAGE_TRANSITION_LINK; |
| 72 } else { |
| 73 const ResourceRequestInfo* info = |
| 74 ResourceRequestInfo::ForRequest(url_request_job_->request()); |
| 75 // ResourceRequestInfo may not be set in some tests. |
| 76 if (!info) |
| 77 return ui::PAGE_TRANSITION_LINK; |
| 78 return info->GetPageTransition(); |
| 79 } |
| 80 } |
| 81 |
| 82 size_t GetURLChainSize() const { |
| 83 if (factory_) { |
| 84 NOTIMPLEMENTED(); |
| 85 return 0; |
| 86 } else { |
| 87 return url_request_job_->request()->url_chain().size(); |
| 88 } |
| 89 } |
| 90 |
| 91 void ForwardToServiceWorker() { |
| 92 if (factory_) { |
| 93 response_type_ = ResponseType::kForwardToServiceWorker; |
| 94 } else { |
| 95 url_request_job_->ForwardToServiceWorker(); |
| 96 } |
| 97 } |
| 98 |
| 99 void FallbackToNetworkOrRenderer() { |
| 100 if (factory_) { |
| 101 DCHECK_EQ(ResponseType::kNotDetermined, response_type_); |
| 102 if (IsFallbackToRendererNeeded()) { |
| 103 response_type_ = ResponseType::kFallbackToRenderer; |
| 104 } else { |
| 105 response_type_ = ResponseType::kFallbackToNetwork; |
| 106 } |
| 107 } else { |
| 108 url_request_job_->FallbackToNetworkOrRenderer(); |
| 109 } |
| 110 } |
| 111 |
| 112 void FailDueToLostController() { |
| 113 if (factory_) { |
| 114 DCHECK_EQ(ResponseType::kNotDetermined, response_type_); |
| 115 response_type_ = ResponseType::kFailDueToLostController; |
| 116 } |
| 117 url_request_job_->FailDueToLostController(); |
| 118 } |
| 119 |
| 120 bool WasCanceled() const { |
| 121 if (factory_) { |
| 122 NOTIMPLEMENTED(); |
| 123 return false; |
| 124 } else { |
| 125 return !url_request_job_; |
| 126 } |
| 127 } |
| 128 |
| 129 private: |
| 130 bool IsFallbackToRendererNeeded() const { |
| 131 DCHECK(factory_); |
| 132 NOTIMPLEMENTED(); |
| 133 return false; |
| 134 } |
| 135 |
| 136 base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job_; |
| 137 ServiceWorkerControlleeURLLoaderFactory* factory_; |
| 138 |
| 139 enum class ResponseType { |
| 140 kNotDetermined, |
| 141 kFailDueToLostController, |
| 142 kFallbackToNetwork, |
| 143 kFallbackToRenderer, // Use this when falling back with CORS check |
| 144 kForwardToServiceWorker, |
| 145 }; |
| 146 ResponseType response_type_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(JobWrapper); |
| 149 }; |
| 150 |
35 namespace { | 151 namespace { |
36 | 152 |
37 bool MaybeForwardToServiceWorker(ServiceWorkerURLRequestJob* job, | 153 bool MaybeForwardToServiceWorker(JobWrapper* job, |
38 const ServiceWorkerVersion* version) { | 154 const ServiceWorkerVersion* version) { |
39 DCHECK(job); | 155 DCHECK(job); |
40 DCHECK(version); | 156 DCHECK(version); |
41 DCHECK_NE(version->fetch_handler_existence(), | 157 DCHECK_NE(version->fetch_handler_existence(), |
42 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 158 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
43 if (version->fetch_handler_existence() == | 159 if (version->fetch_handler_existence() == |
44 ServiceWorkerVersion::FetchHandlerExistence::EXISTS) { | 160 ServiceWorkerVersion::FetchHandlerExistence::EXISTS) { |
45 job->ForwardToServiceWorker(); | 161 job->ForwardToServiceWorker(); |
46 return true; | 162 return true; |
47 } | 163 } |
48 | 164 |
49 job->FallbackToNetworkOrRenderer(); | 165 job->FallbackToNetworkOrRenderer(); |
50 return false; | 166 return false; |
51 } | 167 } |
52 | 168 |
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 | 169 } // namespace |
62 | 170 |
63 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( | 171 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( |
64 base::WeakPtr<ServiceWorkerContextCore> context, | 172 base::WeakPtr<ServiceWorkerContextCore> context, |
65 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 173 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
66 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 174 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
67 FetchRequestMode request_mode, | 175 FetchRequestMode request_mode, |
68 FetchCredentialsMode credentials_mode, | 176 FetchCredentialsMode credentials_mode, |
69 FetchRedirectMode redirect_mode, | 177 FetchRedirectMode redirect_mode, |
70 ResourceType resource_type, | 178 ResourceType resource_type, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 241 } |
134 | 242 |
135 // It's for original request (A) or redirect case (B-a or B-b). | 243 // It's for original request (A) or redirect case (B-a or B-b). |
136 std::unique_ptr<ServiceWorkerURLRequestJob> job( | 244 std::unique_ptr<ServiceWorkerURLRequestJob> job( |
137 new ServiceWorkerURLRequestJob( | 245 new ServiceWorkerURLRequestJob( |
138 request, network_delegate, provider_host_->client_uuid(), | 246 request, network_delegate, provider_host_->client_uuid(), |
139 blob_storage_context_, resource_context, request_mode_, | 247 blob_storage_context_, resource_context, request_mode_, |
140 credentials_mode_, redirect_mode_, resource_type_, | 248 credentials_mode_, redirect_mode_, resource_type_, |
141 request_context_type_, frame_type_, body_, | 249 request_context_type_, frame_type_, body_, |
142 ServiceWorkerFetchType::FETCH, base::nullopt, this)); | 250 ServiceWorkerFetchType::FETCH, base::nullopt, this)); |
143 job_ = job->GetWeakPtr(); | 251 job_ = base::MakeUnique<JobWrapper>(job->GetWeakPtr()); |
144 | 252 |
145 resource_context_ = resource_context; | 253 resource_context_ = resource_context; |
146 | 254 |
147 if (is_main_resource_load_) | 255 if (is_main_resource_load_) |
148 PrepareForMainResource(request); | 256 PrepareForMainResource(request->url(), request->first_party_for_cookies()); |
149 else | 257 else |
150 PrepareForSubResource(); | 258 PrepareForSubResource(); |
151 | 259 |
152 if (job_->ShouldFallbackToNetwork()) { | 260 if (job_->ShouldFallbackToNetwork()) { |
153 // If we know we can fallback to network at this point (in case | 261 // 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 | 262 // the storage lookup returned immediately), just destroy the job and return |
155 // NULL here to fallback to network. | 263 // NULL here to fallback to network. |
156 | 264 |
157 // If this is a subresource request, all subsequent requests should also use | 265 // If this is a subresource request, all subsequent requests should also use |
158 // the network. | 266 // the network. |
159 if (!is_main_resource_load_) | 267 if (!is_main_resource_load_) |
160 use_network_ = true; | 268 use_network_ = true; |
161 | 269 |
162 job.reset(); | 270 job.reset(); |
163 ClearJob(); | 271 ClearJob(); |
164 } | 272 } |
165 | 273 |
166 return job.release(); | 274 return job.release(); |
167 } | 275 } |
168 | 276 |
| 277 mojom::URLLoaderFactoryPtrInfo |
| 278 ServiceWorkerControlleeRequestHandler::MaybeCreateURLLoader( |
| 279 const ResourceRequest& request, |
| 280 ResourceContext* resource_context, |
| 281 std::unique_ptr<ServiceWorkerRequestHandler> request_handler) { |
| 282 ClearJob(); |
| 283 // TODO(scottmg): |
| 284 // ServiceWorkerResponseInfo::ResetDataForRequest(request); |
| 285 |
| 286 if (!context_ || !provider_host_) { |
| 287 // We can't do anything other than to fall back to network. |
| 288 return mojom::URLLoaderFactoryPtrInfo(); |
| 289 } |
| 290 |
| 291 // This may get called multiple times for original and redirect requests: |
| 292 // A. original request case: use_network_ is false, no previous location info. |
| 293 // B. redirect or restarted request case: |
| 294 // a) use_network_ is false if the previous location was forwarded to SW. |
| 295 // b) use_network_ is false if the previous location was fallback. |
| 296 // c) use_network_ is true if additional restart was required to fall back. |
| 297 |
| 298 // Fall back to network. (Case B-c) |
| 299 if (use_network_) { |
| 300 // Once a subresource request has fallen back to the network once, it will |
| 301 // never be handled by a service worker. This is not true of main frame |
| 302 // requests. |
| 303 if (is_main_resource_load_) |
| 304 use_network_ = false; |
| 305 return mojom::URLLoaderFactoryPtrInfo(); |
| 306 } |
| 307 |
| 308 mojom::URLLoaderFactoryPtr url_loader_factory; |
| 309 auto factory_impl = |
| 310 base::MakeUnique<ServiceWorkerControlleeURLLoaderFactory>(); |
| 311 auto* factory = factory_impl.get(); |
| 312 mojo::MakeStrongBinding(std::move(factory_impl), |
| 313 mojo::MakeRequest(&url_loader_factory)); |
| 314 |
| 315 // It's for original request (A) or redirect case (B-a or B-b). |
| 316 job_ = base::MakeUnique<JobWrapper>(factory); |
| 317 |
| 318 resource_context_ = resource_context; |
| 319 |
| 320 if (is_main_resource_load_) |
| 321 PrepareForMainResource(request.url, request.first_party_for_cookies); |
| 322 else |
| 323 PrepareForSubResource(); |
| 324 |
| 325 if (job_->ShouldFallbackToNetwork()) { |
| 326 // If we know we can fallback to network at this point (in case |
| 327 // the storage lookup returned immediately), just destroy the job and return |
| 328 // NULL here to fallback to network. |
| 329 |
| 330 // If this is a subresource request, all subsequent requests should also use |
| 331 // the network. |
| 332 if (!is_main_resource_load_) |
| 333 use_network_ = true; |
| 334 |
| 335 ClearJob(); |
| 336 return mojom::URLLoaderFactoryPtrInfo(); |
| 337 } |
| 338 |
| 339 return url_loader_factory.PassInterface(); |
| 340 } |
| 341 |
169 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource( | 342 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource( |
170 const net::URLRequest* request) { | 343 const GURL& url, |
| 344 const GURL& first_party_for_cookies) { |
171 DCHECK(job_.get()); | 345 DCHECK(job_.get()); |
172 DCHECK(context_); | 346 DCHECK(context_); |
173 DCHECK(provider_host_); | 347 DCHECK(provider_host_); |
174 TRACE_EVENT_ASYNC_BEGIN1( | 348 TRACE_EVENT_ASYNC_BEGIN1( |
175 "ServiceWorker", | 349 "ServiceWorker", |
176 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 350 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
177 job_.get(), | 351 job_.get(), "URL", url.spec()); |
178 "URL", request->url().spec()); | |
179 // The corresponding provider_host may already have associated a registration | 352 // The corresponding provider_host may already have associated a registration |
180 // in redirect case, unassociate it now. | 353 // in redirect case, unassociate it now. |
181 provider_host_->DisassociateRegistration(); | 354 provider_host_->DisassociateRegistration(); |
182 | 355 |
183 // Also prevent a registrater job for establishing an association to a new | 356 // Also prevent a registrater job for establishing an association to a new |
184 // registration while we're finding an existing registration. | 357 // registration while we're finding an existing registration. |
185 provider_host_->SetAllowAssociation(false); | 358 provider_host_->SetAllowAssociation(false); |
186 | 359 |
187 stripped_url_ = net::SimplifyUrlForRequest(request->url()); | 360 stripped_url_ = net::SimplifyUrlForRequest(url); |
188 provider_host_->SetDocumentUrl(stripped_url_); | 361 provider_host_->SetDocumentUrl(stripped_url_); |
189 provider_host_->SetTopmostFrameUrl(request->first_party_for_cookies()); | 362 provider_host_->SetTopmostFrameUrl(first_party_for_cookies); |
190 context_->storage()->FindRegistrationForDocument( | 363 context_->storage()->FindRegistrationForDocument( |
191 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, | 364 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, |
192 weak_factory_.GetWeakPtr())); | 365 weak_factory_.GetWeakPtr())); |
193 } | 366 } |
194 | 367 |
195 void ServiceWorkerControlleeRequestHandler:: | 368 void ServiceWorkerControlleeRequestHandler:: |
196 DidLookupRegistrationForMainResource( | 369 DidLookupRegistrationForMainResource( |
197 ServiceWorkerStatusCode status, | 370 ServiceWorkerStatusCode status, |
198 scoped_refptr<ServiceWorkerRegistration> registration) { | 371 scoped_refptr<ServiceWorkerRegistration> registration) { |
199 // The job may have been canceled and then destroyed before this was invoked. | 372 // The job may have been canceled and then destroyed before this was invoked. |
200 if (!job_) | 373 if (job_->WasCanceled()) |
201 return; | 374 return; |
202 | 375 |
203 const bool need_to_update = !force_update_started_ && registration && | 376 const bool need_to_update = !force_update_started_ && registration && |
204 context_->force_update_on_page_load(); | 377 context_->force_update_on_page_load(); |
205 | 378 |
206 if (provider_host_ && !need_to_update) | 379 if (provider_host_ && !need_to_update) |
207 provider_host_->SetAllowAssociation(true); | 380 provider_host_->SetAllowAssociation(true); |
208 if (status != SERVICE_WORKER_OK || !provider_host_ || !context_) { | 381 if (status != SERVICE_WORKER_OK || !provider_host_ || !context_) { |
209 job_->FallbackToNetwork(); | 382 job_->FallbackToNetwork(); |
210 TRACE_EVENT_ASYNC_END1( | 383 TRACE_EVENT_ASYNC_END1( |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 "Status", status, | 478 "Status", status, |
306 "Info", | 479 "Info", |
307 "ServiceWorkerVersion is not available, so falling back to network"); | 480 "ServiceWorkerVersion is not available, so falling back to network"); |
308 return; | 481 return; |
309 } | 482 } |
310 | 483 |
311 DCHECK_NE(active_version->fetch_handler_existence(), | 484 DCHECK_NE(active_version->fetch_handler_existence(), |
312 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 485 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
313 ServiceWorkerMetrics::CountControlledPageLoad( | 486 ServiceWorkerMetrics::CountControlledPageLoad( |
314 active_version->site_for_uma(), stripped_url_, is_main_frame_load_, | 487 active_version->site_for_uma(), stripped_url_, is_main_frame_load_, |
315 GetPageTransition(job_->request()), job_->request()->url_chain().size()); | 488 job_->GetPageTransition(), job_->GetURLChainSize()); |
316 | 489 |
317 bool is_forwarded = | 490 bool is_forwarded = |
318 MaybeForwardToServiceWorker(job_.get(), active_version.get()); | 491 MaybeForwardToServiceWorker(job_.get(), active_version.get()); |
319 | 492 |
320 TRACE_EVENT_ASYNC_END2( | 493 TRACE_EVENT_ASYNC_END2( |
321 "ServiceWorker", | 494 "ServiceWorker", |
322 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", | 495 "ServiceWorkerControlleeRequestHandler::PrepareForMainResource", |
323 job_.get(), "Status", status, "Info", | 496 job_.get(), "Status", status, "Info", |
324 (is_forwarded) ? "Forwarded to the ServiceWorker" | 497 (is_forwarded) ? "Forwarded to the ServiceWorker" |
325 : "Skipped the ServiceWorker which has no fetch handler"); | 498 : "Skipped the ServiceWorker which has no fetch handler"); |
326 } | 499 } |
327 | 500 |
328 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( | 501 void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged( |
329 ServiceWorkerRegistration* registration, | 502 ServiceWorkerRegistration* registration, |
330 ServiceWorkerVersion* version) { | 503 ServiceWorkerVersion* version) { |
331 // The job may have been canceled and then destroyed before this was invoked. | 504 // The job may have been canceled and then destroyed before this was invoked. |
332 if (!job_) | 505 if (job_->WasCanceled()) |
333 return; | 506 return; |
334 | 507 |
335 if (provider_host_) | 508 if (provider_host_) |
336 provider_host_->SetAllowAssociation(true); | 509 provider_host_->SetAllowAssociation(true); |
337 if (version != registration->active_version() || | 510 if (version != registration->active_version() || |
338 version->status() != ServiceWorkerVersion::ACTIVATED || | 511 version->status() != ServiceWorkerVersion::ACTIVATED || |
339 !provider_host_) { | 512 !provider_host_) { |
340 job_->FallbackToNetwork(); | 513 job_->FallbackToNetwork(); |
341 return; | 514 return; |
342 } | 515 } |
343 | 516 |
344 DCHECK_NE(version->fetch_handler_existence(), | 517 DCHECK_NE(version->fetch_handler_existence(), |
345 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); | 518 ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); |
346 ServiceWorkerMetrics::CountControlledPageLoad( | 519 ServiceWorkerMetrics::CountControlledPageLoad( |
347 version->site_for_uma(), stripped_url_, is_main_frame_load_, | 520 version->site_for_uma(), stripped_url_, is_main_frame_load_, |
348 GetPageTransition(job_->request()), job_->request()->url_chain().size()); | 521 job_->GetPageTransition(), job_->GetURLChainSize()); |
349 | 522 |
350 provider_host_->AssociateRegistration(registration, | 523 provider_host_->AssociateRegistration(registration, |
351 false /* notify_controllerchange */); | 524 false /* notify_controllerchange */); |
352 | 525 |
353 MaybeForwardToServiceWorker(job_.get(), version); | 526 MaybeForwardToServiceWorker(job_.get(), version); |
354 } | 527 } |
355 | 528 |
356 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( | 529 void ServiceWorkerControlleeRequestHandler::DidUpdateRegistration( |
357 const scoped_refptr<ServiceWorkerRegistration>& original_registration, | 530 const scoped_refptr<ServiceWorkerRegistration>& original_registration, |
358 ServiceWorkerStatusCode status, | 531 ServiceWorkerStatusCode status, |
359 const std::string& status_message, | 532 const std::string& status_message, |
360 int64_t registration_id) { | 533 int64_t registration_id) { |
361 DCHECK(force_update_started_); | 534 DCHECK(force_update_started_); |
362 | 535 |
363 // The job may have been canceled and then destroyed before this was invoked. | 536 // The job may have been canceled and then destroyed before this was invoked. |
364 if (!job_) | 537 if (job_->WasCanceled()) |
365 return; | 538 return; |
366 | 539 |
367 if (!context_) { | 540 if (!context_) { |
368 job_->FallbackToNetwork(); | 541 job_->FallbackToNetwork(); |
369 return; | 542 return; |
370 } | 543 } |
371 if (status != SERVICE_WORKER_OK || | 544 if (status != SERVICE_WORKER_OK || |
372 !original_registration->installing_version()) { | 545 !original_registration->installing_version()) { |
373 // Update failed. Look up the registration again since the original | 546 // Update failed. Look up the registration again since the original |
374 // registration was possibly unregistered in the meantime. | 547 // registration was possibly unregistered in the meantime. |
375 context_->storage()->FindRegistrationForDocument( | 548 context_->storage()->FindRegistrationForDocument( |
376 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, | 549 stripped_url_, base::Bind(&self::DidLookupRegistrationForMainResource, |
377 weak_factory_.GetWeakPtr())); | 550 weak_factory_.GetWeakPtr())); |
378 return; | 551 return; |
379 } | 552 } |
380 DCHECK_EQ(original_registration->id(), registration_id); | 553 DCHECK_EQ(original_registration->id(), registration_id); |
381 scoped_refptr<ServiceWorkerVersion> new_version = | 554 scoped_refptr<ServiceWorkerVersion> new_version = |
382 original_registration->installing_version(); | 555 original_registration->installing_version(); |
383 new_version->ReportForceUpdateToDevTools(); | 556 new_version->ReportForceUpdateToDevTools(); |
384 new_version->set_skip_waiting(true); | 557 new_version->set_skip_waiting(true); |
385 new_version->RegisterStatusChangeCallback(base::Bind( | 558 new_version->RegisterStatusChangeCallback(base::Bind( |
386 &self::OnUpdatedVersionStatusChanged, weak_factory_.GetWeakPtr(), | 559 &self::OnUpdatedVersionStatusChanged, weak_factory_.GetWeakPtr(), |
387 original_registration, new_version)); | 560 original_registration, new_version)); |
388 } | 561 } |
389 | 562 |
390 void ServiceWorkerControlleeRequestHandler::OnUpdatedVersionStatusChanged( | 563 void ServiceWorkerControlleeRequestHandler::OnUpdatedVersionStatusChanged( |
391 const scoped_refptr<ServiceWorkerRegistration>& registration, | 564 const scoped_refptr<ServiceWorkerRegistration>& registration, |
392 const scoped_refptr<ServiceWorkerVersion>& version) { | 565 const scoped_refptr<ServiceWorkerVersion>& version) { |
393 // The job may have been canceled and then destroyed before this was invoked. | 566 // The job may have been canceled and then destroyed before this was invoked. |
394 if (!job_) | 567 if (job_->WasCanceled()) |
395 return; | 568 return; |
396 | 569 |
397 if (!context_) { | 570 if (!context_) { |
398 job_->FallbackToNetwork(); | 571 job_->FallbackToNetwork(); |
399 return; | 572 return; |
400 } | 573 } |
401 if (version->status() == ServiceWorkerVersion::ACTIVATED || | 574 if (version->status() == ServiceWorkerVersion::ACTIVATED || |
402 version->status() == ServiceWorkerVersion::REDUNDANT) { | 575 version->status() == ServiceWorkerVersion::REDUNDANT) { |
403 // When the status is REDUNDANT, the update failed (eg: script error), we | 576 // When the status is REDUNDANT, the update failed (eg: script error), we |
404 // continue with the incumbent version. | 577 // continue with the incumbent version. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 DCHECK(provider_host_); | 641 DCHECK(provider_host_); |
469 // Detach the controller so subresource requests also skip the worker. | 642 // Detach the controller so subresource requests also skip the worker. |
470 provider_host_->NotifyControllerLost(); | 643 provider_host_->NotifyControllerLost(); |
471 } | 644 } |
472 | 645 |
473 void ServiceWorkerControlleeRequestHandler::ClearJob() { | 646 void ServiceWorkerControlleeRequestHandler::ClearJob() { |
474 job_.reset(); | 647 job_.reset(); |
475 } | 648 } |
476 | 649 |
477 } // namespace content | 650 } // namespace content |
OLD | NEW |