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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 221 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
246 ResourceType resource_type) | 222 ResourceType resource_type) |
247 : context_(context), | 223 : context_(context), |
248 provider_host_(provider_host), | 224 provider_host_(provider_host), |
249 blob_storage_context_(blob_storage_context), | 225 blob_storage_context_(blob_storage_context), |
250 resource_type_(resource_type), | 226 resource_type_(resource_type), |
251 old_process_id_(0), | 227 old_process_id_(0), |
252 old_provider_id_(kInvalidServiceWorkerProviderId) { | 228 old_provider_id_(kInvalidServiceWorkerProviderId) { |
253 } | 229 } |
254 | 230 |
| 231 std::unique_ptr<ServiceWorkerRequestHandler> |
| 232 ServiceWorkerRequestHandler::InitializeForNavigationInternal( |
| 233 const GURL& url, |
| 234 ServiceWorkerNavigationHandleCore* navigation_handle_core, |
| 235 storage::BlobStorageContext* blob_storage_context, |
| 236 bool skip_service_worker, |
| 237 ResourceType resource_type, |
| 238 RequestContextType request_context_type, |
| 239 RequestContextFrameType frame_type, |
| 240 bool is_parent_frame_secure, |
| 241 scoped_refptr<ResourceRequestBodyImpl> body, |
| 242 const base::Callback<WebContents*(void)>& web_contents_getter, |
| 243 NetworkFallbackCallback network_fallback_callback) { |
| 244 CHECK(IsBrowserSideNavigationEnabled()); |
| 245 |
| 246 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore |
| 247 // to take ownership of a pre-created SeviceWorkerProviderHost. |
| 248 if (!navigation_handle_core) |
| 249 return nullptr; |
| 250 |
| 251 // Create the handler even for insecure HTTP since it's used in the |
| 252 // case of redirect to HTTPS. |
| 253 if (!url.SchemeIsHTTPOrHTTPS() && !OriginCanAccessServiceWorkers(url)) { |
| 254 return nullptr; |
| 255 } |
| 256 |
| 257 if (!navigation_handle_core->context_wrapper() || |
| 258 !navigation_handle_core->context_wrapper()->context()) { |
| 259 return nullptr; |
| 260 } |
| 261 |
| 262 // Initialize the SWProviderHost. |
| 263 std::unique_ptr<ServiceWorkerProviderHost> provider_host = |
| 264 ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 265 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(), |
| 266 is_parent_frame_secure, web_contents_getter); |
| 267 |
| 268 auto handler = provider_host->CreateRequestHandler( |
| 269 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE, |
| 270 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type, |
| 271 frame_type, blob_storage_context->AsWeakPtr(), body, skip_service_worker, |
| 272 network_fallback_callback); |
| 273 |
| 274 // Transfer ownership to the ServiceWorkerNavigationHandleCore. |
| 275 // In the case of a successful navigation, the SWProviderHost will be |
| 276 // transferred to its "final" destination in the OnProviderCreated handler. If |
| 277 // the navigation fails, it will be destroyed along with the |
| 278 // ServiceWorkerNavigationHandleCore. |
| 279 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host)); |
| 280 |
| 281 return handler; |
| 282 } |
| 283 |
255 } // namespace content | 284 } // namespace content |
OLD | NEW |