1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/loader/upload_data_stream_builder.h"
7 #include "base/logging.h"
8 #include "content/browser/fileapi/upload_file_system_file_element_reader.h"
9 #include "content/common/resource_request_body.h"
10 #include "net/base/elements_upload_data_stream.h"
11 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/base/upload_file_element_reader.h"
13 #include "storage/browser/blob/blob_data_handle.h"
14 #include "storage/browser/blob/blob_data_snapshot.h"
15 #include "storage/browser/blob/blob_storage_context.h"
20 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody.
21 class BytesElementReader
: public net::UploadBytesElementReader
{
23 BytesElementReader(ResourceRequestBody
* resource_request_body
,
24 const ResourceRequestBody::Element
& element
)
25 : net::UploadBytesElementReader(element
.bytes(), element
.length()),
26 resource_request_body_(resource_request_body
) {
27 DCHECK_EQ(ResourceRequestBody::Element::TYPE_BYTES
, element
.type());
30 ~BytesElementReader() override
{}
33 scoped_refptr
<ResourceRequestBody
> resource_request_body_
;
35 DISALLOW_COPY_AND_ASSIGN(BytesElementReader
);
38 // A subclass of net::UploadFileElementReader which owns ResourceRequestBody.
39 // This class is necessary to ensure the BlobData and any attached shareable
40 // files survive until upload completion.
41 class FileElementReader
: public net::UploadFileElementReader
{
43 FileElementReader(ResourceRequestBody
* resource_request_body
,
44 base::TaskRunner
* task_runner
,
45 const ResourceRequestBody::Element
& element
)
46 : net::UploadFileElementReader(task_runner
,
50 element
.expected_modification_time()),
51 resource_request_body_(resource_request_body
) {
52 DCHECK_EQ(ResourceRequestBody::Element::TYPE_FILE
, element
.type());
55 ~FileElementReader() override
{}
58 scoped_refptr
<ResourceRequestBody
> resource_request_body_
;
60 DISALLOW_COPY_AND_ASSIGN(FileElementReader
);
63 void ResolveBlobReference(
64 ResourceRequestBody
* body
,
65 storage::BlobStorageContext
* blob_context
,
66 const ResourceRequestBody::Element
& element
,
67 std::vector
<const ResourceRequestBody::Element
*>* resolved_elements
) {
69 scoped_ptr
<storage::BlobDataHandle
> handle
=
70 blob_context
->GetBlobDataFromUUID(element
.blob_uuid());
75 // TODO(dmurph): Create a reader for blobs instead of decomposing the blob
76 // and storing the snapshot on the request to keep the resources around.
77 // Currently a handle is attached to the request in the resource dispatcher
78 // host, so we know the blob won't go away, but it's not very clear or useful.
79 scoped_ptr
<storage::BlobDataSnapshot
> snapshot
= handle
->CreateSnapshot();
80 // If there is no element in the referred blob data, just return.
81 if (snapshot
->items().empty())
84 // Append the elements in the referenced blob data.
85 for (const auto& item
: snapshot
->items()) {
86 DCHECK_NE(storage::DataElement::TYPE_BLOB
, item
->type());
87 resolved_elements
->push_back(item
->data_element_ptr());
89 const void* key
= snapshot
.get();
90 body
->SetUserData(key
, snapshot
.release());
95 scoped_ptr
<net::UploadDataStream
> UploadDataStreamBuilder::Build(
96 ResourceRequestBody
* body
,
97 storage::BlobStorageContext
* blob_context
,
98 storage::FileSystemContext
* file_system_context
,
99 base::TaskRunner
* file_task_runner
) {
100 // Resolve all blob elements.
101 std::vector
<const ResourceRequestBody::Element
*> resolved_elements
;
102 for (size_t i
= 0; i
< body
->elements()->size(); ++i
) {
103 const ResourceRequestBody::Element
& element
= (*body
->elements())[i
];
104 if (element
.type() == ResourceRequestBody::Element::TYPE_BLOB
)
105 ResolveBlobReference(body
, blob_context
, element
, &resolved_elements
);
107 resolved_elements
.push_back(&element
);
110 ScopedVector
<net::UploadElementReader
> element_readers
;
111 for (size_t i
= 0; i
< resolved_elements
.size(); ++i
) {
112 const ResourceRequestBody::Element
& element
= *resolved_elements
[i
];
113 switch (element
.type()) {
114 case ResourceRequestBody::Element::TYPE_BYTES
:
115 element_readers
.push_back(new BytesElementReader(body
, element
));
117 case ResourceRequestBody::Element::TYPE_FILE
:
118 element_readers
.push_back(
119 new FileElementReader(body
, file_task_runner
, element
));
121 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM
:
122 // If |body| contains any filesystem URLs, the caller should have
123 // supplied a FileSystemContext.
124 DCHECK(file_system_context
);
125 element_readers
.push_back(
126 new content::UploadFileSystemFileElementReader(
128 element
.filesystem_url(),
131 element
.expected_modification_time()));
133 case ResourceRequestBody::Element::TYPE_BLOB
:
134 // Blob elements should be resolved beforehand.
135 // TODO(dmurph): Create blob reader and store the snapshot in there.
138 case ResourceRequestBody::Element::TYPE_UNKNOWN
:
144 return make_scoped_ptr(
145 new net::ElementsUploadDataStream(element_readers
.Pass(),
146 body
->identifier()));
149 } // namespace content