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