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

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

Issue 2897063002: Network service: Implement URLLoader chaining for interceptors (Closed)
Patch Set: . Created 3 years, 6 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/command_line.h" 10 #include "base/command_line.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "content/browser/loader/url_loader_request_handler.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 13 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" 14 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_navigation_handle_core.h " 15 #include "content/browser/service_worker/service_worker_navigation_handle_core.h "
15 #include "content/browser/service_worker/service_worker_provider_host.h" 16 #include "content/browser/service_worker/service_worker_provider_host.h"
16 #include "content/browser/service_worker/service_worker_registration.h" 17 #include "content/browser/service_worker/service_worker_registration.h"
17 #include "content/browser/service_worker/service_worker_url_request_job.h" 18 #include "content/browser/service_worker/service_worker_url_request_job.h"
18 #include "content/common/resource_request_body_impl.h" 19 #include "content/common/resource_request_body_impl.h"
19 #include "content/common/service_worker/service_worker_types.h" 20 #include "content/common/service_worker/service_worker_types.h"
20 #include "content/common/service_worker/service_worker_utils.h" 21 #include "content/common/service_worker/service_worker_utils.h"
21 #include "content/public/browser/resource_context.h" 22 #include "content/public/browser/resource_context.h"
(...skipping 28 matching lines...) Expand all
50 return NULL; 51 return NULL;
51 return handler->MaybeCreateJob( 52 return handler->MaybeCreateJob(
52 request, network_delegate, resource_context_); 53 request, network_delegate, resource_context_);
53 } 54 }
54 55
55 private: 56 private:
56 ResourceContext* resource_context_; 57 ResourceContext* resource_context_;
57 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); 58 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
58 }; 59 };
59 60
60 void FinalizeHandlerInitialization(
61 net::URLRequest* request,
62 ServiceWorkerProviderHost* provider_host,
63 storage::BlobStorageContext* blob_storage_context,
64 bool skip_service_worker,
65 FetchRequestMode request_mode,
66 FetchCredentialsMode credentials_mode,
67 FetchRedirectMode redirect_mode,
68 ResourceType resource_type,
69 RequestContextType request_context_type,
70 RequestContextFrameType frame_type,
71 scoped_refptr<ResourceRequestBodyImpl> body) {
72 std::unique_ptr<ServiceWorkerRequestHandler> handler(
73 provider_host->CreateRequestHandler(
74 request_mode, credentials_mode, redirect_mode, resource_type,
75 request_context_type, frame_type, blob_storage_context->AsWeakPtr(),
76 body, skip_service_worker));
77 if (!handler)
78 return;
79
80 request->SetUserData(&kUserDataKey, std::move(handler));
81 }
82
83 } // namespace 61 } // namespace
84 62
85 // PlzNavigate 63 // PlzNavigate
86 void ServiceWorkerRequestHandler::InitializeForNavigation( 64 void ServiceWorkerRequestHandler::InitializeForNavigation(
87 net::URLRequest* request, 65 net::URLRequest* request,
88 ServiceWorkerNavigationHandleCore* navigation_handle_core, 66 ServiceWorkerNavigationHandleCore* navigation_handle_core,
89 storage::BlobStorageContext* blob_storage_context, 67 storage::BlobStorageContext* blob_storage_context,
90 bool skip_service_worker, 68 bool skip_service_worker,
91 ResourceType resource_type, 69 ResourceType resource_type,
92 RequestContextType request_context_type, 70 RequestContextType request_context_type,
(...skipping 19 matching lines...) Expand all
112 !navigation_handle_core->context_wrapper()->context()) { 90 !navigation_handle_core->context_wrapper()->context()) {
113 return; 91 return;
114 } 92 }
115 93
116 // Initialize the SWProviderHost. 94 // Initialize the SWProviderHost.
117 std::unique_ptr<ServiceWorkerProviderHost> provider_host = 95 std::unique_ptr<ServiceWorkerProviderHost> provider_host =
118 ServiceWorkerProviderHost::PreCreateNavigationHost( 96 ServiceWorkerProviderHost::PreCreateNavigationHost(
119 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(), 97 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(),
120 is_parent_frame_secure, web_contents_getter); 98 is_parent_frame_secure, web_contents_getter);
121 99
122 FinalizeHandlerInitialization( 100 std::unique_ptr<ServiceWorkerRequestHandler> handler(
123 request, provider_host.get(), blob_storage_context, skip_service_worker, 101 provider_host->CreateRequestHandler(
124 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE, 102 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE,
125 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type, 103 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
126 frame_type, body); 104 frame_type, blob_storage_context->AsWeakPtr(), body,
105 skip_service_worker));
106 if (handler)
107 request->SetUserData(&kUserDataKey, std::move(handler));
127 108
128 // Transfer ownership to the ServiceWorkerNavigationHandleCore. 109 // Transfer ownership to the ServiceWorkerNavigationHandleCore.
129 // In the case of a successful navigation, the SWProviderHost will be 110 // In the case of a successful navigation, the SWProviderHost will be
130 // transferred to its "final" destination in the OnProviderCreated handler. If 111 // transferred to its "final" destination in the OnProviderCreated handler. If
131 // the navigation fails, it will be destroyed along with the 112 // the navigation fails, it will be destroyed along with the
132 // ServiceWorkerNavigationHandleCore. 113 // ServiceWorkerNavigationHandleCore.
133 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host)); 114 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host));
134 } 115 }
135 116
136 // PlzNavigate and --enable-network-service. 117 // PlzNavigate and --enable-network-service.
137 // static 118 // static
138 mojom::URLLoaderFactoryPtr 119 std::unique_ptr<URLLoaderRequestHandler>
139 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService( 120 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService(
140 const ResourceRequest& resource_request, 121 const ResourceRequest& resource_request,
141 ResourceContext* resource_context, 122 ResourceContext* resource_context,
142 ServiceWorkerNavigationHandleCore* navigation_handle_core, 123 ServiceWorkerNavigationHandleCore* navigation_handle_core,
143 storage::BlobStorageContext* blob_storage_context, 124 storage::BlobStorageContext* blob_storage_context,
144 bool skip_service_worker, 125 bool skip_service_worker,
145 ResourceType resource_type, 126 ResourceType resource_type,
146 RequestContextType request_context_type, 127 RequestContextType request_context_type,
147 RequestContextFrameType frame_type, 128 RequestContextFrameType frame_type,
148 bool is_parent_frame_secure, 129 bool is_parent_frame_secure,
149 scoped_refptr<ResourceRequestBodyImpl> body, 130 scoped_refptr<ResourceRequestBodyImpl> body,
150 const base::Callback<WebContents*(void)>& web_contents_getter) { 131 const base::Callback<WebContents*(void)>& web_contents_getter) {
151 DCHECK(IsBrowserSideNavigationEnabled() && 132 DCHECK(IsBrowserSideNavigationEnabled() &&
152 base::CommandLine::ForCurrentProcess()->HasSwitch( 133 base::CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableNetworkService)); 134 switches::kEnableNetworkService));
154 // TODO(scottmg): Currently being implemented. See https://crbug.com/715640. 135 DCHECK(navigation_handle_core);
155 return mojom::URLLoaderFactoryPtr(); 136
137 // Create the handler even for insecure HTTP since it's used in the
138 // case of redirect to HTTPS.
139 if (!resource_request.url.SchemeIsHTTPOrHTTPS() &&
140 !OriginCanAccessServiceWorkers(resource_request.url)) {
141 return nullptr;
142 }
143
144 if (!navigation_handle_core->context_wrapper() ||
145 !navigation_handle_core->context_wrapper()->context()) {
146 return nullptr;
147 }
148
149 // Initialize the SWProviderHost.
150 std::unique_ptr<ServiceWorkerProviderHost> provider_host =
151 ServiceWorkerProviderHost::PreCreateNavigationHost(
152 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(),
153 is_parent_frame_secure, web_contents_getter);
154
155 std::unique_ptr<ServiceWorkerRequestHandler> handler(
156 provider_host->CreateRequestHandler(
157 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE,
158 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
159 frame_type, blob_storage_context->AsWeakPtr(), body,
160 skip_service_worker));
161
162 // Transfer ownership to the ServiceWorkerNavigationHandleCore.
163 // In the case of a successful navigation, the SWProviderHost will be
164 // transferred to its "final" destination in the OnProviderCreated handler. If
165 // the navigation fails, it will be destroyed along with the
166 // ServiceWorkerNavigationHandleCore.
167 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host));
168
169 return base::WrapUnique<URLLoaderRequestHandler>(handler.release());
156 } 170 }
157 171
158 void ServiceWorkerRequestHandler::InitializeHandler( 172 void ServiceWorkerRequestHandler::InitializeHandler(
159 net::URLRequest* request, 173 net::URLRequest* request,
160 ServiceWorkerContextWrapper* context_wrapper, 174 ServiceWorkerContextWrapper* context_wrapper,
161 storage::BlobStorageContext* blob_storage_context, 175 storage::BlobStorageContext* blob_storage_context,
162 int process_id, 176 int process_id,
163 int provider_id, 177 int provider_id,
164 bool skip_service_worker, 178 bool skip_service_worker,
165 FetchRequestMode request_mode, 179 FetchRequestMode request_mode,
(...skipping 13 matching lines...) Expand all
179 if (!context_wrapper || !context_wrapper->context() || 193 if (!context_wrapper || !context_wrapper->context() ||
180 provider_id == kInvalidServiceWorkerProviderId) { 194 provider_id == kInvalidServiceWorkerProviderId) {
181 return; 195 return;
182 } 196 }
183 197
184 ServiceWorkerProviderHost* provider_host = 198 ServiceWorkerProviderHost* provider_host =
185 context_wrapper->context()->GetProviderHost(process_id, provider_id); 199 context_wrapper->context()->GetProviderHost(process_id, provider_id);
186 if (!provider_host || !provider_host->IsContextAlive()) 200 if (!provider_host || !provider_host->IsContextAlive())
187 return; 201 return;
188 202
189 FinalizeHandlerInitialization(request, provider_host, blob_storage_context, 203 std::unique_ptr<ServiceWorkerRequestHandler> handler(
190 skip_service_worker, request_mode, 204 provider_host->CreateRequestHandler(
191 credentials_mode, redirect_mode, resource_type, 205 request_mode, credentials_mode, redirect_mode, resource_type,
192 request_context_type, frame_type, body); 206 request_context_type, frame_type, blob_storage_context->AsWeakPtr(),
207 body, skip_service_worker));
208 if (handler)
209 request->SetUserData(&kUserDataKey, std::move(handler));
193 } 210 }
194 211
195 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( 212 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
196 const net::URLRequest* request) { 213 const net::URLRequest* request) {
197 return static_cast<ServiceWorkerRequestHandler*>( 214 return static_cast<ServiceWorkerRequestHandler*>(
198 request->GetUserData(&kUserDataKey)); 215 request->GetUserData(&kUserDataKey));
199 } 216 }
200 217
201 std::unique_ptr<net::URLRequestInterceptor> 218 std::unique_ptr<net::URLRequestInterceptor>
202 ServiceWorkerRequestHandler::CreateInterceptor( 219 ServiceWorkerRequestHandler::CreateInterceptor(
(...skipping 10 matching lines...) Expand all
213 return handler->provider_host_->associated_registration() || 230 return handler->provider_host_->associated_registration() ||
214 handler->provider_host_->running_hosted_version(); 231 handler->provider_host_->running_hosted_version();
215 } 232 }
216 233
217 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost( 234 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost(
218 const net::URLRequest* request) { 235 const net::URLRequest* request) {
219 ServiceWorkerRequestHandler* handler = GetHandler(request); 236 ServiceWorkerRequestHandler* handler = GetHandler(request);
220 return handler ? handler->provider_host_.get() : nullptr; 237 return handler ? handler->provider_host_.get() : nullptr;
221 } 238 }
222 239
240 void ServiceWorkerRequestHandler::Start(
241 const ResourceRequest& request,
242 Controller* controller,
243 ResourceContext* resource_context,
244 mojom::URLLoaderAssociatedRequest loader_request,
245 mojom::URLLoaderClientPtr loader_client_ptr) {
246 NOTREACHED();
scottmg 2017/05/25 15:12:24 This is only for ServiceWorkerURLTrackingRequestHa
kinuko 2017/05/26 02:34:20 Yep. I could make only SWControlleeRequestHandler
247 }
248
223 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( 249 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
224 int old_process_id) { 250 int old_process_id) {
225 CHECK(!IsBrowserSideNavigationEnabled()); 251 CHECK(!IsBrowserSideNavigationEnabled());
226 if (!provider_host_ || !context_) 252 if (!provider_host_ || !context_)
227 return; 253 return;
228 old_process_id_ = old_process_id; 254 old_process_id_ = old_process_id;
229 old_provider_id_ = provider_host_->provider_id(); 255 old_provider_id_ = provider_host_->provider_id();
230 host_for_cross_site_transfer_ = context_->TransferProviderHostOut( 256 host_for_cross_site_transfer_ = context_->TransferProviderHostOut(
231 old_process_id, provider_host_->provider_id()); 257 old_process_id, provider_host_->provider_id());
232 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); 258 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 ResourceType resource_type) 296 ResourceType resource_type)
271 : context_(context), 297 : context_(context),
272 provider_host_(provider_host), 298 provider_host_(provider_host),
273 blob_storage_context_(blob_storage_context), 299 blob_storage_context_(blob_storage_context),
274 resource_type_(resource_type), 300 resource_type_(resource_type),
275 old_process_id_(0), 301 old_process_id_(0),
276 old_provider_id_(kInvalidServiceWorkerProviderId) { 302 old_provider_id_(kInvalidServiceWorkerProviderId) {
277 } 303 }
278 304
279 } // namespace content 305 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698