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

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

Issue 475333002: [ServiceWorker] Sends the blob uuid of the request body to the ServiceWorker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: resolve blobs Created 6 years, 4 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_url_request_job.h" 5 #include "content/browser/service_worker/service_worker_url_request_job.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/guid.h"
12 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
13 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" 14 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h"
14 #include "content/browser/service_worker/service_worker_provider_host.h" 15 #include "content/browser/service_worker/service_worker_provider_host.h"
16 #include "content/common/resource_request_body.h"
17 #include "content/common/service_worker/service_worker_types.h"
18 #include "content/public/browser/blob_handle.h"
19 #include "content/public/browser/resource_request_info.h"
20 #include "content/public/common/page_transition_types.h"
15 #include "net/http/http_request_headers.h" 21 #include "net/http/http_request_headers.h"
16 #include "net/http/http_response_headers.h" 22 #include "net/http/http_response_headers.h"
17 #include "net/http/http_response_info.h" 23 #include "net/http/http_response_info.h"
18 #include "net/http/http_util.h" 24 #include "net/http/http_util.h"
19 #include "webkit/browser/blob/blob_data_handle.h" 25 #include "webkit/browser/blob/blob_data_handle.h"
20 #include "webkit/browser/blob/blob_storage_context.h" 26 #include "webkit/browser/blob/blob_storage_context.h"
21 #include "webkit/browser/blob/blob_url_request_job_factory.h" 27 #include "webkit/browser/blob/blob_url_request_job_factory.h"
22 28
23 namespace content { 29 namespace content {
24 30
25 ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob( 31 ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob(
26 net::URLRequest* request, 32 net::URLRequest* request,
27 net::NetworkDelegate* network_delegate, 33 net::NetworkDelegate* network_delegate,
28 base::WeakPtr<ServiceWorkerProviderHost> provider_host, 34 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
29 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context) 35 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context,
36 scoped_refptr<ResourceRequestBody> body)
30 : net::URLRequestJob(request, network_delegate), 37 : net::URLRequestJob(request, network_delegate),
31 provider_host_(provider_host), 38 provider_host_(provider_host),
32 response_type_(NOT_DETERMINED), 39 response_type_(NOT_DETERMINED),
33 is_started_(false), 40 is_started_(false),
34 blob_storage_context_(blob_storage_context), 41 blob_storage_context_(blob_storage_context),
42 body_(body),
35 weak_factory_(this) { 43 weak_factory_(this) {
36 } 44 }
37 45
38 void ServiceWorkerURLRequestJob::FallbackToNetwork() { 46 void ServiceWorkerURLRequestJob::FallbackToNetwork() {
39 DCHECK_EQ(NOT_DETERMINED, response_type_); 47 DCHECK_EQ(NOT_DETERMINED, response_type_);
40 response_type_ = FALLBACK_TO_NETWORK; 48 response_type_ = FALLBACK_TO_NETWORK;
41 MaybeStartRequest(); 49 MaybeStartRequest();
42 } 50 }
43 51
44 void ServiceWorkerURLRequestJob::ForwardToServiceWorker() { 52 void ServiceWorkerURLRequestJob::ForwardToServiceWorker() {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 case FALLBACK_TO_NETWORK: 217 case FALLBACK_TO_NETWORK:
210 // Restart the request to create a new job. Our request handler will 218 // Restart the request to create a new job. Our request handler will
211 // return NULL, and the default job (which will hit network) should be 219 // return NULL, and the default job (which will hit network) should be
212 // created. 220 // created.
213 NotifyRestartRequired(); 221 NotifyRestartRequired();
214 return; 222 return;
215 223
216 case FORWARD_TO_SERVICE_WORKER: 224 case FORWARD_TO_SERVICE_WORKER:
217 DCHECK(provider_host_ && provider_host_->active_version()); 225 DCHECK(provider_host_ && provider_host_->active_version());
218 DCHECK(!fetch_dispatcher_); 226 DCHECK(!fetch_dispatcher_);
219
220 // Send a fetch event to the ServiceWorker associated to the 227 // Send a fetch event to the ServiceWorker associated to the
221 // provider_host. 228 // provider_host.
222 fetch_dispatcher_.reset(new ServiceWorkerFetchDispatcher( 229 fetch_dispatcher_.reset(new ServiceWorkerFetchDispatcher(
223 request(), provider_host_->active_version(), 230 CreateFetchRequest(),
231 provider_host_->active_version(),
224 base::Bind(&ServiceWorkerURLRequestJob::DidDispatchFetchEvent, 232 base::Bind(&ServiceWorkerURLRequestJob::DidDispatchFetchEvent,
225 weak_factory_.GetWeakPtr()))); 233 weak_factory_.GetWeakPtr())));
226 fetch_dispatcher_->Run(); 234 fetch_dispatcher_->Run();
227 return; 235 return;
228 } 236 }
229 237
230 NOTREACHED(); 238 NOTREACHED();
231 } 239 }
232 240
241 scoped_ptr<ServiceWorkerFetchRequest>
242 ServiceWorkerURLRequestJob::CreateFetchRequest() {
243 std::string blob_uuid;
244 uint64 blob_size = 0;
245 CreateRequestBodyBlob(&blob_uuid, &blob_size);
246 scoped_ptr<ServiceWorkerFetchRequest> request(
247 new ServiceWorkerFetchRequest());
248
249 request->url = request_->url();
250 request->method = request_->method();
251 const net::HttpRequestHeaders& headers = request_->extra_request_headers();
252 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();)
253 request->headers[it.name()] = it.value();
254 request->blob_uuid = blob_uuid;
255 request->blob_size = blob_size;
256 request->referrer = GURL(request_->referrer());
257 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_);
258 if (info) {
259 request->is_reload = PageTransitionCoreTypeIs(info->GetPageTransition(),
260 PAGE_TRANSITION_RELOAD);
261 }
262 return request.Pass();
263 }
264
265 bool ServiceWorkerURLRequestJob::CreateRequestBodyBlob(std::string* blob_uuid,
266 uint64* blob_size) {
267 if (!body_ || !blob_storage_context_)
268 return false;
269 const std::string uuid(base::GenerateGUID());
270 uint64 size = 0;
271 std::vector<const ResourceRequestBody::Element*> resolved_elements;
272 for (size_t i = 0; i < body_->elements()->size(); ++i) {
273 const ResourceRequestBody::Element& element = (*body_->elements())[i];
274 if (element.type() != ResourceRequestBody::Element::TYPE_BLOB) {
275 resolved_elements.push_back(&element);
276 continue;
277 }
278 scoped_ptr<webkit_blob::BlobDataHandle> handle =
279 blob_storage_context_->GetBlobDataFromUUID(element.blob_uuid());
280 if (handle->data()->items().empty())
281 continue;
282 for (size_t i = 0; i < handle->data()->items().size(); ++i) {
283 const webkit_blob::BlobData::Item& item = handle->data()->items().at(i);
284 DCHECK_NE(webkit_blob::BlobData::Item::TYPE_BLOB, item.type());
285 resolved_elements.push_back(&item);
286 }
287 }
288 scoped_refptr<webkit_blob::BlobData> blob_data =
289 new webkit_blob::BlobData(uuid);
290 for (size_t i = 0; i < resolved_elements.size(); ++i) {
291 const ResourceRequestBody::Element& element = *resolved_elements[i];
292 size += element.length();
293 switch (element.type()) {
294 case ResourceRequestBody::Element::TYPE_BYTES:
295 blob_data->AppendData(element.bytes(), element.length());
296 break;
297 case ResourceRequestBody::Element::TYPE_FILE:
298 blob_data->AppendFile(element.path(),
299 element.offset(),
300 element.length(),
301 element.expected_modification_time());
302 break;
303 case ResourceRequestBody::Element::TYPE_BLOB:
304 // Blob elements should be resolved beforehand.
305 NOTREACHED();
306 break;
307 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM:
308 blob_data->AppendFileSystemFile(element.filesystem_url(),
309 element.offset(),
310 element.length(),
311 element.expected_modification_time());
312 break;
313 default:
314 NOTIMPLEMENTED();
315 }
316 }
317
318 request_body_blob_data_handle_ =
319 blob_storage_context_->AddFinishedBlob(blob_data.get());
320 *blob_uuid = uuid;
321 *blob_size = size;
322 return true;
323 }
324
233 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( 325 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent(
234 ServiceWorkerStatusCode status, 326 ServiceWorkerStatusCode status,
235 ServiceWorkerFetchEventResult fetch_result, 327 ServiceWorkerFetchEventResult fetch_result,
236 const ServiceWorkerResponse& response) { 328 const ServiceWorkerResponse& response) {
237 fetch_dispatcher_.reset(); 329 fetch_dispatcher_.reset();
238 330
239 // Check if we're not orphaned. 331 // Check if we're not orphaned.
240 if (!request()) 332 if (!request())
241 return; 333 return;
242 334
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 void ServiceWorkerURLRequestJob::DeliverErrorResponse() { 406 void ServiceWorkerURLRequestJob::DeliverErrorResponse() {
315 // TODO(falken): Print an error to the console of the ServiceWorker and of 407 // TODO(falken): Print an error to the console of the ServiceWorker and of
316 // the requesting page. 408 // the requesting page.
317 CreateResponseHeader(500, 409 CreateResponseHeader(500,
318 "Service Worker Response Error", 410 "Service Worker Response Error",
319 std::map<std::string, std::string>()); 411 std::map<std::string, std::string>());
320 CommitResponseHeader(); 412 CommitResponseHeader();
321 } 413 }
322 414
323 } // namespace content 415 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698