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 { | |
mmenke
2015/06/17 19:11:15
We have a short comment on the others. Worth comm
gavinp
2015/06/18 18:50:54
Done. Comment recapitulates the next line of code
| |
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_; | |
mmenke
2015/06/17 19:11:15
Need to include base/ref_counted.h (Pre-existing p
gavinp
2015/06/18 18:50:54
Done.
| |
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 | |
mmenke
2015/06/17 19:11:15
optional nit: I've seen senior people discourage
gavinp
2015/06/18 18:50:54
Done. I agree with those people discouraging this.
| |
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; | |
mmenke
2015/06/17 19:11:15
Old issue, but should include <vector>
gavinp
2015/06/18 18:50:54
Done.
| |
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 if (element.type() != |
107 resolved_elements.push_back(&element); | 141 ResourceRequestBody::Element::TYPE_DISK_CACHE_ENTRY) { |
142 resolved_elements.push_back(std::make_pair(&element, nullptr)); | |
143 } else { | |
144 NOTREACHED(); | |
145 } | |
108 } | 146 } |
109 | 147 |
110 ScopedVector<net::UploadElementReader> element_readers; | 148 ScopedVector<net::UploadElementReader> element_readers; |
111 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 149 for (const auto& element_and_blob_item_pair : resolved_elements) { |
112 const ResourceRequestBody::Element& element = *resolved_elements[i]; | 150 const ResourceRequestBody::Element& element = |
151 *element_and_blob_item_pair.first; | |
113 switch (element.type()) { | 152 switch (element.type()) { |
114 case ResourceRequestBody::Element::TYPE_BYTES: | 153 case ResourceRequestBody::Element::TYPE_BYTES: |
115 element_readers.push_back(new BytesElementReader(body, element)); | 154 element_readers.push_back(new BytesElementReader(body, element)); |
116 break; | 155 break; |
117 case ResourceRequestBody::Element::TYPE_FILE: | 156 case ResourceRequestBody::Element::TYPE_FILE: |
118 element_readers.push_back( | 157 element_readers.push_back( |
119 new FileElementReader(body, file_task_runner, element)); | 158 new FileElementReader(body, file_task_runner, element)); |
120 break; | 159 break; |
121 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: | 160 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: |
122 // If |body| contains any filesystem URLs, the caller should have | 161 // If |body| contains any filesystem URLs, the caller should have |
123 // supplied a FileSystemContext. | 162 // supplied a FileSystemContext. |
124 DCHECK(file_system_context); | 163 DCHECK(file_system_context); |
125 element_readers.push_back( | 164 element_readers.push_back( |
126 new content::UploadFileSystemFileElementReader( | 165 new content::UploadFileSystemFileElementReader( |
127 file_system_context, | 166 file_system_context, |
128 element.filesystem_url(), | 167 element.filesystem_url(), |
129 element.offset(), | 168 element.offset(), |
130 element.length(), | 169 element.length(), |
131 element.expected_modification_time())); | 170 element.expected_modification_time())); |
132 break; | 171 break; |
133 case ResourceRequestBody::Element::TYPE_BLOB: | 172 case ResourceRequestBody::Element::TYPE_BLOB: |
134 // Blob elements should be resolved beforehand. | 173 // Blob elements should be resolved beforehand. |
135 // TODO(dmurph): Create blob reader and store the snapshot in there. | 174 // TODO(dmurph): Create blob reader and store the snapshot in there. |
136 NOTREACHED(); | 175 NOTREACHED(); |
137 break; | 176 break; |
177 case ResourceRequestBody::Element::TYPE_DISK_CACHE_ENTRY: { | |
mmenke
2015/06/17 19:11:15
There's a notreached for this case in the first lo
gavinp
2015/06/18 18:50:54
Yes. There was a long chat between me and michaeln
mmenke
2015/06/18 20:49:05
Ah...yea. Just unexpected that a blob is both an
| |
178 // TODO(gavinp): If Build() is called with a DataElement of | |
179 // TYPE_DISK_CACHE_ENTRY then this code won't work because we won't call | |
180 // ResolveBlobReference() and so we won't find |item|. Is this OK? | |
181 const storage::BlobDataItem* item = element_and_blob_item_pair.second; | |
mmenke
2015/06/17 19:11:16
element_and_blob_item_pair.second is never populat
| |
182 element_readers.push_back( | |
183 new DiskCacheElementReader(body, item->disk_cache_entry(), | |
184 item->disk_cache_stream_index(), | |
185 element)); | |
186 break; | |
187 } | |
mmenke
2015/06/17 19:11:15
nit: -2 indent for the close brace.
gavinp
2015/06/18 18:50:54
Done.
| |
138 case ResourceRequestBody::Element::TYPE_UNKNOWN: | 188 case ResourceRequestBody::Element::TYPE_UNKNOWN: |
139 NOTREACHED(); | 189 NOTREACHED(); |
140 break; | 190 break; |
141 } | 191 } |
142 } | 192 } |
143 | 193 |
144 return make_scoped_ptr( | 194 return make_scoped_ptr( |
145 new net::ElementsUploadDataStream(element_readers.Pass(), | 195 new net::ElementsUploadDataStream(element_readers.Pass(), |
146 body->identifier())); | 196 body->identifier())); |
147 } | 197 } |
148 | 198 |
149 } // namespace content | 199 } // namespace content |
OLD | NEW |