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 | 8 |
9 #include "base/command_line.h" | |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
11 #include "content/browser/service_worker/service_worker_provider_host.h" | 12 #include "content/browser/service_worker/service_worker_provider_host.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/browser/service_worker/service_worker_url_request_job.h" | 14 #include "content/browser/service_worker/service_worker_url_request_job.h" |
14 #include "content/common/resource_request_body.h" | 15 #include "content/common/resource_request_body.h" |
15 #include "content/common/service_worker/service_worker_types.h" | 16 #include "content/common/service_worker/service_worker_types.h" |
16 #include "content/common/service_worker/service_worker_utils.h" | 17 #include "content/common/service_worker/service_worker_utils.h" |
17 #include "content/public/browser/resource_context.h" | 18 #include "content/public/browser/resource_context.h" |
19 #include "content/public/common/content_switches.h" | |
18 #include "content/public/common/origin_util.h" | 20 #include "content/public/common/origin_util.h" |
21 #include "ipc/ipc_message.h" | |
19 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
20 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
21 #include "net/url_request/url_request_interceptor.h" | 24 #include "net/url_request/url_request_interceptor.h" |
22 #include "storage/browser/blob/blob_storage_context.h" | 25 #include "storage/browser/blob/blob_storage_context.h" |
23 | 26 |
24 namespace content { | 27 namespace content { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 int kUserDataKey; // Key value is not important. | 31 int kUserDataKey; // Key value is not important. |
(...skipping 13 matching lines...) Expand all Loading... | |
42 return NULL; | 45 return NULL; |
43 return handler->MaybeCreateJob( | 46 return handler->MaybeCreateJob( |
44 request, network_delegate, resource_context_); | 47 request, network_delegate, resource_context_); |
45 } | 48 } |
46 | 49 |
47 private: | 50 private: |
48 ResourceContext* resource_context_; | 51 ResourceContext* resource_context_; |
49 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); | 52 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); |
50 }; | 53 }; |
51 | 54 |
55 void FinalizeHandlerInitialization( | |
56 net::URLRequest* request, | |
57 ServiceWorkerProviderHost* provider_host, | |
58 storage::BlobStorageContext* blob_storage_context, | |
59 bool skip_service_worker, | |
60 FetchRequestMode request_mode, | |
61 FetchCredentialsMode credentials_mode, | |
62 FetchRedirectMode redirect_mode, | |
63 ResourceType resource_type, | |
64 RequestContextType request_context_type, | |
65 RequestContextFrameType frame_type, | |
66 scoped_refptr<ResourceRequestBody> body) { | |
67 if (skip_service_worker) { | |
68 // TODO(horo): Does this work properly for PlzNavigate? | |
69 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) { | |
70 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url())); | |
71 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies()); | |
72 // A page load with skip_service_worker should be triggered by | |
73 // shift-reload, so retain all live matching registrations. | |
74 provider_host->AddAllMatchingRegistrations(); | |
75 } | |
76 return; | |
77 } | |
78 | |
79 scoped_ptr<ServiceWorkerRequestHandler> handler( | |
80 provider_host->CreateRequestHandler( | |
81 request_mode, credentials_mode, redirect_mode, resource_type, | |
82 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), | |
83 body)); | |
84 if (!handler) | |
85 return; | |
86 | |
87 request->SetUserData(&kUserDataKey, handler.release()); | |
88 } | |
89 | |
52 } // namespace | 90 } // namespace |
53 | 91 |
92 // PlzNavigate | |
93 void ServiceWorkerRequestHandler::InitializeForNavigation( | |
94 net::URLRequest* request, | |
95 ServiceWorkerContextWrapper* context_wrapper, | |
96 storage::BlobStorageContext* blob_storage_context, | |
97 int provider_id, | |
98 bool skip_service_worker, | |
99 ResourceType resource_type, | |
100 RequestContextType request_context_type, | |
101 RequestContextFrameType frame_type, | |
102 scoped_refptr<ResourceRequestBody> body) { | |
103 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
michaeln
2015/10/07 01:16:44
Is this to defend against a bad ipc?
Fabrice (no longer in Chrome)
2015/10/07 12:59:43
No, this is another sanity check. The caller alrea
| |
104 switches::kEnableBrowserSideNavigation)); | |
105 | |
106 // Create the handler even for insecure HTTP since it's used in the | |
107 // case of redirect to HTTPS. | |
108 if (!request->url().SchemeIsHTTPOrHTTPS() && | |
109 !OriginCanAccessServiceWorkers(request->url())) { | |
110 return; | |
111 } | |
112 | |
113 if (!context_wrapper || !context_wrapper->context()) { | |
114 return; | |
115 } | |
116 | |
117 // The navigation request comes from a sandboxed frame. Do not create a | |
118 // SWProviderHost for sandboxed frames. | |
119 if (provider_id == kInvalidServiceWorkerProviderId) | |
120 return; | |
121 | |
122 // Initialize the SWProviderHost. | |
123 scoped_ptr<ServiceWorkerProviderHost> provider_host( | |
124 new ServiceWorkerProviderHost( | |
125 kVirtualProcessIDForBrowserRequest, MSG_ROUTING_NONE, provider_id, | |
126 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | |
127 context_wrapper->context()->AsWeakPtr(), nullptr)); | |
128 | |
129 FinalizeHandlerInitialization( | |
130 request, provider_host.get(), blob_storage_context, skip_service_worker, | |
131 FETCH_REQUEST_MODE_SAME_ORIGIN, FETCH_CREDENTIALS_MODE_INCLUDE, | |
132 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type, | |
133 frame_type, body); | |
134 | |
135 // Add to context map. | |
136 context_wrapper->context()->AddNavigationProviderHost(provider_host.Pass()); | |
137 } | |
138 | |
54 void ServiceWorkerRequestHandler::InitializeHandler( | 139 void ServiceWorkerRequestHandler::InitializeHandler( |
55 net::URLRequest* request, | 140 net::URLRequest* request, |
56 ServiceWorkerContextWrapper* context_wrapper, | 141 ServiceWorkerContextWrapper* context_wrapper, |
57 storage::BlobStorageContext* blob_storage_context, | 142 storage::BlobStorageContext* blob_storage_context, |
58 int process_id, | 143 int process_id, |
59 int provider_id, | 144 int provider_id, |
60 bool skip_service_worker, | 145 bool skip_service_worker, |
61 FetchRequestMode request_mode, | 146 FetchRequestMode request_mode, |
62 FetchCredentialsMode credentials_mode, | 147 FetchCredentialsMode credentials_mode, |
63 FetchRedirectMode redirect_mode, | 148 FetchRedirectMode redirect_mode, |
(...skipping 11 matching lines...) Expand all Loading... | |
75 if (!context_wrapper || !context_wrapper->context() || | 160 if (!context_wrapper || !context_wrapper->context() || |
76 provider_id == kInvalidServiceWorkerProviderId) { | 161 provider_id == kInvalidServiceWorkerProviderId) { |
77 return; | 162 return; |
78 } | 163 } |
79 | 164 |
80 ServiceWorkerProviderHost* provider_host = | 165 ServiceWorkerProviderHost* provider_host = |
81 context_wrapper->context()->GetProviderHost(process_id, provider_id); | 166 context_wrapper->context()->GetProviderHost(process_id, provider_id); |
82 if (!provider_host || !provider_host->IsContextAlive()) | 167 if (!provider_host || !provider_host->IsContextAlive()) |
83 return; | 168 return; |
84 | 169 |
85 if (skip_service_worker) { | 170 FinalizeHandlerInitialization(request, provider_host, blob_storage_context, |
86 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) { | 171 skip_service_worker, request_mode, |
87 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url())); | 172 credentials_mode, redirect_mode, resource_type, |
88 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies()); | 173 request_context_type, frame_type, body); |
89 // A page load with skip_service_worker should be triggered by | |
90 // shift-reload, so retain all live matching registrations. | |
91 provider_host->AddAllMatchingRegistrations(); | |
92 } | |
93 return; | |
94 } | |
95 | |
96 scoped_ptr<ServiceWorkerRequestHandler> handler( | |
97 provider_host->CreateRequestHandler( | |
98 request_mode, credentials_mode, redirect_mode, resource_type, | |
99 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), | |
100 body)); | |
101 if (!handler) | |
102 return; | |
103 | |
104 request->SetUserData(&kUserDataKey, handler.release()); | |
105 } | 174 } |
106 | 175 |
107 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( | 176 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( |
108 net::URLRequest* request) { | 177 net::URLRequest* request) { |
109 return static_cast<ServiceWorkerRequestHandler*>( | 178 return static_cast<ServiceWorkerRequestHandler*>( |
110 request->GetUserData(&kUserDataKey)); | 179 request->GetUserData(&kUserDataKey)); |
111 } | 180 } |
112 | 181 |
113 scoped_ptr<net::URLRequestInterceptor> | 182 scoped_ptr<net::URLRequestInterceptor> |
114 ServiceWorkerRequestHandler::CreateInterceptor( | 183 ServiceWorkerRequestHandler::CreateInterceptor( |
115 ResourceContext* resource_context) { | 184 ResourceContext* resource_context) { |
116 return scoped_ptr<net::URLRequestInterceptor>( | 185 return scoped_ptr<net::URLRequestInterceptor>( |
117 new ServiceWorkerRequestInterceptor(resource_context)); | 186 new ServiceWorkerRequestInterceptor(resource_context)); |
118 } | 187 } |
119 | 188 |
120 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker( | 189 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker( |
121 net::URLRequest* request) { | 190 net::URLRequest* request) { |
122 ServiceWorkerRequestHandler* handler = GetHandler(request); | 191 ServiceWorkerRequestHandler* handler = GetHandler(request); |
123 if (!handler || !handler->provider_host_) | 192 if (!handler || !handler->provider_host_) |
124 return false; | 193 return false; |
125 return handler->provider_host_->associated_registration() || | 194 return handler->provider_host_->associated_registration() || |
126 handler->provider_host_->running_hosted_version(); | 195 handler->provider_host_->running_hosted_version(); |
127 } | 196 } |
128 | 197 |
129 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( | 198 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( |
130 int old_process_id) { | 199 int old_process_id) { |
200 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
201 switches::kEnableBrowserSideNavigation)); | |
131 if (!provider_host_ || !context_) | 202 if (!provider_host_ || !context_) |
132 return; | 203 return; |
133 old_process_id_ = old_process_id; | 204 old_process_id_ = old_process_id; |
134 old_provider_id_ = provider_host_->provider_id(); | 205 old_provider_id_ = provider_host_->provider_id(); |
135 host_for_cross_site_transfer_ = | 206 host_for_cross_site_transfer_ = context_->TransferProviderHostOut( |
136 context_->TransferProviderHostOut(old_process_id, | 207 old_process_id, provider_host_->provider_id()); |
137 provider_host_->provider_id()); | |
138 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); | 208 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); |
139 } | 209 } |
140 | 210 |
141 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer( | 211 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer( |
142 int new_process_id, int new_provider_id) { | 212 int new_process_id, int new_provider_id) { |
213 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
214 switches::kEnableBrowserSideNavigation)); | |
143 if (!host_for_cross_site_transfer_.get() || !context_) | 215 if (!host_for_cross_site_transfer_.get() || !context_) |
144 return; | 216 return; |
145 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); | 217 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); |
146 context_->TransferProviderHostIn( | 218 context_->TransferProviderHostIn(new_process_id, new_provider_id, |
147 new_process_id, | 219 host_for_cross_site_transfer_.Pass()); |
148 new_provider_id, | |
149 host_for_cross_site_transfer_.Pass()); | |
150 DCHECK_EQ(provider_host_->provider_id(), new_provider_id); | 220 DCHECK_EQ(provider_host_->provider_id(), new_provider_id); |
151 } | 221 } |
152 | 222 |
153 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess( | 223 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess( |
154 int old_process_id) { | 224 int old_process_id) { |
225 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
226 switches::kEnableBrowserSideNavigation)); | |
155 if (!host_for_cross_site_transfer_.get() || !context_ || | 227 if (!host_for_cross_site_transfer_.get() || !context_ || |
156 old_process_id_ != old_process_id) { | 228 old_process_id_ != old_process_id) { |
157 return; | 229 return; |
158 } | 230 } |
159 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_); | 231 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_); |
160 } | 232 } |
161 | 233 |
162 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { | 234 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { |
163 } | 235 } |
164 | 236 |
165 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( | 237 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( |
166 base::WeakPtr<ServiceWorkerContextCore> context, | 238 base::WeakPtr<ServiceWorkerContextCore> context, |
167 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 239 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
168 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 240 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
169 ResourceType resource_type) | 241 ResourceType resource_type) |
170 : context_(context), | 242 : context_(context), |
171 provider_host_(provider_host), | 243 provider_host_(provider_host), |
172 blob_storage_context_(blob_storage_context), | 244 blob_storage_context_(blob_storage_context), |
173 resource_type_(resource_type), | 245 resource_type_(resource_type), |
174 old_process_id_(0), | 246 old_process_id_(0), |
175 old_provider_id_(kInvalidServiceWorkerProviderId) { | 247 old_provider_id_(kInvalidServiceWorkerProviderId) { |
176 } | 248 } |
177 | 249 |
178 } // namespace content | 250 } // namespace content |
OLD | NEW |