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

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

Issue 2886843006: servificied service worker interception
Patch Set: 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/command_line.h" 10 #include "base/command_line.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 return NULL; 50 return NULL;
51 return handler->MaybeCreateJob( 51 return handler->MaybeCreateJob(
52 request, network_delegate, resource_context_); 52 request, network_delegate, resource_context_);
53 } 53 }
54 54
55 private: 55 private:
56 ResourceContext* resource_context_; 56 ResourceContext* resource_context_;
57 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); 57 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
58 }; 58 };
59 59
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 60 } // namespace
84 61
85 // PlzNavigate 62 // PlzNavigate
86 void ServiceWorkerRequestHandler::InitializeForNavigation( 63 void ServiceWorkerRequestHandler::InitializeForNavigation(
87 net::URLRequest* request, 64 net::URLRequest* url_request,
88 ServiceWorkerNavigationHandleCore* navigation_handle_core, 65 ServiceWorkerNavigationHandleCore* navigation_handle_core,
89 storage::BlobStorageContext* blob_storage_context, 66 storage::BlobStorageContext* blob_storage_context,
90 bool skip_service_worker, 67 bool skip_service_worker,
91 ResourceType resource_type, 68 ResourceType resource_type,
92 RequestContextType request_context_type, 69 RequestContextType request_context_type,
93 RequestContextFrameType frame_type, 70 RequestContextFrameType frame_type,
94 bool is_parent_frame_secure, 71 bool is_parent_frame_secure,
95 scoped_refptr<ResourceRequestBodyImpl> body, 72 scoped_refptr<ResourceRequestBodyImpl> body,
96 const base::Callback<WebContents*(void)>& web_contents_getter) { 73 const base::Callback<WebContents*(void)>& web_contents_getter) {
97 CHECK(IsBrowserSideNavigationEnabled()); 74 auto handler = InitializeForNavigationInternal(
98 75 url_request->url(), navigation_handle_core, blob_storage_context,
99 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore 76 skip_service_worker, resource_type, request_context_type, frame_type,
100 // to take ownership of a pre-created SeviceWorkerProviderHost. 77 is_parent_frame_secure, body, web_contents_getter);
101 if (!navigation_handle_core) 78 #if 0
102 return; 79 NetworkFallbackCallback());
103 80 #endif
104 // Create the handler even for insecure HTTP since it's used in the 81 if (handler)
105 // case of redirect to HTTPS. 82 url_request->SetUserData(&kUserDataKey, std::move(handler));
106 if (!request->url().SchemeIsHTTPOrHTTPS() &&
107 !OriginCanAccessServiceWorkers(request->url())) {
108 return;
109 }
110
111 if (!navigation_handle_core->context_wrapper() ||
112 !navigation_handle_core->context_wrapper()->context()) {
113 return;
114 }
115
116 // Initialize the SWProviderHost.
117 std::unique_ptr<ServiceWorkerProviderHost> provider_host =
118 ServiceWorkerProviderHost::PreCreateNavigationHost(
119 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(),
120 is_parent_frame_secure, web_contents_getter);
121
122 FinalizeHandlerInitialization(
123 request, provider_host.get(), blob_storage_context, skip_service_worker,
124 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE,
125 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
126 frame_type, body);
127
128 // Transfer ownership to the ServiceWorkerNavigationHandleCore.
129 // In the case of a successful navigation, the SWProviderHost will be
130 // transferred to its "final" destination in the OnProviderCreated handler. If
131 // the navigation fails, it will be destroyed along with the
132 // ServiceWorkerNavigationHandleCore.
133 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host));
134 } 83 }
135 84
136 // PlzNavigate and --enable-network-service. 85 // PlzNavigate and --enable-network-service
137 // static 86 // static
138 mojom::URLLoaderFactoryPtr 87 mojom::URLLoaderFactoryPtr
139 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService( 88 ServiceWorkerRequestHandler::InitializeForNavigationNetworkService(
140 const ResourceRequest& resource_request, 89 const ResourceRequest& resource_request,
141 ResourceContext* resource_context, 90 ResourceContext* resource_context,
142 ServiceWorkerNavigationHandleCore* navigation_handle_core, 91 ServiceWorkerNavigationHandleCore* navigation_handle_core,
143 storage::BlobStorageContext* blob_storage_context, 92 storage::BlobStorageContext* blob_storage_context,
144 bool skip_service_worker, 93 bool skip_service_worker,
145 ResourceType resource_type, 94 ResourceType resource_type,
146 RequestContextType request_context_type, 95 RequestContextType request_context_type,
147 RequestContextFrameType frame_type, 96 RequestContextFrameType frame_type,
148 bool is_parent_frame_secure, 97 bool is_parent_frame_secure,
149 scoped_refptr<ResourceRequestBodyImpl> body, 98 scoped_refptr<ResourceRequestBodyImpl> body,
99 #if 0
100 const base::Callback<WebContents*(void)>& web_contents_getter,
101 NetworkFallbackCallback network_fallback_callback) {
102 #endif
150 const base::Callback<WebContents*(void)>& web_contents_getter) { 103 const base::Callback<WebContents*(void)>& web_contents_getter) {
151 DCHECK(IsBrowserSideNavigationEnabled() && 104 DCHECK(IsBrowserSideNavigationEnabled() &&
152 base::CommandLine::ForCurrentProcess()->HasSwitch( 105 base::CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableNetworkService)); 106 switches::kEnableNetworkService));
154 // TODO(scottmg): Currently being implemented. See https://crbug.com/715640. 107 std::unique_ptr<ServiceWorkerRequestHandler> handler =
108 InitializeForNavigationInternal(
109 resource_request.url, navigation_handle_core, blob_storage_context,
110 skip_service_worker, resource_type, request_context_type, frame_type,
111 is_parent_frame_secure, body, web_contents_getter);
112 #if 0
113 network_fallback_callback);
114 #endif
115 if (handler) {
116 return handler->MaybeGetURLLoaderFactory(resource_request, resource_context,
117 std::move(handler));
118 }
155 return mojom::URLLoaderFactoryPtr(); 119 return mojom::URLLoaderFactoryPtr();
156 } 120 }
157 121
158 void ServiceWorkerRequestHandler::InitializeHandler( 122 void ServiceWorkerRequestHandler::InitializeHandler(
159 net::URLRequest* request, 123 net::URLRequest* request,
160 ServiceWorkerContextWrapper* context_wrapper, 124 ServiceWorkerContextWrapper* context_wrapper,
161 storage::BlobStorageContext* blob_storage_context, 125 storage::BlobStorageContext* blob_storage_context,
162 int process_id, 126 int process_id,
163 int provider_id, 127 int provider_id,
164 bool skip_service_worker, 128 bool skip_service_worker,
(...skipping 14 matching lines...) Expand all
179 if (!context_wrapper || !context_wrapper->context() || 143 if (!context_wrapper || !context_wrapper->context() ||
180 provider_id == kInvalidServiceWorkerProviderId) { 144 provider_id == kInvalidServiceWorkerProviderId) {
181 return; 145 return;
182 } 146 }
183 147
184 ServiceWorkerProviderHost* provider_host = 148 ServiceWorkerProviderHost* provider_host =
185 context_wrapper->context()->GetProviderHost(process_id, provider_id); 149 context_wrapper->context()->GetProviderHost(process_id, provider_id);
186 if (!provider_host || !provider_host->IsContextAlive()) 150 if (!provider_host || !provider_host->IsContextAlive())
187 return; 151 return;
188 152
189 FinalizeHandlerInitialization(request, provider_host, blob_storage_context, 153 auto handler = provider_host->CreateRequestHandler(
190 skip_service_worker, request_mode, 154 request_mode, credentials_mode, redirect_mode, resource_type,
191 credentials_mode, redirect_mode, resource_type, 155 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), body,
192 request_context_type, frame_type, body); 156 skip_service_worker);
157 if (handler)
158 request->SetUserData(&kUserDataKey, std::move(handler));
193 } 159 }
194 160
195 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( 161 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
196 const net::URLRequest* request) { 162 const net::URLRequest* request) {
197 return static_cast<ServiceWorkerRequestHandler*>( 163 return static_cast<ServiceWorkerRequestHandler*>(
198 request->GetUserData(&kUserDataKey)); 164 request->GetUserData(&kUserDataKey));
199 } 165 }
200 166
201 std::unique_ptr<net::URLRequestInterceptor> 167 std::unique_ptr<net::URLRequestInterceptor>
202 ServiceWorkerRequestHandler::CreateInterceptor( 168 ServiceWorkerRequestHandler::CreateInterceptor(
(...skipping 10 matching lines...) Expand all
213 return handler->provider_host_->associated_registration() || 179 return handler->provider_host_->associated_registration() ||
214 handler->provider_host_->running_hosted_version(); 180 handler->provider_host_->running_hosted_version();
215 } 181 }
216 182
217 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost( 183 ServiceWorkerProviderHost* ServiceWorkerRequestHandler::GetProviderHost(
218 const net::URLRequest* request) { 184 const net::URLRequest* request) {
219 ServiceWorkerRequestHandler* handler = GetHandler(request); 185 ServiceWorkerRequestHandler* handler = GetHandler(request);
220 return handler ? handler->provider_host_.get() : nullptr; 186 return handler ? handler->provider_host_.get() : nullptr;
221 } 187 }
222 188
189 mojom::URLLoaderFactoryPtr
190 ServiceWorkerRequestHandler::MaybeGetURLLoaderFactory(
191 const ResourceRequest& resource_request,
192 ResourceContext* resource_context,
193 std::unique_ptr<ServiceWorkerRequestHandler> request_handler) {
194 return mojom::URLLoaderFactoryPtr();
195 }
196
223 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( 197 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
224 int old_process_id) { 198 int old_process_id) {
225 CHECK(!IsBrowserSideNavigationEnabled()); 199 CHECK(!IsBrowserSideNavigationEnabled());
226 if (!provider_host_ || !context_) 200 if (!provider_host_ || !context_)
227 return; 201 return;
228 old_process_id_ = old_process_id; 202 old_process_id_ = old_process_id;
229 old_provider_id_ = provider_host_->provider_id(); 203 old_provider_id_ = provider_host_->provider_id();
230 host_for_cross_site_transfer_ = context_->TransferProviderHostOut( 204 host_for_cross_site_transfer_ = context_->TransferProviderHostOut(
231 old_process_id, provider_host_->provider_id()); 205 old_process_id, provider_host_->provider_id());
232 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); 206 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, 243 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
270 ResourceType resource_type) 244 ResourceType resource_type)
271 : context_(context), 245 : context_(context),
272 provider_host_(provider_host), 246 provider_host_(provider_host),
273 blob_storage_context_(blob_storage_context), 247 blob_storage_context_(blob_storage_context),
274 resource_type_(resource_type), 248 resource_type_(resource_type),
275 old_process_id_(0), 249 old_process_id_(0),
276 old_provider_id_(kInvalidServiceWorkerProviderId) { 250 old_provider_id_(kInvalidServiceWorkerProviderId) {
277 } 251 }
278 252
253 std::unique_ptr<ServiceWorkerRequestHandler>
254 ServiceWorkerRequestHandler::InitializeForNavigationInternal(
255 const GURL& url,
256 ServiceWorkerNavigationHandleCore* navigation_handle_core,
257 storage::BlobStorageContext* blob_storage_context,
258 bool skip_service_worker,
259 ResourceType resource_type,
260 RequestContextType request_context_type,
261 RequestContextFrameType frame_type,
262 bool is_parent_frame_secure,
263 scoped_refptr<ResourceRequestBodyImpl> body,
264 #if 0
265 const base::Callback<WebContents*(void)>& web_contents_getter,
266 NetworkFallbackCallback network_fallback_callback) {
267 #endif
268 const base::Callback<WebContents*(void)>& web_contents_getter) {
269 CHECK(IsBrowserSideNavigationEnabled());
270
271 // Only create a handler when there is a ServiceWorkerNavigationHandlerCore
272 // to take ownership of a pre-created SeviceWorkerProviderHost.
273 if (!navigation_handle_core)
274 return nullptr;
275
276 // Create the handler even for insecure HTTP since it's used in the
277 // case of redirect to HTTPS.
278 if (!url.SchemeIsHTTPOrHTTPS() && !OriginCanAccessServiceWorkers(url)) {
279 return nullptr;
280 }
281
282 if (!navigation_handle_core->context_wrapper() ||
283 !navigation_handle_core->context_wrapper()->context()) {
284 return nullptr;
285 }
286
287 // Initialize the SWProviderHost.
288 std::unique_ptr<ServiceWorkerProviderHost> provider_host =
289 ServiceWorkerProviderHost::PreCreateNavigationHost(
290 navigation_handle_core->context_wrapper()->context()->AsWeakPtr(),
291 is_parent_frame_secure, web_contents_getter);
292
293 auto handler = provider_host->CreateRequestHandler(
294 FETCH_REQUEST_MODE_NAVIGATE, FETCH_CREDENTIALS_MODE_INCLUDE,
295 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
296 frame_type, blob_storage_context->AsWeakPtr(), body, skip_service_worker);
297
298 // Transfer ownership to the ServiceWorkerNavigationHandleCore.
299 // In the case of a successful navigation, the SWProviderHost will be
300 // transferred to its "final" destination in the OnProviderCreated handler. If
301 // the navigation fails, it will be destroyed along with the
302 // ServiceWorkerNavigationHandleCore.
303 navigation_handle_core->DidPreCreateProviderHost(std::move(provider_host));
304
305 return handler;
306 }
307
279 } // namespace content 308 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698