| 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_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/guid.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 13 #include "base/profiler/scoped_tracker.h" | 14 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "content/browser/resource_context_impl.h" | 17 #include "content/browser/resource_context_impl.h" |
| 17 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | 18 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" |
| 18 #include "content/browser/service_worker/service_worker_provider_host.h" | 19 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 19 #include "content/browser/streams/stream.h" | 20 #include "content/browser/streams/stream.h" |
| 20 #include "content/browser/streams/stream_context.h" | 21 #include "content/browser/streams/stream_context.h" |
| 21 #include "content/browser/streams/stream_registry.h" | 22 #include "content/browser/streams/stream_registry.h" |
| 22 #include "content/common/resource_request_body.h" | 23 #include "content/common/resource_request_body.h" |
| 23 #include "content/common/service_worker/service_worker_types.h" | 24 #include "content/common/service_worker/service_worker_types.h" |
| 24 #include "content/public/browser/blob_handle.h" | 25 #include "content/public/browser/blob_handle.h" |
| 25 #include "content/public/browser/resource_request_info.h" | 26 #include "content/public/browser/resource_request_info.h" |
| 26 #include "content/public/browser/service_worker_context.h" | 27 #include "content/public/browser/service_worker_context.h" |
| 27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 28 #include "net/http/http_request_headers.h" | 29 #include "net/http/http_request_headers.h" |
| 29 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 30 #include "net/http/http_response_info.h" | 31 #include "net/http/http_response_info.h" |
| 31 #include "net/http/http_util.h" | 32 #include "net/http/http_util.h" |
| 33 #include "storage/browser/blob/blob_data_builder.h" |
| 32 #include "storage/browser/blob/blob_data_handle.h" | 34 #include "storage/browser/blob/blob_data_handle.h" |
| 33 #include "storage/browser/blob/blob_storage_context.h" | 35 #include "storage/browser/blob/blob_storage_context.h" |
| 34 #include "storage/browser/blob/blob_url_request_job_factory.h" | 36 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 35 #include "ui/base/page_transition_types.h" | 37 #include "ui/base/page_transition_types.h" |
| 36 | 38 |
| 37 namespace content { | 39 namespace content { |
| 38 | 40 |
| 39 ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob( | 41 ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob( |
| 40 net::URLRequest* request, | 42 net::URLRequest* request, |
| 41 net::NetworkDelegate* network_delegate, | 43 net::NetworkDelegate* network_delegate, |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 info->GetPageTransition(), ui::PAGE_TRANSITION_RELOAD); | 404 info->GetPageTransition(), ui::PAGE_TRANSITION_RELOAD); |
| 403 } | 405 } |
| 404 return request.Pass(); | 406 return request.Pass(); |
| 405 } | 407 } |
| 406 | 408 |
| 407 bool ServiceWorkerURLRequestJob::CreateRequestBodyBlob(std::string* blob_uuid, | 409 bool ServiceWorkerURLRequestJob::CreateRequestBodyBlob(std::string* blob_uuid, |
| 408 uint64* blob_size) { | 410 uint64* blob_size) { |
| 409 if (!body_.get() || !blob_storage_context_) | 411 if (!body_.get() || !blob_storage_context_) |
| 410 return false; | 412 return false; |
| 411 | 413 |
| 414 // To ensure the blobs stick around until the end of the reading. |
| 415 ScopedVector<storage::BlobDataHandle> handles; |
| 416 ScopedVector<storage::BlobDataSnapshot> snapshots; |
| 417 // TODO(dmurph): Allow blobs to be added below, so that the context can |
| 418 // efficiently re-use blob items for the new blob. |
| 412 std::vector<const ResourceRequestBody::Element*> resolved_elements; | 419 std::vector<const ResourceRequestBody::Element*> resolved_elements; |
| 413 for (size_t i = 0; i < body_->elements()->size(); ++i) { | 420 for (const ResourceRequestBody::Element& element : (*body_->elements())) { |
| 414 const ResourceRequestBody::Element& element = (*body_->elements())[i]; | |
| 415 if (element.type() != ResourceRequestBody::Element::TYPE_BLOB) { | 421 if (element.type() != ResourceRequestBody::Element::TYPE_BLOB) { |
| 416 resolved_elements.push_back(&element); | 422 resolved_elements.push_back(&element); |
| 417 continue; | 423 continue; |
| 418 } | 424 } |
| 419 scoped_ptr<storage::BlobDataHandle> handle = | 425 scoped_ptr<storage::BlobDataHandle> handle = |
| 420 blob_storage_context_->GetBlobDataFromUUID(element.blob_uuid()); | 426 blob_storage_context_->GetBlobDataFromUUID(element.blob_uuid()); |
| 421 if (handle->data()->items().empty()) | 427 scoped_ptr<storage::BlobDataSnapshot> snapshot = handle->CreateSnapshot(); |
| 428 if (snapshot->items().empty()) |
| 422 continue; | 429 continue; |
| 423 for (size_t i = 0; i < handle->data()->items().size(); ++i) { | 430 const auto& items = snapshot->items(); |
| 424 const storage::BlobData::Item& item = handle->data()->items().at(i); | 431 for (const auto& item : items) { |
| 425 DCHECK_NE(storage::BlobData::Item::TYPE_BLOB, item.type()); | 432 DCHECK_NE(storage::DataElement::TYPE_BLOB, item->type()); |
| 426 resolved_elements.push_back(&item); | 433 resolved_elements.push_back(item->data_element_ptr()); |
| 427 } | 434 } |
| 435 handles.push_back(handle.release()); |
| 436 snapshots.push_back(snapshot.release()); |
| 428 } | 437 } |
| 429 | 438 |
| 430 const std::string uuid(base::GenerateGUID()); | 439 const std::string uuid(base::GenerateGUID()); |
| 431 uint64 total_size = 0; | 440 uint64 total_size = 0; |
| 432 scoped_refptr<storage::BlobData> blob_data = new storage::BlobData(uuid); | 441 |
| 442 storage::BlobDataBuilder blob_builder(uuid); |
| 433 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 443 for (size_t i = 0; i < resolved_elements.size(); ++i) { |
| 434 const ResourceRequestBody::Element& element = *resolved_elements[i]; | 444 const ResourceRequestBody::Element& element = *resolved_elements[i]; |
| 435 if (total_size != kuint64max && element.length() != kuint64max) | 445 if (total_size != kuint64max && element.length() != kuint64max) |
| 436 total_size += element.length(); | 446 total_size += element.length(); |
| 437 else | 447 else |
| 438 total_size = kuint64max; | 448 total_size = kuint64max; |
| 439 switch (element.type()) { | 449 switch (element.type()) { |
| 440 case ResourceRequestBody::Element::TYPE_BYTES: | 450 case ResourceRequestBody::Element::TYPE_BYTES: |
| 441 blob_data->AppendData(element.bytes(), element.length()); | 451 blob_builder.AppendData(element.bytes(), element.length()); |
| 442 break; | 452 break; |
| 443 case ResourceRequestBody::Element::TYPE_FILE: | 453 case ResourceRequestBody::Element::TYPE_FILE: |
| 444 blob_data->AppendFile(element.path(), | 454 blob_builder.AppendFile(element.path(), element.offset(), |
| 445 element.offset(), | 455 element.length(), |
| 446 element.length(), | 456 element.expected_modification_time()); |
| 447 element.expected_modification_time()); | |
| 448 break; | 457 break; |
| 449 case ResourceRequestBody::Element::TYPE_BLOB: | 458 case ResourceRequestBody::Element::TYPE_BLOB: |
| 450 // Blob elements should be resolved beforehand. | 459 // Blob elements should be resolved beforehand. |
| 451 NOTREACHED(); | 460 NOTREACHED(); |
| 452 break; | 461 break; |
| 453 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: | 462 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: |
| 454 blob_data->AppendFileSystemFile(element.filesystem_url(), | 463 blob_builder.AppendFileSystemFile(element.filesystem_url(), |
| 455 element.offset(), | 464 element.offset(), element.length(), |
| 456 element.length(), | 465 element.expected_modification_time()); |
| 457 element.expected_modification_time()); | |
| 458 break; | 466 break; |
| 459 default: | 467 default: |
| 460 NOTIMPLEMENTED(); | 468 NOTIMPLEMENTED(); |
| 461 } | 469 } |
| 462 } | 470 } |
| 463 | 471 |
| 464 request_body_blob_data_handle_ = | 472 request_body_blob_data_handle_ = |
| 465 blob_storage_context_->AddFinishedBlob(blob_data.get()); | 473 blob_storage_context_->AddFinishedBlob(blob_builder); |
| 466 *blob_uuid = uuid; | 474 *blob_uuid = uuid; |
| 467 *blob_size = total_size; | 475 *blob_size = total_size; |
| 468 return true; | 476 return true; |
| 469 } | 477 } |
| 470 | 478 |
| 471 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { | 479 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { |
| 472 fetch_ready_time_ = base::TimeTicks::Now(); | 480 fetch_ready_time_ = base::TimeTicks::Now(); |
| 473 } | 481 } |
| 474 | 482 |
| 475 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( | 483 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 } | 627 } |
| 620 if (!waiting_stream_url_.is_empty()) { | 628 if (!waiting_stream_url_.is_empty()) { |
| 621 StreamRegistry* stream_registry = | 629 StreamRegistry* stream_registry = |
| 622 GetStreamContextForResourceContext(resource_context_)->registry(); | 630 GetStreamContextForResourceContext(resource_context_)->registry(); |
| 623 stream_registry->RemoveRegisterObserver(waiting_stream_url_); | 631 stream_registry->RemoveRegisterObserver(waiting_stream_url_); |
| 624 stream_registry->AbortPendingStream(waiting_stream_url_); | 632 stream_registry->AbortPendingStream(waiting_stream_url_); |
| 625 } | 633 } |
| 626 } | 634 } |
| 627 | 635 |
| 628 } // namespace content | 636 } // namespace content |
| OLD | NEW |