OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/loader/upload_data_stream_builder.h" | 5 #include "content/browser/loader/upload_data_stream_builder.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "content/browser/fileapi/upload_file_system_file_element_reader.h" | 10 #include "content/browser/fileapi/upload_file_system_file_element_reader.h" |
9 #include "content/common/resource_request_body.h" | 11 #include "content/common/resource_request_body.h" |
10 #include "net/base/elements_upload_data_stream.h" | 12 #include "net/base/elements_upload_data_stream.h" |
11 #include "net/base/upload_bytes_element_reader.h" | 13 #include "net/base/upload_bytes_element_reader.h" |
| 14 #include "net/base/upload_disk_cache_entry_element_reader.h" |
12 #include "net/base/upload_file_element_reader.h" | 15 #include "net/base/upload_file_element_reader.h" |
13 #include "storage/browser/blob/blob_data_handle.h" | 16 #include "storage/browser/blob/blob_data_handle.h" |
14 #include "storage/browser/blob/blob_data_snapshot.h" | 17 #include "storage/browser/blob/blob_data_snapshot.h" |
15 #include "storage/browser/blob/blob_storage_context.h" | 18 #include "storage/browser/blob/blob_storage_context.h" |
16 | 19 |
| 20 namespace disk_cache { |
| 21 class Entry; |
| 22 } |
| 23 |
17 namespace content { | 24 namespace content { |
18 namespace { | 25 namespace { |
19 | 26 |
20 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. | 27 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. |
21 class BytesElementReader : public net::UploadBytesElementReader { | 28 class BytesElementReader : public net::UploadBytesElementReader { |
22 public: | 29 public: |
23 BytesElementReader(ResourceRequestBody* resource_request_body, | 30 BytesElementReader(ResourceRequestBody* resource_request_body, |
24 const ResourceRequestBody::Element& element) | 31 const ResourceRequestBody::Element& element) |
25 : net::UploadBytesElementReader(element.bytes(), element.length()), | 32 : net::UploadBytesElementReader(element.bytes(), element.length()), |
26 resource_request_body_(resource_request_body) { | 33 resource_request_body_(resource_request_body) { |
(...skipping 26 matching lines...) Expand all Loading... |
53 } | 60 } |
54 | 61 |
55 ~FileElementReader() override {} | 62 ~FileElementReader() override {} |
56 | 63 |
57 private: | 64 private: |
58 scoped_refptr<ResourceRequestBody> resource_request_body_; | 65 scoped_refptr<ResourceRequestBody> resource_request_body_; |
59 | 66 |
60 DISALLOW_COPY_AND_ASSIGN(FileElementReader); | 67 DISALLOW_COPY_AND_ASSIGN(FileElementReader); |
61 }; | 68 }; |
62 | 69 |
| 70 class DiskCacheElementReader : public net::UploadDiskCacheEntryElementReader { |
| 71 public: |
| 72 DiskCacheElementReader(ResourceRequestBody* resource_request_body, |
| 73 disk_cache::Entry* disk_cache_entry, |
| 74 int disk_cache_stream_index, |
| 75 const ResourceRequestBody::Element& element) |
| 76 : net::UploadDiskCacheEntryElementReader(disk_cache_entry, |
| 77 disk_cache_stream_index, |
| 78 element.offset(), |
| 79 element.length()), |
| 80 resource_request_body_(resource_request_body) { |
| 81 DCHECK_EQ(ResourceRequestBody::Element::TYPE_DISK_CACHE_ENTRY, |
| 82 element.type()); |
| 83 } |
| 84 |
| 85 ~DiskCacheElementReader() override {} |
| 86 |
| 87 private: |
| 88 scoped_refptr<ResourceRequestBody> resource_request_body_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(DiskCacheElementReader); |
| 91 }; |
| 92 |
63 void ResolveBlobReference( | 93 void ResolveBlobReference( |
64 ResourceRequestBody* body, | 94 ResourceRequestBody* body, |
65 storage::BlobStorageContext* blob_context, | 95 storage::BlobStorageContext* blob_context, |
66 const ResourceRequestBody::Element& element, | 96 const ResourceRequestBody::Element& element, |
67 std::vector<const ResourceRequestBody::Element*>* resolved_elements) { | 97 std::vector<std::pair<const ResourceRequestBody::Element*, |
| 98 const storage::BlobDataItem*>>* resolved_elements) { |
68 DCHECK(blob_context); | 99 DCHECK(blob_context); |
69 scoped_ptr<storage::BlobDataHandle> handle = | 100 scoped_ptr<storage::BlobDataHandle> handle = |
70 blob_context->GetBlobDataFromUUID(element.blob_uuid()); | 101 blob_context->GetBlobDataFromUUID(element.blob_uuid()); |
71 DCHECK(handle); | 102 DCHECK(handle); |
72 if (!handle) | 103 if (!handle) |
73 return; | 104 return; |
74 | 105 |
75 // TODO(dmurph): Create a reader for blobs instead of decomposing the blob | 106 // 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. | 107 // 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 | 108 // 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. | 109 // 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(); | 110 scoped_ptr<storage::BlobDataSnapshot> snapshot = handle->CreateSnapshot(); |
80 // If there is no element in the referred blob data, just return. | 111 // If there is no element in the referred blob data, just return. |
81 if (snapshot->items().empty()) | 112 if (snapshot->items().empty()) |
82 return; | 113 return; |
83 | 114 |
84 // Append the elements in the referenced blob data. | 115 // Append the elements in the referenced blob data. |
85 for (const auto& item : snapshot->items()) { | 116 for (const auto& item : snapshot->items()) { |
86 DCHECK_NE(storage::DataElement::TYPE_BLOB, item->type()); | 117 DCHECK_NE(storage::DataElement::TYPE_BLOB, item->type()); |
87 resolved_elements->push_back(item->data_element_ptr()); | 118 resolved_elements->push_back( |
| 119 std::make_pair(item->data_element_ptr(), item.get())); |
88 } | 120 } |
89 const void* key = snapshot.get(); | 121 const void* key = snapshot.get(); |
90 body->SetUserData(key, snapshot.release()); | 122 body->SetUserData(key, snapshot.release()); |
91 } | 123 } |
92 | 124 |
93 } // namespace | 125 } // namespace |
94 | 126 |
95 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( | 127 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( |
96 ResourceRequestBody* body, | 128 ResourceRequestBody* body, |
97 storage::BlobStorageContext* blob_context, | 129 storage::BlobStorageContext* blob_context, |
98 storage::FileSystemContext* file_system_context, | 130 storage::FileSystemContext* file_system_context, |
99 base::TaskRunner* file_task_runner) { | 131 base::TaskRunner* file_task_runner) { |
| 132 |
100 // Resolve all blob elements. | 133 // Resolve all blob elements. |
101 std::vector<const ResourceRequestBody::Element*> resolved_elements; | 134 std::vector<std::pair<const ResourceRequestBody::Element*, |
| 135 const storage::BlobDataItem*>> resolved_elements; |
102 for (size_t i = 0; i < body->elements()->size(); ++i) { | 136 for (size_t i = 0; i < body->elements()->size(); ++i) { |
103 const ResourceRequestBody::Element& element = (*body->elements())[i]; | 137 const ResourceRequestBody::Element& element = (*body->elements())[i]; |
104 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) | 138 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) |
105 ResolveBlobReference(body, blob_context, element, &resolved_elements); | 139 ResolveBlobReference(body, blob_context, element, &resolved_elements); |
106 else | 140 else |
107 resolved_elements.push_back(&element); | 141 resolved_elements.push_back(std::make_pair(&element, nullptr)); |
108 } | 142 } |
109 | 143 |
110 ScopedVector<net::UploadElementReader> element_readers; | 144 ScopedVector<net::UploadElementReader> element_readers; |
111 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 145 for (const auto& element_and_blob_item_pair : resolved_elements) { |
112 const ResourceRequestBody::Element& element = *resolved_elements[i]; | 146 const ResourceRequestBody::Element& element = |
| 147 *element_and_blob_item_pair.first; |
113 switch (element.type()) { | 148 switch (element.type()) { |
114 case ResourceRequestBody::Element::TYPE_BYTES: | 149 case ResourceRequestBody::Element::TYPE_BYTES: |
115 element_readers.push_back(new BytesElementReader(body, element)); | 150 element_readers.push_back(new BytesElementReader(body, element)); |
116 break; | 151 break; |
117 case ResourceRequestBody::Element::TYPE_FILE: | 152 case ResourceRequestBody::Element::TYPE_FILE: |
118 element_readers.push_back( | 153 element_readers.push_back( |
119 new FileElementReader(body, file_task_runner, element)); | 154 new FileElementReader(body, file_task_runner, element)); |
120 break; | 155 break; |
121 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: | 156 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: |
122 // If |body| contains any filesystem URLs, the caller should have | 157 // If |body| contains any filesystem URLs, the caller should have |
123 // supplied a FileSystemContext. | 158 // supplied a FileSystemContext. |
124 DCHECK(file_system_context); | 159 DCHECK(file_system_context); |
125 element_readers.push_back( | 160 element_readers.push_back( |
126 new content::UploadFileSystemFileElementReader( | 161 new content::UploadFileSystemFileElementReader( |
127 file_system_context, | 162 file_system_context, |
128 element.filesystem_url(), | 163 element.filesystem_url(), |
129 element.offset(), | 164 element.offset(), |
130 element.length(), | 165 element.length(), |
131 element.expected_modification_time())); | 166 element.expected_modification_time())); |
132 break; | 167 break; |
133 case ResourceRequestBody::Element::TYPE_BLOB: | 168 case ResourceRequestBody::Element::TYPE_BLOB: |
134 // Blob elements should be resolved beforehand. | 169 // Blob elements should be resolved beforehand. |
135 // TODO(dmurph): Create blob reader and store the snapshot in there. | 170 // TODO(dmurph): Create blob reader and store the snapshot in there. |
136 NOTREACHED(); | 171 NOTREACHED(); |
137 break; | 172 break; |
| 173 case ResourceRequestBody::Element::TYPE_DISK_CACHE_ENTRY: { |
| 174 // TODO(gavinp): If Build() is called with a DataElement of |
| 175 // TYPE_DISK_CACHE_ENTRY then this code won't work because we won't call |
| 176 // ResolveBlobReference() and so we won't find |item|. Is this OK? |
| 177 const storage::BlobDataItem* item = element_and_blob_item_pair.second; |
| 178 element_readers.push_back( |
| 179 new DiskCacheElementReader(body, item->disk_cache_entry(), |
| 180 item->disk_cache_stream_index(), |
| 181 element)); |
| 182 break; |
| 183 } |
138 case ResourceRequestBody::Element::TYPE_UNKNOWN: | 184 case ResourceRequestBody::Element::TYPE_UNKNOWN: |
139 NOTREACHED(); | 185 NOTREACHED(); |
140 break; | 186 break; |
141 } | 187 } |
142 } | 188 } |
143 | 189 |
144 return make_scoped_ptr( | 190 return make_scoped_ptr( |
145 new net::ElementsUploadDataStream(element_readers.Pass(), | 191 new net::ElementsUploadDataStream(element_readers.Pass(), |
146 body->identifier())); | 192 body->identifier())); |
147 } | 193 } |
148 | 194 |
149 } // namespace content | 195 } // namespace content |
OLD | NEW |