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" |
| 10 #include "content/browser/loader/navigation_url_loader_impl_core.h" |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.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/content_switches.h" |
18 #include "content/public/common/origin_util.h" | 21 #include "content/public/common/origin_util.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 |
(...skipping 29 matching lines...) Expand all Loading... |
57 storage::BlobStorageContext* blob_storage_context, | 60 storage::BlobStorageContext* blob_storage_context, |
58 int process_id, | 61 int process_id, |
59 int provider_id, | 62 int provider_id, |
60 bool skip_service_worker, | 63 bool skip_service_worker, |
61 FetchRequestMode request_mode, | 64 FetchRequestMode request_mode, |
62 FetchCredentialsMode credentials_mode, | 65 FetchCredentialsMode credentials_mode, |
63 FetchRedirectMode redirect_mode, | 66 FetchRedirectMode redirect_mode, |
64 ResourceType resource_type, | 67 ResourceType resource_type, |
65 RequestContextType request_context_type, | 68 RequestContextType request_context_type, |
66 RequestContextFrameType frame_type, | 69 RequestContextFrameType frame_type, |
67 scoped_refptr<ResourceRequestBody> body) { | 70 scoped_refptr<ResourceRequestBody> body, |
| 71 NavigationURLLoaderImplCore* loader) { |
68 // Create the handler even for insecure HTTP since it's used in the | 72 // Create the handler even for insecure HTTP since it's used in the |
69 // case of redirect to HTTPS. | 73 // case of redirect to HTTPS. |
70 if (!request->url().SchemeIsHTTPOrHTTPS() && | 74 if (!request->url().SchemeIsHTTPOrHTTPS() && |
71 !OriginCanAccessServiceWorkers(request->url())) { | 75 !OriginCanAccessServiceWorkers(request->url())) { |
72 return; | 76 return; |
73 } | 77 } |
74 | 78 |
75 if (!context_wrapper || !context_wrapper->context() || | 79 if (!context_wrapper || !context_wrapper->context() || |
76 provider_id == kInvalidServiceWorkerProviderId) { | 80 provider_id == kInvalidServiceWorkerProviderId) { |
77 return; | 81 return; |
78 } | 82 } |
79 | 83 |
80 ServiceWorkerProviderHost* provider_host = | 84 ServiceWorkerProviderHost* provider_host = nullptr; |
81 context_wrapper->context()->GetProviderHost(process_id, provider_id); | 85 if (process_id == -1) { |
| 86 // PlzNavigate |
| 87 // Initialize a dummy SWProviderHost for browser-initiated navigations. |
| 88 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 89 switches::kEnableBrowserSideNavigation)); |
| 90 DCHECK(loader); |
| 91 DCHECK(ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)); |
| 92 provider_host = |
| 93 context_wrapper->context()->CreateBrowserProviderHost(provider_id); |
| 94 } else { |
| 95 provider_host = |
| 96 context_wrapper->context()->GetProviderHost(process_id, provider_id); |
| 97 } |
| 98 |
82 if (!provider_host || !provider_host->IsContextAlive()) | 99 if (!provider_host || !provider_host->IsContextAlive()) |
83 return; | 100 return; |
84 | 101 |
85 if (skip_service_worker) { | 102 if (skip_service_worker) { |
86 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) { | 103 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) { |
| 104 // TODO(horo): Does this work properly for PlzNavigate? |
87 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url())); | 105 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url())); |
88 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies()); | 106 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies()); |
89 // A page load with skip_service_worker should be triggered by | 107 // A page load with skip_service_worker should be triggered by |
90 // shift-reload, so retain all live matching registrations. | 108 // shift-reload, so retain all live matching registrations. |
91 provider_host->AddAllMatchingRegistrations(); | 109 provider_host->AddAllMatchingRegistrations(); |
92 } | 110 } |
93 return; | 111 return; |
94 } | 112 } |
95 | 113 |
96 scoped_ptr<ServiceWorkerRequestHandler> handler( | 114 scoped_ptr<ServiceWorkerRequestHandler> handler( |
97 provider_host->CreateRequestHandler( | 115 provider_host->CreateRequestHandler( |
98 request_mode, credentials_mode, redirect_mode, resource_type, | 116 request_mode, credentials_mode, redirect_mode, resource_type, |
99 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), | 117 request_context_type, frame_type, blob_storage_context->AsWeakPtr(), |
100 body)); | 118 body)); |
101 if (!handler) | 119 if (!handler) |
102 return; | 120 return; |
103 | 121 |
104 request->SetUserData(&kUserDataKey, handler.release()); | 122 request->SetUserData(&kUserDataKey, handler.release()); |
| 123 if (process_id == -1) |
| 124 loader->SetServiceWorkerHandler(handler.get()); |
105 } | 125 } |
106 | 126 |
107 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( | 127 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( |
108 net::URLRequest* request) { | 128 net::URLRequest* request) { |
109 return static_cast<ServiceWorkerRequestHandler*>( | 129 return static_cast<ServiceWorkerRequestHandler*>( |
110 request->GetUserData(&kUserDataKey)); | 130 request->GetUserData(&kUserDataKey)); |
111 } | 131 } |
112 | 132 |
113 scoped_ptr<net::URLRequestInterceptor> | 133 scoped_ptr<net::URLRequestInterceptor> |
114 ServiceWorkerRequestHandler::CreateInterceptor( | 134 ServiceWorkerRequestHandler::CreateInterceptor( |
115 ResourceContext* resource_context) { | 135 ResourceContext* resource_context) { |
116 return scoped_ptr<net::URLRequestInterceptor>( | 136 return scoped_ptr<net::URLRequestInterceptor>( |
117 new ServiceWorkerRequestInterceptor(resource_context)); | 137 new ServiceWorkerRequestInterceptor(resource_context)); |
118 } | 138 } |
119 | 139 |
120 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker( | 140 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker( |
121 net::URLRequest* request) { | 141 net::URLRequest* request) { |
122 ServiceWorkerRequestHandler* handler = GetHandler(request); | 142 ServiceWorkerRequestHandler* handler = GetHandler(request); |
123 if (!handler || !handler->provider_host_) | 143 if (!handler || !handler->provider_host_) |
124 return false; | 144 return false; |
125 return handler->provider_host_->associated_registration() || | 145 return handler->provider_host_->associated_registration() || |
126 handler->provider_host_->running_hosted_version(); | 146 handler->provider_host_->running_hosted_version(); |
127 } | 147 } |
128 | 148 |
129 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( | 149 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( |
130 int old_process_id) { | 150 int old_process_id) { |
| 151 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 152 switches::kEnableBrowserSideNavigation)); |
131 if (!provider_host_ || !context_) | 153 if (!provider_host_ || !context_) |
132 return; | 154 return; |
133 old_process_id_ = old_process_id; | 155 old_process_id_ = old_process_id; |
134 old_provider_id_ = provider_host_->provider_id(); | 156 old_provider_id_ = provider_host_->provider_id(); |
135 host_for_cross_site_transfer_ = | 157 temporary_host_ = context_->TransferProviderHostOut( |
136 context_->TransferProviderHostOut(old_process_id, | 158 old_process_id, provider_host_->provider_id()); |
137 provider_host_->provider_id()); | 159 DCHECK_EQ(provider_host_.get(), temporary_host_.get()); |
138 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); | |
139 } | 160 } |
140 | 161 |
141 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer( | 162 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer( |
142 int new_process_id, int new_provider_id) { | 163 int new_process_id, int new_provider_id) { |
143 if (!host_for_cross_site_transfer_.get() || !context_) | 164 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 165 switches::kEnableBrowserSideNavigation)); |
| 166 if (!temporary_host_.get() || !context_) |
144 return; | 167 return; |
145 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); | 168 DCHECK_EQ(provider_host_.get(), temporary_host_.get()); |
146 context_->TransferProviderHostIn( | 169 context_->TransferProviderHostIn(new_process_id, new_provider_id, |
147 new_process_id, | 170 temporary_host_.Pass()); |
148 new_provider_id, | |
149 host_for_cross_site_transfer_.Pass()); | |
150 DCHECK_EQ(provider_host_->provider_id(), new_provider_id); | 171 DCHECK_EQ(provider_host_->provider_id(), new_provider_id); |
151 } | 172 } |
152 | 173 |
153 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess( | 174 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess( |
154 int old_process_id) { | 175 int old_process_id) { |
155 if (!host_for_cross_site_transfer_.get() || !context_ || | 176 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 177 switches::kEnableBrowserSideNavigation)); |
| 178 if (!temporary_host_.get() || !context_ || |
156 old_process_id_ != old_process_id) { | 179 old_process_id_ != old_process_id) { |
157 return; | 180 return; |
158 } | 181 } |
159 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_); | 182 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_); |
160 } | 183 } |
161 | 184 |
| 185 scoped_ptr<ServiceWorkerProviderHost> |
| 186 ServiceWorkerRequestHandler::TakeBrowserProviderHost() { |
| 187 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 188 switches::kEnableBrowserSideNavigation)); |
| 189 return temporary_host_.Pass(); |
| 190 } |
| 191 |
| 192 void ServiceWorkerRequestHandler::SetBrowserProviderHost( |
| 193 scoped_ptr<ServiceWorkerProviderHost> provider_host) { |
| 194 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 195 switches::kEnableBrowserSideNavigation)); |
| 196 temporary_host_ = provider_host.Pass(); |
| 197 } |
| 198 |
162 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { | 199 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { |
163 } | 200 } |
164 | 201 |
165 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( | 202 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( |
166 base::WeakPtr<ServiceWorkerContextCore> context, | 203 base::WeakPtr<ServiceWorkerContextCore> context, |
167 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 204 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
168 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 205 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
169 ResourceType resource_type) | 206 ResourceType resource_type) |
170 : context_(context), | 207 : context_(context), |
171 provider_host_(provider_host), | 208 provider_host_(provider_host), |
172 blob_storage_context_(blob_storage_context), | 209 blob_storage_context_(blob_storage_context), |
173 resource_type_(resource_type), | 210 resource_type_(resource_type), |
174 old_process_id_(0), | 211 old_process_id_(0), |
175 old_provider_id_(kInvalidServiceWorkerProviderId) { | 212 old_provider_id_(kInvalidServiceWorkerProviderId) { |
176 } | 213 } |
177 | 214 |
178 } // namespace content | 215 } // namespace content |
OLD | NEW |