| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/foreign_fetch_request_handler.h" | 5 #include "content/browser/service_worker/foreign_fetch_request_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) | 94 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) |
| 95 return; | 95 return; |
| 96 | 96 |
| 97 if (request->initiator().has_value() && | 97 if (request->initiator().has_value() && |
| 98 request->initiator()->IsSameOriginWith(url::Origin(request->url()))) { | 98 request->initiator()->IsSameOriginWith(url::Origin(request->url()))) { |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 | 101 |
| 102 ServiceWorkerProviderHost* provider_host = |
| 103 context_wrapper->context()->GetProviderHost(process_id, provider_id); |
| 104 if (!provider_host || !provider_host->IsContextAlive()) |
| 105 return; |
| 106 |
| 107 base::Optional<base::TimeDelta> timeout; |
| 108 if (provider_host->IsHostToRunningServiceWorker()) { |
| 109 timeout = base::make_optional( |
| 110 provider_host->running_hosted_version()->remaining_timeout()); |
| 111 } |
| 112 |
| 102 if (!context_wrapper->OriginHasForeignFetchRegistrations( | 113 if (!context_wrapper->OriginHasForeignFetchRegistrations( |
| 103 request->url().GetOrigin())) { | 114 request->url().GetOrigin())) { |
| 104 return; | 115 return; |
| 105 } | 116 } |
| 106 | 117 |
| 107 // Any more precise checks to see if the request should be intercepted are | 118 // Any more precise checks to see if the request should be intercepted are |
| 108 // asynchronous, so just create our handler in all cases. | 119 // asynchronous, so just create our handler in all cases. |
| 109 std::unique_ptr<ForeignFetchRequestHandler> handler( | 120 std::unique_ptr<ForeignFetchRequestHandler> handler( |
| 110 new ForeignFetchRequestHandler( | 121 new ForeignFetchRequestHandler( |
| 111 context_wrapper, blob_storage_context->AsWeakPtr(), request_mode, | 122 context_wrapper, blob_storage_context->AsWeakPtr(), request_mode, |
| 112 credentials_mode, redirect_mode, resource_type, request_context_type, | 123 credentials_mode, redirect_mode, resource_type, request_context_type, |
| 113 frame_type, body)); | 124 frame_type, body, timeout)); |
| 114 request->SetUserData(&kUserDataKey, handler.release()); | 125 request->SetUserData(&kUserDataKey, handler.release()); |
| 115 } | 126 } |
| 116 | 127 |
| 117 ForeignFetchRequestHandler* ForeignFetchRequestHandler::GetHandler( | 128 ForeignFetchRequestHandler* ForeignFetchRequestHandler::GetHandler( |
| 118 net::URLRequest* request) { | 129 net::URLRequest* request) { |
| 119 return static_cast<ForeignFetchRequestHandler*>( | 130 return static_cast<ForeignFetchRequestHandler*>( |
| 120 request->GetUserData(&kUserDataKey)); | 131 request->GetUserData(&kUserDataKey)); |
| 121 } | 132 } |
| 122 | 133 |
| 123 std::unique_ptr<net::URLRequestInterceptor> | 134 std::unique_ptr<net::URLRequestInterceptor> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 return nullptr; | 167 return nullptr; |
| 157 } | 168 } |
| 158 | 169 |
| 159 // It's for original request (A) or redirect case (B-a or B-b). | 170 // It's for original request (A) or redirect case (B-a or B-b). |
| 160 DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker()); | 171 DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker()); |
| 161 | 172 |
| 162 ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob( | 173 ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob( |
| 163 request, network_delegate, std::string(), blob_storage_context_, | 174 request, network_delegate, std::string(), blob_storage_context_, |
| 164 resource_context, request_mode_, credentials_mode_, redirect_mode_, | 175 resource_context, request_mode_, credentials_mode_, redirect_mode_, |
| 165 resource_type_, request_context_type_, frame_type_, body_, | 176 resource_type_, request_context_type_, frame_type_, body_, |
| 166 ServiceWorkerFetchType::FOREIGN_FETCH, this); | 177 ServiceWorkerFetchType::FOREIGN_FETCH, timeout_, this); |
| 167 job_ = job->GetWeakPtr(); | 178 job_ = job->GetWeakPtr(); |
| 168 resource_context_ = resource_context; | 179 resource_context_ = resource_context; |
| 169 | 180 |
| 170 context_->FindReadyRegistrationForDocument( | 181 context_->FindReadyRegistrationForDocument( |
| 171 request->url(), | 182 request->url(), |
| 172 base::Bind(&ForeignFetchRequestHandler::DidFindRegistration, | 183 base::Bind(&ForeignFetchRequestHandler::DidFindRegistration, |
| 173 weak_factory_.GetWeakPtr(), job_)); | 184 weak_factory_.GetWeakPtr(), job_)); |
| 174 | 185 |
| 175 return job_.get(); | 186 return job_.get(); |
| 176 } | 187 } |
| 177 | 188 |
| 178 ForeignFetchRequestHandler::ForeignFetchRequestHandler( | 189 ForeignFetchRequestHandler::ForeignFetchRequestHandler( |
| 179 ServiceWorkerContextWrapper* context, | 190 ServiceWorkerContextWrapper* context, |
| 180 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 191 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
| 181 FetchRequestMode request_mode, | 192 FetchRequestMode request_mode, |
| 182 FetchCredentialsMode credentials_mode, | 193 FetchCredentialsMode credentials_mode, |
| 183 FetchRedirectMode redirect_mode, | 194 FetchRedirectMode redirect_mode, |
| 184 ResourceType resource_type, | 195 ResourceType resource_type, |
| 185 RequestContextType request_context_type, | 196 RequestContextType request_context_type, |
| 186 RequestContextFrameType frame_type, | 197 RequestContextFrameType frame_type, |
| 187 scoped_refptr<ResourceRequestBodyImpl> body) | 198 scoped_refptr<ResourceRequestBodyImpl> body, |
| 199 const base::Optional<base::TimeDelta>& timeout) |
| 188 : context_(context), | 200 : context_(context), |
| 189 blob_storage_context_(blob_storage_context), | 201 blob_storage_context_(blob_storage_context), |
| 190 resource_type_(resource_type), | 202 resource_type_(resource_type), |
| 191 request_mode_(request_mode), | 203 request_mode_(request_mode), |
| 192 credentials_mode_(credentials_mode), | 204 credentials_mode_(credentials_mode), |
| 193 redirect_mode_(redirect_mode), | 205 redirect_mode_(redirect_mode), |
| 194 request_context_type_(request_context_type), | 206 request_context_type_(request_context_type), |
| 195 frame_type_(frame_type), | 207 frame_type_(frame_type), |
| 196 body_(body), | 208 body_(body), |
| 209 timeout_(timeout), |
| 197 weak_factory_(this) {} | 210 weak_factory_(this) {} |
| 198 | 211 |
| 199 void ForeignFetchRequestHandler::DidFindRegistration( | 212 void ForeignFetchRequestHandler::DidFindRegistration( |
| 200 const base::WeakPtr<ServiceWorkerURLRequestJob>& job, | 213 const base::WeakPtr<ServiceWorkerURLRequestJob>& job, |
| 201 ServiceWorkerStatusCode status, | 214 ServiceWorkerStatusCode status, |
| 202 scoped_refptr<ServiceWorkerRegistration> registration) { | 215 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 203 if (!job || job.get() != job_.get()) { | 216 if (!job || job.get() != job_.get()) { |
| 204 // No more job to handle, or job changed somehow, so just return. | 217 // No more job to handle, or job changed somehow, so just return. |
| 205 return; | 218 return; |
| 206 } | 219 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 // The worker entry in the database was written by old version Chrome (< M56) | 298 // The worker entry in the database was written by old version Chrome (< M56) |
| 286 // and the main script was not loaded yet. In this case, we can't check the | 299 // and the main script was not loaded yet. In this case, we can't check the |
| 287 // origin trial token. | 300 // origin trial token. |
| 288 if (!active_version->origin_trial_tokens()) | 301 if (!active_version->origin_trial_tokens()) |
| 289 return true; | 302 return true; |
| 290 const auto& token_map = *active_version->origin_trial_tokens(); | 303 const auto& token_map = *active_version->origin_trial_tokens(); |
| 291 return base::ContainsKey(token_map, "ForeignFetch"); | 304 return base::ContainsKey(token_map, "ForeignFetch"); |
| 292 } | 305 } |
| 293 | 306 |
| 294 } // namespace content | 307 } // namespace content |
| OLD | NEW |