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