| 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_request_handler.h" | 5 #include "content/browser/service_worker/service_worker_request_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 return NULL; | 48 return NULL; |
| 49 return handler->MaybeCreateJob( | 49 return handler->MaybeCreateJob( |
| 50 request, network_delegate, resource_context_); | 50 request, network_delegate, resource_context_); |
| 51 } | 51 } |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 ResourceContext* resource_context_; | 54 ResourceContext* resource_context_; |
| 55 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); | 55 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 void FinalizeHandlerInitialization( | |
| 59 net::URLRequest* request, | |
| 60 ServiceWorkerProviderHost* provider_host, | |
| 61 storage::BlobStorageContext* blob_storage_context, | |
| 62 bool skip_service_worker, | |
| 63 FetchRequestMode request_mode, | |
| 64 FetchCredentialsMode credentials_mode, | |
| 65 FetchRedirectMode redirect_mode, | |
| 66 ResourceType resource_type, | |
| 67 RequestContextType request_context_type, | |
| 68 RequestContextFrameType frame_type, | |
| 69 scoped_refptr<ResourceRequestBodyImpl> body) { | |
| 70 std::unique_ptr<ServiceWorkerRequestHandler> handler( | |
| 71 provider_host->CreateRequestHandler( | |
| 72 request_mode, credentials_mode, redirect_mode, resource_type, | |
| 73 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), | |
| 74 body, skip_service_worker)); | |
| 75 if (!handler) | |
| 76 return; | |
| 77 | |
| 78 request->SetUserData(&kUserDataKey, std::move(handler)); | |
| 79 } | |
| 80 | |
| 81 } // namespace | 58 } // namespace |
| 82 | 59 |
| 83 // PlzNavigate | 60 // PlzNavigate |
| 84 void ServiceWorkerRequestHandler::InitializeForNavigation( | 61 void ServiceWorkerRequestHandler::InitializeForNavigation( |
| 85 net::URLRequest* request, | 62 net::URLRequest* url_request, |
| 86 ServiceWorkerNavigationHandleCore* navigation_handle_core, | 63 ServiceWorkerNavigationHandleCore* navigation_handle_core, |
| 87 storage::BlobStorageContext* blob_storage_context, | 64 storage::BlobStorageContext* blob_storage_context, |
| 88 bool skip_service_worker, | 65 bool skip_service_worker, |
| 66 ResourceType resource_type, |
| 67 RequestContextType request_context_type, |
| 68 RequestContextFrameType frame_type, |
| 69 bool is_parent_frame_secure, |
| 70 scoped_refptr<ResourceRequestBodyImpl> body, |
| 71 const base::Callback<WebContents*(void)>& web_contents_getter) { |
| 72 auto handler = InitializeForNavigationInternal( |
| 73 url_request->url(), navigation_handle_core, blob_storage_context, |
| 74 skip_service_worker, resource_type, request_context_type, frame_type, |
| 75 is_parent_frame_secure, body, web_contents_getter, |
| 76 NetworkFallbackCallback()); |
| 77 if (handler) |
| 78 url_request->SetUserData(&kUserDataKey, std::move(handler)); |
| 79 } |
| 80 |
| 81 // PlzNavigate and --enable-network-service |
| 82 mojom::URLLoaderFactoryPtrInfo |
| 83 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService( |
| 84 const ResourceRequest& resource_request, |
| 85 ResourceContext* resource_context, |
| 86 ServiceWorkerNavigationHandleCore* navigation_handle_core, |
| 87 storage::BlobStorageContext* blob_storage_context, |
| 88 bool skip_service_worker, |
| 89 ResourceType resource_type, | 89 ResourceType resource_type, |
| 90 RequestContextType request_context_type, | 90 RequestContextType request_context_type, |
| 91 RequestContextFrameType frame_type, | 91 RequestContextFrameType frame_type, |
| 92 bool is_parent_frame_secure, | 92 bool is_parent_frame_secure, |
| 93 scoped_refptr<ResourceRequestBodyImpl> body, | 93 scoped_refptr<ResourceRequestBodyImpl> body, |
| 94 const base::Callback<WebContents*(void)>& web_contents_getter) { | 94 const base::Callback<WebContents*(void)>& web_contents_getter, |
| 95 CHECK(IsBrowserSideNavigationEnabled()); | 95 NetworkFallbackCallback network_fallback_callback) { |
| 96 | 96 auto handler = InitializeForNavigationInternal( |
| 97 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore | 97 resource_request.url, navigation_handle_core, blob_storage_context, |
| 98 // to take ownership of a pre-created SeviceWorkerProviderHost. | 98 skip_service_worker, resource_type, request_context_type, frame_type, |
| 99 if (!navigation_handle_core) | 99 is_parent_frame_secure, body, web_contents_getter, |
| 100 return; | 100 network_fallback_callback); |
| 101 | 101 if (handler) { |
| 102 // Create the handler even for insecure HTTP since it's used in the | 102 return handler->MaybeGetURLLoaderFactory(resource_request, resource_context, |
| 103 // case of redirect to HTTPS. | 103 std::move(handler)); |
| 104 if (!request->url().SchemeIsHTTPOrHTTPS() && | |
| 105 !OriginCanAccessServiceWorkers(request->url())) { | |
| 106 return; | |
| 107 } | 104 } |
| 108 | 105 return mojom::URLLoaderFactoryPtrInfo(); |
| 109 if (!navigation_handle_core->context_wrapper() || | |
| 110 !navigation_handle_core->context_wrapper()->context()) { | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 // Initialize the SWProviderHost. | |
| 115 std::unique_ptr<ServiceWorkerProviderHost> provider_host = | |
| 116 ServiceWorkerProviderHost::PreCreateNavigationHost( | |
| 117 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(), | |
| 118 is_parent_frame_secure, web_contents_getter); | |
| 119 | |
| 120 FinalizeHandlerInitialization( | |
| 121 request, provider_host.get(), blob_storage_context, skip_service_worker, | |
| 122 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE, | |
| 123 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type, | |
| 124 frame_type, body); | |
| 125 | |
| 126 // Transfer ownership to the ServiceWorkerNavigationHandleCore. | |
| 127 // In the case of a successful navigation, the SWProviderHost will be | |
| 128 // transferred to its "final" destination in the OnProviderCreated handler. If | |
| 129 // the navigation fails, it will be destroyed along with the | |
| 130 // ServiceWorkerNavigationHandleCore. | |
| 131 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host)); | |
| 132 } | 106 } |
| 133 | 107 |
| 134 void ServiceWorkerRequestHandler::InitializeHandler( | 108 void ServiceWorkerRequestHandler::InitializeHandler( |
| 135 net::URLRequest* request, | 109 net::URLRequest* request, |
| 136 ServiceWorkerContextWrapper* context_wrapper, | 110 ServiceWorkerContextWrapper* context_wrapper, |
| 137 storage::BlobStorageContext* blob_storage_context, | 111 storage::BlobStorageContext* blob_storage_context, |
| 138 int process_id, | 112 int process_id, |
| 139 int provider_id, | 113 int provider_id, |
| 140 bool skip_service_worker, | 114 bool skip_service_worker, |
| 141 FetchRequestMode request_mode, | 115 FetchRequestMode request_mode, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 155 if (!context_wrapper || !context_wrapper->context() || | 129 if (!context_wrapper || !context_wrapper->context() || |
| 156 provider_id == kInvalidServiceWorkerProviderId) { | 130 provider_id == kInvalidServiceWorkerProviderId) { |
| 157 return; | 131 return; |
| 158 } | 132 } |
| 159 | 133 |
| 160 ServiceWorkerProviderHost* provider_host = | 134 ServiceWorkerProviderHost* provider_host = |
| 161 context_wrapper->context()->GetProviderHost(process_id, provider_id); | 135 context_wrapper->context()->GetProviderHost(process_id, provider_id); |
| 162 if (!provider_host || !provider_host->IsContextAlive()) | 136 if (!provider_host || !provider_host->IsContextAlive()) |
| 163 return; | 137 return; |
| 164 | 138 |
| 165 FinalizeHandlerInitialization(request, provider_host, blob_storage_context, | 139 auto handler = provider_host->CreateRequestHandler( |
| 166 skip_service_worker, request_mode, | 140 request_mode, credentials_mode, redirect_mode, resource_type, |
| 167 credentials_mode, redirect_mode, resource_type, | 141 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), body, |
| 168 request_context_type, frame_type, body); | 142 skip_service_worker, NetworkFallbackCallback()); |
| 143 if (handler) |
| 144 request->SetUserData(&kUserDataKey, std::move(handler)); |
| 169 } | 145 } |
| 170 | 146 |
| 171 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( | 147 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( |
| 172 const net::URLRequest* request) { | 148 const net::URLRequest* request) { |
| 173 return static_cast<ServiceWorkerRequestHandler*>( | 149 return static_cast<ServiceWorkerRequestHandler*>( |
| 174 request->GetUserData(&kUserDataKey)); | 150 request->GetUserData(&kUserDataKey)); |
| 175 } | 151 } |
| 176 | 152 |
| 177 std::unique_ptr<net::URLRequestInterceptor> | 153 std::unique_ptr<net::URLRequestInterceptor> |
| 178 ServiceWorkerRequestHandler::CreateInterceptor( | 154 ServiceWorkerRequestHandler::CreateInterceptor( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 189 return handler->provider_host_->associated_registration() || | 165 return handler->provider_host_->associated_registration() || |
| 190 handler->provider_host_->running_hosted_version(); | 166 handler->provider_host_->running_hosted_version(); |
| 191 } | 167 } |
| 192 | 168 |
| 193 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost( | 169 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost( |
| 194 const net::URLRequest* request) { | 170 const net::URLRequest* request) { |
| 195 ServiceWorkerRequestHandler* handler = GetHandler(request); | 171 ServiceWorkerRequestHandler* handler = GetHandler(request); |
| 196 return handler ? handler->provider_host_.get() : nullptr; | 172 return handler ? handler->provider_host_.get() : nullptr; |
| 197 } | 173 } |
| 198 | 174 |
| 175 mojom::URLLoaderFactoryPtrInfo |
| 176 ServiceWorkerRequestHandler::MaybeGetURLLoaderFactory( |
| 177 const ResourceRequest& resource_request, |
| 178 ResourceContext* resource_context, |
| 179 std::unique_ptr<ServiceWorkerRequestHandler> request_handler) { |
| 180 return mojom::URLLoaderFactoryPtrInfo(); |
| 181 } |
| 182 |
| 199 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( | 183 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( |
| 200 int old_process_id) { | 184 int old_process_id) { |
| 201 CHECK(!IsBrowserSideNavigationEnabled()); | 185 CHECK(!IsBrowserSideNavigationEnabled()); |
| 202 if (!provider_host_ || !context_) | 186 if (!provider_host_ || !context_) |
| 203 return; | 187 return; |
| 204 old_process_id_ = old_process_id; | 188 old_process_id_ = old_process_id; |
| 205 old_provider_id_ = provider_host_->provider_id(); | 189 old_provider_id_ = provider_host_->provider_id(); |
| 206 host_for_cross_site_transfer_ = context_->TransferProviderHostOut( | 190 host_for_cross_site_transfer_ = context_->TransferProviderHostOut( |
| 207 old_process_id, provider_host_->provider_id()); | 191 old_process_id, provider_host_->provider_id()); |
| 208 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); | 192 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 229 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
| 246 ResourceType resource_type) | 230 ResourceType resource_type) |
| 247 : context_(context), | 231 : context_(context), |
| 248 provider_host_(provider_host), | 232 provider_host_(provider_host), |
| 249 blob_storage_context_(blob_storage_context), | 233 blob_storage_context_(blob_storage_context), |
| 250 resource_type_(resource_type), | 234 resource_type_(resource_type), |
| 251 old_process_id_(0), | 235 old_process_id_(0), |
| 252 old_provider_id_(kInvalidServiceWorkerProviderId) { | 236 old_provider_id_(kInvalidServiceWorkerProviderId) { |
| 253 } | 237 } |
| 254 | 238 |
| 239 std::unique_ptr<ServiceWorkerRequestHandler> |
| 240 ServiceWorkerRequestHandler::InitializeForNavigationInternal( |
| 241 const GURL& url, |
| 242 ServiceWorkerNavigationHandleCore* navigation_handle_core, |
| 243 storage::BlobStorageContext* blob_storage_context, |
| 244 bool skip_service_worker, |
| 245 ResourceType resource_type, |
| 246 RequestContextType request_context_type, |
| 247 RequestContextFrameType frame_type, |
| 248 bool is_parent_frame_secure, |
| 249 scoped_refptr<ResourceRequestBodyImpl> body, |
| 250 const base::Callback<WebContents*(void)>& web_contents_getter, |
| 251 NetworkFallbackCallback network_fallback_callback) { |
| 252 CHECK(IsBrowserSideNavigationEnabled()); |
| 253 |
| 254 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore |
| 255 // to take ownership of a pre-created SeviceWorkerProviderHost. |
| 256 if (!navigation_handle_core) |
| 257 return nullptr; |
| 258 |
| 259 // Create the handler even for insecure HTTP since it's used in the |
| 260 // case of redirect to HTTPS. |
| 261 if (!url.SchemeIsHTTPOrHTTPS() && !OriginCanAccessServiceWorkers(url)) { |
| 262 return nullptr; |
| 263 } |
| 264 |
| 265 if (!navigation_handle_core->context_wrapper() || |
| 266 !navigation_handle_core->context_wrapper()->context()) { |
| 267 return nullptr; |
| 268 } |
| 269 |
| 270 // Initialize the SWProviderHost. |
| 271 std::unique_ptr<ServiceWorkerProviderHost> provider_host = |
| 272 ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 273 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(), |
| 274 is_parent_frame_secure, web_contents_getter); |
| 275 |
| 276 auto handler = provider_host->CreateRequestHandler( |
| 277 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE, |
| 278 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type, |
| 279 frame_type, blob_storage_context->AsWeakPtr(), body, skip_service_worker, |
| 280 network_fallback_callback); |
| 281 |
| 282 // Transfer ownership to the ServiceWorkerNavigationHandleCore. |
| 283 // In the case of a successful navigation, the SWProviderHost will be |
| 284 // transferred to its "final" destination in the OnProviderCreated handler. If |
| 285 // the navigation fails, it will be destroyed along with the |
| 286 // ServiceWorkerNavigationHandleCore. |
| 287 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host)); |
| 288 |
| 289 return handler; |
| 290 } |
| 291 |
| 255 } // namespace content | 292 } // namespace content |
| OLD | NEW |