Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(711)

Side by Side Diff: content/browser/service_worker/service_worker_request_handler.cc

Issue 2843043002: network service: Create URLLoader for service worker navigation case
Patch Set: network fallback in simple cases Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 AttachToURLRequest(net::URLRequest* url_request,
59 std::unique_ptr<ServiceWorkerRequestHandler> handler) {
60 url_request->SetUserData(&kUserDataKey, std::move(handler));
61 }
62
63 void CreateURLLoader(mojom::URLLoaderFactoryPtrInfo* url_loader_factory_info,
64 const ResourceRequest& resource_request,
65 ResourceContext* resource_context,
66 std::unique_ptr<ServiceWorkerRequestHandler> handler) {
67 *url_loader_factory_info = handler->MaybeCreateURLLoader(
68 resource_request, resource_context, std::move(handler));
69 }
70
58 void FinalizeHandlerInitialization( 71 void FinalizeHandlerInitialization(
59 net::URLRequest* request,
60 ServiceWorkerProviderHost* provider_host, 72 ServiceWorkerProviderHost* provider_host,
61 storage::BlobStorageContext* blob_storage_context, 73 storage::BlobStorageContext* blob_storage_context,
62 bool skip_service_worker, 74 bool skip_service_worker,
63 FetchRequestMode request_mode, 75 FetchRequestMode request_mode,
64 FetchCredentialsMode credentials_mode, 76 FetchCredentialsMode credentials_mode,
65 FetchRedirectMode redirect_mode, 77 FetchRedirectMode redirect_mode,
66 ResourceType resource_type, 78 ResourceType resource_type,
67 RequestContextType request_context_type, 79 RequestContextType request_context_type,
68 RequestContextFrameType frame_type, 80 RequestContextFrameType frame_type,
69 scoped_refptr<ResourceRequestBodyImpl> body) { 81 scoped_refptr<ResourceRequestBodyImpl> body,
82 const ServiceWorkerRequestHandler::HandlerCreated& on_created) {
70 std::unique_ptr<ServiceWorkerRequestHandler> handler( 83 std::unique_ptr<ServiceWorkerRequestHandler> handler(
71 provider_host->CreateRequestHandler( 84 provider_host->CreateRequestHandler(
72 request_mode, credentials_mode, redirect_mode, resource_type, 85 request_mode, credentials_mode, redirect_mode, resource_type,
73 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), 86 request_context_type, frame_type, blob_storage_context->AsWeakPtr(),
74 body, skip_service_worker)); 87 body, skip_service_worker));
75 if (!handler) 88 if (!handler)
76 return; 89 return;
77 90
78 request->SetUserData(&kUserDataKey, std::move(handler)); 91 on_created.Run(std::move(handler));
kinuko 2017/05/08 14:55:47 I think this method and InitializeForNavigationInt
scottmg 2017/05/08 20:12:31 The only reason I did that the complicated way was
79 } 92 }
80 93
81 } // namespace 94 } // namespace
82 95
83 // PlzNavigate 96 // PlzNavigate
84 void ServiceWorkerRequestHandler::InitializeForNavigation( 97 void ServiceWorkerRequestHandler::InitializeForNavigation(
85 net::URLRequest* request, 98 net::URLRequest* url_request,
86 ServiceWorkerNavigationHandleCore* navigation_handle_core, 99 ServiceWorkerNavigationHandleCore* navigation_handle_core,
87 storage::BlobStorageContext* blob_storage_context, 100 storage::BlobStorageContext* blob_storage_context,
88 bool skip_service_worker, 101 bool skip_service_worker,
89 ResourceType resource_type, 102 ResourceType resource_type,
90 RequestContextType request_context_type, 103 RequestContextType request_context_type,
91 RequestContextFrameType frame_type, 104 RequestContextFrameType frame_type,
92 bool is_parent_frame_secure, 105 bool is_parent_frame_secure,
93 scoped_refptr<ResourceRequestBodyImpl> body, 106 scoped_refptr<ResourceRequestBodyImpl> body,
94 const base::Callback<WebContents*(void)>& web_contents_getter) { 107 const base::Callback<WebContents*(void)>& web_contents_getter) {
95 CHECK(IsBrowserSideNavigationEnabled()); 108 InitializeForNavigationImpl(url_request->url(), navigation_handle_core,
109 blob_storage_context, skip_service_worker,
110 resource_type, request_context_type, frame_type,
111 is_parent_frame_secure, body, web_contents_getter,
112 base::Bind(&AttachToURLRequest, url_request));
113 }
96 114
97 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore 115 // PlzNavigate and --enable-network-service
98 // to take ownership of a pre-created SeviceWorkerProviderHost. 116 mojom::URLLoaderFactoryPtrInfo
99 if (!navigation_handle_core) 117 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService(
100 return; 118 const ResourceRequest& resource_request,
101 119 ResourceContext* resource_context,
102 // Create the handler even for insecure HTTP since it's used in the 120 ServiceWorkerNavigationHandleCore* navigation_handle_core,
103 // case of redirect to HTTPS. 121 storage::BlobStorageContext* blob_storage_context,
104 if (!request->url().SchemeIsHTTPOrHTTPS() && 122 bool skip_service_worker,
105 !OriginCanAccessServiceWorkers(request->url())) { 123 ResourceType resource_type,
106 return; 124 RequestContextType request_context_type,
107 } 125 RequestContextFrameType frame_type,
108 126 bool is_parent_frame_secure,
109 if (!navigation_handle_core->context_wrapper() || 127 scoped_refptr<ResourceRequestBodyImpl> body,
110 !navigation_handle_core->context_wrapper()->context()) { 128 const base::Callback<WebContents*(void)>& web_contents_getter) {
111 return; 129 mojom::URLLoaderFactoryPtrInfo result;
112 } 130 InitializeForNavigationImpl(resource_request.url, navigation_handle_core,
113 131 blob_storage_context, skip_service_worker,
114 // Initialize the SWProviderHost. 132 resource_type, request_context_type, frame_type,
115 std::unique_ptr<ServiceWorkerProviderHost> provider_host = 133 is_parent_frame_secure, body, web_contents_getter,
116 ServiceWorkerProviderHost::PreCreateNavigationHost( 134 base::Bind(&CreateURLLoader, &result,
117 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(), 135 resource_request, resource_context));
118 is_parent_frame_secure, web_contents_getter); 136 return std::move(result);
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 } 137 }
133 138
134 void ServiceWorkerRequestHandler::InitializeHandler( 139 void ServiceWorkerRequestHandler::InitializeHandler(
135 net::URLRequest* request, 140 net::URLRequest* request,
136 ServiceWorkerContextWrapper* context_wrapper, 141 ServiceWorkerContextWrapper* context_wrapper,
137 storage::BlobStorageContext* blob_storage_context, 142 storage::BlobStorageContext* blob_storage_context,
138 int process_id, 143 int process_id,
139 int provider_id, 144 int provider_id,
140 bool skip_service_worker, 145 bool skip_service_worker,
141 FetchRequestMode request_mode, 146 FetchRequestMode request_mode,
(...skipping 13 matching lines...) Expand all
155 if (!context_wrapper || !context_wrapper->context() || 160 if (!context_wrapper || !context_wrapper->context() ||
156 provider_id == kInvalidServiceWorkerProviderId) { 161 provider_id == kInvalidServiceWorkerProviderId) {
157 return; 162 return;
158 } 163 }
159 164
160 ServiceWorkerProviderHost* provider_host = 165 ServiceWorkerProviderHost* provider_host =
161 context_wrapper->context()->GetProviderHost(process_id, provider_id); 166 context_wrapper->context()->GetProviderHost(process_id, provider_id);
162 if (!provider_host || !provider_host->IsContextAlive()) 167 if (!provider_host || !provider_host->IsContextAlive())
163 return; 168 return;
164 169
165 FinalizeHandlerInitialization(request, provider_host, blob_storage_context, 170 FinalizeHandlerInitialization(
166 skip_service_worker, request_mode, 171 provider_host, blob_storage_context, skip_service_worker, request_mode,
167 credentials_mode, redirect_mode, resource_type, 172 credentials_mode, redirect_mode, resource_type, request_context_type,
168 request_context_type, frame_type, body); 173 frame_type, body, base::Bind(&AttachToURLRequest, request));
169 } 174 }
170 175
171 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( 176 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
172 const net::URLRequest* request) { 177 const net::URLRequest* request) {
173 return static_cast<ServiceWorkerRequestHandler*>( 178 return static_cast<ServiceWorkerRequestHandler*>(
174 request->GetUserData(&kUserDataKey)); 179 request->GetUserData(&kUserDataKey));
175 } 180 }
176 181
177 std::unique_ptr<net::URLRequestInterceptor> 182 std::unique_ptr<net::URLRequestInterceptor>
178 ServiceWorkerRequestHandler::CreateInterceptor( 183 ServiceWorkerRequestHandler::CreateInterceptor(
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, 250 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
246 ResourceType resource_type) 251 ResourceType resource_type)
247 : context_(context), 252 : context_(context),
248 provider_host_(provider_host), 253 provider_host_(provider_host),
249 blob_storage_context_(blob_storage_context), 254 blob_storage_context_(blob_storage_context),
250 resource_type_(resource_type), 255 resource_type_(resource_type),
251 old_process_id_(0), 256 old_process_id_(0),
252 old_provider_id_(kInvalidServiceWorkerProviderId) { 257 old_provider_id_(kInvalidServiceWorkerProviderId) {
253 } 258 }
254 259
260 void ServiceWorkerRequestHandler::InitializeForNavigationImpl(
261 const GURL& url,
262 ServiceWorkerNavigationHandleCore* navigation_handle_core,
263 storage::BlobStorageContext* blob_storage_context,
264 bool skip_service_worker,
265 ResourceType resource_type,
266 RequestContextType request_context_type,
267 RequestContextFrameType frame_type,
268 bool is_parent_frame_secure,
269 scoped_refptr<ResourceRequestBodyImpl> body,
270 const base::Callback<WebContents*(void)>& web_contents_getter,
271 const HandlerCreated& on_handler_created) {
272 CHECK(IsBrowserSideNavigationEnabled());
273
274 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore
275 // to take ownership of a pre-created SeviceWorkerProviderHost.
276 if (!navigation_handle_core)
277 return;
278
279 // Create the handler even for insecure HTTP since it's used in the
280 // case of redirect to HTTPS.
281 if (!url.SchemeIsHTTPOrHTTPS() && !OriginCanAccessServiceWorkers(url)) {
282 return;
283 }
284
285 if (!navigation_handle_core->context_wrapper() ||
286 !navigation_handle_core->context_wrapper()->context()) {
287 return;
288 }
289
290 // Initialize the SWProviderHost.
291 std::unique_ptr<ServiceWorkerProviderHost> provider_host =
292 ServiceWorkerProviderHost::PreCreateNavigationHost(
293 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(),
294 is_parent_frame_secure, web_contents_getter);
295
296 FinalizeHandlerInitialization(
297 provider_host.get(), blob_storage_context, skip_service_worker,
298 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE,
299 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
300 frame_type, body, on_handler_created);
301
302 // Transfer ownership to the ServiceWorkerNavigationHandleCore.
303 // In the case of a successful navigation, the SWProviderHost will be
304 // transferred to its "final" destination in the OnProviderCreated handler. If
305 // the navigation fails, it will be destroyed along with the
306 // ServiceWorkerNavigationHandleCore.
307 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host));
308 }
309
255 } // namespace content 310 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698