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