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

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

Issue 1294243004: PlzNavigate: Make ServiceWorker work with PlzNavigate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 2 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 8
9 #include "base/command_line.h"
10 #include "content/browser/frame_host/navigation_handle_impl.h"
11 #include "content/browser/loader/navigation_url_loader_impl_core.h"
michaeln 2015/10/01 01:42:16 are the navstack includes still needed?
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 No, that's leftovers from earlier attempts, remove
9 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" 13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h" 14 #include "content/browser/service_worker/service_worker_provider_host.h"
12 #include "content/browser/service_worker/service_worker_registration.h" 15 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_url_request_job.h" 16 #include "content/browser/service_worker/service_worker_url_request_job.h"
14 #include "content/common/resource_request_body.h" 17 #include "content/common/resource_request_body.h"
15 #include "content/common/service_worker/service_worker_types.h" 18 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/common/service_worker/service_worker_utils.h" 19 #include "content/common/service_worker/service_worker_utils.h"
17 #include "content/public/browser/resource_context.h" 20 #include "content/public/browser/resource_context.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"
michaeln 2015/10/01 01:42:16 is this needed? what brought this include in?
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 MSG_ROUTING_NONE
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 15 matching lines...) Expand all
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
52 } // namespace 57 } // namespace
53 58
59 // PlzNavigate
60 void ServiceWorkerRequestHandler::InitializeForNavigation(
61 net::URLRequest* request,
62 ServiceWorkerContextWrapper* context_wrapper,
63 storage::BlobStorageContext* blob_storage_context,
64 int provider_id,
65 bool skip_service_worker,
66 ResourceType resource_type,
67 RequestContextType request_context_type,
68 RequestContextFrameType frame_type,
69 scoped_refptr<ResourceRequestBody> body) {
70 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
michaeln 2015/10/01 01:42:16 CHECK vs DCHECK?
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 We use CHECK for PlzNavigate checks in the browser
71 switches::kEnableBrowserSideNavigation));
72
73 // Create the handler even for insecure HTTP since it's used in the
74 // case of redirect to HTTPS.
75 if (!request->url().SchemeIsHTTPOrHTTPS() &&
76 !OriginCanAccessServiceWorkers(request->url())) {
77 return;
78 }
79
80 if (!context_wrapper || !context_wrapper->context()) {
81 return;
82 }
83
84 // Initialize the SWProviderHost.
85 ServiceWorkerProviderHost* provider_host = new ServiceWorkerProviderHost(
86 ServiceWorkerProviderHost::kVirtualProcessIDForBrowserRequest,
87 MSG_ROUTING_NONE, provider_id, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
88 context_wrapper->context()->AsWeakPtr(), nullptr);
89
90 // Add to context map.
91 context_wrapper->context()->AddNavigationProviderHost(provider_host);
michaeln 2015/10/01 01:42:16 I really don't like the passing around of int type
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 I changed the API in ServiceWorkerContextCore to u
92
93 if (skip_service_worker) {
94 // TODO(horo): Does this work properly for PlzNavigate?
95 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) {
96 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url()));
97 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies());
98 // A page load with skip_service_worker should be triggered by
99 // shift-reload, so retain all live matching registrations.
100 provider_host->AddAllMatchingRegistrations();
101 }
102 return;
103 }
104
105 scoped_ptr<ServiceWorkerRequestHandler> handler(
106 provider_host->CreateRequestHandler(
107 FETCH_REQUEST_MODE_SAME_ORIGIN, FETCH_CREDENTIALS_MODE_INCLUDE,
108 FetchRedirectMode::MANUAL_MODE, resource_type, request_context_type,
109 frame_type, blob_storage_context->AsWeakPtr(), body));
110 if (!handler)
111 return;
112
113 request->SetUserData(&kUserDataKey, handler.release());
114 }
115
54 void ServiceWorkerRequestHandler::InitializeHandler( 116 void ServiceWorkerRequestHandler::InitializeHandler(
55 net::URLRequest* request, 117 net::URLRequest* request,
56 ServiceWorkerContextWrapper* context_wrapper, 118 ServiceWorkerContextWrapper* context_wrapper,
57 storage::BlobStorageContext* blob_storage_context, 119 storage::BlobStorageContext* blob_storage_context,
58 int process_id, 120 int process_id,
59 int provider_id, 121 int provider_id,
60 bool skip_service_worker, 122 bool skip_service_worker,
61 FetchRequestMode request_mode, 123 FetchRequestMode request_mode,
62 FetchCredentialsMode credentials_mode, 124 FetchCredentialsMode credentials_mode,
63 FetchRedirectMode redirect_mode, 125 FetchRedirectMode redirect_mode,
(...skipping 11 matching lines...) Expand all
75 if (!context_wrapper || !context_wrapper->context() || 137 if (!context_wrapper || !context_wrapper->context() ||
76 provider_id == kInvalidServiceWorkerProviderId) { 138 provider_id == kInvalidServiceWorkerProviderId) {
77 return; 139 return;
78 } 140 }
79 141
80 ServiceWorkerProviderHost* provider_host = 142 ServiceWorkerProviderHost* provider_host =
81 context_wrapper->context()->GetProviderHost(process_id, provider_id); 143 context_wrapper->context()->GetProviderHost(process_id, provider_id);
82 if (!provider_host || !provider_host->IsContextAlive()) 144 if (!provider_host || !provider_host->IsContextAlive())
83 return; 145 return;
84 146
85 if (skip_service_worker) { 147 if (skip_service_worker) {
michaeln 2015/10/01 01:42:16 please refctor and share common code between these
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 This is kinda awkward due to the large number of a
86 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) { 148 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) {
87 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url())); 149 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url()));
88 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies()); 150 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies());
89 // A page load with skip_service_worker should be triggered by 151 // A page load with skip_service_worker should be triggered by
90 // shift-reload, so retain all live matching registrations. 152 // shift-reload, so retain all live matching registrations.
91 provider_host->AddAllMatchingRegistrations(); 153 provider_host->AddAllMatchingRegistrations();
92 } 154 }
93 return; 155 return;
94 } 156 }
95 157
(...skipping 25 matching lines...) Expand all
121 net::URLRequest* request) { 183 net::URLRequest* request) {
122 ServiceWorkerRequestHandler* handler = GetHandler(request); 184 ServiceWorkerRequestHandler* handler = GetHandler(request);
123 if (!handler || !handler->provider_host_) 185 if (!handler || !handler->provider_host_)
124 return false; 186 return false;
125 return handler->provider_host_->associated_registration() || 187 return handler->provider_host_->associated_registration() ||
126 handler->provider_host_->running_hosted_version(); 188 handler->provider_host_->running_hosted_version();
127 } 189 }
128 190
129 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer( 191 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
130 int old_process_id) { 192 int old_process_id) {
193 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
michaeln 2015/10/01 01:42:16 ditto question about CHECK vs DCHECK
Fabrice (no longer in Chrome) 2015/10/01 18:29:55 There is no cross-site transfer mechanics in PlzNa
194 switches::kEnableBrowserSideNavigation));
131 if (!provider_host_ || !context_) 195 if (!provider_host_ || !context_)
132 return; 196 return;
133 old_process_id_ = old_process_id; 197 old_process_id_ = old_process_id;
134 old_provider_id_ = provider_host_->provider_id(); 198 old_provider_id_ = provider_host_->provider_id();
135 host_for_cross_site_transfer_ = 199 host_for_cross_site_transfer_ = context_->TransferProviderHostOut(
136 context_->TransferProviderHostOut(old_process_id, 200 old_process_id, provider_host_->provider_id());
137 provider_host_->provider_id());
138 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); 201 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
139 } 202 }
140 203
141 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer( 204 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer(
142 int new_process_id, int new_provider_id) { 205 int new_process_id, int new_provider_id) {
206 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
207 switches::kEnableBrowserSideNavigation));
143 if (!host_for_cross_site_transfer_.get() || !context_) 208 if (!host_for_cross_site_transfer_.get() || !context_)
144 return; 209 return;
145 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get()); 210 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
146 context_->TransferProviderHostIn( 211 context_->TransferProviderHostIn(new_process_id, new_provider_id,
147 new_process_id, 212 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); 213 DCHECK_EQ(provider_host_->provider_id(), new_provider_id);
151 } 214 }
152 215
153 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess( 216 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess(
154 int old_process_id) { 217 int old_process_id) {
218 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
219 switches::kEnableBrowserSideNavigation));
155 if (!host_for_cross_site_transfer_.get() || !context_ || 220 if (!host_for_cross_site_transfer_.get() || !context_ ||
156 old_process_id_ != old_process_id) { 221 old_process_id_ != old_process_id) {
157 return; 222 return;
158 } 223 }
159 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_); 224 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_);
160 } 225 }
161 226
162 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { 227 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
163 } 228 }
164 229
165 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( 230 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
166 base::WeakPtr<ServiceWorkerContextCore> context, 231 base::WeakPtr<ServiceWorkerContextCore> context,
167 base::WeakPtr<ServiceWorkerProviderHost> provider_host, 232 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
168 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, 233 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
169 ResourceType resource_type) 234 ResourceType resource_type)
170 : context_(context), 235 : context_(context),
171 provider_host_(provider_host), 236 provider_host_(provider_host),
172 blob_storage_context_(blob_storage_context), 237 blob_storage_context_(blob_storage_context),
173 resource_type_(resource_type), 238 resource_type_(resource_type),
174 old_process_id_(0), 239 old_process_id_(0),
175 old_provider_id_(kInvalidServiceWorkerProviderId) { 240 old_provider_id_(kInvalidServiceWorkerProviderId) {
176 } 241 }
177 242
178 } // namespace content 243 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698