| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 if (!context_wrapper) | 85 if (!context_wrapper) |
| 86 return; | 86 return; |
| 87 | 87 |
| 88 if (skip_service_worker == SkipServiceWorker::ALL) | 88 if (skip_service_worker == SkipServiceWorker::ALL) |
| 89 return; | 89 return; |
| 90 | 90 |
| 91 if (!initiated_in_secure_context) | 91 if (!initiated_in_secure_context) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) | 94 // ServiceWorkerUtils::IsMainResource doesn't consider all worker types to |
| 95 // be main resources. This code shouldn't handle any main resource requests |
| 96 // though, so explicitly exclude the extra worker types. |
| 97 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || |
| 98 resource_type == RESOURCE_TYPE_WORKER || |
| 99 resource_type == RESOURCE_TYPE_SERVICE_WORKER) |
| 95 return; | 100 return; |
| 96 | 101 |
| 97 if (request->initiator().has_value() && | 102 if (request->initiator().has_value() && |
| 98 request->initiator()->IsSameOriginWith(url::Origin(request->url()))) { | 103 request->initiator()->IsSameOriginWith(url::Origin(request->url()))) { |
| 99 return; | 104 return; |
| 100 } | 105 } |
| 101 | 106 |
| 107 ServiceWorkerProviderHost* provider_host = |
| 108 context_wrapper->context()->GetProviderHost(process_id, provider_id); |
| 109 if (!provider_host || !provider_host->IsContextAlive()) |
| 110 return; |
| 111 |
| 112 base::Optional<base::TimeDelta> timeout; |
| 113 if (provider_host->IsHostToRunningServiceWorker()) { |
| 114 timeout = base::make_optional( |
| 115 provider_host->running_hosted_version()->remaining_timeout()); |
| 116 } |
| 117 |
| 102 if (!context_wrapper->OriginHasForeignFetchRegistrations( | 118 if (!context_wrapper->OriginHasForeignFetchRegistrations( |
| 103 request->url().GetOrigin())) { | 119 request->url().GetOrigin())) { |
| 104 return; | 120 return; |
| 105 } | 121 } |
| 106 | 122 |
| 107 // Any more precise checks to see if the request should be intercepted are | 123 // Any more precise checks to see if the request should be intercepted are |
| 108 // asynchronous, so just create our handler in all cases. | 124 // asynchronous, so just create our handler in all cases. |
| 109 std::unique_ptr<ForeignFetchRequestHandler> handler( | 125 std::unique_ptr<ForeignFetchRequestHandler> handler( |
| 110 new ForeignFetchRequestHandler( | 126 new ForeignFetchRequestHandler( |
| 111 context_wrapper, blob_storage_context->AsWeakPtr(), request_mode, | 127 context_wrapper, blob_storage_context->AsWeakPtr(), request_mode, |
| 112 credentials_mode, redirect_mode, resource_type, request_context_type, | 128 credentials_mode, redirect_mode, resource_type, request_context_type, |
| 113 frame_type, body)); | 129 frame_type, body, timeout)); |
| 114 request->SetUserData(&kUserDataKey, handler.release()); | 130 request->SetUserData(&kUserDataKey, handler.release()); |
| 115 } | 131 } |
| 116 | 132 |
| 117 ForeignFetchRequestHandler* ForeignFetchRequestHandler::GetHandler( | 133 ForeignFetchRequestHandler* ForeignFetchRequestHandler::GetHandler( |
| 118 net::URLRequest* request) { | 134 net::URLRequest* request) { |
| 119 return static_cast<ForeignFetchRequestHandler*>( | 135 return static_cast<ForeignFetchRequestHandler*>( |
| 120 request->GetUserData(&kUserDataKey)); | 136 request->GetUserData(&kUserDataKey)); |
| 121 } | 137 } |
| 122 | 138 |
| 123 std::unique_ptr<net::URLRequestInterceptor> | 139 std::unique_ptr<net::URLRequestInterceptor> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 return nullptr; | 172 return nullptr; |
| 157 } | 173 } |
| 158 | 174 |
| 159 // It's for original request (A) or redirect case (B-a or B-b). | 175 // It's for original request (A) or redirect case (B-a or B-b). |
| 160 DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker()); | 176 DCHECK(!job_.get() || job_->ShouldForwardToServiceWorker()); |
| 161 | 177 |
| 162 ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob( | 178 ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob( |
| 163 request, network_delegate, std::string(), blob_storage_context_, | 179 request, network_delegate, std::string(), blob_storage_context_, |
| 164 resource_context, request_mode_, credentials_mode_, redirect_mode_, | 180 resource_context, request_mode_, credentials_mode_, redirect_mode_, |
| 165 resource_type_, request_context_type_, frame_type_, body_, | 181 resource_type_, request_context_type_, frame_type_, body_, |
| 166 ServiceWorkerFetchType::FOREIGN_FETCH, this); | 182 ServiceWorkerFetchType::FOREIGN_FETCH, timeout_, this); |
| 167 job_ = job->GetWeakPtr(); | 183 job_ = job->GetWeakPtr(); |
| 168 resource_context_ = resource_context; | 184 resource_context_ = resource_context; |
| 169 | 185 |
| 170 context_->FindReadyRegistrationForDocument( | 186 context_->FindReadyRegistrationForDocument( |
| 171 request->url(), | 187 request->url(), |
| 172 base::Bind(&ForeignFetchRequestHandler::DidFindRegistration, | 188 base::Bind(&ForeignFetchRequestHandler::DidFindRegistration, |
| 173 weak_factory_.GetWeakPtr(), job_)); | 189 weak_factory_.GetWeakPtr(), job_)); |
| 174 | 190 |
| 175 return job_.get(); | 191 return job_.get(); |
| 176 } | 192 } |
| 177 | 193 |
| 178 ForeignFetchRequestHandler::ForeignFetchRequestHandler( | 194 ForeignFetchRequestHandler::ForeignFetchRequestHandler( |
| 179 ServiceWorkerContextWrapper* context, | 195 ServiceWorkerContextWrapper* context, |
| 180 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 196 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
| 181 FetchRequestMode request_mode, | 197 FetchRequestMode request_mode, |
| 182 FetchCredentialsMode credentials_mode, | 198 FetchCredentialsMode credentials_mode, |
| 183 FetchRedirectMode redirect_mode, | 199 FetchRedirectMode redirect_mode, |
| 184 ResourceType resource_type, | 200 ResourceType resource_type, |
| 185 RequestContextType request_context_type, | 201 RequestContextType request_context_type, |
| 186 RequestContextFrameType frame_type, | 202 RequestContextFrameType frame_type, |
| 187 scoped_refptr<ResourceRequestBodyImpl> body) | 203 scoped_refptr<ResourceRequestBodyImpl> body, |
| 204 const base::Optional<base::TimeDelta>& timeout) |
| 188 : context_(context), | 205 : context_(context), |
| 189 blob_storage_context_(blob_storage_context), | 206 blob_storage_context_(blob_storage_context), |
| 190 resource_type_(resource_type), | 207 resource_type_(resource_type), |
| 191 request_mode_(request_mode), | 208 request_mode_(request_mode), |
| 192 credentials_mode_(credentials_mode), | 209 credentials_mode_(credentials_mode), |
| 193 redirect_mode_(redirect_mode), | 210 redirect_mode_(redirect_mode), |
| 194 request_context_type_(request_context_type), | 211 request_context_type_(request_context_type), |
| 195 frame_type_(frame_type), | 212 frame_type_(frame_type), |
| 196 body_(body), | 213 body_(body), |
| 214 timeout_(timeout), |
| 197 weak_factory_(this) {} | 215 weak_factory_(this) {} |
| 198 | 216 |
| 199 void ForeignFetchRequestHandler::DidFindRegistration( | 217 void ForeignFetchRequestHandler::DidFindRegistration( |
| 200 const base::WeakPtr<ServiceWorkerURLRequestJob>& job, | 218 const base::WeakPtr<ServiceWorkerURLRequestJob>& job, |
| 201 ServiceWorkerStatusCode status, | 219 ServiceWorkerStatusCode status, |
| 202 scoped_refptr<ServiceWorkerRegistration> registration) { | 220 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 203 if (!job || job.get() != job_.get()) { | 221 if (!job || job.get() != job_.get()) { |
| 204 // No more job to handle, or job changed somehow, so just return. | 222 // No more job to handle, or job changed somehow, so just return. |
| 205 return; | 223 return; |
| 206 } | 224 } |
| (...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) | 303 // 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 | 304 // and the main script was not loaded yet. In this case, we can't check the |
| 287 // origin trial token. | 305 // origin trial token. |
| 288 if (!active_version->origin_trial_tokens()) | 306 if (!active_version->origin_trial_tokens()) |
| 289 return true; | 307 return true; |
| 290 const auto& token_map = *active_version->origin_trial_tokens(); | 308 const auto& token_map = *active_version->origin_trial_tokens(); |
| 291 return base::ContainsKey(token_map, "ForeignFetch"); | 309 return base::ContainsKey(token_map, "ForeignFetch"); |
| 292 } | 310 } |
| 293 | 311 |
| 294 } // namespace content | 312 } // namespace content |
| OLD | NEW |